X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/727d2513c0a8e7b3e6370f5e08b0d7579ae89564..a6403716439ae72bfdf86a14af94ea9d212f08ee:/src/dolphintabwidget.cpp diff --git a/src/dolphintabwidget.cpp b/src/dolphintabwidget.cpp index 0b1f07e0e..7928c510e 100644 --- a/src/dolphintabwidget.cpp +++ b/src/dolphintabwidget.cpp @@ -19,32 +19,36 @@ #include "dolphintabwidget.h" +#include "dolphin_generalsettings.h" #include "dolphintabbar.h" #include "dolphintabpage.h" #include "dolphinviewcontainer.h" -#include #include +#include #include #include -#include + +#include +#include DolphinTabWidget::DolphinTabWidget(QWidget* parent) : QTabWidget(parent), - m_placesSelectorVisible(true) + m_placesSelectorVisible(true), + m_lastViewedTab(0) { - connect(this, SIGNAL(tabCloseRequested(int)), - this, SLOT(closeTab(int))); - connect(this, SIGNAL(currentChanged(int)), - this, SLOT(currentTabChanged(int))); + connect(this, &DolphinTabWidget::tabCloseRequested, + this, QOverload::of(&DolphinTabWidget::closeTab)); + connect(this, &DolphinTabWidget::currentChanged, + this, &DolphinTabWidget::currentTabChanged); DolphinTabBar* tabBar = new DolphinTabBar(this); - connect(tabBar, SIGNAL(openNewActivatedTab(int)), - this, SLOT(openNewActivatedTab(int))); - connect(tabBar, SIGNAL(tabDropEvent(int,QDropEvent*)), - this, SLOT(tabDropEvent(int,QDropEvent*))); - connect(tabBar, SIGNAL(tabDetachRequested(int)), - this, SLOT(detachTab(int))); + connect(tabBar, &DolphinTabBar::openNewActivatedTab, + this, QOverload::of(&DolphinTabWidget::openNewActivatedTab)); + connect(tabBar, &DolphinTabBar::tabDropEvent, + this, &DolphinTabWidget::tabDropEvent); + connect(tabBar, &DolphinTabBar::tabDetachRequested, + this, &DolphinTabWidget::detachTab); tabBar->hide(); setTabBar(tabBar); @@ -58,6 +62,18 @@ DolphinTabPage* DolphinTabWidget::currentTabPage() const return tabPageAt(currentIndex()); } +DolphinTabPage* DolphinTabWidget::nextTabPage() const +{ + const int index = currentIndex() + 1; + return tabPageAt(index < count() ? index : 0); +} + +DolphinTabPage* DolphinTabWidget::prevTabPage() const +{ + const int index = currentIndex() - 1; + return tabPageAt(index >= 0 ? index : (count() - 1)); +} + DolphinTabPage* DolphinTabWidget::tabPageAt(const int index) const { return static_cast(widget(index)); @@ -99,8 +115,17 @@ void DolphinTabWidget::readProperties(const KConfigGroup& group) void DolphinTabWidget::refreshViews() { + // Left-elision is better when showing full paths, since you care most + // about the current directory which is on the right + if (GeneralSettings::showFullPathInTitlebar()) { + setElideMode(Qt::ElideLeft); + } else { + setElideMode(Qt::ElideRight); + } + const int tabCount = count(); for (int i = 0; i < tabCount; ++i) { + tabBar()->setTabText(i, tabName(tabPageAt(i))); tabPageAt(i)->refreshViews(); } } @@ -119,14 +144,10 @@ void DolphinTabWidget::openNewActivatedTab() // The URL navigator of the new tab should have the same editable state // as the current tab - KUrlNavigator* navigator = newActiveViewContainer->urlNavigator(); - navigator->setUrlEditable(isUrlEditable); + newActiveViewContainer->urlNavigator()->setUrlEditable(isUrlEditable); - if (isUrlEditable) { - // If a new tab is opened and the URL is editable, assure that - // the user can edit the URL without manually setting the focus - navigator->setFocus(); - } + // Always focus the new tab's view + newActiveViewContainer->view()->setFocus(); } void DolphinTabWidget::openNewActivatedTab(const QUrl& primaryUrl, const QUrl& secondaryUrl) @@ -135,17 +156,21 @@ void DolphinTabWidget::openNewActivatedTab(const QUrl& primaryUrl, const QUrl& s setCurrentIndex(count() - 1); } -void DolphinTabWidget::openNewTab(const QUrl& primaryUrl, const QUrl& secondaryUrl) +void DolphinTabWidget::openNewTab(const QUrl& primaryUrl, const QUrl& secondaryUrl, TabPlacement tabPlacement) { QWidget* focusWidget = QApplication::focusWidget(); DolphinTabPage* tabPage = new DolphinTabPage(primaryUrl, secondaryUrl, this); tabPage->setPlacesSelectorVisible(m_placesSelectorVisible); - connect(tabPage, SIGNAL(activeViewChanged(DolphinViewContainer*)), - this, SIGNAL(activeViewChanged(DolphinViewContainer*))); - connect(tabPage, SIGNAL(activeViewUrlChanged(QUrl)), - this, SLOT(tabUrlChanged(QUrl))); - addTab(tabPage, QIcon::fromTheme(KIO::iconNameForUrl(primaryUrl)), tabName(primaryUrl)); + connect(tabPage, &DolphinTabPage::activeViewChanged, + this, &DolphinTabWidget::activeViewChanged); + connect(tabPage, &DolphinTabPage::activeViewUrlChanged, + this, &DolphinTabWidget::tabUrlChanged); + int newTabIndex = -1; + if (tabPlacement == AfterCurrentTab) { + newTabIndex = currentIndex() + 1; + } + insertTab(newTabIndex, tabPage, QIcon::fromTheme(KIO::iconNameForUrl(primaryUrl)), tabName(tabPage)); if (focusWidget) { // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened @@ -161,11 +186,22 @@ void DolphinTabWidget::openDirectories(const QList& dirs, bool splitView) QList::const_iterator it = dirs.constBegin(); while (it != dirs.constEnd()) { const QUrl& primaryUrl = *(it++); + const QPair viewLocation = getIndexByUrl(primaryUrl); + if (viewLocation.first >= 0) { + setCurrentIndex(viewLocation.first); + const auto tabPage = tabPageAt(viewLocation.first); + if (viewLocation.second) { + tabPage->primaryViewContainer()->setActive(true); + } else { + tabPage->secondaryViewContainer()->setActive(true); + } + continue; + } if (splitView && (it != dirs.constEnd())) { const QUrl& secondaryUrl = *(it++); - openNewTab(primaryUrl, secondaryUrl); + openNewActivatedTab(primaryUrl, secondaryUrl); } else { - openNewTab(primaryUrl); + openNewActivatedTab(primaryUrl); } } } @@ -210,7 +246,8 @@ void DolphinTabWidget::closeTab(const int index) Q_ASSERT(index < count()); if (count() < 2) { - // Never close the last tab. + // Close Dolphin when closing the last tab. + parentWidget()->close(); return; } @@ -264,6 +301,7 @@ void DolphinTabWidget::detachTab(int index) args << tabPage->secondaryViewContainer()->url().url(); args << QStringLiteral("--split"); } + args << QStringLiteral("--new-window"); const QString command = QStringLiteral("dolphin %1").arg(KShell::joinArgs(args)); KRun::runCommand(command, this); @@ -282,7 +320,7 @@ void DolphinTabWidget::tabDropEvent(int index, QDropEvent* event) { if (index >= 0) { DolphinView* view = tabPageAt(index)->activeViewContainer()->view(); - view->dropUrls(view->url(), event); + view->dropUrls(view->url(), event, view); } } @@ -290,7 +328,7 @@ void DolphinTabWidget::tabUrlChanged(const QUrl& url) { const int index = indexOf(qobject_cast(sender())); if (index >= 0) { - tabBar()->setTabText(index, tabName(url)); + tabBar()->setTabText(index, tabName(tabPageAt(index))); tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url))); // Emit the currentUrlChanged signal if the url of the current tab has been changed. @@ -302,10 +340,16 @@ void DolphinTabWidget::tabUrlChanged(const QUrl& url) void DolphinTabWidget::currentTabChanged(int index) { - DolphinViewContainer* viewContainer = tabPageAt(index)->activeViewContainer(); + // last-viewed tab deactivation + if (DolphinTabPage* tabPage = tabPageAt(m_lastViewedTab)) { + tabPage->setActive(false); + } + DolphinTabPage* tabPage = tabPageAt(index); + DolphinViewContainer* viewContainer = tabPage->activeViewContainer(); emit activeViewChanged(viewContainer); emit currentUrlChanged(viewContainer->url()); - viewContainer->view()->setFocus(); + tabPage->setActive(true); + m_lastViewedTab = index; } void DolphinTabWidget::tabInserted(int index) @@ -332,20 +376,28 @@ void DolphinTabWidget::tabRemoved(int index) emit tabCountChanged(count()); } -QString DolphinTabWidget::tabName(const QUrl& url) const +QString DolphinTabWidget::tabName(DolphinTabPage* tabPage) const { - QString name; - if (url == QUrl("file:///")) { - name = '/'; - } else { - name = url.adjusted(QUrl::StripTrailingSlash).fileName(); - if (name.isEmpty()) { - name = url.scheme(); - } else { - // Make sure that a '&' inside the directory name is displayed correctly - // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText() - name.replace('&', "&&"); + if (!tabPage) { + return QString(); + } + QString name = tabPage->activeViewContainer()->caption(); + // Make sure that a '&' inside the directory name is displayed correctly + // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText() + return name.replace('&', QLatin1String("&&")); +} + +QPair DolphinTabWidget::getIndexByUrl(const QUrl& url) const +{ + for (int i = 0; i < count(); i++) { + const auto tabPage = tabPageAt(i); + if (url == tabPage->primaryViewContainer()->url()) { + return qMakePair(i, true); + } + + if (tabPage->splitViewEnabled() && url == tabPage->secondaryViewContainer()->url()) { + return qMakePair(i, false); } } - return name; + return qMakePair(-1, false); }