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 connect(view
, &DolphinView::selectionModeChangeRequested
, this, [this](bool enabled
) {
64 Q_EMIT
selectionModeChangeTriggered(enabled
);
66 connect(view
, &DolphinView::selectionChanged
, this, &DolphinViewActionHandler::slotSelectionChanged
);
67 slotSelectionChanged(m_currentView
->selectedItems());
70 DolphinView
*DolphinViewActionHandler::currentView()
75 void DolphinViewActionHandler::createActions(SelectionMode::ActionTextHelper
*actionTextHelper
)
77 // This action doesn't appear in the GUI, it's for the shortcut only.
78 // KNewFileMenu takes care of the GUI stuff.
79 QAction
*newDirAction
= m_actionCollection
->addAction(QStringLiteral("create_dir"));
80 newDirAction
->setText(i18nc("@action", "Create Folder..."));
81 m_actionCollection
->setDefaultShortcuts(newDirAction
, KStandardShortcut::createFolder());
82 newDirAction
->setIcon(QIcon::fromTheme(QStringLiteral("folder-new")));
83 newDirAction
->setEnabled(false); // Will be enabled in slotWriteStateChanged(bool) if the current URL is writable
84 connect(newDirAction
, &QAction::triggered
, this, &DolphinViewActionHandler::createDirectoryTriggered
);
88 auto renameAction
= KStandardAction::renameFile(this, &DolphinViewActionHandler::slotRename
, m_actionCollection
);
89 renameAction
->setWhatsThis(xi18nc("@info:whatsthis",
91 "items in your current selection.<nl/>Renaming multiple items "
92 "at once amounts to their new names differing only in a number."));
94 auto trashAction
= KStandardAction::moveToTrash(this, &DolphinViewActionHandler::slotTrashActivated
, m_actionCollection
);
95 auto trashShortcuts
= trashAction
->shortcuts();
96 if (!trashShortcuts
.contains(QKeySequence::Delete
)) {
97 trashShortcuts
.append(QKeySequence::Delete
);
98 m_actionCollection
->setDefaultShortcuts(trashAction
, trashShortcuts
);
100 trashAction
->setWhatsThis(xi18nc("@info:whatsthis",
102 "items in your current selection to the <filename>Trash"
103 "</filename>.<nl/>The trash is a temporary storage where "
104 "items can be deleted from if disk space is needed."));
106 auto deleteAction
= KStandardAction::deleteFile(this, &DolphinViewActionHandler::slotDeleteItems
, m_actionCollection
);
107 auto deleteShortcuts
= deleteAction
->shortcuts();
108 if (!deleteShortcuts
.contains(Qt::SHIFT
| Qt::Key_Delete
)) {
109 deleteShortcuts
.append(Qt::SHIFT
| Qt::Key_Delete
);
110 m_actionCollection
->setDefaultShortcuts(deleteAction
, deleteShortcuts
);
112 deleteAction
->setWhatsThis(xi18nc("@info:whatsthis",
114 "the items in your current selection completely. They can "
115 "not be recovered by normal means."));
117 // This action is useful for being enabled when KStandardAction::MoveToTrash should be
118 // disabled and KStandardAction::DeleteFile is enabled (e.g. non-local files), so that Key_Del
119 // can be used for deleting the file (#76016). It needs to be a separate action
120 // so that the Edit menu isn't affected.
121 QAction
*deleteWithTrashShortcut
= m_actionCollection
->addAction(QStringLiteral("delete_shortcut"));
122 // The descriptive text is just for the shortcuts editor.
123 deleteWithTrashShortcut
->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
124 m_actionCollection
->setDefaultShortcuts(deleteWithTrashShortcut
, KStandardShortcut::moveToTrash());
125 deleteWithTrashShortcut
->setEnabled(false);
126 connect(deleteWithTrashShortcut
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDeleteItems
);
128 QAction
*duplicateAction
= m_actionCollection
->addAction(QStringLiteral("duplicate"));
129 duplicateAction
->setText(i18nc("@action:inmenu File", "Duplicate Here"));
130 duplicateAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-duplicate")));
131 m_actionCollection
->setDefaultShortcut(duplicateAction
, Qt::CTRL
| Qt::Key_D
);
132 duplicateAction
->setEnabled(false);
133 connect(duplicateAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDuplicate
);
135 QAction
*propertiesAction
= m_actionCollection
->addAction(QStringLiteral("properties"));
136 // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
137 propertiesAction
->setText(i18nc("@action:inmenu File", "Properties"));
138 propertiesAction
->setWhatsThis(xi18nc("@info:whatsthis properties",
139 "This shows a complete list of properties of the currently "
140 "selected items in a new window.<nl/>If nothing is selected the "
141 "window will be about the currently viewed folder instead.<nl/>"
142 "You can configure advanced options there like managing "
143 "read- and write-permissions."));
144 propertiesAction
->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
145 m_actionCollection
->setDefaultShortcuts(propertiesAction
, {Qt::ALT
| Qt::Key_Return
, Qt::ALT
| Qt::Key_Enter
});
146 connect(propertiesAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotProperties
);
148 QAction
*copyPathAction
= m_actionCollection
->addAction(QStringLiteral("copy_location"));
149 copyPathAction
->setText(i18nc("@action:incontextmenu", "Copy Location"));
150 copyPathAction
->setWhatsThis(i18nc("@info:whatsthis copy_location", "This will copy the path of the first selected item into the clipboard."));
152 copyPathAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy-path")));
153 m_actionCollection
->setDefaultShortcuts(copyPathAction
, {Qt::CTRL
| Qt::ALT
| Qt::Key_C
});
154 connect(copyPathAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotCopyPath
);
156 if (actionTextHelper
) {
157 // The "…" at the end make clear that they won't trigger their respective actions directly.
158 actionTextHelper
->registerTextWhenNothingIsSelected(trashAction
, i18nc("@action:inmenu File", "Move to Trash…"));
159 actionTextHelper
->registerTextWhenNothingIsSelected(deleteAction
, i18nc("@action:inmenu File", "Delete…"));
160 actionTextHelper
->registerTextWhenNothingIsSelected(duplicateAction
, i18nc("@action:inmenu File", "Duplicate Here…"));
161 actionTextHelper
->registerTextWhenNothingIsSelected(copyPathAction
, i18nc("@action:incontextmenu", "Copy Location…"));
164 // This menu makes sure that users who don't know how to open a context menu and haven't
165 // figured out how to enable the menu bar can still perform basic file manipulation.
166 // This only works if they know how to select a file.
167 // The text when nothing is selected at least implies that a selection can /somehow/ be made.
168 // This menu is by default only used in the hamburger menu but created here so users can put
169 // it on their toolbar.
170 KActionMenu
*basicActionsMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("basic_actions"), this);
171 // The text is set later depending on the selection in the currently active view.
172 basicActionsMenu
->setPopupMode(QToolButton::InstantPopup
);
173 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Cut
)));
174 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Copy
)));
175 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Paste
)));
176 basicActionsMenu
->addSeparator();
177 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
178 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
179 basicActionsMenu
->addSeparator();
180 basicActionsMenu
->addAction(m_actionCollection
->action(QStringLiteral("properties")));
181 basicActionsMenu
->addSeparator(); // We add one more separator because we sometimes add contextual
182 // actions in slotSelectionChanged() after the static ones above.
185 KToggleAction
*iconsAction
= iconsModeAction();
186 KToggleAction
*compactAction
= compactModeAction();
187 KToggleAction
*detailsAction
= detailsModeAction();
189 iconsAction
->setWhatsThis(xi18nc("@info:whatsthis Icons view mode",
190 "<para>This switches to a view mode that focuses on the folder "
191 "and file icons. This mode makes it easy to distinguish folders "
192 "from files and to detect items with distinctive <emphasis>"
193 "file types</emphasis>.</para><para> This mode is handy to "
194 "browse through pictures when the <interface>Preview"
195 "</interface> option is enabled.</para>"));
196 compactAction
->setWhatsThis(xi18nc("@info:whatsthis Compact view mode",
197 "<para>This switches to a compact view mode that lists the folders "
198 "and files in columns with the names beside the icons.</para><para>"
199 "This helps to keep the overview in folders with many items.</para>"));
200 detailsAction
->setWhatsThis(xi18nc("@info:whatsthis Details view mode",
201 "<para>This switches to a list view mode that focuses on folder "
202 "and file details.</para><para>Click on a detail in the column "
203 "header to sort the items by it. Click again to sort the other "
204 "way around. To select which details should be displayed click "
205 "the header with the right mouse button.</para><para>You can "
206 "view the contents of a folder without leaving the current "
207 "location by clicking to the left of it. This way you can view "
208 "the contents of multiple folders in the same list.</para>"));
210 KSelectAction
*viewModeActions
= m_actionCollection
->add
<KSelectAction
>(QStringLiteral("view_mode"));
211 viewModeActions
->setText(i18nc("@action:intoolbar", "View Mode"));
212 viewModeActions
->addAction(iconsAction
);
213 viewModeActions
->addAction(compactAction
);
214 viewModeActions
->addAction(detailsAction
);
215 viewModeActions
->setToolBarMode(KSelectAction::MenuMode
);
216 connect(viewModeActions
, &KSelectAction::triggered
, this, &DolphinViewActionHandler::slotViewModeActionTriggered
);
218 QAction
*zoomInAction
= KStandardAction::zoomIn(this, &DolphinViewActionHandler::zoomIn
, m_actionCollection
);
219 zoomInAction
->setWhatsThis(i18nc("@info:whatsthis zoom in", "This increases the icon size."));
221 QAction
*zoomResetAction
= m_actionCollection
->addAction(QStringLiteral("view_zoom_reset"));
222 zoomResetAction
->setText(i18nc("@action:inmenu View", "Reset Zoom Level"));
223 zoomResetAction
->setToolTip(i18n("Zoom To Default"));
224 zoomResetAction
->setWhatsThis(i18nc("@info:whatsthis zoom reset", "This resets the icon size to default."));
225 zoomResetAction
->setIcon(QIcon::fromTheme(QStringLiteral("zoom-original")));
226 m_actionCollection
->setDefaultShortcuts(zoomResetAction
, {Qt::CTRL
| Qt::Key_0
});
227 connect(zoomResetAction
, &QAction::triggered
, this, &DolphinViewActionHandler::zoomReset
);
229 QAction
*zoomOutAction
= KStandardAction::zoomOut(this, &DolphinViewActionHandler::zoomOut
, m_actionCollection
);
230 zoomOutAction
->setWhatsThis(i18nc("@info:whatsthis zoom out", "This reduces the icon size."));
232 KActionMenu
*zoomMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("zoom"));
233 zoomMenu
->setText(i18nc("@action:inmenu menu of zoom actions", "Zoom"));
234 zoomMenu
->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
235 zoomMenu
->setPopupMode(QToolButton::InstantPopup
);
236 zoomMenu
->addAction(zoomInAction
);
237 zoomMenu
->addAction(zoomResetAction
);
238 zoomMenu
->addAction(zoomOutAction
);
240 KToggleAction
*showPreview
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_preview"));
241 showPreview
->setText(i18nc("@action:intoolbar", "Show Previews"));
242 showPreview
->setToolTip(i18nc("@info", "Show preview of files and folders"));
243 showPreview
->setWhatsThis(xi18nc("@info:whatsthis",
245 "enabled, the icons are based on the actual file or folder "
246 "contents.<nl/>For example the icons of images become scaled "
247 "down versions of the images."));
248 showPreview
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
249 connect(showPreview
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::togglePreview
);
251 KToggleAction
*sortFoldersFirst
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("folders_first"));
252 sortFoldersFirst
->setText(i18nc("@action:inmenu Sort", "Folders First"));
253 connect(sortFoldersFirst
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleSortFoldersFirst
);
255 KToggleAction
*sortHiddenLast
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("hidden_last"));
256 sortHiddenLast
->setText(i18nc("@action:inmenu Sort", "Hidden Files Last"));
257 connect(sortHiddenLast
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleSortHiddenLast
);
260 QActionGroup
*sortByActionGroup
= createFileItemRolesActionGroup(QStringLiteral("sort_by_"));
262 KActionMenu
*sortByActionMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("sort"));
263 sortByActionMenu
->setIcon(QIcon::fromTheme(QStringLiteral("view-sort")));
264 sortByActionMenu
->setText(i18nc("@action:inmenu View", "Sort By"));
265 sortByActionMenu
->setPopupMode(QToolButton::InstantPopup
);
267 const auto sortByActionGroupActions
= sortByActionGroup
->actions();
268 for (QAction
*action
: sortByActionGroupActions
) {
269 sortByActionMenu
->addAction(action
);
272 sortByActionMenu
->addSeparator();
274 QActionGroup
*group
= new QActionGroup(sortByActionMenu
);
275 group
->setExclusive(true);
277 KToggleAction
*ascendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("ascending"));
278 ascendingAction
->setActionGroup(group
);
279 connect(ascendingAction
, &QAction::triggered
, this, [this] {
280 m_currentView
->setSortOrder(Qt::AscendingOrder
);
283 KToggleAction
*descendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("descending"));
284 descendingAction
->setActionGroup(group
);
285 connect(descendingAction
, &QAction::triggered
, this, [this] {
286 m_currentView
->setSortOrder(Qt::DescendingOrder
);
289 sortByActionMenu
->addAction(ascendingAction
);
290 sortByActionMenu
->addAction(descendingAction
);
291 sortByActionMenu
->addSeparator();
292 sortByActionMenu
->addAction(sortFoldersFirst
);
293 sortByActionMenu
->addAction(sortHiddenLast
);
295 // View -> Additional Information
296 QActionGroup
*visibleRolesGroup
= createFileItemRolesActionGroup(QStringLiteral("show_"));
298 KActionMenu
*visibleRolesMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("additional_info"));
299 visibleRolesMenu
->setText(i18nc("@action:inmenu View", "Show Additional Information"));
300 visibleRolesMenu
->setIcon(QIcon::fromTheme(QStringLiteral("documentinfo")));
301 visibleRolesMenu
->setPopupMode(QToolButton::InstantPopup
);
303 const auto visibleRolesGroupActions
= visibleRolesGroup
->actions();
304 for (QAction
*action
: visibleRolesGroupActions
) {
305 visibleRolesMenu
->addAction(action
);
308 KToggleAction
*showInGroups
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_in_groups"));
309 showInGroups
->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
310 showInGroups
->setText(i18nc("@action:inmenu View", "Show in Groups"));
311 showInGroups
->setWhatsThis(i18nc("@info:whatsthis", "This groups files and folders by their first letter."));
312 connect(showInGroups
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleGroupedSorting
);
314 KToggleAction
*showHiddenFiles
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_hidden_files"));
315 showHiddenFiles
->setIcon(QIcon::fromTheme(QStringLiteral("view-visible")));
316 showHiddenFiles
->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
317 showHiddenFiles
->setWhatsThis(xi18nc("@info:whatsthis",
319 "this is enabled <emphasis>hidden</emphasis> files and folders "
320 "are visible. They will be displayed semi-transparent.</para>"
321 "<para>Hidden items only differ from other ones in that their "
322 "name starts with a \".\". In general there is no need for "
323 "users to access them which is why they are hidden.</para>"));
324 m_actionCollection
->setDefaultShortcuts(showHiddenFiles
, KStandardShortcut::showHideHiddenFiles());
325 connect(showHiddenFiles
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleShowHiddenFiles
);
327 QAction
*adjustViewProps
= m_actionCollection
->addAction(QStringLiteral("view_properties"));
328 adjustViewProps
->setText(i18nc("@action:inmenu View", "Adjust View Display Style..."));
329 adjustViewProps
->setIcon(QIcon::fromTheme(QStringLiteral("view-choose")));
330 adjustViewProps
->setWhatsThis(i18nc("@info:whatsthis",
331 "This opens a window "
332 "in which all folder view properties can be adjusted."));
333 connect(adjustViewProps
, &QAction::triggered
, this, &DolphinViewActionHandler::slotAdjustViewProperties
);
336 QActionGroup
*DolphinViewActionHandler::createFileItemRolesActionGroup(const QString
&groupPrefix
)
338 const bool isSortGroup
= (groupPrefix
== QLatin1String("sort_by_"));
339 Q_ASSERT(isSortGroup
|| groupPrefix
== QLatin1String("show_"));
341 QActionGroup
*rolesActionGroup
= new QActionGroup(m_actionCollection
);
342 rolesActionGroup
->setExclusive(isSortGroup
);
344 connect(rolesActionGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::slotSortTriggered
);
346 connect(rolesActionGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::toggleVisibleRole
);
350 KActionMenu
*groupMenu
= nullptr;
351 QActionGroup
*groupMenuGroup
= nullptr;
353 bool indexingEnabled
= false;
355 Baloo::IndexerConfig config
;
356 indexingEnabled
= config
.fileIndexingEnabled();
359 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
360 for (const KFileItemModel::RoleInfo
&info
: rolesInfo
) {
361 if (!isSortGroup
&& info
.role
== "text") {
362 // It should not be possible to hide the "text" role
366 KToggleAction
*action
= nullptr;
367 const QString name
= groupPrefix
+ info
.role
;
368 if (info
.group
.isEmpty()) {
369 action
= m_actionCollection
->add
<KToggleAction
>(name
);
370 action
->setActionGroup(rolesActionGroup
);
372 if (!groupMenu
|| info
.group
!= groupName
) {
373 groupName
= info
.group
;
374 groupMenu
= m_actionCollection
->add
<KActionMenu
>(groupName
);
375 groupMenu
->setText(groupName
);
376 groupMenu
->setActionGroup(rolesActionGroup
);
378 groupMenuGroup
= new QActionGroup(groupMenu
);
379 groupMenuGroup
->setExclusive(isSortGroup
);
381 connect(groupMenuGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::slotSortTriggered
);
383 connect(groupMenuGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::toggleVisibleRole
);
387 action
= new KToggleAction(groupMenu
);
388 action
->setActionGroup(groupMenuGroup
);
389 groupMenu
->addAction(action
);
391 action
->setText(info
.translation
);
392 action
->setData(info
.role
);
394 const bool enable
= (!info
.requiresBaloo
&& !info
.requiresIndexer
) || (info
.requiresBaloo
) || (info
.requiresIndexer
&& indexingEnabled
);
395 action
->setEnabled(enable
);
398 m_sortByActions
.insert(info
.role
, action
);
400 m_visibleRoles
.insert(info
.role
, action
);
404 return rolesActionGroup
;
407 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction
*action
)
409 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
410 m_currentView
->setViewMode(mode
);
412 QAction
*viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
413 viewModeMenu
->setIcon(action
->icon());
416 void DolphinViewActionHandler::slotRename()
418 if (m_currentView
->selectedItemsCount() == 0) {
419 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::RenameContents
);
421 Q_EMIT
actionBeingHandled();
422 m_currentView
->renameSelectedItems();
423 // We don't exit selectionMode here because users might want to rename more items.
427 void DolphinViewActionHandler::slotTrashActivated()
429 if (m_currentView
->selectedItemsCount() == 0) {
430 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::MoveToTrashContents
);
432 Q_EMIT
actionBeingHandled();
433 m_currentView
->trashSelectedItems();
434 Q_EMIT
selectionModeChangeTriggered(false);
438 void DolphinViewActionHandler::slotDeleteItems()
440 if (m_currentView
->selectedItemsCount() == 0) {
441 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DeleteContents
);
443 Q_EMIT
actionBeingHandled();
444 m_currentView
->deleteSelectedItems();
445 Q_EMIT
selectionModeChangeTriggered(false);
449 void DolphinViewActionHandler::togglePreview(bool show
)
451 Q_EMIT
actionBeingHandled();
452 m_currentView
->setPreviewsShown(show
);
455 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown
)
458 // It is not enough to update the 'Show Preview' action, also
459 // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
463 QString
DolphinViewActionHandler::currentViewModeActionName() const
465 switch (m_currentView
->viewMode()) {
466 case DolphinView::IconsView
:
467 return QStringLiteral("icons");
468 case DolphinView::DetailsView
:
469 return QStringLiteral("details");
470 case DolphinView::CompactView
:
471 return QStringLiteral("compact");
476 return QString(); // can't happen
479 KActionCollection
*DolphinViewActionHandler::actionCollection()
481 return m_actionCollection
;
484 void DolphinViewActionHandler::updateViewActions()
486 QAction
*viewModeAction
= m_actionCollection
->action(currentViewModeActionName());
487 if (viewModeAction
) {
488 viewModeAction
->setChecked(true);
490 QAction
*viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
491 viewModeMenu
->setIcon(viewModeAction
->icon());
494 QAction
*showPreviewAction
= m_actionCollection
->action(QStringLiteral("show_preview"));
495 showPreviewAction
->setChecked(m_currentView
->previewsShown());
497 slotSortOrderChanged(m_currentView
->sortOrder());
498 slotSortFoldersFirstChanged(m_currentView
->sortFoldersFirst());
499 slotSortHiddenLastChanged(m_currentView
->sortHiddenLast());
500 slotVisibleRolesChanged(m_currentView
->visibleRoles(), QList
<QByteArray
>());
501 slotGroupedSortingChanged(m_currentView
->groupedSorting());
502 slotSortRoleChanged(m_currentView
->sortRole());
503 slotZoomLevelChanged(m_currentView
->zoomLevel(), -1);
505 // Updates the "show_hidden_files" action state and icon
506 slotHiddenFilesShownChanged(m_currentView
->hiddenFilesShown());
509 void DolphinViewActionHandler::zoomIn()
511 const int level
= m_currentView
->zoomLevel();
512 m_currentView
->setZoomLevel(level
+ 1);
516 void DolphinViewActionHandler::zoomOut()
518 const int level
= m_currentView
->zoomLevel();
519 m_currentView
->setZoomLevel(level
- 1);
523 void DolphinViewActionHandler::zoomReset()
525 m_currentView
->resetZoomLevel();
529 void DolphinViewActionHandler::toggleSortFoldersFirst()
531 const bool sortFirst
= m_currentView
->sortFoldersFirst();
532 m_currentView
->setSortFoldersFirst(!sortFirst
);
535 void DolphinViewActionHandler::toggleSortHiddenLast()
537 const bool sortHiddenLast
= m_currentView
->sortHiddenLast();
538 m_currentView
->setSortHiddenLast(!sortHiddenLast
);
541 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order
)
543 QAction
*descending
= m_actionCollection
->action(QStringLiteral("descending"));
544 QAction
*ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
545 const bool sortDescending
= (order
== Qt::DescendingOrder
);
546 descending
->setChecked(sortDescending
);
547 ascending
->setChecked(!sortDescending
);
550 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst
)
552 m_actionCollection
->action(QStringLiteral("folders_first"))->setChecked(foldersFirst
);
555 void DolphinViewActionHandler::slotSortHiddenLastChanged(bool hiddenLast
)
557 m_actionCollection
->action(QStringLiteral("hidden_last"))->setChecked(hiddenLast
);
560 void DolphinViewActionHandler::toggleVisibleRole(QAction
*action
)
562 Q_EMIT
actionBeingHandled();
564 const QByteArray toggledRole
= action
->data().toByteArray();
566 QList
<QByteArray
> roles
= m_currentView
->visibleRoles();
568 const bool show
= action
->isChecked();
570 const int index
= roles
.indexOf(toggledRole
);
571 const bool containsInfo
= (index
>= 0);
572 if (show
&& !containsInfo
) {
573 roles
.append(toggledRole
);
574 m_currentView
->setVisibleRoles(roles
);
575 } else if (!show
&& containsInfo
) {
576 roles
.removeAt(index
);
577 m_currentView
->setVisibleRoles(roles
);
578 Q_ASSERT(roles
.indexOf(toggledRole
) < 0);
582 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList
<QByteArray
> ¤t
, const QList
<QByteArray
> &previous
)
586 const auto checkedRoles
= QSet
<QByteArray
>(current
.constBegin(), current
.constEnd());
587 QHashIterator
<QByteArray
, KToggleAction
*> it(m_visibleRoles
);
588 while (it
.hasNext()) {
590 const QByteArray
&role
= it
.key();
591 KToggleAction
*action
= it
.value();
592 action
->setChecked(checkedRoles
.contains(role
));
596 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped
)
598 m_currentView
->setGroupedSorting(grouped
);
601 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting
)
603 QAction
*showInGroupsAction
= m_actionCollection
->action(QStringLiteral("show_in_groups"));
604 showInGroupsAction
->setChecked(groupedSorting
);
607 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show
)
609 Q_EMIT
actionBeingHandled();
610 m_currentView
->setHiddenFilesShown(show
);
613 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown
)
615 QAction
*showHiddenFilesAction
= m_actionCollection
->action(QStringLiteral("show_hidden_files"));
616 showHiddenFilesAction
->setChecked(shown
);
619 void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable
)
621 m_actionCollection
->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable
&& KProtocolManager::supportsMakeDir(currentView()->url()));
624 KToggleAction
*DolphinViewActionHandler::iconsModeAction()
626 KToggleAction
*iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("icons"));
627 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
628 iconsView
->setToolTip(i18nc("@info", "Icons view mode"));
629 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_1
);
630 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
631 iconsView
->setData(QVariant::fromValue(DolphinView::IconsView
));
635 KToggleAction
*DolphinViewActionHandler::compactModeAction()
637 KToggleAction
*iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("compact"));
638 iconsView
->setText(i18nc("@action:inmenu View Mode", "Compact"));
639 iconsView
->setToolTip(i18nc("@info", "Compact view mode"));
640 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_2
);
641 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
642 iconsView
->setData(QVariant::fromValue(DolphinView::CompactView
));
646 KToggleAction
*DolphinViewActionHandler::detailsModeAction()
648 KToggleAction
*detailsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("details"));
649 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
650 detailsView
->setToolTip(i18nc("@info", "Details view mode"));
651 m_actionCollection
->setDefaultShortcut(detailsView
, Qt::CTRL
| Qt::Key_3
);
652 detailsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
653 detailsView
->setData(QVariant::fromValue(DolphinView::DetailsView
));
657 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray
&role
)
659 KToggleAction
*action
= m_sortByActions
.value(role
);
661 action
->setChecked(true);
663 if (!action
->icon().isNull()) {
664 QAction
*sortByMenu
= m_actionCollection
->action(QStringLiteral("sort"));
665 sortByMenu
->setIcon(action
->icon());
669 QAction
*descending
= m_actionCollection
->action(QStringLiteral("descending"));
670 QAction
*ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
672 if (role
== "text" || role
== "type" || role
== "extension" || role
== "tags" || role
== "comment") {
673 descending
->setText(i18nc("Sort descending", "Z-A"));
674 ascending
->setText(i18nc("Sort ascending", "A-Z"));
675 } else if (role
== "size") {
676 descending
->setText(i18nc("Sort descending", "Largest First"));
677 ascending
->setText(i18nc("Sort ascending", "Smallest First"));
678 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
679 descending
->setText(i18nc("Sort descending", "Newest First"));
680 ascending
->setText(i18nc("Sort ascending", "Oldest First"));
681 } else if (role
== "rating") {
682 descending
->setText(i18nc("Sort descending", "Highest First"));
683 ascending
->setText(i18nc("Sort ascending", "Lowest First"));
685 descending
->setText(i18nc("Sort descending", "Descending"));
686 ascending
->setText(i18nc("Sort ascending", "Ascending"));
689 slotSortOrderChanged(m_currentView
->sortOrder());
692 void DolphinViewActionHandler::slotZoomLevelChanged(int current
, int previous
)
696 QAction
*zoomInAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomIn
));
698 zoomInAction
->setEnabled(current
< ZoomLevelInfo::maximumLevel());
701 QAction
*zoomOutAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomOut
));
703 zoomOutAction
->setEnabled(current
> ZoomLevelInfo::minimumLevel());
707 void DolphinViewActionHandler::slotSortTriggered(QAction
*action
)
709 // The radiobuttons of the "Sort By"-menu are split between the main-menu
710 // and several sub-menus. Because of this they don't have a common
711 // action-group that assures an exclusive toggle-state between the main-menu
712 // actions and the sub-menu-actions. If an action gets checked, it must
713 // be assured that all other actions get unchecked, except the ascending/
714 // descending actions
715 for (QAction
*groupAction
: qAsConst(m_sortByActions
)) {
716 KActionMenu
*actionMenu
= qobject_cast
<KActionMenu
*>(groupAction
);
718 const auto actions
= actionMenu
->menu()->actions();
719 for (QAction
*subAction
: actions
) {
720 subAction
->setChecked(false);
722 } else if (groupAction
->actionGroup()) {
723 groupAction
->setChecked(false);
726 action
->setChecked(true);
728 // Apply the activated sort-role to the view
729 const QByteArray role
= action
->data().toByteArray();
730 m_currentView
->setSortRole(role
);
733 void DolphinViewActionHandler::slotAdjustViewProperties()
735 Q_EMIT
actionBeingHandled();
736 QPointer
<ViewPropertiesDialog
> dialog
= new ViewPropertiesDialog(m_currentView
);
741 void DolphinViewActionHandler::slotDuplicate()
743 if (m_currentView
->selectedItemsCount() == 0) {
744 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DuplicateContents
);
746 Q_EMIT
actionBeingHandled();
747 m_currentView
->duplicateSelectedItems();
748 Q_EMIT
selectionModeChangeTriggered(false);
752 void DolphinViewActionHandler::slotProperties()
754 KPropertiesDialog
*dialog
= nullptr;
755 const KFileItemList list
= m_currentView
->selectedItems();
756 if (list
.isEmpty()) {
757 const QUrl url
= m_currentView
->url();
758 dialog
= new KPropertiesDialog(url
, m_currentView
);
760 dialog
= new KPropertiesDialog(list
, m_currentView
);
763 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
766 dialog
->activateWindow();
769 void DolphinViewActionHandler::slotCopyPath()
771 if (m_currentView
->selectedItemsCount() == 0) {
772 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::CopyLocationContents
);
774 m_currentView
->copyPathToClipboard();
775 Q_EMIT
selectionModeChangeTriggered(false);
779 void DolphinViewActionHandler::slotSelectionChanged(const KFileItemList
&selection
)
781 QString basicActionsMenuText
;
782 if (selection
.isEmpty()) {
783 basicActionsMenuText
= i18nc("@action:inmenu menu with actions like copy, paste, rename. The user's selection is empty when this text is shown.",
784 "Actions for Current View");
787 QFontMetrics fontMetrics
= QMenu().fontMetrics();
788 // i18n: @action:inmenu menu with actions like copy, paste, rename.
789 // %1 is a textual representation of the currently selected files or folders. This can be the name of
790 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
791 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
792 // and a fallback will be used.
793 basicActionsMenuText
= i18n("Actions for %1", fileItemListToString(selection
, fontMetrics
.averageCharWidth() * 40, fontMetrics
, ItemsState::Selected
));
797 if (basicActionsMenuText
== QStringLiteral("NULL")) {
798 const KFileItemListProperties
properties(selection
);
799 basicActionsMenuText
= i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
800 "Actions for One Selected Item",
801 "Actions for %1 Selected Items",
805 QAction
*basicActionsMenu
= m_actionCollection
->action(QStringLiteral("basic_actions"));
806 basicActionsMenu
->setText(basicActionsMenuText
);
808 // Add or remove contextual actions
809 while (!basicActionsMenu
->menu()->actions().constLast()->isSeparator()) {
810 basicActionsMenu
->menu()->removeAction(basicActionsMenu
->menu()->actions().last());
812 if (selection
.count() == 1) {
813 if (selection
.first().isLink()) {
814 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("show_target")));
816 if (selection
.first().isDir()) {
817 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("add_to_places")));