X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/513fe3bb428c9d10f40a93efeca60fbf95d89bec..d57990e681397d3dc2d2ebf737eced730dd3ecf7:/src/dolphincontextmenu.cpp diff --git a/src/dolphincontextmenu.cpp b/src/dolphincontextmenu.cpp index 6c1fa4ad3..e1c67aad1 100644 --- a/src/dolphincontextmenu.cpp +++ b/src/dolphincontextmenu.cpp @@ -6,8 +6,8 @@ #include "dolphincontextmenu.h" -#include "dolphin_generalsettings.h" #include "dolphin_contextmenusettings.h" +#include "dolphin_generalsettings.h" #include "dolphinmainwindow.h" #include "dolphinnewfilemenu.h" #include "dolphinplacesmodelsingleton.h" @@ -16,7 +16,6 @@ #include "global.h" #include "trash/dolphintrash.h" #include "views/dolphinview.h" -#include "views/viewmodecontroller.h" #include #include @@ -28,33 +27,29 @@ #include #include #include -#include #include -#include -#include #include #include #include -#include -#include +#include -DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent, - const KFileItem& fileInfo, +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) + 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); @@ -71,8 +66,7 @@ DolphinContextMenu::~DolphinContextMenu() void DolphinContextMenu::addAllActions() { - static_cast(m_mainWindow->actionCollection()-> - action(QStringLiteral("hamburger_menu")))->addToMenu(this); + static_cast(m_mainWindow->actionCollection()->action(QStringLiteral("hamburger_menu")))->addToMenu(this); // get the context information const auto scheme = m_baseUrl.scheme(); @@ -82,6 +76,8 @@ void DolphinContextMenu::addAllActions() m_context |= SearchContext; } else if (scheme.contains(QLatin1String("timeline"))) { m_context |= TimelineContext; + } else if (scheme == QStringLiteral("recentlyused")) { + m_context |= RecentlyUsedContext; } if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) { @@ -103,12 +99,12 @@ void DolphinContextMenu::addAllActions() } } -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) { @@ -126,12 +122,24 @@ void DolphinContextMenu::addTrashContextMenu() { Q_ASSERT(m_context & TrashContext); - QAction *emptyTrashAction = addAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), [this](){ + QAction *emptyTrashAction = addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18nc("@action:inmenu", "Empty Trash"), this, [this]() { Trash::empty(m_mainWindow); }); emptyTrashAction->setEnabled(!Trash::isEmpty()); - QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); + // Insert 'Sort By' and 'View Mode' + if (ContextMenuSettings::showSortBy() || ContextMenuSettings::showViewMode()) { + addSeparator(); + } + if (ContextMenuSettings::showSortBy()) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("sort"))); + } + if (ContextMenuSettings::showViewMode()) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("view_mode"))); + } + + addSeparator(); + QAction *propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); addAction(propertiesAction); } @@ -140,29 +148,44 @@ void DolphinContextMenu::addTrashItemContextMenu() Q_ASSERT(m_context & TrashContext); Q_ASSERT(m_context & ItemContext); - addAction(QIcon::fromTheme("restoration"), i18nc("@action:inmenu", "Restore"), [this](){ - QList selectedUrls; - selectedUrls.reserve(m_selectedItems.count()); - for (const KFileItem &item : qAsConst(m_selectedItems)) { - selectedUrls.append(item.url()); - } + addAction(QIcon::fromTheme(QStringLiteral("edit-reset")), + i18ncp("@action:inmenu Restore the selected files that are in the trash to the place they lived at the moment they were trashed. Minimize the " + "length of this string if possible.", + "Restore to Former Location", + "Restore to Former Locations", + m_selectedItems.count()), + this, + [this]() { + QList selectedUrls; + selectedUrls.reserve(m_selectedItems.count()); + for (const KFileItem &item : std::as_const(m_selectedItems)) { + selectedUrls.append(item.url()); + } + + KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls); + KJobWidgets::setWindow(job, m_mainWindow); + job->uiDelegate()->setAutoErrorHandlingEnabled(true); + }); - KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls); - KJobWidgets::setWindow(job, m_mainWindow); - job->uiDelegate()->setAutoErrorHandlingEnabled(true); - }); + addSeparator(); - QAction* deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)); + addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Cut))); + addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Copy))); + + addSeparator(); + + QAction *deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)); addAction(deleteAction); - QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); - addAction(propertiesAction); + addSeparator(); + + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("properties"))); } 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"))); } @@ -170,22 +193,24 @@ void DolphinContextMenu::addDirectoryItemContextMenu() addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window"))); } + if (ContextMenuSettings::showOpenInSplitView()) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_split_view"))); + } + // Insert 'Open With' entries addOpenWithActions(); // set up 'Create New' menu - DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection(), m_mainWindow); + QAction *newDirAction = m_mainWindow->actionCollection()->action(QStringLiteral("create_dir")); + QAction *newFileAction = m_mainWindow->actionCollection()->action(QStringLiteral("create_file")); + DolphinNewFileMenu *newFileMenu = new DolphinNewFileMenu(newDirAction, newFileAction, this); 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); @@ -193,11 +218,34 @@ void DolphinContextMenu::addDirectoryItemContextMenu() addSeparator(); } +void DolphinContextMenu::addOpenParentFolderActions() +{ + addAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), i18nc("@action:inmenu", "Open Path"), [this]() { + const QUrl url = m_fileInfo.targetUrl(); + const QUrl parentUrl = KIO::upUrl(url); + m_mainWindow->changeUrl(parentUrl); + m_mainWindow->activeViewContainer()->view()->markUrlsAsSelected({url}); + m_mainWindow->activeViewContainer()->view()->markUrlAsCurrent(url); + }); + + addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "Open Path in New Tab"), [this]() { + const QUrl url = m_fileInfo.targetUrl(); + const QUrl parentUrl = KIO::upUrl(url); + DolphinTabPage *tabPage = m_mainWindow->openNewTab(parentUrl); + tabPage->activeViewContainer()->view()->markUrlsAsSelected({url}); + tabPage->activeViewContainer()->view()->markUrlAsCurrent(url); + }); + + addAction(QIcon::fromTheme(QStringLiteral("window-new")), i18nc("@action:inmenu", "Open Path in New Window"), [this]() { + Dolphin::openNewWindow({m_fileInfo.targetUrl()}, m_mainWindow, Dolphin::OpenNewWindowFlag::Select); + }); +} + void DolphinContextMenu::addItemContextMenu() { Q_ASSERT(!m_fileInfo.isNull()); - const KFileItemListProperties& selectedItemsProps = selectedItemsProperties(); + const KFileItemListProperties &selectedItemsProps = selectedItemsProperties(); m_fileItemActions->setItemListProperties(selectedItemsProps); @@ -205,31 +253,10 @@ void DolphinContextMenu::addItemContextMenu() // single files if (m_fileInfo.isDir()) { addDirectoryItemContextMenu(); - } else if (m_context & TimelineContext || m_context & SearchContext) { + } else if (m_context & TimelineContext || m_context & SearchContext || m_context & RecentlyUsedContext) { addOpenWithActions(); - 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()); - }); - - 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); - }); - - addAction(QIcon::fromTheme(QStringLiteral("tab-new")), - i18nc("@action:inmenu", - "Open Path in New Tab"), - [this](){ - m_mainWindow->openNewTab(KIO::upUrl(m_fileInfo.url())); - }); + addOpenParentFolderActions(); addSeparator(); } else { @@ -243,8 +270,8 @@ void DolphinContextMenu::addItemContextMenu() } else { // multiple files bool selectionHasOnlyDirs = true; - for (const auto &item : qAsConst(m_selectedItems)) { - const QUrl& url = DolphinView::openItemAsFolderUrl(item); + for (const auto &item : std::as_const(m_selectedItems)) { + const QUrl &url = DolphinView::openItemAsFolderUrl(item); if (url.isEmpty()) { selectionHasOnlyDirs = false; break; @@ -271,9 +298,19 @@ void DolphinContextMenu::addItemContextMenu() m_copyToMenu.addActionsTo(this); } + if (m_mainWindow->isSplitViewEnabledInCurrentTab()) { + if (ContextMenuSettings::showCopyToOtherSplitView()) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("copy_to_inactive_split_view"))); + } + + 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")); + QAction *propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); addAction(propertiesAction); } @@ -283,27 +320,22 @@ void DolphinContextMenu::addViewportContextMenu() 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(); @@ -323,23 +355,23 @@ void DolphinContextMenu::addViewportContextMenu() addSeparator(); - QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); + QAction *propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); addAction(propertiesAction); } -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(QStringLiteral("copy_location")); copyPathAction->setEnabled(m_selectedItems.size() == 1); addAction(copyPathAction); } - QAction* pasteAction = createPasteAction(); + QAction *pasteAction = createPasteAction(); if (pasteAction) { addAction(pasteAction); } @@ -353,20 +385,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(QStringLiteral("KDE")).readEntry("ShowDeleteCommand", false) || !properties.isLocal()); + const bool showMoveToTrashAction = (properties.isLocal() && properties.supportsMoving()); if (showDeleteAction && showMoveToTrashAction) { delete m_removeAction; @@ -384,18 +411,17 @@ 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 auto& matchedPlaces = placesModel->match(placesModel->index(0,0), KFilePlacesModel::UrlRole, url, 1, Qt::MatchExactly); + const KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel(); - return !matchedPlaces.isEmpty(); + QModelIndex url_index = placesModel->closestItem(url); + return url_index.isValid() && placesModel->url(url_index).matches(url, QUrl::StripTrailingSlash); } -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; @@ -421,7 +447,7 @@ QAction* DolphinContextMenu::createPasteAction() return action; } -KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const +KFileItemListProperties &DolphinContextMenu::selectedItemsProperties() const { if (!m_selectedItemsProperties) { m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems); @@ -432,7 +458,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); @@ -447,6 +473,23 @@ void DolphinContextMenu::addOpenWithActions() { // insert 'Open With...' action or sub menu m_fileItemActions->insertOpenWithActionsTo(nullptr, this, QStringList{qApp->desktopFileName()}); + + // For a single file, hint in "Open with" menu that middle-clicking would open it in the secondary app. + // (Unless middle-clicking would open it as a folder in a new tab (e.g. archives).) + const QUrl &url = DolphinView::openItemAsFolderUrl(m_fileInfo, GeneralSettings::browseThroughArchives()); + if (m_selectedItems.count() == 1 && url.isEmpty()) { + if (QAction *openWithSubMenu = findChild(QStringLiteral("openWith_submenu"))) { + Q_ASSERT(openWithSubMenu->menu()); + Q_ASSERT(!openWithSubMenu->menu()->isEmpty()); + + auto *secondaryApp = openWithSubMenu->menu()->actions().first(); + // Add it like a keyboard shortcut, Qt uses \t as a separator. + if (!secondaryApp->text().contains(QLatin1Char('\t'))) { + secondaryApp->setText(secondaryApp->text() + QLatin1Char('\t') + + i18nc("@action:inmenu Shortcut, middle click to trigger menu item, keep short", "Middle Click")); + } + } + } } void DolphinContextMenu::addAdditionalActions(const KFileItemListProperties &props) @@ -459,11 +502,13 @@ void DolphinContextMenu::addAdditionalActions(const KFileItemListProperties &pro } 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()) { + addSeparator(); addActions(versionControlActions); addSeparator(); } } +#include "moc_dolphincontextmenu.cpp"