]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/panels/folders/folderspanel.cpp
Add a SetFolderIcon ItemAction plugin
[dolphin.git] / src / panels / folders / folderspanel.cpp
index 1b75f36bc213b2e6e5cbe7b3ba9bb5bc66498789..e375f2ce51fc84603020becc0e78b2fcff02b213 100644 (file)
@@ -1,97 +1,83 @@
-/***************************************************************************
- *   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 "settings/dolphinsettings.h"
 #include "dolphin_folderspanelsettings.h"
 #include "dolphin_generalsettings.h"
-#include "paneltreeview.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 <kfileplacesmodel.h>
-#include <kdirlister.h>
-#include <kfileitem.h>
-#include <konq_operations.h>
+#include <KIO/CopyJob>
+#include <KIO/DropJob>
+#include <KIO/FileUndoManager>
+#include <KIO/RenameFileDialog>
+#include <KJobUiDelegate>
+#include <KJobWidgets>
 
 #include <QApplication>
 #include <QBoxLayout>
-#include <QItemSelection>
-#include <QModelIndex>
-#include <QPointer>
-#include <QTreeView>
-#include <QScrollBar>
+#include <QGraphicsSceneDragDropEvent>
+#include <QGraphicsView>
+#include <QPropertyAnimation>
 #include <QTimer>
 
-#include <views/draganddrophelper.h>
-#include <views/dolphinmodel.h>
-#include <views/dolphinsortfilterproxymodel.h>
-#include <views/dolphinview.h>
-#include <views/folderexpander.h>
-#include <views/renamedialog.h>
-
-FoldersPanel::FoldersPanel(QWidget* parent) :
-    Panel(parent),
-    m_setLeafVisible(false),
-    m_mouseButtons(Qt::NoButton),
-    m_dirLister(0),
-    m_dolphinModel(0),
-    m_proxyModel(0),
-    m_treeView(0),
-    m_leafDir()
+FoldersPanel::FoldersPanel(QWidget *parent)
+    : Panel(parent)
+    , m_updateCurrentItem(false)
+    , m_controller(nullptr)
+    , m_model(nullptr)
 {
     setLayoutDirection(Qt::LeftToRight);
 }
 
 FoldersPanel::~FoldersPanel()
 {
-    FoldersPanelSettings::self()->writeConfig();
+    FoldersPanelSettings::self()->save();
 
-    delete m_proxyModel;
-    m_proxyModel = 0;
-    delete m_dolphinModel;
-    m_dolphinModel = 0;
-    m_dirLister = 0; // deleted by m_dolphinModel
+    if (m_controller) {
+        KItemListView *view = m_controller->view();
+        m_controller->setView(nullptr);
+        delete view;
+    }
 }
 
-QSize FoldersPanel::sizeHint() const
+void FoldersPanel::setShowHiddenFiles(bool show)
 {
-    return QSize(200, 400);
+    FoldersPanelSettings::setHiddenFilesShown(show);
+    m_model->setShowHiddenFiles(show);
 }
 
-void FoldersPanel::setShowHiddenFiles(bool show)
+bool FoldersPanel::showHiddenFiles() const
 {
-    FoldersPanelSettings::setShowHiddenFiles(show);
-    if (m_dirLister != 0) {
-        m_dirLister->setShowingDotFiles(show);
-        m_dirLister->openUrl(m_dirLister->url(), KDirLister::Reload);
-    }
+    return FoldersPanelSettings::hiddenFilesShown();
 }
 
-bool FoldersPanel::showHiddenFiles() const
+void FoldersPanel::setLimitFoldersPanelToHome(bool enable)
 {
-    return FoldersPanelSettings::showHiddenFiles();
+    FoldersPanelSettings::setLimitFoldersPanelToHome(enable);
+    reloadTree();
+}
+
+bool FoldersPanel::limitFoldersPanelToHome() const
+{
+    return FoldersPanelSettings::limitFoldersPanelToHome();
 }
 
 void FoldersPanel::setAutoScrolling(bool enable)
 {
-    m_treeView->setAutoHorizontalScroll(enable);
+    // TODO: Not supported yet in Dolphin 2.0
     FoldersPanelSettings::setAutoScrolling(enable);
 }
 
@@ -100,206 +86,286 @@ bool FoldersPanel::autoScrolling() const
     return FoldersPanelSettings::autoScrolling();
 }
 
-void FoldersPanel::rename(const KFileItemitem)
+void FoldersPanel::rename(const KFileItem &item)
 {
-    if (DolphinSettings::instance().generalSettings()->renameInline()) {
-        const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
-        const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
-        m_treeView->edit(proxyIndex);
+    if (GeneralSettings::renameInline()) {
+        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().protocol().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;
     }
 
-    if (m_dirLister != 0) {
-        m_setLeafVisible = true;
+    if (m_controller) {
         loadTree(url());
     }
 
     return true;
 }
 
-void FoldersPanel::showEvent(QShowEvent* event)
+void FoldersPanel::reloadTree()
+{
+    if (m_controller) {
+        loadTree(url(), AllowJumpHome);
+    }
+}
+
+void FoldersPanel::showEvent(QShowEvent *event)
 {
     if (event->spontaneous()) {
         Panel::showEvent(event);
         return;
     }
 
-    if (m_dirLister == 0) {
-        // Postpone the creating of the dir lister to the first show event.
-        // This assures that no performance and memory overhead is given when the TreeView is not
-        // used at all (see FoldersPanel::setUrl()).
-        m_dirLister = new KDirLister();
-        m_dirLister->setDirOnlyMode(true);
-        m_dirLister->setAutoUpdate(true);
-        m_dirLister->setMainWindow(window());
-        m_dirLister->setDelayedMimeTypes(true);
-        m_dirLister->setAutoErrorHandlingEnabled(false, this);
-        m_dirLister->setShowingDotFiles(FoldersPanelSettings::showHiddenFiles());
-        connect(m_dirLister, SIGNAL(completed()), this, SLOT(slotDirListerCompleted()));
-
-        Q_ASSERT(m_dolphinModel == 0);
-        m_dolphinModel = new DolphinModel(this);
-        m_dolphinModel->setDirLister(m_dirLister);
-        m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
-        connect(m_dolphinModel, SIGNAL(expand(const QModelIndex&)),
-                this, SLOT(expandToDir(const QModelIndex&)));
-
-        Q_ASSERT(m_proxyModel == 0);
-        m_proxyModel = new DolphinSortFilterProxyModel(this);
-        m_proxyModel->setSourceModel(m_dolphinModel);
-
-        Q_ASSERT(m_treeView == 0);
-        m_treeView = new PanelTreeView(this);
-        m_treeView->setModel(m_proxyModel);
-        m_proxyModel->setSorting(DolphinView::SortByName);
-        m_proxyModel->setSortOrder(Qt::AscendingOrder);
-
-        new FolderExpander(m_treeView, m_proxyModel);
-
-        connect(m_treeView, SIGNAL(clicked(const QModelIndex&)),
-                this, SLOT(updateActiveView(const QModelIndex&)));
-        connect(m_treeView, SIGNAL(urlsDropped(const QModelIndex&, QDropEvent*)),
-                this, SLOT(dropUrls(const QModelIndex&, QDropEvent*)));
-        connect(m_treeView, SIGNAL(pressed(const QModelIndex&)),
-                this, SLOT(updateMouseButtons()));
-
-        QVBoxLayout* layout = new QVBoxLayout(this);
-        layout->setMargin(0);
-        layout->addWidget(m_treeView);
-
-        setAutoScrolling(FoldersPanelSettings::autoScrolling());
-        setShowHiddenFiles(FoldersPanelSettings::showHiddenFiles());
+    if (!m_controller) {
+        // Postpone the creating of the controller to the first show 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->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
+        // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
+        // opening the folders panel.
+        view->setOpacity(0);
+
+        connect(view, &KFileItemListView::roleEditingFinished, this, &FoldersPanel::slotRoleEditingFinished);
+
+        m_model = new KFileItemModel(this);
+        m_model->setShowDirectoriesOnly(true);
+        m_model->setShowHiddenFiles(FoldersPanelSettings::hiddenFilesShown());
+        // Use a QueuedConnection to give the view the possibility to react first on the
+        // finished loading.
+        connect(m_model, &KFileItemModel::directoryLoadingCompleted, this, &FoldersPanel::slotLoadingCompleted, Qt::QueuedConnection);
+
+        m_controller = new KItemListController(m_model, view, this);
+        m_controller->setSelectionBehavior(KItemListController::SingleSelection);
+        m_controller->setAutoActivationBehavior(KItemListController::ExpansionOnly);
+        m_controller->setMouseDoubleClickAction(KItemListController::ActivateAndExpandItem);
+        m_controller->setSingleClickActivationEnforced(true);
+
+        connect(m_controller, &KItemListController::itemActivated, this, &FoldersPanel::slotItemActivated);
+        connect(m_controller, &KItemListController::itemMiddleClicked, this, &FoldersPanel::slotItemMiddleClicked);
+        connect(m_controller, &KItemListController::itemContextMenuRequested, this, &FoldersPanel::slotItemContextMenuRequested);
+        connect(m_controller, &KItemListController::viewContextMenuRequested, this, &FoldersPanel::slotViewContextMenuRequested);
+        connect(m_controller, &KItemListController::itemDropEvent, this, &FoldersPanel::slotItemDropEvent);
+
+        KItemListContainer *container = new KItemListContainer(m_controller, this);
+#ifndef QT_NO_ACCESSIBILITY
+        view->setAccessibleParentsObject(container);
+#endif
+        container->setEnabledFrame(false);
+
+        QVBoxLayout *layout = new QVBoxLayout(this);
+        layout->setContentsMargins(0, 0, 0, 0);
+        layout->addWidget(container);
     }
 
     loadTree(url());
     Panel::showEvent(event);
 }
 
-void FoldersPanel::contextMenuEvent(QContextMenuEvent* event)
-{
-    Panel::contextMenuEvent(event);
-
-    KFileItem item;
-    const QModelIndex index = m_treeView->indexAt(event->pos());
-    if (index.isValid()) {
-        const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
-        item = m_dolphinModel->itemForIndex(dolphinModelIndex);
-    }
-
-    QPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, item);
-    contextMenu->open();
-    delete contextMenu;
-}
-
-void FoldersPanel::keyPressEvent(QKeyEvent* event)
+void FoldersPanel::keyPressEvent(QKeyEvent *event)
 {
     const int key = event->key();
     if ((key == Qt::Key_Enter) || (key == Qt::Key_Return)) {
         event->accept();
-        updateActiveView(m_treeView->currentIndex());
     } else {
         Panel::keyPressEvent(event);
     }
 }
 
-void FoldersPanel::updateActiveView(const QModelIndex& index)
+void FoldersPanel::slotItemActivated(int index)
 {
-    const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
-    const KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
+    const KFileItem item = m_model->fileItem(index);
     if (!item.isNull()) {
-        emit changeUrl(item.url(), m_mouseButtons);
+        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());
+        }
     }
 }
 
-void FoldersPanel::dropUrls(const QModelIndex& index, QDropEvent* event)
+void FoldersPanel::slotItemMiddleClicked(int index)
 {
-    if (index.isValid()) {
-        const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
-        KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
-        Q_ASSERT(!item.isNull());
-        if (item.isDir()) {
-            DragAndDropHelper::instance().dropUrls(item, item.url(), event, this);
+    const KFileItem item = m_model->fileItem(index);
+    if (!item.isNull()) {
+        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::expandToDir(const QModelIndex& index)
+void FoldersPanel::slotItemContextMenuRequested(int index, const QPointF &pos)
+{
+    const KFileItem fileItem = m_model->fileItem(index);
+
+    QPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, fileItem);
+    contextMenu.data()->open(pos.toPoint());
+    if (contextMenu.data()) {
+        delete contextMenu.data();
+    }
+}
+
+void FoldersPanel::slotViewContextMenuRequested(const QPointF &pos)
 {
-    m_treeView->setExpanded(index, true);
-    selectLeafDirectory();
+    QPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, KFileItem());
+    contextMenu.data()->open(pos.toPoint());
+    if (contextMenu.data()) {
+        delete contextMenu.data();
+    }
 }
 
-void FoldersPanel::scrollToLeaf()
+void FoldersPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent *event)
 {
-    const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
-    const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
-    if (proxyIndex.isValid()) {
-        m_treeView->scrollTo(proxyIndex);
+    if (index >= 0) {
+        KFileItem destItem = m_model->fileItem(index);
+        if (destItem.isNull()) {
+            return;
+        }
+
+        QDropEvent dropEvent(event->pos().toPoint(), event->possibleActions(), event->mimeData(), event->buttons(), event->modifiers());
+
+        KIO::DropJob *job = DragAndDropHelper::dropUrls(destItem.mostLocalUrl(), &dropEvent, this);
+        if (job) {
+            connect(job, &KIO::DropJob::result, this, [this](KJob *job) {
+                if (job->error() && job->error() != KIO::ERR_USER_CANCELED) {
+                    Q_EMIT errorMessage(job->errorString());
+                }
+            });
+        }
     }
 }
 
-void FoldersPanel::updateMouseButtons()
+void FoldersPanel::slotRoleEditingFinished(int index, const QByteArray &role, const QVariant &value)
 {
-    m_mouseButtons = QApplication::mouseButtons();
+    if (role == "text") {
+        const KFileItem item = m_model->fileItem(index);
+        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));
+
+            KIO::Job *job = KIO::moveAs(oldUrl, newUrl);
+            KJobWidgets::setWindow(job, this);
+            KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Rename, {oldUrl}, newUrl, job);
+            job->uiDelegate()->setAutoErrorHandlingEnabled(true);
+        }
+    }
 }
 
-void FoldersPanel::slotDirListerCompleted()
+void FoldersPanel::slotLoadingCompleted()
 {
-    m_treeView->resizeColumnToContents(DolphinModel::Name);
-
-    if (m_setLeafVisible) {
-        // Invoke scrollToLeaf() asynchronously. This assures that
-        // the horizontal scrollbar is shown after resizing the column
-        // (otherwise the scrollbar might hide the leaf).
-        QTimer::singleShot(0, this, SLOT(scrollToLeaf()));
-        m_setLeafVisible = false;
+    if (m_controller->view()->opacity() == 0) {
+        // The loading of the initial tree after opening the Folders panel
+        // has been finished. Trigger the increasing of the opacity after
+        // a short delay to give the view the chance to finish its internal
+        // animations.
+        // TODO: Check whether it makes sense to allow accessing the
+        // view-internal delay for usecases like this.
+        QTimer::singleShot(250, this, &FoldersPanel::startFadeInAnimation);
+    }
+
+    if (!m_updateCurrentItem) {
+        return;
     }
+
+    const int index = m_model->index(url());
+    updateCurrentItem(index);
+    m_updateCurrentItem = false;
 }
 
-void FoldersPanel::loadTree(const KUrl& url)
+void FoldersPanel::startFadeInAnimation()
 {
-    Q_ASSERT(m_dirLister != 0);
-    m_leafDir = url;
+    QPropertyAnimation *anim = new QPropertyAnimation(m_controller->view(), "opacity", this);
+    anim->setStartValue(0);
+    anim->setEndValue(1);
+    anim->setEasingCurve(QEasingCurve::InOutQuad);
+    anim->start(QAbstractAnimation::DeleteWhenStopped);
+    anim->setDuration(200);
+}
 
-    KUrl baseUrl;
-    if (url.isLocalFile()) {
-        // use the root directory as base for local URLs (#150941)
-        baseUrl = QDir::rootPath();
-    } else {
-        // clear the path for non-local URLs and use it as base
+void FoldersPanel::loadTree(const QUrl &url, FoldersPanel::NavigationBehaviour navigationBehaviour)
+{
+    Q_ASSERT(m_controller);
+
+    m_updateCurrentItem = false;
+    bool jumpHome = false;
+
+    QUrl baseUrl;
+    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_dirLister->url() != baseUrl) {
-        m_dirLister->stop();
-        m_dirLister->openUrl(baseUrl, KDirLister::Reload);
+    if (m_model->directory() != baseUrl && !jumpHome) {
+        m_updateCurrentItem = true;
+        m_model->refreshDirectory(baseUrl);
+    }
+
+    const int index = m_model->index(url);
+    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
     }
-    m_dolphinModel->expandToUrl(m_leafDir);
 }
 
-void FoldersPanel::selectLeafDirectory()
+void FoldersPanel::updateCurrentItem(int index)
 {
-    const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
-    const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
-    if (proxyIndex.isValid()) {
-        QItemSelectionModel* selModel = m_treeView->selectionModel();
-        selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::ClearAndSelect);
-    }
+    KItemListSelectionManager *selectionManager = m_controller->selectionManager();
+    selectionManager->setCurrentItem(index);
+    selectionManager->clearSelection();
+    selectionManager->setSelected(index);
+
+    m_controller->view()->scrollToItem(index);
 }
 
-#include "folderspanel.moc"
+#include "moc_folderspanel.cpp"