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