]> cloud.milkyroute.net Git - dolphin.git/commitdiff
* provide context menu for tabs
authorPeter Penz <peter.penz19@gmail.com>
Wed, 16 Apr 2008 20:03:05 +0000 (20:03 +0000)
committerPeter Penz <peter.penz19@gmail.com>
Wed, 16 Apr 2008 20:03:05 +0000 (20:03 +0000)
* assure that the tab name is set to "/" for the root URL instead of using an empty string

svn path=/trunk/KDE/kdebase/apps/; revision=797725

src/dolphinmainwindow.cpp
src/dolphinmainwindow.h

index f64f847feb97d69705e6c581ff90e64aad6d4d68..37fdce75b6fc1d6d224efc39c7ca2d2fd5b2228d 100644 (file)
@@ -180,10 +180,9 @@ void DolphinMainWindow::changeUrl(const KUrl& url)
         updateEditActions();
         updateViewActions();
         updateGoActions();
-        const QString caption = url.fileName();
-        setCaption(caption);
+        setCaption(url.fileName());
         if (m_viewTab.count() > 1) {
-            m_tabBar->setTabText(m_tabIndex, caption);
+            m_tabBar->setTabText(m_tabIndex, tabName(url));
         }
         emit urlChanged(url);
     }
@@ -267,11 +266,11 @@ void DolphinMainWindow::openNewTab(const KUrl& url)
     if (m_viewTab.count() == 1) {
         // Only one view is open currently and hence no tab is shown at
         // all. Before creating a tab for 'url', provide a tab for the current URL.
-        m_tabBar->addTab(KIcon("folder"), m_activeViewContainer->url().fileName());
+        m_tabBar->addTab(KIcon("folder"), tabName(m_activeViewContainer->url()));
         m_tabBar->blockSignals(false);
     }
 
-    m_tabBar->addTab(KIcon("folder"), url.fileName());
+    m_tabBar->addTab(KIcon("folder"), tabName(url));
 
     ViewTab viewTab;
     viewTab.splitter = new QSplitter(this);
@@ -694,6 +693,11 @@ void DolphinMainWindow::setActiveTab(int index)
                                                          viewTab.secondaryView);
 }
 
+void DolphinMainWindow::closeTab()
+{
+    closeTab(m_tabBar->currentIndex());
+}
+
 void DolphinMainWindow::closeTab(int index)
 {
     Q_ASSERT(index >= 0);
@@ -734,6 +738,38 @@ void DolphinMainWindow::closeTab(int index)
     }
 }
 
+void DolphinMainWindow::openTabContextMenu(int index, const QPoint& pos)
+{
+    KMenu menu(this);
+
+    QAction* newTabAction = menu.addAction(KIcon("tab-new"), i18nc("@action:inmenu", "New Tab"));
+    newTabAction->setShortcut(actionCollection()->action("new_tab")->shortcut());
+
+    QAction* closeOtherTabsAction = menu.addAction(KIcon("tab-close"), i18nc("@action:inmenu", "Close Other Tabs"));
+
+    QAction* closeTabAction = menu.addAction(KIcon("tab-close"), i18nc("@action:inmenu", "Close Tab"));
+    closeTabAction->setShortcut(actionCollection()->action("close_tab")->shortcut());
+
+    QAction* selectedAction = menu.exec(pos);
+    if (selectedAction == newTabAction) {
+        const ViewTab& tab = m_viewTab[index];
+        const KUrl url = tab.primaryView->isActive() ? tab.primaryView->url() :
+                                                       tab.secondaryView->url();
+        openNewTab(url);
+        m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
+    } else if (selectedAction == closeOtherTabsAction) {
+        const int count = m_tabBar->count();
+        for (int i = 0; i < index; ++i) {
+            closeTab(0);
+        }
+        for (int i = index + 1; i < count; ++i) {
+            closeTab(1);
+        }
+    } else if (selectedAction == closeTabAction) {
+        closeTab(index);
+    }
+}
+
 void DolphinMainWindow::init()
 {
     DolphinSettings& settings = DolphinSettings::instance();
@@ -774,6 +810,8 @@ void DolphinMainWindow::init()
             this, SLOT(setActiveTab(int)));
     connect(m_tabBar, SIGNAL(closeRequest(int)),
             this, SLOT(closeTab(int)));
+    connect(m_tabBar, SIGNAL(contextMenu(int, const QPoint&)),
+            this, SLOT(openTabContextMenu(int, const QPoint&)));
     m_tabBar->blockSignals(true);  // signals get unblocked after at least 2 tabs are open
 
     QWidget* centralWidget = new QWidget(this);
@@ -839,10 +877,9 @@ void DolphinMainWindow::setActiveViewContainer(DolphinViewContainer* viewContain
     updateGoActions();
 
     const KUrl& url = m_activeViewContainer->url();
-    const QString caption = url.fileName();
-    setCaption(caption);
+    setCaption(url.fileName());
     if (m_viewTab.count() > 1) {
-        m_tabBar->setTabText(m_tabIndex, caption);
+        m_tabBar->setTabText(m_tabIndex, tabName(url));
     }
 
     emit urlChanged(url);
@@ -870,6 +907,11 @@ void DolphinMainWindow::setupActions()
     newTab->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_N);
     connect(newTab, SIGNAL(triggered()), this, SLOT(openNewTab()));
 
+    QAction* closeTab = new QAction(KIcon("tab-close"), i18nc("@action:inmenu File", "Close Tab"), this);
+    closeTab->setShortcut(Qt::CTRL | Qt::Key_W);
+    connect(closeTab, SIGNAL(triggered()), this, SLOT(closeTab()));
+    actionCollection()->addAction("close_tab", closeTab);
+
     KAction* properties = actionCollection()->addAction("properties");
     properties->setText(i18nc("@action:inmenu File", "Properties"));
     properties->setShortcut(Qt::ALT | Qt::Key_Return);
@@ -1158,6 +1200,11 @@ void DolphinMainWindow::updateSplitAction()
     }
 }
 
+QString DolphinMainWindow::tabName(const KUrl& url) const
+{
+    return url.equals(KUrl("file:///")) ? "/" : url.fileName();
+}
+
 DolphinMainWindow::UndoUiInterface::UndoUiInterface(DolphinMainWindow* mainWin) :
     KonqFileUndoManager::UiInterface(mainWin),
     m_mainWin(mainWin)
index 3ac766d7713bac30263d2c913d5aa846efac5931..2f0fb9589a21e287d52fb33a2cced79f917de7b2 100644 (file)
@@ -331,11 +331,20 @@ private slots:
      */
     void setActiveTab(int index);
 
+    /** Closes the currently active tab. */
+    void closeTab();
+
     /**
      * Closes the tab with the index \index and activates the tab with index - 1.
      */
     void closeTab(int index);
 
+    /**
+     * Opens a context menu for the tab with the index \a index
+     * on the position \a pos.
+     */
+    void openTabContextMenu(int index, const QPoint& pos);
+
 private:
     DolphinMainWindow(int id);
     void init();
@@ -370,6 +379,9 @@ private:
      */
     void updateSplitAction();
 
+    /** Returns the name of the tab for the URL \a url. */
+    QString tabName(const KUrl& url) const;
+
 private:
     /**
      * Implements a custom error handling for the undo manager. This