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