]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinviewactionhandler.cpp
Merge remote-tracking branch 'upstream/master' into work/zakharafoniam/useful-groups
[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 KActionMenu *groupByActionMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("group"));
306 groupByActionMenu->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
307 groupByActionMenu->setText(i18nc("@action:inmenu View", "Group By"));
308 groupByActionMenu->setPopupMode(QToolButton::InstantPopup);
309
310 const auto groupByActionGroupActions = groupByActionGroup->actions();
311 for (QAction *action : groupByActionGroupActions) {
312 groupByActionMenu->addAction(action);
313 }
314
315 groupByActionMenu->addSeparator();
316
317 QActionGroup *groupForGroup = new QActionGroup(groupByActionMenu);
318 groupForGroup->setExclusive(true);
319
320 KToggleAction *groupAscendingAction = m_actionCollection->add<KToggleAction>(QStringLiteral("group_ascending"));
321 groupAscendingAction->setActionGroup(groupForGroup);
322 connect(groupAscendingAction, &QAction::triggered, this, [this] {
323 m_currentView->setGroupOrder(Qt::AscendingOrder);
324 });
325
326 KToggleAction *groupDescendingAction = m_actionCollection->add<KToggleAction>(QStringLiteral("group_descending"));
327 groupDescendingAction->setActionGroup(groupForGroup);
328 connect(groupDescendingAction, &QAction::triggered, this, [this] {
329 m_currentView->setGroupOrder(Qt::DescendingOrder);
330 });
331
332 groupByActionMenu->addAction(groupAscendingAction);
333 groupByActionMenu->addAction(groupDescendingAction);
334
335 // View -> Additional Information
336 QActionGroup *visibleRolesGroup = createFileItemRolesActionGroup(QStringLiteral("show_"));
337
338 KActionMenu *visibleRolesMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("additional_info"));
339 visibleRolesMenu->setText(i18nc("@action:inmenu View", "Show Additional Information"));
340 visibleRolesMenu->setIcon(QIcon::fromTheme(QStringLiteral("documentinfo")));
341 visibleRolesMenu->setPopupMode(QToolButton::InstantPopup);
342
343 const auto visibleRolesGroupActions = visibleRolesGroup->actions();
344 for (QAction *action : visibleRolesGroupActions) {
345 visibleRolesMenu->addAction(action);
346 }
347
348 KToggleAction *showInGroups = m_actionCollection->add<KToggleAction>(QStringLiteral("show_in_groups"));
349 showInGroups->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
350 showInGroups->setText(i18nc("@action:inmenu View", "Show in Groups"));
351 showInGroups->setWhatsThis(i18nc("@info:whatsthis", "This groups files and folders by their first letter."));
352 connect(showInGroups, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleGroupedSorting);
353
354 KToggleAction *showHiddenFiles = m_actionCollection->add<KToggleAction>(QStringLiteral("show_hidden_files"));
355 showHiddenFiles->setIcon(QIcon::fromTheme(QStringLiteral("view-visible")));
356 showHiddenFiles->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
357 showHiddenFiles->setWhatsThis(xi18nc("@info:whatsthis",
358 "<para>When "
359 "this is enabled, <emphasis>hidden</emphasis> files and folders "
360 "are visible. They will be displayed semi-transparent.</para>"
361 "<para>Hidden items only differ from normal ones in that their "
362 "name starts with a dot (\".\"). Typically, there is no need for "
363 "users to access them, which is why they are hidden.</para>"
364 "<para>Items can also be hidden if their names are listed in a text file "
365 "named \".hidden\". Files with the \"application/x-trash\" MIME type, "
366 "such as backup files, can also be hidden by enabling that setting in "
367 "Configure Dolphin > View > General.</para>"));
368 m_actionCollection->setDefaultShortcuts(showHiddenFiles, KStandardShortcut::showHideHiddenFiles());
369 connect(showHiddenFiles, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleShowHiddenFiles);
370
371 QAction *adjustViewProps = m_actionCollection->addAction(QStringLiteral("view_properties"));
372 adjustViewProps->setText(i18nc("@action:inmenu View", "Adjust View Display Style…"));
373 adjustViewProps->setIcon(QIcon::fromTheme(QStringLiteral("view-choose")));
374 adjustViewProps->setWhatsThis(i18nc("@info:whatsthis",
375 "This opens a window "
376 "in which all folder view properties can be adjusted."));
377 connect(adjustViewProps, &QAction::triggered, this, &DolphinViewActionHandler::slotAdjustViewProperties);
378 }
379
380 QActionGroup *DolphinViewActionHandler::createFileItemRolesActionGroup(const QString &groupPrefix)
381 {
382 const bool isSortGroup = (groupPrefix == QLatin1String("sort_by_"));
383 const bool isGroupGroup = (groupPrefix == QLatin1String("group_by_"));
384 Q_ASSERT(isSortGroup || isGroupGroup || groupPrefix == QLatin1String("show_"));
385
386 QActionGroup *rolesActionGroup = new QActionGroup(m_actionCollection);
387 rolesActionGroup->setExclusive(isSortGroup || isGroupGroup);
388 if (isSortGroup) {
389 connect(rolesActionGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::slotSortTriggered);
390 } else if (isGroupGroup) {
391 connect(rolesActionGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::slotGroupTriggered);
392 } else {
393 connect(rolesActionGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::toggleVisibleRole);
394 }
395
396 QString groupName;
397 KActionMenu *groupMenu = nullptr;
398 QActionGroup *groupMenuGroup = nullptr;
399
400 bool indexingEnabled = false;
401 #if HAVE_BALOO
402 Baloo::IndexerConfig config;
403 indexingEnabled = config.fileIndexingEnabled();
404 #endif
405
406 QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
407 if (isGroupGroup) {
408 rolesInfo += KFileItemModel::extraGroupingInformation();
409 }
410
411 for (const KFileItemModel::RoleInfo &info : rolesInfo) {
412 if (!isSortGroup && !isGroupGroup && info.role == "text") {
413 // It should not be possible to hide the "text" role
414 continue;
415 }
416
417 KToggleAction *action = nullptr;
418 const QString name = groupPrefix + info.role;
419 if (info.group.isEmpty()) {
420 action = m_actionCollection->add<KToggleAction>(name);
421 action->setActionGroup(rolesActionGroup);
422 } else {
423 if (!groupMenu || info.group != groupName) {
424 groupName = info.group;
425 groupMenu = m_actionCollection->add<KActionMenu>(groupName);
426 groupMenu->setText(groupName);
427 groupMenu->setActionGroup(rolesActionGroup);
428
429 groupMenuGroup = new QActionGroup(groupMenu);
430 groupMenuGroup->setExclusive(isSortGroup || isGroupGroup);
431 if (isSortGroup) {
432 connect(groupMenuGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::slotSortTriggered);
433 } else if (isGroupGroup) {
434 connect(groupMenuGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::slotGroupTriggered);
435 } else {
436 connect(groupMenuGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::toggleVisibleRole);
437 }
438 }
439
440 action = new KToggleAction(groupMenu);
441 action->setActionGroup(groupMenuGroup);
442 groupMenu->addAction(action);
443 }
444 action->setText(info.translation);
445 action->setData(info.role);
446
447 const bool enable = (!info.requiresBaloo && !info.requiresIndexer) || (info.requiresBaloo) || (info.requiresIndexer && indexingEnabled);
448 action->setEnabled(enable);
449
450 if (isSortGroup) {
451 m_sortByActions.insert(info.role, action);
452 } else if (isGroupGroup) {
453 m_groupByActions.insert(info.role, action);
454 } else {
455 m_visibleRoles.insert(info.role, action);
456 }
457 }
458
459 return rolesActionGroup;
460 }
461
462 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction *action)
463 {
464 const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
465 m_currentView->setViewMode(mode);
466
467 QAction *viewModeMenu = m_actionCollection->action(QStringLiteral("view_mode"));
468 viewModeMenu->setIcon(action->icon());
469 }
470
471 void DolphinViewActionHandler::slotRename()
472 {
473 if (m_currentView->selectedItemsCount() == 0) {
474 Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::RenameContents);
475 } else {
476 Q_EMIT actionBeingHandled();
477 m_currentView->renameSelectedItems();
478 // We don't exit selectionMode here because users might want to rename more items.
479 }
480 }
481
482 void DolphinViewActionHandler::slotTrashActivated()
483 {
484 if (m_currentView->selectedItemsCount() == 0) {
485 Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::MoveToTrashContents);
486 } else {
487 Q_EMIT actionBeingHandled();
488 m_currentView->trashSelectedItems();
489 Q_EMIT selectionModeChangeTriggered(false);
490 }
491 }
492
493 void DolphinViewActionHandler::slotDeleteItems()
494 {
495 if (m_currentView->selectedItemsCount() == 0) {
496 Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DeleteContents);
497 } else {
498 Q_EMIT actionBeingHandled();
499 m_currentView->deleteSelectedItems();
500 Q_EMIT selectionModeChangeTriggered(false);
501 }
502 }
503
504 void DolphinViewActionHandler::togglePreview(bool show)
505 {
506 Q_EMIT actionBeingHandled();
507 m_currentView->setPreviewsShown(show);
508 }
509
510 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown)
511 {
512 Q_UNUSED(shown)
513 // It is not enough to update the 'Show Preview' action, also
514 // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
515 updateViewActions();
516 }
517
518 QString DolphinViewActionHandler::currentViewModeActionName() const
519 {
520 switch (m_currentView->viewMode()) {
521 case DolphinView::IconsView:
522 return QStringLiteral("icons");
523 case DolphinView::DetailsView:
524 return QStringLiteral("details");
525 case DolphinView::CompactView:
526 return QStringLiteral("compact");
527 default:
528 Q_ASSERT(false);
529 break;
530 }
531 return QString(); // can't happen
532 }
533
534 KActionCollection *DolphinViewActionHandler::actionCollection()
535 {
536 return m_actionCollection;
537 }
538
539 void DolphinViewActionHandler::updateViewActions()
540 {
541 QAction *viewModeAction = m_actionCollection->action(currentViewModeActionName());
542 if (viewModeAction) {
543 viewModeAction->setChecked(true);
544
545 QAction *viewModeMenu = m_actionCollection->action(QStringLiteral("view_mode"));
546 viewModeMenu->setIcon(viewModeAction->icon());
547 }
548
549 QAction *showPreviewAction = m_actionCollection->action(QStringLiteral("show_preview"));
550 showPreviewAction->setChecked(m_currentView->previewsShown());
551
552 slotSortOrderChanged(m_currentView->sortOrder());
553 slotSortFoldersFirstChanged(m_currentView->sortFoldersFirst());
554 slotSortHiddenLastChanged(m_currentView->sortHiddenLast());
555 slotVisibleRolesChanged(m_currentView->visibleRoles(), QList<QByteArray>());
556 slotGroupedSortingChanged(m_currentView->groupedSorting());
557 slotSortRoleChanged(m_currentView->sortRole());
558 slotGroupRoleChanged(m_currentView->groupRole());
559 slotGroupOrderChanged(m_currentView->groupOrder());
560 slotZoomLevelChanged(m_currentView->zoomLevel(), -1);
561
562 // Updates the "show_hidden_files" action state and icon
563 slotHiddenFilesShownChanged(m_currentView->hiddenFilesShown());
564 }
565
566 void DolphinViewActionHandler::zoomIn()
567 {
568 const int level = m_currentView->zoomLevel();
569 m_currentView->setZoomLevel(level + 1);
570 updateViewActions();
571 }
572
573 void DolphinViewActionHandler::zoomOut()
574 {
575 const int level = m_currentView->zoomLevel();
576 m_currentView->setZoomLevel(level - 1);
577 updateViewActions();
578 }
579
580 void DolphinViewActionHandler::zoomReset()
581 {
582 m_currentView->resetZoomLevel();
583 updateViewActions();
584 }
585
586 void DolphinViewActionHandler::toggleSortFoldersFirst()
587 {
588 const bool sortFirst = m_currentView->sortFoldersFirst();
589 m_currentView->setSortFoldersFirst(!sortFirst);
590 }
591
592 void DolphinViewActionHandler::toggleSortHiddenLast()
593 {
594 const bool sortHiddenLast = m_currentView->sortHiddenLast();
595 m_currentView->setSortHiddenLast(!sortHiddenLast);
596 }
597
598 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order)
599 {
600 QAction *descending = m_actionCollection->action(QStringLiteral("sort_descending"));
601 QAction *ascending = m_actionCollection->action(QStringLiteral("sort_ascending"));
602 const bool sortDescending = (order == Qt::DescendingOrder);
603 descending->setChecked(sortDescending);
604 ascending->setChecked(!sortDescending);
605 }
606
607 void DolphinViewActionHandler::slotGroupOrderChanged(Qt::SortOrder order)
608 {
609 QAction *descending = m_actionCollection->action(QStringLiteral("group_descending"));
610 QAction *ascending = m_actionCollection->action(QStringLiteral("group_ascending"));
611 const bool groupDescending = (order == Qt::DescendingOrder);
612 descending->setChecked(groupDescending);
613 ascending->setChecked(!groupDescending);
614 }
615
616 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst)
617 {
618 m_actionCollection->action(QStringLiteral("folders_first"))->setChecked(foldersFirst);
619 }
620
621 void DolphinViewActionHandler::slotSortHiddenLastChanged(bool hiddenLast)
622 {
623 m_actionCollection->action(QStringLiteral("hidden_last"))->setChecked(hiddenLast);
624 }
625
626 void DolphinViewActionHandler::toggleVisibleRole(QAction *action)
627 {
628 Q_EMIT actionBeingHandled();
629
630 const QByteArray toggledRole = action->data().toByteArray();
631
632 QList<QByteArray> roles = m_currentView->visibleRoles();
633
634 const bool show = action->isChecked();
635
636 const int index = roles.indexOf(toggledRole);
637 const bool containsInfo = (index >= 0);
638 if (show && !containsInfo) {
639 roles.append(toggledRole);
640 m_currentView->setVisibleRoles(roles);
641 } else if (!show && containsInfo) {
642 roles.removeAt(index);
643 m_currentView->setVisibleRoles(roles);
644 Q_ASSERT(roles.indexOf(toggledRole) < 0);
645 }
646 }
647
648 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList<QByteArray> &current, const QList<QByteArray> &previous)
649 {
650 Q_UNUSED(previous)
651
652 const auto checkedRoles = QSet<QByteArray>(current.constBegin(), current.constEnd());
653 QHashIterator<QByteArray, KToggleAction *> it(m_visibleRoles);
654 while (it.hasNext()) {
655 it.next();
656 const QByteArray &role = it.key();
657 KToggleAction *action = it.value();
658 action->setChecked(checkedRoles.contains(role));
659 }
660 }
661
662 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped)
663 {
664 m_currentView->setGroupedSorting(grouped);
665 }
666
667 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting)
668 {
669 QAction *showInGroupsAction = m_actionCollection->action(QStringLiteral("show_in_groups"));
670 showInGroupsAction->setChecked(groupedSorting);
671 }
672
673 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show)
674 {
675 Q_EMIT actionBeingHandled();
676 m_currentView->setHiddenFilesShown(show);
677 }
678
679 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown)
680 {
681 QAction *showHiddenFilesAction = m_actionCollection->action(QStringLiteral("show_hidden_files"));
682 showHiddenFilesAction->setChecked(shown);
683 }
684
685 void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable)
686 {
687 m_actionCollection->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable && KProtocolManager::supportsMakeDir(currentView()->url()));
688 }
689
690 KToggleAction *DolphinViewActionHandler::iconsModeAction()
691 {
692 KToggleAction *iconsView = m_actionCollection->add<KToggleAction>(QStringLiteral("icons"));
693 iconsView->setText(i18nc("@action:inmenu View Mode", "Icons"));
694 iconsView->setToolTip(i18nc("@info", "Icons view mode"));
695 m_actionCollection->setDefaultShortcut(iconsView, Qt::CTRL | Qt::Key_1);
696 iconsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
697 iconsView->setData(QVariant::fromValue(DolphinView::IconsView));
698 return iconsView;
699 }
700
701 KToggleAction *DolphinViewActionHandler::compactModeAction()
702 {
703 KToggleAction *iconsView = m_actionCollection->add<KToggleAction>(QStringLiteral("compact"));
704 iconsView->setText(i18nc("@action:inmenu View Mode", "Compact"));
705 iconsView->setToolTip(i18nc("@info", "Compact view mode"));
706 m_actionCollection->setDefaultShortcut(iconsView, Qt::CTRL | Qt::Key_2);
707 iconsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
708 iconsView->setData(QVariant::fromValue(DolphinView::CompactView));
709 return iconsView;
710 }
711
712 KToggleAction *DolphinViewActionHandler::detailsModeAction()
713 {
714 KToggleAction *detailsView = m_actionCollection->add<KToggleAction>(QStringLiteral("details"));
715 detailsView->setText(i18nc("@action:inmenu View Mode", "Details"));
716 detailsView->setToolTip(i18nc("@info", "Details view mode"));
717 m_actionCollection->setDefaultShortcut(detailsView, Qt::CTRL | Qt::Key_3);
718 detailsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
719 detailsView->setData(QVariant::fromValue(DolphinView::DetailsView));
720 return detailsView;
721 }
722
723 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray &role)
724 {
725 KToggleAction *action = m_sortByActions.value(role);
726 if (action) {
727 action->setChecked(true);
728
729 if (!action->icon().isNull()) {
730 QAction *sortByMenu = m_actionCollection->action(QStringLiteral("sort"));
731 sortByMenu->setIcon(action->icon());
732 }
733 }
734
735 QAction *descending = m_actionCollection->action(QStringLiteral("sort_descending"));
736 QAction *ascending = m_actionCollection->action(QStringLiteral("sort_ascending"));
737
738 if (role == "text" || role == "type" || role == "extension" || role == "tags" || role == "comment") {
739 descending->setText(i18nc("Sort descending", "Z-A"));
740 ascending->setText(i18nc("Sort ascending", "A-Z"));
741 } else if (role == "size") {
742 descending->setText(i18nc("Sort descending", "Largest First"));
743 ascending->setText(i18nc("Sort ascending", "Smallest First"));
744 } else if (role == "modificationtime" || role == "creationtime" || role == "accesstime") {
745 descending->setText(i18nc("Sort descending", "Newest First"));
746 ascending->setText(i18nc("Sort ascending", "Oldest First"));
747 } else if (role == "rating") {
748 descending->setText(i18nc("Sort descending", "Highest First"));
749 ascending->setText(i18nc("Sort ascending", "Lowest First"));
750 } else {
751 descending->setText(i18nc("Sort descending", "Descending"));
752 ascending->setText(i18nc("Sort ascending", "Ascending"));
753 }
754
755 slotSortOrderChanged(m_currentView->sortOrder());
756 }
757
758 void DolphinViewActionHandler::slotGroupRoleChanged(const QByteArray &role)
759 {
760 KToggleAction *action = m_groupByActions.value(role);
761 if (action) {
762 action->setChecked(true);
763
764 if (!action->icon().isNull()) {
765 QAction *groupByMenu = m_actionCollection->action(QStringLiteral("group"));
766 groupByMenu->setIcon(action->icon());
767 }
768 }
769
770 QAction *descending = m_actionCollection->action(QStringLiteral("group_descending"));
771 QAction *ascending = m_actionCollection->action(QStringLiteral("group_ascending"));
772
773 if (role == "text" || role == "type" || role == "extension" || role == "tags" || role == "comment") {
774 descending->setText(i18nc("Group descending", "Z-A"));
775 ascending->setText(i18nc("Group ascending", "A-Z"));
776 } else if (role == "size") {
777 descending->setText(i18nc("Group descending", "Largest First"));
778 ascending->setText(i18nc("Group ascending", "Smallest First"));
779 } else if (role == "modificationtime" || role == "creationtime" || role == "accesstime") {
780 descending->setText(i18nc("Group descending", "Newest First"));
781 ascending->setText(i18nc("Group ascending", "Oldest First"));
782 } else if (role == "rating") {
783 descending->setText(i18nc("Group descending", "Highest First"));
784 ascending->setText(i18nc("Group ascending", "Lowest First"));
785 } else {
786 descending->setText(i18nc("Group descending", "Descending"));
787 ascending->setText(i18nc("Group ascending", "Ascending"));
788 }
789
790 // Disable group order selector if grouping behavior does not support it
791 if (role == "none" || role == "followSort") {
792 descending->setEnabled(false);
793 ascending->setEnabled(false);
794 } else {
795 descending->setEnabled(true);
796 ascending->setEnabled(true);
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"