]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinviewactionhandler.cpp
2934e800582e28dd48d7da835a8361d4cf56e93a
[dolphin.git] / src / views / dolphinviewactionhandler.cpp
1 /*
2 * SPDX-FileCopyrightText: 2008 David Faure <faure@kde.org>
3 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include "dolphinviewactionhandler.h"
9
10 #include "kitemviews/kfileitemlisttostring.h"
11 #include "kitemviews/kfileitemmodel.h"
12 #include "selectionmode/actiontexthelper.h"
13 #include "settings/viewpropertiesdialog.h"
14 #include "views/zoomlevelinfo.h"
15
16 #if HAVE_BALOO
17 #include <Baloo/IndexerConfig>
18 #endif
19 #include <KActionCollection>
20 #include <KActionMenu>
21 #include <KFileItemListProperties>
22 #include <KLocalizedString>
23 #include <KNewFileMenu>
24 #include <KPropertiesDialog>
25 #include <KProtocolManager>
26
27 #include <QActionGroup>
28 #include <QMenu>
29 #include <QPointer>
30
31 DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection *collection, SelectionMode::ActionTextHelper *actionTextHelper, QObject *parent)
32 : QObject(parent)
33 , m_actionCollection(collection)
34 , m_currentView(nullptr)
35 , m_sortByActions()
36 , m_visibleRoles()
37 {
38 Q_ASSERT(m_actionCollection);
39 createActions(actionTextHelper);
40 }
41
42 void DolphinViewActionHandler::setCurrentView(DolphinView *view)
43 {
44 Q_ASSERT(view);
45
46 if (m_currentView) {
47 disconnect(m_currentView, nullptr, this, nullptr);
48 }
49
50 m_currentView = view;
51
52 connect(view, &DolphinView::modeChanged, this, &DolphinViewActionHandler::updateViewActions);
53 connect(view, &DolphinView::previewsShownChanged, this, &DolphinViewActionHandler::slotPreviewsShownChanged);
54 connect(view, &DolphinView::sortOrderChanged, this, &DolphinViewActionHandler::slotSortOrderChanged);
55 connect(view, &DolphinView::sortFoldersFirstChanged, this, &DolphinViewActionHandler::slotSortFoldersFirstChanged);
56 connect(view, &DolphinView::sortHiddenLastChanged, this, &DolphinViewActionHandler::slotSortHiddenLastChanged);
57 connect(view, &DolphinView::visibleRolesChanged, this, &DolphinViewActionHandler::slotVisibleRolesChanged);
58 connect(view, &DolphinView::groupedSortingChanged, this, &DolphinViewActionHandler::slotGroupedSortingChanged);
59 connect(view, &DolphinView::hiddenFilesShownChanged, this, &DolphinViewActionHandler::slotHiddenFilesShownChanged);
60 connect(view, &DolphinView::sortRoleChanged, this, &DolphinViewActionHandler::slotSortRoleChanged);
61 connect(view, &DolphinView::zoomLevelChanged, this, &DolphinViewActionHandler::slotZoomLevelChanged);
62 connect(view, &DolphinView::writeStateChanged, this, &DolphinViewActionHandler::slotWriteStateChanged);
63 slotWriteStateChanged(view->isFolderWritable());
64 connect(view, &DolphinView::selectionModeChangeRequested, this, [this](bool enabled) {
65 Q_EMIT selectionModeChangeTriggered(enabled);
66 });
67 connect(view, &DolphinView::selectionChanged, this, &DolphinViewActionHandler::slotSelectionChanged);
68 slotSelectionChanged(m_currentView->selectedItems());
69 }
70
71 DolphinView *DolphinViewActionHandler::currentView()
72 {
73 return m_currentView;
74 }
75
76 void DolphinViewActionHandler::createActions(SelectionMode::ActionTextHelper *actionTextHelper)
77 {
78 // This action doesn't appear in the GUI, it's for the shortcut only.
79 // KNewFileMenu takes care of the GUI stuff.
80 QAction *newDirAction = m_actionCollection->addAction(QStringLiteral("create_dir"));
81 newDirAction->setText(i18nc("@action", "Create Folder…"));
82 m_actionCollection->setDefaultShortcuts(newDirAction, KStandardShortcut::createFolder());
83 newDirAction->setIcon(QIcon::fromTheme(QStringLiteral("folder-new")));
84 newDirAction->setEnabled(false); // Will be enabled in slotWriteStateChanged(bool) if the current URL is writable
85 connect(newDirAction, &QAction::triggered, this, &DolphinViewActionHandler::createDirectoryTriggered);
86
87 // File menu
88
89 auto renameAction = KStandardAction::renameFile(this, &DolphinViewActionHandler::slotRename, m_actionCollection);
90 renameAction->setWhatsThis(xi18nc("@info:whatsthis",
91 "This renames the "
92 "items in your current selection.<nl/>Renaming multiple items "
93 "at once results in their new names differing only in a number."));
94
95 auto trashAction = KStandardAction::moveToTrash(this, &DolphinViewActionHandler::slotTrashActivated, m_actionCollection);
96 auto trashShortcuts = trashAction->shortcuts();
97 trashAction->setAutoRepeat(false);
98 if (!trashShortcuts.contains(QKeySequence::Delete)) {
99 trashShortcuts.append(QKeySequence::Delete);
100 m_actionCollection->setDefaultShortcuts(trashAction, trashShortcuts);
101 }
102 trashAction->setWhatsThis(xi18nc("@info:whatsthis",
103 "This moves the "
104 "items in your current selection to the <filename>Trash"
105 "</filename>.<nl/>The trash is a temporary storage location where "
106 "items can be deleted later if disk space is needed."));
107
108 auto deleteAction = KStandardAction::deleteFile(this, &DolphinViewActionHandler::slotDeleteItems, m_actionCollection);
109 auto deleteShortcuts = deleteAction->shortcuts();
110 deleteAction->setAutoRepeat(false);
111 if (!deleteShortcuts.contains(Qt::SHIFT | Qt::Key_Delete)) {
112 deleteShortcuts.append(Qt::SHIFT | Qt::Key_Delete);
113 m_actionCollection->setDefaultShortcuts(deleteAction, deleteShortcuts);
114 }
115 deleteAction->setWhatsThis(xi18nc("@info:whatsthis",
116 "This deletes "
117 "the items in your current selection permanently. They "
118 "cannot be recovered by normal means."));
119
120 // This action is useful for being enabled when KStandardAction::MoveToTrash should be
121 // disabled and KStandardAction::DeleteFile is enabled (e.g. non-local files), so that Key_Del
122 // can be used for deleting the file (#76016). It needs to be a separate action
123 // so that the Edit menu isn't affected.
124 QAction *deleteWithTrashShortcut = m_actionCollection->addAction(QStringLiteral("delete_shortcut"));
125 // The descriptive text is just for the shortcuts editor.
126 deleteWithTrashShortcut->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
127 m_actionCollection->setDefaultShortcuts(deleteWithTrashShortcut, KStandardShortcut::moveToTrash());
128 deleteWithTrashShortcut->setEnabled(false);
129 connect(deleteWithTrashShortcut, &QAction::triggered, this, &DolphinViewActionHandler::slotDeleteItems);
130
131 QAction *duplicateAction = m_actionCollection->addAction(QStringLiteral("duplicate"));
132 duplicateAction->setText(i18nc("@action:inmenu File", "Duplicate Here"));
133 duplicateAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-duplicate")));
134 m_actionCollection->setDefaultShortcut(duplicateAction, Qt::CTRL | Qt::Key_D);
135 duplicateAction->setEnabled(false);
136 connect(duplicateAction, &QAction::triggered, this, &DolphinViewActionHandler::slotDuplicate);
137
138 QAction *propertiesAction = m_actionCollection->addAction(QStringLiteral("properties"));
139 // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
140 propertiesAction->setText(i18nc("@action:inmenu File", "Properties"));
141 propertiesAction->setWhatsThis(xi18nc("@info:whatsthis properties",
142 "This shows a complete list of properties of the currently "
143 "selected items in a new window.<nl/>If nothing is selected the "
144 "window will be about the currently viewed folder instead.<nl/>"
145 "You can configure advanced options there like managing "
146 "read- and write-permissions."));
147 propertiesAction->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
148 m_actionCollection->setDefaultShortcuts(propertiesAction, {Qt::ALT | Qt::Key_Return, Qt::ALT | Qt::Key_Enter});
149 connect(propertiesAction, &QAction::triggered, this, &DolphinViewActionHandler::slotProperties);
150
151 QAction *copyPathAction = m_actionCollection->addAction(QStringLiteral("copy_location"));
152 copyPathAction->setText(i18nc("@action:incontextmenu", "Copy Location"));
153 copyPathAction->setWhatsThis(i18nc("@info:whatsthis copy_location", "This will copy the path of the first selected item into the clipboard."));
154
155 copyPathAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy-path")));
156 m_actionCollection->setDefaultShortcuts(copyPathAction, {Qt::CTRL | Qt::ALT | Qt::Key_C});
157 connect(copyPathAction, &QAction::triggered, this, &DolphinViewActionHandler::slotCopyPath);
158
159 if (actionTextHelper) {
160 // The "…" at the end make clear that they won't trigger their respective actions directly.
161 actionTextHelper->registerTextWhenNothingIsSelected(trashAction, i18nc("@action:inmenu File", "Move to Trash…"));
162 actionTextHelper->registerTextWhenNothingIsSelected(deleteAction, i18nc("@action:inmenu File", "Delete…"));
163 actionTextHelper->registerTextWhenNothingIsSelected(duplicateAction, i18nc("@action:inmenu File", "Duplicate Here…"));
164 actionTextHelper->registerTextWhenNothingIsSelected(copyPathAction, i18nc("@action:incontextmenu", "Copy Location…"));
165 }
166
167 // This menu makes sure that users who don't know how to open a context menu and haven't
168 // figured out how to enable the menu bar can still perform basic file manipulation.
169 // This only works if they know how to select a file.
170 // The text when nothing is selected at least implies that a selection can /somehow/ be made.
171 // This menu is by default only used in the hamburger menu but created here so users can put
172 // it on their toolbar.
173 KActionMenu *basicActionsMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("basic_actions"), this);
174 // The text is set later depending on the selection in the currently active view.
175 basicActionsMenu->setPopupMode(QToolButton::InstantPopup);
176 basicActionsMenu->addAction(m_actionCollection->action(KStandardAction::name(KStandardAction::Cut)));
177 basicActionsMenu->addAction(m_actionCollection->action(KStandardAction::name(KStandardAction::Copy)));
178 basicActionsMenu->addAction(m_actionCollection->action(KStandardAction::name(KStandardAction::Paste)));
179 basicActionsMenu->addSeparator();
180 basicActionsMenu->addAction(m_actionCollection->action(KStandardAction::name(KStandardAction::RenameFile)));
181 basicActionsMenu->addAction(m_actionCollection->action(KStandardAction::name(KStandardAction::MoveToTrash)));
182 basicActionsMenu->addSeparator();
183 basicActionsMenu->addAction(m_actionCollection->action(QStringLiteral("properties")));
184 basicActionsMenu->addSeparator(); // We add one more separator because we sometimes add contextual
185 // actions in slotSelectionChanged() after the static ones above.
186
187 // View menu
188 KToggleAction *iconsAction = iconsModeAction();
189 KToggleAction *compactAction = compactModeAction();
190 KToggleAction *detailsAction = detailsModeAction();
191
192 iconsAction->setWhatsThis(xi18nc("@info:whatsthis Icons view mode",
193 "<para>This switches to a view mode that focuses on the folder "
194 "and file icons. This mode makes it easy to distinguish folders "
195 "from files and to detect items with distinctive <emphasis>"
196 "file types</emphasis>.</para><para> This mode is handy to "
197 "browse through pictures when the <interface>Preview"
198 "</interface> option is enabled.</para>"));
199 compactAction->setWhatsThis(xi18nc("@info:whatsthis Compact view mode",
200 "<para>This switches to a compact view mode that lists the folders "
201 "and files in columns with the names beside the icons.</para><para>"
202 "This helps to give you an overview in folders with many items.</para>"));
203 detailsAction->setWhatsThis(xi18nc("@info:whatsthis Details view mode",
204 "<para>This switches to a list view mode that focuses on folder "
205 "and file details.</para><para>Click on a detail in the column "
206 "header to sort the items by it. Click again to sort the other "
207 "way around. To select which details should be displayed click "
208 "the header with the right mouse button.</para><para>You can "
209 "view the contents of a folder without leaving the current "
210 "location by clicking the region to the left of it. This way you can "
211 "view the contents of multiple folders in the same list.</para>"));
212
213 KSelectAction *viewModeActions = m_actionCollection->add<KSelectAction>(QStringLiteral("view_mode"));
214 viewModeActions->setText(i18nc("@action:intoolbar", "View Mode"));
215 viewModeActions->addAction(iconsAction);
216 viewModeActions->addAction(compactAction);
217 viewModeActions->addAction(detailsAction);
218 viewModeActions->setToolBarMode(KSelectAction::MenuMode);
219 connect(viewModeActions, &KSelectAction::actionTriggered, this, &DolphinViewActionHandler::slotViewModeActionTriggered);
220
221 QAction *zoomInAction = KStandardAction::zoomIn(this, &DolphinViewActionHandler::zoomIn, m_actionCollection);
222 zoomInAction->setWhatsThis(i18nc("@info:whatsthis zoom in", "This increases the icon size."));
223
224 QAction *zoomResetAction = m_actionCollection->addAction(QStringLiteral("view_zoom_reset"));
225 zoomResetAction->setText(i18nc("@action:inmenu View", "Reset Zoom Level"));
226 zoomResetAction->setToolTip(i18n("Zoom To Default"));
227 zoomResetAction->setWhatsThis(i18nc("@info:whatsthis zoom reset", "This resets the icon size to default."));
228 zoomResetAction->setIcon(QIcon::fromTheme(QStringLiteral("zoom-original")));
229 m_actionCollection->setDefaultShortcuts(zoomResetAction, {Qt::CTRL | Qt::Key_0});
230 connect(zoomResetAction, &QAction::triggered, this, &DolphinViewActionHandler::zoomReset);
231
232 QAction *zoomOutAction = KStandardAction::zoomOut(this, &DolphinViewActionHandler::zoomOut, m_actionCollection);
233 zoomOutAction->setWhatsThis(i18nc("@info:whatsthis zoom out", "This reduces the icon size."));
234
235 KActionMenu *zoomMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("zoom"));
236 zoomMenu->setText(i18nc("@action:inmenu menu of zoom actions", "Zoom"));
237 zoomMenu->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
238 zoomMenu->setPopupMode(QToolButton::InstantPopup);
239 zoomMenu->addAction(zoomInAction);
240 zoomMenu->addAction(zoomResetAction);
241 zoomMenu->addAction(zoomOutAction);
242
243 KToggleAction *showPreview = m_actionCollection->add<KToggleAction>(QStringLiteral("show_preview"));
244 showPreview->setText(i18nc("@action:intoolbar", "Show Previews"));
245 showPreview->setToolTip(i18nc("@info", "Show preview of files and folders"));
246 showPreview->setWhatsThis(xi18nc("@info:whatsthis",
247 "When this is "
248 "enabled, the icons are based on the actual file or folder "
249 "contents.<nl/>For example the icons of images become scaled "
250 "down versions of the images."));
251 showPreview->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
252 m_actionCollection->setDefaultShortcut(showPreview, QKeySequence(Qt::Key_F12));
253 connect(showPreview, &KToggleAction::triggered, this, &DolphinViewActionHandler::togglePreview);
254
255 KToggleAction *sortFoldersFirst = m_actionCollection->add<KToggleAction>(QStringLiteral("folders_first"));
256 sortFoldersFirst->setText(i18nc("@action:inmenu Sort", "Folders First"));
257 connect(sortFoldersFirst, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleSortFoldersFirst);
258
259 KToggleAction *sortHiddenLast = m_actionCollection->add<KToggleAction>(QStringLiteral("hidden_last"));
260 sortHiddenLast->setText(i18nc("@action:inmenu Sort", "Hidden Files Last"));
261 connect(sortHiddenLast, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleSortHiddenLast);
262
263 // View -> Sort By
264 QActionGroup *sortByActionGroup = createFileItemRolesActionGroup(QStringLiteral("sort_by_"));
265
266 KActionMenu *sortByActionMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("sort"));
267 sortByActionMenu->setIcon(QIcon::fromTheme(QStringLiteral("view-sort")));
268 sortByActionMenu->setText(i18nc("@action:inmenu View", "Sort By"));
269 sortByActionMenu->setPopupMode(QToolButton::InstantPopup);
270
271 const auto sortByActionGroupActions = sortByActionGroup->actions();
272 for (QAction *action : sortByActionGroupActions) {
273 sortByActionMenu->addAction(action);
274 }
275
276 sortByActionMenu->addSeparator();
277
278 QActionGroup *group = new QActionGroup(sortByActionMenu);
279 group->setExclusive(true);
280
281 KToggleAction *ascendingAction = m_actionCollection->add<KToggleAction>(QStringLiteral("ascending"));
282 ascendingAction->setActionGroup(group);
283 connect(ascendingAction, &QAction::triggered, this, [this] {
284 m_currentView->setSortOrder(Qt::AscendingOrder);
285 });
286
287 KToggleAction *descendingAction = m_actionCollection->add<KToggleAction>(QStringLiteral("descending"));
288 descendingAction->setActionGroup(group);
289 connect(descendingAction, &QAction::triggered, this, [this] {
290 m_currentView->setSortOrder(Qt::DescendingOrder);
291 });
292
293 sortByActionMenu->addAction(ascendingAction);
294 sortByActionMenu->addAction(descendingAction);
295 sortByActionMenu->addSeparator();
296 sortByActionMenu->addAction(sortFoldersFirst);
297 sortByActionMenu->addAction(sortHiddenLast);
298
299 // View -> Additional Information
300 QActionGroup *visibleRolesGroup = createFileItemRolesActionGroup(QStringLiteral("show_"));
301
302 KActionMenu *visibleRolesMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("additional_info"));
303 visibleRolesMenu->setText(i18nc("@action:inmenu View", "Show Additional Information"));
304 visibleRolesMenu->setIcon(QIcon::fromTheme(QStringLiteral("documentinfo")));
305 visibleRolesMenu->setPopupMode(QToolButton::InstantPopup);
306
307 const auto visibleRolesGroupActions = visibleRolesGroup->actions();
308 for (QAction *action : visibleRolesGroupActions) {
309 visibleRolesMenu->addAction(action);
310 }
311
312 KToggleAction *showInGroups = m_actionCollection->add<KToggleAction>(QStringLiteral("show_in_groups"));
313 showInGroups->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
314 showInGroups->setText(i18nc("@action:inmenu View", "Show in Groups"));
315 showInGroups->setWhatsThis(i18nc("@info:whatsthis", "This groups files and folders by their first letter."));
316 connect(showInGroups, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleGroupedSorting);
317
318 KToggleAction *showHiddenFiles = m_actionCollection->add<KToggleAction>(QStringLiteral("show_hidden_files"));
319 showHiddenFiles->setIcon(QIcon::fromTheme(QStringLiteral("view-visible")));
320 showHiddenFiles->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
321 showHiddenFiles->setWhatsThis(xi18nc("@info:whatsthis",
322 "<para>When "
323 "this is enabled, <emphasis>hidden</emphasis> files and folders "
324 "are visible. They will be displayed semi-transparent.</para>"
325 "<para>Hidden items only differ from normal ones in that their "
326 "name starts with a dot (\".\"). Typically, there is no need for "
327 "users to access them, which is why they are hidden.</para>"
328 "<para>Items can also be hidden if their names are listed in a text file "
329 "named \".hidden\". Files with the \"application/x-trash\" MIME type, "
330 "such as backup files, can also be hidden by enabling that setting in "
331 "Configure Dolphin > View > General.</para>"));
332 m_actionCollection->setDefaultShortcuts(showHiddenFiles, KStandardShortcut::showHideHiddenFiles());
333 connect(showHiddenFiles, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleShowHiddenFiles);
334
335 QAction *adjustViewProps = m_actionCollection->addAction(QStringLiteral("view_properties"));
336 adjustViewProps->setText(i18nc("@action:inmenu View", "Adjust View Display Style…"));
337 adjustViewProps->setIcon(QIcon::fromTheme(QStringLiteral("view-choose")));
338 adjustViewProps->setWhatsThis(i18nc("@info:whatsthis",
339 "This opens a window "
340 "in which all folder view properties can be adjusted."));
341 connect(adjustViewProps, &QAction::triggered, this, &DolphinViewActionHandler::slotAdjustViewProperties);
342 }
343
344 QActionGroup *DolphinViewActionHandler::createFileItemRolesActionGroup(const QString &groupPrefix)
345 {
346 const bool isSortGroup = (groupPrefix == QLatin1String("sort_by_"));
347 Q_ASSERT(isSortGroup || groupPrefix == QLatin1String("show_"));
348
349 QActionGroup *rolesActionGroup = new QActionGroup(m_actionCollection);
350 rolesActionGroup->setExclusive(isSortGroup);
351 if (isSortGroup) {
352 connect(rolesActionGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::slotSortTriggered);
353 } else {
354 connect(rolesActionGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::toggleVisibleRole);
355 }
356
357 QString groupName;
358 KActionMenu *groupMenu = nullptr;
359 QActionGroup *groupMenuGroup = nullptr;
360
361 bool indexingEnabled = false;
362 #if HAVE_BALOO
363 Baloo::IndexerConfig config;
364 indexingEnabled = config.fileIndexingEnabled();
365 #endif
366
367 const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
368 for (const KFileItemModel::RoleInfo &info : rolesInfo) {
369 if (!isSortGroup && info.role == "text") {
370 // It should not be possible to hide the "text" role
371 continue;
372 }
373
374 KToggleAction *action = nullptr;
375 const QString name = groupPrefix + info.role;
376 if (info.group.isEmpty()) {
377 action = m_actionCollection->add<KToggleAction>(name);
378 action->setActionGroup(rolesActionGroup);
379 } else {
380 if (!groupMenu || info.group != groupName) {
381 groupName = info.group;
382 groupMenu = m_actionCollection->add<KActionMenu>(groupName);
383 groupMenu->setText(groupName);
384 groupMenu->setActionGroup(rolesActionGroup);
385
386 groupMenuGroup = new QActionGroup(groupMenu);
387 groupMenuGroup->setExclusive(isSortGroup);
388 if (isSortGroup) {
389 connect(groupMenuGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::slotSortTriggered);
390 } else {
391 connect(groupMenuGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::toggleVisibleRole);
392 }
393 }
394
395 action = new KToggleAction(groupMenu);
396 action->setActionGroup(groupMenuGroup);
397 groupMenu->addAction(action);
398 }
399 action->setText(info.translation);
400 action->setData(info.role);
401
402 const bool enable = (!info.requiresBaloo && !info.requiresIndexer) || (info.requiresBaloo) || (info.requiresIndexer && indexingEnabled);
403 action->setEnabled(enable);
404
405 if (isSortGroup) {
406 m_sortByActions.insert(info.role, action);
407 } else {
408 m_visibleRoles.insert(info.role, action);
409 }
410 }
411
412 return rolesActionGroup;
413 }
414
415 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction *action)
416 {
417 const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
418 m_currentView->setViewMode(mode);
419
420 QAction *viewModeMenu = m_actionCollection->action(QStringLiteral("view_mode"));
421 viewModeMenu->setIcon(action->icon());
422 }
423
424 void DolphinViewActionHandler::slotRename()
425 {
426 if (m_currentView->selectedItemsCount() == 0) {
427 Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::RenameContents);
428 } else {
429 Q_EMIT actionBeingHandled();
430 m_currentView->renameSelectedItems();
431 // We don't exit selectionMode here because users might want to rename more items.
432 }
433 }
434
435 void DolphinViewActionHandler::slotTrashActivated()
436 {
437 if (m_currentView->selectedItemsCount() == 0) {
438 Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::MoveToTrashContents);
439 } else {
440 Q_EMIT actionBeingHandled();
441 m_currentView->trashSelectedItems();
442 Q_EMIT selectionModeChangeTriggered(false);
443 }
444 }
445
446 void DolphinViewActionHandler::slotDeleteItems()
447 {
448 if (m_currentView->selectedItemsCount() == 0) {
449 Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DeleteContents);
450 } else {
451 Q_EMIT actionBeingHandled();
452 m_currentView->deleteSelectedItems();
453 Q_EMIT selectionModeChangeTriggered(false);
454 }
455 }
456
457 void DolphinViewActionHandler::togglePreview(bool show)
458 {
459 Q_EMIT actionBeingHandled();
460 m_currentView->setPreviewsShown(show);
461 }
462
463 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown)
464 {
465 Q_UNUSED(shown)
466 // It is not enough to update the 'Show Preview' action, also
467 // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
468 updateViewActions();
469 }
470
471 QString DolphinViewActionHandler::currentViewModeActionName() const
472 {
473 switch (m_currentView->viewMode()) {
474 case DolphinView::IconsView:
475 return QStringLiteral("icons");
476 case DolphinView::DetailsView:
477 return QStringLiteral("details");
478 case DolphinView::CompactView:
479 return QStringLiteral("compact");
480 default:
481 Q_ASSERT(false);
482 break;
483 }
484 return QString(); // can't happen
485 }
486
487 KActionCollection *DolphinViewActionHandler::actionCollection()
488 {
489 return m_actionCollection;
490 }
491
492 void DolphinViewActionHandler::updateViewActions()
493 {
494 QAction *viewModeAction = m_actionCollection->action(currentViewModeActionName());
495 if (viewModeAction) {
496 viewModeAction->setChecked(true);
497
498 QAction *viewModeMenu = m_actionCollection->action(QStringLiteral("view_mode"));
499 viewModeMenu->setIcon(viewModeAction->icon());
500 }
501
502 QAction *showPreviewAction = m_actionCollection->action(QStringLiteral("show_preview"));
503 showPreviewAction->setChecked(m_currentView->previewsShown());
504
505 slotSortOrderChanged(m_currentView->sortOrder());
506 slotSortFoldersFirstChanged(m_currentView->sortFoldersFirst());
507 slotSortHiddenLastChanged(m_currentView->sortHiddenLast());
508 slotVisibleRolesChanged(m_currentView->visibleRoles(), QList<QByteArray>());
509 slotGroupedSortingChanged(m_currentView->groupedSorting());
510 slotSortRoleChanged(m_currentView->sortRole());
511 slotZoomLevelChanged(m_currentView->zoomLevel(), -1);
512
513 // Updates the "show_hidden_files" action state and icon
514 slotHiddenFilesShownChanged(m_currentView->hiddenFilesShown());
515 }
516
517 void DolphinViewActionHandler::zoomIn()
518 {
519 const int level = m_currentView->zoomLevel();
520 m_currentView->setZoomLevel(level + 1);
521 updateViewActions();
522 }
523
524 void DolphinViewActionHandler::zoomOut()
525 {
526 const int level = m_currentView->zoomLevel();
527 m_currentView->setZoomLevel(level - 1);
528 updateViewActions();
529 }
530
531 void DolphinViewActionHandler::zoomReset()
532 {
533 m_currentView->resetZoomLevel();
534 updateViewActions();
535 }
536
537 void DolphinViewActionHandler::toggleSortFoldersFirst()
538 {
539 const bool sortFirst = m_currentView->sortFoldersFirst();
540 m_currentView->setSortFoldersFirst(!sortFirst);
541 }
542
543 void DolphinViewActionHandler::toggleSortHiddenLast()
544 {
545 const bool sortHiddenLast = m_currentView->sortHiddenLast();
546 m_currentView->setSortHiddenLast(!sortHiddenLast);
547 }
548
549 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order)
550 {
551 QAction *descending = m_actionCollection->action(QStringLiteral("descending"));
552 QAction *ascending = m_actionCollection->action(QStringLiteral("ascending"));
553 const bool sortDescending = (order == Qt::DescendingOrder);
554 descending->setChecked(sortDescending);
555 ascending->setChecked(!sortDescending);
556 }
557
558 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst)
559 {
560 m_actionCollection->action(QStringLiteral("folders_first"))->setChecked(foldersFirst);
561 }
562
563 void DolphinViewActionHandler::slotSortHiddenLastChanged(bool hiddenLast)
564 {
565 m_actionCollection->action(QStringLiteral("hidden_last"))->setChecked(hiddenLast);
566 }
567
568 void DolphinViewActionHandler::toggleVisibleRole(QAction *action)
569 {
570 Q_EMIT actionBeingHandled();
571
572 const QByteArray toggledRole = action->data().toByteArray();
573
574 QList<QByteArray> roles = m_currentView->visibleRoles();
575
576 const bool show = action->isChecked();
577
578 const int index = roles.indexOf(toggledRole);
579 const bool containsInfo = (index >= 0);
580 if (show && !containsInfo) {
581 roles.append(toggledRole);
582 m_currentView->setVisibleRoles(roles);
583 } else if (!show && containsInfo) {
584 roles.removeAt(index);
585 m_currentView->setVisibleRoles(roles);
586 Q_ASSERT(roles.indexOf(toggledRole) < 0);
587 }
588 }
589
590 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList<QByteArray> &current, const QList<QByteArray> &previous)
591 {
592 Q_UNUSED(previous)
593
594 const auto checkedRoles = QSet<QByteArray>(current.constBegin(), current.constEnd());
595 QHashIterator<QByteArray, KToggleAction *> it(m_visibleRoles);
596 while (it.hasNext()) {
597 it.next();
598 const QByteArray &role = it.key();
599 KToggleAction *action = it.value();
600 action->setChecked(checkedRoles.contains(role));
601 }
602 }
603
604 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped)
605 {
606 m_currentView->setGroupedSorting(grouped);
607 }
608
609 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting)
610 {
611 QAction *showInGroupsAction = m_actionCollection->action(QStringLiteral("show_in_groups"));
612 showInGroupsAction->setChecked(groupedSorting);
613 }
614
615 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show)
616 {
617 Q_EMIT actionBeingHandled();
618 m_currentView->setHiddenFilesShown(show);
619 }
620
621 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown)
622 {
623 QAction *showHiddenFilesAction = m_actionCollection->action(QStringLiteral("show_hidden_files"));
624 showHiddenFilesAction->setChecked(shown);
625 }
626
627 void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable)
628 {
629 m_actionCollection->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable && KProtocolManager::supportsMakeDir(currentView()->url()));
630 }
631
632 KToggleAction *DolphinViewActionHandler::iconsModeAction()
633 {
634 KToggleAction *iconsView = m_actionCollection->add<KToggleAction>(QStringLiteral("icons"));
635 iconsView->setText(i18nc("@action:inmenu View Mode", "Icons"));
636 iconsView->setToolTip(i18nc("@info", "Icons view mode"));
637 m_actionCollection->setDefaultShortcut(iconsView, Qt::CTRL | Qt::Key_1);
638 iconsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
639 iconsView->setData(QVariant::fromValue(DolphinView::IconsView));
640 return iconsView;
641 }
642
643 KToggleAction *DolphinViewActionHandler::compactModeAction()
644 {
645 KToggleAction *iconsView = m_actionCollection->add<KToggleAction>(QStringLiteral("compact"));
646 iconsView->setText(i18nc("@action:inmenu View Mode", "Compact"));
647 iconsView->setToolTip(i18nc("@info", "Compact view mode"));
648 m_actionCollection->setDefaultShortcut(iconsView, Qt::CTRL | Qt::Key_2);
649 iconsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
650 iconsView->setData(QVariant::fromValue(DolphinView::CompactView));
651 return iconsView;
652 }
653
654 KToggleAction *DolphinViewActionHandler::detailsModeAction()
655 {
656 KToggleAction *detailsView = m_actionCollection->add<KToggleAction>(QStringLiteral("details"));
657 detailsView->setText(i18nc("@action:inmenu View Mode", "Details"));
658 detailsView->setToolTip(i18nc("@info", "Details view mode"));
659 m_actionCollection->setDefaultShortcut(detailsView, Qt::CTRL | Qt::Key_3);
660 detailsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
661 detailsView->setData(QVariant::fromValue(DolphinView::DetailsView));
662 return detailsView;
663 }
664
665 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray &role)
666 {
667 KToggleAction *action = m_sortByActions.value(role);
668 if (action) {
669 action->setChecked(true);
670
671 if (!action->icon().isNull()) {
672 QAction *sortByMenu = m_actionCollection->action(QStringLiteral("sort"));
673 sortByMenu->setIcon(action->icon());
674 }
675 }
676
677 QAction *descending = m_actionCollection->action(QStringLiteral("descending"));
678 QAction *ascending = m_actionCollection->action(QStringLiteral("ascending"));
679
680 if (role == "text" || role == "type" || role == "extension" || role == "tags" || role == "comment") {
681 descending->setText(i18nc("Sort descending", "Z-A"));
682 ascending->setText(i18nc("Sort ascending", "A-Z"));
683 } else if (role == "size") {
684 descending->setText(i18nc("Sort descending", "Largest First"));
685 ascending->setText(i18nc("Sort ascending", "Smallest First"));
686 } else if (role == "modificationtime" || role == "creationtime" || role == "accesstime") {
687 descending->setText(i18nc("Sort descending", "Newest First"));
688 ascending->setText(i18nc("Sort ascending", "Oldest First"));
689 } else if (role == "rating") {
690 descending->setText(i18nc("Sort descending", "Highest First"));
691 ascending->setText(i18nc("Sort ascending", "Lowest First"));
692 } else {
693 descending->setText(i18nc("Sort descending", "Descending"));
694 ascending->setText(i18nc("Sort ascending", "Ascending"));
695 }
696
697 slotSortOrderChanged(m_currentView->sortOrder());
698 }
699
700 void DolphinViewActionHandler::slotZoomLevelChanged(int current, int previous)
701 {
702 Q_UNUSED(previous)
703
704 QAction *zoomInAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomIn));
705 if (zoomInAction) {
706 zoomInAction->setEnabled(current < ZoomLevelInfo::maximumLevel());
707 }
708
709 QAction *zoomOutAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomOut));
710 if (zoomOutAction) {
711 zoomOutAction->setEnabled(current > ZoomLevelInfo::minimumLevel());
712 }
713 }
714
715 void DolphinViewActionHandler::slotSortTriggered(QAction *action)
716 {
717 // The radiobuttons of the "Sort By"-menu are split between the main-menu
718 // and several sub-menus. Because of this they don't have a common
719 // action-group that assures an exclusive toggle-state between the main-menu
720 // actions and the sub-menu-actions. If an action gets checked, it must
721 // be assured that all other actions get unchecked, except the ascending/
722 // descending actions
723 for (QAction *groupAction : std::as_const(m_sortByActions)) {
724 KActionMenu *actionMenu = qobject_cast<KActionMenu *>(groupAction);
725 if (actionMenu) {
726 const auto actions = actionMenu->menu()->actions();
727 for (QAction *subAction : actions) {
728 subAction->setChecked(false);
729 }
730 } else if (groupAction->actionGroup()) {
731 groupAction->setChecked(false);
732 }
733 }
734 action->setChecked(true);
735
736 // Apply the activated sort-role to the view
737 const QByteArray role = action->data().toByteArray();
738 m_currentView->setSortRole(role);
739 }
740
741 void DolphinViewActionHandler::slotAdjustViewProperties()
742 {
743 Q_EMIT actionBeingHandled();
744 QPointer<ViewPropertiesDialog> dialog = new ViewPropertiesDialog(m_currentView);
745 dialog->exec();
746 delete dialog;
747 }
748
749 void DolphinViewActionHandler::slotDuplicate()
750 {
751 if (m_currentView->selectedItemsCount() == 0) {
752 Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DuplicateContents);
753 } else {
754 Q_EMIT actionBeingHandled();
755 m_currentView->duplicateSelectedItems();
756 Q_EMIT selectionModeChangeTriggered(false);
757 }
758 }
759
760 void DolphinViewActionHandler::slotProperties()
761 {
762 KPropertiesDialog *dialog = nullptr;
763 const KFileItemList list = m_currentView->selectedItems();
764 if (list.isEmpty()) {
765 const QUrl url = m_currentView->url();
766 dialog = new KPropertiesDialog(url, m_currentView);
767 } else {
768 dialog = new KPropertiesDialog(list, m_currentView);
769 }
770
771 dialog->setAttribute(Qt::WA_DeleteOnClose);
772 dialog->show();
773 dialog->raise();
774 dialog->activateWindow();
775 }
776
777 void DolphinViewActionHandler::slotCopyPath()
778 {
779 if (m_currentView->selectedItemsCount() == 0) {
780 Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::CopyLocationContents);
781 } else {
782 m_currentView->copyPathToClipboard();
783 Q_EMIT selectionModeChangeTriggered(false);
784 }
785 }
786
787 void DolphinViewActionHandler::slotSelectionChanged(const KFileItemList &selection)
788 {
789 QString basicActionsMenuText;
790 if (selection.isEmpty()) {
791 basicActionsMenuText = i18nc("@action:inmenu menu with actions like copy, paste, rename. The user's selection is empty when this text is shown.",
792 "Actions for Current View");
793 } else {
794 // clang-format off
795 QFontMetrics fontMetrics = QMenu().fontMetrics();
796 // i18n: @action:inmenu menu with actions like copy, paste, rename.
797 // %1 is a textual representation of the currently selected files or folders. This can be the name of
798 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
799 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
800 // and a fallback will be used.
801 basicActionsMenuText = i18n("Actions for %1", fileItemListToString(selection, fontMetrics.averageCharWidth() * 40, fontMetrics, ItemsState::Selected));
802 // clang-format on
803 }
804
805 if (basicActionsMenuText == QStringLiteral("NULL")) {
806 const KFileItemListProperties properties(selection);
807 basicActionsMenuText = i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
808 "Actions for One Selected Item",
809 "Actions for %1 Selected Items",
810 selection.count());
811 }
812
813 QAction *basicActionsMenu = m_actionCollection->action(QStringLiteral("basic_actions"));
814 basicActionsMenu->setText(basicActionsMenuText);
815
816 // Add or remove contextual actions
817 while (!basicActionsMenu->menu()->actions().constLast()->isSeparator()) {
818 basicActionsMenu->menu()->removeAction(basicActionsMenu->menu()->actions().last());
819 }
820 if (selection.count() == 1) {
821 if (selection.first().isLink()) {
822 basicActionsMenu->menu()->addAction(m_actionCollection->action(QStringLiteral("show_target")));
823 }
824 if (selection.first().isDir()) {
825 basicActionsMenu->menu()->addAction(m_actionCollection->action(QStringLiteral("add_to_places")));
826 }
827 }
828 }
829
830 #include "moc_dolphinviewactionhandler.cpp"