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
);
363 QActionGroup
*DolphinViewActionHandler::createFileItemRolesActionGroup(const QString
&groupPrefix
)
365 const bool isSortGroup
= (groupPrefix
== QLatin1String("sort_by_"));
366 Q_ASSERT(isSortGroup
|| groupPrefix
== QLatin1String("show_"));
368 QActionGroup
*rolesActionGroup
= new QActionGroup(m_actionCollection
);
369 rolesActionGroup
->setExclusive(isSortGroup
);
371 connect(rolesActionGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::slotSortTriggered
);
373 connect(rolesActionGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::toggleVisibleRole
);
377 KActionMenu
*groupMenu
= nullptr;
378 QActionGroup
*groupMenuGroup
= nullptr;
380 bool indexingEnabled
= false;
382 Baloo::IndexerConfig config
;
383 indexingEnabled
= config
.fileIndexingEnabled();
386 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
387 for (const KFileItemModel::RoleInfo
&info
: rolesInfo
) {
388 if (!isSortGroup
&& info
.role
== "text") {
389 // It should not be possible to hide the "text" role
393 KToggleAction
*action
= nullptr;
394 const QString name
= groupPrefix
+ info
.role
;
395 if (info
.group
.isEmpty()) {
396 action
= m_actionCollection
->add
<KToggleAction
>(name
);
397 action
->setActionGroup(rolesActionGroup
);
399 if (!groupMenu
|| info
.group
!= groupName
) {
400 groupName
= info
.group
;
401 groupMenu
= m_actionCollection
->add
<KActionMenu
>(groupName
);
402 groupMenu
->setText(groupName
);
403 groupMenu
->setActionGroup(rolesActionGroup
);
405 groupMenuGroup
= new QActionGroup(groupMenu
);
406 groupMenuGroup
->setExclusive(isSortGroup
);
408 connect(groupMenuGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::slotSortTriggered
);
410 connect(groupMenuGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::toggleVisibleRole
);
414 action
= new KToggleAction(groupMenu
);
415 action
->setActionGroup(groupMenuGroup
);
416 groupMenu
->addAction(action
);
418 action
->setText(info
.translation
);
419 action
->setData(info
.role
);
421 const bool enable
= (!info
.requiresBaloo
&& !info
.requiresIndexer
) || (info
.requiresBaloo
) || (info
.requiresIndexer
&& indexingEnabled
);
422 action
->setEnabled(enable
);
425 m_sortByActions
.insert(info
.role
, action
);
427 m_visibleRoles
.insert(info
.role
, action
);
431 return rolesActionGroup
;
434 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction
*action
)
436 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
437 m_currentView
->setViewMode(mode
);
439 QAction
*viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
440 viewModeMenu
->setIcon(action
->icon());
443 void DolphinViewActionHandler::slotRename()
445 if (m_currentView
->selectedItemsCount() == 0) {
446 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::RenameContents
);
448 Q_EMIT
actionBeingHandled();
449 m_currentView
->renameSelectedItems();
450 // We don't exit selectionMode here because users might want to rename more items.
454 void DolphinViewActionHandler::slotTrashActivated()
456 if (m_currentView
->selectedItemsCount() == 0) {
457 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::MoveToTrashContents
);
459 Q_EMIT
actionBeingHandled();
460 m_currentView
->trashSelectedItems();
461 Q_EMIT
selectionModeChangeTriggered(false);
465 void DolphinViewActionHandler::slotDeleteItems()
467 if (m_currentView
->selectedItemsCount() == 0) {
468 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DeleteContents
);
470 Q_EMIT
actionBeingHandled();
471 m_currentView
->deleteSelectedItems();
472 Q_EMIT
selectionModeChangeTriggered(false);
476 void DolphinViewActionHandler::togglePreview(bool show
)
478 Q_EMIT
actionBeingHandled();
479 m_currentView
->setPreviewsShown(show
);
482 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown
)
485 // It is not enough to update the 'Show Preview' action, also
486 // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
490 QString
DolphinViewActionHandler::currentViewModeActionName() const
492 switch (m_currentView
->viewMode()) {
493 case DolphinView::IconsView
:
494 return QStringLiteral("icons");
495 case DolphinView::DetailsView
:
496 return QStringLiteral("details");
497 case DolphinView::CompactView
:
498 return QStringLiteral("compact");
503 return QString(); // can't happen
506 KActionCollection
*DolphinViewActionHandler::actionCollection()
508 return m_actionCollection
;
511 void DolphinViewActionHandler::updateViewActions()
513 QAction
*viewModeAction
= m_actionCollection
->action(currentViewModeActionName());
514 if (viewModeAction
) {
515 viewModeAction
->setChecked(true);
517 QAction
*viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
518 viewModeMenu
->setIcon(viewModeAction
->icon());
521 QAction
*showPreviewAction
= m_actionCollection
->action(QStringLiteral("show_preview"));
522 showPreviewAction
->setChecked(m_currentView
->previewsShown());
524 slotSortOrderChanged(m_currentView
->sortOrder());
525 slotSortFoldersFirstChanged(m_currentView
->sortFoldersFirst());
526 slotSortHiddenLastChanged(m_currentView
->sortHiddenLast());
527 slotVisibleRolesChanged(m_currentView
->visibleRoles(), QList
<QByteArray
>());
528 slotGroupedSortingChanged(m_currentView
->groupedSorting());
529 slotSortRoleChanged(m_currentView
->sortRole());
530 slotZoomLevelChanged(m_currentView
->zoomLevel(), -1);
532 // Updates the "show_hidden_files" action state and icon
533 slotHiddenFilesShownChanged(m_currentView
->hiddenFilesShown());
536 void DolphinViewActionHandler::zoomIn()
538 const int level
= m_currentView
->zoomLevel();
539 m_currentView
->setZoomLevel(level
+ 1);
543 void DolphinViewActionHandler::zoomOut()
545 const int level
= m_currentView
->zoomLevel();
546 m_currentView
->setZoomLevel(level
- 1);
550 void DolphinViewActionHandler::zoomReset()
552 m_currentView
->resetZoomLevel();
556 void DolphinViewActionHandler::toggleSortFoldersFirst()
558 const bool sortFirst
= m_currentView
->sortFoldersFirst();
559 m_currentView
->setSortFoldersFirst(!sortFirst
);
562 void DolphinViewActionHandler::toggleSortHiddenLast()
564 const bool sortHiddenLast
= m_currentView
->sortHiddenLast();
565 m_currentView
->setSortHiddenLast(!sortHiddenLast
);
568 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order
)
570 QAction
*descending
= m_actionCollection
->action(QStringLiteral("descending"));
571 QAction
*ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
572 const bool sortDescending
= (order
== Qt::DescendingOrder
);
573 descending
->setChecked(sortDescending
);
574 ascending
->setChecked(!sortDescending
);
577 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst
)
579 m_actionCollection
->action(QStringLiteral("folders_first"))->setChecked(foldersFirst
);
582 void DolphinViewActionHandler::slotSortHiddenLastChanged(bool hiddenLast
)
584 m_actionCollection
->action(QStringLiteral("hidden_last"))->setChecked(hiddenLast
);
587 void DolphinViewActionHandler::toggleVisibleRole(QAction
*action
)
589 Q_EMIT
actionBeingHandled();
591 const QByteArray toggledRole
= action
->data().toByteArray();
593 QList
<QByteArray
> roles
= m_currentView
->visibleRoles();
595 const bool show
= action
->isChecked();
597 const int index
= roles
.indexOf(toggledRole
);
598 const bool containsInfo
= (index
>= 0);
599 if (show
&& !containsInfo
) {
600 roles
.append(toggledRole
);
601 m_currentView
->setVisibleRoles(roles
);
602 } else if (!show
&& containsInfo
) {
603 roles
.removeAt(index
);
604 m_currentView
->setVisibleRoles(roles
);
605 Q_ASSERT(roles
.indexOf(toggledRole
) < 0);
609 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList
<QByteArray
> ¤t
, const QList
<QByteArray
> &previous
)
613 const auto checkedRoles
= QSet
<QByteArray
>(current
.constBegin(), current
.constEnd());
614 QHashIterator
<QByteArray
, KToggleAction
*> it(m_visibleRoles
);
615 while (it
.hasNext()) {
617 const QByteArray
&role
= it
.key();
618 KToggleAction
*action
= it
.value();
619 action
->setChecked(checkedRoles
.contains(role
));
623 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped
)
625 m_currentView
->setGroupedSorting(grouped
);
628 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting
)
630 QAction
*showInGroupsAction
= m_actionCollection
->action(QStringLiteral("show_in_groups"));
631 showInGroupsAction
->setChecked(groupedSorting
);
634 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show
)
636 Q_EMIT
actionBeingHandled();
637 m_currentView
->setHiddenFilesShown(show
);
640 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown
)
642 QAction
*showHiddenFilesAction
= m_actionCollection
->action(QStringLiteral("show_hidden_files"));
643 showHiddenFilesAction
->setChecked(shown
);
646 void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable
)
648 const bool supportsMakeDir
= KProtocolManager::supportsMakeDir(currentView()->url());
649 m_actionCollection
->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable
&& supportsMakeDir
);
650 m_actionCollection
->action(QStringLiteral("create_file"))->setEnabled(isFolderWritable
);
653 KToggleAction
*DolphinViewActionHandler::iconsModeAction()
655 KToggleAction
*iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("icons"));
656 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
657 iconsView
->setToolTip(i18nc("@info", "Icons view mode"));
658 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_1
);
659 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
660 iconsView
->setData(QVariant::fromValue(DolphinView::IconsView
));
664 KToggleAction
*DolphinViewActionHandler::compactModeAction()
666 KToggleAction
*iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("compact"));
667 iconsView
->setText(i18nc("@action:inmenu View Mode", "Compact"));
668 iconsView
->setToolTip(i18nc("@info", "Compact view mode"));
669 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_2
);
670 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
671 iconsView
->setData(QVariant::fromValue(DolphinView::CompactView
));
675 KToggleAction
*DolphinViewActionHandler::detailsModeAction()
677 KToggleAction
*detailsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("details"));
678 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
679 detailsView
->setToolTip(i18nc("@info", "Details view mode"));
680 m_actionCollection
->setDefaultShortcut(detailsView
, Qt::CTRL
| Qt::Key_3
);
681 detailsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
682 detailsView
->setData(QVariant::fromValue(DolphinView::DetailsView
));
686 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray
&role
)
688 KToggleAction
*action
= m_sortByActions
.value(role
);
690 action
->setChecked(true);
692 if (!action
->icon().isNull()) {
693 QAction
*sortByMenu
= m_actionCollection
->action(QStringLiteral("sort"));
694 sortByMenu
->setIcon(action
->icon());
698 QAction
*descending
= m_actionCollection
->action(QStringLiteral("descending"));
699 QAction
*ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
701 if (role
== "text" || role
== "type" || role
== "extension" || role
== "tags" || role
== "comment") {
702 descending
->setText(i18nc("Sort descending", "Z-A"));
703 ascending
->setText(i18nc("Sort ascending", "A-Z"));
704 } else if (role
== "size") {
705 descending
->setText(i18nc("Sort descending", "Largest First"));
706 ascending
->setText(i18nc("Sort ascending", "Smallest First"));
707 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
708 descending
->setText(i18nc("Sort descending", "Newest First"));
709 ascending
->setText(i18nc("Sort ascending", "Oldest First"));
710 } else if (role
== "rating") {
711 descending
->setText(i18nc("Sort descending", "Highest First"));
712 ascending
->setText(i18nc("Sort ascending", "Lowest First"));
714 descending
->setText(i18nc("Sort descending", "Descending"));
715 ascending
->setText(i18nc("Sort ascending", "Ascending"));
718 slotSortOrderChanged(m_currentView
->sortOrder());
721 void DolphinViewActionHandler::slotZoomLevelChanged(int current
, int previous
)
725 QAction
*zoomInAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomIn
));
727 zoomInAction
->setEnabled(current
< ZoomLevelInfo::maximumLevel());
730 QAction
*zoomOutAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomOut
));
732 zoomOutAction
->setEnabled(current
> ZoomLevelInfo::minimumLevel());
736 void DolphinViewActionHandler::slotSortTriggered(QAction
*action
)
738 // The radiobuttons of the "Sort By"-menu are split between the main-menu
739 // and several sub-menus. Because of this they don't have a common
740 // action-group that assures an exclusive toggle-state between the main-menu
741 // actions and the sub-menu-actions. If an action gets checked, it must
742 // be assured that all other actions get unchecked, except the ascending/
743 // descending actions
744 for (QAction
*groupAction
: std::as_const(m_sortByActions
)) {
745 KActionMenu
*actionMenu
= qobject_cast
<KActionMenu
*>(groupAction
);
747 const auto actions
= actionMenu
->menu()->actions();
748 for (QAction
*subAction
: actions
) {
749 subAction
->setChecked(false);
751 } else if (groupAction
->actionGroup()) {
752 groupAction
->setChecked(false);
755 action
->setChecked(true);
757 // Apply the activated sort-role to the view
758 const QByteArray role
= action
->data().toByteArray();
759 m_currentView
->setSortRole(role
);
762 void DolphinViewActionHandler::slotAdjustViewProperties()
764 Q_EMIT
actionBeingHandled();
765 QPointer
<ViewPropertiesDialog
> dialog
= new ViewPropertiesDialog(m_currentView
);
770 void DolphinViewActionHandler::slotDuplicate()
772 if (m_currentView
->selectedItemsCount() == 0) {
773 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DuplicateContents
);
775 Q_EMIT
actionBeingHandled();
776 m_currentView
->duplicateSelectedItems();
777 Q_EMIT
selectionModeChangeTriggered(false);
781 void DolphinViewActionHandler::slotProperties()
783 KPropertiesDialog
*dialog
= nullptr;
784 const KFileItemList list
= m_currentView
->selectedItems();
785 if (list
.isEmpty()) {
786 const QUrl url
= m_currentView
->url();
787 dialog
= new KPropertiesDialog(url
, m_currentView
);
789 dialog
= new KPropertiesDialog(list
, m_currentView
);
792 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
795 dialog
->activateWindow();
798 void DolphinViewActionHandler::slotCopyPath()
800 if (m_currentView
->selectedItemsCount() == 0) {
801 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::CopyLocationContents
);
803 m_currentView
->copyPathToClipboard();
804 Q_EMIT
selectionModeChangeTriggered(false);
808 void DolphinViewActionHandler::slotSelectionChanged(const KFileItemList
&selection
)
810 QString basicActionsMenuText
;
811 if (selection
.isEmpty()) {
812 basicActionsMenuText
= i18nc("@action:inmenu menu with actions like copy, paste, rename. The user's selection is empty when this text is shown.",
813 "Actions for Current View");
816 QFontMetrics fontMetrics
= QMenu().fontMetrics();
817 // i18n: @action:inmenu menu with actions like copy, paste, rename.
818 // %1 is a textual representation of the currently selected files or folders. This can be the name of
819 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
820 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
821 // and a fallback will be used.
822 basicActionsMenuText
= i18n("Actions for %1", fileItemListToString(selection
, fontMetrics
.averageCharWidth() * 40, fontMetrics
, ItemsState::Selected
));
826 if (basicActionsMenuText
== QStringLiteral("NULL")) {
827 const KFileItemListProperties
properties(selection
);
828 basicActionsMenuText
= i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
829 "Actions for One Selected Item",
830 "Actions for %1 Selected Items",
834 QAction
*basicActionsMenu
= m_actionCollection
->action(QStringLiteral("basic_actions"));
835 basicActionsMenu
->setText(basicActionsMenuText
);
837 // Add or remove contextual actions
838 while (!basicActionsMenu
->menu()->actions().constLast()->isSeparator()) {
839 basicActionsMenu
->menu()->removeAction(basicActionsMenu
->menu()->actions().last());
841 if (selection
.count() == 1) {
842 if (selection
.first().isLink()) {
843 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("show_target")));
845 if (selection
.first().isDir()) {
846 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("add_to_places")));
851 #include "moc_dolphinviewactionhandler.cpp"