]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Unify window and tab title
authorDavid Hallas <david@davidhallas.dk>
Sat, 28 Jul 2018 15:29:00 +0000 (17:29 +0200)
committerDavid Hallas <david@davidhallas.dk>
Fri, 24 Aug 2018 10:09:26 +0000 (12:09 +0200)
Summary:
Previously the title of tabs was a prettyfied version of the URL.
This is inconsistent with the title of the Window and in some cases
for specials URLs kind of misleading. This commit generalizes the
code from DolphinMainWindow so that both the DolphinMainWindow title
and the tab title uses the same function for the title. This also
means that the 'Show Full Path in Title Bar' also applies to the
tab title, and also that searches changes the tab title.

FEATURE: 387851

Test Plan: Open a new tab from the places panel and navigate around.

Reviewers: #dolphin, ngraham, elvisangelaccio, markg

Reviewed By: #dolphin, ngraham, markg

Subscribers: markg, kfm-devel

Tags: #dolphin

Differential Revision: https://phabricator.kde.org/D14442

src/dolphinmainwindow.cpp
src/dolphinmainwindow.h
src/dolphintabwidget.cpp
src/dolphintabwidget.h
src/dolphinviewcontainer.cpp
src/dolphinviewcontainer.h

index 3b905b4d355cf865ffb99ec347708cb4eadfdf4d..23894f3466910f616ecaa28ab78482849d2ec5e3 100644 (file)
@@ -25,7 +25,6 @@
 #include "dolphindockwidget.h"
 #include "dolphincontextmenu.h"
 #include "dolphinnewfilemenu.h"
-#include "dolphinplacesmodelsingleton.h"
 #include "dolphinrecenttabsmenu.h"
 #include "dolphintabwidget.h"
 #include "dolphinviewcontainer.h"
@@ -49,7 +48,6 @@
 #include <KAuthorized>
 #include <KConfig>
 #include <KFileItemListProperties>
-#include <KFilePlacesModel>
 #include <KHelpMenu>
 #include <KIO/JobUiDelegate>
 #include <KIO/OpenFileManagerWindowJob>
@@ -137,7 +135,7 @@ DolphinMainWindow::DolphinMainWindow() :
     connect(m_tabWidget, &DolphinTabWidget::tabCountChanged,
             this, &DolphinMainWindow::tabCountChanged);
     connect(m_tabWidget, &DolphinTabWidget::currentUrlChanged,
-            this, &DolphinMainWindow::setUrlAsCaption);
+            this, &DolphinMainWindow::updateWindowTitle);
     setCentralWidget(m_tabWidget);
 
     setupActions();
@@ -1001,46 +999,9 @@ void DolphinMainWindow::tabCountChanged(int count)
     actionCollection()->action(QStringLiteral("activate_prev_tab"))->setEnabled(enableTabActions);
 }
 
-void DolphinMainWindow::setUrlAsCaption(const QUrl& url)
+void DolphinMainWindow::updateWindowTitle()
 {
-    QString schemePrefix;
-    if (!url.isLocalFile()) {
-        schemePrefix.append(url.scheme() + " - ");
-        if (!url.host().isEmpty()) {
-            schemePrefix.append(url.host() + " - ");
-        }
-    }
-
-    if (GeneralSettings::showFullPathInTitlebar()) {
-        const QString path = url.adjusted(QUrl::StripTrailingSlash).path();
-        setWindowTitle(schemePrefix + path);
-        return;
-    }
-
-    KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
-    const auto& matchedPlaces = placesModel->match(placesModel->index(0,0), KFilePlacesModel::UrlRole, url, 1, Qt::MatchExactly);
-
-    if (!matchedPlaces.isEmpty()) {
-        setWindowTitle(placesModel->text(matchedPlaces.first()));
-        return;
-    }
-
-    QString fileName = url.adjusted(QUrl::StripTrailingSlash).fileName();
-    if (fileName.isEmpty()) {
-        fileName = '/';
-    }
-
-    if (m_activeViewContainer->isSearchModeEnabled()) {
-        if(m_activeViewContainer->currentSearchText().isEmpty()){
-            setWindowTitle(i18n("Search"));
-        } else {
-            const auto searchText = i18n("Search for %1", m_activeViewContainer->currentSearchText());
-            setWindowTitle(searchText);
-        }
-        return;
-    }
-
-    setWindowTitle(schemePrefix + fileName);
+    setWindowTitle(m_activeViewContainer->getCaption());
 }
 
 void DolphinMainWindow::slotStorageTearDownFromPlacesRequested(const QString& mountPath)
@@ -1506,7 +1467,7 @@ void DolphinMainWindow::refreshViews()
         const bool splitView = GeneralSettings::splitView();
         m_tabWidget->currentTabPage()->setSplitViewEnabled(splitView);
         updateSplitAction();
-        setUrlAsCaption(activeViewContainer()->url());
+        updateWindowTitle();
     }
 
     emit settingsChanged();
index 637f41f2108e2d143abc2beeadb6cd9b6ff0c624..f2876a8770d240e214660a3b713aeff151f13cfe 100644 (file)
@@ -412,10 +412,9 @@ private slots:
     void tabCountChanged(int count);
 
     /**
-     * Sets the window caption to url.fileName() if this is non-empty,
-     * "/" if the URL is "file:///", and url.protocol() otherwise.
+     * Updates the Window Title with the caption from the active view container
      */
-    void setUrlAsCaption(const QUrl& url);
+    void updateWindowTitle();
 
     /**
      * This slot is called when the user requested to unmount a removable media
index a5c2f8c98c77e48dd72d02aaec36f5bf92838483..600974f561ce8272d2fbb7065fc61e398a609a8b 100644 (file)
@@ -116,6 +116,7 @@ void DolphinTabWidget::refreshViews()
 {
     const int tabCount = count();
     for (int i = 0; i < tabCount; ++i) {
+        tabBar()->setTabText(i, tabName());
         tabPageAt(i)->refreshViews();
     }
 }
@@ -160,7 +161,7 @@ void DolphinTabWidget::openNewTab(const QUrl& primaryUrl, const QUrl& secondaryU
             this, &DolphinTabWidget::activeViewChanged);
     connect(tabPage, &DolphinTabPage::activeViewUrlChanged,
             this, &DolphinTabWidget::tabUrlChanged);
-    addTab(tabPage, QIcon::fromTheme(KIO::iconNameForUrl(primaryUrl)), tabName(primaryUrl));
+    addTab(tabPage, QIcon::fromTheme(KIO::iconNameForUrl(primaryUrl)), tabName());
 
     if (focusWidget) {
         // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
@@ -305,7 +306,7 @@ void DolphinTabWidget::tabUrlChanged(const QUrl& url)
 {
     const int index = indexOf(qobject_cast<QWidget*>(sender()));
     if (index >= 0) {
-        tabBar()->setTabText(index, tabName(url));
+        tabBar()->setTabText(index, tabName());
         tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url)));
 
         // Emit the currentUrlChanged signal if the url of the current tab has been changed.
@@ -353,20 +354,13 @@ void DolphinTabWidget::tabRemoved(int index)
     emit tabCountChanged(count());
 }
 
-QString DolphinTabWidget::tabName(const QUrl& url) const
+QString DolphinTabWidget::tabName() const
 {
-    QString name;
-    if (url == QUrl(QStringLiteral("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('&', QLatin1String("&&"));
-        }
+    if (currentTabPage() == nullptr) {
+        return QString();
     }
-    return name;
+    QString name = currentTabPage()->activeViewContainer()->getCaption();
+    // 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("&&"));
 }
index b4493f7ed82e00e1a82270b0cf9d3e48fe8eaf1c..d781953ddb9f96f0e0604b52a43c401f30ca5a64 100644 (file)
@@ -193,7 +193,7 @@ private:
     /**
      * Returns the name of the tab for the URL \a url.
      */
-    QString tabName(const QUrl& url) const;
+    QString tabName() const;
 
 private:
     /** Caches the (negated) places panel visibility */
index f473752dc11af6de163e80cfcb443a20d28fd977..fc3090183bf14076c8b2ad7097857a9c7c894b4e 100644 (file)
@@ -31,6 +31,7 @@
 #include "views/viewproperties.h"
 
 #include <KFileItemActions>
+#include <KFilePlacesModel>
 #include <KIO/PreviewJob>
 #include <KLocalizedString>
 #include <KMessageWidget>
@@ -409,6 +410,52 @@ void DolphinViewContainer::reload()
     m_messageWidget->hide();
 }
 
+QString DolphinViewContainer::getCaption() const
+{
+    if (GeneralSettings::showFullPathInTitlebar()) {
+        if (!url().isLocalFile()) {
+            return url().adjusted(QUrl::StripTrailingSlash).toString();
+        }
+        return url().adjusted(QUrl::StripTrailingSlash).path();
+    }
+
+    KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
+    const auto& matchedPlaces = placesModel->match(placesModel->index(0,0), KFilePlacesModel::UrlRole, url(), 1, Qt::MatchExactly);
+
+    if (!matchedPlaces.isEmpty()) {
+        return placesModel->text(matchedPlaces.first());
+    }
+    if (!url().isLocalFile()) {
+        QUrl adjustedUrl = url().adjusted(QUrl::StripTrailingSlash);
+        QString caption;
+        if (!adjustedUrl.fileName().isEmpty()) {
+            caption = adjustedUrl.fileName();
+        } else if (!adjustedUrl.path().isEmpty() && adjustedUrl.path() != "/") {
+            caption = adjustedUrl.path();
+        } else if (!adjustedUrl.host().isEmpty()) {
+            caption = adjustedUrl.host();
+        } else {
+            caption = adjustedUrl.toString();
+        }
+        return caption;
+    }
+
+    QString fileName = url().adjusted(QUrl::StripTrailingSlash).fileName();
+    if (fileName.isEmpty()) {
+        fileName = '/';
+    }
+
+    if (isSearchModeEnabled()) {
+        if(currentSearchText().isEmpty()){
+            return i18n("Search");
+        } else {
+            return i18n("Search for %1", currentSearchText());
+        }
+    }
+
+    return fileName;
+}
+
 void DolphinViewContainer::setUrl(const QUrl& newUrl)
 {
     if (newUrl != m_urlNavigator->locationUrl()) {
index b5bcc78ad344c55ddc0c1720cec0a1b887085851..5d8a347140983daf3b0d3c171c4fb954941bb591 100644 (file)
@@ -136,6 +136,13 @@ public:
      */
     void reload();
 
+    /**
+     * @return Returns a Caption suitable for display to the user. It is
+     * calculated depending on settings, if a search is active and other
+     * factors.
+     */
+    QString getCaption() const;
+
 public slots:
     /**
      * Sets the current active URL, where all actions are applied. The