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 "kitemviews/kfileitemlisttostring.h"
11 #include "kitemviews/kfileitemmodel.h"
12 #include "selectionmode/actiontexthelper.h"
13 #include "settings/viewpropertiesdialog.h"
14 #include "views/zoomlevelinfo.h"
17 #include <Baloo/IndexerConfig>
19 #include <KActionCollection>
20 #include <KActionMenu>
21 #include <KFileItemListProperties>
22 #include <KLocalizedString>
23 #include <KNewFileMenu>
24 #include <KPropertiesDialog>
25 #include <KProtocolManager>
27 #include <QActionGroup>
31 DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection
*collection
, SelectionMode::ActionTextHelper
*actionTextHelper
, QObject
*parent
)
33 , m_actionCollection(collection
)
34 , m_currentView(nullptr)
39 Q_ASSERT(m_actionCollection
);
40 createActions(actionTextHelper
);
43 void DolphinViewActionHandler::setCurrentView(DolphinView
*view
)
48 disconnect(m_currentView
, nullptr, this, nullptr);
53 connect(view
, &DolphinView::modeChanged
, this, &DolphinViewActionHandler::updateViewActions
);
54 connect(view
, &DolphinView::previewsShownChanged
, this, &DolphinViewActionHandler::slotPreviewsShownChanged
);
55 connect(view
, &DolphinView::sortOrderChanged
, this, &DolphinViewActionHandler::slotSortOrderChanged
);
56 connect(view
, &DolphinView::sortFoldersFirstChanged
, this, &DolphinViewActionHandler::slotSortFoldersFirstChanged
);
57 connect(view
, &DolphinView::sortHiddenLastChanged
, this, &DolphinViewActionHandler::slotSortHiddenLastChanged
);
58 connect(view
, &DolphinView::visibleRolesChanged
, this, &DolphinViewActionHandler::slotVisibleRolesChanged
);
59 connect(view
, &DolphinView::groupedSortingChanged
, this, &DolphinViewActionHandler::slotGroupedSortingChanged
);
60 connect(view
, &DolphinView::hiddenFilesShownChanged
, this, &DolphinViewActionHandler::slotHiddenFilesShownChanged
);
61 connect(view
, &DolphinView::sortRoleChanged
, this, &DolphinViewActionHandler::slotSortRoleChanged
);
62 connect(view
, &DolphinView::groupRoleChanged
, this, &DolphinViewActionHandler::slotGroupRoleChanged
);
63 connect(view
, &DolphinView::groupOrderChanged
, this, &DolphinViewActionHandler::slotGroupOrderChanged
);
64 connect(view
, &DolphinView::zoomLevelChanged
, this, &DolphinViewActionHandler::slotZoomLevelChanged
);
65 connect(view
, &DolphinView::writeStateChanged
, this, &DolphinViewActionHandler::slotWriteStateChanged
);
66 slotWriteStateChanged(view
->isFolderWritable());
67 connect(view
, &DolphinView::selectionModeChangeRequested
, this, [this](bool enabled
) {
68 Q_EMIT
selectionModeChangeTriggered(enabled
);
70 connect(view
, &DolphinView::selectionChanged
, this, &DolphinViewActionHandler::slotSelectionChanged
);
71 slotSelectionChanged(m_currentView
->selectedItems());
74 DolphinView
*DolphinViewActionHandler::currentView()
79 void DolphinViewActionHandler::createActions(SelectionMode::ActionTextHelper
*actionTextHelper
)
81 // This action doesn't appear in the GUI, it's for the shortcut only.
82 // KNewFileMenu takes care of the GUI stuff.
83 QAction
*newDirAction
= m_actionCollection
->addAction(QStringLiteral("create_dir"));
84 newDirAction
->setText(i18nc("@action", "Create Folder…"));
85 m_actionCollection
->setDefaultShortcuts(newDirAction
, KStandardShortcut::createFolder());
86 newDirAction
->setIcon(QIcon::fromTheme(QStringLiteral("folder-new")));
87 newDirAction
->setEnabled(false); // Will be enabled in slotWriteStateChanged(bool) if the current URL is writable
88 connect(newDirAction
, &QAction::triggered
, this, &DolphinViewActionHandler::createDirectoryTriggered
);
92 auto renameAction
= KStandardAction::renameFile(this, &DolphinViewActionHandler::slotRename
, m_actionCollection
);
93 renameAction
->setWhatsThis(xi18nc("@info:whatsthis",
95 "items in your current selection.<nl/>Renaming multiple items "
96 "at once results in their new names differing only in a number."));
98 auto trashAction
= KStandardAction::moveToTrash(this, &DolphinViewActionHandler::slotTrashActivated
, m_actionCollection
);
99 auto trashShortcuts
= trashAction
->shortcuts();
100 trashAction
->setAutoRepeat(false);
101 if (!trashShortcuts
.contains(QKeySequence::Delete
)) {
102 trashShortcuts
.append(QKeySequence::Delete
);
103 m_actionCollection
->setDefaultShortcuts(trashAction
, trashShortcuts
);
105 trashAction
->setWhatsThis(xi18nc("@info:whatsthis",
107 "items in your current selection to the <filename>Trash"
108 "</filename>.<nl/>The trash is a temporary storage location where "
109 "items can be deleted later if disk space is needed."));
111 auto deleteAction
= KStandardAction::deleteFile(this, &DolphinViewActionHandler::slotDeleteItems
, m_actionCollection
);
112 auto deleteShortcuts
= deleteAction
->shortcuts();
113 deleteAction
->setAutoRepeat(false);
114 if (!deleteShortcuts
.contains(Qt::SHIFT
| Qt::Key_Delete
)) {
115 deleteShortcuts
.append(Qt::SHIFT
| Qt::Key_Delete
);
116 m_actionCollection
->setDefaultShortcuts(deleteAction
, deleteShortcuts
);
118 deleteAction
->setWhatsThis(xi18nc("@info:whatsthis",
120 "the items in your current selection permanently. They "
121 "cannot be recovered by normal means."));
123 // This action is useful for being enabled when KStandardAction::MoveToTrash should be
124 // disabled and KStandardAction::DeleteFile is enabled (e.g. non-local files), so that Key_Del
125 // can be used for deleting the file (#76016). It needs to be a separate action
126 // so that the Edit menu isn't affected.
127 QAction
*deleteWithTrashShortcut
= m_actionCollection
->addAction(QStringLiteral("delete_shortcut"));
128 // The descriptive text is just for the shortcuts editor.
129 deleteWithTrashShortcut
->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
130 m_actionCollection
->setDefaultShortcuts(deleteWithTrashShortcut
, KStandardShortcut::moveToTrash());
131 deleteWithTrashShortcut
->setEnabled(false);
132 connect(deleteWithTrashShortcut
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDeleteItems
);
134 QAction
*duplicateAction
= m_actionCollection
->addAction(QStringLiteral("duplicate"));
135 duplicateAction
->setText(i18nc("@action:inmenu File", "Duplicate Here"));
136 duplicateAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-duplicate")));
137 m_actionCollection
->setDefaultShortcut(duplicateAction
, Qt::CTRL
| Qt::Key_D
);
138 duplicateAction
->setEnabled(false);
139 connect(duplicateAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDuplicate
);
141 QAction
*propertiesAction
= m_actionCollection
->addAction(QStringLiteral("properties"));
142 // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
143 propertiesAction
->setText(i18nc("@action:inmenu File", "Properties"));
144 propertiesAction
->setWhatsThis(xi18nc("@info:whatsthis properties",
145 "This shows a complete list of properties of the currently "
146 "selected items in a new window.<nl/>If nothing is selected the "
147 "window will be about the currently viewed folder instead.<nl/>"
148 "You can configure advanced options there like managing "
149 "read- and write-permissions."));
150 propertiesAction
->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
151 m_actionCollection
->setDefaultShortcuts(propertiesAction
, {Qt::ALT
| Qt::Key_Return
, Qt::ALT
| Qt::Key_Enter
});
152 connect(propertiesAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotProperties
);
154 QAction
*copyPathAction
= m_actionCollection
->addAction(QStringLiteral("copy_location"));
155 copyPathAction
->setText(i18nc("@action:incontextmenu", "Copy Location"));
156 copyPathAction
->setWhatsThis(i18nc("@info:whatsthis copy_location", "This will copy the path of the first selected item into the clipboard."));
158 copyPathAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy-path")));
159 m_actionCollection
->setDefaultShortcuts(copyPathAction
, {Qt::CTRL
| Qt::ALT
| Qt::Key_C
});
160 connect(copyPathAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotCopyPath
);
162 if (actionTextHelper
) {
163 // The "…" at the end make clear that they won't trigger their respective actions directly.
164 actionTextHelper
->registerTextWhenNothingIsSelected(trashAction
, i18nc("@action:inmenu File", "Move to Trash…"));
165 actionTextHelper
->registerTextWhenNothingIsSelected(deleteAction
, i18nc("@action:inmenu File", "Delete…"));
166 actionTextHelper
->registerTextWhenNothingIsSelected(duplicateAction
, i18nc("@action:inmenu File", "Duplicate Here…"));
167 actionTextHelper
->registerTextWhenNothingIsSelected(copyPathAction
, i18nc("@action:incontextmenu", "Copy Location…"));
170 // This menu makes sure that users who don't know how to open a context menu and haven't
171 // figured out how to enable the menu bar can still perform basic file manipulation.
172 // This only works if they know how to select a file.
173 // The text when nothing is selected at least implies that a selection can /somehow/ be made.
174 // This menu is by default only used in the hamburger menu but created here so users can put
175 // it on their toolbar.
176 KActionMenu
*basicActionsMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("basic_actions"), this);
177 // The text is set later depending on the selection in the currently active view.
178 basicActionsMenu
->setPopupMode(QToolButton::InstantPopup
);
179 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Cut
)));
180 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Copy
)));
181 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Paste
)));
182 basicActionsMenu
->addSeparator();
183 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
184 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
185 basicActionsMenu
->addSeparator();
186 basicActionsMenu
->addAction(m_actionCollection
->action(QStringLiteral("properties")));
187 basicActionsMenu
->addSeparator(); // We add one more separator because we sometimes add contextual
188 // actions in slotSelectionChanged() after the static ones above.
191 KToggleAction
*iconsAction
= iconsModeAction();
192 KToggleAction
*compactAction
= compactModeAction();
193 KToggleAction
*detailsAction
= detailsModeAction();
195 iconsAction
->setWhatsThis(xi18nc("@info:whatsthis Icons view mode",
196 "<para>This switches to a view mode that focuses on the folder "
197 "and file icons. This mode makes it easy to distinguish folders "
198 "from files and to detect items with distinctive <emphasis>"
199 "file types</emphasis>.</para><para> This mode is handy to "
200 "browse through pictures when the <interface>Preview"
201 "</interface> option is enabled.</para>"));
202 compactAction
->setWhatsThis(xi18nc("@info:whatsthis Compact view mode",
203 "<para>This switches to a compact view mode that lists the folders "
204 "and files in columns with the names beside the icons.</para><para>"
205 "This helps to give you an overview in folders with many items.</para>"));
206 detailsAction
->setWhatsThis(xi18nc("@info:whatsthis Details view mode",
207 "<para>This switches to a list view mode that focuses on folder "
208 "and file details.</para><para>Click on a detail in the column "
209 "header to sort the items by it. Click again to sort the other "
210 "way around. To select which details should be displayed click "
211 "the header with the right mouse button.</para><para>You can "
212 "view the contents of a folder without leaving the current "
213 "location by clicking the region to the left of it. This way you can "
214 "view the contents of multiple folders in the same list.</para>"));
216 KSelectAction
*viewModeActions
= m_actionCollection
->add
<KSelectAction
>(QStringLiteral("view_mode"));
217 viewModeActions
->setText(i18nc("@action:intoolbar", "View Mode"));
218 viewModeActions
->addAction(iconsAction
);
219 viewModeActions
->addAction(compactAction
);
220 viewModeActions
->addAction(detailsAction
);
221 viewModeActions
->setToolBarMode(KSelectAction::MenuMode
);
222 connect(viewModeActions
, &KSelectAction::actionTriggered
, this, &DolphinViewActionHandler::slotViewModeActionTriggered
);
224 QAction
*zoomInAction
= KStandardAction::zoomIn(this, &DolphinViewActionHandler::zoomIn
, m_actionCollection
);
225 zoomInAction
->setWhatsThis(i18nc("@info:whatsthis zoom in", "This increases the icon size."));
227 QAction
*zoomResetAction
= m_actionCollection
->addAction(QStringLiteral("view_zoom_reset"));
228 zoomResetAction
->setText(i18nc("@action:inmenu View", "Reset Zoom Level"));
229 zoomResetAction
->setToolTip(i18n("Zoom To Default"));
230 zoomResetAction
->setWhatsThis(i18nc("@info:whatsthis zoom reset", "This resets the icon size to default."));
231 zoomResetAction
->setIcon(QIcon::fromTheme(QStringLiteral("zoom-original")));
232 m_actionCollection
->setDefaultShortcuts(zoomResetAction
, {Qt::CTRL
| Qt::Key_0
});
233 connect(zoomResetAction
, &QAction::triggered
, this, &DolphinViewActionHandler::zoomReset
);
235 QAction
*zoomOutAction
= KStandardAction::zoomOut(this, &DolphinViewActionHandler::zoomOut
, m_actionCollection
);
236 zoomOutAction
->setWhatsThis(i18nc("@info:whatsthis zoom out", "This reduces the icon size."));
238 KActionMenu
*zoomMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("zoom"));
239 zoomMenu
->setText(i18nc("@action:inmenu menu of zoom actions", "Zoom"));
240 zoomMenu
->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
241 zoomMenu
->setPopupMode(QToolButton::InstantPopup
);
242 zoomMenu
->addAction(zoomInAction
);
243 zoomMenu
->addAction(zoomResetAction
);
244 zoomMenu
->addAction(zoomOutAction
);
246 KToggleAction
*showPreview
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_preview"));
247 showPreview
->setText(i18nc("@action:intoolbar", "Show Previews"));
248 showPreview
->setToolTip(i18nc("@info", "Show preview of files and folders"));
249 showPreview
->setWhatsThis(xi18nc("@info:whatsthis",
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 m_actionCollection
->setDefaultShortcut(showPreview
, QKeySequence(Qt::Key_F12
));
256 connect(showPreview
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::togglePreview
);
258 KToggleAction
*sortFoldersFirst
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("folders_first"));
259 sortFoldersFirst
->setText(i18nc("@action:inmenu Sort", "Folders First"));
260 connect(sortFoldersFirst
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleSortFoldersFirst
);
262 KToggleAction
*sortHiddenLast
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("hidden_last"));
263 sortHiddenLast
->setText(i18nc("@action:inmenu Sort", "Hidden Files Last"));
264 connect(sortHiddenLast
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleSortHiddenLast
);
267 QActionGroup
*sortByActionGroup
= createFileItemRolesActionGroup(QStringLiteral("sort_by_"));
269 KActionMenu
*sortByActionMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("sort"));
270 sortByActionMenu
->setIcon(QIcon::fromTheme(QStringLiteral("view-sort")));
271 sortByActionMenu
->setText(i18nc("@action:inmenu View", "Sort By"));
272 sortByActionMenu
->setPopupMode(QToolButton::InstantPopup
);
274 const auto sortByActionGroupActions
= sortByActionGroup
->actions();
275 for (QAction
*action
: sortByActionGroupActions
) {
276 sortByActionMenu
->addAction(action
);
279 sortByActionMenu
->addSeparator();
281 QActionGroup
*groupForSort
= new QActionGroup(sortByActionMenu
);
282 groupForSort
->setExclusive(true);
284 KToggleAction
*sortAscendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("sort_ascending"));
285 sortAscendingAction
->setActionGroup(groupForSort
);
286 connect(sortAscendingAction
, &QAction::triggered
, this, [this] {
287 m_currentView
->setSortOrder(Qt::AscendingOrder
);
290 KToggleAction
*sortDescendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("sort_descending"));
291 sortDescendingAction
->setActionGroup(groupForSort
);
292 connect(sortDescendingAction
, &QAction::triggered
, this, [this] {
293 m_currentView
->setSortOrder(Qt::DescendingOrder
);
296 sortByActionMenu
->addAction(sortAscendingAction
);
297 sortByActionMenu
->addAction(sortDescendingAction
);
298 sortByActionMenu
->addSeparator();
299 sortByActionMenu
->addAction(sortFoldersFirst
);
300 sortByActionMenu
->addAction(sortHiddenLast
);
303 QActionGroup
*groupByActionGroup
= createFileItemRolesActionGroup(QStringLiteral("group_by_"));
305 KToggleAction
*groupAsNone
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("group_none"));
306 groupAsNone
->setData("none");
307 groupAsNone
->setActionGroup(groupByActionGroup
);
308 groupAsNone
->setText(i18nc("@label", "No grouping"));
309 m_groupByActions
.insert("none", groupAsNone
);
311 KToggleAction
*groupAsFollowSort
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("group_followSort"));
312 groupAsFollowSort
->setData("followSort");
313 groupAsFollowSort
->setActionGroup(groupByActionGroup
);
314 groupAsFollowSort
->setText(i18nc("@label", "Follow sorting"));
315 m_groupByActions
.insert("followSort", groupAsFollowSort
);
317 KActionMenu
*groupByActionMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("group"));
318 groupByActionMenu
->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
319 groupByActionMenu
->setText(i18nc("@action:inmenu View", "Group By"));
320 groupByActionMenu
->setPopupMode(QToolButton::InstantPopup
);
322 const auto groupByActionGroupActions
= groupByActionGroup
->actions();
323 for (QAction
*action
: groupByActionGroupActions
) {
324 groupByActionMenu
->addAction(action
);
327 groupByActionMenu
->addSeparator();
329 QActionGroup
*groupForGroup
= new QActionGroup(groupByActionMenu
);
330 groupForGroup
->setExclusive(true);
332 KToggleAction
*groupAscendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("group_ascending"));
333 groupAscendingAction
->setActionGroup(groupForGroup
);
334 connect(groupAscendingAction
, &QAction::triggered
, this, [this] {
335 m_currentView
->setGroupOrder(Qt::AscendingOrder
);
338 KToggleAction
*groupDescendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("group_descending"));
339 groupDescendingAction
->setActionGroup(groupForGroup
);
340 connect(groupDescendingAction
, &QAction::triggered
, this, [this] {
341 m_currentView
->setGroupOrder(Qt::DescendingOrder
);
344 groupByActionMenu
->addAction(groupAscendingAction
);
345 groupByActionMenu
->addAction(groupDescendingAction
);
347 // View -> Additional Information
348 QActionGroup
*visibleRolesGroup
= createFileItemRolesActionGroup(QStringLiteral("show_"));
350 KActionMenu
*visibleRolesMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("additional_info"));
351 visibleRolesMenu
->setText(i18nc("@action:inmenu View", "Show Additional Information"));
352 visibleRolesMenu
->setIcon(QIcon::fromTheme(QStringLiteral("documentinfo")));
353 visibleRolesMenu
->setPopupMode(QToolButton::InstantPopup
);
355 const auto visibleRolesGroupActions
= visibleRolesGroup
->actions();
356 for (QAction
*action
: visibleRolesGroupActions
) {
357 visibleRolesMenu
->addAction(action
);
360 KToggleAction
*showInGroups
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_in_groups"));
361 showInGroups
->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
362 showInGroups
->setText(i18nc("@action:inmenu View", "Show in Groups"));
363 showInGroups
->setWhatsThis(i18nc("@info:whatsthis", "This groups files and folders by their first letter."));
364 connect(showInGroups
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleGroupedSorting
);
366 KToggleAction
*showHiddenFiles
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_hidden_files"));
367 showHiddenFiles
->setIcon(QIcon::fromTheme(QStringLiteral("view-visible")));
368 showHiddenFiles
->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
369 showHiddenFiles
->setWhatsThis(xi18nc("@info:whatsthis",
371 "this is enabled, <emphasis>hidden</emphasis> files and folders "
372 "are visible. They will be displayed semi-transparent.</para>"
373 "<para>Hidden items only differ from normal ones in that their "
374 "name starts with a dot (\".\"). Typically, there is no need for "
375 "users to access them, which is why they are hidden.</para>"
376 "<para>Items can also be hidden if their names are listed in a text file "
377 "named \".hidden\". Files with the \"application/x-trash\" MIME type, "
378 "such as backup files, can also be hidden by enabling that setting in "
379 "Configure Dolphin > View > General.</para>"));
380 m_actionCollection
->setDefaultShortcuts(showHiddenFiles
, KStandardShortcut::showHideHiddenFiles());
381 connect(showHiddenFiles
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleShowHiddenFiles
);
383 QAction
*adjustViewProps
= m_actionCollection
->addAction(QStringLiteral("view_properties"));
384 adjustViewProps
->setText(i18nc("@action:inmenu View", "Adjust View Display Style…"));
385 adjustViewProps
->setIcon(QIcon::fromTheme(QStringLiteral("view-choose")));
386 adjustViewProps
->setWhatsThis(i18nc("@info:whatsthis",
387 "This opens a window "
388 "in which all folder view properties can be adjusted."));
389 connect(adjustViewProps
, &QAction::triggered
, this, &DolphinViewActionHandler::slotAdjustViewProperties
);
392 QActionGroup
*DolphinViewActionHandler::createFileItemRolesActionGroup(const QString
&groupPrefix
)
394 const bool isSortGroup
= (groupPrefix
== QLatin1String("sort_by_"));
395 const bool isGroupGroup
= (groupPrefix
== QLatin1String("group_by_"));
396 Q_ASSERT(isSortGroup
|| isGroupGroup
|| groupPrefix
== QLatin1String("show_"));
398 QActionGroup
*rolesActionGroup
= new QActionGroup(m_actionCollection
);
399 rolesActionGroup
->setExclusive(isSortGroup
|| isGroupGroup
);
401 connect(rolesActionGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::slotSortTriggered
);
402 } else if (isGroupGroup
) {
403 connect(rolesActionGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::slotGroupTriggered
);
405 connect(rolesActionGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::toggleVisibleRole
);
409 KActionMenu
*groupMenu
= nullptr;
410 QActionGroup
*groupMenuGroup
= nullptr;
412 bool indexingEnabled
= false;
414 Baloo::IndexerConfig config
;
415 indexingEnabled
= config
.fileIndexingEnabled();
418 QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
420 for (const KFileItemModel::RoleInfo
&info
: rolesInfo
) {
421 if (!isSortGroup
&& !isGroupGroup
&& info
.role
== "text") {
422 // It should not be possible to hide the "text" role
426 KToggleAction
*action
= nullptr;
427 const QString name
= groupPrefix
+ info
.role
;
428 if (info
.group
.isEmpty()) {
429 action
= m_actionCollection
->add
<KToggleAction
>(name
);
430 action
->setActionGroup(rolesActionGroup
);
432 if (!groupMenu
|| info
.group
!= groupName
) {
433 groupName
= info
.group
;
434 groupMenu
= m_actionCollection
->add
<KActionMenu
>(groupName
);
435 groupMenu
->setText(groupName
);
436 groupMenu
->setActionGroup(rolesActionGroup
);
438 groupMenuGroup
= new QActionGroup(groupMenu
);
439 groupMenuGroup
->setExclusive(isSortGroup
|| isGroupGroup
);
441 connect(groupMenuGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::slotSortTriggered
);
442 } else if (isGroupGroup
) {
443 connect(groupMenuGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::slotGroupTriggered
);
445 connect(groupMenuGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::toggleVisibleRole
);
449 action
= new KToggleAction(groupMenu
);
450 action
->setActionGroup(groupMenuGroup
);
451 groupMenu
->addAction(action
);
453 action
->setText(info
.translation
);
454 action
->setData(info
.role
);
456 const bool enable
= (!info
.requiresBaloo
&& !info
.requiresIndexer
) || (info
.requiresBaloo
) || (info
.requiresIndexer
&& indexingEnabled
);
457 action
->setEnabled(enable
);
460 m_sortByActions
.insert(info
.role
, action
);
461 } else if (isGroupGroup
) {
462 m_groupByActions
.insert(info
.role
, action
);
464 m_visibleRoles
.insert(info
.role
, action
);
468 return rolesActionGroup
;
471 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction
*action
)
473 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
474 m_currentView
->setViewMode(mode
);
476 QAction
*viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
477 viewModeMenu
->setIcon(action
->icon());
480 void DolphinViewActionHandler::slotRename()
482 if (m_currentView
->selectedItemsCount() == 0) {
483 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::RenameContents
);
485 Q_EMIT
actionBeingHandled();
486 m_currentView
->renameSelectedItems();
487 // We don't exit selectionMode here because users might want to rename more items.
491 void DolphinViewActionHandler::slotTrashActivated()
493 if (m_currentView
->selectedItemsCount() == 0) {
494 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::MoveToTrashContents
);
496 Q_EMIT
actionBeingHandled();
497 m_currentView
->trashSelectedItems();
498 Q_EMIT
selectionModeChangeTriggered(false);
502 void DolphinViewActionHandler::slotDeleteItems()
504 if (m_currentView
->selectedItemsCount() == 0) {
505 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DeleteContents
);
507 Q_EMIT
actionBeingHandled();
508 m_currentView
->deleteSelectedItems();
509 Q_EMIT
selectionModeChangeTriggered(false);
513 void DolphinViewActionHandler::togglePreview(bool show
)
515 Q_EMIT
actionBeingHandled();
516 m_currentView
->setPreviewsShown(show
);
519 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown
)
522 // It is not enough to update the 'Show Preview' action, also
523 // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
527 QString
DolphinViewActionHandler::currentViewModeActionName() const
529 switch (m_currentView
->viewMode()) {
530 case DolphinView::IconsView
:
531 return QStringLiteral("icons");
532 case DolphinView::DetailsView
:
533 return QStringLiteral("details");
534 case DolphinView::CompactView
:
535 return QStringLiteral("compact");
540 return QString(); // can't happen
543 KActionCollection
*DolphinViewActionHandler::actionCollection()
545 return m_actionCollection
;
548 void DolphinViewActionHandler::updateViewActions()
550 QAction
*viewModeAction
= m_actionCollection
->action(currentViewModeActionName());
551 if (viewModeAction
) {
552 viewModeAction
->setChecked(true);
554 QAction
*viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
555 viewModeMenu
->setIcon(viewModeAction
->icon());
558 QAction
*showPreviewAction
= m_actionCollection
->action(QStringLiteral("show_preview"));
559 showPreviewAction
->setChecked(m_currentView
->previewsShown());
561 slotSortOrderChanged(m_currentView
->sortOrder());
562 slotSortFoldersFirstChanged(m_currentView
->sortFoldersFirst());
563 slotSortHiddenLastChanged(m_currentView
->sortHiddenLast());
564 slotVisibleRolesChanged(m_currentView
->visibleRoles(), QList
<QByteArray
>());
565 slotGroupedSortingChanged(m_currentView
->groupedSorting());
566 slotSortRoleChanged(m_currentView
->sortRole());
567 slotGroupRoleChanged(m_currentView
->groupRole());
568 slotGroupOrderChanged(m_currentView
->groupOrder());
569 slotZoomLevelChanged(m_currentView
->zoomLevel(), -1);
571 // Updates the "show_hidden_files" action state and icon
572 slotHiddenFilesShownChanged(m_currentView
->hiddenFilesShown());
575 void DolphinViewActionHandler::zoomIn()
577 const int level
= m_currentView
->zoomLevel();
578 m_currentView
->setZoomLevel(level
+ 1);
582 void DolphinViewActionHandler::zoomOut()
584 const int level
= m_currentView
->zoomLevel();
585 m_currentView
->setZoomLevel(level
- 1);
589 void DolphinViewActionHandler::zoomReset()
591 m_currentView
->resetZoomLevel();
595 void DolphinViewActionHandler::toggleSortFoldersFirst()
597 const bool sortFirst
= m_currentView
->sortFoldersFirst();
598 m_currentView
->setSortFoldersFirst(!sortFirst
);
601 void DolphinViewActionHandler::toggleSortHiddenLast()
603 const bool sortHiddenLast
= m_currentView
->sortHiddenLast();
604 m_currentView
->setSortHiddenLast(!sortHiddenLast
);
607 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order
)
609 QAction
*descending
= m_actionCollection
->action(QStringLiteral("sort_descending"));
610 QAction
*ascending
= m_actionCollection
->action(QStringLiteral("sort_ascending"));
611 const bool sortDescending
= (order
== Qt::DescendingOrder
);
612 descending
->setChecked(sortDescending
);
613 ascending
->setChecked(!sortDescending
);
616 void DolphinViewActionHandler::slotGroupOrderChanged(Qt::SortOrder order
)
618 QAction
*descending
= m_actionCollection
->action(QStringLiteral("group_descending"));
619 QAction
*ascending
= m_actionCollection
->action(QStringLiteral("group_ascending"));
620 const bool groupDescending
= (order
== Qt::DescendingOrder
);
621 descending
->setChecked(groupDescending
);
622 ascending
->setChecked(!groupDescending
);
625 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst
)
627 m_actionCollection
->action(QStringLiteral("folders_first"))->setChecked(foldersFirst
);
630 void DolphinViewActionHandler::slotSortHiddenLastChanged(bool hiddenLast
)
632 m_actionCollection
->action(QStringLiteral("hidden_last"))->setChecked(hiddenLast
);
635 void DolphinViewActionHandler::toggleVisibleRole(QAction
*action
)
637 Q_EMIT
actionBeingHandled();
639 const QByteArray toggledRole
= action
->data().toByteArray();
641 QList
<QByteArray
> roles
= m_currentView
->visibleRoles();
643 const bool show
= action
->isChecked();
645 const int index
= roles
.indexOf(toggledRole
);
646 const bool containsInfo
= (index
>= 0);
647 if (show
&& !containsInfo
) {
648 roles
.append(toggledRole
);
649 m_currentView
->setVisibleRoles(roles
);
650 } else if (!show
&& containsInfo
) {
651 roles
.removeAt(index
);
652 m_currentView
->setVisibleRoles(roles
);
653 Q_ASSERT(roles
.indexOf(toggledRole
) < 0);
657 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList
<QByteArray
> ¤t
, const QList
<QByteArray
> &previous
)
661 const auto checkedRoles
= QSet
<QByteArray
>(current
.constBegin(), current
.constEnd());
662 QHashIterator
<QByteArray
, KToggleAction
*> it(m_visibleRoles
);
663 while (it
.hasNext()) {
665 const QByteArray
&role
= it
.key();
666 KToggleAction
*action
= it
.value();
667 action
->setChecked(checkedRoles
.contains(role
));
671 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped
)
673 m_currentView
->setGroupedSorting(grouped
);
676 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting
)
678 QAction
*showInGroupsAction
= m_actionCollection
->action(QStringLiteral("show_in_groups"));
679 showInGroupsAction
->setChecked(groupedSorting
);
682 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show
)
684 Q_EMIT
actionBeingHandled();
685 m_currentView
->setHiddenFilesShown(show
);
688 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown
)
690 QAction
*showHiddenFilesAction
= m_actionCollection
->action(QStringLiteral("show_hidden_files"));
691 showHiddenFilesAction
->setChecked(shown
);
694 void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable
)
696 m_actionCollection
->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable
&& KProtocolManager::supportsMakeDir(currentView()->url()));
699 KToggleAction
*DolphinViewActionHandler::iconsModeAction()
701 KToggleAction
*iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("icons"));
702 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
703 iconsView
->setToolTip(i18nc("@info", "Icons view mode"));
704 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_1
);
705 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
706 iconsView
->setData(QVariant::fromValue(DolphinView::IconsView
));
710 KToggleAction
*DolphinViewActionHandler::compactModeAction()
712 KToggleAction
*iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("compact"));
713 iconsView
->setText(i18nc("@action:inmenu View Mode", "Compact"));
714 iconsView
->setToolTip(i18nc("@info", "Compact view mode"));
715 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_2
);
716 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
717 iconsView
->setData(QVariant::fromValue(DolphinView::CompactView
));
721 KToggleAction
*DolphinViewActionHandler::detailsModeAction()
723 KToggleAction
*detailsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("details"));
724 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
725 detailsView
->setToolTip(i18nc("@info", "Details view mode"));
726 m_actionCollection
->setDefaultShortcut(detailsView
, Qt::CTRL
| Qt::Key_3
);
727 detailsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
728 detailsView
->setData(QVariant::fromValue(DolphinView::DetailsView
));
732 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray
&role
)
734 KToggleAction
*action
= m_sortByActions
.value(role
);
736 action
->setChecked(true);
738 if (!action
->icon().isNull()) {
739 QAction
*sortByMenu
= m_actionCollection
->action(QStringLiteral("sort"));
740 sortByMenu
->setIcon(action
->icon());
744 QAction
*descending
= m_actionCollection
->action(QStringLiteral("sort_descending"));
745 QAction
*ascending
= m_actionCollection
->action(QStringLiteral("sort_ascending"));
747 if (role
== "text" || role
== "type" || role
== "extension" || role
== "tags" || role
== "comment") {
748 descending
->setText(i18nc("Sort descending", "Z-A"));
749 ascending
->setText(i18nc("Sort ascending", "A-Z"));
750 } else if (role
== "size") {
751 descending
->setText(i18nc("Sort descending", "Largest First"));
752 ascending
->setText(i18nc("Sort ascending", "Smallest First"));
753 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
754 descending
->setText(i18nc("Sort descending", "Newest First"));
755 ascending
->setText(i18nc("Sort ascending", "Oldest First"));
756 } else if (role
== "rating") {
757 descending
->setText(i18nc("Sort descending", "Highest First"));
758 ascending
->setText(i18nc("Sort ascending", "Lowest First"));
760 descending
->setText(i18nc("Sort descending", "Descending"));
761 ascending
->setText(i18nc("Sort ascending", "Ascending"));
764 slotSortOrderChanged(m_currentView
->sortOrder());
767 void DolphinViewActionHandler::slotGroupRoleChanged(const QByteArray
&role
)
769 KToggleAction
*action
= m_groupByActions
.value(role
);
771 action
->setChecked(true);
773 if (!action
->icon().isNull()) {
774 QAction
*groupByMenu
= m_actionCollection
->action(QStringLiteral("group"));
775 groupByMenu
->setIcon(action
->icon());
779 QAction
*descending
= m_actionCollection
->action(QStringLiteral("group_descending"));
780 QAction
*ascending
= m_actionCollection
->action(QStringLiteral("group_ascending"));
782 if (role
== "text" || role
== "type" || role
== "extension" || role
== "tags" || role
== "comment") {
783 descending
->setText(i18nc("Group descending", "Z-A"));
784 ascending
->setText(i18nc("Group ascending", "A-Z"));
785 } else if (role
== "size") {
786 descending
->setText(i18nc("Group descending", "Largest First"));
787 ascending
->setText(i18nc("Group ascending", "Smallest First"));
788 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
789 descending
->setText(i18nc("Group descending", "Newest First"));
790 ascending
->setText(i18nc("Group ascending", "Oldest First"));
791 } else if (role
== "rating") {
792 descending
->setText(i18nc("Group descending", "Highest First"));
793 ascending
->setText(i18nc("Group ascending", "Lowest First"));
795 descending
->setText(i18nc("Group descending", "Descending"));
796 ascending
->setText(i18nc("Group ascending", "Ascending"));
799 slotGroupOrderChanged(m_currentView
->groupOrder());
802 void DolphinViewActionHandler::slotZoomLevelChanged(int current
, int previous
)
806 QAction
*zoomInAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomIn
));
808 zoomInAction
->setEnabled(current
< ZoomLevelInfo::maximumLevel());
811 QAction
*zoomOutAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomOut
));
813 zoomOutAction
->setEnabled(current
> ZoomLevelInfo::minimumLevel());
817 void DolphinViewActionHandler::slotSortTriggered(QAction
*action
)
819 // The radiobuttons of the "Sort By"-menu are split between the main-menu
820 // and several sub-menus. Because of this they don't have a common
821 // action-group that assures an exclusive toggle-state between the main-menu
822 // actions and the sub-menu-actions. If an action gets checked, it must
823 // be assured that all other actions get unchecked, except the ascending/
824 // descending actions
825 for (QAction
*groupAction
: std::as_const(m_sortByActions
)) {
826 KActionMenu
*actionMenu
= qobject_cast
<KActionMenu
*>(groupAction
);
828 const auto actions
= actionMenu
->menu()->actions();
829 for (QAction
*subAction
: actions
) {
830 subAction
->setChecked(false);
832 } else if (groupAction
->actionGroup()) {
833 groupAction
->setChecked(false);
836 action
->setChecked(true);
838 // Apply the activated sort-role to the view
839 const QByteArray role
= action
->data().toByteArray();
840 m_currentView
->setSortRole(role
);
843 void DolphinViewActionHandler::slotGroupTriggered(QAction
*action
)
845 // The radiobuttons of the "Group By"-menu are split between the main-menu
846 // and several sub-menus. Because of this they don't have a common
847 // action-group that assures an exclusive toggle-state between the main-menu
848 // actions and the sub-menu-actions. If an action gets checked, it must
849 // be assured that all other actions get unchecked, except the ascending/
850 // descending actions
851 for (QAction
*groupAction
: std::as_const(m_groupByActions
)) {
852 KActionMenu
*actionMenu
= qobject_cast
<KActionMenu
*>(groupAction
);
854 const auto actions
= actionMenu
->menu()->actions();
855 for (QAction
*subAction
: actions
) {
856 subAction
->setChecked(false);
858 } else if (groupAction
->actionGroup()) {
859 groupAction
->setChecked(false);
862 action
->setChecked(true);
864 // Apply the activated sort-role to the view
865 const QByteArray role
= action
->data().toByteArray();
866 m_currentView
->setGroupRole(role
);
869 void DolphinViewActionHandler::slotAdjustViewProperties()
871 Q_EMIT
actionBeingHandled();
872 QPointer
<ViewPropertiesDialog
> dialog
= new ViewPropertiesDialog(m_currentView
);
877 void DolphinViewActionHandler::slotDuplicate()
879 if (m_currentView
->selectedItemsCount() == 0) {
880 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DuplicateContents
);
882 Q_EMIT
actionBeingHandled();
883 m_currentView
->duplicateSelectedItems();
884 Q_EMIT
selectionModeChangeTriggered(false);
888 void DolphinViewActionHandler::slotProperties()
890 KPropertiesDialog
*dialog
= nullptr;
891 const KFileItemList list
= m_currentView
->selectedItems();
892 if (list
.isEmpty()) {
893 const QUrl url
= m_currentView
->url();
894 dialog
= new KPropertiesDialog(url
, m_currentView
);
896 dialog
= new KPropertiesDialog(list
, m_currentView
);
899 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
902 dialog
->activateWindow();
905 void DolphinViewActionHandler::slotCopyPath()
907 if (m_currentView
->selectedItemsCount() == 0) {
908 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::CopyLocationContents
);
910 m_currentView
->copyPathToClipboard();
911 Q_EMIT
selectionModeChangeTriggered(false);
915 void DolphinViewActionHandler::slotSelectionChanged(const KFileItemList
&selection
)
917 QString basicActionsMenuText
;
918 if (selection
.isEmpty()) {
919 basicActionsMenuText
= i18nc("@action:inmenu menu with actions like copy, paste, rename. The user's selection is empty when this text is shown.",
920 "Actions for Current View");
923 QFontMetrics fontMetrics
= QMenu().fontMetrics();
924 // i18n: @action:inmenu menu with actions like copy, paste, rename.
925 // %1 is a textual representation of the currently selected files or folders. This can be the name of
926 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
927 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
928 // and a fallback will be used.
929 basicActionsMenuText
= i18n("Actions for %1", fileItemListToString(selection
, fontMetrics
.averageCharWidth() * 40, fontMetrics
, ItemsState::Selected
));
933 if (basicActionsMenuText
== QStringLiteral("NULL")) {
934 const KFileItemListProperties
properties(selection
);
935 basicActionsMenuText
= i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
936 "Actions for One Selected Item",
937 "Actions for %1 Selected Items",
941 QAction
*basicActionsMenu
= m_actionCollection
->action(QStringLiteral("basic_actions"));
942 basicActionsMenu
->setText(basicActionsMenuText
);
944 // Add or remove contextual actions
945 while (!basicActionsMenu
->menu()->actions().constLast()->isSeparator()) {
946 basicActionsMenu
->menu()->removeAction(basicActionsMenu
->menu()->actions().last());
948 if (selection
.count() == 1) {
949 if (selection
.first().isLink()) {
950 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("show_target")));
952 if (selection
.first().isDir()) {
953 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("add_to_places")));
958 #include "moc_dolphinviewactionhandler.cpp"