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