]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphintabwidget.cpp
Merge branch 'Applications/19.08'
[dolphin.git] / src / dolphintabwidget.cpp
index 42a8aff09e63c8a7d4f644cb06b7d12297b7e488..afb5462e1ad4fcdc9cff9fdd12a1b705d0c23ca4 100644 (file)
 
 #include "dolphintabwidget.h"
 
+#include "dolphin_generalsettings.h"
 #include "dolphintabbar.h"
 #include "dolphintabpage.h"
 #include "dolphinviewcontainer.h"
-#include "dolphin_generalsettings.h"
-#include "views/draganddrophelper.h"
 
-#include <QApplication>
 #include <KConfigGroup>
-#include <kio/global.h>
 #include <KRun>
+#include <KShell>
+#include <kio/global.h>
+
+#include <QApplication>
+#include <QDropEvent>
 
 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<int>::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<int>::of(&DolphinTabWidget::openNewActivatedTab));
+    connect(tabBar, &DolphinTabBar::tabDropEvent,
+            this, &DolphinTabWidget::tabDropEvent);
+    connect(tabBar, &DolphinTabBar::tabDetachRequested,
+            this, &DolphinTabWidget::detachTab);
     tabBar->hide();
 
     setTabBar(tabBar);
@@ -59,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<DolphinTabPage*>(widget(index));
@@ -100,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();
     }
 }
@@ -120,33 +144,34 @@ 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 KUrl& primaryUrl, const KUrl& secondaryUrl)
+void DolphinTabWidget::openNewActivatedTab(const QUrl& primaryUrl, const QUrl& secondaryUrl)
 {
     openNewTab(primaryUrl, secondaryUrl);
     setCurrentIndex(count() - 1);
 }
 
-void DolphinTabWidget::openNewTab(const KUrl& primaryUrl, const KUrl& secondaryUrl)
+void DolphinTabWidget::openNewTab(const QUrl& primaryUrl, const QUrl& secondaryUrl, TabPlacement tabPlacement)
 {
     QWidget* focusWidget = QApplication::focusWidget();
 
     DolphinTabPage* tabPage = new DolphinTabPage(primaryUrl, secondaryUrl, this);
+    tabPage->setActive(false);
     tabPage->setPlacesSelectorVisible(m_placesSelectorVisible);
-    connect(tabPage, SIGNAL(activeViewChanged(DolphinViewContainer*)),
-            this, SIGNAL(activeViewChanged(DolphinViewContainer*)));
-    connect(tabPage, SIGNAL(activeViewUrlChanged(KUrl)),
-            this, SLOT(tabUrlChanged(KUrl)));
-    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
@@ -155,43 +180,44 @@ void DolphinTabWidget::openNewTab(const KUrl& primaryUrl, const KUrl& secondaryU
     }
 }
 
-void DolphinTabWidget::openDirectories(const QList<KUrl>& dirs)
+void DolphinTabWidget::openDirectories(const QList<QUrl>& dirs, bool splitView)
 {
-    const bool hasSplitView = GeneralSettings::splitView();
+    Q_ASSERT(dirs.size() > 0);
 
-    // Open each directory inside a new tab. If the "split view" option has been enabled,
-    // always show two directories within one tab.
-    QList<KUrl>::const_iterator it = dirs.constBegin();
+    QList<QUrl>::const_iterator it = dirs.constBegin();
     while (it != dirs.constEnd()) {
-        const KUrl& primaryUrl = *(it++);
-        if (hasSplitView && (it != dirs.constEnd())) {
-            const KUrl& secondaryUrl = *(it++);
-            openNewTab(primaryUrl, secondaryUrl);
+        const QUrl& primaryUrl = *(it++);
+        const int index = getIndexByUrl(primaryUrl);
+        if (index >= 0) {
+            setCurrentIndex(index);
+            continue;
+        }
+        if (splitView && (it != dirs.constEnd())) {
+            const QUrl& secondaryUrl = *(it++);
+            openNewActivatedTab(primaryUrl, secondaryUrl);
         } else {
-            openNewTab(primaryUrl);
+            openNewActivatedTab(primaryUrl);
         }
     }
 }
 
-void DolphinTabWidget::openFiles(const QList<KUrl>& files)
+void DolphinTabWidget::openFiles(const QList<QUrl>& files, bool splitView)
 {
-    if (files.isEmpty()) {
-        return;
-    }
+    Q_ASSERT(files.size() > 0);
 
     // Get all distinct directories from 'files' and open a tab
     // for each directory. If the "split view" option is enabled, two
     // directories are shown inside one tab (see openDirectories()).
-    QList<KUrl> dirs;
-    foreach (const KUrl& url, files) {
-        const KUrl dir(url.directory());
+    QList<QUrl> dirs;
+    foreach (const QUrl& url, files) {
+        const QUrl dir(url.adjusted(QUrl::RemoveFilename));
         if (!dirs.contains(dir)) {
             dirs.append(dir);
         }
     }
 
     const int oldTabCount = count();
-    openDirectories(dirs);
+    openDirectories(dirs, splitView);
     const int tabCount = count();
 
     // Select the files. Although the files can be split between several
@@ -215,7 +241,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;
     }
 
@@ -261,16 +288,17 @@ void DolphinTabWidget::detachTab(int index)
 {
     Q_ASSERT(index >= 0);
 
-    const QString separator(QLatin1Char(' '));
-    QString command = QLatin1String("dolphin");
+    QStringList args;
 
     const DolphinTabPage* tabPage = tabPageAt(index);
-    command += separator + tabPage->primaryViewContainer()->url().url();
+    args << tabPage->primaryViewContainer()->url().url();
     if (tabPage->splitViewEnabled()) {
-        command += separator + tabPage->secondaryViewContainer()->url().url();
-        command += separator + QLatin1String("-split");
+        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);
 
     closeTab(index);
@@ -286,21 +314,16 @@ void DolphinTabWidget::openNewActivatedTab(int index)
 void DolphinTabWidget::tabDropEvent(int index, QDropEvent* event)
 {
     if (index >= 0) {
-        const DolphinView* view = tabPageAt(index)->activeViewContainer()->view();
-
-        QString error;
-        DragAndDropHelper::dropUrls(view->rootItem(), view->url(), event, error);
-        if (!error.isEmpty()) {
-            currentTabPage()->activeViewContainer()->showMessage(error, DolphinViewContainer::Error);
-        }
+        DolphinView* view = tabPageAt(index)->activeViewContainer()->view();
+        view->dropUrls(view->url(), event, view);
     }
 }
 
-void DolphinTabWidget::tabUrlChanged(const KUrl& url)
+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(tabPageAt(index)));
         tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url)));
 
         // Emit the currentUrlChanged signal if the url of the current tab has been changed.
@@ -312,10 +335,16 @@ void DolphinTabWidget::tabUrlChanged(const KUrl& 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)
@@ -342,20 +371,27 @@ void DolphinTabWidget::tabRemoved(int index)
     emit tabCountChanged(count());
 }
 
-QString DolphinTabWidget::tabName(const KUrl& url) const
+QString DolphinTabWidget::tabName(DolphinTabPage* tabPage) const
 {
-    QString name;
-    if (url.equals(KUrl("file:///"))) {
-        name = '/';
-    } else {
-        name = url.fileName();
-        if (name.isEmpty()) {
-            name = url.protocol();
-        } 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("&&"));
+}
+
+int DolphinTabWidget::getIndexByUrl(const QUrl& url) const
+{
+    for (int i = 0; i < count(); i++) {
+        // Conversion to display string is necessary to deal with the '~' alias.
+        // i.e. to acknowledge that ~/ is equivalent to /home/user/
+        const QUrl tabUrl = tabPageAt(i)->activeViewContainer()->url();
+        if (url == tabUrl ||
+            url.toDisplayString(QUrl::StripTrailingSlash) == tabUrl.toDisplayString(QUrl::StripTrailingSlash)) {
+            return i;
         }
     }
-    return name;
+    return -1;
 }