X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/275d7b537de4bf6db971c9cb1d591ffe512272f9..1b192c67f9735cd724c4958e7ba32a7b549770b6:/src/dolphincontextmenu.cpp diff --git a/src/dolphincontextmenu.cpp b/src/dolphincontextmenu.cpp index 912caf859..eabd81e22 100644 --- a/src/dolphincontextmenu.cpp +++ b/src/dolphincontextmenu.cpp @@ -1,22 +1,8 @@ - /*************************************************************************** - * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and * - * 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 * - ***************************************************************************/ +/* + * SPDX-FileCopyrightText: 2006 Peter Penz (peter.penz@gmx.at) and Cvetoslav Ludmiloff + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ #include "dolphincontextmenu.h" @@ -50,7 +36,6 @@ #include #include #include -#include #include #include @@ -76,6 +61,8 @@ DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent, // or the items itself. To increase the performance both lists are cached. const DolphinView* view = m_mainWindow->activeViewContainer()->view(); m_selectedItems = view->selectedItems(); + + installEventFilter(this); } DolphinContextMenu::~DolphinContextMenu() @@ -118,27 +105,34 @@ DolphinContextMenu::Command DolphinContextMenu::open() } else if (m_context & ItemContext) { openItemContextMenu(); } else { - Q_ASSERT(m_context == NoContext); openViewportContextMenu(); } return m_command; } -void DolphinContextMenu::keyPressEvent(QKeyEvent *ev) +void DolphinContextMenu::childEvent(QChildEvent* event) { - if (m_removeAction && ev->key() == Qt::Key_Shift) { - m_removeAction->update(DolphinRemoveAction::ShiftState::Pressed); + if(event->added()) { + event->child()->installEventFilter(this); } - QMenu::keyPressEvent(ev); + QMenu::childEvent(event); } -void DolphinContextMenu::keyReleaseEvent(QKeyEvent *ev) +bool DolphinContextMenu::eventFilter(QObject* dest, QEvent* event) { - if (m_removeAction && ev->key() == Qt::Key_Shift) { - m_removeAction->update(DolphinRemoveAction::ShiftState::Released); + if(event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) { + QKeyEvent* keyEvent = static_cast(event); + if(m_removeAction && keyEvent->key() == Qt::Key_Shift) { + if(event->type() == QEvent::KeyPress) { + m_removeAction->update(DolphinRemoveAction::ShiftState::Pressed); + } else { + m_removeAction->update(DolphinRemoveAction::ShiftState::Released); + } + return true; + } } - QMenu::keyReleaseEvent(ev); + return QMenu::eventFilter(dest, event); } void DolphinContextMenu::openTrashContextMenu() @@ -178,7 +172,7 @@ void DolphinContextMenu::openTrashItemContextMenu() if (exec(m_pos) == restoreAction) { QList selectedUrls; selectedUrls.reserve(m_selectedItems.count()); - foreach (const KFileItem &item, m_selectedItems) { + for (const KFileItem &item : qAsConst(m_selectedItems)) { selectedUrls.append(item.url()); } @@ -213,6 +207,7 @@ void DolphinContextMenu::addDirectoryItemContextMenu(KFileItemActions &fileItemA QMenu* menu = newFileMenu->menu(); menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New")); menu->setIcon(QIcon::fromTheme(QStringLiteral("document-new"))); + menu->setParent(this, Qt::Popup); addMenu(menu); addSeparator(); @@ -228,7 +223,7 @@ void DolphinContextMenu::openItemContextMenu() const KFileItemListProperties& selectedItemsProps = selectedItemsProperties(); KFileItemActions fileItemActions; - fileItemActions.setParentWidget(m_mainWindow); + fileItemActions.setParentWidget(this); fileItemActions.setItemListProperties(selectedItemsProps); if (m_selectedItems.count() == 1) { @@ -286,21 +281,7 @@ void DolphinContextMenu::openItemContextMenu() insertDefaultItemActions(selectedItemsProps); - // insert 'Add to Places' entry if appropriate - if (m_selectedItems.count() == 1) { - if (m_fileInfo.isDir()) { - if (!placeExists(m_fileInfo.url())) { - addAction(m_mainWindow->actionCollection()->action(QStringLiteral("add_to_places"))); - } - } - } - - addSeparator(); - - fileItemActions.addServiceActionsTo(this); - fileItemActions.addPluginActionsTo(this); - - addVersionControlPluginActions(); + addAdditionalActions(fileItemActions, selectedItemsProps); // insert 'Copy To' and 'Move To' sub menus if (GeneralSettings::showCopyMoveMenu()) { @@ -331,13 +312,7 @@ void DolphinContextMenu::openViewportContextMenu() { const DolphinView* view = m_mainWindow->activeViewContainer()->view(); - // Insert 'Open With' entries - KFileItem baseItem = view->rootItem(); - if (baseItem.isNull() || baseItem.url() != m_baseUrl) { - baseItem = baseFileItem(); - } - - const KFileItemListProperties baseUrlProperties(KFileItemList() << baseItem); + const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem()); KFileItemActions fileItemActions; fileItemActions.setParentWidget(m_mainWindow); fileItemActions.setItemListProperties(baseUrlProperties); @@ -349,14 +324,14 @@ void DolphinContextMenu::openViewportContextMenu() newFileMenu->setPopupFiles(QList() << m_baseUrl); addMenu(newFileMenu->menu()); - // Don't show "Open With" menu items if the current dir is empty, because there's - // generally no app that can do anything interesting with an empty directory - if (view->itemsCount() != 0) { - addOpenWithActions(fileItemActions); - } + // Show "open with" menu items even if the dir is empty, because there are legitimate + // use cases for this, such as opening an empty dir in Kate or VSCode or something + addOpenWithActions(fileItemActions); QAction* pasteAction = createPasteAction(); - addAction(pasteAction); + if (pasteAction) { + addAction(pasteAction); + } // Insert 'Add to Places' entry if it's not already in the places panel if (!placeExists(m_mainWindow->activeViewContainer()->url())) { @@ -368,14 +343,7 @@ void DolphinContextMenu::openViewportContextMenu() addAction(m_mainWindow->actionCollection()->action(QStringLiteral("sort"))); addAction(m_mainWindow->actionCollection()->action(QStringLiteral("view_mode"))); - addSeparator(); - - // Insert service actions - fileItemActions.addServiceActionsTo(this); - fileItemActions.addPluginActionsTo(this); - - addVersionControlPluginActions(); - + addAdditionalActions(fileItemActions, baseUrlProperties); addCustomActions(); addSeparator(); @@ -392,20 +360,32 @@ void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& { const KActionCollection* collection = m_mainWindow->actionCollection(); - // Insert 'Cut', 'Copy', 'Copy location' and 'Paste' + // Insert 'Cut', 'Copy', 'Copy Location' and 'Paste' addAction(collection->action(KStandardAction::name(KStandardAction::Cut))); addAction(collection->action(KStandardAction::name(KStandardAction::Copy))); QAction* copyPathAction = collection->action(QString("copy_location")); copyPathAction->setEnabled(m_selectedItems.size() == 1); addAction(copyPathAction); - addAction(createPasteAction()); + QAction* pasteAction = createPasteAction(); + if (pasteAction) { + addAction(pasteAction); + } addAction(m_mainWindow->actionCollection()->action(QStringLiteral("duplicate"))); - addSeparator(); - // Insert 'Rename' addAction(collection->action(KStandardAction::name(KStandardAction::RenameFile))); + // insert 'Add to Places' entry if appropriate + if (m_selectedItems.count() == 1) { + if (m_fileInfo.isDir()) { + if (!placeExists(m_fileInfo.url())) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("add_to_places"))); + } + } + } + + addSeparator(); + // Insert 'Move to Trash' and/or 'Delete' const bool showDeleteAction = (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) || !properties.isLocal()); @@ -450,16 +430,26 @@ bool DolphinContextMenu::placeExists(const QUrl& url) const QAction* DolphinContextMenu::createPasteAction() { QAction* action = nullptr; - const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir(); - if (isDir && (m_selectedItems.count() == 1)) { + KFileItem destItem; + if (!m_fileInfo.isNull() && m_selectedItems.count() <= 1) { + destItem = m_fileInfo; + } else { + destItem = baseFileItem(); + } + + if (!destItem.isNull() && destItem.isDir()) { const QMimeData *mimeData = QApplication::clipboard()->mimeData(); bool canPaste; - const QString text = KIO::pasteActionText(mimeData, &canPaste, m_fileInfo); - action = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this); - action->setEnabled(canPaste); - connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder); - } else { - action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste)); + const QString text = KIO::pasteActionText(mimeData, &canPaste, destItem); + if (canPaste) { + if (destItem == m_fileInfo) { + // if paste destination is a selected folder + action = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this); + connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder); + } else { + action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste)); + } + } } return action; @@ -476,7 +466,13 @@ KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const KFileItem DolphinContextMenu::baseFileItem() { if (!m_baseFileItem) { - m_baseFileItem = new KFileItem(m_baseUrl); + const DolphinView* view = m_mainWindow->activeViewContainer()->view(); + KFileItem baseItem = view->rootItem(); + if (baseItem.isNull() || baseItem.url() != m_baseUrl) { + m_baseFileItem = new KFileItem(m_baseUrl); + } else { + m_baseFileItem = new KFileItem(baseItem); + } } return *m_baseFileItem; } @@ -487,8 +483,21 @@ void DolphinContextMenu::addOpenWithActions(KFileItemActions& fileItemActions) fileItemActions.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp->desktopFileName())); } -void DolphinContextMenu::addVersionControlPluginActions() +void DolphinContextMenu::addCustomActions() +{ + addActions(m_customActions); +} + +void DolphinContextMenu::addAdditionalActions(KFileItemActions &fileItemActions, const KFileItemListProperties &props) { + addSeparator(); + + QList additionalActions; + if (props.isDirectory() && props.isLocal()) { + additionalActions << m_mainWindow->actionCollection()->action(QStringLiteral("open_terminal")); + } + fileItemActions.addActionsTo(this, KFileItemActions::MenuActionSource::All, additionalActions); + const DolphinView* view = m_mainWindow->activeViewContainer()->view(); const QList versionControlActions = view->versionControlActions(m_selectedItems); if (!versionControlActions.isEmpty()) { @@ -497,8 +506,3 @@ void DolphinContextMenu::addVersionControlPluginActions() } } -void DolphinContextMenu::addCustomActions() -{ - addActions(m_customActions); -} -