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