diff --git a/src/panels/folders/folderspanel.cpp b/src/panels/folders/folderspanel.cpp index 95953f66b..020c41e55 100644 --- a/src/panels/folders/folderspanel.cpp +++ b/src/panels/folders/folderspanel.cpp @@ -1,367 +1,363 @@ /*************************************************************************** * Copyright (C) 2006-2010 by Peter Penz * * * * 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 * ***************************************************************************/ #include "folderspanel.h" #include "dolphin_folderspanelsettings.h" #include "dolphin_generalsettings.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 "treeviewcontextmenu.h" #include "views/draganddrophelper.h" #include "views/renamedialog.h" #include #include #include #include #include #include #include #include #include #include #include FoldersPanel::FoldersPanel(QWidget* parent) : Panel(parent), m_updateCurrentItem(false), m_controller(nullptr), m_model(nullptr) { setLayoutDirection(Qt::LeftToRight); } FoldersPanel::~FoldersPanel() { FoldersPanelSettings::self()->save(); if (m_controller) { KItemListView* view = m_controller->view(); m_controller->setView(nullptr); delete view; } } void FoldersPanel::setShowHiddenFiles(bool show) { FoldersPanelSettings::setHiddenFilesShown(show); m_model->setShowHiddenFiles(show); } 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 FoldersPanelSettings::setAutoScrolling(enable); } bool FoldersPanel::autoScrolling() const { return FoldersPanelSettings::autoScrolling(); } void FoldersPanel::rename(const KFileItem& item) { 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->open(); } } bool FoldersPanel::urlChanged() { if (!url().isValid() || url().scheme().contains(QStringLiteral("search"))) { // Skip results shown by a search, as possible identical // directory names are useless without parent-path information. return false; } if (m_controller) { loadTree(url()); } return true; } void FoldersPanel::reloadTree() { if (m_controller) { loadTree(url(), AllowJumpHome); } } void FoldersPanel::showEvent(QShowEvent* event) { if (event->spontaneous()) { Panel::showEvent(event); return; } 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()); 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->setAutoActivationDelay(750); 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); container->setEnabledFrame(false); QVBoxLayout* layout = new QVBoxLayout(this); layout->setContentsMargins(0, 0, 0, 0); layout->addWidget(container); } loadTree(url()); Panel::showEvent(event); } void FoldersPanel::keyPressEvent(QKeyEvent* event) { const int key = event->key(); if ((key == Qt::Key_Enter) || (key == Qt::Key_Return)) { event->accept(); } else { Panel::keyPressEvent(event); } } void FoldersPanel::slotItemActivated(int index) { const KFileItem item = m_model->fileItem(index); if (!item.isNull()) { emit folderActivated(item.url()); } } void FoldersPanel::slotItemMiddleClicked(int index) { const KFileItem item = m_model->fileItem(index); if (!item.isNull()) { emit folderMiddleClicked(item.url()); } } void FoldersPanel::slotItemContextMenuRequested(int index, const QPointF& pos) { - Q_UNUSED(pos); - const KFileItem fileItem = m_model->fileItem(index); QPointer contextMenu = new TreeViewContextMenu(this, fileItem); - contextMenu.data()->open(); + contextMenu.data()->open(pos.toPoint()); if (contextMenu.data()) { delete contextMenu.data(); } } void FoldersPanel::slotViewContextMenuRequested(const QPointF& pos) { - Q_UNUSED(pos); - QPointer contextMenu = new TreeViewContextMenu(this, KFileItem()); - contextMenu.data()->open(); + contextMenu.data()->open(pos.toPoint()); if (contextMenu.data()) { delete contextMenu.data(); } } void FoldersPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event) { 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()) emit errorMessage(job->errorString()); }); } } } void FoldersPanel::slotRoleEditingFinished(int index, const QByteArray& role, const QVariant& value) { 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 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::slotLoadingCompleted() { 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::startFadeInAnimation() { 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); } 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(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 && !jumpHome) { m_updateCurrentItem = true; m_model->refreshDirectory(baseUrl); } const int index = m_model->index(url); if (jumpHome) { 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 } } void FoldersPanel::updateCurrentItem(int index) { KItemListSelectionManager* selectionManager = m_controller->selectionManager(); selectionManager->setCurrentItem(index); selectionManager->clearSelection(); selectionManager->setSelected(index); m_controller->view()->scrollToItem(index); } diff --git a/src/panels/folders/treeviewcontextmenu.cpp b/src/panels/folders/treeviewcontextmenu.cpp index f8cb0b4d6..df9b9d62e 100644 --- a/src/panels/folders/treeviewcontextmenu.cpp +++ b/src/panels/folders/treeviewcontextmenu.cpp @@ -1,252 +1,252 @@ /*************************************************************************** * Copyright (C) 2006-2010 by Peter Penz * * Copyright (C) 2006 by Cvetoslav Ludmiloff * * * * 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 * ***************************************************************************/ #include "treeviewcontextmenu.h" #include "folderspanel.h" #include "global.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include TreeViewContextMenu::TreeViewContextMenu(FoldersPanel* parent, const KFileItem& fileInfo) : QObject(parent), m_parent(parent), m_fileItem(fileInfo) { } TreeViewContextMenu::~TreeViewContextMenu() { } -void TreeViewContextMenu::open() +void TreeViewContextMenu::open(const QPoint& pos) { QMenu* popup = new QMenu(m_parent); if (!m_fileItem.isNull()) { KFileItemListProperties capabilities(KFileItemList() << m_fileItem); // insert 'Cut', 'Copy' and 'Paste' QAction* cutAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-cut")), i18nc("@action:inmenu", "Cut"), this); cutAction->setEnabled(capabilities.supportsMoving()); connect(cutAction, &QAction::triggered, this, &TreeViewContextMenu::cut); QAction* copyAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-copy")), i18nc("@action:inmenu", "Copy"), this); connect(copyAction, &QAction::triggered, this, &TreeViewContextMenu::copy); const QMimeData *mimeData = QApplication::clipboard()->mimeData(); bool canPaste; const QString text = KIO::pasteActionText(mimeData, &canPaste, m_fileItem); QAction* pasteAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this); connect(pasteAction, &QAction::triggered, this, &TreeViewContextMenu::paste); pasteAction->setEnabled(canPaste); popup->addAction(cutAction); popup->addAction(copyAction); popup->addAction(pasteAction); popup->addSeparator(); // insert 'Rename' QAction* renameAction = new QAction(i18nc("@action:inmenu", "Rename..."), this); renameAction->setEnabled(capabilities.supportsMoving()); renameAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename"))); connect(renameAction, &QAction::triggered, this, &TreeViewContextMenu::rename); popup->addAction(renameAction); // insert 'Move to Trash' and (optionally) 'Delete' KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals); KConfigGroup configGroup(globalConfig, "KDE"); bool showDeleteCommand = configGroup.readEntry("ShowDeleteCommand", false); const QUrl url = m_fileItem.url(); if (url.isLocalFile()) { QAction* moveToTrashAction = new QAction(QIcon::fromTheme(QStringLiteral("user-trash")), i18nc("@action:inmenu", "Move to Trash"), this); const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving(); moveToTrashAction->setEnabled(enableMoveToTrash); connect(moveToTrashAction, &QAction::triggered, this, &TreeViewContextMenu::moveToTrash); popup->addAction(moveToTrashAction); } else { showDeleteCommand = true; } if (showDeleteCommand) { QAction* deleteAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18nc("@action:inmenu", "Delete"), this); deleteAction->setEnabled(capabilities.supportsDeleting()); connect(deleteAction, &QAction::triggered, this, &TreeViewContextMenu::deleteItem); popup->addAction(deleteAction); } popup->addSeparator(); } // insert 'Show Hidden Files' QAction* showHiddenFilesAction = new QAction(i18nc("@action:inmenu", "Show Hidden Files"), this); showHiddenFilesAction->setCheckable(true); showHiddenFilesAction->setChecked(m_parent->showHiddenFiles()); popup->addAction(showHiddenFilesAction); connect(showHiddenFilesAction, &QAction::toggled, this, &TreeViewContextMenu::setShowHiddenFiles); if (!m_fileItem.isNull()) { // insert 'Limit to Home Directory' const QUrl url = m_fileItem.url(); const bool enableLimitToHomeDirectory = url.isLocalFile(); QAction* limitFoldersPanelToHomeAction = new QAction(i18nc("@action:inmenu", "Limit to Home Directory"), this); limitFoldersPanelToHomeAction->setCheckable(true); limitFoldersPanelToHomeAction->setEnabled(enableLimitToHomeDirectory); limitFoldersPanelToHomeAction->setChecked(m_parent->limitFoldersPanelToHome()); popup->addAction(limitFoldersPanelToHomeAction); connect(limitFoldersPanelToHomeAction, &QAction::toggled, this, &TreeViewContextMenu::setLimitFoldersPanelToHome); } // insert 'Automatic Scrolling' QAction* autoScrollingAction = new QAction(i18nc("@action:inmenu", "Automatic Scrolling"), this); autoScrollingAction->setCheckable(true); autoScrollingAction->setChecked(m_parent->autoScrolling()); // TODO: Temporary disabled. Horizontal autoscrolling will be implemented later either // in KItemViews or manually as part of the FoldersPanel //popup->addAction(autoScrollingAction); connect(autoScrollingAction, &QAction::toggled, this, &TreeViewContextMenu::setAutoScrolling); if (!m_fileItem.isNull()) { // insert 'Properties' entry QAction* propertiesAction = new QAction(i18nc("@action:inmenu", "Properties"), this); propertiesAction->setIcon(QIcon::fromTheme(QStringLiteral("document-properties"))); connect(propertiesAction, &QAction::triggered, this, &TreeViewContextMenu::showProperties); popup->addAction(propertiesAction); } QList customActions = m_parent->customContextMenuActions(); if (!customActions.isEmpty()) { popup->addSeparator(); foreach (QAction* action, customActions) { popup->addAction(action); } } QPointer popupPtr = popup; - popup->exec(QCursor::pos()); + popup->exec(pos); if (popupPtr.data()) { popupPtr.data()->deleteLater(); } } void TreeViewContextMenu::populateMimeData(QMimeData* mimeData, bool cut) { QList kdeUrls; kdeUrls.append(m_fileItem.url()); QList mostLocalUrls; bool dummy; mostLocalUrls.append(m_fileItem.mostLocalUrl(dummy)); KIO::setClipboardDataCut(mimeData, cut); KUrlMimeData::setUrls(kdeUrls, mostLocalUrls, mimeData); } void TreeViewContextMenu::cut() { QMimeData* mimeData = new QMimeData(); populateMimeData(mimeData, true); QApplication::clipboard()->setMimeData(mimeData); } void TreeViewContextMenu::copy() { QMimeData* mimeData = new QMimeData(); populateMimeData(mimeData, false); QApplication::clipboard()->setMimeData(mimeData); } void TreeViewContextMenu::paste() { KIO::PasteJob *job = KIO::paste(QApplication::clipboard()->mimeData(), m_fileItem.url()); KJobWidgets::setWindow(job, m_parent); } void TreeViewContextMenu::rename() { m_parent->rename(m_fileItem); } void TreeViewContextMenu::moveToTrash() { const QList list{m_fileItem.url()}; KIO::JobUiDelegate uiDelegate; uiDelegate.setWindow(m_parent); if (uiDelegate.askDeleteConfirmation(list, KIO::JobUiDelegate::Trash, KIO::JobUiDelegate::DefaultConfirmation)) { KIO::Job* job = KIO::trash(list); KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Trash, list, QUrl(QStringLiteral("trash:/")), job); KJobWidgets::setWindow(job, m_parent); job->uiDelegate()->setAutoErrorHandlingEnabled(true); } } void TreeViewContextMenu::deleteItem() { const QList list{m_fileItem.url()}; KIO::JobUiDelegate uiDelegate; uiDelegate.setWindow(m_parent); if (uiDelegate.askDeleteConfirmation(list, KIO::JobUiDelegate::Delete, KIO::JobUiDelegate::DefaultConfirmation)) { KIO::Job* job = KIO::del(list); KJobWidgets::setWindow(job, m_parent); job->uiDelegate()->setAutoErrorHandlingEnabled(true); } } void TreeViewContextMenu::showProperties() { KPropertiesDialog* dialog = new KPropertiesDialog(m_fileItem.url(), m_parent); dialog->setAttribute(Qt::WA_DeleteOnClose); dialog->show(); } void TreeViewContextMenu::setShowHiddenFiles(bool show) { m_parent->setShowHiddenFiles(show); } void TreeViewContextMenu::setLimitFoldersPanelToHome(bool enable) { m_parent->setLimitFoldersPanelToHome(enable); } void TreeViewContextMenu::setAutoScrolling(bool enable) { m_parent->setAutoScrolling(enable); } diff --git a/src/panels/folders/treeviewcontextmenu.h b/src/panels/folders/treeviewcontextmenu.h index a9491fb5c..245b7d6b0 100644 --- a/src/panels/folders/treeviewcontextmenu.h +++ b/src/panels/folders/treeviewcontextmenu.h @@ -1,102 +1,102 @@ /*************************************************************************** * Copyright (C) 2006 by Peter Penz * * * * 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 * ***************************************************************************/ #ifndef TREEVIEWCONTEXTMENU_H #define TREEVIEWCONTEXTMENU_H #include #include class QMimeData; class FoldersPanel; /** * @brief Represents the context menu which appears when doing a right * click on an item of the treeview. */ class TreeViewContextMenu : public QObject { Q_OBJECT public: /** * @parent Pointer to the folders panel the context menu * belongs to. * @fileInfo Pointer to the file item the context menu * is applied. If 0 is passed, the context menu * is above the viewport. */ TreeViewContextMenu(FoldersPanel* parent, const KFileItem& fileInfo); ~TreeViewContextMenu() override; /** Opens the context menu modal. */ - void open(); + void open(const QPoint& pos); private slots: /** Cuts the item m_fileItem. */ void cut(); /** Copies the item m_fileItem. */ void copy(); /** Paste the clipboard to m_fileItem. */ void paste(); /** Renames the item m_fileItem. */ void rename(); /** Moves the item m_fileItem to the trash. */ void moveToTrash(); /** Deletes the item m_fileItem. */ void deleteItem(); /** Shows the properties of the item m_fileItem. */ void showProperties(); /** * Sets the 'Show Hidden Files' setting for the * folders panel to \a show. */ void setShowHiddenFiles(bool show); /** * Sets the 'Limit folders panel to home' setting for the * folders panel to \a enable. */ void setLimitFoldersPanelToHome(bool enable); /** * Sets the 'Automatic Scrolling' setting for the * folders panel to \a enable. */ void setAutoScrolling(bool enable); private: void populateMimeData(QMimeData* mimeData, bool cut); private: FoldersPanel* m_parent; KFileItem m_fileItem; }; #endif