]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphintabwidget.cpp
DolphinTabWidget: Allow specifying new tab position in openNewTab
[dolphin.git] / src / dolphintabwidget.cpp
index eb3f741ee741be46e8a067313f95e62364c4925c..7eae6f297ec3ef4154c6b31abf8144dbe028248f 100644 (file)
@@ -8,7 +8,6 @@
 
 #include "dolphin_generalsettings.h"
 #include "dolphintabbar.h"
-#include "dolphintabpage.h"
 #include "dolphinviewcontainer.h"
 
 #include <KConfigGroup>
@@ -88,15 +87,8 @@ void DolphinTabWidget::readProperties(const KConfigGroup& group)
         if (i >= count()) {
             openNewActivatedTab();
         }
-        if (group.hasKey("Tab Data " % QString::number(i))) {
-            // Tab state created with Dolphin > 4.14.x
-            const QByteArray state = group.readEntry("Tab Data " % QString::number(i), QByteArray());
-            tabPageAt(i)->restoreState(state);
-        } else {
-            // Tab state created with Dolphin <= 4.14.x
-            const QByteArray state = group.readEntry("Tab " % QString::number(i), QByteArray());
-            tabPageAt(i)->restoreStateV1(state);
-        }
+        const QByteArray state = group.readEntry("Tab Data " % QString::number(i), QByteArray());
+        tabPageAt(i)->restoreState(state);
     }
 
     const int index = group.readEntry("Active Tab Index", 0);
@@ -125,6 +117,11 @@ bool DolphinTabWidget::isUrlOpen(const QUrl &url) const
     return indexByUrl(url).first >= 0;
 }
 
+bool DolphinTabWidget::isUrlOrParentOpen(const QUrl &url) const
+{
+    return indexByUrl(url, ReturnIndexForOpenedParentAlso).first >= 0;
+}
+
 void DolphinTabWidget::openNewActivatedTab()
 {
     std::unique_ptr<DolphinUrlNavigator::VisualState> oldNavigatorState;
@@ -153,10 +150,14 @@ void DolphinTabWidget::openNewActivatedTab()
 void DolphinTabWidget::openNewActivatedTab(const QUrl& primaryUrl, const QUrl& secondaryUrl)
 {
     openNewTab(primaryUrl, secondaryUrl);
-    setCurrentIndex(count() - 1);
+    if (GeneralSettings::openNewTabAfterLastTab()) {
+        setCurrentIndex(count() - 1);
+    } else {
+        setCurrentIndex(currentIndex() + 1);
+    }
 }
 
-void DolphinTabWidget::openNewTab(const QUrl& primaryUrl, const QUrl& secondaryUrl, TabPlacement tabPlacement)
+void DolphinTabWidget::openNewTab(const QUrl& primaryUrl, const QUrl& secondaryUrl, DolphinTabWidget::NewTabPosition position)
 {
     QWidget* focusWidget = QApplication::focusWidget();
 
@@ -166,10 +167,25 @@ void DolphinTabWidget::openNewTab(const QUrl& primaryUrl, const QUrl& secondaryU
             this, &DolphinTabWidget::activeViewChanged);
     connect(tabPage, &DolphinTabPage::activeViewUrlChanged,
             this, &DolphinTabWidget::tabUrlChanged);
+    connect(tabPage->activeViewContainer(), &DolphinViewContainer::captionChanged, this, [this, tabPage]() {
+        const int tabIndex = indexOf(tabPage);
+        Q_ASSERT(tabIndex >= 0);
+        tabBar()->setTabText(tabIndex, tabName(tabPage));
+    });
+
+    if (position == NewTabPosition::FollowSetting) {
+        if (GeneralSettings::openNewTabAfterLastTab()) {
+            position = NewTabPosition::AtEnd;
+        } else {
+            position = NewTabPosition::AfterCurrent;
+        }
+    }
+
     int newTabIndex = -1;
-    if (tabPlacement == AfterCurrentTab) {
+    if (position == NewTabPosition::AfterCurrent || (position == NewTabPosition::FollowSetting && !GeneralSettings::openNewTabAfterLastTab())) {
         newTabIndex = currentIndex() + 1;
     }
+
     insertTab(newTabIndex, tabPage, QIcon() /* loaded in tabInserted */, tabName(tabPage));
 
     if (focusWidget) {
@@ -179,18 +195,24 @@ void DolphinTabWidget::openNewTab(const QUrl& primaryUrl, const QUrl& secondaryU
     }
 }
 
-void DolphinTabWidget::openDirectories(const QList<QUrl>& dirs, bool splitView)
+void DolphinTabWidget::openDirectories(const QList<QUrl>& dirs, bool splitView, bool skipChildUrls)
 {
     Q_ASSERT(dirs.size() > 0);
 
+    bool somethingWasAlreadyOpen = false;
+
     QList<QUrl>::const_iterator it = dirs.constBegin();
     while (it != dirs.constEnd()) {
         const QUrl& primaryUrl = *(it++);
-        const QPair<int, bool> indexInfo = indexByUrl(primaryUrl);
+        const QPair<int, bool> indexInfo = indexByUrl(primaryUrl, skipChildUrls ? ReturnIndexForOpenedParentAlso : ReturnIndexForOpenedUrlOnly);
         const int index = indexInfo.first;
         const bool isInPrimaryView = indexInfo.second;
+
+        // When the user asks for a URL that's already open (or it's parent is open if skipChildUrls is set),
+        // activate it instead of opening a new tab
         if (index >= 0) {
-            setCurrentIndex(index);
+            somethingWasAlreadyOpen = true;
+            activateTab(index);
             const auto tabPage = tabPageAt(index);
             if (isInPrimaryView) {
                 tabPage->primaryViewContainer()->setActive(true);
@@ -201,13 +223,19 @@ void DolphinTabWidget::openDirectories(const QList<QUrl>& dirs, bool splitView)
             // Required for updateViewState() call in openFiles() to work as expected
             // If there is a selection, updateViewState() calls are effectively a no-op
             tabPage->activeViewContainer()->view()->clearSelection();
-            continue;
-        }
-        if (splitView && (it != dirs.constEnd())) {
+        } else if (splitView && (it != dirs.constEnd())) {
             const QUrl& secondaryUrl = *(it++);
-            openNewActivatedTab(primaryUrl, secondaryUrl);
+            if (somethingWasAlreadyOpen) {
+                openNewTab(primaryUrl, secondaryUrl);
+            } else {
+                openNewActivatedTab(primaryUrl, secondaryUrl);
+            }
         } else {
-            openNewActivatedTab(primaryUrl);
+            if (somethingWasAlreadyOpen) {
+                openNewTab(primaryUrl);
+            } else {
+                openNewActivatedTab(primaryUrl);
+            }
         }
     }
 }
@@ -221,14 +249,14 @@ void DolphinTabWidget::openFiles(const QList<QUrl>& files, bool splitView)
     // directories are shown inside one tab (see openDirectories()).
     QList<QUrl> dirs;
     for (const QUrl& url : files) {
-        const QUrl dir(url.adjusted(QUrl::RemoveFilename));
+        const QUrl dir(url.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash));
         if (!dirs.contains(dir)) {
             dirs.append(dir);
         }
     }
 
     const int oldTabCount = count();
-    openDirectories(dirs, splitView);
+    openDirectories(dirs, splitView, true);
     const int tabCount = count();
 
     // Select the files. Although the files can be split between several
@@ -375,7 +403,9 @@ void DolphinTabWidget::tabUrlChanged(const QUrl& url)
         tabBar()->setTabText(index, tabName(tabPageAt(index)));
         tabBar()->setTabToolTip(index, url.toDisplayString(QUrl::PreferLocalFile));
         if (tabBar()->isVisible()) {
-            tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url)));
+            // ensure the path url ends with a slash to have proper folder icon for remote folders
+            const QUrl pathUrl = QUrl(url.adjusted(QUrl::StripTrailingSlash).toString(QUrl::FullyEncoded).append("/"));
+            tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(pathUrl)));
         } else {
             // Mark as dirty, actually load once the tab bar actually gets shown
             tabBar()->setTabIcon(index, QIcon());
@@ -407,7 +437,7 @@ void DolphinTabWidget::currentTabChanged(int index)
     tabPage->setActive(true);
     tabPage->connectNavigators(m_navigatorsWidget);
     m_navigatorsWidget->setSecondaryNavigatorVisible(tabPage->splitViewEnabled());
-    m_lastViewedTab = tabPageAt(index);
+    m_lastViewedTab = tabPage;
 }
 
 void DolphinTabWidget::tabInserted(int index)
@@ -419,7 +449,9 @@ void DolphinTabWidget::tabInserted(int index)
         for (int i = 0; i < count(); ++i) {
             const QUrl url = tabPageAt(i)->activeViewContainer()->url();
             if (tabBar()->tabIcon(i).isNull()) {
-                tabBar()->setTabIcon(i, QIcon::fromTheme(KIO::iconNameForUrl(url)));
+                // ensure the path url ends with a slash to have proper folder icon for remote folders
+                const QUrl pathUrl = QUrl(url.adjusted(QUrl::StripTrailingSlash).toString(QUrl::FullyEncoded).append("/"));
+                tabBar()->setTabIcon(i, QIcon::fromTheme(KIO::iconNameForUrl(pathUrl)));
             }
             if (tabBar()->tabToolTip(i).isEmpty()) {
                 tabBar()->setTabToolTip(index, url.toDisplayString(QUrl::PreferLocalFile));
@@ -456,17 +488,29 @@ QString DolphinTabWidget::tabName(DolphinTabPage* tabPage) const
     return name.replace('&', QLatin1String("&&"));
 }
 
-QPair<int, bool> DolphinTabWidget::indexByUrl(const QUrl& url) const
+QPair<int, bool> DolphinTabWidget::indexByUrl(const QUrl& url, ChildUrlBehavior childUrlBehavior) const
 {
-    for (int i = 0; i < count(); i++) {
+    int i = currentIndex();
+    if (i < 0) {
+        return qMakePair(-1, false);
+    }
+    // loop over the tabs starting from the current one
+    do {
         const auto tabPage = tabPageAt(i);
-        if (url == tabPage->primaryViewContainer()->url()) {
+        if (tabPage->primaryViewContainer()->url() == url ||
+                (childUrlBehavior == ReturnIndexForOpenedParentAlso && tabPage->primaryViewContainer()->url().isParentOf(url))) {
             return qMakePair(i, true);
         }
 
-        if (tabPage->splitViewEnabled() && url == tabPage->secondaryViewContainer()->url()) {
+        if (tabPage->splitViewEnabled() &&
+                (url == tabPage->secondaryViewContainer()->url() ||
+                 (childUrlBehavior == ReturnIndexForOpenedParentAlso && tabPage->secondaryViewContainer()->url().isParentOf(url)))) {
             return qMakePair(i, false);
         }
+
+        i = (i + 1) % count();
     }
+    while (i != currentIndex());
+
     return qMakePair(-1, false);
 }