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