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