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