Add "Add to Places" action to file menu

Summary:
It's recommended that actions available in context menus be available in the main menu
as well for discoverability's sake. This patch does so for the "Add to Places" action.

The action is moved over to the main window, and accessed in the context menu via the
actionCollection it lives in.

BUG: 390757
FIXED-IN: 19.08.0

Test Plan:
- Action still works
- Action still appears in context menu when relevant
- Action in the File menu only becomes enabled when only a single directory is selected or nothing is selected

{F7143876}

{F7143877}

{F7143878}

{F7143879}

Reviewers: #dolphin, elvisangelaccio

Reviewed By: #dolphin, elvisangelaccio

Subscribers: elvisangelaccio, kfm-devel

Tags: #dolphin

Differential Revision: https://phabricator.kde.org/D22149
This commit is contained in:
Nate Graham 2019-09-01 15:01:57 -06:00
parent 3a7586907e
commit 7cad80b292
4 changed files with 64 additions and 39 deletions

View file

@ -1,4 +1,4 @@
/***************************************************************************
/***************************************************************************
* Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and *
* Cvetoslav Ludmiloff *
* *
@ -192,7 +192,6 @@ void DolphinContextMenu::openItemContextMenu()
QAction* openParentAction = nullptr;
QAction* openParentInNewWindowAction = nullptr;
QAction* openParentInNewTabAction = nullptr;
QAction* addToPlacesAction = nullptr;
const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();
KFileItemActions fileItemActions;
@ -210,9 +209,7 @@ void DolphinContextMenu::openItemContextMenu()
// insert 'Add to Places' entry
if (!placeExists(m_fileInfo.url())) {
addToPlacesAction = addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
i18nc("@action:inmenu Add selected folder to places",
"Add to Places"));
addAction(m_mainWindow->actionCollection()->action(QStringLiteral("add_to_places")));
}
addSeparator();
@ -314,14 +311,7 @@ void DolphinContextMenu::openItemContextMenu()
QAction* activatedAction = exec(m_pos);
if (activatedAction) {
if (activatedAction == addToPlacesAction) {
const QUrl selectedUrl(m_fileInfo.url());
if (selectedUrl.isValid()) {
PlacesItemModel model;
const QString text = selectedUrl.fileName();
model.createPlacesItem(text, selectedUrl, KIO::iconNameForUrl(selectedUrl));
}
} else if (activatedAction == openParentAction) {
if (activatedAction == openParentAction) {
m_command = OpenParentFolder;
} else if (activatedAction == openParentInNewWindowAction) {
m_command = OpenParentFolderInNewWindow;
@ -365,10 +355,8 @@ void DolphinContextMenu::openViewportContextMenu()
addAction(m_mainWindow->actionCollection()->action(QStringLiteral("new_tab")));
// Insert 'Add to Places' entry if exactly one item is selected
QAction* addToPlacesAction = nullptr;
if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
addToPlacesAction = addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
i18nc("@action:inmenu Add current folder to places", "Add to Places"));
addAction(m_mainWindow->actionCollection()->action(QStringLiteral("add_to_places")));
}
addSeparator();
@ -395,22 +383,6 @@ void DolphinContextMenu::openViewportContextMenu()
addAction(propertiesAction);
addShowMenuBarAction();
QAction* action = exec(m_pos);
if (addToPlacesAction && (action == addToPlacesAction)) {
const DolphinViewContainer* container = m_mainWindow->activeViewContainer();
const QUrl url = container->url();
if (url.isValid()) {
PlacesItemModel model;
QString icon;
if (container->isSearchModeEnabled()) {
icon = QStringLiteral("folder-saved-search-symbolic");
} else {
icon = KIO::iconNameForUrl(url);
}
model.createPlacesItem(container->placesText(), url, icon);
}
}
}
void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties)

View file

@ -32,6 +32,7 @@
#include "dolphintabpage.h"
#include "middleclickactioneventfilter.h"
#include "panels/folders/folderspanel.h"
#include "panels/places/placesitemmodel.h"
#include "panels/places/placespanel.h"
#include "panels/information/informationpanel.h"
#include "panels/terminal/terminalpanel.h"
@ -272,7 +273,7 @@ void DolphinMainWindow::changeUrl(const QUrl &url)
}
m_activeViewContainer->setUrl(url);
updateEditActions();
updateFileAndEditActions();
updatePasteAction();
updateViewActions();
updateGoActions();
@ -301,7 +302,7 @@ void DolphinMainWindow::slotEditableStateChanged(bool editable)
void DolphinMainWindow::slotSelectionChanged(const KFileItemList& selection)
{
updateEditActions();
updateFileAndEditActions();
const int selectedUrlsCount = m_tabWidget->currentTabPage()->selectedItemsCount();
@ -352,6 +353,32 @@ void DolphinMainWindow::openNewActivatedTab()
m_tabWidget->openNewActivatedTab();
}
void DolphinMainWindow::addToPlaces()
{
QUrl url;
QString name;
// If nothing is selected, act on the current dir
if (m_activeViewContainer->view()->selectedItems().count() == 0) {
url = m_activeViewContainer->url();
name = m_activeViewContainer->placesText();
} else {
const auto dirToAdd = m_activeViewContainer->view()->selectedItems().first();
url = dirToAdd.url();
name = dirToAdd.name();
}
if (url.isValid()) {
PlacesItemModel model;
QString icon;
if (m_activeViewContainer->isSearchModeEnabled()) {
icon = QStringLiteral("folder-saved-search-symbolic");
} else {
icon = KIO::iconNameForUrl(url);
}
model.createPlacesItem(name, url, icon);
}
}
void DolphinMainWindow::openNewTab(const QUrl& url, DolphinTabWidget::TabPlacement tabPlacement)
{
m_tabWidget->openNewTab(url, QUrl(), tabPlacement);
@ -1109,7 +1136,7 @@ void DolphinMainWindow::activeViewChanged(DolphinViewContainer* viewContainer)
m_actionHandler->setCurrentView(viewContainer->view());
updateHistory();
updateEditActions();
updateFileAndEditActions();
updatePasteAction();
updateViewActions();
updateGoActions();
@ -1182,6 +1209,12 @@ void DolphinMainWindow::setupActions()
actionCollection()->setDefaultShortcuts(newTab, {Qt::CTRL + Qt::Key_T, Qt::CTRL + Qt::SHIFT + Qt::Key_N});
connect(newTab, &QAction::triggered, this, &DolphinMainWindow::openNewActivatedTab);
QAction* addToPlaces = actionCollection()->addAction(QStringLiteral("add_to_places"));
addToPlaces->setIcon(QIcon::fromTheme(QStringLiteral("bookmark-new")));
addToPlaces->setWhatsThis(xi18nc("@info:whatsthis", "This adds the selected folder"
"to the Places panel."));
connect(addToPlaces, &QAction::triggered, this, &DolphinMainWindow::addToPlaces);
QAction* closeTab = KStandardAction::close(m_tabWidget, QOverload<>::of(&DolphinTabWidget::closeTab), actionCollection());
closeTab->setText(i18nc("@action:inmenu File", "Close Tab"));
closeTab->setWhatsThis(i18nc("@info:whatsthis", "This closes the "
@ -1681,15 +1714,20 @@ void DolphinMainWindow::setupDockWidgets()
});
}
void DolphinMainWindow::updateEditActions()
void DolphinMainWindow::updateFileAndEditActions()
{
const KFileItemList list = m_activeViewContainer->view()->selectedItems();
const KActionCollection* col = actionCollection();
QAction* addToPlacesAction = col->action(QStringLiteral("add_to_places"));
if (list.isEmpty()) {
stateChanged(QStringLiteral("has_no_selection"));
addToPlacesAction->setText(i18nc("@action:inmenu Add current folder to places", "Add '%1' to Places", m_activeViewContainer->placesText()));
} else {
stateChanged(QStringLiteral("has_selection"));
KActionCollection* col = actionCollection();
QAction* renameAction = col->action(KStandardAction::name(KStandardAction::RenameFile));
QAction* moveToTrashAction = col->action(KStandardAction::name(KStandardAction::MoveToTrash));
QAction* deleteAction = col->action(KStandardAction::name(KStandardAction::DeleteFile));
@ -1697,6 +1735,14 @@ void DolphinMainWindow::updateEditActions()
QAction* deleteWithTrashShortcut = col->action(QStringLiteral("delete_shortcut")); // see DolphinViewActionHandler
QAction* showTarget = col->action(QStringLiteral("show_target"));
if (list.length() == 1 && list.first().isDir()) {
addToPlacesAction->setEnabled(true);
addToPlacesAction->setText(i18nc("@action:inmenu Add current folder to places", "Add '%1' to Places", list.first().name()));
} else {
addToPlacesAction->setEnabled(false);
addToPlacesAction->setText(i18nc("@action:inmenu Add current folder to places", "Add to Places"));
}
KFileItemListProperties capabilities(list);
const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();

View file

@ -384,6 +384,11 @@ private slots:
*/
void openNewActivatedTab();
/**
* Adds the current URL as an entry to the Places panel
*/
void addToPlaces();
/**
* Opens a new tab in the background showing the URL \a url.
*/
@ -515,7 +520,7 @@ private:
*/
void setupDockWidgets();
void updateEditActions();
void updateFileAndEditActions();
void updateViewActions();
void updateGoActions();

View file

@ -1,5 +1,5 @@
<!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
<kpartgui name="dolphin" version="24">
<kpartgui name="dolphin" version="25">
<MenuBar>
<Menu name="file">
<Action name="new_menu" />
@ -8,6 +8,8 @@
<Action name="file_close" />
<Action name="undo_close_tab" />
<Separator/>
<Action name="add_to_places" />
<Separator/>
<Action name="renamefile" />
<Action name="movetotrash" />
<Action name="deletefile" />