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