]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/panels/folders/folderspanel.cpp
Merge branch 'release/21.12'
[dolphin.git] / src / panels / folders / folderspanel.cpp
index b83e950e5c2020ef2f9a804b9a68a850034c1828..d3d8b81f14d6ed34c8cb1e0fcd457b5cc0bbc10d 100644 (file)
@@ -1,61 +1,43 @@
-/***************************************************************************
- *   Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com>        *
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
- *                                                                         *
- *   This program is distributed in the hope that it will be useful,       *
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
- *   GNU General Public License for more details.                          *
- *                                                                         *
- *   You should have received a copy of the GNU General Public License     *
- *   along with this program; if not, write to the                         *
- *   Free Software Foundation, Inc.,                                       *
- *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
- ***************************************************************************/
+/*
+ * SPDX-FileCopyrightText: 2006-2010 Peter Penz <peter.penz19@gmail.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
 
 #include "folderspanel.h"
 
 #include "dolphin_folderspanelsettings.h"
 #include "dolphin_generalsettings.h"
-#include "treeviewcontextmenu.h"
 #include "foldersitemlistwidget.h"
+#include "global.h"
+#include "kitemviews/kfileitemlistview.h"
+#include "kitemviews/kfileitemmodel.h"
+#include "kitemviews/kitemlistcontainer.h"
+#include "kitemviews/kitemlistcontroller.h"
+#include "kitemviews/kitemlistselectionmanager.h"
+#include "kitemviews/private/kitemlistroleeditor.h"
+#include "treeviewcontextmenu.h"
+#include "views/draganddrophelper.h"
 
-#include <views/renamedialog.h>
-#include <kitemviews/kitemlistselectionmanager.h>
-#include <kitemviews/kfileitemlistview.h>
-#include <kitemviews/kfileitemlistwidget.h>
-#include <kitemviews/kitemlistcontainer.h>
-#include <kitemviews/kitemlistcontroller.h>
-#include <kitemviews/kfileitemmodel.h>
-
-#include <KFileItem>
 #include <KJobWidgets>
 #include <KJobUiDelegate>
 #include <KIO/CopyJob>
 #include <KIO/DropJob>
 #include <KIO/FileUndoManager>
+#include <KIO/RenameFileDialog>
 
 #include <QApplication>
 #include <QBoxLayout>
-#include <QDropEvent>
 #include <QGraphicsSceneDragDropEvent>
 #include <QGraphicsView>
 #include <QPropertyAnimation>
 #include <QTimer>
 
-#include <views/draganddrophelper.h>
-
-#include <KDebug>
-
 FoldersPanel::FoldersPanel(QWidget* parent) :
     Panel(parent),
     m_updateCurrentItem(false),
-    m_controller(0),
-    m_model(0)
+    m_controller(nullptr),
+    m_model(nullptr)
 {
     setLayoutDirection(Qt::LeftToRight);
 }
@@ -66,7 +48,7 @@ FoldersPanel::~FoldersPanel()
 
     if (m_controller) {
         KItemListView* view = m_controller->view();
-        m_controller->setView(0);
+        m_controller->setView(nullptr);
         delete view;
     }
 }
@@ -82,6 +64,17 @@ bool FoldersPanel::showHiddenFiles() const
     return FoldersPanelSettings::hiddenFilesShown();
 }
 
+void FoldersPanel::setLimitFoldersPanelToHome(bool enable)
+{
+    FoldersPanelSettings::setLimitFoldersPanelToHome(enable);
+    reloadTree();
+}
+
+bool FoldersPanel::limitFoldersPanelToHome() const
+{
+    return FoldersPanelSettings::limitFoldersPanelToHome();
+}
+
 void FoldersPanel::setAutoScrolling(bool enable)
 {
     // TODO: Not supported yet in Dolphin 2.0
@@ -99,17 +92,14 @@ void FoldersPanel::rename(const KFileItem& item)
         const int index = m_model->index(item);
         m_controller->view()->editRole(index, "text");
     } else {
-        RenameDialog* dialog = new RenameDialog(this, KFileItemList() << item);
-        dialog->setAttribute(Qt::WA_DeleteOnClose);
-        dialog->show();
-        dialog->raise();
-        dialog->activateWindow();
+        KIO::RenameFileDialog* dialog = new KIO::RenameFileDialog(KFileItemList({item}), this);
+        dialog->open();
     }
 }
 
 bool FoldersPanel::urlChanged()
 {
-    if (!url().isValid() || url().scheme().contains("search")) {
+    if (!url().isValid() || url().scheme().contains(QLatin1String("search"))) {
         // Skip results shown by a search, as possible identical
         // directory names are useless without parent-path information.
         return false;
@@ -122,6 +112,14 @@ bool FoldersPanel::urlChanged()
     return true;
 }
 
+void FoldersPanel::reloadTree()
+{
+    if (m_controller) {
+        loadTree(url(), AllowJumpHome);
+    }
+}
+
+
 void FoldersPanel::showEvent(QShowEvent* event)
 {
     if (event->spontaneous()) {
@@ -134,6 +132,7 @@ void FoldersPanel::showEvent(QShowEvent* event)
         // This assures that no performance and memory overhead is given when the folders panel is not
         // used at all and stays invisible.
         KFileItemListView* view  = new KFileItemListView();
+        view->setScanDirectories(false);
         view->setWidgetCreator(new KItemListWidgetCreator<FoldersItemListWidget>());
         view->setSupportsItemExpanding(true);
         // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
@@ -168,7 +167,7 @@ void FoldersPanel::showEvent(QShowEvent* event)
         container->setEnabledFrame(false);
 
         QVBoxLayout* layout = new QVBoxLayout(this);
-        layout->setMargin(0);
+        layout->setContentsMargins(0, 0, 0, 0);
         layout->addWidget(container);
     }
 
@@ -190,7 +189,19 @@ void FoldersPanel::slotItemActivated(int index)
 {
     const KFileItem item = m_model->fileItem(index);
     if (!item.isNull()) {
-        emit folderActivated(item.url());
+        const auto modifiers = QGuiApplication::keyboardModifiers();
+        // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
+        if (modifiers & Qt::ControlModifier && modifiers & Qt::ShiftModifier) {
+            Q_EMIT folderInNewActiveTab(item.url());
+        } else if (modifiers & Qt::ControlModifier) {
+            Q_EMIT folderInNewTab(item.url());
+        } else if (modifiers & Qt::ShiftModifier) {
+            // The shift modifier is not considered because it is used to expand the tree view without actually
+            // opening the folder
+            return;
+        } else {
+            Q_EMIT folderActivated(item.url());
+        }
     }
 }
 
@@ -198,18 +209,22 @@ void FoldersPanel::slotItemMiddleClicked(int index)
 {
     const KFileItem item = m_model->fileItem(index);
     if (!item.isNull()) {
-        emit folderMiddleClicked(item.url());
+        const auto modifiers = QGuiApplication::keyboardModifiers();
+        // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
+        if (modifiers & Qt::ShiftModifier) {
+            Q_EMIT folderInNewActiveTab(item.url());
+        } else {
+            Q_EMIT folderInNewTab(item.url());
+        }
     }
 }
 
 void FoldersPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
 {
-    Q_UNUSED(pos);
-
     const KFileItem fileItem = m_model->fileItem(index);
 
-    QWeakPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, fileItem);
-    contextMenu.data()->open();
+    QPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, fileItem);
+    contextMenu.data()->open(pos.toPoint());
     if (contextMenu.data()) {
         delete contextMenu.data();
     }
@@ -217,10 +232,8 @@ void FoldersPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
 
 void FoldersPanel::slotViewContextMenuRequested(const QPointF& pos)
 {
-    Q_UNUSED(pos);
-
-    QWeakPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, KFileItem());
-    contextMenu.data()->open();
+    QPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, KFileItem());
+    contextMenu.data()->open(pos.toPoint());
     if (contextMenu.data()) {
         delete contextMenu.data();
     }
@@ -240,9 +253,9 @@ void FoldersPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* eve
                              event->buttons(),
                              event->modifiers());
 
-        KIO::DropJob *job = DragAndDropHelper::dropUrls(destItem.url(), &dropEvent, this);
+        KIO::DropJob *job = DragAndDropHelper::dropUrls(destItem.mostLocalUrl(), &dropEvent, this);
         if (job) {
-            connect(job, &KIO::DropJob::result, this, [this](KJob *job) { if (job->error()) emit errorMessage(job->errorString()); });
+            connect(job, &KIO::DropJob::result, this, [this](KJob *job) { if (job->error()) Q_EMIT errorMessage(job->errorString()); });
         }
     }
 }
@@ -251,8 +264,9 @@ void FoldersPanel::slotRoleEditingFinished(int index, const QByteArray& role, co
 {
     if (role == "text") {
         const KFileItem item = m_model->fileItem(index);
-        const QString newName = value.toString();
-        if (!newName.isEmpty() && newName != item.text() && newName != QLatin1String(".") && newName != QLatin1String("..")) {
+        const EditResult retVal = value.value<EditResult>();
+        const QString newName = retVal.newName;
+        if (!newName.isEmpty() && newName != item.text() && newName != QLatin1Char('.') && newName != QLatin1String("..")) {
             const QUrl oldUrl = item.url();
             QUrl newUrl = oldUrl.adjusted(QUrl::RemoveFilename);
             newUrl.setPath(newUrl.path() + KIO::encodeFileName(newName));
@@ -260,7 +274,7 @@ void FoldersPanel::slotRoleEditingFinished(int index, const QByteArray& role, co
             KIO::Job* job = KIO::moveAs(oldUrl, newUrl);
             KJobWidgets::setWindow(job, this);
             KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Rename, {oldUrl}, newUrl, job);
-            job->ui()->setAutoErrorHandlingEnabled(true);
+            job->uiDelegate()->setAutoErrorHandlingEnabled(true);
         }
     }
 }
@@ -274,7 +288,7 @@ void FoldersPanel::slotLoadingCompleted()
         // animations.
         // TODO: Check whether it makes sense to allow accessing the
         // view-internal delay for usecases like this.
-        QTimer::singleShot(250, this, SLOT(startFadeInAnimation()));
+        QTimer::singleShot(250, this, &FoldersPanel::startFadeInAnimation);
     }
 
     if (!m_updateCurrentItem) {
@@ -296,33 +310,50 @@ void FoldersPanel::startFadeInAnimation()
     anim->setDuration(200);
 }
 
-void FoldersPanel::loadTree(const QUrl& url)
+void FoldersPanel::loadTree(const QUrl& url, FoldersPanel::NavigationBehaviour navigationBehaviour)
 {
     Q_ASSERT(m_controller);
 
     m_updateCurrentItem = false;
+    bool jumpHome = false;
 
     QUrl baseUrl;
-    if (url.isLocalFile()) {
-        // Use the root directory as base for local URLs (#150941)
-        baseUrl = QDir::rootPath();
-    } else {
+    if (!url.isLocalFile()) {
         // Clear the path for non-local URLs and use it as base
         baseUrl = url;
-        baseUrl.setPath(QString('/'));
+        baseUrl.setPath(QStringLiteral("/"));
+    } else if (Dolphin::homeUrl().isParentOf(url) || (Dolphin::homeUrl() == url)) {
+        if (FoldersPanelSettings::limitFoldersPanelToHome() ) {
+            baseUrl = Dolphin::homeUrl();
+        } else {
+            // Use the root directory as base for local URLs (#150941)
+            baseUrl = QUrl::fromLocalFile(QDir::rootPath());
+        }
+    } else if (FoldersPanelSettings::limitFoldersPanelToHome() && navigationBehaviour == AllowJumpHome) {
+        baseUrl = Dolphin::homeUrl();
+        jumpHome = true;
+    } else {
+        // Use the root directory as base for local URLs (#150941)
+        baseUrl = QUrl::fromLocalFile(QDir::rootPath());
     }
 
-    if (m_model->directory() != baseUrl) {
+    if (m_model->directory() != baseUrl && !jumpHome) {
         m_updateCurrentItem = true;
         m_model->refreshDirectory(baseUrl);
     }
 
     const int index = m_model->index(url);
-    if (index >= 0) {
+    if (jumpHome) {
+      Q_EMIT folderActivated(baseUrl);
+    } else if (index >= 0) {
         updateCurrentItem(index);
+    } else if (url == baseUrl) {
+        // clear the selection when visiting the base url
+        updateCurrentItem(-1);
     } else {
         m_updateCurrentItem = true;
         m_model->expandParentDirectories(url);
+
         // slotLoadingCompleted() will be invoked after the model has
         // expanded the url
     }