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