]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinviewactionhandler.cpp
7a3c758a57890e8f6a433f38134829163f1ffc07
[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 *group = new QActionGroup(sortByActionMenu);
282 group->setExclusive(true);
283
284 KToggleAction *sortAscendingAction = m_actionCollection->add<KToggleAction>(QStringLiteral("sort_ascending"));
285 sortAscendingAction->setActionGroup(group);
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(group);
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 KToggleAction *groupAscendingAction = m_actionCollection->add<KToggleAction>(QStringLiteral("group_ascending"));
318 groupAscendingAction->setActionGroup(group);
319 connect(groupAscendingAction, &QAction::triggered, this, [this] {
320 m_currentView->setGroupOrder(Qt::AscendingOrder);
321 });
322
323 KToggleAction *groupDescendingAction = m_actionCollection->add<KToggleAction>(QStringLiteral("group_descending"));
324 groupDescendingAction->setActionGroup(group);
325 connect(groupDescendingAction, &QAction::triggered, this, [this] {
326 m_currentView->setGroupOrder(Qt::DescendingOrder);
327 });
328
329 groupByActionMenu->addAction(groupAscendingAction);
330 groupByActionMenu->addAction(groupDescendingAction);
331
332 // View -> Additional Information
333 QActionGroup *visibleRolesGroup = createFileItemRolesActionGroup(QStringLiteral("show_"));
334
335 KActionMenu *visibleRolesMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("additional_info"));
336 visibleRolesMenu->setText(i18nc("@action:inmenu View", "Show Additional Information"));
337 visibleRolesMenu->setIcon(QIcon::fromTheme(QStringLiteral("documentinfo")));
338 visibleRolesMenu->setPopupMode(QToolButton::InstantPopup);
339
340 const auto visibleRolesGroupActions = visibleRolesGroup->actions();
341 for (QAction *action : visibleRolesGroupActions) {
342 visibleRolesMenu->addAction(action);
343 }
344
345 KToggleAction *showInGroups = m_actionCollection->add<KToggleAction>(QStringLiteral("show_in_groups"));
346 showInGroups->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
347 showInGroups->setText(i18nc("@action:inmenu View", "Show in Groups"));
348 showInGroups->setWhatsThis(i18nc("@info:whatsthis", "This groups files and folders by their first letter."));
349 connect(showInGroups, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleGroupedSorting);
350
351 KToggleAction *showHiddenFiles = m_actionCollection->add<KToggleAction>(QStringLiteral("show_hidden_files"));
352 showHiddenFiles->setIcon(QIcon::fromTheme(QStringLiteral("view-visible")));
353 showHiddenFiles->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
354 showHiddenFiles->setWhatsThis(xi18nc("@info:whatsthis",
355 "<para>When "
356 "this is enabled, <emphasis>hidden</emphasis> files and folders "
357 "are visible. They will be displayed semi-transparent.</para>"
358 "<para>Hidden items only differ from normal ones in that their "
359 "name starts with a dot (\".\"). Typically, there is no need for "
360 "users to access them, which is why they are hidden.</para>"
361 "<para>Items can also be hidden if their names are listed in a text file "
362 "named \".hidden\". Files with the \"application/x-trash\" MIME type, "
363 "such as backup files, can also be hidden by enabling that setting in "
364 "Configure Dolphin > View > General.</para>"));
365 m_actionCollection->setDefaultShortcuts(showHiddenFiles, KStandardShortcut::showHideHiddenFiles());
366 connect(showHiddenFiles, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleShowHiddenFiles);
367
368 QAction *adjustViewProps = m_actionCollection->addAction(QStringLiteral("view_properties"));
369 adjustViewProps->setText(i18nc("@action:inmenu View", "Adjust View Display Style…"));
370 adjustViewProps->setIcon(QIcon::fromTheme(QStringLiteral("view-choose")));
371 adjustViewProps->setWhatsThis(i18nc("@info:whatsthis",
372 "This opens a window "
373 "in which all folder view properties can be adjusted."));
374 connect(adjustViewProps, &QAction::triggered, this, &DolphinViewActionHandler::slotAdjustViewProperties);
375 }
376
377 QActionGroup *DolphinViewActionHandler::createFileItemRolesActionGroup(const QString &groupPrefix)
378 {
379 const bool isSortGroup = (groupPrefix == QLatin1String("sort_by_"));
380 const bool isGroupGroup = (groupPrefix == QLatin1String("group_by_"));
381 Q_ASSERT(isSortGroup || isGroupGroup || groupPrefix == QLatin1String("show_"));
382
383 QActionGroup *rolesActionGroup = new QActionGroup(m_actionCollection);
384 rolesActionGroup->setExclusive(isSortGroup || isGroupGroup);
385 if (isSortGroup) {
386 connect(rolesActionGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::slotSortTriggered);
387 } else if (isGroupGroup) {
388 connect(rolesActionGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::slotGroupTriggered);
389 } else {
390 connect(rolesActionGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::toggleVisibleRole);
391 }
392
393 QString groupName;
394 KActionMenu *groupMenu = nullptr;
395 QActionGroup *groupMenuGroup = nullptr;
396
397 bool indexingEnabled = false;
398 #if HAVE_BALOO
399 Baloo::IndexerConfig config;
400 indexingEnabled = config.fileIndexingEnabled();
401 #endif
402
403 QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
404 // Unlike sorting, grouping is optional. If creating for group_by_, include a None role.
405 if (isGroupGroup)
406 rolesInfo.append(KFileItemModel::roleInformation(nullptr));
407
408 for (const KFileItemModel::RoleInfo &info : rolesInfo) {
409 if (!isSortGroup && !isGroupGroup && info.role == "text") {
410 // It should not be possible to hide the "text" role
411 continue;
412 }
413
414 KToggleAction *action = nullptr;
415 const QString name = groupPrefix + info.role;
416 if (info.group.isEmpty()) {
417 action = m_actionCollection->add<KToggleAction>(name);
418 action->setActionGroup(rolesActionGroup);
419 } else {
420 if (!groupMenu || info.group != groupName) {
421 groupName = info.group;
422 groupMenu = m_actionCollection->add<KActionMenu>(groupName);
423 groupMenu->setText(groupName);
424 groupMenu->setActionGroup(rolesActionGroup);
425
426 groupMenuGroup = new QActionGroup(groupMenu);
427 groupMenuGroup->setExclusive(isSortGroup || isGroupGroup);
428 if (isSortGroup) {
429 connect(groupMenuGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::slotSortTriggered);
430 } else if (isGroupGroup) {
431 connect(groupMenuGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::slotGroupTriggered);
432 } else {
433 connect(groupMenuGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::toggleVisibleRole);
434 }
435 }
436
437 action = new KToggleAction(groupMenu);
438 action->setActionGroup(groupMenuGroup);
439 groupMenu->addAction(action);
440 }
441 action->setText(info.translation);
442 action->setData(info.role);
443
444 const bool enable = (!info.requiresBaloo && !info.requiresIndexer) || (info.requiresBaloo) || (info.requiresIndexer && indexingEnabled);
445 action->setEnabled(enable);
446
447 if (isSortGroup) {
448 m_sortByActions.insert(info.role, action);
449 } else if (isGroupGroup) {
450 m_groupByActions.insert(info.role, action);
451 } else {
452 m_visibleRoles.insert(info.role, action);
453 }
454 }
455
456 return rolesActionGroup;
457 }
458
459 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction *action)
460 {
461 const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
462 m_currentView->setViewMode(mode);
463
464 QAction *viewModeMenu = m_actionCollection->action(QStringLiteral("view_mode"));
465 viewModeMenu->setIcon(action->icon());
466 }
467
468 void DolphinViewActionHandler::slotRename()
469 {
470 if (m_currentView->selectedItemsCount() == 0) {
471 Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::RenameContents);
472 } else {
473 Q_EMIT actionBeingHandled();
474 m_currentView->renameSelectedItems();
475 // We don't exit selectionMode here because users might want to rename more items.
476 }
477 }
478
479 void DolphinViewActionHandler::slotTrashActivated()
480 {
481 if (m_currentView->selectedItemsCount() == 0) {
482 Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::MoveToTrashContents);
483 } else {
484 Q_EMIT actionBeingHandled();
485 m_currentView->trashSelectedItems();
486 Q_EMIT selectionModeChangeTriggered(false);
487 }
488 }
489
490 void DolphinViewActionHandler::slotDeleteItems()
491 {
492 if (m_currentView->selectedItemsCount() == 0) {
493 Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DeleteContents);
494 } else {
495 Q_EMIT actionBeingHandled();
496 m_currentView->deleteSelectedItems();
497 Q_EMIT selectionModeChangeTriggered(false);
498 }
499 }
500
501 void DolphinViewActionHandler::togglePreview(bool show)
502 {
503 Q_EMIT actionBeingHandled();
504 m_currentView->setPreviewsShown(show);
505 }
506
507 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown)
508 {
509 Q_UNUSED(shown)
510 // It is not enough to update the 'Show Preview' action, also
511 // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
512 updateViewActions();
513 }
514
515 QString DolphinViewActionHandler::currentViewModeActionName() const
516 {
517 switch (m_currentView->viewMode()) {
518 case DolphinView::IconsView:
519 return QStringLiteral("icons");
520 case DolphinView::DetailsView:
521 return QStringLiteral("details");
522 case DolphinView::CompactView:
523 return QStringLiteral("compact");
524 default:
525 Q_ASSERT(false);
526 break;
527 }
528 return QString(); // can't happen
529 }
530
531 KActionCollection *DolphinViewActionHandler::actionCollection()
532 {
533 return m_actionCollection;
534 }
535
536 void DolphinViewActionHandler::updateViewActions()
537 {
538 QAction *viewModeAction = m_actionCollection->action(currentViewModeActionName());
539 if (viewModeAction) {
540 viewModeAction->setChecked(true);
541
542 QAction *viewModeMenu = m_actionCollection->action(QStringLiteral("view_mode"));
543 viewModeMenu->setIcon(viewModeAction->icon());
544 }
545
546 QAction *showPreviewAction = m_actionCollection->action(QStringLiteral("show_preview"));
547 showPreviewAction->setChecked(m_currentView->previewsShown());
548
549 slotSortOrderChanged(m_currentView->sortOrder());
550 slotSortFoldersFirstChanged(m_currentView->sortFoldersFirst());
551 slotSortHiddenLastChanged(m_currentView->sortHiddenLast());
552 slotVisibleRolesChanged(m_currentView->visibleRoles(), QList<QByteArray>());
553 slotGroupedSortingChanged(m_currentView->groupedSorting());
554 slotSortRoleChanged(m_currentView->sortRole());
555 slotGroupRoleChanged(m_currentView->groupRole());
556 slotGroupOrderChanged(m_currentView->groupOrder());
557 slotZoomLevelChanged(m_currentView->zoomLevel(), -1);
558
559 // Updates the "show_hidden_files" action state and icon
560 slotHiddenFilesShownChanged(m_currentView->hiddenFilesShown());
561 }
562
563 void DolphinViewActionHandler::zoomIn()
564 {
565 const int level = m_currentView->zoomLevel();
566 m_currentView->setZoomLevel(level + 1);
567 updateViewActions();
568 }
569
570 void DolphinViewActionHandler::zoomOut()
571 {
572 const int level = m_currentView->zoomLevel();
573 m_currentView->setZoomLevel(level - 1);
574 updateViewActions();
575 }
576
577 void DolphinViewActionHandler::zoomReset()
578 {
579 m_currentView->resetZoomLevel();
580 updateViewActions();
581 }
582
583 void DolphinViewActionHandler::toggleSortFoldersFirst()
584 {
585 const bool sortFirst = m_currentView->sortFoldersFirst();
586 m_currentView->setSortFoldersFirst(!sortFirst);
587 }
588
589 void DolphinViewActionHandler::toggleSortHiddenLast()
590 {
591 const bool sortHiddenLast = m_currentView->sortHiddenLast();
592 m_currentView->setSortHiddenLast(!sortHiddenLast);
593 }
594
595 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order)
596 {
597 QAction *descending = m_actionCollection->action(QStringLiteral("sort_descending"));
598 QAction *ascending = m_actionCollection->action(QStringLiteral("sort_ascending"));
599 const bool sortDescending = (order == Qt::DescendingOrder);
600 descending->setChecked(sortDescending);
601 ascending->setChecked(!sortDescending);
602 }
603
604 void DolphinViewActionHandler::slotGroupOrderChanged(Qt::SortOrder order)
605 {
606 QAction *descending = m_actionCollection->action(QStringLiteral("group_descending"));
607 QAction *ascending = m_actionCollection->action(QStringLiteral("group_ascending"));
608 const bool sortDescending = (order == Qt::DescendingOrder);
609 descending->setChecked(sortDescending);
610 ascending->setChecked(!sortDescending);
611 }
612
613 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst)
614 {
615 m_actionCollection->action(QStringLiteral("folders_first"))->setChecked(foldersFirst);
616 }
617
618 void DolphinViewActionHandler::slotSortHiddenLastChanged(bool hiddenLast)
619 {
620 m_actionCollection->action(QStringLiteral("hidden_last"))->setChecked(hiddenLast);
621 }
622
623 void DolphinViewActionHandler::toggleVisibleRole(QAction *action)
624 {
625 Q_EMIT actionBeingHandled();
626
627 const QByteArray toggledRole = action->data().toByteArray();
628
629 QList<QByteArray> roles = m_currentView->visibleRoles();
630
631 const bool show = action->isChecked();
632
633 const int index = roles.indexOf(toggledRole);
634 const bool containsInfo = (index >= 0);
635 if (show && !containsInfo) {
636 roles.append(toggledRole);
637 m_currentView->setVisibleRoles(roles);
638 } else if (!show && containsInfo) {
639 roles.removeAt(index);
640 m_currentView->setVisibleRoles(roles);
641 Q_ASSERT(roles.indexOf(toggledRole) < 0);
642 }
643 }
644
645 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList<QByteArray> &current, const QList<QByteArray> &previous)
646 {
647 Q_UNUSED(previous)
648
649 const auto checkedRoles = QSet<QByteArray>(current.constBegin(), current.constEnd());
650 QHashIterator<QByteArray, KToggleAction *> it(m_visibleRoles);
651 while (it.hasNext()) {
652 it.next();
653 const QByteArray &role = it.key();
654 KToggleAction *action = it.value();
655 action->setChecked(checkedRoles.contains(role));
656 }
657 }
658
659 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped)
660 {
661 m_currentView->setGroupedSorting(grouped);
662 }
663
664 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting)
665 {
666 QAction *showInGroupsAction = m_actionCollection->action(QStringLiteral("show_in_groups"));
667 showInGroupsAction->setChecked(groupedSorting);
668 }
669
670 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show)
671 {
672 Q_EMIT actionBeingHandled();
673 m_currentView->setHiddenFilesShown(show);
674 }
675
676 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown)
677 {
678 QAction *showHiddenFilesAction = m_actionCollection->action(QStringLiteral("show_hidden_files"));
679 showHiddenFilesAction->setChecked(shown);
680 }
681
682 void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable)
683 {
684 m_actionCollection->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable && KProtocolManager::supportsMakeDir(currentView()->url()));
685 }
686
687 KToggleAction *DolphinViewActionHandler::iconsModeAction()
688 {
689 KToggleAction *iconsView = m_actionCollection->add<KToggleAction>(QStringLiteral("icons"));
690 iconsView->setText(i18nc("@action:inmenu View Mode", "Icons"));
691 iconsView->setToolTip(i18nc("@info", "Icons view mode"));
692 m_actionCollection->setDefaultShortcut(iconsView, Qt::CTRL | Qt::Key_1);
693 iconsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
694 iconsView->setData(QVariant::fromValue(DolphinView::IconsView));
695 return iconsView;
696 }
697
698 KToggleAction *DolphinViewActionHandler::compactModeAction()
699 {
700 KToggleAction *iconsView = m_actionCollection->add<KToggleAction>(QStringLiteral("compact"));
701 iconsView->setText(i18nc("@action:inmenu View Mode", "Compact"));
702 iconsView->setToolTip(i18nc("@info", "Compact view mode"));
703 m_actionCollection->setDefaultShortcut(iconsView, Qt::CTRL | Qt::Key_2);
704 iconsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
705 iconsView->setData(QVariant::fromValue(DolphinView::CompactView));
706 return iconsView;
707 }
708
709 KToggleAction *DolphinViewActionHandler::detailsModeAction()
710 {
711 KToggleAction *detailsView = m_actionCollection->add<KToggleAction>(QStringLiteral("details"));
712 detailsView->setText(i18nc("@action:inmenu View Mode", "Details"));
713 detailsView->setToolTip(i18nc("@info", "Details view mode"));
714 m_actionCollection->setDefaultShortcut(detailsView, Qt::CTRL | Qt::Key_3);
715 detailsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
716 detailsView->setData(QVariant::fromValue(DolphinView::DetailsView));
717 return detailsView;
718 }
719
720 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray &role)
721 {
722 KToggleAction *action = m_sortByActions.value(role);
723 if (action) {
724 action->setChecked(true);
725
726 if (!action->icon().isNull()) {
727 QAction *sortByMenu = m_actionCollection->action(QStringLiteral("sort"));
728 sortByMenu->setIcon(action->icon());
729 }
730 }
731
732 QAction *descending = m_actionCollection->action(QStringLiteral("sort_descending"));
733 QAction *ascending = m_actionCollection->action(QStringLiteral("sort_ascending"));
734
735 if (role == "text" || role == "type" || role == "extension" || role == "tags" || role == "comment") {
736 descending->setText(i18nc("Sort descending", "Z-A"));
737 ascending->setText(i18nc("Sort ascending", "A-Z"));
738 } else if (role == "size") {
739 descending->setText(i18nc("Sort descending", "Largest First"));
740 ascending->setText(i18nc("Sort ascending", "Smallest First"));
741 } else if (role == "modificationtime" || role == "creationtime" || role == "accesstime") {
742 descending->setText(i18nc("Sort descending", "Newest First"));
743 ascending->setText(i18nc("Sort ascending", "Oldest First"));
744 } else if (role == "rating") {
745 descending->setText(i18nc("Sort descending", "Highest First"));
746 ascending->setText(i18nc("Sort ascending", "Lowest First"));
747 } else {
748 descending->setText(i18nc("Sort descending", "Descending"));
749 ascending->setText(i18nc("Sort ascending", "Ascending"));
750 }
751
752 slotSortOrderChanged(m_currentView->sortOrder());
753 }
754
755 void DolphinViewActionHandler::slotGroupRoleChanged(const QByteArray &role)
756 {
757 KToggleAction *action = m_groupByActions.value(role);
758 if (action) {
759 action->setChecked(true);
760
761 if (!action->icon().isNull()) {
762 QAction *groupByMenu = m_actionCollection->action(QStringLiteral("group"));
763 groupByMenu->setIcon(action->icon());
764 }
765 }
766
767 QAction *descending = m_actionCollection->action(QStringLiteral("group_descending"));
768 QAction *ascending = m_actionCollection->action(QStringLiteral("group_ascending"));
769
770 if (role == "text" || role == "type" || role == "extension" || role == "tags" || role == "comment") {
771 descending->setText(i18nc("Group descending", "Z-A"));
772 ascending->setText(i18nc("Group ascending", "A-Z"));
773 } else if (role == "size") {
774 descending->setText(i18nc("Group descending", "Largest First"));
775 ascending->setText(i18nc("Group ascending", "Smallest First"));
776 } else if (role == "modificationtime" || role == "creationtime" || role == "accesstime") {
777 descending->setText(i18nc("Group descending", "Newest First"));
778 ascending->setText(i18nc("Group ascending", "Oldest First"));
779 } else if (role == "rating") {
780 descending->setText(i18nc("Group descending", "Highest First"));
781 ascending->setText(i18nc("Group ascending", "Lowest First"));
782 } else {
783 descending->setText(i18nc("Group descending", "Descending"));
784 ascending->setText(i18nc("Group ascending", "Ascending"));
785 }
786
787 slotGroupOrderChanged(m_currentView->groupOrder());
788 }
789
790 void DolphinViewActionHandler::slotZoomLevelChanged(int current, int previous)
791 {
792 Q_UNUSED(previous)
793
794 QAction *zoomInAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomIn));
795 if (zoomInAction) {
796 zoomInAction->setEnabled(current < ZoomLevelInfo::maximumLevel());
797 }
798
799 QAction *zoomOutAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomOut));
800 if (zoomOutAction) {
801 zoomOutAction->setEnabled(current > ZoomLevelInfo::minimumLevel());
802 }
803 }
804
805 void DolphinViewActionHandler::slotSortTriggered(QAction *action)
806 {
807 // The radiobuttons of the "Sort By"-menu are split between the main-menu
808 // and several sub-menus. Because of this they don't have a common
809 // action-group that assures an exclusive toggle-state between the main-menu
810 // actions and the sub-menu-actions. If an action gets checked, it must
811 // be assured that all other actions get unchecked, except the ascending/
812 // descending actions
813 for (QAction *groupAction : std::as_const(m_sortByActions)) {
814 KActionMenu *actionMenu = qobject_cast<KActionMenu *>(groupAction);
815 if (actionMenu) {
816 const auto actions = actionMenu->menu()->actions();
817 for (QAction *subAction : actions) {
818 subAction->setChecked(false);
819 }
820 } else if (groupAction->actionGroup()) {
821 groupAction->setChecked(false);
822 }
823 }
824 action->setChecked(true);
825
826 // Apply the activated sort-role to the view
827 const QByteArray role = action->data().toByteArray();
828 m_currentView->setSortRole(role);
829 }
830
831 void DolphinViewActionHandler::slotGroupTriggered(QAction *action)
832 {
833 // The radiobuttons of the "Group By"-menu are split between the main-menu
834 // and several sub-menus. Because of this they don't have a common
835 // action-group that assures an exclusive toggle-state between the main-menu
836 // actions and the sub-menu-actions. If an action gets checked, it must
837 // be assured that all other actions get unchecked, except the ascending/
838 // descending actions
839 for (QAction *groupAction : std::as_const(m_sortByActions)) {
840 KActionMenu *actionMenu = qobject_cast<KActionMenu *>(groupAction);
841 if (actionMenu) {
842 const auto actions = actionMenu->menu()->actions();
843 for (QAction *subAction : actions) {
844 subAction->setChecked(false);
845 }
846 } else if (groupAction->actionGroup()) {
847 groupAction->setChecked(false);
848 }
849 }
850 action->setChecked(true);
851
852 // Apply the activated sort-role to the view
853 const QByteArray role = action->data().toByteArray();
854 m_currentView->setGroupRole(role);
855 }
856
857 void DolphinViewActionHandler::slotAdjustViewProperties()
858 {
859 Q_EMIT actionBeingHandled();
860 QPointer<ViewPropertiesDialog> dialog = new ViewPropertiesDialog(m_currentView);
861 dialog->exec();
862 delete dialog;
863 }
864
865 void DolphinViewActionHandler::slotDuplicate()
866 {
867 if (m_currentView->selectedItemsCount() == 0) {
868 Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DuplicateContents);
869 } else {
870 Q_EMIT actionBeingHandled();
871 m_currentView->duplicateSelectedItems();
872 Q_EMIT selectionModeChangeTriggered(false);
873 }
874 }
875
876 void DolphinViewActionHandler::slotProperties()
877 {
878 KPropertiesDialog *dialog = nullptr;
879 const KFileItemList list = m_currentView->selectedItems();
880 if (list.isEmpty()) {
881 const QUrl url = m_currentView->url();
882 dialog = new KPropertiesDialog(url, m_currentView);
883 } else {
884 dialog = new KPropertiesDialog(list, m_currentView);
885 }
886
887 dialog->setAttribute(Qt::WA_DeleteOnClose);
888 dialog->show();
889 dialog->raise();
890 dialog->activateWindow();
891 }
892
893 void DolphinViewActionHandler::slotCopyPath()
894 {
895 if (m_currentView->selectedItemsCount() == 0) {
896 Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::CopyLocationContents);
897 } else {
898 m_currentView->copyPathToClipboard();
899 Q_EMIT selectionModeChangeTriggered(false);
900 }
901 }
902
903 void DolphinViewActionHandler::slotSelectionChanged(const KFileItemList &selection)
904 {
905 QString basicActionsMenuText;
906 if (selection.isEmpty()) {
907 basicActionsMenuText = i18nc("@action:inmenu menu with actions like copy, paste, rename. The user's selection is empty when this text is shown.",
908 "Actions for Current View");
909 } else {
910 // clang-format off
911 QFontMetrics fontMetrics = QMenu().fontMetrics();
912 // i18n: @action:inmenu menu with actions like copy, paste, rename.
913 // %1 is a textual representation of the currently selected files or folders. This can be the name of
914 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
915 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
916 // and a fallback will be used.
917 basicActionsMenuText = i18n("Actions for %1", fileItemListToString(selection, fontMetrics.averageCharWidth() * 40, fontMetrics, ItemsState::Selected));
918 // clang-format on
919 }
920
921 if (basicActionsMenuText == QStringLiteral("NULL")) {
922 const KFileItemListProperties properties(selection);
923 basicActionsMenuText = i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
924 "Actions for One Selected Item",
925 "Actions for %1 Selected Items",
926 selection.count());
927 }
928
929 QAction *basicActionsMenu = m_actionCollection->action(QStringLiteral("basic_actions"));
930 basicActionsMenu->setText(basicActionsMenuText);
931
932 // Add or remove contextual actions
933 while (!basicActionsMenu->menu()->actions().constLast()->isSeparator()) {
934 basicActionsMenu->menu()->removeAction(basicActionsMenu->menu()->actions().last());
935 }
936 if (selection.count() == 1) {
937 if (selection.first().isLink()) {
938 basicActionsMenu->menu()->addAction(m_actionCollection->action(QStringLiteral("show_target")));
939 }
940 if (selection.first().isDir()) {
941 basicActionsMenu->menu()->addAction(m_actionCollection->action(QStringLiteral("add_to_places")));
942 }
943 }
944 }
945
946 #include "moc_dolphinviewactionhandler.cpp"