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