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
*group
= new QActionGroup(sortByActionMenu
);
282 group
->setExclusive(true);
284 KToggleAction
*sortAscendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("sort_ascending"));
285 sortAscendingAction
->setActionGroup(group
);
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(group
);
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 KActionMenu
*groupByActionMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("group"));
306 groupByActionMenu
->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
307 groupByActionMenu
->setText(i18nc("@action:inmenu View", "Group By"));
308 groupByActionMenu
->setPopupMode(QToolButton::InstantPopup
);
310 const auto groupByActionGroupActions
= groupByActionGroup
->actions();
311 for (QAction
*action
: groupByActionGroupActions
) {
312 groupByActionMenu
->addAction(action
);
315 groupByActionMenu
->addSeparator();
317 KToggleAction
*groupAscendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("group_ascending"));
318 groupAscendingAction
->setActionGroup(group
);
319 connect(groupAscendingAction
, &QAction::triggered
, this, [this] {
320 m_currentView
->setGroupOrder(Qt::AscendingOrder
);
323 KToggleAction
*groupDescendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("group_descending"));
324 groupDescendingAction
->setActionGroup(group
);
325 connect(groupDescendingAction
, &QAction::triggered
, this, [this] {
326 m_currentView
->setGroupOrder(Qt::DescendingOrder
);
329 groupByActionMenu
->addAction(groupAscendingAction
);
330 groupByActionMenu
->addAction(groupDescendingAction
);
332 // View -> Additional Information
333 QActionGroup
*visibleRolesGroup
= createFileItemRolesActionGroup(QStringLiteral("show_"));
335 KActionMenu
*visibleRolesMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("additional_info"));
336 visibleRolesMenu
->setText(i18nc("@action:inmenu View", "Show Additional Information"));
337 visibleRolesMenu
->setIcon(QIcon::fromTheme(QStringLiteral("documentinfo")));
338 visibleRolesMenu
->setPopupMode(QToolButton::InstantPopup
);
340 const auto visibleRolesGroupActions
= visibleRolesGroup
->actions();
341 for (QAction
*action
: visibleRolesGroupActions
) {
342 visibleRolesMenu
->addAction(action
);
345 KToggleAction
*showInGroups
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_in_groups"));
346 showInGroups
->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
347 showInGroups
->setText(i18nc("@action:inmenu View", "Show in Groups"));
348 showInGroups
->setWhatsThis(i18nc("@info:whatsthis", "This groups files and folders by their first letter."));
349 connect(showInGroups
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleGroupedSorting
);
351 KToggleAction
*showHiddenFiles
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_hidden_files"));
352 showHiddenFiles
->setIcon(QIcon::fromTheme(QStringLiteral("view-visible")));
353 showHiddenFiles
->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
354 showHiddenFiles
->setWhatsThis(xi18nc("@info:whatsthis",
356 "this is enabled, <emphasis>hidden</emphasis> files and folders "
357 "are visible. They will be displayed semi-transparent.</para>"
358 "<para>Hidden items only differ from normal ones in that their "
359 "name starts with a dot (\".\"). Typically, there is no need for "
360 "users to access them, which is why they are hidden.</para>"
361 "<para>Items can also be hidden if their names are listed in a text file "
362 "named \".hidden\". Files with the \"application/x-trash\" MIME type, "
363 "such as backup files, can also be hidden by enabling that setting in "
364 "Configure Dolphin > View > General.</para>"));
365 m_actionCollection
->setDefaultShortcuts(showHiddenFiles
, KStandardShortcut::showHideHiddenFiles());
366 connect(showHiddenFiles
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleShowHiddenFiles
);
368 QAction
*adjustViewProps
= m_actionCollection
->addAction(QStringLiteral("view_properties"));
369 adjustViewProps
->setText(i18nc("@action:inmenu View", "Adjust View Display Style…"));
370 adjustViewProps
->setIcon(QIcon::fromTheme(QStringLiteral("view-choose")));
371 adjustViewProps
->setWhatsThis(i18nc("@info:whatsthis",
372 "This opens a window "
373 "in which all folder view properties can be adjusted."));
374 connect(adjustViewProps
, &QAction::triggered
, this, &DolphinViewActionHandler::slotAdjustViewProperties
);
377 QActionGroup
*DolphinViewActionHandler::createFileItemRolesActionGroup(const QString
&groupPrefix
)
379 const bool isSortGroup
= (groupPrefix
== QLatin1String("sort_by_"));
380 const bool isGroupGroup
= (groupPrefix
== QLatin1String("group_by_"));
381 Q_ASSERT(isSortGroup
|| isGroupGroup
|| groupPrefix
== QLatin1String("show_"));
383 QActionGroup
*rolesActionGroup
= new QActionGroup(m_actionCollection
);
384 rolesActionGroup
->setExclusive(isSortGroup
|| isGroupGroup
);
386 connect(rolesActionGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::slotSortTriggered
);
387 } else if (isGroupGroup
) {
388 connect(rolesActionGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::slotGroupTriggered
);
390 connect(rolesActionGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::toggleVisibleRole
);
394 KActionMenu
*groupMenu
= nullptr;
395 QActionGroup
*groupMenuGroup
= nullptr;
397 bool indexingEnabled
= false;
399 Baloo::IndexerConfig config
;
400 indexingEnabled
= config
.fileIndexingEnabled();
403 QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
404 // Unlike sorting, grouping is optional. If creating for group_by_, include a None role.
406 rolesInfo
.append(KFileItemModel::roleInformation(nullptr));
408 for (const KFileItemModel::RoleInfo
&info
: rolesInfo
) {
409 if (!isSortGroup
&& !isGroupGroup
&& info
.role
== "text") {
410 // It should not be possible to hide the "text" role
414 KToggleAction
*action
= nullptr;
415 const QString name
= groupPrefix
+ info
.role
;
416 if (info
.group
.isEmpty()) {
417 action
= m_actionCollection
->add
<KToggleAction
>(name
);
418 action
->setActionGroup(rolesActionGroup
);
420 if (!groupMenu
|| info
.group
!= groupName
) {
421 groupName
= info
.group
;
422 groupMenu
= m_actionCollection
->add
<KActionMenu
>(groupName
);
423 groupMenu
->setText(groupName
);
424 groupMenu
->setActionGroup(rolesActionGroup
);
426 groupMenuGroup
= new QActionGroup(groupMenu
);
427 groupMenuGroup
->setExclusive(isSortGroup
|| isGroupGroup
);
429 connect(groupMenuGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::slotSortTriggered
);
430 } else if (isGroupGroup
) {
431 connect(groupMenuGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::slotGroupTriggered
);
433 connect(groupMenuGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::toggleVisibleRole
);
437 action
= new KToggleAction(groupMenu
);
438 action
->setActionGroup(groupMenuGroup
);
439 groupMenu
->addAction(action
);
441 action
->setText(info
.translation
);
442 action
->setData(info
.role
);
444 const bool enable
= (!info
.requiresBaloo
&& !info
.requiresIndexer
) || (info
.requiresBaloo
) || (info
.requiresIndexer
&& indexingEnabled
);
445 action
->setEnabled(enable
);
448 m_sortByActions
.insert(info
.role
, action
);
449 } else if (isGroupGroup
) {
450 m_groupByActions
.insert(info
.role
, action
);
452 m_visibleRoles
.insert(info
.role
, action
);
456 return rolesActionGroup
;
459 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction
*action
)
461 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
462 m_currentView
->setViewMode(mode
);
464 QAction
*viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
465 viewModeMenu
->setIcon(action
->icon());
468 void DolphinViewActionHandler::slotRename()
470 if (m_currentView
->selectedItemsCount() == 0) {
471 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::RenameContents
);
473 Q_EMIT
actionBeingHandled();
474 m_currentView
->renameSelectedItems();
475 // We don't exit selectionMode here because users might want to rename more items.
479 void DolphinViewActionHandler::slotTrashActivated()
481 if (m_currentView
->selectedItemsCount() == 0) {
482 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::MoveToTrashContents
);
484 Q_EMIT
actionBeingHandled();
485 m_currentView
->trashSelectedItems();
486 Q_EMIT
selectionModeChangeTriggered(false);
490 void DolphinViewActionHandler::slotDeleteItems()
492 if (m_currentView
->selectedItemsCount() == 0) {
493 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DeleteContents
);
495 Q_EMIT
actionBeingHandled();
496 m_currentView
->deleteSelectedItems();
497 Q_EMIT
selectionModeChangeTriggered(false);
501 void DolphinViewActionHandler::togglePreview(bool show
)
503 Q_EMIT
actionBeingHandled();
504 m_currentView
->setPreviewsShown(show
);
507 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown
)
510 // It is not enough to update the 'Show Preview' action, also
511 // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
515 QString
DolphinViewActionHandler::currentViewModeActionName() const
517 switch (m_currentView
->viewMode()) {
518 case DolphinView::IconsView
:
519 return QStringLiteral("icons");
520 case DolphinView::DetailsView
:
521 return QStringLiteral("details");
522 case DolphinView::CompactView
:
523 return QStringLiteral("compact");
528 return QString(); // can't happen
531 KActionCollection
*DolphinViewActionHandler::actionCollection()
533 return m_actionCollection
;
536 void DolphinViewActionHandler::updateViewActions()
538 QAction
*viewModeAction
= m_actionCollection
->action(currentViewModeActionName());
539 if (viewModeAction
) {
540 viewModeAction
->setChecked(true);
542 QAction
*viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
543 viewModeMenu
->setIcon(viewModeAction
->icon());
546 QAction
*showPreviewAction
= m_actionCollection
->action(QStringLiteral("show_preview"));
547 showPreviewAction
->setChecked(m_currentView
->previewsShown());
549 slotSortOrderChanged(m_currentView
->sortOrder());
550 slotSortFoldersFirstChanged(m_currentView
->sortFoldersFirst());
551 slotSortHiddenLastChanged(m_currentView
->sortHiddenLast());
552 slotVisibleRolesChanged(m_currentView
->visibleRoles(), QList
<QByteArray
>());
553 slotGroupedSortingChanged(m_currentView
->groupedSorting());
554 slotSortRoleChanged(m_currentView
->sortRole());
555 slotGroupRoleChanged(m_currentView
->groupRole());
556 slotGroupOrderChanged(m_currentView
->groupOrder());
557 slotZoomLevelChanged(m_currentView
->zoomLevel(), -1);
559 // Updates the "show_hidden_files" action state and icon
560 slotHiddenFilesShownChanged(m_currentView
->hiddenFilesShown());
563 void DolphinViewActionHandler::zoomIn()
565 const int level
= m_currentView
->zoomLevel();
566 m_currentView
->setZoomLevel(level
+ 1);
570 void DolphinViewActionHandler::zoomOut()
572 const int level
= m_currentView
->zoomLevel();
573 m_currentView
->setZoomLevel(level
- 1);
577 void DolphinViewActionHandler::zoomReset()
579 m_currentView
->resetZoomLevel();
583 void DolphinViewActionHandler::toggleSortFoldersFirst()
585 const bool sortFirst
= m_currentView
->sortFoldersFirst();
586 m_currentView
->setSortFoldersFirst(!sortFirst
);
589 void DolphinViewActionHandler::toggleSortHiddenLast()
591 const bool sortHiddenLast
= m_currentView
->sortHiddenLast();
592 m_currentView
->setSortHiddenLast(!sortHiddenLast
);
595 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order
)
597 QAction
*descending
= m_actionCollection
->action(QStringLiteral("sort_descending"));
598 QAction
*ascending
= m_actionCollection
->action(QStringLiteral("sort_ascending"));
599 const bool sortDescending
= (order
== Qt::DescendingOrder
);
600 descending
->setChecked(sortDescending
);
601 ascending
->setChecked(!sortDescending
);
604 void DolphinViewActionHandler::slotGroupOrderChanged(Qt::SortOrder order
)
606 QAction
*descending
= m_actionCollection
->action(QStringLiteral("group_descending"));
607 QAction
*ascending
= m_actionCollection
->action(QStringLiteral("group_ascending"));
608 const bool sortDescending
= (order
== Qt::DescendingOrder
);
609 descending
->setChecked(sortDescending
);
610 ascending
->setChecked(!sortDescending
);
613 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst
)
615 m_actionCollection
->action(QStringLiteral("folders_first"))->setChecked(foldersFirst
);
618 void DolphinViewActionHandler::slotSortHiddenLastChanged(bool hiddenLast
)
620 m_actionCollection
->action(QStringLiteral("hidden_last"))->setChecked(hiddenLast
);
623 void DolphinViewActionHandler::toggleVisibleRole(QAction
*action
)
625 Q_EMIT
actionBeingHandled();
627 const QByteArray toggledRole
= action
->data().toByteArray();
629 QList
<QByteArray
> roles
= m_currentView
->visibleRoles();
631 const bool show
= action
->isChecked();
633 const int index
= roles
.indexOf(toggledRole
);
634 const bool containsInfo
= (index
>= 0);
635 if (show
&& !containsInfo
) {
636 roles
.append(toggledRole
);
637 m_currentView
->setVisibleRoles(roles
);
638 } else if (!show
&& containsInfo
) {
639 roles
.removeAt(index
);
640 m_currentView
->setVisibleRoles(roles
);
641 Q_ASSERT(roles
.indexOf(toggledRole
) < 0);
645 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList
<QByteArray
> ¤t
, const QList
<QByteArray
> &previous
)
649 const auto checkedRoles
= QSet
<QByteArray
>(current
.constBegin(), current
.constEnd());
650 QHashIterator
<QByteArray
, KToggleAction
*> it(m_visibleRoles
);
651 while (it
.hasNext()) {
653 const QByteArray
&role
= it
.key();
654 KToggleAction
*action
= it
.value();
655 action
->setChecked(checkedRoles
.contains(role
));
659 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped
)
661 m_currentView
->setGroupedSorting(grouped
);
664 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting
)
666 QAction
*showInGroupsAction
= m_actionCollection
->action(QStringLiteral("show_in_groups"));
667 showInGroupsAction
->setChecked(groupedSorting
);
670 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show
)
672 Q_EMIT
actionBeingHandled();
673 m_currentView
->setHiddenFilesShown(show
);
676 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown
)
678 QAction
*showHiddenFilesAction
= m_actionCollection
->action(QStringLiteral("show_hidden_files"));
679 showHiddenFilesAction
->setChecked(shown
);
682 void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable
)
684 m_actionCollection
->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable
&& KProtocolManager::supportsMakeDir(currentView()->url()));
687 KToggleAction
*DolphinViewActionHandler::iconsModeAction()
689 KToggleAction
*iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("icons"));
690 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
691 iconsView
->setToolTip(i18nc("@info", "Icons view mode"));
692 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_1
);
693 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
694 iconsView
->setData(QVariant::fromValue(DolphinView::IconsView
));
698 KToggleAction
*DolphinViewActionHandler::compactModeAction()
700 KToggleAction
*iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("compact"));
701 iconsView
->setText(i18nc("@action:inmenu View Mode", "Compact"));
702 iconsView
->setToolTip(i18nc("@info", "Compact view mode"));
703 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_2
);
704 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
705 iconsView
->setData(QVariant::fromValue(DolphinView::CompactView
));
709 KToggleAction
*DolphinViewActionHandler::detailsModeAction()
711 KToggleAction
*detailsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("details"));
712 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
713 detailsView
->setToolTip(i18nc("@info", "Details view mode"));
714 m_actionCollection
->setDefaultShortcut(detailsView
, Qt::CTRL
| Qt::Key_3
);
715 detailsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
716 detailsView
->setData(QVariant::fromValue(DolphinView::DetailsView
));
720 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray
&role
)
722 KToggleAction
*action
= m_sortByActions
.value(role
);
724 action
->setChecked(true);
726 if (!action
->icon().isNull()) {
727 QAction
*sortByMenu
= m_actionCollection
->action(QStringLiteral("sort"));
728 sortByMenu
->setIcon(action
->icon());
732 QAction
*descending
= m_actionCollection
->action(QStringLiteral("sort_descending"));
733 QAction
*ascending
= m_actionCollection
->action(QStringLiteral("sort_ascending"));
735 if (role
== "text" || role
== "type" || role
== "extension" || role
== "tags" || role
== "comment") {
736 descending
->setText(i18nc("Sort descending", "Z-A"));
737 ascending
->setText(i18nc("Sort ascending", "A-Z"));
738 } else if (role
== "size") {
739 descending
->setText(i18nc("Sort descending", "Largest First"));
740 ascending
->setText(i18nc("Sort ascending", "Smallest First"));
741 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
742 descending
->setText(i18nc("Sort descending", "Newest First"));
743 ascending
->setText(i18nc("Sort ascending", "Oldest First"));
744 } else if (role
== "rating") {
745 descending
->setText(i18nc("Sort descending", "Highest First"));
746 ascending
->setText(i18nc("Sort ascending", "Lowest First"));
748 descending
->setText(i18nc("Sort descending", "Descending"));
749 ascending
->setText(i18nc("Sort ascending", "Ascending"));
752 slotSortOrderChanged(m_currentView
->sortOrder());
755 void DolphinViewActionHandler::slotGroupRoleChanged(const QByteArray
&role
)
757 KToggleAction
*action
= m_groupByActions
.value(role
);
759 action
->setChecked(true);
761 if (!action
->icon().isNull()) {
762 QAction
*groupByMenu
= m_actionCollection
->action(QStringLiteral("group"));
763 groupByMenu
->setIcon(action
->icon());
767 QAction
*descending
= m_actionCollection
->action(QStringLiteral("group_descending"));
768 QAction
*ascending
= m_actionCollection
->action(QStringLiteral("group_ascending"));
770 if (role
== "text" || role
== "type" || role
== "extension" || role
== "tags" || role
== "comment") {
771 descending
->setText(i18nc("Group descending", "Z-A"));
772 ascending
->setText(i18nc("Group ascending", "A-Z"));
773 } else if (role
== "size") {
774 descending
->setText(i18nc("Group descending", "Largest First"));
775 ascending
->setText(i18nc("Group ascending", "Smallest First"));
776 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
777 descending
->setText(i18nc("Group descending", "Newest First"));
778 ascending
->setText(i18nc("Group ascending", "Oldest First"));
779 } else if (role
== "rating") {
780 descending
->setText(i18nc("Group descending", "Highest First"));
781 ascending
->setText(i18nc("Group ascending", "Lowest First"));
783 descending
->setText(i18nc("Group descending", "Descending"));
784 ascending
->setText(i18nc("Group ascending", "Ascending"));
787 slotGroupOrderChanged(m_currentView
->groupOrder());
790 void DolphinViewActionHandler::slotZoomLevelChanged(int current
, int previous
)
794 QAction
*zoomInAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomIn
));
796 zoomInAction
->setEnabled(current
< ZoomLevelInfo::maximumLevel());
799 QAction
*zoomOutAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomOut
));
801 zoomOutAction
->setEnabled(current
> ZoomLevelInfo::minimumLevel());
805 void DolphinViewActionHandler::slotSortTriggered(QAction
*action
)
807 // The radiobuttons of the "Sort By"-menu are split between the main-menu
808 // and several sub-menus. Because of this they don't have a common
809 // action-group that assures an exclusive toggle-state between the main-menu
810 // actions and the sub-menu-actions. If an action gets checked, it must
811 // be assured that all other actions get unchecked, except the ascending/
812 // descending actions
813 for (QAction
*groupAction
: std::as_const(m_sortByActions
)) {
814 KActionMenu
*actionMenu
= qobject_cast
<KActionMenu
*>(groupAction
);
816 const auto actions
= actionMenu
->menu()->actions();
817 for (QAction
*subAction
: actions
) {
818 subAction
->setChecked(false);
820 } else if (groupAction
->actionGroup()) {
821 groupAction
->setChecked(false);
824 action
->setChecked(true);
826 // Apply the activated sort-role to the view
827 const QByteArray role
= action
->data().toByteArray();
828 m_currentView
->setSortRole(role
);
831 void DolphinViewActionHandler::slotGroupTriggered(QAction
*action
)
833 // The radiobuttons of the "Group By"-menu are split between the main-menu
834 // and several sub-menus. Because of this they don't have a common
835 // action-group that assures an exclusive toggle-state between the main-menu
836 // actions and the sub-menu-actions. If an action gets checked, it must
837 // be assured that all other actions get unchecked, except the ascending/
838 // descending actions
839 for (QAction
*groupAction
: std::as_const(m_sortByActions
)) {
840 KActionMenu
*actionMenu
= qobject_cast
<KActionMenu
*>(groupAction
);
842 const auto actions
= actionMenu
->menu()->actions();
843 for (QAction
*subAction
: actions
) {
844 subAction
->setChecked(false);
846 } else if (groupAction
->actionGroup()) {
847 groupAction
->setChecked(false);
850 action
->setChecked(true);
852 // Apply the activated sort-role to the view
853 const QByteArray role
= action
->data().toByteArray();
854 m_currentView
->setGroupRole(role
);
857 void DolphinViewActionHandler::slotAdjustViewProperties()
859 Q_EMIT
actionBeingHandled();
860 QPointer
<ViewPropertiesDialog
> dialog
= new ViewPropertiesDialog(m_currentView
);
865 void DolphinViewActionHandler::slotDuplicate()
867 if (m_currentView
->selectedItemsCount() == 0) {
868 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DuplicateContents
);
870 Q_EMIT
actionBeingHandled();
871 m_currentView
->duplicateSelectedItems();
872 Q_EMIT
selectionModeChangeTriggered(false);
876 void DolphinViewActionHandler::slotProperties()
878 KPropertiesDialog
*dialog
= nullptr;
879 const KFileItemList list
= m_currentView
->selectedItems();
880 if (list
.isEmpty()) {
881 const QUrl url
= m_currentView
->url();
882 dialog
= new KPropertiesDialog(url
, m_currentView
);
884 dialog
= new KPropertiesDialog(list
, m_currentView
);
887 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
890 dialog
->activateWindow();
893 void DolphinViewActionHandler::slotCopyPath()
895 if (m_currentView
->selectedItemsCount() == 0) {
896 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::CopyLocationContents
);
898 m_currentView
->copyPathToClipboard();
899 Q_EMIT
selectionModeChangeTriggered(false);
903 void DolphinViewActionHandler::slotSelectionChanged(const KFileItemList
&selection
)
905 QString basicActionsMenuText
;
906 if (selection
.isEmpty()) {
907 basicActionsMenuText
= i18nc("@action:inmenu menu with actions like copy, paste, rename. The user's selection is empty when this text is shown.",
908 "Actions for Current View");
911 QFontMetrics fontMetrics
= QMenu().fontMetrics();
912 // i18n: @action:inmenu menu with actions like copy, paste, rename.
913 // %1 is a textual representation of the currently selected files or folders. This can be the name of
914 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
915 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
916 // and a fallback will be used.
917 basicActionsMenuText
= i18n("Actions for %1", fileItemListToString(selection
, fontMetrics
.averageCharWidth() * 40, fontMetrics
, ItemsState::Selected
));
921 if (basicActionsMenuText
== QStringLiteral("NULL")) {
922 const KFileItemListProperties
properties(selection
);
923 basicActionsMenuText
= i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
924 "Actions for One Selected Item",
925 "Actions for %1 Selected Items",
929 QAction
*basicActionsMenu
= m_actionCollection
->action(QStringLiteral("basic_actions"));
930 basicActionsMenu
->setText(basicActionsMenuText
);
932 // Add or remove contextual actions
933 while (!basicActionsMenu
->menu()->actions().constLast()->isSeparator()) {
934 basicActionsMenu
->menu()->removeAction(basicActionsMenu
->menu()->actions().last());
936 if (selection
.count() == 1) {
937 if (selection
.first().isLink()) {
938 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("show_target")));
940 if (selection
.first().isDir()) {
941 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("add_to_places")));
946 #include "moc_dolphinviewactionhandler.cpp"