]> 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->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
276 showHiddenFiles->setToolTip(i18nc("@info", "Visibility of hidden files and folders"));
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 // #374508: don't overwrite custom icons.
557 const QString iconName = showHiddenFilesAction->icon().name();
558 if (!iconName.isEmpty() && iconName != QLatin1String("view-visible") && iconName != QLatin1String("view-hidden")) {
559 return;
560 }
561
562 showHiddenFilesAction->setIcon(QIcon::fromTheme(shown ? QStringLiteral("view-visible") : QStringLiteral("view-hidden")));
563 }
564
565 void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable)
566 {
567 m_actionCollection->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable &&
568 KProtocolManager::supportsMakeDir(currentView()->url()));
569 }
570
571 KToggleAction* DolphinViewActionHandler::iconsModeAction()
572 {
573 KToggleAction* iconsView = m_actionCollection->add<KToggleAction>(QStringLiteral("icons"));
574 iconsView->setText(i18nc("@action:inmenu View Mode", "Icons"));
575 iconsView->setToolTip(i18nc("@info", "Icons view mode"));
576 m_actionCollection->setDefaultShortcut(iconsView, Qt::CTRL + Qt::Key_1);
577 iconsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
578 iconsView->setData(QVariant::fromValue(DolphinView::IconsView));
579 return iconsView;
580 }
581
582 KToggleAction* DolphinViewActionHandler::compactModeAction()
583 {
584 KToggleAction* iconsView = m_actionCollection->add<KToggleAction>(QStringLiteral("compact"));
585 iconsView->setText(i18nc("@action:inmenu View Mode", "Compact"));
586 iconsView->setToolTip(i18nc("@info", "Compact view mode"));
587 m_actionCollection->setDefaultShortcut(iconsView, Qt::CTRL + Qt::Key_2);
588 iconsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
589 iconsView->setData(QVariant::fromValue(DolphinView::CompactView));
590 return iconsView;
591 }
592
593 KToggleAction* DolphinViewActionHandler::detailsModeAction()
594 {
595 KToggleAction* detailsView = m_actionCollection->add<KToggleAction>(QStringLiteral("details"));
596 detailsView->setText(i18nc("@action:inmenu View Mode", "Details"));
597 detailsView->setToolTip(i18nc("@info", "Details view mode"));
598 m_actionCollection->setDefaultShortcut(detailsView, Qt::CTRL + Qt::Key_3);
599 detailsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
600 detailsView->setData(QVariant::fromValue(DolphinView::DetailsView));
601 return detailsView;
602 }
603
604 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray& role)
605 {
606 KToggleAction* action = m_sortByActions.value(role);
607 if (action) {
608 action->setChecked(true);
609
610 if (!action->icon().isNull()) {
611 QAction* sortByMenu = m_actionCollection->action(QStringLiteral("sort"));
612 sortByMenu->setIcon(action->icon());
613 }
614 }
615
616 QAction* descending = m_actionCollection->action(QStringLiteral("descending"));
617 QAction* ascending = m_actionCollection->action(QStringLiteral("ascending"));
618
619 if (role == "text" || role == "type" || role == "tags" || role == "comment") {
620 descending->setText(i18nc("Sort descending", "Z-A"));
621 ascending->setText(i18nc("Sort ascending", "A-Z"));
622 } else if (role == "size") {
623 descending->setText(i18nc("Sort descending", "Largest First"));
624 ascending->setText(i18nc("Sort ascending", "Smallest First"));
625 } else if (role == "modificationtime" || role == "creationtime" || role == "accesstime") {
626 descending->setText(i18nc("Sort descending", "Newest First"));
627 ascending->setText(i18nc("Sort ascending", "Oldest First"));
628 } else if (role == "rating") {
629 descending->setText(i18nc("Sort descending", "Highest First"));
630 ascending->setText(i18nc("Sort ascending", "Lowest First"));
631 } else {
632 descending->setText(i18nc("Sort descending", "Descending"));
633 ascending->setText(i18nc("Sort ascending", "Ascending"));
634 }
635
636 slotSortOrderChanged(m_currentView->sortOrder());
637 }
638
639 void DolphinViewActionHandler::slotZoomLevelChanged(int current, int previous)
640 {
641 Q_UNUSED(previous)
642
643 QAction* zoomInAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomIn));
644 if (zoomInAction) {
645 zoomInAction->setEnabled(current < ZoomLevelInfo::maximumLevel());
646 }
647
648 QAction* zoomOutAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomOut));
649 if (zoomOutAction) {
650 zoomOutAction->setEnabled(current > ZoomLevelInfo::minimumLevel());
651 }
652 }
653
654 void DolphinViewActionHandler::slotSortTriggered(QAction* action)
655 {
656 // The radiobuttons of the "Sort By"-menu are split between the main-menu
657 // and several sub-menus. Because of this they don't have a common
658 // action-group that assures an exclusive toggle-state between the main-menu
659 // actions and the sub-menu-actions. If an action gets checked, it must
660 // be assured that all other actions get unchecked, except the ascending/
661 // descending actions
662 for (QAction *groupAction : qAsConst(m_sortByActions)) {
663 KActionMenu* actionMenu = qobject_cast<KActionMenu*>(groupAction);
664 if (actionMenu) {
665 foreach (QAction* subAction, actionMenu->menu()->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 emit actionBeingHandled();
682 QPointer<ViewPropertiesDialog> dialog = new ViewPropertiesDialog(m_currentView);
683 dialog->exec();
684 delete dialog;
685 }
686
687 void DolphinViewActionHandler::slotDuplicate()
688 {
689 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 }