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