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