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