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