X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/0603e18cd4e36b988196a99810f2e3e803fe3125..863ee3a87cee8b1f22a311d6a6a62e56714b5eae:/src/dolphincontextmenu.cpp diff --git a/src/dolphincontextmenu.cpp b/src/dolphincontextmenu.cpp index 340af6bd0..8c52aac39 100644 --- a/src/dolphincontextmenu.cpp +++ b/src/dolphincontextmenu.cpp @@ -6,13 +6,14 @@ #include "dolphincontextmenu.h" -#include "dolphin_generalsettings.h" #include "dolphin_contextmenusettings.h" +#include "dolphin_generalsettings.h" #include "dolphinmainwindow.h" #include "dolphinnewfilemenu.h" #include "dolphinplacesmodelsingleton.h" #include "dolphinremoveaction.h" #include "dolphinviewcontainer.h" +#include "global.h" #include "trash/dolphintrash.h" #include "views/dolphinview.h" #include "views/viewmodecontroller.h" @@ -27,45 +28,33 @@ #include #include #include -#include #include -#include +#include #include #include #include -#include -#include - -DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent, - const QPoint& pos, - const KFileItem& fileInfo, - const QUrl& baseUrl, - KFileItemActions *fileItemActions) : - QMenu(parent), - m_pos(pos), - m_mainWindow(parent), - m_fileInfo(fileInfo), - m_baseUrl(baseUrl), - m_baseFileItem(nullptr), - m_selectedItems(), - m_selectedItemsProperties(nullptr), - m_context(NoContext), - m_copyToMenu(parent), - m_customActions(), - m_command(None), - m_removeAction(nullptr), - m_fileItemActions(fileItemActions) -{ - // The context menu either accesses the URLs of the selected items - // or the items itself. To increase the performance both lists are cached. - const DolphinView* view = m_mainWindow->activeViewContainer()->view(); - m_selectedItems = view->selectedItems(); +DolphinContextMenu::DolphinContextMenu(DolphinMainWindow *parent, + const KFileItem &fileInfo, + const KFileItemList &selectedItems, + const QUrl &baseUrl, + KFileItemActions *fileItemActions) + : QMenu(parent) + , m_mainWindow(parent) + , m_fileInfo(fileInfo) + , m_baseUrl(baseUrl) + , m_baseFileItem(nullptr) + , m_selectedItems(selectedItems) + , m_selectedItemsProperties(nullptr) + , m_context(NoContext) + , m_copyToMenu(parent) + , m_removeAction(nullptr) + , m_fileItemActions(fileItemActions) +{ QApplication::instance()->installEventFilter(this); - static_cast(m_mainWindow->actionCollection()-> - action(QStringLiteral("hamburger_menu")))->addToMenu(this); + addAllActions(); } DolphinContextMenu::~DolphinContextMenu() @@ -76,13 +65,10 @@ DolphinContextMenu::~DolphinContextMenu() m_selectedItemsProperties = nullptr; } -void DolphinContextMenu::setCustomActions(const QList& actions) +void DolphinContextMenu::addAllActions() { - m_customActions = actions; -} + static_cast(m_mainWindow->actionCollection()->action(QStringLiteral("hamburger_menu")))->addToMenu(this); -DolphinContextMenu::Command DolphinContextMenu::open() -{ // get the context information const auto scheme = m_baseUrl.scheme(); if (scheme == QLatin1String("trash")) { @@ -101,25 +87,23 @@ DolphinContextMenu::Command DolphinContextMenu::open() // open the corresponding popup for the context if (m_context & TrashContext) { if (m_context & ItemContext) { - openTrashItemContextMenu(); + addTrashItemContextMenu(); } else { - openTrashContextMenu(); + addTrashContextMenu(); } } else if (m_context & ItemContext) { - openItemContextMenu(); + addItemContextMenu(); } else { - openViewportContextMenu(); + addViewportContextMenu(); } - - return m_command; } -bool DolphinContextMenu::eventFilter(QObject* object, QEvent* event) +bool DolphinContextMenu::eventFilter(QObject *object, QEvent *event) { Q_UNUSED(object) - if(event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) { - QKeyEvent* keyEvent = static_cast(event); + 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) { @@ -133,39 +117,25 @@ bool DolphinContextMenu::eventFilter(QObject* object, QEvent* event) return false; } -void DolphinContextMenu::openTrashContextMenu() +void DolphinContextMenu::addTrashContextMenu() { Q_ASSERT(m_context & TrashContext); - QAction* emptyTrashAction = new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this); + QAction *emptyTrashAction = addAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), [this]() { + Trash::empty(m_mainWindow); + }); emptyTrashAction->setEnabled(!Trash::isEmpty()); - addAction(emptyTrashAction); - addCustomActions(); - - QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); + QAction *propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); addAction(propertiesAction); - - if (exec(m_pos) == emptyTrashAction) { - Trash::empty(m_mainWindow); - } } -void DolphinContextMenu::openTrashItemContextMenu() +void DolphinContextMenu::addTrashItemContextMenu() { Q_ASSERT(m_context & TrashContext); Q_ASSERT(m_context & ItemContext); - QAction* restoreAction = new QAction(QIcon::fromTheme("restoration"), i18nc("@action:inmenu", "Restore"), m_mainWindow); - addAction(restoreAction); - - QAction* deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)); - addAction(deleteAction); - - QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); - addAction(propertiesAction); - - if (exec(m_pos) == restoreAction) { + addAction(QIcon::fromTheme("restoration"), i18nc("@action:inmenu", "Restore"), [this]() { QList selectedUrls; selectedUrls.reserve(m_selectedItems.count()); for (const KFileItem &item : qAsConst(m_selectedItems)) { @@ -175,13 +145,19 @@ void DolphinContextMenu::openTrashItemContextMenu() KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls); KJobWidgets::setWindow(job, m_mainWindow); job->uiDelegate()->setAutoErrorHandlingEnabled(true); - } + }); + + QAction *deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)); + addAction(deleteAction); + + QAction *propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); + addAction(propertiesAction); } void DolphinContextMenu::addDirectoryItemContextMenu() { // insert 'Open in new window' and 'Open in new tab' entries - const KFileItemListProperties& selectedItemsProps = selectedItemsProperties(); + const KFileItemListProperties &selectedItemsProps = selectedItemsProperties(); if (ContextMenuSettings::showOpenInNewTab()) { addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab"))); } @@ -193,15 +169,21 @@ void DolphinContextMenu::addDirectoryItemContextMenu() addOpenWithActions(); // set up 'Create New' menu - DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection(), m_mainWindow); - const DolphinView* view = m_mainWindow->activeViewContainer()->view(); + DolphinNewFileMenu *newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection(), m_mainWindow); +#if KIO_VERSION >= QT_VERSION_CHECK(5, 100, 0) + newFileMenu->setNewFolderShortcutAction(m_mainWindow->actionCollection()->action("create_dir")); +#endif newFileMenu->checkUpToDate(); +#if KIO_VERSION >= QT_VERSION_CHECK(5, 97, 0) + newFileMenu->setWorkingDirectory(m_fileInfo.url()); +#else newFileMenu->setPopupFiles(QList() << m_fileInfo.url()); +#endif newFileMenu->setEnabled(selectedItemsProps.supportsWriting()); connect(newFileMenu, &DolphinNewFileMenu::fileCreated, newFileMenu, &DolphinNewFileMenu::deleteLater); connect(newFileMenu, &DolphinNewFileMenu::directoryCreated, newFileMenu, &DolphinNewFileMenu::deleteLater); - QMenu* menu = newFileMenu->menu(); + QMenu *menu = newFileMenu->menu(); menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New")); menu->setIcon(QIcon::fromTheme(QStringLiteral("list-add"))); addMenu(menu); @@ -209,15 +191,11 @@ void DolphinContextMenu::addDirectoryItemContextMenu() addSeparator(); } -void DolphinContextMenu::openItemContextMenu() +void DolphinContextMenu::addItemContextMenu() { Q_ASSERT(!m_fileInfo.isNull()); - QAction* openParentAction = nullptr; - QAction* openParentInNewWindowAction = nullptr; - QAction* openParentInNewTabAction = nullptr; - const KFileItemListProperties& selectedItemsProps = selectedItemsProperties(); - + const KFileItemListProperties &selectedItemsProps = selectedItemsProperties(); m_fileItemActions->setItemListProperties(selectedItemsProps); @@ -228,23 +206,19 @@ void DolphinContextMenu::openItemContextMenu() } else if (m_context & TimelineContext || m_context & SearchContext) { addOpenWithActions(); - openParentAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), - i18nc("@action:inmenu", - "Open Path"), - this); - addAction(openParentAction); + addAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), i18nc("@action:inmenu", "Open Path"), [this]() { + m_mainWindow->changeUrl(KIO::upUrl(m_fileInfo.url())); + m_mainWindow->activeViewContainer()->view()->markUrlsAsSelected({m_fileInfo.url()}); + m_mainWindow->activeViewContainer()->view()->markUrlAsCurrent(m_fileInfo.url()); + }); - openParentInNewWindowAction = new QAction(QIcon::fromTheme(QStringLiteral("window-new")), - i18nc("@action:inmenu", - "Open Path in New Window"), - this); - addAction(openParentInNewWindowAction); + addAction(QIcon::fromTheme(QStringLiteral("window-new")), i18nc("@action:inmenu", "Open Path in New Window"), [this]() { + Dolphin::openNewWindow({m_fileInfo.url()}, m_mainWindow, Dolphin::OpenNewWindowFlag::Select); + }); - openParentInNewTabAction = new QAction(QIcon::fromTheme(QStringLiteral("tab-new")), - i18nc("@action:inmenu", - "Open Path in New Tab"), - this); - addAction(openParentInNewTabAction); + addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "Open Path in New Tab"), [this]() { + m_mainWindow->openNewTab(KIO::upUrl(m_fileInfo.url())); + }); addSeparator(); } else { @@ -259,7 +233,7 @@ void DolphinContextMenu::openItemContextMenu() // multiple files bool selectionHasOnlyDirs = true; for (const auto &item : qAsConst(m_selectedItems)) { - const QUrl& url = DolphinView::openItemAsFolderUrl(item); + const QUrl &url = DolphinView::openItemAsFolderUrl(item); if (url.isEmpty()) { selectionHasOnlyDirs = false; break; @@ -286,48 +260,48 @@ void DolphinContextMenu::openItemContextMenu() m_copyToMenu.addActionsTo(this); } - // insert 'Properties...' entry - addSeparator(); - QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); - addAction(propertiesAction); + if (m_mainWindow->isSplitViewEnabledInCurrentTab()) { + if (ContextMenuSettings::showCopyToOtherSplitView()) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("copy_to_inactive_split_view"))); + } - QAction* activatedAction = exec(m_pos); - if (activatedAction) { - if (activatedAction == openParentAction) { - m_command = OpenParentFolder; - } else if (activatedAction == openParentInNewWindowAction) { - m_command = OpenParentFolderInNewWindow; - } else if (activatedAction == openParentInNewTabAction) { - m_command = OpenParentFolderInNewTab; + if (ContextMenuSettings::showMoveToOtherSplitView()) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("move_to_inactive_split_view"))); } } + + // insert 'Properties...' entry + addSeparator(); + QAction *propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); + addAction(propertiesAction); } -void DolphinContextMenu::openViewportContextMenu() +void DolphinContextMenu::addViewportContextMenu() { - const DolphinView* view = m_mainWindow->activeViewContainer()->view(); - const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem()); m_fileItemActions->setItemListProperties(baseUrlProperties); // Set up and insert 'Create New' menu - KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu(); + KNewFileMenu *newFileMenu = m_mainWindow->newFileMenu(); newFileMenu->checkUpToDate(); +#if KIO_VERSION >= QT_VERSION_CHECK(5, 97, 0) + newFileMenu->setWorkingDirectory(m_baseUrl); +#else newFileMenu->setPopupFiles(QList() << m_baseUrl); +#endif addMenu(newFileMenu->menu()); // 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(); - QAction* pasteAction = createPasteAction(); + QAction *pasteAction = createPasteAction(); if (pasteAction) { addAction(pasteAction); } // Insert 'Add to Places' entry if it's not already in the places panel - if (ContextMenuSettings::showAddToPlaces() && - !placeExists(m_mainWindow->activeViewContainer()->url())) { + if (ContextMenuSettings::showAddToPlaces() && !placeExists(m_mainWindow->activeViewContainer()->url())) { addAction(m_mainWindow->actionCollection()->action(QStringLiteral("add_to_places"))); } addSeparator(); @@ -344,29 +318,26 @@ void DolphinContextMenu::openViewportContextMenu() } addAdditionalActions(baseUrlProperties); - addCustomActions(); addSeparator(); - QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); + QAction *propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); addAction(propertiesAction); - - exec(m_pos); } -void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties) +void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties &properties) { - const KActionCollection* collection = m_mainWindow->actionCollection(); + const KActionCollection *collection = m_mainWindow->actionCollection(); // Insert 'Cut', 'Copy', 'Copy Location' and 'Paste' addAction(collection->action(KStandardAction::name(KStandardAction::Cut))); addAction(collection->action(KStandardAction::name(KStandardAction::Copy))); if (ContextMenuSettings::showCopyLocation()) { - QAction* copyPathAction = collection->action(QString("copy_location")); + QAction *copyPathAction = collection->action(QString("copy_location")); copyPathAction->setEnabled(m_selectedItems.size() == 1); addAction(copyPathAction); } - QAction* pasteAction = createPasteAction(); + QAction *pasteAction = createPasteAction(); if (pasteAction) { addAction(pasteAction); } @@ -380,20 +351,15 @@ void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& addAction(collection->action(KStandardAction::name(KStandardAction::RenameFile))); // Insert 'Add to Places' entry if appropriate - if (ContextMenuSettings::showAddToPlaces() && - m_selectedItems.count() == 1 && - m_fileInfo.isDir() && - !placeExists(m_fileInfo.url())) { + if (ContextMenuSettings::showAddToPlaces() && m_selectedItems.count() == 1 && m_fileInfo.isDir() && !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()); - const bool showMoveToTrashAction = (properties.isLocal() && - properties.supportsMoving()); + const bool showDeleteAction = (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) || !properties.isLocal()); + const bool showMoveToTrashAction = (properties.isLocal() && properties.supportsMoving()); if (showDeleteAction && showMoveToTrashAction) { delete m_removeAction; @@ -411,18 +377,18 @@ void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& } } -bool DolphinContextMenu::placeExists(const QUrl& url) const +bool DolphinContextMenu::placeExists(const QUrl &url) const { - const KFilePlacesModel* placesModel = DolphinPlacesModelSingleton::instance().placesModel(); + const KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel(); - const auto& matchedPlaces = placesModel->match(placesModel->index(0,0), KFilePlacesModel::UrlRole, url, 1, Qt::MatchExactly); + const auto &matchedPlaces = placesModel->match(placesModel->index(0, 0), KFilePlacesModel::UrlRole, url, 1, Qt::MatchExactly); return !matchedPlaces.isEmpty(); } -QAction* DolphinContextMenu::createPasteAction() +QAction *DolphinContextMenu::createPasteAction() { - QAction* action = nullptr; + QAction *action = nullptr; KFileItem destItem; if (!m_fileInfo.isNull() && m_selectedItems.count() <= 1) { destItem = m_fileInfo; @@ -448,7 +414,7 @@ QAction* DolphinContextMenu::createPasteAction() return action; } -KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const +KFileItemListProperties &DolphinContextMenu::selectedItemsProperties() const { if (!m_selectedItemsProperties) { m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems); @@ -459,7 +425,7 @@ KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const KFileItem DolphinContextMenu::baseFileItem() { if (!m_baseFileItem) { - const DolphinView* view = m_mainWindow->activeViewContainer()->view(); + const DolphinView *view = m_mainWindow->activeViewContainer()->view(); KFileItem baseItem = view->rootItem(); if (baseItem.isNull() || baseItem.url() != m_baseUrl) { m_baseFileItem = new KFileItem(m_baseUrl); @@ -476,26 +442,20 @@ void DolphinContextMenu::addOpenWithActions() m_fileItemActions->insertOpenWithActionsTo(nullptr, this, QStringList{qApp->desktopFileName()}); } -void DolphinContextMenu::addCustomActions() -{ - addActions(m_customActions); -} - void DolphinContextMenu::addAdditionalActions(const KFileItemListProperties &props) { addSeparator(); QList additionalActions; - if (props.isDirectory() && props.isLocal() && ContextMenuSettings::showOpenTerminal()) { - additionalActions << m_mainWindow->actionCollection()->action(QStringLiteral("open_terminal")); + if (props.isLocal() && ContextMenuSettings::showOpenTerminal()) { + additionalActions << m_mainWindow->actionCollection()->action(QStringLiteral("open_terminal_here")); } m_fileItemActions->addActionsTo(this, KFileItemActions::MenuActionSource::All, additionalActions); - const DolphinView* view = m_mainWindow->activeViewContainer()->view(); - const QList versionControlActions = view->versionControlActions(m_selectedItems); + const DolphinView *view = m_mainWindow->activeViewContainer()->view(); + const QList versionControlActions = view->versionControlActions(m_selectedItems); if (!versionControlActions.isEmpty()) { addActions(versionControlActions); addSeparator(); } } -