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