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