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