]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphintabwidget.cpp
SVN_SILENT made messages (.desktop file) - always resolve ours
[dolphin.git] / src / dolphintabwidget.cpp
index 0b1f07e0e22b8539ef53576a810afaec0eda3d8b..0cbe4529d6b49c4d12db753f35900287d1b621b8 100644 (file)
 #include "dolphintabpage.h"
 #include "dolphinviewcontainer.h"
 
-#include <QApplication>
 #include <KConfigGroup>
+#include <KRun>
 #include <KShell>
 #include <kio/global.h>
-#include <KRun>
+
+#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, static_cast<void (DolphinTabWidget::*)(int)>(&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,  static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::openNewActivatedTab));
+    connect(tabBar, &DolphinTabBar::tabDropEvent,
+            this, &DolphinTabWidget::tabDropEvent);
+    connect(tabBar, &DolphinTabBar::tabDetachRequested,
+            this, &DolphinTabWidget::detachTab);
     tabBar->hide();
 
     setTabBar(tabBar);
@@ -58,6 +61,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));
@@ -101,6 +116,7 @@ void DolphinTabWidget::refreshViews()
 {
     const int tabCount = count();
     for (int i = 0; i < tabCount; ++i) {
+        tabBar()->setTabText(i, tabName(tabPageAt(i)));
         tabPageAt(i)->refreshViews();
     }
 }
@@ -141,11 +157,11 @@ void DolphinTabWidget::openNewTab(const QUrl& primaryUrl, const QUrl& secondaryU
 
     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);
+    addTab(tabPage, QIcon::fromTheme(KIO::iconNameForUrl(primaryUrl)), tabName(tabPage));
 
     if (focusWidget) {
         // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
@@ -210,7 +226,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;
     }
 
@@ -282,7 +299,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 +307,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(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 +319,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 +355,13 @@ 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();
     }
-    return name;
+    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("&&"));
 }