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)
38 Q_ASSERT(m_actionCollection
);
39 createActions(actionTextHelper
);
42 void DolphinViewActionHandler::setCurrentView(DolphinView
*view
)
47 disconnect(m_currentView
, nullptr, this, nullptr);
52 connect(view
, &DolphinView::modeChanged
, this, &DolphinViewActionHandler::updateViewActions
);
53 connect(view
, &DolphinView::previewsShownChanged
, this, &DolphinViewActionHandler::slotPreviewsShownChanged
);
54 connect(view
, &DolphinView::sortOrderChanged
, this, &DolphinViewActionHandler::slotSortOrderChanged
);
55 connect(view
, &DolphinView::sortFoldersFirstChanged
, this, &DolphinViewActionHandler::slotSortFoldersFirstChanged
);
56 connect(view
, &DolphinView::sortHiddenLastChanged
, this, &DolphinViewActionHandler::slotSortHiddenLastChanged
);
57 connect(view
, &DolphinView::visibleRolesChanged
, this, &DolphinViewActionHandler::slotVisibleRolesChanged
);
58 connect(view
, &DolphinView::groupedSortingChanged
, this, &DolphinViewActionHandler::slotGroupedSortingChanged
);
59 connect(view
, &DolphinView::hiddenFilesShownChanged
, this, &DolphinViewActionHandler::slotHiddenFilesShownChanged
);
60 connect(view
, &DolphinView::sortRoleChanged
, this, &DolphinViewActionHandler::slotSortRoleChanged
);
61 connect(view
, &DolphinView::zoomLevelChanged
, this, &DolphinViewActionHandler::slotZoomLevelChanged
);
62 connect(view
, &DolphinView::writeStateChanged
, this, &DolphinViewActionHandler::slotWriteStateChanged
);
63 slotWriteStateChanged(view
->isFolderWritable());
64 connect(view
, &DolphinView::selectionModeChangeRequested
, this, [this](bool enabled
) {
65 Q_EMIT
selectionModeChangeTriggered(enabled
);
67 connect(view
, &DolphinView::selectionChanged
, this, &DolphinViewActionHandler::slotSelectionChanged
);
68 slotSelectionChanged(m_currentView
->selectedItems());
71 DolphinView
*DolphinViewActionHandler::currentView()
76 void DolphinViewActionHandler::createActions(SelectionMode::ActionTextHelper
*actionTextHelper
)
78 // This action doesn't appear in the GUI, it's for the shortcut only.
79 // KNewFileMenu takes care of the GUI stuff.
80 QAction
*newDirAction
= m_actionCollection
->addAction(QStringLiteral("create_dir"));
81 newDirAction
->setText(i18nc("@action", "Create Folder…"));
82 m_actionCollection
->setDefaultShortcuts(newDirAction
, KStandardShortcut::createFolder());
83 newDirAction
->setIcon(QIcon::fromTheme(QStringLiteral("folder-new")));
84 newDirAction
->setEnabled(false); // Will be enabled in slotWriteStateChanged(bool) if the current URL is writable
85 connect(newDirAction
, &QAction::triggered
, this, &DolphinViewActionHandler::createDirectoryTriggered
);
87 QAction
*newFileAction
= m_actionCollection
->addAction(QStringLiteral("create_file"));
88 newFileAction
->setText(i18nc("@action", "Create File…"));
89 newFileAction
->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
90 newFileAction
->setEnabled(false); // Will be enabled in slotWriteStateChanged(bool) if the current URL is writable
91 connect(newFileAction
, &QAction::triggered
, this, &DolphinViewActionHandler::createFileTriggered
);
95 auto renameAction
= KStandardAction::renameFile(this, &DolphinViewActionHandler::slotRename
, m_actionCollection
);
96 renameAction
->setWhatsThis(xi18nc("@info:whatsthis",
98 "items in your current selection.<nl/>Renaming multiple items "
99 "at once results in their new names differing only in a number."));
101 auto trashAction
= KStandardAction::moveToTrash(this, &DolphinViewActionHandler::slotTrashActivated
, m_actionCollection
);
102 auto trashShortcuts
= trashAction
->shortcuts();
103 trashAction
->setAutoRepeat(false);
104 if (!trashShortcuts
.contains(QKeySequence::Delete
)) {
105 trashShortcuts
.append(QKeySequence::Delete
);
106 m_actionCollection
->setDefaultShortcuts(trashAction
, trashShortcuts
);
108 trashAction
->setWhatsThis(xi18nc("@info:whatsthis",
110 "items in your current selection to the <filename>Trash"
111 "</filename>.<nl/>The trash is a temporary storage location where "
112 "items can be deleted later if disk space is needed."));
114 auto deleteAction
= KStandardAction::deleteFile(this, &DolphinViewActionHandler::slotDeleteItems
, m_actionCollection
);
115 auto deleteShortcuts
= deleteAction
->shortcuts();
116 deleteAction
->setAutoRepeat(false);
117 if (!deleteShortcuts
.contains(Qt::SHIFT
| Qt::Key_Delete
)) {
118 deleteShortcuts
.append(Qt::SHIFT
| Qt::Key_Delete
);
119 m_actionCollection
->setDefaultShortcuts(deleteAction
, deleteShortcuts
);
121 deleteAction
->setWhatsThis(xi18nc("@info:whatsthis",
123 "the items in your current selection permanently. They "
124 "cannot be recovered by normal means."));
126 // This action is useful for being enabled when KStandardAction::MoveToTrash should be
127 // disabled and KStandardAction::DeleteFile is enabled (e.g. non-local files), so that Key_Del
128 // can be used for deleting the file (#76016). It needs to be a separate action
129 // so that the Edit menu isn't affected.
130 QAction
*deleteWithTrashShortcut
= m_actionCollection
->addAction(QStringLiteral("delete_shortcut"));
131 // The descriptive text is just for the shortcuts editor.
132 deleteWithTrashShortcut
->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
133 m_actionCollection
->setDefaultShortcuts(deleteWithTrashShortcut
, KStandardShortcut::moveToTrash());
134 deleteWithTrashShortcut
->setEnabled(false);
135 connect(deleteWithTrashShortcut
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDeleteItems
);
137 QAction
*duplicateAction
= m_actionCollection
->addAction(QStringLiteral("duplicate"));
138 duplicateAction
->setText(i18nc("@action:inmenu File", "Duplicate Here"));
139 duplicateAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-duplicate")));
140 m_actionCollection
->setDefaultShortcut(duplicateAction
, Qt::CTRL
| Qt::Key_D
);
141 duplicateAction
->setEnabled(false);
142 connect(duplicateAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDuplicate
);
144 QAction
*propertiesAction
= m_actionCollection
->addAction(QStringLiteral("properties"));
145 // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
146 propertiesAction
->setText(i18nc("@action:inmenu File", "Properties"));
147 propertiesAction
->setWhatsThis(xi18nc("@info:whatsthis properties",
148 "This shows a complete list of properties of the currently "
149 "selected items in a new window.<nl/>If nothing is selected the "
150 "window will be about the currently viewed folder instead.<nl/>"
151 "You can configure advanced options there like managing "
152 "read- and write-permissions."));
153 propertiesAction
->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
154 m_actionCollection
->setDefaultShortcuts(propertiesAction
, {Qt::ALT
| Qt::Key_Return
, Qt::ALT
| Qt::Key_Enter
});
155 connect(propertiesAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotProperties
);
157 QAction
*copyPathAction
= m_actionCollection
->addAction(QStringLiteral("copy_location"));
158 copyPathAction
->setText(i18nc("@action:incontextmenu", "Copy Location"));
159 copyPathAction
->setWhatsThis(i18nc("@info:whatsthis copy_location", "This will copy the path of the first selected item into the clipboard."));
161 copyPathAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy-path")));
162 m_actionCollection
->setDefaultShortcuts(copyPathAction
, {Qt::CTRL
| Qt::ALT
| Qt::Key_C
});
163 connect(copyPathAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotCopyPath
);
165 if (actionTextHelper
) {
166 // The "…" at the end make clear that they won't trigger their respective actions directly.
167 actionTextHelper
->registerTextWhenNothingIsSelected(trashAction
, i18nc("@action:inmenu File", "Move to Trash…"));
168 actionTextHelper
->registerTextWhenNothingIsSelected(deleteAction
, i18nc("@action:inmenu File", "Delete…"));
169 actionTextHelper
->registerTextWhenNothingIsSelected(duplicateAction
, i18nc("@action:inmenu File", "Duplicate Here…"));
170 actionTextHelper
->registerTextWhenNothingIsSelected(copyPathAction
, i18nc("@action:incontextmenu", "Copy Location…"));
173 // This menu makes sure that users who don't know how to open a context menu and haven't
174 // figured out how to enable the menu bar can still perform basic file manipulation.
175 // This only works if they know how to select a file.
176 // The text when nothing is selected at least implies that a selection can /somehow/ be made.
177 // This menu is by default only used in the hamburger menu but created here so users can put
178 // it on their toolbar.
179 KActionMenu
*basicActionsMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("basic_actions"), this);
180 // The text is set later depending on the selection in the currently active view.
181 basicActionsMenu
->setPopupMode(QToolButton::InstantPopup
);
182 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Cut
)));
183 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Copy
)));
184 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Paste
)));
185 basicActionsMenu
->addSeparator();
186 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
187 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
188 basicActionsMenu
->addSeparator();
189 basicActionsMenu
->addAction(m_actionCollection
->action(QStringLiteral("properties")));
190 basicActionsMenu
->addSeparator(); // We add one more separator because we sometimes add contextual
191 // actions in slotSelectionChanged() after the static ones above.
194 KToggleAction
*iconsAction
= iconsModeAction();
195 KToggleAction
*compactAction
= compactModeAction();
196 KToggleAction
*detailsAction
= detailsModeAction();
198 iconsAction
->setWhatsThis(xi18nc("@info:whatsthis Icons view mode",
199 "<para>This switches to a view mode that focuses on the folder "
200 "and file icons. This mode makes it easy to distinguish folders "
201 "from files and to detect items with distinctive <emphasis>"
202 "file types</emphasis>.</para><para> This mode is handy to "
203 "browse through pictures when the <interface>Preview"
204 "</interface> option is enabled.</para>"));
205 compactAction
->setWhatsThis(xi18nc("@info:whatsthis Compact view mode",
206 "<para>This switches to a compact view mode that lists the folders "
207 "and files in columns with the names beside the icons.</para><para>"
208 "This helps to give you an overview in folders with many items.</para>"));
209 detailsAction
->setWhatsThis(xi18nc("@info:whatsthis Details view mode",
210 "<para>This switches to a list view mode that focuses on folder "
211 "and file details.</para><para>Click on a detail in the column "
212 "header to sort the items by it. Click again to sort the other "
213 "way around. To select which details should be displayed click "
214 "the header with the right mouse button.</para><para>You can "
215 "view the contents of a folder without leaving the current "
216 "location by clicking the region to the left of it. This way you can "
217 "view the contents of multiple folders in the same list.</para>"));
219 KSelectAction
*viewModeActions
= m_actionCollection
->add
<KSelectAction
>(QStringLiteral("view_mode"));
220 viewModeActions
->setText(i18nc("@action:intoolbar", "Change View Mode"));
221 viewModeActions
->setWhatsThis(xi18nc("@info:whatsthis View Mode Toolbutton", "This cycles through all view modes."));
222 viewModeActions
->addAction(iconsAction
);
223 viewModeActions
->addAction(compactAction
);
224 viewModeActions
->addAction(detailsAction
);
225 viewModeActions
->setToolBarMode(KSelectAction::MenuMode
);
226 viewModeActions
->setToolButtonPopupMode(QToolButton::ToolButtonPopupMode::MenuButtonPopup
);
227 connect(viewModeActions
, &KSelectAction::actionTriggered
, this, &DolphinViewActionHandler::slotViewModeActionTriggered
);
228 connect(viewModeActions
, &KSelectAction::triggered
, this, [this, viewModeActions
, iconsAction
, compactAction
, detailsAction
]() {
229 // Loop through the actions when button is clicked
230 const auto currentAction
= viewModeActions
->currentAction();
231 if (currentAction
== iconsAction
) {
232 slotViewModeActionTriggered(compactAction
);
233 } else if (currentAction
== compactAction
) {
234 slotViewModeActionTriggered(detailsAction
);
235 } else if (currentAction
== detailsAction
) {
236 slotViewModeActionTriggered(iconsAction
);
240 QAction
*zoomInAction
= KStandardAction::zoomIn(this, &DolphinViewActionHandler::zoomIn
, m_actionCollection
);
241 zoomInAction
->setWhatsThis(i18nc("@info:whatsthis zoom in", "This increases the icon size."));
243 QAction
*zoomResetAction
= m_actionCollection
->addAction(QStringLiteral("view_zoom_reset"));
244 zoomResetAction
->setText(i18nc("@action:inmenu View", "Reset Zoom Level"));
245 zoomResetAction
->setToolTip(i18n("Zoom To Default"));
246 zoomResetAction
->setWhatsThis(i18nc("@info:whatsthis zoom reset", "This resets the icon size to default."));
247 zoomResetAction
->setIcon(QIcon::fromTheme(QStringLiteral("zoom-original")));
248 m_actionCollection
->setDefaultShortcuts(zoomResetAction
, {Qt::CTRL
| Qt::Key_0
});
249 connect(zoomResetAction
, &QAction::triggered
, this, &DolphinViewActionHandler::zoomReset
);
251 QAction
*zoomOutAction
= KStandardAction::zoomOut(this, &DolphinViewActionHandler::zoomOut
, m_actionCollection
);
252 zoomOutAction
->setWhatsThis(i18nc("@info:whatsthis zoom out", "This reduces the icon size."));
254 KActionMenu
*zoomMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("zoom"));
255 zoomMenu
->setText(i18nc("@action:inmenu menu of zoom actions", "Zoom"));
256 zoomMenu
->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
257 zoomMenu
->setPopupMode(QToolButton::InstantPopup
);
258 zoomMenu
->addAction(zoomInAction
);
259 zoomMenu
->addAction(zoomResetAction
);
260 zoomMenu
->addAction(zoomOutAction
);
262 KToggleAction
*showPreview
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_preview"));
263 showPreview
->setText(i18nc("@action:intoolbar", "Show Previews"));
264 showPreview
->setToolTip(i18nc("@info", "Show preview of files and folders"));
265 showPreview
->setWhatsThis(xi18nc("@info:whatsthis",
267 "enabled, the icons are based on the actual file or folder "
268 "contents.<nl/>For example the icons of images become scaled "
269 "down versions of the images."));
270 showPreview
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
271 m_actionCollection
->setDefaultShortcut(showPreview
, QKeySequence(Qt::Key_F12
));
272 connect(showPreview
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::togglePreview
);
274 KToggleAction
*sortFoldersFirst
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("folders_first"));
275 sortFoldersFirst
->setText(i18nc("@action:inmenu Sort", "Folders First"));
276 connect(sortFoldersFirst
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleSortFoldersFirst
);
278 KToggleAction
*sortHiddenLast
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("hidden_last"));
279 sortHiddenLast
->setText(i18nc("@action:inmenu Sort", "Hidden Files Last"));
280 connect(sortHiddenLast
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleSortHiddenLast
);
283 QActionGroup
*sortByActionGroup
= createFileItemRolesActionGroup(QStringLiteral("sort_by_"));
285 KActionMenu
*sortByActionMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("sort"));
286 sortByActionMenu
->setIcon(QIcon::fromTheme(QStringLiteral("view-sort")));
287 sortByActionMenu
->setText(i18nc("@action:inmenu View", "Sort By"));
288 sortByActionMenu
->setPopupMode(QToolButton::InstantPopup
);
290 const auto sortByActionGroupActions
= sortByActionGroup
->actions();
291 for (QAction
*action
: sortByActionGroupActions
) {
292 sortByActionMenu
->addAction(action
);
295 sortByActionMenu
->addSeparator();
297 QActionGroup
*group
= new QActionGroup(sortByActionMenu
);
298 group
->setExclusive(true);
300 KToggleAction
*ascendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("ascending"));
301 ascendingAction
->setActionGroup(group
);
302 connect(ascendingAction
, &QAction::triggered
, this, [this] {
303 m_currentView
->setSortOrder(Qt::AscendingOrder
);
306 KToggleAction
*descendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("descending"));
307 descendingAction
->setActionGroup(group
);
308 connect(descendingAction
, &QAction::triggered
, this, [this] {
309 m_currentView
->setSortOrder(Qt::DescendingOrder
);
312 sortByActionMenu
->addAction(ascendingAction
);
313 sortByActionMenu
->addAction(descendingAction
);
314 sortByActionMenu
->addSeparator();
315 sortByActionMenu
->addAction(sortFoldersFirst
);
316 sortByActionMenu
->addAction(sortHiddenLast
);
318 // View -> Additional Information
319 QActionGroup
*visibleRolesGroup
= createFileItemRolesActionGroup(QStringLiteral("show_"));
321 KActionMenu
*visibleRolesMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("additional_info"));
322 visibleRolesMenu
->setText(i18nc("@action:inmenu View", "Show Additional Information"));
323 visibleRolesMenu
->setIcon(QIcon::fromTheme(QStringLiteral("documentinfo")));
324 visibleRolesMenu
->setPopupMode(QToolButton::InstantPopup
);
326 const auto visibleRolesGroupActions
= visibleRolesGroup
->actions();
327 for (QAction
*action
: visibleRolesGroupActions
) {
328 visibleRolesMenu
->addAction(action
);
331 KToggleAction
*showInGroups
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_in_groups"));
332 showInGroups
->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
333 showInGroups
->setText(i18nc("@action:inmenu View", "Show in Groups"));
334 showInGroups
->setWhatsThis(i18nc("@info:whatsthis", "This groups files and folders by their first letter."));
335 connect(showInGroups
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleGroupedSorting
);
337 KToggleAction
*showHiddenFiles
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_hidden_files"));
338 showHiddenFiles
->setIcon(QIcon::fromTheme(QStringLiteral("view-visible")));
339 showHiddenFiles
->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
340 showHiddenFiles
->setWhatsThis(xi18nc("@info:whatsthis",
342 "this is enabled, <emphasis>hidden</emphasis> files and folders "
343 "are visible. They will be displayed semi-transparent.</para>"
344 "<para>Hidden items only differ from normal ones in that their "
345 "name starts with a dot (\".\"). Typically, there is no need for "
346 "users to access them, which is why they are hidden.</para>"
347 "<para>Items can also be hidden if their names are listed in a text file "
348 "named \".hidden\". Files with the \"application/x-trash\" MIME type, "
349 "such as backup files, can also be hidden by enabling that setting in "
350 "Configure Dolphin > View > General.</para>"));
351 m_actionCollection
->setDefaultShortcuts(showHiddenFiles
, KStandardShortcut::showHideHiddenFiles());
352 connect(showHiddenFiles
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleShowHiddenFiles
);
354 QAction
*adjustViewProps
= m_actionCollection
->addAction(QStringLiteral("view_properties"));
355 adjustViewProps
->setText(i18nc("@action:inmenu View", "Adjust View Display Style…"));
356 adjustViewProps
->setIcon(QIcon::fromTheme(QStringLiteral("view-choose")));
357 adjustViewProps
->setWhatsThis(i18nc("@info:whatsthis",
358 "This opens a window "
359 "in which all folder view properties can be adjusted."));
360 connect(adjustViewProps
, &QAction::triggered
, this, &DolphinViewActionHandler::slotAdjustViewProperties
);
362 // View settings: the dropdown menu contains various view-related actions
363 KActionMenu
*viewSettings
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("view_settings"));
364 viewSettings
->setText(i18nc("@action:intoolbar", "View Settings"));
365 viewSettings
->setWhatsThis(
366 xi18nc("@info:whatsthis View Settings Toolbutton", "This cycles through all view modes. The dropdown menu contains various view-related actions."));
367 const auto actions
= viewModeActions
->actions();
368 for (QAction
*action
: actions
) {
369 viewSettings
->addAction(action
);
371 viewSettings
->addSeparator();
372 viewSettings
->addAction(zoomMenu
);
373 viewSettings
->addAction(sortByActionMenu
);
374 viewSettings
->addAction(visibleRolesMenu
);
375 viewSettings
->addAction(showPreview
);
376 viewSettings
->addAction(showInGroups
);
377 viewSettings
->addAction(showHiddenFiles
);
378 viewSettings
->addAction(adjustViewProps
);
379 viewSettings
->setPopupMode(QToolButton::ToolButtonPopupMode::MenuButtonPopup
);
380 connect(viewSettings
, &KActionMenu::triggered
, viewModeActions
, &KSelectAction::triggered
);
383 QActionGroup
*DolphinViewActionHandler::createFileItemRolesActionGroup(const QString
&groupPrefix
)
385 const bool isSortGroup
= (groupPrefix
== QLatin1String("sort_by_"));
386 Q_ASSERT(isSortGroup
|| groupPrefix
== QLatin1String("show_"));
388 QActionGroup
*rolesActionGroup
= new QActionGroup(m_actionCollection
);
389 rolesActionGroup
->setExclusive(isSortGroup
);
391 connect(rolesActionGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::slotSortTriggered
);
393 connect(rolesActionGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::toggleVisibleRole
);
397 KActionMenu
*groupMenu
= nullptr;
398 QActionGroup
*groupMenuGroup
= nullptr;
400 bool indexingEnabled
= false;
402 Baloo::IndexerConfig config
;
403 indexingEnabled
= config
.fileIndexingEnabled();
406 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
407 for (const KFileItemModel::RoleInfo
&info
: rolesInfo
) {
408 if (!isSortGroup
&& info
.role
== "text") {
409 // It should not be possible to hide the "text" role
413 KToggleAction
*action
= nullptr;
414 const QString name
= groupPrefix
+ info
.role
;
415 if (info
.group
.isEmpty()) {
416 action
= m_actionCollection
->add
<KToggleAction
>(name
);
417 action
->setActionGroup(rolesActionGroup
);
419 if (!groupMenu
|| info
.group
!= groupName
) {
420 groupName
= info
.group
;
421 groupMenu
= m_actionCollection
->add
<KActionMenu
>(groupName
);
422 groupMenu
->setText(groupName
);
423 groupMenu
->setActionGroup(rolesActionGroup
);
425 groupMenuGroup
= new QActionGroup(groupMenu
);
426 groupMenuGroup
->setExclusive(isSortGroup
);
428 connect(groupMenuGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::slotSortTriggered
);
430 connect(groupMenuGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::toggleVisibleRole
);
434 action
= new KToggleAction(groupMenu
);
435 action
->setActionGroup(groupMenuGroup
);
436 groupMenu
->addAction(action
);
438 action
->setText(info
.translation
);
439 action
->setData(info
.role
);
441 const bool enable
= (!info
.requiresBaloo
&& !info
.requiresIndexer
) || (info
.requiresBaloo
) || (info
.requiresIndexer
&& indexingEnabled
);
442 action
->setEnabled(enable
);
445 m_sortByActions
.insert(info
.role
, action
);
447 m_visibleRoles
.insert(info
.role
, action
);
451 return rolesActionGroup
;
454 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction
*action
)
456 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
457 m_currentView
->setViewMode(mode
);
459 QAction
*viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
460 viewModeMenu
->setIcon(action
->icon());
462 QAction
*viewSettingsAction
= m_actionCollection
->action(QStringLiteral("view_settings"));
463 viewSettingsAction
->setIcon(action
->icon());
466 void DolphinViewActionHandler::slotRename()
468 if (m_currentView
->selectedItemsCount() == 0) {
469 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::RenameContents
);
471 Q_EMIT
actionBeingHandled();
472 m_currentView
->renameSelectedItems();
473 // We don't exit selectionMode here because users might want to rename more items.
477 void DolphinViewActionHandler::slotTrashActivated()
479 if (m_currentView
->selectedItemsCount() == 0) {
480 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::MoveToTrashContents
);
482 Q_EMIT
actionBeingHandled();
483 m_currentView
->trashSelectedItems();
484 Q_EMIT
selectionModeChangeTriggered(false);
488 void DolphinViewActionHandler::slotDeleteItems()
490 if (m_currentView
->selectedItemsCount() == 0) {
491 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DeleteContents
);
493 Q_EMIT
actionBeingHandled();
494 m_currentView
->deleteSelectedItems();
495 Q_EMIT
selectionModeChangeTriggered(false);
499 void DolphinViewActionHandler::togglePreview(bool show
)
501 Q_EMIT
actionBeingHandled();
502 m_currentView
->setPreviewsShown(show
);
505 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown
)
508 // It is not enough to update the 'Show Preview' action, also
509 // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
513 QString
DolphinViewActionHandler::currentViewModeActionName() const
515 switch (m_currentView
->viewMode()) {
516 case DolphinView::IconsView
:
517 return QStringLiteral("icons");
518 case DolphinView::DetailsView
:
519 return QStringLiteral("details");
520 case DolphinView::CompactView
:
521 return QStringLiteral("compact");
526 return QString(); // can't happen
529 KActionCollection
*DolphinViewActionHandler::actionCollection()
531 return m_actionCollection
;
534 void DolphinViewActionHandler::updateViewActions()
536 QAction
*viewModeAction
= m_actionCollection
->action(currentViewModeActionName());
537 if (viewModeAction
) {
538 viewModeAction
->setChecked(true);
540 QAction
*viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
541 viewModeMenu
->setIcon(viewModeAction
->icon());
543 QAction
*viewSettingsAction
= m_actionCollection
->action(QStringLiteral("view_settings"));
544 viewSettingsAction
->setIcon(viewModeAction
->icon());
547 QAction
*showPreviewAction
= m_actionCollection
->action(QStringLiteral("show_preview"));
548 showPreviewAction
->setChecked(m_currentView
->previewsShown());
550 slotSortOrderChanged(m_currentView
->sortOrder());
551 slotSortFoldersFirstChanged(m_currentView
->sortFoldersFirst());
552 slotSortHiddenLastChanged(m_currentView
->sortHiddenLast());
553 slotVisibleRolesChanged(m_currentView
->visibleRoles(), QList
<QByteArray
>());
554 slotGroupedSortingChanged(m_currentView
->groupedSorting());
555 slotSortRoleChanged(m_currentView
->sortRole());
556 slotZoomLevelChanged(m_currentView
->zoomLevel(), -1);
558 // Updates the "show_hidden_files" action state and icon
559 slotHiddenFilesShownChanged(m_currentView
->hiddenFilesShown());
562 void DolphinViewActionHandler::zoomIn()
564 const int level
= m_currentView
->zoomLevel();
565 m_currentView
->setZoomLevel(level
+ 1);
569 void DolphinViewActionHandler::zoomOut()
571 const int level
= m_currentView
->zoomLevel();
572 m_currentView
->setZoomLevel(level
- 1);
576 void DolphinViewActionHandler::zoomReset()
578 m_currentView
->resetZoomLevel();
582 void DolphinViewActionHandler::toggleSortFoldersFirst()
584 const bool sortFirst
= m_currentView
->sortFoldersFirst();
585 m_currentView
->setSortFoldersFirst(!sortFirst
);
588 void DolphinViewActionHandler::toggleSortHiddenLast()
590 const bool sortHiddenLast
= m_currentView
->sortHiddenLast();
591 m_currentView
->setSortHiddenLast(!sortHiddenLast
);
594 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order
)
596 QAction
*descending
= m_actionCollection
->action(QStringLiteral("descending"));
597 QAction
*ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
598 const bool sortDescending
= (order
== Qt::DescendingOrder
);
599 descending
->setChecked(sortDescending
);
600 ascending
->setChecked(!sortDescending
);
603 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst
)
605 m_actionCollection
->action(QStringLiteral("folders_first"))->setChecked(foldersFirst
);
608 void DolphinViewActionHandler::slotSortHiddenLastChanged(bool hiddenLast
)
610 m_actionCollection
->action(QStringLiteral("hidden_last"))->setChecked(hiddenLast
);
613 void DolphinViewActionHandler::toggleVisibleRole(QAction
*action
)
615 Q_EMIT
actionBeingHandled();
617 const QByteArray toggledRole
= action
->data().toByteArray();
619 QList
<QByteArray
> roles
= m_currentView
->visibleRoles();
621 const bool show
= action
->isChecked();
623 const int index
= roles
.indexOf(toggledRole
);
624 const bool containsInfo
= (index
>= 0);
625 if (show
&& !containsInfo
) {
626 roles
.append(toggledRole
);
627 m_currentView
->setVisibleRoles(roles
);
628 } else if (!show
&& containsInfo
) {
629 roles
.removeAt(index
);
630 m_currentView
->setVisibleRoles(roles
);
631 Q_ASSERT(roles
.indexOf(toggledRole
) < 0);
635 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList
<QByteArray
> ¤t
, const QList
<QByteArray
> &previous
)
639 const auto checkedRoles
= QSet
<QByteArray
>(current
.constBegin(), current
.constEnd());
640 QHashIterator
<QByteArray
, KToggleAction
*> it(m_visibleRoles
);
641 while (it
.hasNext()) {
643 const QByteArray
&role
= it
.key();
644 KToggleAction
*action
= it
.value();
645 action
->setChecked(checkedRoles
.contains(role
));
649 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped
)
651 m_currentView
->setGroupedSorting(grouped
);
654 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting
)
656 QAction
*showInGroupsAction
= m_actionCollection
->action(QStringLiteral("show_in_groups"));
657 showInGroupsAction
->setChecked(groupedSorting
);
660 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show
)
662 Q_EMIT
actionBeingHandled();
663 m_currentView
->setHiddenFilesShown(show
);
666 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown
)
668 QAction
*showHiddenFilesAction
= m_actionCollection
->action(QStringLiteral("show_hidden_files"));
669 showHiddenFilesAction
->setChecked(shown
);
672 void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable
)
674 const bool supportsMakeDir
= KProtocolManager::supportsMakeDir(currentView()->url());
675 m_actionCollection
->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable
&& supportsMakeDir
);
676 m_actionCollection
->action(QStringLiteral("create_file"))->setEnabled(isFolderWritable
);
679 KToggleAction
*DolphinViewActionHandler::iconsModeAction()
681 KToggleAction
*iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("icons"));
682 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
683 iconsView
->setToolTip(i18nc("@info", "Icons view mode"));
684 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_1
);
685 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
686 iconsView
->setData(QVariant::fromValue(DolphinView::IconsView
));
690 KToggleAction
*DolphinViewActionHandler::compactModeAction()
692 KToggleAction
*iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("compact"));
693 iconsView
->setText(i18nc("@action:inmenu View Mode", "Compact"));
694 iconsView
->setToolTip(i18nc("@info", "Compact view mode"));
695 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_2
);
696 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
697 iconsView
->setData(QVariant::fromValue(DolphinView::CompactView
));
701 KToggleAction
*DolphinViewActionHandler::detailsModeAction()
703 KToggleAction
*detailsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("details"));
704 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
705 detailsView
->setToolTip(i18nc("@info", "Details view mode"));
706 m_actionCollection
->setDefaultShortcut(detailsView
, Qt::CTRL
| Qt::Key_3
);
707 detailsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
708 detailsView
->setData(QVariant::fromValue(DolphinView::DetailsView
));
712 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray
&role
)
714 KToggleAction
*action
= m_sortByActions
.value(role
);
716 action
->setChecked(true);
718 if (!action
->icon().isNull()) {
719 QAction
*sortByMenu
= m_actionCollection
->action(QStringLiteral("sort"));
720 sortByMenu
->setIcon(action
->icon());
724 QAction
*descending
= m_actionCollection
->action(QStringLiteral("descending"));
725 QAction
*ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
727 if (role
== "text" || role
== "type" || role
== "extension" || role
== "tags" || role
== "comment") {
728 descending
->setText(i18nc("Sort descending", "Z-A"));
729 ascending
->setText(i18nc("Sort ascending", "A-Z"));
730 } else if (role
== "size") {
731 descending
->setText(i18nc("Sort descending", "Largest First"));
732 ascending
->setText(i18nc("Sort ascending", "Smallest First"));
733 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
734 descending
->setText(i18nc("Sort descending", "Newest First"));
735 ascending
->setText(i18nc("Sort ascending", "Oldest First"));
736 } else if (role
== "rating") {
737 descending
->setText(i18nc("Sort descending", "Highest First"));
738 ascending
->setText(i18nc("Sort ascending", "Lowest First"));
740 descending
->setText(i18nc("Sort descending", "Descending"));
741 ascending
->setText(i18nc("Sort ascending", "Ascending"));
744 slotSortOrderChanged(m_currentView
->sortOrder());
747 void DolphinViewActionHandler::slotZoomLevelChanged(int current
, int previous
)
751 QAction
*zoomInAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomIn
));
753 zoomInAction
->setEnabled(current
< ZoomLevelInfo::maximumLevel());
756 QAction
*zoomOutAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomOut
));
758 zoomOutAction
->setEnabled(current
> ZoomLevelInfo::minimumLevel());
762 void DolphinViewActionHandler::slotSortTriggered(QAction
*action
)
764 // The radiobuttons of the "Sort By"-menu are split between the main-menu
765 // and several sub-menus. Because of this they don't have a common
766 // action-group that assures an exclusive toggle-state between the main-menu
767 // actions and the sub-menu-actions. If an action gets checked, it must
768 // be assured that all other actions get unchecked, except the ascending/
769 // descending actions
770 for (QAction
*groupAction
: std::as_const(m_sortByActions
)) {
771 KActionMenu
*actionMenu
= qobject_cast
<KActionMenu
*>(groupAction
);
773 const auto actions
= actionMenu
->menu()->actions();
774 for (QAction
*subAction
: actions
) {
775 subAction
->setChecked(false);
777 } else if (groupAction
->actionGroup()) {
778 groupAction
->setChecked(false);
781 action
->setChecked(true);
783 // Apply the activated sort-role to the view
784 const QByteArray role
= action
->data().toByteArray();
785 m_currentView
->setSortRole(role
);
788 void DolphinViewActionHandler::slotAdjustViewProperties()
790 Q_EMIT
actionBeingHandled();
791 QPointer
<ViewPropertiesDialog
> dialog
= new ViewPropertiesDialog(m_currentView
);
796 void DolphinViewActionHandler::slotDuplicate()
798 if (m_currentView
->selectedItemsCount() == 0) {
799 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DuplicateContents
);
801 Q_EMIT
actionBeingHandled();
802 m_currentView
->duplicateSelectedItems();
803 Q_EMIT
selectionModeChangeTriggered(false);
807 void DolphinViewActionHandler::slotProperties()
809 KPropertiesDialog
*dialog
= nullptr;
810 const KFileItemList list
= m_currentView
->selectedItems();
811 if (list
.isEmpty()) {
812 const QUrl url
= m_currentView
->url();
813 dialog
= new KPropertiesDialog(url
, m_currentView
);
815 dialog
= new KPropertiesDialog(list
, m_currentView
);
818 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
821 dialog
->activateWindow();
824 void DolphinViewActionHandler::slotCopyPath()
826 if (m_currentView
->selectedItemsCount() == 0) {
827 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::CopyLocationContents
);
829 m_currentView
->copyPathToClipboard();
830 Q_EMIT
selectionModeChangeTriggered(false);
834 void DolphinViewActionHandler::slotSelectionChanged(const KFileItemList
&selection
)
836 QString basicActionsMenuText
;
837 if (selection
.isEmpty()) {
838 basicActionsMenuText
= i18nc("@action:inmenu menu with actions like copy, paste, rename. The user's selection is empty when this text is shown.",
839 "Actions for Current View");
842 QFontMetrics fontMetrics
= QMenu().fontMetrics();
843 // i18n: @action:inmenu menu with actions like copy, paste, rename.
844 // %1 is a textual representation of the currently selected files or folders. This can be the name of
845 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
846 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
847 // and a fallback will be used.
848 basicActionsMenuText
= i18n("Actions for %1", fileItemListToString(selection
, fontMetrics
.averageCharWidth() * 40, fontMetrics
, ItemsState::Selected
));
852 if (basicActionsMenuText
== QStringLiteral("NULL")) {
853 const KFileItemListProperties
properties(selection
);
854 basicActionsMenuText
= i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
855 "Actions for One Selected Item",
856 "Actions for %1 Selected Items",
860 QAction
*basicActionsMenu
= m_actionCollection
->action(QStringLiteral("basic_actions"));
861 basicActionsMenu
->setText(basicActionsMenuText
);
863 // Add or remove contextual actions
864 while (!basicActionsMenu
->menu()->actions().constLast()->isSeparator()) {
865 basicActionsMenu
->menu()->removeAction(basicActionsMenu
->menu()->actions().last());
867 if (selection
.count() == 1) {
868 if (selection
.first().isLink()) {
869 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("show_target")));
871 if (selection
.first().isDir()) {
872 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("add_to_places")));
877 #include "moc_dolphinviewactionhandler.cpp"