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