]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphinmainwindow.cpp
Improve drawing selected items in Compact/Details View and Places Panel
[dolphin.git] / src / dolphinmainwindow.cpp
index 9da73f96e4f39f0162e23576e0bfd8503c4f0652..c60951d2c6de54ba4501b2b41eac34ae67bc0c18 100644 (file)
 #include "dolphindockwidget.h"
 #include "dolphincontextmenu.h"
 #include "dolphinnewfilemenu.h"
+#include "dolphinrecenttabsmenu.h"
 #include "dolphinviewcontainer.h"
 #include "panels/folders/folderspanel.h"
 #include "panels/places/placespanel.h"
 #include "panels/information/informationpanel.h"
 #include "settings/dolphinsettingsdialog.h"
 #include "statusbar/dolphinstatusbar.h"
-#include "views/dolphinview.h"
 #include "views/dolphinviewactionhandler.h"
 #include "views/dolphinremoteencoding.h"
 #include "views/draganddrophelper.h"
 #include "views/viewproperties.h"
+#include "views/dolphinnewfilemenuobserver.h"
 
 #ifndef Q_OS_WIN
 #include "panels/terminal/terminalpanel.h"
@@ -93,19 +94,6 @@ namespace {
     const int CurrentDolphinVersion = 200;
 };
 
-/*
- * 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() :
     KXmlGuiWindow(0),
     m_newFileMenu(0),
@@ -127,6 +115,9 @@ DolphinMainWindow::DolphinMainWindow() :
     ViewTab& viewTab = m_viewTab[m_tabIndex];
     viewTab.wasActive = true; // The first opened tab is automatically active
 
+    connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(errorMessage(QString)),
+            this, SLOT(showErrorMessage(QString)));
+
     KIO::FileUndoManager* undoManager = KIO::FileUndoManager::self();
     undoManager->setUiInterface(new UndoUiInterface());
 
@@ -214,6 +205,7 @@ DolphinMainWindow::DolphinMainWindow() :
         toggleSplitView();
     }
     updateEditActions();
+    updatePasteAction();
     updateViewActions();
     updateGoActions();
 
@@ -244,20 +236,8 @@ void DolphinMainWindow::openDirectories(const QList<KUrl>& dirs)
         return;
     }
 
-    // dirs could contain URLs that actually point to archives or other files.
-    // Replace them by URLs we can open where possible and filter the rest out.
-    QList<KUrl> urlsToOpen;
-    foreach (const KUrl& rawUrl, dirs) {
-        const KFileItem& item = KFileItem(KFileItem::Unknown, KFileItem::Unknown, rawUrl);
-        item.determineMimeType();
-        const KUrl& url = DolphinView::openItemAsFolderUrl(item);
-        if (!url.isEmpty()) {
-            urlsToOpen.append(url);
-        }
-    }
-
-    if (urlsToOpen.count() == 1) {
-        m_activeViewContainer->setUrl(urlsToOpen.first());
+    if (dirs.count() == 1) {
+        m_activeViewContainer->setUrl(dirs.first());
         return;
     }
 
@@ -267,12 +247,12 @@ void DolphinMainWindow::openDirectories(const QList<KUrl>& dirs)
 
     // 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 = urlsToOpen.begin();
-    while (it != urlsToOpen.end()) {
+    QList<KUrl>::const_iterator it = dirs.constBegin();
+    while (it != dirs.constEnd()) {
         openNewTab(*it);
         ++it;
 
-        if (hasSplitView && (it != urlsToOpen.end())) {
+        if (hasSplitView && (it != dirs.constEnd())) {
             const int tabIndex = m_viewTab.count() - 1;
             m_viewTab[tabIndex].secondaryView->setUrl(*it);
             ++it;
@@ -365,6 +345,7 @@ void DolphinMainWindow::changeUrl(const KUrl& url)
     if (view) {
         view->setUrl(url);
         updateEditActions();
+        updatePasteAction();
         updateViewActions();
         updateGoActions();
         setUrlAsCaption(url);
@@ -490,6 +471,8 @@ void DolphinMainWindow::openNewTab(const KUrl& url)
     m_viewTab.append(viewTab);
 
     actionCollection()->action("close_tab")->setEnabled(true);
+    actionCollection()->action("activate_prev_tab")->setEnabled(true);
+    actionCollection()->action("activate_next_tab")->setEnabled(true);
 
     // Provide a split view, if the startup settings are set this way
     if (GeneralSettings::splitView()) {
@@ -686,6 +669,13 @@ void DolphinMainWindow::readProperties(const KConfigGroup& group)
                 Q_ASSERT(cont);
             }
 
+            // The right view must be activated before the URL is set. Changing
+            // the URL in the right view will emit the right URL navigator's
+            // urlChanged(KUrl) signal, which is connected to the changeUrl(KUrl)
+            // slot. That slot will change the URL in the left view if it is still
+            // active. See https://bugs.kde.org/show_bug.cgi?id=330047.
+            setActiveViewContainer(cont);
+
             cont->setUrl(secondaryUrl);
             const bool editable = group.readEntry(tabProperty("Secondary Editable", i), false);
             cont->urlNavigator()->setUrlEditable(editable);
@@ -737,35 +727,6 @@ 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));
@@ -1134,7 +1095,10 @@ void DolphinMainWindow::closeTab(int index)
         // previous tab before closing the tab.
         m_tabBar->setCurrentIndex((index > 0) ? index - 1 : 1);
     }
-    rememberClosedTab(index);
+
+    const KUrl primaryUrl(m_viewTab[index].primaryView->url());
+    const KUrl secondaryUrl(m_viewTab[index].secondaryView ? m_viewTab[index].secondaryView->url() : KUrl());
+    emit rememberClosedTab(primaryUrl, secondaryUrl);
 
     // delete tab
     m_viewTab[index].primaryView->deleteLater();
@@ -1157,6 +1121,8 @@ void DolphinMainWindow::closeTab(int index)
     if (m_viewTab.count() == 1) {
         m_tabBar->removeTab(0);
         actionCollection()->action("close_tab")->setEnabled(false);
+        actionCollection()->action("activate_prev_tab")->setEnabled(false);
+        actionCollection()->action("activate_next_tab")->setEnabled(false);
     } else {
         m_tabBar->blockSignals(false);
     }
@@ -1430,6 +1396,18 @@ void DolphinMainWindow::slotPlaceActivated(const KUrl& url)
     }
 }
 
+void DolphinMainWindow::restoreClosedTab(const KUrl& primaryUrl, const KUrl& secondaryUrl)
+{
+    openNewActivatedTab(primaryUrl);
+
+    if (!secondaryUrl.isEmpty() && secondaryUrl.isValid()) {
+        const int index = m_tabBar->currentIndex();
+        createSecondaryView(index);
+        setActiveViewContainer(m_viewTab[index].secondaryView);
+        m_viewTab[index].secondaryView->setUrl(secondaryUrl);
+    }
+}
+
 void DolphinMainWindow::setActiveViewContainer(DolphinViewContainer* viewContainer)
 {
     Q_ASSERT(viewContainer);
@@ -1453,6 +1431,7 @@ void DolphinMainWindow::setActiveViewContainer(DolphinViewContainer* viewContain
 
     updateHistory();
     updateEditActions();
+    updatePasteAction();
     updateViewActions();
     updateGoActions();
 
@@ -1481,7 +1460,7 @@ DolphinViewContainer* DolphinMainWindow::createViewContainer(const KUrl& url, QW
 void DolphinMainWindow::setupActions()
 {
     // setup 'File' menu
-    m_newFileMenu = new DolphinNewFileMenu(this);
+    m_newFileMenu = new DolphinNewFileMenu(actionCollection(), this);
     KMenu* menu = m_newFileMenu->menu();
     menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
     menu->setIcon(KIcon("document-new"));
@@ -1577,19 +1556,12 @@ 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"));
-    m_recentTabsMenu->setDelayed(false);
-    actionCollection()->addAction("closed_tabs", m_recentTabsMenu);
-    connect(m_recentTabsMenu->menu(), SIGNAL(triggered(QAction*)),
-            this, SLOT(restoreClosedTab(QAction*)));
-
-    QAction* action = new QAction(i18n("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);
+    DolphinRecentTabsMenu* recentTabsMenu = new DolphinRecentTabsMenu(this);
+    actionCollection()->addAction("closed_tabs", recentTabsMenu);
+    connect(this, SIGNAL(rememberClosedTab(KUrl,KUrl)),
+            recentTabsMenu, SLOT(rememberClosedTab(KUrl,KUrl)));
+    connect(recentTabsMenu, SIGNAL(restoreClosedTab(KUrl,KUrl)),
+            this, SLOT(restoreClosedTab(KUrl,KUrl)));
 
     KAction* forwardAction = KStandardAction::forward(this, SLOT(goForward()), actionCollection());
     connect(forwardAction, SIGNAL(triggered(Qt::MouseButtons,Qt::KeyboardModifiers)), this, SLOT(goForward(Qt::MouseButtons)));
@@ -1635,12 +1607,16 @@ void DolphinMainWindow::setupActions()
     prevTabKeys.append(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_Tab));
 
     KAction* activateNextTab = actionCollection()->addAction("activate_next_tab");
+    activateNextTab->setIconText(i18nc("@action:inmenu", "Next Tab"));
     activateNextTab->setText(i18nc("@action:inmenu", "Activate Next Tab"));
+    activateNextTab->setEnabled(false);
     connect(activateNextTab, SIGNAL(triggered()), SLOT(activateNextTab()));
     activateNextTab->setShortcuts(QApplication::isRightToLeft() ? prevTabKeys : nextTabKeys);
 
     KAction* activatePrevTab = actionCollection()->addAction("activate_prev_tab");
+    activatePrevTab->setIconText(i18nc("@action:inmenu", "Previous Tab"));
     activatePrevTab->setText(i18nc("@action:inmenu", "Activate Previous Tab"));
+    activatePrevTab->setEnabled(false);
     connect(activatePrevTab, SIGNAL(triggered()), SLOT(activatePrevTab()));
     activatePrevTab->setShortcuts(QApplication::isRightToLeft() ? nextTabKeys : prevTabKeys);
 
@@ -1813,7 +1789,6 @@ void DolphinMainWindow::updateEditActions()
         deleteWithTrashShortcut->setEnabled(capabilities.supportsDeleting() && !enableMoveToTrash);
         cutAction->setEnabled(capabilities.supportsMoving());
     }
-    updatePasteAction();
 }
 
 void DolphinMainWindow::updateViewActions()
@@ -1895,44 +1870,6 @@ bool DolphinMainWindow::addActionToMenu(QAction* action, KMenu* menu)
     return true;
 }
 
-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(squeezedText(primaryPath), tabsMenu);
-
-    ClosedTab closedTab;
-    closedTab.primaryUrl = m_viewTab[index].primaryView->url();
-
-    if (m_viewTab[index].secondaryView) {
-        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);
-    }
-
-    // assure that only up to 8 closed tabs are shown in the menu
-    if (tabsMenu->actions().size() > 8) {
-        tabsMenu->removeAction(tabsMenu->actions().last());
-    }
-    actionCollection()->action("closed_tabs")->setEnabled(true);
-    KAcceleratorManager::manage(tabsMenu);
-}
-
 void DolphinMainWindow::refreshViews()
 {
     Q_ASSERT(m_viewTab[m_tabIndex].primaryView);