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