2 * SPDX-FileCopyrightText: 2008 David Faure <faure@kde.org>
3 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
5 * SPDX-License-Identifier: GPL-2.0-or-later
8 #include "dolphinviewactionhandler.h"
10 #include "dolphindebug.h"
11 #include "kitemviews/kfileitemlisttostring.h"
12 #include "kitemviews/kfileitemmodel.h"
13 #include "selectionmode/actiontexthelper.h"
14 #include "settings/viewpropertiesdialog.h"
15 #include "views/zoomlevelinfo.h"
16 #include "kconfig_version.h"
19 #include <Baloo/IndexerConfig>
21 #include <KActionCollection>
22 #include <KActionMenu>
23 #include <KFileItemListProperties>
24 #include <KLocalizedString>
25 #include <KNewFileMenu>
26 #include <KPropertiesDialog>
27 #include <KProtocolManager>
29 #include <QActionGroup>
33 DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection
* collection
, SelectionMode::ActionTextHelper
* actionTextHelper
, QObject
* parent
) :
35 m_actionCollection(collection
),
36 m_currentView(nullptr),
40 Q_ASSERT(m_actionCollection
);
41 createActions(actionTextHelper
);
44 void DolphinViewActionHandler::setCurrentView(DolphinView
* view
)
49 disconnect(m_currentView
, nullptr, this, nullptr);
54 connect(view
, &DolphinView::modeChanged
,
55 this, &DolphinViewActionHandler::updateViewActions
);
56 connect(view
, &DolphinView::previewsShownChanged
,
57 this, &DolphinViewActionHandler::slotPreviewsShownChanged
);
58 connect(view
, &DolphinView::sortOrderChanged
,
59 this, &DolphinViewActionHandler::slotSortOrderChanged
);
60 connect(view
, &DolphinView::sortFoldersFirstChanged
,
61 this, &DolphinViewActionHandler::slotSortFoldersFirstChanged
);
62 connect(view
, &DolphinView::sortHiddenLastChanged
,
63 this, &DolphinViewActionHandler::slotSortHiddenLastChanged
);
64 connect(view
, &DolphinView::visibleRolesChanged
,
65 this, &DolphinViewActionHandler::slotVisibleRolesChanged
);
66 connect(view
, &DolphinView::groupedSortingChanged
,
67 this, &DolphinViewActionHandler::slotGroupedSortingChanged
);
68 connect(view
, &DolphinView::hiddenFilesShownChanged
,
69 this, &DolphinViewActionHandler::slotHiddenFilesShownChanged
);
70 connect(view
, &DolphinView::sortRoleChanged
,
71 this, &DolphinViewActionHandler::slotSortRoleChanged
);
72 connect(view
, &DolphinView::zoomLevelChanged
,
73 this, &DolphinViewActionHandler::slotZoomLevelChanged
);
74 connect(view
, &DolphinView::writeStateChanged
,
75 this, &DolphinViewActionHandler::slotWriteStateChanged
);
76 connect(view
, &DolphinView::selectionModeRequested
,
77 this, [this]() { Q_EMIT
setSelectionMode(true); });
78 connect(view
, &DolphinView::selectionChanged
,
79 this, &DolphinViewActionHandler::slotSelectionChanged
);
80 slotSelectionChanged(m_currentView
->selectedItems());
83 DolphinView
* DolphinViewActionHandler::currentView()
88 void DolphinViewActionHandler::createActions(SelectionMode::ActionTextHelper
*actionTextHelper
)
90 // This action doesn't appear in the GUI, it's for the shortcut only.
91 // KNewFileMenu takes care of the GUI stuff.
92 QAction
* newDirAction
= m_actionCollection
->addAction(QStringLiteral("create_dir"));
93 newDirAction
->setText(i18nc("@action", "Create Folder..."));
94 m_actionCollection
->setDefaultShortcuts(newDirAction
, KStandardShortcut::createFolder());
95 newDirAction
->setIcon(QIcon::fromTheme(QStringLiteral("folder-new")));
96 newDirAction
->setEnabled(false); // Will be enabled in slotWriteStateChanged(bool) if the current URL is writable
97 connect(newDirAction
, &QAction::triggered
, this, &DolphinViewActionHandler::createDirectoryTriggered
);
101 auto renameAction
= KStandardAction::renameFile(this, &DolphinViewActionHandler::slotRename
, m_actionCollection
);
102 renameAction
->setWhatsThis(xi18nc("@info:whatsthis", "This renames the "
103 "items in your current selection.<nl/>Renaming multiple items "
104 "at once amounts to their new names differing only in a number."));
106 auto trashAction
= KStandardAction::moveToTrash(this, &DolphinViewActionHandler::slotTrashActivated
, m_actionCollection
);
107 auto trashShortcuts
= trashAction
->shortcuts();
108 if (!trashShortcuts
.contains(QKeySequence::Delete
)) {
109 trashShortcuts
.append(QKeySequence::Delete
);
110 m_actionCollection
->setDefaultShortcuts(trashAction
, trashShortcuts
);
112 trashAction
->setWhatsThis(xi18nc("@info:whatsthis", "This moves the "
113 "items in your current selection to the <filename>Trash"
114 "</filename>.<nl/>The trash is a temporary storage where "
115 "items can be deleted from if disk space is needed."));
117 auto deleteAction
= KStandardAction::deleteFile(this, &DolphinViewActionHandler::slotDeleteItems
, m_actionCollection
);
118 auto deleteShortcuts
= deleteAction
->shortcuts();
119 if (!deleteShortcuts
.contains(Qt::SHIFT
| Qt::Key_Delete
)) {
120 deleteShortcuts
.append(Qt::SHIFT
| Qt::Key_Delete
);
121 m_actionCollection
->setDefaultShortcuts(deleteAction
, deleteShortcuts
);
123 deleteAction
->setWhatsThis(xi18nc("@info:whatsthis", "This deletes "
124 "the items in your current selection completely. They can "
125 "not be recovered by normal means."));
127 // This action is useful for being enabled when KStandardAction::MoveToTrash should be
128 // disabled and KStandardAction::DeleteFile is enabled (e.g. non-local files), so that Key_Del
129 // can be used for deleting the file (#76016). It needs to be a separate action
130 // so that the Edit menu isn't affected.
131 QAction
* deleteWithTrashShortcut
= m_actionCollection
->addAction(QStringLiteral("delete_shortcut"));
132 // The descriptive text is just for the shortcuts editor.
133 deleteWithTrashShortcut
->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
134 m_actionCollection
->setDefaultShortcuts(deleteWithTrashShortcut
, KStandardShortcut::moveToTrash());
135 deleteWithTrashShortcut
->setEnabled(false);
136 connect(deleteWithTrashShortcut
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDeleteItems
);
138 QAction
* duplicateAction
= m_actionCollection
->addAction(QStringLiteral("duplicate"));
139 duplicateAction
->setText(i18nc("@action:inmenu File", "Duplicate Here"));
140 duplicateAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-duplicate")));
141 m_actionCollection
->setDefaultShortcut(duplicateAction
, Qt::CTRL
| Qt::Key_D
);
142 duplicateAction
->setEnabled(false);
143 connect(duplicateAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDuplicate
);
145 QAction
*propertiesAction
= m_actionCollection
->addAction( QStringLiteral("properties") );
146 // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
147 propertiesAction
->setText( i18nc("@action:inmenu File", "Properties") );
148 propertiesAction
->setWhatsThis(xi18nc("@info:whatsthis properties",
149 "This shows a complete list of properties of the currently "
150 "selected items in a new window.<nl/>If nothing is selected the "
151 "window will be about the currently viewed folder instead.<nl/>"
152 "You can configure advanced options there like managing "
153 "read- and write-permissions."));
154 propertiesAction
->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
155 m_actionCollection
->setDefaultShortcuts(propertiesAction
, {Qt::ALT
| Qt::Key_Return
, Qt::ALT
| Qt::Key_Enter
});
156 connect(propertiesAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotProperties
);
158 QAction
*copyPathAction
= m_actionCollection
->addAction( QStringLiteral("copy_location") );
159 copyPathAction
->setText(i18nc("@action:incontextmenu", "Copy Location"));
160 copyPathAction
->setWhatsThis(i18nc("@info:whatsthis copy_location",
161 "This will copy the path of the first selected item into the clipboard."
164 copyPathAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy-path")));
165 m_actionCollection
->setDefaultShortcuts(copyPathAction
, {Qt::CTRL
| Qt::ALT
| Qt::Key_C
});
166 connect(copyPathAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotCopyPath
);
168 if (actionTextHelper
) {
169 // The "…" at the end make clear that they won't trigger their respective actions directly.
170 actionTextHelper
->registerTextWhenNothingIsSelected(trashAction
, i18nc("@action:inmenu File", "Move to Trash…"));
171 actionTextHelper
->registerTextWhenNothingIsSelected(deleteAction
, i18nc("@action:inmenu File", "Delete…"));
172 actionTextHelper
->registerTextWhenNothingIsSelected(duplicateAction
, i18nc("@action:inmenu File", "Duplicate Here…"));
173 actionTextHelper
->registerTextWhenNothingIsSelected(copyPathAction
, i18nc("@action:incontextmenu", "Copy Location…"));
176 // This menu makes sure that users who don't know how to open a context menu and haven't
177 // figured out how to enable the menu bar can still perform basic file manipulation.
178 // This only works if they know how to select a file.
179 // The text when nothing is selected at least implies that a selection can /somehow/ be made.
180 // This menu is by default only used in the hamburger menu but created here so users can put
181 // it on their toolbar.
182 KActionMenu
*basicActionsMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("basic_actions"), this);
183 // The text is set later depending on the selection in the currently active view.
184 basicActionsMenu
->setPopupMode(QToolButton::InstantPopup
);
185 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Cut
)));
186 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Copy
)));
187 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Paste
)));
188 basicActionsMenu
->addSeparator();
189 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
190 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
191 basicActionsMenu
->addSeparator();
192 basicActionsMenu
->addAction(m_actionCollection
->action(QStringLiteral("properties")));
193 basicActionsMenu
->addSeparator(); // We add one more separator because we sometimes add contextual
194 // actions in slotSelectionChanged() after the static ones above.
197 KToggleAction
* iconsAction
= iconsModeAction();
198 KToggleAction
* compactAction
= compactModeAction();
199 KToggleAction
* detailsAction
= detailsModeAction();
201 iconsAction
->setWhatsThis(xi18nc("@info:whatsthis Icons view mode",
202 "<para>This switches to a view mode that focuses on the folder "
203 "and file icons. This mode makes it easy to distinguish folders "
204 "from files and to detect items with distinctive <emphasis>"
205 "file types</emphasis>.</para><para> This mode is handy to "
206 "browse through pictures when the <interface>Preview"
207 "</interface> option is enabled.</para>"));
208 compactAction
->setWhatsThis(xi18nc("@info:whatsthis Compact view mode",
209 "<para>This switches to a compact view mode that lists the folders "
210 "and files in columns with the names beside the icons.</para><para>"
211 "This helps to keep the overview in folders with many items.</para>"));
212 detailsAction
->setWhatsThis(xi18nc("@info:whatsthis Details view mode",
213 "<para>This switches to a list view mode that focuses on folder "
214 "and file details.</para><para>Click on a detail in the column "
215 "header to sort the items by it. Click again to sort the other "
216 "way around. To select which details should be displayed click "
217 "the header with the right mouse button.</para><para>You can "
218 "view the contents of a folder without leaving the current "
219 "location by clicking to the left of it. This way you can view "
220 "the contents of multiple folders in the same list.</para>"));
222 KSelectAction
* viewModeActions
= m_actionCollection
->add
<KSelectAction
>(QStringLiteral("view_mode"));
223 viewModeActions
->setText(i18nc("@action:intoolbar", "View Mode"));
224 viewModeActions
->addAction(iconsAction
);
225 viewModeActions
->addAction(compactAction
);
226 viewModeActions
->addAction(detailsAction
);
227 viewModeActions
->setToolBarMode(KSelectAction::MenuMode
);
228 connect(viewModeActions
, &KSelectAction::triggered
, this, &DolphinViewActionHandler::slotViewModeActionTriggered
);
230 QAction
* zoomInAction
= KStandardAction::zoomIn(this,
231 &DolphinViewActionHandler::zoomIn
,
233 zoomInAction
->setWhatsThis(i18nc("@info:whatsthis zoom in", "This increases the icon size."));
235 QAction
* zoomResetAction
= m_actionCollection
->addAction(QStringLiteral("view_zoom_reset"));
236 zoomResetAction
->setText(i18nc("@action:inmenu View", "Reset Zoom Level"));
237 zoomResetAction
->setToolTip(i18n("Zoom To Default"));
238 zoomResetAction
->setWhatsThis(i18nc("@info:whatsthis zoom reset", "This resets the icon size to default."));
239 zoomResetAction
->setIcon(QIcon::fromTheme(QStringLiteral("zoom-original")));
240 m_actionCollection
->setDefaultShortcuts(zoomResetAction
, {Qt::CTRL
| Qt::Key_0
});
241 connect(zoomResetAction
, &QAction::triggered
, this, &DolphinViewActionHandler::zoomReset
);
243 QAction
* zoomOutAction
= KStandardAction::zoomOut(this,
244 &DolphinViewActionHandler::zoomOut
,
246 zoomOutAction
->setWhatsThis(i18nc("@info:whatsthis zoom out", "This reduces the icon size."));
248 KActionMenu
* zoomMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("zoom"));
249 zoomMenu
->setText(i18nc("@action:inmenu menu of zoom actions", "Zoom"));
250 zoomMenu
->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
251 zoomMenu
->setPopupMode(QToolButton::InstantPopup
);
252 zoomMenu
->addAction(zoomInAction
);
253 zoomMenu
->addAction(zoomResetAction
);
254 zoomMenu
->addAction(zoomOutAction
);
256 KToggleAction
* showPreview
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_preview"));
257 showPreview
->setText(i18nc("@action:intoolbar", "Show Previews"));
258 showPreview
->setToolTip(i18nc("@info", "Show preview of files and folders"));
259 showPreview
->setWhatsThis(xi18nc("@info:whatsthis", "When this is "
260 "enabled, the icons are based on the actual file or folder "
261 "contents.<nl/>For example the icons of images become scaled "
262 "down versions of the images."));
263 showPreview
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
264 connect(showPreview
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::togglePreview
);
266 KToggleAction
* sortFoldersFirst
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("folders_first"));
267 sortFoldersFirst
->setText(i18nc("@action:inmenu Sort", "Folders First"));
268 connect(sortFoldersFirst
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleSortFoldersFirst
);
270 KToggleAction
* sortHiddenLast
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("hidden_last"));
271 sortHiddenLast
->setText(i18nc("@action:inmenu Sort", "Hidden Files Last"));
272 connect(sortHiddenLast
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleSortHiddenLast
);
275 QActionGroup
* sortByActionGroup
= createFileItemRolesActionGroup(QStringLiteral("sort_by_"));
277 KActionMenu
* sortByActionMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("sort"));
278 sortByActionMenu
->setIcon(QIcon::fromTheme(QStringLiteral("view-sort")));
279 sortByActionMenu
->setText(i18nc("@action:inmenu View", "Sort By"));
280 sortByActionMenu
->setPopupMode(QToolButton::InstantPopup
);
282 const auto sortByActionGroupActions
= sortByActionGroup
->actions();
283 for (QAction
* action
: sortByActionGroupActions
) {
284 sortByActionMenu
->addAction(action
);
287 sortByActionMenu
->addSeparator();
289 QActionGroup
* group
= new QActionGroup(sortByActionMenu
);
290 group
->setExclusive(true);
292 KToggleAction
* ascendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("ascending"));
293 ascendingAction
->setActionGroup(group
);
294 connect(ascendingAction
, &QAction::triggered
, this, [this] {
295 m_currentView
->setSortOrder(Qt::AscendingOrder
);
298 KToggleAction
* descendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("descending"));
299 descendingAction
->setActionGroup(group
);
300 connect(descendingAction
, &QAction::triggered
, this, [this] {
301 m_currentView
->setSortOrder(Qt::DescendingOrder
);
304 sortByActionMenu
->addAction(ascendingAction
);
305 sortByActionMenu
->addAction(descendingAction
);
306 sortByActionMenu
->addSeparator();
307 sortByActionMenu
->addAction(sortFoldersFirst
);
308 sortByActionMenu
->addAction(sortHiddenLast
);
310 // View -> Additional Information
311 QActionGroup
* visibleRolesGroup
= createFileItemRolesActionGroup(QStringLiteral("show_"));
313 KActionMenu
* visibleRolesMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("additional_info"));
314 visibleRolesMenu
->setText(i18nc("@action:inmenu View", "Show Additional Information"));
315 visibleRolesMenu
->setIcon(QIcon::fromTheme(QStringLiteral("documentinfo")));
316 visibleRolesMenu
->setPopupMode(QToolButton::InstantPopup
);
318 const auto visibleRolesGroupActions
= visibleRolesGroup
->actions();
319 for (QAction
* action
: visibleRolesGroupActions
) {
320 visibleRolesMenu
->addAction(action
);
323 KToggleAction
* showInGroups
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_in_groups"));
324 showInGroups
->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
325 showInGroups
->setText(i18nc("@action:inmenu View", "Show in Groups"));
326 showInGroups
->setWhatsThis(i18nc("@info:whatsthis", "This groups files and folders by their first letter."));
327 connect(showInGroups
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleGroupedSorting
);
329 KToggleAction
* showHiddenFiles
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_hidden_files"));
330 showHiddenFiles
->setIcon(QIcon::fromTheme(QStringLiteral("view-visible")));
331 showHiddenFiles
->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
332 showHiddenFiles
->setWhatsThis(xi18nc("@info:whatsthis", "<para>When "
333 "this is enabled <emphasis>hidden</emphasis> files and folders "
334 "are visible. They will be displayed semi-transparent.</para>"
335 "<para>Hidden items only differ from other ones in that their "
336 "name starts with a \".\". In general there is no need for "
337 "users to access them which is why they are hidden.</para>"));
338 m_actionCollection
->setDefaultShortcuts(showHiddenFiles
, KStandardShortcut::showHideHiddenFiles());
339 connect(showHiddenFiles
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleShowHiddenFiles
);
341 QAction
* adjustViewProps
= m_actionCollection
->addAction(QStringLiteral("view_properties"));
342 adjustViewProps
->setText(i18nc("@action:inmenu View", "Adjust View Display Style..."));
343 adjustViewProps
->setIcon(QIcon::fromTheme(QStringLiteral("view-choose")));
344 adjustViewProps
->setWhatsThis(i18nc("@info:whatsthis", "This opens a window "
345 "in which all folder view properties can be adjusted."));
346 connect(adjustViewProps
, &QAction::triggered
, this, &DolphinViewActionHandler::slotAdjustViewProperties
);
349 QActionGroup
* DolphinViewActionHandler::createFileItemRolesActionGroup(const QString
& groupPrefix
)
351 const bool isSortGroup
= (groupPrefix
== QLatin1String("sort_by_"));
352 Q_ASSERT(isSortGroup
|| groupPrefix
== QLatin1String("show_"));
354 QActionGroup
* rolesActionGroup
= new QActionGroup(m_actionCollection
);
355 rolesActionGroup
->setExclusive(isSortGroup
);
357 connect(rolesActionGroup
, &QActionGroup::triggered
,
358 this, &DolphinViewActionHandler::slotSortTriggered
);
360 connect(rolesActionGroup
, &QActionGroup::triggered
,
361 this, &DolphinViewActionHandler::toggleVisibleRole
);
365 KActionMenu
* groupMenu
= nullptr;
366 QActionGroup
* groupMenuGroup
= nullptr;
368 bool indexingEnabled
= false;
370 Baloo::IndexerConfig config
;
371 indexingEnabled
= config
.fileIndexingEnabled();
374 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
375 for (const KFileItemModel::RoleInfo
& info
: rolesInfo
) {
376 if (!isSortGroup
&& info
.role
== "text") {
377 // It should not be possible to hide the "text" role
381 KToggleAction
* action
= nullptr;
382 const QString name
= groupPrefix
+ info
.role
;
383 if (info
.group
.isEmpty()) {
384 action
= m_actionCollection
->add
<KToggleAction
>(name
);
385 action
->setActionGroup(rolesActionGroup
);
387 if (!groupMenu
|| info
.group
!= groupName
) {
388 groupName
= info
.group
;
389 groupMenu
= m_actionCollection
->add
<KActionMenu
>(groupName
);
390 groupMenu
->setText(groupName
);
391 groupMenu
->setActionGroup(rolesActionGroup
);
393 groupMenuGroup
= new QActionGroup(groupMenu
);
394 groupMenuGroup
->setExclusive(isSortGroup
);
396 connect(groupMenuGroup
, &QActionGroup::triggered
,
397 this, &DolphinViewActionHandler::slotSortTriggered
);
399 connect(groupMenuGroup
, &QActionGroup::triggered
,
400 this, &DolphinViewActionHandler::toggleVisibleRole
);
404 action
= new KToggleAction(groupMenu
);
405 action
->setActionGroup(groupMenuGroup
);
406 groupMenu
->addAction(action
);
408 action
->setText(info
.translation
);
409 action
->setData(info
.role
);
411 const bool enable
= (!info
.requiresBaloo
&& !info
.requiresIndexer
) ||
412 (info
.requiresBaloo
) ||
413 (info
.requiresIndexer
&& indexingEnabled
);
414 action
->setEnabled(enable
);
417 m_sortByActions
.insert(info
.role
, action
);
419 m_visibleRoles
.insert(info
.role
, action
);
423 return rolesActionGroup
;
426 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction
* action
)
428 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
429 m_currentView
->setViewMode(mode
);
431 QAction
* viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
432 viewModeMenu
->setIcon(action
->icon());
435 void DolphinViewActionHandler::slotRename()
437 if (m_currentView
->selectedItemsCount() == 0) {
438 Q_EMIT
setSelectionMode(true, SelectionMode::BottomBar::Contents::RenameContents
);
440 Q_EMIT
actionBeingHandled();
441 m_currentView
->renameSelectedItems();
442 // We don't exit selectionMode here because users might want to rename more items.
446 void DolphinViewActionHandler::slotTrashActivated()
448 if (m_currentView
->selectedItemsCount() == 0) {
449 Q_EMIT
setSelectionMode(true, SelectionMode::BottomBar::Contents::MoveToTrashContents
);
451 Q_EMIT
actionBeingHandled();
452 m_currentView
->trashSelectedItems();
453 Q_EMIT
setSelectionMode(false);
457 void DolphinViewActionHandler::slotDeleteItems()
459 if (m_currentView
->selectedItemsCount() == 0) {
460 Q_EMIT
setSelectionMode(true, SelectionMode::BottomBar::Contents::DeleteContents
);
462 Q_EMIT
actionBeingHandled();
463 m_currentView
->deleteSelectedItems();
464 Q_EMIT
setSelectionMode(false);
468 void DolphinViewActionHandler::togglePreview(bool show
)
470 Q_EMIT
actionBeingHandled();
471 m_currentView
->setPreviewsShown(show
);
474 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown
)
477 // It is not enough to update the 'Show Preview' action, also
478 // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
482 QString
DolphinViewActionHandler::currentViewModeActionName() const
484 switch (m_currentView
->viewMode()) {
485 case DolphinView::IconsView
:
486 return QStringLiteral("icons");
487 case DolphinView::DetailsView
:
488 return QStringLiteral("details");
489 case DolphinView::CompactView
:
490 return QStringLiteral("compact");
495 return QString(); // can't happen
498 KActionCollection
* DolphinViewActionHandler::actionCollection()
500 return m_actionCollection
;
503 void DolphinViewActionHandler::updateViewActions()
505 QAction
* viewModeAction
= m_actionCollection
->action(currentViewModeActionName());
506 if (viewModeAction
) {
507 viewModeAction
->setChecked(true);
509 QAction
* viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
510 viewModeMenu
->setIcon(viewModeAction
->icon());
513 QAction
* showPreviewAction
= m_actionCollection
->action(QStringLiteral("show_preview"));
514 showPreviewAction
->setChecked(m_currentView
->previewsShown());
516 slotSortOrderChanged(m_currentView
->sortOrder());
517 slotSortFoldersFirstChanged(m_currentView
->sortFoldersFirst());
518 slotSortHiddenLastChanged(m_currentView
->sortHiddenLast());
519 slotVisibleRolesChanged(m_currentView
->visibleRoles(), QList
<QByteArray
>());
520 slotGroupedSortingChanged(m_currentView
->groupedSorting());
521 slotSortRoleChanged(m_currentView
->sortRole());
522 slotZoomLevelChanged(m_currentView
->zoomLevel(), -1);
524 // Updates the "show_hidden_files" action state and icon
525 slotHiddenFilesShownChanged(m_currentView
->hiddenFilesShown());
528 void DolphinViewActionHandler::zoomIn()
530 const int level
= m_currentView
->zoomLevel();
531 m_currentView
->setZoomLevel(level
+ 1);
535 void DolphinViewActionHandler::zoomOut()
537 const int level
= m_currentView
->zoomLevel();
538 m_currentView
->setZoomLevel(level
- 1);
542 void DolphinViewActionHandler::zoomReset()
544 m_currentView
->resetZoomLevel();
548 void DolphinViewActionHandler::toggleSortFoldersFirst()
550 const bool sortFirst
= m_currentView
->sortFoldersFirst();
551 m_currentView
->setSortFoldersFirst(!sortFirst
);
554 void DolphinViewActionHandler::toggleSortHiddenLast()
556 const bool sortHiddenLast
= m_currentView
->sortHiddenLast();
557 m_currentView
->setSortHiddenLast(!sortHiddenLast
);
560 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order
)
562 QAction
* descending
= m_actionCollection
->action(QStringLiteral("descending"));
563 QAction
* ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
564 const bool sortDescending
= (order
== Qt::DescendingOrder
);
565 descending
->setChecked(sortDescending
);
566 ascending
->setChecked(!sortDescending
);
569 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst
)
571 m_actionCollection
->action(QStringLiteral("folders_first"))->setChecked(foldersFirst
);
574 void DolphinViewActionHandler::slotSortHiddenLastChanged(bool hiddenLast
)
576 m_actionCollection
->action(QStringLiteral("hidden_last"))->setChecked(hiddenLast
);
579 void DolphinViewActionHandler::toggleVisibleRole(QAction
* action
)
581 Q_EMIT
actionBeingHandled();
583 const QByteArray toggledRole
= action
->data().toByteArray();
585 QList
<QByteArray
> roles
= m_currentView
->visibleRoles();
587 const bool show
= action
->isChecked();
589 const int index
= roles
.indexOf(toggledRole
);
590 const bool containsInfo
= (index
>= 0);
591 if (show
&& !containsInfo
) {
592 roles
.append(toggledRole
);
593 m_currentView
->setVisibleRoles(roles
);
594 } else if (!show
&& containsInfo
) {
595 roles
.removeAt(index
);
596 m_currentView
->setVisibleRoles(roles
);
597 Q_ASSERT(roles
.indexOf(toggledRole
) < 0);
601 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList
<QByteArray
>& current
,
602 const QList
<QByteArray
>& previous
)
606 const auto checkedRoles
= QSet
<QByteArray
>(current
.constBegin(), current
.constEnd());
607 QHashIterator
<QByteArray
, KToggleAction
*> it(m_visibleRoles
);
608 while (it
.hasNext()) {
610 const QByteArray
& role
= it
.key();
611 KToggleAction
* action
= it
.value();
612 action
->setChecked(checkedRoles
.contains(role
));
616 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped
)
618 m_currentView
->setGroupedSorting(grouped
);
621 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting
)
623 QAction
* showInGroupsAction
= m_actionCollection
->action(QStringLiteral("show_in_groups"));
624 showInGroupsAction
->setChecked(groupedSorting
);
627 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show
)
629 Q_EMIT
actionBeingHandled();
630 m_currentView
->setHiddenFilesShown(show
);
633 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown
)
635 QAction
* showHiddenFilesAction
= m_actionCollection
->action(QStringLiteral("show_hidden_files"));
636 showHiddenFilesAction
->setChecked(shown
);
639 void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable
)
641 m_actionCollection
->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable
&&
642 KProtocolManager::supportsMakeDir(currentView()->url()));
645 KToggleAction
* DolphinViewActionHandler::iconsModeAction()
647 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("icons"));
648 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
649 iconsView
->setToolTip(i18nc("@info", "Icons view mode"));
650 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_1
);
651 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
652 iconsView
->setData(QVariant::fromValue(DolphinView::IconsView
));
656 KToggleAction
* DolphinViewActionHandler::compactModeAction()
658 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("compact"));
659 iconsView
->setText(i18nc("@action:inmenu View Mode", "Compact"));
660 iconsView
->setToolTip(i18nc("@info", "Compact view mode"));
661 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_2
);
662 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
663 iconsView
->setData(QVariant::fromValue(DolphinView::CompactView
));
667 KToggleAction
* DolphinViewActionHandler::detailsModeAction()
669 KToggleAction
* detailsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("details"));
670 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
671 detailsView
->setToolTip(i18nc("@info", "Details view mode"));
672 m_actionCollection
->setDefaultShortcut(detailsView
, Qt::CTRL
| Qt::Key_3
);
673 detailsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
674 detailsView
->setData(QVariant::fromValue(DolphinView::DetailsView
));
678 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray
& role
)
680 KToggleAction
* action
= m_sortByActions
.value(role
);
682 action
->setChecked(true);
684 if (!action
->icon().isNull()) {
685 QAction
* sortByMenu
= m_actionCollection
->action(QStringLiteral("sort"));
686 sortByMenu
->setIcon(action
->icon());
690 QAction
* descending
= m_actionCollection
->action(QStringLiteral("descending"));
691 QAction
* ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
693 if (role
== "text" || role
== "type" || role
== "extension" || role
== "tags" || role
== "comment") {
694 descending
->setText(i18nc("Sort descending", "Z-A"));
695 ascending
->setText(i18nc("Sort ascending", "A-Z"));
696 } else if (role
== "size") {
697 descending
->setText(i18nc("Sort descending", "Largest First"));
698 ascending
->setText(i18nc("Sort ascending", "Smallest First"));
699 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
700 descending
->setText(i18nc("Sort descending", "Newest First"));
701 ascending
->setText(i18nc("Sort ascending", "Oldest First"));
702 } else if (role
== "rating") {
703 descending
->setText(i18nc("Sort descending", "Highest First"));
704 ascending
->setText(i18nc("Sort ascending", "Lowest First"));
706 descending
->setText(i18nc("Sort descending", "Descending"));
707 ascending
->setText(i18nc("Sort ascending", "Ascending"));
710 slotSortOrderChanged(m_currentView
->sortOrder());
713 void DolphinViewActionHandler::slotZoomLevelChanged(int current
, int previous
)
717 QAction
* zoomInAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomIn
));
719 zoomInAction
->setEnabled(current
< ZoomLevelInfo::maximumLevel());
722 QAction
* zoomOutAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomOut
));
724 zoomOutAction
->setEnabled(current
> ZoomLevelInfo::minimumLevel());
728 void DolphinViewActionHandler::slotSortTriggered(QAction
* action
)
730 // The radiobuttons of the "Sort By"-menu are split between the main-menu
731 // and several sub-menus. Because of this they don't have a common
732 // action-group that assures an exclusive toggle-state between the main-menu
733 // actions and the sub-menu-actions. If an action gets checked, it must
734 // be assured that all other actions get unchecked, except the ascending/
735 // descending actions
736 for (QAction
*groupAction
: qAsConst(m_sortByActions
)) {
737 KActionMenu
* actionMenu
= qobject_cast
<KActionMenu
*>(groupAction
);
739 const auto actions
= actionMenu
->menu()->actions();
740 for (QAction
* subAction
: actions
) {
741 subAction
->setChecked(false);
743 } else if (groupAction
->actionGroup()) {
744 groupAction
->setChecked(false);
747 action
->setChecked(true);
749 // Apply the activated sort-role to the view
750 const QByteArray role
= action
->data().toByteArray();
751 m_currentView
->setSortRole(role
);
754 void DolphinViewActionHandler::slotAdjustViewProperties()
756 Q_EMIT
actionBeingHandled();
757 QPointer
<ViewPropertiesDialog
> dialog
= new ViewPropertiesDialog(m_currentView
);
762 void DolphinViewActionHandler::slotDuplicate()
764 if (m_currentView
->selectedItemsCount() == 0) {
765 Q_EMIT
setSelectionMode(true, SelectionMode::BottomBar::Contents::DuplicateContents
);
767 Q_EMIT
actionBeingHandled();
768 m_currentView
->duplicateSelectedItems();
769 Q_EMIT
setSelectionMode(false);
773 void DolphinViewActionHandler::slotProperties()
775 KPropertiesDialog
* dialog
= nullptr;
776 const KFileItemList list
= m_currentView
->selectedItems();
777 if (list
.isEmpty()) {
778 const QUrl url
= m_currentView
->url();
779 dialog
= new KPropertiesDialog(url
, m_currentView
);
781 dialog
= new KPropertiesDialog(list
, m_currentView
);
784 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
787 dialog
->activateWindow();
790 void DolphinViewActionHandler::slotCopyPath()
792 if (m_currentView
->selectedItemsCount() == 0) {
793 Q_EMIT
setSelectionMode(true, SelectionMode::BottomBar::Contents::CopyLocationContents
);
795 m_currentView
->copyPathToClipboard();
796 Q_EMIT
setSelectionMode(false);
800 void DolphinViewActionHandler::slotSelectionChanged(const KFileItemList
& selection
)
802 QString basicActionsMenuText
;
803 if (selection
.isEmpty()) {
804 basicActionsMenuText
=
805 i18nc("@action:inmenu menu with actions like copy, paste, rename. The user's selection is empty when this text is shown.",
806 "Actions for Current View");
808 QFontMetrics fontMetrics
= QMenu().fontMetrics();
809 // i18n: @action:inmenu menu with actions like copy, paste, rename.
810 // %1 is a textual representation of the currently selected files or folders. This can be the name of
811 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
812 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
813 // and a fallback will be used.
814 basicActionsMenuText
= i18n("Actions for %1", fileItemListToString(selection
, fontMetrics
.averageCharWidth() * 40, fontMetrics
, ItemsState::Selected
));
817 if (basicActionsMenuText
== QStringLiteral("NULL")) {
818 const KFileItemListProperties
properties(selection
);
819 basicActionsMenuText
=
820 i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
821 "Actions for One Selected Item", "Actions for %1 Selected Items", selection
.count());
824 QAction
*basicActionsMenu
= m_actionCollection
->action(QStringLiteral("basic_actions"));
825 basicActionsMenu
->setText(basicActionsMenuText
);
827 // Add or remove contextual actions
828 while (!basicActionsMenu
->menu()->actions().constLast()->isSeparator()) {
829 basicActionsMenu
->menu()->removeAction(basicActionsMenu
->menu()->actions().last());
831 if (selection
.count() == 1) {
832 if (selection
.first().isLink()) {
833 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("show_target")));
835 if (selection
.first().isDir()) {
836 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("add_to_places")));