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