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