]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinviewactionhandler.cpp
Merge branch 'release/20.08' into master
[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::SHIFT + 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 foreach (QAction* action, sortByActionGroup->actions()) {
231 sortByActionMenu->addAction(action);
232 }
233
234 sortByActionMenu->addSeparator();
235
236 QActionGroup* group = new QActionGroup(sortByActionMenu);
237 group->setExclusive(true);
238
239 KToggleAction* ascendingAction = m_actionCollection->add<KToggleAction>(QStringLiteral("ascending"));
240 ascendingAction->setActionGroup(group);
241 connect(ascendingAction, &QAction::triggered, this, [this] {
242 m_currentView->setSortOrder(Qt::AscendingOrder);
243 });
244
245 KToggleAction* descendingAction = m_actionCollection->add<KToggleAction>(QStringLiteral("descending"));
246 descendingAction->setActionGroup(group);
247 connect(descendingAction, &QAction::triggered, this, [this] {
248 m_currentView->setSortOrder(Qt::DescendingOrder);
249 });
250
251 sortByActionMenu->addAction(ascendingAction);
252 sortByActionMenu->addAction(descendingAction);
253 sortByActionMenu->addSeparator();
254 sortByActionMenu->addAction(sortFoldersFirst);
255
256 // View -> Additional Information
257 QActionGroup* visibleRolesGroup = createFileItemRolesActionGroup(QStringLiteral("show_"));
258
259 KActionMenu* visibleRolesMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("additional_info"));
260 visibleRolesMenu->setText(i18nc("@action:inmenu View", "Show Additional Information"));
261 visibleRolesMenu->setIcon(QIcon::fromTheme(QStringLiteral("documentinfo")));
262 visibleRolesMenu->setDelayed(false);
263
264 foreach (QAction* action, visibleRolesGroup->actions()) {
265 visibleRolesMenu->addAction(action);
266 }
267
268 KToggleAction* showInGroups = m_actionCollection->add<KToggleAction>(QStringLiteral("show_in_groups"));
269 showInGroups->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
270 showInGroups->setText(i18nc("@action:inmenu View", "Show in Groups"));
271 showInGroups->setWhatsThis(i18nc("@info:whatsthis", "This groups files and folders by their first letter."));
272 connect(showInGroups, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleGroupedSorting);
273
274 KToggleAction* showHiddenFiles = m_actionCollection->add<KToggleAction>(QStringLiteral("show_hidden_files"));
275 showHiddenFiles->setIcon(QIcon::fromTheme(QStringLiteral("view-visible")));
276 showHiddenFiles->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
277 showHiddenFiles->setWhatsThis(xi18nc("@info:whatsthis", "<para>When "
278 "this is enabled <emphasis>hidden</emphasis> files and folders "
279 "are visible. They will be displayed semi-transparent.</para>"
280 "<para>Hidden items only differ from other ones in that their "
281 "name starts with a \".\". In general there is no need for "
282 "users to access them which is why they are hidden.</para>"));
283 m_actionCollection->setDefaultShortcuts(showHiddenFiles, KStandardShortcut::showHideHiddenFiles());
284 connect(showHiddenFiles, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleShowHiddenFiles);
285
286 QAction* adjustViewProps = m_actionCollection->addAction(QStringLiteral("view_properties"));
287 adjustViewProps->setText(i18nc("@action:inmenu View", "Adjust View Display Style..."));
288 adjustViewProps->setIcon(QIcon::fromTheme(QStringLiteral("view-choose")));
289 adjustViewProps->setWhatsThis(i18nc("@info:whatsthis", "This opens a window "
290 "in which all folder view properties can be adjusted."));
291 connect(adjustViewProps, &QAction::triggered, this, &DolphinViewActionHandler::slotAdjustViewProperties);
292 }
293
294 QActionGroup* DolphinViewActionHandler::createFileItemRolesActionGroup(const QString& groupPrefix)
295 {
296 const bool isSortGroup = (groupPrefix == QLatin1String("sort_by_"));
297 Q_ASSERT(isSortGroup || groupPrefix == QLatin1String("show_"));
298
299 QActionGroup* rolesActionGroup = new QActionGroup(m_actionCollection);
300 rolesActionGroup->setExclusive(isSortGroup);
301 if (isSortGroup) {
302 connect(rolesActionGroup, &QActionGroup::triggered,
303 this, &DolphinViewActionHandler::slotSortTriggered);
304 } else {
305 connect(rolesActionGroup, &QActionGroup::triggered,
306 this, &DolphinViewActionHandler::toggleVisibleRole);
307 }
308
309 QString groupName;
310 KActionMenu* groupMenu = nullptr;
311 QActionGroup* groupMenuGroup = nullptr;
312
313 bool indexingEnabled = false;
314 #ifdef HAVE_BALOO
315 Baloo::IndexerConfig config;
316 indexingEnabled = config.fileIndexingEnabled();
317 #endif
318
319 const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
320 foreach (const KFileItemModel::RoleInfo& info, rolesInfo) {
321 if (!isSortGroup && info.role == "text") {
322 // It should not be possible to hide the "text" role
323 continue;
324 }
325
326 KToggleAction* action = nullptr;
327 const QString name = groupPrefix + info.role;
328 if (info.group.isEmpty()) {
329 action = m_actionCollection->add<KToggleAction>(name);
330 action->setActionGroup(rolesActionGroup);
331 } else {
332 if (!groupMenu || info.group != groupName) {
333 groupName = info.group;
334 groupMenu = m_actionCollection->add<KActionMenu>(groupName);
335 groupMenu->setText(groupName);
336 groupMenu->setActionGroup(rolesActionGroup);
337
338 groupMenuGroup = new QActionGroup(groupMenu);
339 groupMenuGroup->setExclusive(isSortGroup);
340 if (isSortGroup) {
341 connect(groupMenuGroup, &QActionGroup::triggered,
342 this, &DolphinViewActionHandler::slotSortTriggered);
343 } else {
344 connect(groupMenuGroup, &QActionGroup::triggered,
345 this, &DolphinViewActionHandler::toggleVisibleRole);
346 }
347 }
348
349 action = new KToggleAction(groupMenu);
350 action->setActionGroup(groupMenuGroup);
351 groupMenu->addAction(action);
352 }
353 action->setText(info.translation);
354 action->setData(info.role);
355
356 const bool enable = (!info.requiresBaloo && !info.requiresIndexer) ||
357 (info.requiresBaloo) ||
358 (info.requiresIndexer && indexingEnabled);
359 action->setEnabled(enable);
360
361 if (isSortGroup) {
362 m_sortByActions.insert(info.role, action);
363 } else {
364 m_visibleRoles.insert(info.role, action);
365 }
366 }
367
368 return rolesActionGroup;
369 }
370
371 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction* action)
372 {
373 const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
374 m_currentView->setMode(mode);
375
376 QAction* viewModeMenu = m_actionCollection->action(QStringLiteral("view_mode"));
377 viewModeMenu->setIcon(action->icon());
378 }
379
380 void DolphinViewActionHandler::slotRename()
381 {
382 emit actionBeingHandled();
383 m_currentView->renameSelectedItems();
384 }
385
386 void DolphinViewActionHandler::slotTrashActivated()
387 {
388 emit actionBeingHandled();
389 m_currentView->trashSelectedItems();
390 }
391
392 void DolphinViewActionHandler::slotDeleteItems()
393 {
394 emit actionBeingHandled();
395 m_currentView->deleteSelectedItems();
396 }
397
398 void DolphinViewActionHandler::togglePreview(bool show)
399 {
400 emit actionBeingHandled();
401 m_currentView->setPreviewsShown(show);
402 }
403
404 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown)
405 {
406 Q_UNUSED(shown)
407 // It is not enough to update the 'Show Preview' action, also
408 // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
409 updateViewActions();
410 }
411
412 QString DolphinViewActionHandler::currentViewModeActionName() const
413 {
414 switch (m_currentView->mode()) {
415 case DolphinView::IconsView:
416 return QStringLiteral("icons");
417 case DolphinView::DetailsView:
418 return QStringLiteral("details");
419 case DolphinView::CompactView:
420 return QStringLiteral("compact");
421 default:
422 Q_ASSERT(false);
423 break;
424 }
425 return QString(); // can't happen
426 }
427
428 KActionCollection* DolphinViewActionHandler::actionCollection()
429 {
430 return m_actionCollection;
431 }
432
433 void DolphinViewActionHandler::updateViewActions()
434 {
435 QAction* viewModeAction = m_actionCollection->action(currentViewModeActionName());
436 if (viewModeAction) {
437 viewModeAction->setChecked(true);
438
439 QAction* viewModeMenu = m_actionCollection->action(QStringLiteral("view_mode"));
440 viewModeMenu->setIcon(viewModeAction->icon());
441 }
442
443 QAction* showPreviewAction = m_actionCollection->action(QStringLiteral("show_preview"));
444 showPreviewAction->setChecked(m_currentView->previewsShown());
445
446 slotSortOrderChanged(m_currentView->sortOrder());
447 slotSortFoldersFirstChanged(m_currentView->sortFoldersFirst());
448 slotVisibleRolesChanged(m_currentView->visibleRoles(), QList<QByteArray>());
449 slotGroupedSortingChanged(m_currentView->groupedSorting());
450 slotSortRoleChanged(m_currentView->sortRole());
451 slotZoomLevelChanged(m_currentView->zoomLevel(), -1);
452
453 // Updates the "show_hidden_files" action state and icon
454 slotHiddenFilesShownChanged(m_currentView->hiddenFilesShown());
455 }
456
457 void DolphinViewActionHandler::zoomIn()
458 {
459 const int level = m_currentView->zoomLevel();
460 m_currentView->setZoomLevel(level + 1);
461 updateViewActions();
462 }
463
464 void DolphinViewActionHandler::zoomOut()
465 {
466 const int level = m_currentView->zoomLevel();
467 m_currentView->setZoomLevel(level - 1);
468 updateViewActions();
469 }
470
471 void DolphinViewActionHandler::zoomReset()
472 {
473 m_currentView->resetZoomLevel();
474 updateViewActions();
475 }
476
477 void DolphinViewActionHandler::toggleSortFoldersFirst()
478 {
479 const bool sortFirst = m_currentView->sortFoldersFirst();
480 m_currentView->setSortFoldersFirst(!sortFirst);
481 }
482
483 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order)
484 {
485 QAction* descending = m_actionCollection->action(QStringLiteral("descending"));
486 QAction* ascending = m_actionCollection->action(QStringLiteral("ascending"));
487 const bool sortDescending = (order == Qt::DescendingOrder);
488 descending->setChecked(sortDescending);
489 ascending->setChecked(!sortDescending);
490 }
491
492 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst)
493 {
494 m_actionCollection->action(QStringLiteral("folders_first"))->setChecked(foldersFirst);
495 }
496
497 void DolphinViewActionHandler::toggleVisibleRole(QAction* action)
498 {
499 emit actionBeingHandled();
500
501 const QByteArray toggledRole = action->data().toByteArray();
502
503 QList<QByteArray> roles = m_currentView->visibleRoles();
504
505 const bool show = action->isChecked();
506
507 const int index = roles.indexOf(toggledRole);
508 const bool containsInfo = (index >= 0);
509 if (show && !containsInfo) {
510 roles.append(toggledRole);
511 m_currentView->setVisibleRoles(roles);
512 } else if (!show && containsInfo) {
513 roles.removeAt(index);
514 m_currentView->setVisibleRoles(roles);
515 Q_ASSERT(roles.indexOf(toggledRole) < 0);
516 }
517 }
518
519 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList<QByteArray>& current,
520 const QList<QByteArray>& previous)
521 {
522 Q_UNUSED(previous)
523
524 const QSet<QByteArray> checkedRoles = current.toSet();
525 QHashIterator<QByteArray, KToggleAction*> it(m_visibleRoles);
526 while (it.hasNext()) {
527 it.next();
528 const QByteArray& role = it.key();
529 KToggleAction* action = it.value();
530 action->setChecked(checkedRoles.contains(role));
531 }
532 }
533
534 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped)
535 {
536 m_currentView->setGroupedSorting(grouped);
537 }
538
539 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting)
540 {
541 QAction* showInGroupsAction = m_actionCollection->action(QStringLiteral("show_in_groups"));
542 showInGroupsAction->setChecked(groupedSorting);
543 }
544
545 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show)
546 {
547 emit actionBeingHandled();
548 m_currentView->setHiddenFilesShown(show);
549 }
550
551 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown)
552 {
553 QAction* showHiddenFilesAction = m_actionCollection->action(QStringLiteral("show_hidden_files"));
554 showHiddenFilesAction->setChecked(shown);
555 }
556
557 void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable)
558 {
559 m_actionCollection->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable &&
560 KProtocolManager::supportsMakeDir(currentView()->url()));
561 }
562
563 KToggleAction* DolphinViewActionHandler::iconsModeAction()
564 {
565 KToggleAction* iconsView = m_actionCollection->add<KToggleAction>(QStringLiteral("icons"));
566 iconsView->setText(i18nc("@action:inmenu View Mode", "Icons"));
567 iconsView->setToolTip(i18nc("@info", "Icons view mode"));
568 m_actionCollection->setDefaultShortcut(iconsView, Qt::CTRL + Qt::Key_1);
569 iconsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
570 iconsView->setData(QVariant::fromValue(DolphinView::IconsView));
571 return iconsView;
572 }
573
574 KToggleAction* DolphinViewActionHandler::compactModeAction()
575 {
576 KToggleAction* iconsView = m_actionCollection->add<KToggleAction>(QStringLiteral("compact"));
577 iconsView->setText(i18nc("@action:inmenu View Mode", "Compact"));
578 iconsView->setToolTip(i18nc("@info", "Compact view mode"));
579 m_actionCollection->setDefaultShortcut(iconsView, Qt::CTRL + Qt::Key_2);
580 iconsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
581 iconsView->setData(QVariant::fromValue(DolphinView::CompactView));
582 return iconsView;
583 }
584
585 KToggleAction* DolphinViewActionHandler::detailsModeAction()
586 {
587 KToggleAction* detailsView = m_actionCollection->add<KToggleAction>(QStringLiteral("details"));
588 detailsView->setText(i18nc("@action:inmenu View Mode", "Details"));
589 detailsView->setToolTip(i18nc("@info", "Details view mode"));
590 m_actionCollection->setDefaultShortcut(detailsView, Qt::CTRL + Qt::Key_3);
591 detailsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
592 detailsView->setData(QVariant::fromValue(DolphinView::DetailsView));
593 return detailsView;
594 }
595
596 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray& role)
597 {
598 KToggleAction* action = m_sortByActions.value(role);
599 if (action) {
600 action->setChecked(true);
601
602 if (!action->icon().isNull()) {
603 QAction* sortByMenu = m_actionCollection->action(QStringLiteral("sort"));
604 sortByMenu->setIcon(action->icon());
605 }
606 }
607
608 QAction* descending = m_actionCollection->action(QStringLiteral("descending"));
609 QAction* ascending = m_actionCollection->action(QStringLiteral("ascending"));
610
611 if (role == "text" || role == "type" || role == "tags" || role == "comment") {
612 descending->setText(i18nc("Sort descending", "Z-A"));
613 ascending->setText(i18nc("Sort ascending", "A-Z"));
614 } else if (role == "size") {
615 descending->setText(i18nc("Sort descending", "Largest First"));
616 ascending->setText(i18nc("Sort ascending", "Smallest First"));
617 } else if (role == "modificationtime" || role == "creationtime" || role == "accesstime") {
618 descending->setText(i18nc("Sort descending", "Newest First"));
619 ascending->setText(i18nc("Sort ascending", "Oldest First"));
620 } else if (role == "rating") {
621 descending->setText(i18nc("Sort descending", "Highest First"));
622 ascending->setText(i18nc("Sort ascending", "Lowest First"));
623 } else {
624 descending->setText(i18nc("Sort descending", "Descending"));
625 ascending->setText(i18nc("Sort ascending", "Ascending"));
626 }
627
628 slotSortOrderChanged(m_currentView->sortOrder());
629 }
630
631 void DolphinViewActionHandler::slotZoomLevelChanged(int current, int previous)
632 {
633 Q_UNUSED(previous)
634
635 QAction* zoomInAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomIn));
636 if (zoomInAction) {
637 zoomInAction->setEnabled(current < ZoomLevelInfo::maximumLevel());
638 }
639
640 QAction* zoomOutAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomOut));
641 if (zoomOutAction) {
642 zoomOutAction->setEnabled(current > ZoomLevelInfo::minimumLevel());
643 }
644 }
645
646 void DolphinViewActionHandler::slotSortTriggered(QAction* action)
647 {
648 // The radiobuttons of the "Sort By"-menu are split between the main-menu
649 // and several sub-menus. Because of this they don't have a common
650 // action-group that assures an exclusive toggle-state between the main-menu
651 // actions and the sub-menu-actions. If an action gets checked, it must
652 // be assured that all other actions get unchecked, except the ascending/
653 // descending actions
654 for (QAction *groupAction : qAsConst(m_sortByActions)) {
655 KActionMenu* actionMenu = qobject_cast<KActionMenu*>(groupAction);
656 if (actionMenu) {
657 foreach (QAction* subAction, actionMenu->menu()->actions()) {
658 subAction->setChecked(false);
659 }
660 } else if (groupAction->actionGroup()) {
661 groupAction->setChecked(false);
662 }
663 }
664 action->setChecked(true);
665
666 // Apply the activated sort-role to the view
667 const QByteArray role = action->data().toByteArray();
668 m_currentView->setSortRole(role);
669 }
670
671 void DolphinViewActionHandler::slotAdjustViewProperties()
672 {
673 emit actionBeingHandled();
674 QPointer<ViewPropertiesDialog> dialog = new ViewPropertiesDialog(m_currentView);
675 dialog->exec();
676 delete dialog;
677 }
678
679 void DolphinViewActionHandler::slotDuplicate()
680 {
681 emit actionBeingHandled();
682 m_currentView->duplicateSelectedItems();
683 }
684
685 void DolphinViewActionHandler::slotProperties()
686 {
687 KPropertiesDialog* dialog = nullptr;
688 const KFileItemList list = m_currentView->selectedItems();
689 if (list.isEmpty()) {
690 const QUrl url = m_currentView->url();
691 dialog = new KPropertiesDialog(url, m_currentView);
692 } else {
693 dialog = new KPropertiesDialog(list, m_currentView);
694 }
695
696 dialog->setAttribute(Qt::WA_DeleteOnClose);
697 dialog->show();
698 dialog->raise();
699 dialog->activateWindow();
700 }
701
702 void DolphinViewActionHandler::slotCopyPath()
703 {
704 m_currentView->copyPathToClipboard();
705 }