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