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