]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinviewactionhandler.cpp
dolphinview: Fix right-click on header whilst unfocused
[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 amounts to 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 where "
106 "items can be deleted from 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 completely. They can "
118 "not 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 keep the 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 to the left of it. This way you can view "
211 "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", "View Mode"));
215 viewModeActions->addAction(iconsAction);
216 viewModeActions->addAction(compactAction);
217 viewModeActions->addAction(detailsAction);
218 viewModeActions->setToolBarMode(KSelectAction::MenuMode);
219 connect(viewModeActions, &KSelectAction::actionTriggered, this, &DolphinViewActionHandler::slotViewModeActionTriggered);
220
221 QAction *zoomInAction = KStandardAction::zoomIn(this, &DolphinViewActionHandler::zoomIn, m_actionCollection);
222 zoomInAction->setWhatsThis(i18nc("@info:whatsthis zoom in", "This increases the icon size."));
223
224 QAction *zoomResetAction = m_actionCollection->addAction(QStringLiteral("view_zoom_reset"));
225 zoomResetAction->setText(i18nc("@action:inmenu View", "Reset Zoom Level"));
226 zoomResetAction->setToolTip(i18n("Zoom To Default"));
227 zoomResetAction->setWhatsThis(i18nc("@info:whatsthis zoom reset", "This resets the icon size to default."));
228 zoomResetAction->setIcon(QIcon::fromTheme(QStringLiteral("zoom-original")));
229 m_actionCollection->setDefaultShortcuts(zoomResetAction, {Qt::CTRL | Qt::Key_0});
230 connect(zoomResetAction, &QAction::triggered, this, &DolphinViewActionHandler::zoomReset);
231
232 QAction *zoomOutAction = KStandardAction::zoomOut(this, &DolphinViewActionHandler::zoomOut, m_actionCollection);
233 zoomOutAction->setWhatsThis(i18nc("@info:whatsthis zoom out", "This reduces the icon size."));
234
235 KActionMenu *zoomMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("zoom"));
236 zoomMenu->setText(i18nc("@action:inmenu menu of zoom actions", "Zoom"));
237 zoomMenu->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
238 zoomMenu->setPopupMode(QToolButton::InstantPopup);
239 zoomMenu->addAction(zoomInAction);
240 zoomMenu->addAction(zoomResetAction);
241 zoomMenu->addAction(zoomOutAction);
242
243 KToggleAction *showPreview = m_actionCollection->add<KToggleAction>(QStringLiteral("show_preview"));
244 showPreview->setText(i18nc("@action:intoolbar", "Show Previews"));
245 showPreview->setToolTip(i18nc("@info", "Show preview of files and folders"));
246 showPreview->setWhatsThis(xi18nc("@info:whatsthis",
247 "When this is "
248 "enabled, the icons are based on the actual file or folder "
249 "contents.<nl/>For example the icons of images become scaled "
250 "down versions of the images."));
251 showPreview->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
252 m_actionCollection->setDefaultShortcut(showPreview, QKeySequence(Qt::Key_F12));
253 connect(showPreview, &KToggleAction::triggered, this, &DolphinViewActionHandler::togglePreview);
254
255 KToggleAction *sortFoldersFirst = m_actionCollection->add<KToggleAction>(QStringLiteral("folders_first"));
256 sortFoldersFirst->setText(i18nc("@action:inmenu Sort", "Folders First"));
257 connect(sortFoldersFirst, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleSortFoldersFirst);
258
259 KToggleAction *sortHiddenLast = m_actionCollection->add<KToggleAction>(QStringLiteral("hidden_last"));
260 sortHiddenLast->setText(i18nc("@action:inmenu Sort", "Hidden Files Last"));
261 connect(sortHiddenLast, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleSortHiddenLast);
262
263 // View -> Sort By
264 QActionGroup *sortByActionGroup = createFileItemRolesActionGroup(QStringLiteral("sort_by_"));
265
266 KActionMenu *sortByActionMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("sort"));
267 sortByActionMenu->setIcon(QIcon::fromTheme(QStringLiteral("view-sort")));
268 sortByActionMenu->setText(i18nc("@action:inmenu View", "Sort By"));
269 sortByActionMenu->setPopupMode(QToolButton::InstantPopup);
270
271 const auto sortByActionGroupActions = sortByActionGroup->actions();
272 for (QAction *action : sortByActionGroupActions) {
273 sortByActionMenu->addAction(action);
274 }
275
276 sortByActionMenu->addSeparator();
277
278 QActionGroup *group = new QActionGroup(sortByActionMenu);
279 group->setExclusive(true);
280
281 KToggleAction *ascendingAction = m_actionCollection->add<KToggleAction>(QStringLiteral("ascending"));
282 ascendingAction->setActionGroup(group);
283 connect(ascendingAction, &QAction::triggered, this, [this] {
284 m_currentView->setSortOrder(Qt::AscendingOrder);
285 });
286
287 KToggleAction *descendingAction = m_actionCollection->add<KToggleAction>(QStringLiteral("descending"));
288 descendingAction->setActionGroup(group);
289 connect(descendingAction, &QAction::triggered, this, [this] {
290 m_currentView->setSortOrder(Qt::DescendingOrder);
291 });
292
293 sortByActionMenu->addAction(ascendingAction);
294 sortByActionMenu->addAction(descendingAction);
295 sortByActionMenu->addSeparator();
296 sortByActionMenu->addAction(sortFoldersFirst);
297 sortByActionMenu->addAction(sortHiddenLast);
298
299 // View -> Additional Information
300 QActionGroup *visibleRolesGroup = createFileItemRolesActionGroup(QStringLiteral("show_"));
301
302 KActionMenu *visibleRolesMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("additional_info"));
303 visibleRolesMenu->setText(i18nc("@action:inmenu View", "Show Additional Information"));
304 visibleRolesMenu->setIcon(QIcon::fromTheme(QStringLiteral("documentinfo")));
305 visibleRolesMenu->setPopupMode(QToolButton::InstantPopup);
306
307 const auto visibleRolesGroupActions = visibleRolesGroup->actions();
308 for (QAction *action : visibleRolesGroupActions) {
309 visibleRolesMenu->addAction(action);
310 }
311
312 KToggleAction *showInGroups = m_actionCollection->add<KToggleAction>(QStringLiteral("show_in_groups"));
313 showInGroups->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
314 showInGroups->setText(i18nc("@action:inmenu View", "Show in Groups"));
315 showInGroups->setWhatsThis(i18nc("@info:whatsthis", "This groups files and folders by their first letter."));
316 connect(showInGroups, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleGroupedSorting);
317
318 KToggleAction *showHiddenFiles = m_actionCollection->add<KToggleAction>(QStringLiteral("show_hidden_files"));
319 showHiddenFiles->setIcon(QIcon::fromTheme(QStringLiteral("view-visible")));
320 showHiddenFiles->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
321 showHiddenFiles->setWhatsThis(xi18nc("@info:whatsthis",
322 "<para>When "
323 "this is enabled <emphasis>hidden</emphasis> files and folders "
324 "are visible. They will be displayed semi-transparent.</para>"
325 "<para>Hidden items only differ from other ones in that their "
326 "name starts with a \".\". In general there is no need for "
327 "users to access them which is why they are hidden.</para>"));
328 m_actionCollection->setDefaultShortcuts(showHiddenFiles, KStandardShortcut::showHideHiddenFiles());
329 connect(showHiddenFiles, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleShowHiddenFiles);
330
331 QAction *adjustViewProps = m_actionCollection->addAction(QStringLiteral("view_properties"));
332 adjustViewProps->setText(i18nc("@action:inmenu View", "Adjust View Display Style…"));
333 adjustViewProps->setIcon(QIcon::fromTheme(QStringLiteral("view-choose")));
334 adjustViewProps->setWhatsThis(i18nc("@info:whatsthis",
335 "This opens a window "
336 "in which all folder view properties can be adjusted."));
337 connect(adjustViewProps, &QAction::triggered, this, &DolphinViewActionHandler::slotAdjustViewProperties);
338 }
339
340 QActionGroup *DolphinViewActionHandler::createFileItemRolesActionGroup(const QString &groupPrefix)
341 {
342 const bool isSortGroup = (groupPrefix == QLatin1String("sort_by_"));
343 Q_ASSERT(isSortGroup || groupPrefix == QLatin1String("show_"));
344
345 QActionGroup *rolesActionGroup = new QActionGroup(m_actionCollection);
346 rolesActionGroup->setExclusive(isSortGroup);
347 if (isSortGroup) {
348 connect(rolesActionGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::slotSortTriggered);
349 } else {
350 connect(rolesActionGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::toggleVisibleRole);
351 }
352
353 QString groupName;
354 KActionMenu *groupMenu = nullptr;
355 QActionGroup *groupMenuGroup = nullptr;
356
357 bool indexingEnabled = false;
358 #if HAVE_BALOO
359 Baloo::IndexerConfig config;
360 indexingEnabled = config.fileIndexingEnabled();
361 #endif
362
363 const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
364 for (const KFileItemModel::RoleInfo &info : rolesInfo) {
365 if (!isSortGroup && info.role == "text") {
366 // It should not be possible to hide the "text" role
367 continue;
368 }
369
370 KToggleAction *action = nullptr;
371 const QString name = groupPrefix + info.role;
372 if (info.group.isEmpty()) {
373 action = m_actionCollection->add<KToggleAction>(name);
374 action->setActionGroup(rolesActionGroup);
375 } else {
376 if (!groupMenu || info.group != groupName) {
377 groupName = info.group;
378 groupMenu = m_actionCollection->add<KActionMenu>(groupName);
379 groupMenu->setText(groupName);
380 groupMenu->setActionGroup(rolesActionGroup);
381
382 groupMenuGroup = new QActionGroup(groupMenu);
383 groupMenuGroup->setExclusive(isSortGroup);
384 if (isSortGroup) {
385 connect(groupMenuGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::slotSortTriggered);
386 } else {
387 connect(groupMenuGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::toggleVisibleRole);
388 }
389 }
390
391 action = new KToggleAction(groupMenu);
392 action->setActionGroup(groupMenuGroup);
393 groupMenu->addAction(action);
394 }
395 action->setText(info.translation);
396 action->setData(info.role);
397
398 const bool enable = (!info.requiresBaloo && !info.requiresIndexer) || (info.requiresBaloo) || (info.requiresIndexer && indexingEnabled);
399 action->setEnabled(enable);
400
401 if (isSortGroup) {
402 m_sortByActions.insert(info.role, action);
403 } else {
404 m_visibleRoles.insert(info.role, action);
405 }
406 }
407
408 return rolesActionGroup;
409 }
410
411 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction *action)
412 {
413 const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
414 m_currentView->setViewMode(mode);
415
416 QAction *viewModeMenu = m_actionCollection->action(QStringLiteral("view_mode"));
417 viewModeMenu->setIcon(action->icon());
418 }
419
420 void DolphinViewActionHandler::slotRename()
421 {
422 if (m_currentView->selectedItemsCount() == 0) {
423 Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::RenameContents);
424 } else {
425 Q_EMIT actionBeingHandled();
426 m_currentView->renameSelectedItems();
427 // We don't exit selectionMode here because users might want to rename more items.
428 }
429 }
430
431 void DolphinViewActionHandler::slotTrashActivated()
432 {
433 if (m_currentView->selectedItemsCount() == 0) {
434 Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::MoveToTrashContents);
435 } else {
436 Q_EMIT actionBeingHandled();
437 m_currentView->trashSelectedItems();
438 Q_EMIT selectionModeChangeTriggered(false);
439 }
440 }
441
442 void DolphinViewActionHandler::slotDeleteItems()
443 {
444 if (m_currentView->selectedItemsCount() == 0) {
445 Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DeleteContents);
446 } else {
447 Q_EMIT actionBeingHandled();
448 m_currentView->deleteSelectedItems();
449 Q_EMIT selectionModeChangeTriggered(false);
450 }
451 }
452
453 void DolphinViewActionHandler::togglePreview(bool show)
454 {
455 Q_EMIT actionBeingHandled();
456 m_currentView->setPreviewsShown(show);
457 }
458
459 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown)
460 {
461 Q_UNUSED(shown)
462 // It is not enough to update the 'Show Preview' action, also
463 // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
464 updateViewActions();
465 }
466
467 QString DolphinViewActionHandler::currentViewModeActionName() const
468 {
469 switch (m_currentView->viewMode()) {
470 case DolphinView::IconsView:
471 return QStringLiteral("icons");
472 case DolphinView::DetailsView:
473 return QStringLiteral("details");
474 case DolphinView::CompactView:
475 return QStringLiteral("compact");
476 default:
477 Q_ASSERT(false);
478 break;
479 }
480 return QString(); // can't happen
481 }
482
483 KActionCollection *DolphinViewActionHandler::actionCollection()
484 {
485 return m_actionCollection;
486 }
487
488 void DolphinViewActionHandler::updateViewActions()
489 {
490 QAction *viewModeAction = m_actionCollection->action(currentViewModeActionName());
491 if (viewModeAction) {
492 viewModeAction->setChecked(true);
493
494 QAction *viewModeMenu = m_actionCollection->action(QStringLiteral("view_mode"));
495 viewModeMenu->setIcon(viewModeAction->icon());
496 }
497
498 QAction *showPreviewAction = m_actionCollection->action(QStringLiteral("show_preview"));
499 showPreviewAction->setChecked(m_currentView->previewsShown());
500
501 slotSortOrderChanged(m_currentView->sortOrder());
502 slotSortFoldersFirstChanged(m_currentView->sortFoldersFirst());
503 slotSortHiddenLastChanged(m_currentView->sortHiddenLast());
504 slotVisibleRolesChanged(m_currentView->visibleRoles(), QList<QByteArray>());
505 slotGroupedSortingChanged(m_currentView->groupedSorting());
506 slotSortRoleChanged(m_currentView->sortRole());
507 slotZoomLevelChanged(m_currentView->zoomLevel(), -1);
508
509 // Updates the "show_hidden_files" action state and icon
510 slotHiddenFilesShownChanged(m_currentView->hiddenFilesShown());
511 }
512
513 void DolphinViewActionHandler::zoomIn()
514 {
515 const int level = m_currentView->zoomLevel();
516 m_currentView->setZoomLevel(level + 1);
517 updateViewActions();
518 }
519
520 void DolphinViewActionHandler::zoomOut()
521 {
522 const int level = m_currentView->zoomLevel();
523 m_currentView->setZoomLevel(level - 1);
524 updateViewActions();
525 }
526
527 void DolphinViewActionHandler::zoomReset()
528 {
529 m_currentView->resetZoomLevel();
530 updateViewActions();
531 }
532
533 void DolphinViewActionHandler::toggleSortFoldersFirst()
534 {
535 const bool sortFirst = m_currentView->sortFoldersFirst();
536 m_currentView->setSortFoldersFirst(!sortFirst);
537 }
538
539 void DolphinViewActionHandler::toggleSortHiddenLast()
540 {
541 const bool sortHiddenLast = m_currentView->sortHiddenLast();
542 m_currentView->setSortHiddenLast(!sortHiddenLast);
543 }
544
545 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order)
546 {
547 QAction *descending = m_actionCollection->action(QStringLiteral("descending"));
548 QAction *ascending = m_actionCollection->action(QStringLiteral("ascending"));
549 const bool sortDescending = (order == Qt::DescendingOrder);
550 descending->setChecked(sortDescending);
551 ascending->setChecked(!sortDescending);
552 }
553
554 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst)
555 {
556 m_actionCollection->action(QStringLiteral("folders_first"))->setChecked(foldersFirst);
557 }
558
559 void DolphinViewActionHandler::slotSortHiddenLastChanged(bool hiddenLast)
560 {
561 m_actionCollection->action(QStringLiteral("hidden_last"))->setChecked(hiddenLast);
562 }
563
564 void DolphinViewActionHandler::toggleVisibleRole(QAction *action)
565 {
566 Q_EMIT actionBeingHandled();
567
568 const QByteArray toggledRole = action->data().toByteArray();
569
570 QList<QByteArray> roles = m_currentView->visibleRoles();
571
572 const bool show = action->isChecked();
573
574 const int index = roles.indexOf(toggledRole);
575 const bool containsInfo = (index >= 0);
576 if (show && !containsInfo) {
577 roles.append(toggledRole);
578 m_currentView->setVisibleRoles(roles);
579 } else if (!show && containsInfo) {
580 roles.removeAt(index);
581 m_currentView->setVisibleRoles(roles);
582 Q_ASSERT(roles.indexOf(toggledRole) < 0);
583 }
584 }
585
586 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList<QByteArray> &current, const QList<QByteArray> &previous)
587 {
588 Q_UNUSED(previous)
589
590 const auto checkedRoles = QSet<QByteArray>(current.constBegin(), current.constEnd());
591 QHashIterator<QByteArray, KToggleAction *> it(m_visibleRoles);
592 while (it.hasNext()) {
593 it.next();
594 const QByteArray &role = it.key();
595 KToggleAction *action = it.value();
596 action->setChecked(checkedRoles.contains(role));
597 }
598 }
599
600 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped)
601 {
602 m_currentView->setGroupedSorting(grouped);
603 }
604
605 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting)
606 {
607 QAction *showInGroupsAction = m_actionCollection->action(QStringLiteral("show_in_groups"));
608 showInGroupsAction->setChecked(groupedSorting);
609 }
610
611 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show)
612 {
613 Q_EMIT actionBeingHandled();
614 m_currentView->setHiddenFilesShown(show);
615 }
616
617 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown)
618 {
619 QAction *showHiddenFilesAction = m_actionCollection->action(QStringLiteral("show_hidden_files"));
620 showHiddenFilesAction->setChecked(shown);
621 }
622
623 void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable)
624 {
625 m_actionCollection->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable && KProtocolManager::supportsMakeDir(currentView()->url()));
626 }
627
628 KToggleAction *DolphinViewActionHandler::iconsModeAction()
629 {
630 KToggleAction *iconsView = m_actionCollection->add<KToggleAction>(QStringLiteral("icons"));
631 iconsView->setText(i18nc("@action:inmenu View Mode", "Icons"));
632 iconsView->setToolTip(i18nc("@info", "Icons view mode"));
633 m_actionCollection->setDefaultShortcut(iconsView, Qt::CTRL | Qt::Key_1);
634 iconsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
635 iconsView->setData(QVariant::fromValue(DolphinView::IconsView));
636 return iconsView;
637 }
638
639 KToggleAction *DolphinViewActionHandler::compactModeAction()
640 {
641 KToggleAction *iconsView = m_actionCollection->add<KToggleAction>(QStringLiteral("compact"));
642 iconsView->setText(i18nc("@action:inmenu View Mode", "Compact"));
643 iconsView->setToolTip(i18nc("@info", "Compact view mode"));
644 m_actionCollection->setDefaultShortcut(iconsView, Qt::CTRL | Qt::Key_2);
645 iconsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
646 iconsView->setData(QVariant::fromValue(DolphinView::CompactView));
647 return iconsView;
648 }
649
650 KToggleAction *DolphinViewActionHandler::detailsModeAction()
651 {
652 KToggleAction *detailsView = m_actionCollection->add<KToggleAction>(QStringLiteral("details"));
653 detailsView->setText(i18nc("@action:inmenu View Mode", "Details"));
654 detailsView->setToolTip(i18nc("@info", "Details view mode"));
655 m_actionCollection->setDefaultShortcut(detailsView, Qt::CTRL | Qt::Key_3);
656 detailsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
657 detailsView->setData(QVariant::fromValue(DolphinView::DetailsView));
658 return detailsView;
659 }
660
661 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray &role)
662 {
663 KToggleAction *action = m_sortByActions.value(role);
664 if (action) {
665 action->setChecked(true);
666
667 if (!action->icon().isNull()) {
668 QAction *sortByMenu = m_actionCollection->action(QStringLiteral("sort"));
669 sortByMenu->setIcon(action->icon());
670 }
671 }
672
673 QAction *descending = m_actionCollection->action(QStringLiteral("descending"));
674 QAction *ascending = m_actionCollection->action(QStringLiteral("ascending"));
675
676 if (role == "text" || role == "type" || role == "extension" || role == "tags" || role == "comment") {
677 descending->setText(i18nc("Sort descending", "Z-A"));
678 ascending->setText(i18nc("Sort ascending", "A-Z"));
679 } else if (role == "size") {
680 descending->setText(i18nc("Sort descending", "Largest First"));
681 ascending->setText(i18nc("Sort ascending", "Smallest First"));
682 } else if (role == "modificationtime" || role == "creationtime" || role == "accesstime") {
683 descending->setText(i18nc("Sort descending", "Newest First"));
684 ascending->setText(i18nc("Sort ascending", "Oldest First"));
685 } else if (role == "rating") {
686 descending->setText(i18nc("Sort descending", "Highest First"));
687 ascending->setText(i18nc("Sort ascending", "Lowest First"));
688 } else {
689 descending->setText(i18nc("Sort descending", "Descending"));
690 ascending->setText(i18nc("Sort ascending", "Ascending"));
691 }
692
693 slotSortOrderChanged(m_currentView->sortOrder());
694 }
695
696 void DolphinViewActionHandler::slotZoomLevelChanged(int current, int previous)
697 {
698 Q_UNUSED(previous)
699
700 QAction *zoomInAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomIn));
701 if (zoomInAction) {
702 zoomInAction->setEnabled(current < ZoomLevelInfo::maximumLevel());
703 }
704
705 QAction *zoomOutAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomOut));
706 if (zoomOutAction) {
707 zoomOutAction->setEnabled(current > ZoomLevelInfo::minimumLevel());
708 }
709 }
710
711 void DolphinViewActionHandler::slotSortTriggered(QAction *action)
712 {
713 // The radiobuttons of the "Sort By"-menu are split between the main-menu
714 // and several sub-menus. Because of this they don't have a common
715 // action-group that assures an exclusive toggle-state between the main-menu
716 // actions and the sub-menu-actions. If an action gets checked, it must
717 // be assured that all other actions get unchecked, except the ascending/
718 // descending actions
719 for (QAction *groupAction : std::as_const(m_sortByActions)) {
720 KActionMenu *actionMenu = qobject_cast<KActionMenu *>(groupAction);
721 if (actionMenu) {
722 const auto actions = actionMenu->menu()->actions();
723 for (QAction *subAction : actions) {
724 subAction->setChecked(false);
725 }
726 } else if (groupAction->actionGroup()) {
727 groupAction->setChecked(false);
728 }
729 }
730 action->setChecked(true);
731
732 // Apply the activated sort-role to the view
733 const QByteArray role = action->data().toByteArray();
734 m_currentView->setSortRole(role);
735 }
736
737 void DolphinViewActionHandler::slotAdjustViewProperties()
738 {
739 Q_EMIT actionBeingHandled();
740 QPointer<ViewPropertiesDialog> dialog = new ViewPropertiesDialog(m_currentView);
741 dialog->exec();
742 delete dialog;
743 }
744
745 void DolphinViewActionHandler::slotDuplicate()
746 {
747 if (m_currentView->selectedItemsCount() == 0) {
748 Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DuplicateContents);
749 } else {
750 Q_EMIT actionBeingHandled();
751 m_currentView->duplicateSelectedItems();
752 Q_EMIT selectionModeChangeTriggered(false);
753 }
754 }
755
756 void DolphinViewActionHandler::slotProperties()
757 {
758 KPropertiesDialog *dialog = nullptr;
759 const KFileItemList list = m_currentView->selectedItems();
760 if (list.isEmpty()) {
761 const QUrl url = m_currentView->url();
762 dialog = new KPropertiesDialog(url, m_currentView);
763 } else {
764 dialog = new KPropertiesDialog(list, m_currentView);
765 }
766
767 dialog->setAttribute(Qt::WA_DeleteOnClose);
768 dialog->show();
769 dialog->raise();
770 dialog->activateWindow();
771 }
772
773 void DolphinViewActionHandler::slotCopyPath()
774 {
775 if (m_currentView->selectedItemsCount() == 0) {
776 Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::CopyLocationContents);
777 } else {
778 m_currentView->copyPathToClipboard();
779 Q_EMIT selectionModeChangeTriggered(false);
780 }
781 }
782
783 void DolphinViewActionHandler::slotSelectionChanged(const KFileItemList &selection)
784 {
785 QString basicActionsMenuText;
786 if (selection.isEmpty()) {
787 basicActionsMenuText = i18nc("@action:inmenu menu with actions like copy, paste, rename. The user's selection is empty when this text is shown.",
788 "Actions for Current View");
789 } else {
790 // clang-format off
791 QFontMetrics fontMetrics = QMenu().fontMetrics();
792 // i18n: @action:inmenu menu with actions like copy, paste, rename.
793 // %1 is a textual representation of the currently selected files or folders. This can be the name of
794 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
795 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
796 // and a fallback will be used.
797 basicActionsMenuText = i18n("Actions for %1", fileItemListToString(selection, fontMetrics.averageCharWidth() * 40, fontMetrics, ItemsState::Selected));
798 // clang-format on
799 }
800
801 if (basicActionsMenuText == QStringLiteral("NULL")) {
802 const KFileItemListProperties properties(selection);
803 basicActionsMenuText = i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
804 "Actions for One Selected Item",
805 "Actions for %1 Selected Items",
806 selection.count());
807 }
808
809 QAction *basicActionsMenu = m_actionCollection->action(QStringLiteral("basic_actions"));
810 basicActionsMenu->setText(basicActionsMenuText);
811
812 // Add or remove contextual actions
813 while (!basicActionsMenu->menu()->actions().constLast()->isSeparator()) {
814 basicActionsMenu->menu()->removeAction(basicActionsMenu->menu()->actions().last());
815 }
816 if (selection.count() == 1) {
817 if (selection.first().isLink()) {
818 basicActionsMenu->menu()->addAction(m_actionCollection->action(QStringLiteral("show_target")));
819 }
820 if (selection.first().isDir()) {
821 basicActionsMenu->menu()->addAction(m_actionCollection->action(QStringLiteral("add_to_places")));
822 }
823 }
824 }
825
826 #include "moc_dolphinviewactionhandler.cpp"