]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphinmainwindow.cpp
reset the information panel if an item is shown that got deleted
[dolphin.git] / src / dolphinmainwindow.cpp
index 24e1a1f97d0bfea7b32dd3b9e39cfd252df12be9..bfdf8dafd6d2359de54589cf9321c3abbb3b5cba 100644 (file)
@@ -28,6 +28,7 @@
 #include "dolphinnewmenu.h"
 #include "settings/dolphinsettings.h"
 #include "settings/dolphinsettingsdialog.h"
+#include "dolphinsearchbox.h"
 #include "dolphinstatusbar.h"
 #include "dolphinviewcontainer.h"
 #include "panels/folders/folderspanel.h"
 #include <QSplitter>
 #include <QDockWidget>
 
+/*
+ * Remembers the tab configuration if a tab has been closed.
+ * Each closed tab can be restored by the menu
+ * "Go -> Recently Closed Tabs".
+ */
+struct ClosedTab
+{
+    KUrl primaryUrl;
+    KUrl secondaryUrl;
+    bool isSplit;
+};
+Q_DECLARE_METATYPE(ClosedTab)
+
 DolphinMainWindow::DolphinMainWindow(int id) :
     KXmlGuiWindow(0),
     m_newMenu(0),
@@ -92,7 +106,7 @@ DolphinMainWindow::DolphinMainWindow(int id) :
     m_tabBar(0),
     m_activeViewContainer(0),
     m_centralWidgetLayout(0),
-    m_searchBar(0),
+    m_searchBox(0),
     m_id(id),
     m_tabIndex(0),
     m_viewTab(),
@@ -255,16 +269,20 @@ void DolphinMainWindow::slotSelectionChanged(const KFileItemList& selection)
         compareFilesAction->setEnabled(false);
     }
 
-#if defined(QUICK_VIEW)
-    const bool activeViewHasSelection = (activeViewContainer()->view()->selectedItemsCount() > 0);
-    actionCollection()->action("quick_view")->setEnabled(activeViewHasSelection);
-#endif
-
     m_activeViewContainer->updateStatusBar();
 
     emit selectionChanged(selection);
 }
 
+void DolphinMainWindow::slotWheelMoved(int wheelDelta)
+{
+    if (wheelDelta > 0) {
+        activatePrevTab();
+    } else {
+        activateNextTab();
+    }
+}
+
 void DolphinMainWindow::slotRequestItemInfo(const KFileItem& item)
 {
     emit requestItemInfo(item);
@@ -344,7 +362,7 @@ void DolphinMainWindow::openNewTab(const KUrl& url)
 
 void DolphinMainWindow::activateNextTab()
 {
-    if (m_viewTab.count() == 1 || m_tabBar->count() < 2) {
+    if ((m_viewTab.count() == 1) || (m_tabBar->count() < 2)) {
         return;
     }
 
@@ -354,7 +372,7 @@ void DolphinMainWindow::activateNextTab()
 
 void DolphinMainWindow::activatePrevTab()
 {
-    if (m_viewTab.count() == 1 || m_tabBar->count() < 2) {
+    if ((m_viewTab.count() == 1) || (m_tabBar->count() < 2)) {
         return;
     }
 
@@ -480,6 +498,35 @@ void DolphinMainWindow::slotUndoAvailable(bool available)
     }
 }
 
+void DolphinMainWindow::restoreClosedTab(QAction* action)
+{
+    if (action->data().toBool()) {
+        // clear all actions except the "Empty Recently Closed Tabs"
+        // action and the separator
+        QList<QAction*> actions = m_recentTabsMenu->menu()->actions();
+        const int count = actions.size();
+        for (int i = 2; i < count; i++) {
+            m_recentTabsMenu->menu()->removeAction(actions.at(i));
+        }
+    } else {
+        const ClosedTab closedTab = action->data().value<ClosedTab>();
+        openNewTab(closedTab.primaryUrl);
+        m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
+
+        if (closedTab.isSplit) {
+            // create secondary view
+            toggleSplitView();
+            m_viewTab[m_tabIndex].secondaryView->setUrl(closedTab.secondaryUrl);
+        }
+
+        m_recentTabsMenu->removeAction(action);
+    }
+
+    if (m_recentTabsMenu->menu()->actions().count() == 2) {
+        m_recentTabsMenu->setEnabled(false);
+    }
+}
+
 void DolphinMainWindow::slotUndoTextChanged(const QString& text)
 {
     QAction* undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
@@ -685,18 +732,6 @@ void DolphinMainWindow::compareFiles()
     KRun::runCommand(command, "Kompare", "kompare", this);
 }
 
-void DolphinMainWindow::quickView()
-{
-    const KUrl::List urls = activeViewContainer()->view()->selectedUrls();
-    Q_ASSERT(urls.count() > 0);
-
-    QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.plasma", "/Previewer", "", "openFile");
-    foreach (const KUrl& url, urls) {
-        msg.setArguments(QList<QVariant>() << url.prettyUrl());
-        QDBusConnection::sessionBus().send(msg);
-    }
-}
-
 void DolphinMainWindow::toggleShowMenuBar()
 {
     const bool visible = menuBar()->isVisible();
@@ -759,7 +794,7 @@ void DolphinMainWindow::closeTab(int index)
     Q_ASSERT(index >= 0);
     Q_ASSERT(index < m_viewTab.count());
     if (m_viewTab.count() == 1) {
-          // the last tab may never get closed
+        // the last tab may never get closed
         return;
     }
 
@@ -768,6 +803,7 @@ void DolphinMainWindow::closeTab(int index)
         // previous tab before closing the tab.
         m_tabBar->setCurrentIndex((index > 0) ? index - 1 : 1);
     }
+    rememberClosedTab(index);
 
     // delete tab
     m_viewTab[index].primaryView->deleteLater();
@@ -843,13 +879,11 @@ void DolphinMainWindow::slotTestCanDecode(const QDragMoveEvent* event, bool& can
     canDecode = KUrl::List::canDecode(event->mimeData());
 }
 
-void DolphinMainWindow::searchItems()
+void DolphinMainWindow::searchItems(const KUrl& url)
 {
-    const QString nepomukString = "nepomuksearch:/" + m_searchBar->text();
-    m_activeViewContainer->setUrl(KUrl(nepomukString));
+    m_activeViewContainer->setUrl(url);
 }
 
-
 void DolphinMainWindow::init()
 {
     DolphinSettings& settings = DolphinSettings::instance();
@@ -885,10 +919,11 @@ void DolphinMainWindow::init()
     m_actionHandler->setCurrentView(view);
 
     m_tabBar = new KTabBar(this);
-    m_tabBar->setCloseButtonEnabled(true);
+    m_tabBar->setMovable(true);
+    m_tabBar->setTabsClosable(true);
     connect(m_tabBar, SIGNAL(currentChanged(int)),
             this, SLOT(setActiveTab(int)));
-    connect(m_tabBar, SIGNAL(closeRequest(int)),
+    connect(m_tabBar, SIGNAL(tabCloseRequested(int)),
             this, SLOT(closeTab(int)));
     connect(m_tabBar, SIGNAL(contextMenu(int, const QPoint&)),
             this, SLOT(openTabContextMenu(int, const QPoint&)));
@@ -896,6 +931,11 @@ void DolphinMainWindow::init()
             this, SLOT(openNewTab()));
     connect(m_tabBar, SIGNAL(testCanDecode(const QDragMoveEvent*, bool&)),
             this, SLOT(slotTestCanDecode(const QDragMoveEvent*, bool&)));
+    connect(m_tabBar, SIGNAL(wheelDelta(int)),
+           this, SLOT(slotWheelMoved(int)));
+    connect(m_tabBar, SIGNAL(mouseMiddleClick(int)),
+            this, SLOT(closeTab(int)));
+
     m_tabBar->blockSignals(true);  // signals get unblocked after at least 2 tabs are open
 
     QWidget* centralWidget = new QWidget(this);
@@ -911,9 +951,8 @@ void DolphinMainWindow::init()
 
     setupGUI(Keys | Save | Create | ToolBar);
 
-    m_searchBar->setParent(toolBar("searchToolBar"));
-    m_searchBar->setFont(KGlobalSettings::generalFont());
-    m_searchBar->show();
+    m_searchBox->setParent(toolBar("searchToolBar"));
+    m_searchBox->show();
 
     stateChanged("new_file");
 
@@ -993,10 +1032,10 @@ void DolphinMainWindow::setupActions()
     KAction* newTab = actionCollection()->addAction("new_tab");
     newTab->setIcon(KIcon("tab-new"));
     newTab->setText(i18nc("@action:inmenu File", "New Tab"));
-    newTab->setShortcut(KShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_N, Qt::CTRL | Qt::Key_T));
+    newTab->setShortcut(KShortcut(Qt::CTRL | Qt::Key_T, Qt::CTRL | Qt::SHIFT | Qt::Key_N));
     connect(newTab, SIGNAL(triggered()), this, SLOT(openNewTab()));
 
-    QAction* closeTab = actionCollection()->addAction("close_tab");
+    KAction* closeTab = actionCollection()->addAction("close_tab");
     closeTab->setIcon(KIcon("tab-close"));
     closeTab->setText(i18nc("@action:inmenu File", "Close Tab"));
     closeTab->setShortcut(Qt::CTRL | Qt::Key_W);
@@ -1064,6 +1103,19 @@ void DolphinMainWindow::setupActions()
     backShortcut.setAlternate(Qt::Key_Backspace);
     backAction->setShortcut(backShortcut);
 
+    m_recentTabsMenu = new KActionMenu(i18n("&Recently Closed Tabs"), this);
+    m_recentTabsMenu->setIcon(KIcon("edit-undo"));
+    actionCollection()->addAction("closed_tabs", m_recentTabsMenu);
+    connect(m_recentTabsMenu->menu(), SIGNAL(triggered(QAction *)),
+            this, SLOT(restoreClosedTab(QAction *)));
+
+    QAction* action = new QAction("&Empty Recently Closed Tabs", m_recentTabsMenu);
+    action->setIcon(KIcon("edit-clear-list"));
+    action->setData(QVariant::fromValue(true));
+    m_recentTabsMenu->addAction(action);
+    m_recentTabsMenu->addSeparator();
+    m_recentTabsMenu->setEnabled(false);
+
     KStandardAction::forward(this, SLOT(goForward()), actionCollection());
     KStandardAction::up(this, SLOT(goUp()), actionCollection());
     KStandardAction::home(this, SLOT(goHome()), actionCollection());
@@ -1085,16 +1137,6 @@ void DolphinMainWindow::setupActions()
     compareFiles->setEnabled(false);
     connect(compareFiles, SIGNAL(triggered()), this, SLOT(compareFiles()));
 
-    // disabled Quick View
-#if defined(QUICK_VIEW)
-    KAction* quickView = actionCollection()->addAction("quick_view");
-    quickView->setText(i18nc("@action:inmenu Tools", "Quick View"));
-    quickView->setIcon(KIcon("view-preview"));
-    quickView->setShortcut(Qt::CTRL + Qt::Key_Return);
-    quickView->setEnabled(false);
-    connect(quickView, SIGNAL(triggered()), this, SLOT(quickView()));
-#endif
-
     // setup 'Settings' menu
     m_showMenuBar = KStandardAction::showMenubar(this, SLOT(toggleShowMenuBar()), actionCollection());
     KStandardAction::preferences(this, SLOT(editSettings()), actionCollection());
@@ -1124,15 +1166,13 @@ void DolphinMainWindow::setupActions()
     connect(openInNewWindow, SIGNAL(triggered()), this, SLOT(openInNewWindow()));
 
     // 'Search' toolbar
-    m_searchBar = new KLineEdit(this);
-    m_searchBar->setMinimumWidth(150);
-    m_searchBar->setClearButtonShown(true);
-    connect(m_searchBar, SIGNAL(returnPressed()), this, SLOT(searchItems()));
+    m_searchBox = new DolphinSearchBox(this);
+    connect(m_searchBox, SIGNAL(search(KUrl)), this, SLOT(searchItems(KUrl)));
 
     KAction* search = new KAction(this);
     actionCollection()->addAction("search_bar", search);
     search->setText(i18nc("@action:inmenu", "Search Bar"));
-    search->setDefaultWidget(m_searchBar);
+    search->setDefaultWidget(m_searchBox);
     search->setShortcutConfigurable(false);
 }
 
@@ -1279,6 +1319,38 @@ void DolphinMainWindow::updateGoActions()
     goUpAction->setEnabled(currentUrl.upUrl() != currentUrl);
 }
 
+void DolphinMainWindow::rememberClosedTab(int index)
+{
+    KMenu* tabsMenu = m_recentTabsMenu->menu();
+
+    const QString primaryPath = m_viewTab[index].primaryView->url().path();
+    const QString iconName = KMimeType::iconNameForUrl(primaryPath);
+
+    QAction* action = new QAction(primaryPath, tabsMenu);
+
+    ClosedTab closedTab;
+    closedTab.primaryUrl = m_viewTab[index].primaryView->url();
+
+    if (m_viewTab[index].secondaryView != 0) {
+        closedTab.secondaryUrl = m_viewTab[index].secondaryView->url();
+        closedTab.isSplit = true;
+    } else {
+        closedTab.isSplit = false;
+    }
+
+    action->setData(QVariant::fromValue(closedTab));
+    action->setIcon(KIcon(iconName));
+
+    // add the closed tab menu entry after the separator and
+    // "Empty Recently Closed Tabs" entry
+    if (tabsMenu->actions().size() == 2) {
+        tabsMenu->addAction(action);
+    } else {
+        tabsMenu->insertAction(tabsMenu->actions().at(2), action);
+    }
+    actionCollection()->action("closed_tabs")->setEnabled(true);
+}
+
 void DolphinMainWindow::clearStatusBar()
 {
     m_activeViewContainer->statusBar()->clear();
@@ -1334,6 +1406,10 @@ QString DolphinMainWindow::tabName(const KUrl& url) const
         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('&', "&&");
         }
     }
     return name;