2 * SPDX-FileCopyrightText: 2008 David Faure <faure@kde.org>
3 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
5 * SPDX-License-Identifier: GPL-2.0-or-later
8 #include "dolphinviewactionhandler.h"
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"
18 #include <Baloo/IndexerConfig>
20 #include <KActionCollection>
21 #include <KActionMenu>
22 #include <KFileItemListProperties>
23 #include <KLocalizedString>
24 #include <KNewFileMenu>
25 #include <KPropertiesDialog>
26 #include <KProtocolManager>
28 #include <QActionGroup>
34 DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection
* collection
, QObject
* parent
) :
36 m_actionCollection(collection
),
37 m_currentView(nullptr),
41 Q_ASSERT(m_actionCollection
);
45 void DolphinViewActionHandler::setCurrentView(DolphinView
* view
)
50 disconnect(m_currentView
, nullptr, this, nullptr);
55 connect(view
, &DolphinView::modeChanged
,
56 this, &DolphinViewActionHandler::updateViewActions
);
57 connect(view
, &DolphinView::previewsShownChanged
,
58 this, &DolphinViewActionHandler::slotPreviewsShownChanged
);
59 connect(view
, &DolphinView::sortOrderChanged
,
60 this, &DolphinViewActionHandler::slotSortOrderChanged
);
61 connect(view
, &DolphinView::sortFoldersFirstChanged
,
62 this, &DolphinViewActionHandler::slotSortFoldersFirstChanged
);
63 connect(view
, &DolphinView::sortHiddenLastChanged
,
64 this, &DolphinViewActionHandler::slotSortHiddenLastChanged
);
65 connect(view
, &DolphinView::visibleRolesChanged
,
66 this, &DolphinViewActionHandler::slotVisibleRolesChanged
);
67 connect(view
, &DolphinView::groupedSortingChanged
,
68 this, &DolphinViewActionHandler::slotGroupedSortingChanged
);
69 connect(view
, &DolphinView::hiddenFilesShownChanged
,
70 this, &DolphinViewActionHandler::slotHiddenFilesShownChanged
);
71 connect(view
, &DolphinView::sortRoleChanged
,
72 this, &DolphinViewActionHandler::slotSortRoleChanged
);
73 connect(view
, &DolphinView::zoomLevelChanged
,
74 this, &DolphinViewActionHandler::slotZoomLevelChanged
);
75 connect(view
, &DolphinView::writeStateChanged
,
76 this, &DolphinViewActionHandler::slotWriteStateChanged
);
77 connect(view
, &DolphinView::selectionModeRequested
,
78 this, [this]() { Q_EMIT
setSelectionMode(true); });
79 connect(view
, &DolphinView::selectionChanged
,
80 this, &DolphinViewActionHandler::slotSelectionChanged
);
81 slotSelectionChanged(m_currentView
->selectedItems());
84 DolphinView
* DolphinViewActionHandler::currentView()
89 void DolphinViewActionHandler::createActions()
91 // This action doesn't appear in the GUI, it's for the shortcut only.
92 // KNewFileMenu takes care of the GUI stuff.
93 QAction
* newDirAction
= m_actionCollection
->addAction(QStringLiteral("create_dir"));
94 newDirAction
->setText(i18nc("@action", "Create Folder..."));
95 m_actionCollection
->setDefaultShortcuts(newDirAction
, KStandardShortcut::createFolder());
96 newDirAction
->setIcon(QIcon::fromTheme(QStringLiteral("folder-new")));
97 newDirAction
->setEnabled(false); // Will be enabled in slotWriteStateChanged(bool) if the current URL is writable
98 connect(newDirAction
, &QAction::triggered
, this, &DolphinViewActionHandler::createDirectoryTriggered
);
102 auto renameAction
= KStandardAction::renameFile(this, &DolphinViewActionHandler::slotRename
, m_actionCollection
);
103 renameAction
->setWhatsThis(xi18nc("@info:whatsthis", "This renames the "
104 "items in your current selection.<nl/>Renaming multiple items "
105 "at once amounts to their new names differing only in a number."));
107 auto trashAction
= KStandardAction::moveToTrash(this, &DolphinViewActionHandler::slotTrashActivated
, m_actionCollection
);
108 auto trashShortcuts
= trashAction
->shortcuts();
109 if (!trashShortcuts
.contains(QKeySequence::Delete
)) {
110 trashShortcuts
.append(QKeySequence::Delete
);
111 m_actionCollection
->setDefaultShortcuts(trashAction
, trashShortcuts
);
113 trashAction
->setWhatsThis(xi18nc("@info:whatsthis", "This moves the "
114 "items in your current selection to the <filename>Trash"
115 "</filename>.<nl/>The trash is a temporary storage where "
116 "items can be deleted from if disk space is needed."));
118 auto deleteAction
= KStandardAction::deleteFile(this, &DolphinViewActionHandler::slotDeleteItems
, m_actionCollection
);
119 auto deleteShortcuts
= deleteAction
->shortcuts();
120 if (!deleteShortcuts
.contains(Qt::SHIFT
| Qt::Key_Delete
)) {
121 deleteShortcuts
.append(Qt::SHIFT
| Qt::Key_Delete
);
122 m_actionCollection
->setDefaultShortcuts(deleteAction
, deleteShortcuts
);
124 deleteAction
->setWhatsThis(xi18nc("@info:whatsthis", "This deletes "
125 "the items in your current selection completely. They can "
126 "not be recovered by normal means."));
128 // This action is useful for being enabled when KStandardAction::MoveToTrash should be
129 // disabled and KStandardAction::DeleteFile is enabled (e.g. non-local files), so that Key_Del
130 // can be used for deleting the file (#76016). It needs to be a separate action
131 // so that the Edit menu isn't affected.
132 QAction
* deleteWithTrashShortcut
= m_actionCollection
->addAction(QStringLiteral("delete_shortcut"));
133 // The descriptive text is just for the shortcuts editor.
134 deleteWithTrashShortcut
->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
135 m_actionCollection
->setDefaultShortcuts(deleteWithTrashShortcut
, KStandardShortcut::moveToTrash());
136 deleteWithTrashShortcut
->setEnabled(false);
137 connect(deleteWithTrashShortcut
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDeleteItems
);
139 QAction
* duplicateAction
= m_actionCollection
->addAction(QStringLiteral("duplicate"));
140 duplicateAction
->setText(i18nc("@action:inmenu File", "Duplicate Here"));
141 duplicateAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-duplicate")));
142 m_actionCollection
->setDefaultShortcut(duplicateAction
, Qt::CTRL
| Qt::Key_D
);
143 duplicateAction
->setEnabled(false);
144 connect(duplicateAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDuplicate
);
146 QAction
*propertiesAction
= m_actionCollection
->addAction( QStringLiteral("properties") );
147 // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
148 propertiesAction
->setText( i18nc("@action:inmenu File", "Properties") );
149 propertiesAction
->setWhatsThis(xi18nc("@info:whatsthis properties",
150 "This shows a complete list of properties of the currently "
151 "selected items in a new window.<nl/>If nothing is selected the "
152 "window will be about the currently viewed folder instead.<nl/>"
153 "You can configure advanced options there like managing "
154 "read- and write-permissions."));
155 propertiesAction
->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
156 m_actionCollection
->setDefaultShortcuts(propertiesAction
, {Qt::ALT
| Qt::Key_Return
, Qt::ALT
| Qt::Key_Enter
});
157 connect(propertiesAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotProperties
);
159 QAction
*copyPathAction
= m_actionCollection
->addAction( QStringLiteral("copy_location") );
160 copyPathAction
->setText(i18nc("@action:incontextmenu", "Copy Location"));
161 copyPathAction
->setWhatsThis(i18nc("@info:whatsthis copy_location",
162 "This will copy the path of the first selected item into the clipboard."
165 copyPathAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy-path")));
166 m_actionCollection
->setDefaultShortcuts(copyPathAction
, {Qt::CTRL
| Qt::ALT
| Qt::Key_C
});
167 connect(copyPathAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotCopyPath
);
169 // This menu makes sure that users who don't know how to open a context menu and haven't
170 // figured out how to enable the menu bar can still perform basic file manipulation.
171 // This only works if they know how to select a file.
172 // The text when nothing is selected at least implies that a selection can /somehow/ be made.
173 // This menu is by default only used in the hamburger menu but created here so users can put
174 // it on their toolbar.
175 KActionMenu
*basicActionsMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("basic_actions"), this);
176 // The text is set later depending on the selection in the currently active view.
177 basicActionsMenu
->setPopupMode(QToolButton::InstantPopup
);
178 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Cut
)));
179 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Copy
)));
180 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Paste
)));
181 basicActionsMenu
->addSeparator();
182 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
183 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
184 basicActionsMenu
->addSeparator();
185 basicActionsMenu
->addAction(m_actionCollection
->action(QStringLiteral("properties")));
186 basicActionsMenu
->addSeparator(); // We add one more separator because we sometimes add contextual
187 // actions in slotSelectionChanged() after the static ones above.
190 KToggleAction
* iconsAction
= iconsModeAction();
191 KToggleAction
* compactAction
= compactModeAction();
192 KToggleAction
* detailsAction
= detailsModeAction();
194 iconsAction
->setWhatsThis(xi18nc("@info:whatsthis Icons view mode",
195 "<para>This switches to a view mode that focuses on the folder "
196 "and file icons. This mode makes it easy to distinguish folders "
197 "from files and to detect items with distinctive <emphasis>"
198 "file types</emphasis>.</para><para> This mode is handy to "
199 "browse through pictures when the <interface>Preview"
200 "</interface> option is enabled.</para>"));
201 compactAction
->setWhatsThis(xi18nc("@info:whatsthis Compact view mode",
202 "<para>This switches to a compact view mode that lists the folders "
203 "and files in columns with the names beside the icons.</para><para>"
204 "This helps to keep the overview in folders with many items.</para>"));
205 detailsAction
->setWhatsThis(xi18nc("@info:whatsthis Details view mode",
206 "<para>This switches to a list view mode that focuses on folder "
207 "and file details.</para><para>Click on a detail in the column "
208 "header to sort the items by it. Click again to sort the other "
209 "way around. To select which details should be displayed click "
210 "the header with the right mouse button.</para><para>You can "
211 "view the contents of a folder without leaving the current "
212 "location by clicking to the left of it. This way you can view "
213 "the contents of multiple folders in the same list.</para>"));
215 KSelectAction
* viewModeActions
= m_actionCollection
->add
<KSelectAction
>(QStringLiteral("view_mode"));
216 viewModeActions
->setText(i18nc("@action:intoolbar", "View Mode"));
217 viewModeActions
->addAction(iconsAction
);
218 viewModeActions
->addAction(compactAction
);
219 viewModeActions
->addAction(detailsAction
);
220 viewModeActions
->setToolBarMode(KSelectAction::MenuMode
);
221 connect(viewModeActions
, &KSelectAction::triggered
, this, &DolphinViewActionHandler::slotViewModeActionTriggered
);
223 QAction
* zoomInAction
= KStandardAction::zoomIn(this,
224 &DolphinViewActionHandler::zoomIn
,
226 zoomInAction
->setWhatsThis(i18nc("@info:whatsthis zoom in", "This increases the icon size."));
228 QAction
* zoomResetAction
= m_actionCollection
->addAction(QStringLiteral("view_zoom_reset"));
229 zoomResetAction
->setText(i18nc("@action:inmenu View", "Reset Zoom Level"));
230 zoomResetAction
->setToolTip(i18n("Zoom To Default"));
231 zoomResetAction
->setWhatsThis(i18nc("@info:whatsthis zoom reset", "This resets the icon size to default."));
232 zoomResetAction
->setIcon(QIcon::fromTheme(QStringLiteral("zoom-original")));
233 m_actionCollection
->setDefaultShortcuts(zoomResetAction
, {Qt::CTRL
| Qt::Key_0
});
234 connect(zoomResetAction
, &QAction::triggered
, this, &DolphinViewActionHandler::zoomReset
);
236 QAction
* zoomOutAction
= KStandardAction::zoomOut(this,
237 &DolphinViewActionHandler::zoomOut
,
239 zoomOutAction
->setWhatsThis(i18nc("@info:whatsthis zoom out", "This reduces the icon size."));
241 KActionMenu
* zoomMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("zoom"));
242 zoomMenu
->setText(i18nc("@action:inmenu menu of zoom actions", "Zoom"));
243 zoomMenu
->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
244 zoomMenu
->setPopupMode(QToolButton::InstantPopup
);
245 zoomMenu
->addAction(zoomInAction
);
246 zoomMenu
->addAction(zoomResetAction
);
247 zoomMenu
->addAction(zoomOutAction
);
249 KToggleAction
* showPreview
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_preview"));
250 showPreview
->setText(i18nc("@action:intoolbar", "Show Previews"));
251 showPreview
->setToolTip(i18nc("@info", "Show preview of files and folders"));
252 showPreview
->setWhatsThis(xi18nc("@info:whatsthis", "When this is "
253 "enabled, the icons are based on the actual file or folder "
254 "contents.<nl/>For example the icons of images become scaled "
255 "down versions of the images."));
256 showPreview
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
257 connect(showPreview
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::togglePreview
);
259 KToggleAction
* sortFoldersFirst
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("folders_first"));
260 sortFoldersFirst
->setText(i18nc("@action:inmenu Sort", "Folders First"));
261 connect(sortFoldersFirst
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleSortFoldersFirst
);
263 KToggleAction
* sortHiddenLast
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("hidden_last"));
264 sortHiddenLast
->setText(i18nc("@action:inmenu Sort", "Hidden Files Last"));
265 connect(sortHiddenLast
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleSortHiddenLast
);
268 QActionGroup
* sortByActionGroup
= createFileItemRolesActionGroup(QStringLiteral("sort_by_"));
270 KActionMenu
* sortByActionMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("sort"));
271 sortByActionMenu
->setIcon(QIcon::fromTheme(QStringLiteral("view-sort")));
272 sortByActionMenu
->setText(i18nc("@action:inmenu View", "Sort By"));
273 sortByActionMenu
->setPopupMode(QToolButton::InstantPopup
);
275 const auto sortByActionGroupActions
= sortByActionGroup
->actions();
276 for (QAction
* action
: sortByActionGroupActions
) {
277 sortByActionMenu
->addAction(action
);
280 sortByActionMenu
->addSeparator();
282 QActionGroup
* group
= new QActionGroup(sortByActionMenu
);
283 group
->setExclusive(true);
285 KToggleAction
* ascendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("ascending"));
286 ascendingAction
->setActionGroup(group
);
287 connect(ascendingAction
, &QAction::triggered
, this, [this] {
288 m_currentView
->setSortOrder(Qt::AscendingOrder
);
291 KToggleAction
* descendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("descending"));
292 descendingAction
->setActionGroup(group
);
293 connect(descendingAction
, &QAction::triggered
, this, [this] {
294 m_currentView
->setSortOrder(Qt::DescendingOrder
);
297 sortByActionMenu
->addAction(ascendingAction
);
298 sortByActionMenu
->addAction(descendingAction
);
299 sortByActionMenu
->addSeparator();
300 sortByActionMenu
->addAction(sortFoldersFirst
);
301 sortByActionMenu
->addAction(sortHiddenLast
);
303 // View -> Additional Information
304 QActionGroup
* visibleRolesGroup
= createFileItemRolesActionGroup(QStringLiteral("show_"));
306 KActionMenu
* visibleRolesMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("additional_info"));
307 visibleRolesMenu
->setText(i18nc("@action:inmenu View", "Show Additional Information"));
308 visibleRolesMenu
->setIcon(QIcon::fromTheme(QStringLiteral("documentinfo")));
309 visibleRolesMenu
->setPopupMode(QToolButton::InstantPopup
);
311 const auto visibleRolesGroupActions
= visibleRolesGroup
->actions();
312 for (QAction
* action
: visibleRolesGroupActions
) {
313 visibleRolesMenu
->addAction(action
);
316 KToggleAction
* showInGroups
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_in_groups"));
317 showInGroups
->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
318 showInGroups
->setText(i18nc("@action:inmenu View", "Show in Groups"));
319 showInGroups
->setWhatsThis(i18nc("@info:whatsthis", "This groups files and folders by their first letter."));
320 connect(showInGroups
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleGroupedSorting
);
322 KToggleAction
* showHiddenFiles
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_hidden_files"));
323 showHiddenFiles
->setIcon(QIcon::fromTheme(QStringLiteral("view-visible")));
324 showHiddenFiles
->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
325 showHiddenFiles
->setWhatsThis(xi18nc("@info:whatsthis", "<para>When "
326 "this is enabled <emphasis>hidden</emphasis> files and folders "
327 "are visible. They will be displayed semi-transparent.</para>"
328 "<para>Hidden items only differ from other ones in that their "
329 "name starts with a \".\". In general there is no need for "
330 "users to access them which is why they are hidden.</para>"));
331 m_actionCollection
->setDefaultShortcuts(showHiddenFiles
, KStandardShortcut::showHideHiddenFiles());
332 connect(showHiddenFiles
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleShowHiddenFiles
);
334 QAction
* adjustViewProps
= m_actionCollection
->addAction(QStringLiteral("view_properties"));
335 adjustViewProps
->setText(i18nc("@action:inmenu View", "Adjust View Display Style..."));
336 adjustViewProps
->setIcon(QIcon::fromTheme(QStringLiteral("view-choose")));
337 adjustViewProps
->setWhatsThis(i18nc("@info:whatsthis", "This opens a window "
338 "in which all folder view properties can be adjusted."));
339 connect(adjustViewProps
, &QAction::triggered
, this, &DolphinViewActionHandler::slotAdjustViewProperties
);
342 QActionGroup
* DolphinViewActionHandler::createFileItemRolesActionGroup(const QString
& groupPrefix
)
344 const bool isSortGroup
= (groupPrefix
== QLatin1String("sort_by_"));
345 Q_ASSERT(isSortGroup
|| groupPrefix
== QLatin1String("show_"));
347 QActionGroup
* rolesActionGroup
= new QActionGroup(m_actionCollection
);
348 rolesActionGroup
->setExclusive(isSortGroup
);
350 connect(rolesActionGroup
, &QActionGroup::triggered
,
351 this, &DolphinViewActionHandler::slotSortTriggered
);
353 connect(rolesActionGroup
, &QActionGroup::triggered
,
354 this, &DolphinViewActionHandler::toggleVisibleRole
);
358 KActionMenu
* groupMenu
= nullptr;
359 QActionGroup
* groupMenuGroup
= nullptr;
361 bool indexingEnabled
= false;
363 Baloo::IndexerConfig config
;
364 indexingEnabled
= config
.fileIndexingEnabled();
367 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
368 for (const KFileItemModel::RoleInfo
& info
: rolesInfo
) {
369 if (!isSortGroup
&& info
.role
== "text") {
370 // It should not be possible to hide the "text" role
374 KToggleAction
* action
= nullptr;
375 const QString name
= groupPrefix
+ info
.role
;
376 if (info
.group
.isEmpty()) {
377 action
= m_actionCollection
->add
<KToggleAction
>(name
);
378 action
->setActionGroup(rolesActionGroup
);
380 if (!groupMenu
|| info
.group
!= groupName
) {
381 groupName
= info
.group
;
382 groupMenu
= m_actionCollection
->add
<KActionMenu
>(groupName
);
383 groupMenu
->setText(groupName
);
384 groupMenu
->setActionGroup(rolesActionGroup
);
386 groupMenuGroup
= new QActionGroup(groupMenu
);
387 groupMenuGroup
->setExclusive(isSortGroup
);
389 connect(groupMenuGroup
, &QActionGroup::triggered
,
390 this, &DolphinViewActionHandler::slotSortTriggered
);
392 connect(groupMenuGroup
, &QActionGroup::triggered
,
393 this, &DolphinViewActionHandler::toggleVisibleRole
);
397 action
= new KToggleAction(groupMenu
);
398 action
->setActionGroup(groupMenuGroup
);
399 groupMenu
->addAction(action
);
401 action
->setText(info
.translation
);
402 action
->setData(info
.role
);
404 const bool enable
= (!info
.requiresBaloo
&& !info
.requiresIndexer
) ||
405 (info
.requiresBaloo
) ||
406 (info
.requiresIndexer
&& indexingEnabled
);
407 action
->setEnabled(enable
);
410 m_sortByActions
.insert(info
.role
, action
);
412 m_visibleRoles
.insert(info
.role
, action
);
416 return rolesActionGroup
;
419 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction
* action
)
421 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
422 m_currentView
->setViewMode(mode
);
424 QAction
* viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
425 viewModeMenu
->setIcon(action
->icon());
428 void DolphinViewActionHandler::slotRename()
430 if (m_currentView
->selectedItemsCount() == 0) {
431 Q_EMIT
setSelectionMode(true, SelectionModeBottomBar::Contents::RenameContents
);
433 Q_EMIT
actionBeingHandled();
434 m_currentView
->renameSelectedItems();
438 void DolphinViewActionHandler::slotTrashActivated()
440 if (m_currentView
->selectedItemsCount() == 0) {
441 Q_EMIT
setSelectionMode(true, SelectionModeBottomBar::Contents::MoveToTrashContents
);
443 Q_EMIT
actionBeingHandled();
444 m_currentView
->trashSelectedItems();
445 Q_EMIT
setSelectionMode(false);
449 void DolphinViewActionHandler::slotDeleteItems()
451 if (m_currentView
->selectedItemsCount() == 0) {
452 Q_EMIT
setSelectionMode(true, SelectionModeBottomBar::Contents::DeleteContents
);
454 Q_EMIT
actionBeingHandled();
455 m_currentView
->deleteSelectedItems();
456 Q_EMIT
setSelectionMode(false);
460 void DolphinViewActionHandler::togglePreview(bool show
)
462 Q_EMIT
actionBeingHandled();
463 m_currentView
->setPreviewsShown(show
);
466 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown
)
469 // It is not enough to update the 'Show Preview' action, also
470 // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
474 QString
DolphinViewActionHandler::currentViewModeActionName() const
476 switch (m_currentView
->viewMode()) {
477 case DolphinView::IconsView
:
478 return QStringLiteral("icons");
479 case DolphinView::DetailsView
:
480 return QStringLiteral("details");
481 case DolphinView::CompactView
:
482 return QStringLiteral("compact");
487 return QString(); // can't happen
490 KActionCollection
* DolphinViewActionHandler::actionCollection()
492 return m_actionCollection
;
495 void DolphinViewActionHandler::updateViewActions()
497 QAction
* viewModeAction
= m_actionCollection
->action(currentViewModeActionName());
498 if (viewModeAction
) {
499 viewModeAction
->setChecked(true);
501 QAction
* viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
502 viewModeMenu
->setIcon(viewModeAction
->icon());
505 QAction
* showPreviewAction
= m_actionCollection
->action(QStringLiteral("show_preview"));
506 showPreviewAction
->setChecked(m_currentView
->previewsShown());
508 slotSortOrderChanged(m_currentView
->sortOrder());
509 slotSortFoldersFirstChanged(m_currentView
->sortFoldersFirst());
510 slotSortHiddenLastChanged(m_currentView
->sortHiddenLast());
511 slotVisibleRolesChanged(m_currentView
->visibleRoles(), QList
<QByteArray
>());
512 slotGroupedSortingChanged(m_currentView
->groupedSorting());
513 slotSortRoleChanged(m_currentView
->sortRole());
514 slotZoomLevelChanged(m_currentView
->zoomLevel(), -1);
516 // Updates the "show_hidden_files" action state and icon
517 slotHiddenFilesShownChanged(m_currentView
->hiddenFilesShown());
520 void DolphinViewActionHandler::zoomIn()
522 const int level
= m_currentView
->zoomLevel();
523 m_currentView
->setZoomLevel(level
+ 1);
527 void DolphinViewActionHandler::zoomOut()
529 const int level
= m_currentView
->zoomLevel();
530 m_currentView
->setZoomLevel(level
- 1);
534 void DolphinViewActionHandler::zoomReset()
536 m_currentView
->resetZoomLevel();
540 void DolphinViewActionHandler::toggleSortFoldersFirst()
542 const bool sortFirst
= m_currentView
->sortFoldersFirst();
543 m_currentView
->setSortFoldersFirst(!sortFirst
);
546 void DolphinViewActionHandler::toggleSortHiddenLast()
548 const bool sortHiddenLast
= m_currentView
->sortHiddenLast();
549 m_currentView
->setSortHiddenLast(!sortHiddenLast
);
552 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order
)
554 QAction
* descending
= m_actionCollection
->action(QStringLiteral("descending"));
555 QAction
* ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
556 const bool sortDescending
= (order
== Qt::DescendingOrder
);
557 descending
->setChecked(sortDescending
);
558 ascending
->setChecked(!sortDescending
);
561 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst
)
563 m_actionCollection
->action(QStringLiteral("folders_first"))->setChecked(foldersFirst
);
566 void DolphinViewActionHandler::slotSortHiddenLastChanged(bool hiddenLast
)
568 m_actionCollection
->action(QStringLiteral("hidden_last"))->setChecked(hiddenLast
);
571 void DolphinViewActionHandler::toggleVisibleRole(QAction
* action
)
573 Q_EMIT
actionBeingHandled();
575 const QByteArray toggledRole
= action
->data().toByteArray();
577 QList
<QByteArray
> roles
= m_currentView
->visibleRoles();
579 const bool show
= action
->isChecked();
581 const int index
= roles
.indexOf(toggledRole
);
582 const bool containsInfo
= (index
>= 0);
583 if (show
&& !containsInfo
) {
584 roles
.append(toggledRole
);
585 m_currentView
->setVisibleRoles(roles
);
586 } else if (!show
&& containsInfo
) {
587 roles
.removeAt(index
);
588 m_currentView
->setVisibleRoles(roles
);
589 Q_ASSERT(roles
.indexOf(toggledRole
) < 0);
593 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList
<QByteArray
>& current
,
594 const QList
<QByteArray
>& previous
)
598 const auto checkedRoles
= QSet
<QByteArray
>(current
.constBegin(), current
.constEnd());
599 QHashIterator
<QByteArray
, KToggleAction
*> it(m_visibleRoles
);
600 while (it
.hasNext()) {
602 const QByteArray
& role
= it
.key();
603 KToggleAction
* action
= it
.value();
604 action
->setChecked(checkedRoles
.contains(role
));
608 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped
)
610 m_currentView
->setGroupedSorting(grouped
);
613 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting
)
615 QAction
* showInGroupsAction
= m_actionCollection
->action(QStringLiteral("show_in_groups"));
616 showInGroupsAction
->setChecked(groupedSorting
);
619 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show
)
621 Q_EMIT
actionBeingHandled();
622 m_currentView
->setHiddenFilesShown(show
);
625 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown
)
627 QAction
* showHiddenFilesAction
= m_actionCollection
->action(QStringLiteral("show_hidden_files"));
628 showHiddenFilesAction
->setChecked(shown
);
631 void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable
)
633 m_actionCollection
->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable
&&
634 KProtocolManager::supportsMakeDir(currentView()->url()));
637 KToggleAction
* DolphinViewActionHandler::iconsModeAction()
639 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("icons"));
640 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
641 iconsView
->setToolTip(i18nc("@info", "Icons view mode"));
642 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_1
);
643 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
644 iconsView
->setData(QVariant::fromValue(DolphinView::IconsView
));
648 KToggleAction
* DolphinViewActionHandler::compactModeAction()
650 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("compact"));
651 iconsView
->setText(i18nc("@action:inmenu View Mode", "Compact"));
652 iconsView
->setToolTip(i18nc("@info", "Compact view mode"));
653 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_2
);
654 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
655 iconsView
->setData(QVariant::fromValue(DolphinView::CompactView
));
659 KToggleAction
* DolphinViewActionHandler::detailsModeAction()
661 KToggleAction
* detailsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("details"));
662 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
663 detailsView
->setToolTip(i18nc("@info", "Details view mode"));
664 m_actionCollection
->setDefaultShortcut(detailsView
, Qt::CTRL
| Qt::Key_3
);
665 detailsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
666 detailsView
->setData(QVariant::fromValue(DolphinView::DetailsView
));
670 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray
& role
)
672 KToggleAction
* action
= m_sortByActions
.value(role
);
674 action
->setChecked(true);
676 if (!action
->icon().isNull()) {
677 QAction
* sortByMenu
= m_actionCollection
->action(QStringLiteral("sort"));
678 sortByMenu
->setIcon(action
->icon());
682 QAction
* descending
= m_actionCollection
->action(QStringLiteral("descending"));
683 QAction
* ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
685 if (role
== "text" || role
== "type" || role
== "extension" || role
== "tags" || role
== "comment") {
686 descending
->setText(i18nc("Sort descending", "Z-A"));
687 ascending
->setText(i18nc("Sort ascending", "A-Z"));
688 } else if (role
== "size") {
689 descending
->setText(i18nc("Sort descending", "Largest First"));
690 ascending
->setText(i18nc("Sort ascending", "Smallest First"));
691 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
692 descending
->setText(i18nc("Sort descending", "Newest First"));
693 ascending
->setText(i18nc("Sort ascending", "Oldest First"));
694 } else if (role
== "rating") {
695 descending
->setText(i18nc("Sort descending", "Highest First"));
696 ascending
->setText(i18nc("Sort ascending", "Lowest First"));
698 descending
->setText(i18nc("Sort descending", "Descending"));
699 ascending
->setText(i18nc("Sort ascending", "Ascending"));
702 slotSortOrderChanged(m_currentView
->sortOrder());
705 void DolphinViewActionHandler::slotZoomLevelChanged(int current
, int previous
)
709 QAction
* zoomInAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomIn
));
711 zoomInAction
->setEnabled(current
< ZoomLevelInfo::maximumLevel());
714 QAction
* zoomOutAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomOut
));
716 zoomOutAction
->setEnabled(current
> ZoomLevelInfo::minimumLevel());
720 void DolphinViewActionHandler::slotSortTriggered(QAction
* action
)
722 // The radiobuttons of the "Sort By"-menu are split between the main-menu
723 // and several sub-menus. Because of this they don't have a common
724 // action-group that assures an exclusive toggle-state between the main-menu
725 // actions and the sub-menu-actions. If an action gets checked, it must
726 // be assured that all other actions get unchecked, except the ascending/
727 // descending actions
728 for (QAction
*groupAction
: qAsConst(m_sortByActions
)) {
729 KActionMenu
* actionMenu
= qobject_cast
<KActionMenu
*>(groupAction
);
731 const auto actions
= actionMenu
->menu()->actions();
732 for (QAction
* subAction
: actions
) {
733 subAction
->setChecked(false);
735 } else if (groupAction
->actionGroup()) {
736 groupAction
->setChecked(false);
739 action
->setChecked(true);
741 // Apply the activated sort-role to the view
742 const QByteArray role
= action
->data().toByteArray();
743 m_currentView
->setSortRole(role
);
746 void DolphinViewActionHandler::slotAdjustViewProperties()
748 Q_EMIT
actionBeingHandled();
749 QPointer
<ViewPropertiesDialog
> dialog
= new ViewPropertiesDialog(m_currentView
);
754 void DolphinViewActionHandler::slotDuplicate()
756 if (m_currentView
->selectedItemsCount() == 0) {
757 Q_EMIT
setSelectionMode(true, SelectionModeBottomBar::Contents::DuplicateContents
);
759 Q_EMIT
actionBeingHandled();
760 m_currentView
->duplicateSelectedItems();
761 Q_EMIT
setSelectionMode(false);
765 void DolphinViewActionHandler::slotProperties()
767 KPropertiesDialog
* dialog
= nullptr;
768 const KFileItemList list
= m_currentView
->selectedItems();
769 if (list
.isEmpty()) {
770 const QUrl url
= m_currentView
->url();
771 dialog
= new KPropertiesDialog(url
, m_currentView
);
773 dialog
= new KPropertiesDialog(list
, m_currentView
);
776 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
779 dialog
->activateWindow();
782 void DolphinViewActionHandler::slotCopyPath()
784 if (m_currentView
->selectedItemsCount() == 0) {
785 Q_EMIT
setSelectionMode(true, SelectionModeBottomBar::Contents::CopyLocationContents
);
787 m_currentView
->copyPathToClipboard();
788 Q_EMIT
setSelectionMode(false);
792 void DolphinViewActionHandler::slotSelectionChanged(const KFileItemList
& selection
)
794 QString basicActionsMenuText
;
795 if (selection
.isEmpty()) {
796 basicActionsMenuText
=
797 i18nc("@action:inmenu menu with actions like copy, paste, rename. The user's selection is empty when this text is shown.",
798 "Actions for Current View");
800 QFontMetrics fontMetrics
= QMenu().fontMetrics();
801 // i18n: @action:inmenu menu with actions like copy, paste, rename.
802 // %1 is a textual representation of the currently selected files or folders. This can be the name of
803 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
804 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
805 // and a fallback will be used.
806 basicActionsMenuText
= i18n("Actions for %1", fileItemListToString(selection
, fontMetrics
.averageCharWidth() * 40, fontMetrics
, ItemsState::Selected
));
809 if (basicActionsMenuText
== QStringLiteral("NULL")) {
810 const KFileItemListProperties
properties(selection
);
811 basicActionsMenuText
=
812 i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
813 "Actions for One Selected Item", "Actions for %1 Selected Items", selection
.count());
816 QAction
*basicActionsMenu
= m_actionCollection
->action(QStringLiteral("basic_actions"));
817 basicActionsMenu
->setText(basicActionsMenuText
);
819 // Add or remove contextual actions
820 while (!basicActionsMenu
->menu()->actions().constLast()->isSeparator()) {
821 basicActionsMenu
->menu()->removeAction(basicActionsMenu
->menu()->actions().last());
823 if (selection
.count() == 1) {
824 if (selection
.first().isLink()) {
825 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("show_target")));
827 if (selection
.first().isDir()) {
828 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("add_to_places")));