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