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
);
89 auto renameAction
= KStandardAction::renameFile(this, &DolphinViewActionHandler::slotRename
, m_actionCollection
);
90 renameAction
->setWhatsThis(xi18nc("@info:whatsthis",
92 "items in your current selection.<nl/>Renaming multiple items "
93 "at once amounts to their new names differing only in a number."));
95 auto trashAction
= KStandardAction::moveToTrash(this, &DolphinViewActionHandler::slotTrashActivated
, m_actionCollection
);
96 auto trashShortcuts
= trashAction
->shortcuts();
97 trashAction
->setAutoRepeat(false);
98 if (!trashShortcuts
.contains(QKeySequence::Delete
)) {
99 trashShortcuts
.append(QKeySequence::Delete
);
100 m_actionCollection
->setDefaultShortcuts(trashAction
, trashShortcuts
);
102 trashAction
->setWhatsThis(xi18nc("@info:whatsthis",
104 "items in your current selection to the <filename>Trash"
105 "</filename>.<nl/>The trash is a temporary storage where "
106 "items can be deleted from if disk space is needed."));
108 auto deleteAction
= KStandardAction::deleteFile(this, &DolphinViewActionHandler::slotDeleteItems
, m_actionCollection
);
109 auto deleteShortcuts
= deleteAction
->shortcuts();
110 deleteAction
->setAutoRepeat(false);
111 if (!deleteShortcuts
.contains(Qt::SHIFT
| Qt::Key_Delete
)) {
112 deleteShortcuts
.append(Qt::SHIFT
| Qt::Key_Delete
);
113 m_actionCollection
->setDefaultShortcuts(deleteAction
, deleteShortcuts
);
115 deleteAction
->setWhatsThis(xi18nc("@info:whatsthis",
117 "the items in your current selection completely. They can "
118 "not be recovered by normal means."));
120 // This action is useful for being enabled when KStandardAction::MoveToTrash should be
121 // disabled and KStandardAction::DeleteFile is enabled (e.g. non-local files), so that Key_Del
122 // can be used for deleting the file (#76016). It needs to be a separate action
123 // so that the Edit menu isn't affected.
124 QAction
*deleteWithTrashShortcut
= m_actionCollection
->addAction(QStringLiteral("delete_shortcut"));
125 // The descriptive text is just for the shortcuts editor.
126 deleteWithTrashShortcut
->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
127 m_actionCollection
->setDefaultShortcuts(deleteWithTrashShortcut
, KStandardShortcut::moveToTrash());
128 deleteWithTrashShortcut
->setEnabled(false);
129 connect(deleteWithTrashShortcut
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDeleteItems
);
131 QAction
*duplicateAction
= m_actionCollection
->addAction(QStringLiteral("duplicate"));
132 duplicateAction
->setText(i18nc("@action:inmenu File", "Duplicate Here"));
133 duplicateAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-duplicate")));
134 m_actionCollection
->setDefaultShortcut(duplicateAction
, Qt::CTRL
| Qt::Key_D
);
135 duplicateAction
->setEnabled(false);
136 connect(duplicateAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDuplicate
);
138 QAction
*propertiesAction
= m_actionCollection
->addAction(QStringLiteral("properties"));
139 // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
140 propertiesAction
->setText(i18nc("@action:inmenu File", "Properties"));
141 propertiesAction
->setWhatsThis(xi18nc("@info:whatsthis properties",
142 "This shows a complete list of properties of the currently "
143 "selected items in a new window.<nl/>If nothing is selected the "
144 "window will be about the currently viewed folder instead.<nl/>"
145 "You can configure advanced options there like managing "
146 "read- and write-permissions."));
147 propertiesAction
->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
148 m_actionCollection
->setDefaultShortcuts(propertiesAction
, {Qt::ALT
| Qt::Key_Return
, Qt::ALT
| Qt::Key_Enter
});
149 connect(propertiesAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotProperties
);
151 QAction
*copyPathAction
= m_actionCollection
->addAction(QStringLiteral("copy_location"));
152 copyPathAction
->setText(i18nc("@action:incontextmenu", "Copy Location"));
153 copyPathAction
->setWhatsThis(i18nc("@info:whatsthis copy_location", "This will copy the path of the first selected item into the clipboard."));
155 copyPathAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy-path")));
156 m_actionCollection
->setDefaultShortcuts(copyPathAction
, {Qt::CTRL
| Qt::ALT
| Qt::Key_C
});
157 connect(copyPathAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotCopyPath
);
159 if (actionTextHelper
) {
160 // The "…" at the end make clear that they won't trigger their respective actions directly.
161 actionTextHelper
->registerTextWhenNothingIsSelected(trashAction
, i18nc("@action:inmenu File", "Move to Trash…"));
162 actionTextHelper
->registerTextWhenNothingIsSelected(deleteAction
, i18nc("@action:inmenu File", "Delete…"));
163 actionTextHelper
->registerTextWhenNothingIsSelected(duplicateAction
, i18nc("@action:inmenu File", "Duplicate Here…"));
164 actionTextHelper
->registerTextWhenNothingIsSelected(copyPathAction
, i18nc("@action:incontextmenu", "Copy Location…"));
167 // This menu makes sure that users who don't know how to open a context menu and haven't
168 // figured out how to enable the menu bar can still perform basic file manipulation.
169 // This only works if they know how to select a file.
170 // The text when nothing is selected at least implies that a selection can /somehow/ be made.
171 // This menu is by default only used in the hamburger menu but created here so users can put
172 // it on their toolbar.
173 KActionMenu
*basicActionsMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("basic_actions"), this);
174 // The text is set later depending on the selection in the currently active view.
175 basicActionsMenu
->setPopupMode(QToolButton::InstantPopup
);
176 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Cut
)));
177 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Copy
)));
178 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Paste
)));
179 basicActionsMenu
->addSeparator();
180 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
181 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
182 basicActionsMenu
->addSeparator();
183 basicActionsMenu
->addAction(m_actionCollection
->action(QStringLiteral("properties")));
184 basicActionsMenu
->addSeparator(); // We add one more separator because we sometimes add contextual
185 // actions in slotSelectionChanged() after the static ones above.
188 KToggleAction
*iconsAction
= iconsModeAction();
189 KToggleAction
*compactAction
= compactModeAction();
190 KToggleAction
*detailsAction
= detailsModeAction();
192 iconsAction
->setWhatsThis(xi18nc("@info:whatsthis Icons view mode",
193 "<para>This switches to a view mode that focuses on the folder "
194 "and file icons. This mode makes it easy to distinguish folders "
195 "from files and to detect items with distinctive <emphasis>"
196 "file types</emphasis>.</para><para> This mode is handy to "
197 "browse through pictures when the <interface>Preview"
198 "</interface> option is enabled.</para>"));
199 compactAction
->setWhatsThis(xi18nc("@info:whatsthis Compact view mode",
200 "<para>This switches to a compact view mode that lists the folders "
201 "and files in columns with the names beside the icons.</para><para>"
202 "This helps to keep the overview in folders with many items.</para>"));
203 detailsAction
->setWhatsThis(xi18nc("@info:whatsthis Details view mode",
204 "<para>This switches to a list view mode that focuses on folder "
205 "and file details.</para><para>Click on a detail in the column "
206 "header to sort the items by it. Click again to sort the other "
207 "way around. To select which details should be displayed click "
208 "the header with the right mouse button.</para><para>You can "
209 "view the contents of a folder without leaving the current "
210 "location by clicking to the left of it. This way you can view "
211 "the contents of multiple folders in the same list.</para>"));
213 KSelectAction
*viewModeActions
= m_actionCollection
->add
<KSelectAction
>(QStringLiteral("view_mode"));
214 viewModeActions
->setText(i18nc("@action:intoolbar", "View Mode"));
215 viewModeActions
->addAction(iconsAction
);
216 viewModeActions
->addAction(compactAction
);
217 viewModeActions
->addAction(detailsAction
);
218 viewModeActions
->setToolBarMode(KSelectAction::MenuMode
);
219 connect(viewModeActions
, &KSelectAction::actionTriggered
, this, &DolphinViewActionHandler::slotViewModeActionTriggered
);
221 QAction
*zoomInAction
= KStandardAction::zoomIn(this, &DolphinViewActionHandler::zoomIn
, m_actionCollection
);
222 zoomInAction
->setWhatsThis(i18nc("@info:whatsthis zoom in", "This increases the icon size."));
224 QAction
*zoomResetAction
= m_actionCollection
->addAction(QStringLiteral("view_zoom_reset"));
225 zoomResetAction
->setText(i18nc("@action:inmenu View", "Reset Zoom Level"));
226 zoomResetAction
->setToolTip(i18n("Zoom To Default"));
227 zoomResetAction
->setWhatsThis(i18nc("@info:whatsthis zoom reset", "This resets the icon size to default."));
228 zoomResetAction
->setIcon(QIcon::fromTheme(QStringLiteral("zoom-original")));
229 m_actionCollection
->setDefaultShortcuts(zoomResetAction
, {Qt::CTRL
| Qt::Key_0
});
230 connect(zoomResetAction
, &QAction::triggered
, this, &DolphinViewActionHandler::zoomReset
);
232 QAction
*zoomOutAction
= KStandardAction::zoomOut(this, &DolphinViewActionHandler::zoomOut
, m_actionCollection
);
233 zoomOutAction
->setWhatsThis(i18nc("@info:whatsthis zoom out", "This reduces the icon size."));
235 KActionMenu
*zoomMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("zoom"));
236 zoomMenu
->setText(i18nc("@action:inmenu menu of zoom actions", "Zoom"));
237 zoomMenu
->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
238 zoomMenu
->setPopupMode(QToolButton::InstantPopup
);
239 zoomMenu
->addAction(zoomInAction
);
240 zoomMenu
->addAction(zoomResetAction
);
241 zoomMenu
->addAction(zoomOutAction
);
243 KToggleAction
*showPreview
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_preview"));
244 showPreview
->setText(i18nc("@action:intoolbar", "Show Previews"));
245 showPreview
->setToolTip(i18nc("@info", "Show preview of files and folders"));
246 showPreview
->setWhatsThis(xi18nc("@info:whatsthis",
248 "enabled, the icons are based on the actual file or folder "
249 "contents.<nl/>For example the icons of images become scaled "
250 "down versions of the images."));
251 showPreview
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
252 m_actionCollection
->setDefaultShortcut(showPreview
, QKeySequence(Qt::Key_F12
));
253 connect(showPreview
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::togglePreview
);
255 KToggleAction
*sortFoldersFirst
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("folders_first"));
256 sortFoldersFirst
->setText(i18nc("@action:inmenu Sort", "Folders First"));
257 connect(sortFoldersFirst
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleSortFoldersFirst
);
259 KToggleAction
*sortHiddenLast
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("hidden_last"));
260 sortHiddenLast
->setText(i18nc("@action:inmenu Sort", "Hidden Files Last"));
261 connect(sortHiddenLast
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleSortHiddenLast
);
264 QActionGroup
*sortByActionGroup
= createFileItemRolesActionGroup(QStringLiteral("sort_by_"));
266 KActionMenu
*sortByActionMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("sort"));
267 sortByActionMenu
->setIcon(QIcon::fromTheme(QStringLiteral("view-sort")));
268 sortByActionMenu
->setText(i18nc("@action:inmenu View", "Sort By"));
269 sortByActionMenu
->setPopupMode(QToolButton::InstantPopup
);
271 const auto sortByActionGroupActions
= sortByActionGroup
->actions();
272 for (QAction
*action
: sortByActionGroupActions
) {
273 sortByActionMenu
->addAction(action
);
276 sortByActionMenu
->addSeparator();
278 QActionGroup
*group
= new QActionGroup(sortByActionMenu
);
279 group
->setExclusive(true);
281 KToggleAction
*ascendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("ascending"));
282 ascendingAction
->setActionGroup(group
);
283 connect(ascendingAction
, &QAction::triggered
, this, [this] {
284 m_currentView
->setSortOrder(Qt::AscendingOrder
);
287 KToggleAction
*descendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("descending"));
288 descendingAction
->setActionGroup(group
);
289 connect(descendingAction
, &QAction::triggered
, this, [this] {
290 m_currentView
->setSortOrder(Qt::DescendingOrder
);
293 sortByActionMenu
->addAction(ascendingAction
);
294 sortByActionMenu
->addAction(descendingAction
);
295 sortByActionMenu
->addSeparator();
296 sortByActionMenu
->addAction(sortFoldersFirst
);
297 sortByActionMenu
->addAction(sortHiddenLast
);
299 // View -> Additional Information
300 QActionGroup
*visibleRolesGroup
= createFileItemRolesActionGroup(QStringLiteral("show_"));
302 KActionMenu
*visibleRolesMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("additional_info"));
303 visibleRolesMenu
->setText(i18nc("@action:inmenu View", "Show Additional Information"));
304 visibleRolesMenu
->setIcon(QIcon::fromTheme(QStringLiteral("documentinfo")));
305 visibleRolesMenu
->setPopupMode(QToolButton::InstantPopup
);
307 const auto visibleRolesGroupActions
= visibleRolesGroup
->actions();
308 for (QAction
*action
: visibleRolesGroupActions
) {
309 visibleRolesMenu
->addAction(action
);
312 KToggleAction
*showInGroups
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_in_groups"));
313 showInGroups
->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
314 showInGroups
->setText(i18nc("@action:inmenu View", "Show in Groups"));
315 showInGroups
->setWhatsThis(i18nc("@info:whatsthis", "This groups files and folders by their first letter."));
316 connect(showInGroups
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleGroupedSorting
);
318 KToggleAction
*showHiddenFiles
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_hidden_files"));
319 showHiddenFiles
->setIcon(QIcon::fromTheme(QStringLiteral("view-visible")));
320 showHiddenFiles
->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
321 showHiddenFiles
->setWhatsThis(xi18nc("@info:whatsthis",
323 "this is enabled <emphasis>hidden</emphasis> files and folders "
324 "are visible. They will be displayed semi-transparent.</para>"
325 "<para>Hidden items only differ from other ones in that their "
326 "name starts with a \".\". In general there is no need for "
327 "users to access them which is why they are hidden.</para>"));
328 m_actionCollection
->setDefaultShortcuts(showHiddenFiles
, KStandardShortcut::showHideHiddenFiles());
329 connect(showHiddenFiles
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleShowHiddenFiles
);
331 QAction
*adjustViewProps
= m_actionCollection
->addAction(QStringLiteral("view_properties"));
332 adjustViewProps
->setText(i18nc("@action:inmenu View", "Adjust View Display Style…"));
333 adjustViewProps
->setIcon(QIcon::fromTheme(QStringLiteral("view-choose")));
334 adjustViewProps
->setWhatsThis(i18nc("@info:whatsthis",
335 "This opens a window "
336 "in which all folder view properties can be adjusted."));
337 connect(adjustViewProps
, &QAction::triggered
, this, &DolphinViewActionHandler::slotAdjustViewProperties
);
340 QActionGroup
*DolphinViewActionHandler::createFileItemRolesActionGroup(const QString
&groupPrefix
)
342 const bool isSortGroup
= (groupPrefix
== QLatin1String("sort_by_"));
343 Q_ASSERT(isSortGroup
|| groupPrefix
== QLatin1String("show_"));
345 QActionGroup
*rolesActionGroup
= new QActionGroup(m_actionCollection
);
346 rolesActionGroup
->setExclusive(isSortGroup
);
348 connect(rolesActionGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::slotSortTriggered
);
350 connect(rolesActionGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::toggleVisibleRole
);
354 KActionMenu
*groupMenu
= nullptr;
355 QActionGroup
*groupMenuGroup
= nullptr;
357 bool indexingEnabled
= false;
359 Baloo::IndexerConfig config
;
360 indexingEnabled
= config
.fileIndexingEnabled();
363 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
364 for (const KFileItemModel::RoleInfo
&info
: rolesInfo
) {
365 if (!isSortGroup
&& info
.role
== "text") {
366 // It should not be possible to hide the "text" role
370 KToggleAction
*action
= nullptr;
371 const QString name
= groupPrefix
+ info
.role
;
372 if (info
.group
.isEmpty()) {
373 action
= m_actionCollection
->add
<KToggleAction
>(name
);
374 action
->setActionGroup(rolesActionGroup
);
376 if (!groupMenu
|| info
.group
!= groupName
) {
377 groupName
= info
.group
;
378 groupMenu
= m_actionCollection
->add
<KActionMenu
>(groupName
);
379 groupMenu
->setText(groupName
);
380 groupMenu
->setActionGroup(rolesActionGroup
);
382 groupMenuGroup
= new QActionGroup(groupMenu
);
383 groupMenuGroup
->setExclusive(isSortGroup
);
385 connect(groupMenuGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::slotSortTriggered
);
387 connect(groupMenuGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::toggleVisibleRole
);
391 action
= new KToggleAction(groupMenu
);
392 action
->setActionGroup(groupMenuGroup
);
393 groupMenu
->addAction(action
);
395 action
->setText(info
.translation
);
396 action
->setData(info
.role
);
398 const bool enable
= (!info
.requiresBaloo
&& !info
.requiresIndexer
) || (info
.requiresBaloo
) || (info
.requiresIndexer
&& indexingEnabled
);
399 action
->setEnabled(enable
);
402 m_sortByActions
.insert(info
.role
, action
);
404 m_visibleRoles
.insert(info
.role
, action
);
408 return rolesActionGroup
;
411 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction
*action
)
413 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
414 m_currentView
->setViewMode(mode
);
416 QAction
*viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
417 viewModeMenu
->setIcon(action
->icon());
420 void DolphinViewActionHandler::slotRename()
422 if (m_currentView
->selectedItemsCount() == 0) {
423 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::RenameContents
);
425 Q_EMIT
actionBeingHandled();
426 m_currentView
->renameSelectedItems();
427 // We don't exit selectionMode here because users might want to rename more items.
431 void DolphinViewActionHandler::slotTrashActivated()
433 if (m_currentView
->selectedItemsCount() == 0) {
434 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::MoveToTrashContents
);
436 Q_EMIT
actionBeingHandled();
437 m_currentView
->trashSelectedItems();
438 Q_EMIT
selectionModeChangeTriggered(false);
442 void DolphinViewActionHandler::slotDeleteItems()
444 if (m_currentView
->selectedItemsCount() == 0) {
445 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DeleteContents
);
447 Q_EMIT
actionBeingHandled();
448 m_currentView
->deleteSelectedItems();
449 Q_EMIT
selectionModeChangeTriggered(false);
453 void DolphinViewActionHandler::togglePreview(bool show
)
455 Q_EMIT
actionBeingHandled();
456 m_currentView
->setPreviewsShown(show
);
459 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown
)
462 // It is not enough to update the 'Show Preview' action, also
463 // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
467 QString
DolphinViewActionHandler::currentViewModeActionName() const
469 switch (m_currentView
->viewMode()) {
470 case DolphinView::IconsView
:
471 return QStringLiteral("icons");
472 case DolphinView::DetailsView
:
473 return QStringLiteral("details");
474 case DolphinView::CompactView
:
475 return QStringLiteral("compact");
480 return QString(); // can't happen
483 KActionCollection
*DolphinViewActionHandler::actionCollection()
485 return m_actionCollection
;
488 void DolphinViewActionHandler::updateViewActions()
490 QAction
*viewModeAction
= m_actionCollection
->action(currentViewModeActionName());
491 if (viewModeAction
) {
492 viewModeAction
->setChecked(true);
494 QAction
*viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
495 viewModeMenu
->setIcon(viewModeAction
->icon());
498 QAction
*showPreviewAction
= m_actionCollection
->action(QStringLiteral("show_preview"));
499 showPreviewAction
->setChecked(m_currentView
->previewsShown());
501 slotSortOrderChanged(m_currentView
->sortOrder());
502 slotSortFoldersFirstChanged(m_currentView
->sortFoldersFirst());
503 slotSortHiddenLastChanged(m_currentView
->sortHiddenLast());
504 slotVisibleRolesChanged(m_currentView
->visibleRoles(), QList
<QByteArray
>());
505 slotGroupedSortingChanged(m_currentView
->groupedSorting());
506 slotSortRoleChanged(m_currentView
->sortRole());
507 slotZoomLevelChanged(m_currentView
->zoomLevel(), -1);
509 // Updates the "show_hidden_files" action state and icon
510 slotHiddenFilesShownChanged(m_currentView
->hiddenFilesShown());
513 void DolphinViewActionHandler::zoomIn()
515 const int level
= m_currentView
->zoomLevel();
516 m_currentView
->setZoomLevel(level
+ 1);
520 void DolphinViewActionHandler::zoomOut()
522 const int level
= m_currentView
->zoomLevel();
523 m_currentView
->setZoomLevel(level
- 1);
527 void DolphinViewActionHandler::zoomReset()
529 m_currentView
->resetZoomLevel();
533 void DolphinViewActionHandler::toggleSortFoldersFirst()
535 const bool sortFirst
= m_currentView
->sortFoldersFirst();
536 m_currentView
->setSortFoldersFirst(!sortFirst
);
539 void DolphinViewActionHandler::toggleSortHiddenLast()
541 const bool sortHiddenLast
= m_currentView
->sortHiddenLast();
542 m_currentView
->setSortHiddenLast(!sortHiddenLast
);
545 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order
)
547 QAction
*descending
= m_actionCollection
->action(QStringLiteral("descending"));
548 QAction
*ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
549 const bool sortDescending
= (order
== Qt::DescendingOrder
);
550 descending
->setChecked(sortDescending
);
551 ascending
->setChecked(!sortDescending
);
554 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst
)
556 m_actionCollection
->action(QStringLiteral("folders_first"))->setChecked(foldersFirst
);
559 void DolphinViewActionHandler::slotSortHiddenLastChanged(bool hiddenLast
)
561 m_actionCollection
->action(QStringLiteral("hidden_last"))->setChecked(hiddenLast
);
564 void DolphinViewActionHandler::toggleVisibleRole(QAction
*action
)
566 Q_EMIT
actionBeingHandled();
568 const QByteArray toggledRole
= action
->data().toByteArray();
570 QList
<QByteArray
> roles
= m_currentView
->visibleRoles();
572 const bool show
= action
->isChecked();
574 const int index
= roles
.indexOf(toggledRole
);
575 const bool containsInfo
= (index
>= 0);
576 if (show
&& !containsInfo
) {
577 roles
.append(toggledRole
);
578 m_currentView
->setVisibleRoles(roles
);
579 } else if (!show
&& containsInfo
) {
580 roles
.removeAt(index
);
581 m_currentView
->setVisibleRoles(roles
);
582 Q_ASSERT(roles
.indexOf(toggledRole
) < 0);
586 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList
<QByteArray
> ¤t
, const QList
<QByteArray
> &previous
)
590 const auto checkedRoles
= QSet
<QByteArray
>(current
.constBegin(), current
.constEnd());
591 QHashIterator
<QByteArray
, KToggleAction
*> it(m_visibleRoles
);
592 while (it
.hasNext()) {
594 const QByteArray
&role
= it
.key();
595 KToggleAction
*action
= it
.value();
596 action
->setChecked(checkedRoles
.contains(role
));
600 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped
)
602 m_currentView
->setGroupedSorting(grouped
);
605 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting
)
607 QAction
*showInGroupsAction
= m_actionCollection
->action(QStringLiteral("show_in_groups"));
608 showInGroupsAction
->setChecked(groupedSorting
);
611 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show
)
613 Q_EMIT
actionBeingHandled();
614 m_currentView
->setHiddenFilesShown(show
);
617 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown
)
619 QAction
*showHiddenFilesAction
= m_actionCollection
->action(QStringLiteral("show_hidden_files"));
620 showHiddenFilesAction
->setChecked(shown
);
623 void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable
)
625 m_actionCollection
->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable
&& KProtocolManager::supportsMakeDir(currentView()->url()));
628 KToggleAction
*DolphinViewActionHandler::iconsModeAction()
630 KToggleAction
*iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("icons"));
631 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
632 iconsView
->setToolTip(i18nc("@info", "Icons view mode"));
633 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_1
);
634 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
635 iconsView
->setData(QVariant::fromValue(DolphinView::IconsView
));
639 KToggleAction
*DolphinViewActionHandler::compactModeAction()
641 KToggleAction
*iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("compact"));
642 iconsView
->setText(i18nc("@action:inmenu View Mode", "Compact"));
643 iconsView
->setToolTip(i18nc("@info", "Compact view mode"));
644 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_2
);
645 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
646 iconsView
->setData(QVariant::fromValue(DolphinView::CompactView
));
650 KToggleAction
*DolphinViewActionHandler::detailsModeAction()
652 KToggleAction
*detailsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("details"));
653 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
654 detailsView
->setToolTip(i18nc("@info", "Details view mode"));
655 m_actionCollection
->setDefaultShortcut(detailsView
, Qt::CTRL
| Qt::Key_3
);
656 detailsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
657 detailsView
->setData(QVariant::fromValue(DolphinView::DetailsView
));
661 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray
&role
)
663 KToggleAction
*action
= m_sortByActions
.value(role
);
665 action
->setChecked(true);
667 if (!action
->icon().isNull()) {
668 QAction
*sortByMenu
= m_actionCollection
->action(QStringLiteral("sort"));
669 sortByMenu
->setIcon(action
->icon());
673 QAction
*descending
= m_actionCollection
->action(QStringLiteral("descending"));
674 QAction
*ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
676 if (role
== "text" || role
== "type" || role
== "extension" || role
== "tags" || role
== "comment") {
677 descending
->setText(i18nc("Sort descending", "Z-A"));
678 ascending
->setText(i18nc("Sort ascending", "A-Z"));
679 } else if (role
== "size") {
680 descending
->setText(i18nc("Sort descending", "Largest First"));
681 ascending
->setText(i18nc("Sort ascending", "Smallest First"));
682 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
683 descending
->setText(i18nc("Sort descending", "Newest First"));
684 ascending
->setText(i18nc("Sort ascending", "Oldest First"));
685 } else if (role
== "rating") {
686 descending
->setText(i18nc("Sort descending", "Highest First"));
687 ascending
->setText(i18nc("Sort ascending", "Lowest First"));
689 descending
->setText(i18nc("Sort descending", "Descending"));
690 ascending
->setText(i18nc("Sort ascending", "Ascending"));
693 slotSortOrderChanged(m_currentView
->sortOrder());
696 void DolphinViewActionHandler::slotZoomLevelChanged(int current
, int previous
)
700 QAction
*zoomInAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomIn
));
702 zoomInAction
->setEnabled(current
< ZoomLevelInfo::maximumLevel());
705 QAction
*zoomOutAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomOut
));
707 zoomOutAction
->setEnabled(current
> ZoomLevelInfo::minimumLevel());
711 void DolphinViewActionHandler::slotSortTriggered(QAction
*action
)
713 // The radiobuttons of the "Sort By"-menu are split between the main-menu
714 // and several sub-menus. Because of this they don't have a common
715 // action-group that assures an exclusive toggle-state between the main-menu
716 // actions and the sub-menu-actions. If an action gets checked, it must
717 // be assured that all other actions get unchecked, except the ascending/
718 // descending actions
719 for (QAction
*groupAction
: std::as_const(m_sortByActions
)) {
720 KActionMenu
*actionMenu
= qobject_cast
<KActionMenu
*>(groupAction
);
722 const auto actions
= actionMenu
->menu()->actions();
723 for (QAction
*subAction
: actions
) {
724 subAction
->setChecked(false);
726 } else if (groupAction
->actionGroup()) {
727 groupAction
->setChecked(false);
730 action
->setChecked(true);
732 // Apply the activated sort-role to the view
733 const QByteArray role
= action
->data().toByteArray();
734 m_currentView
->setSortRole(role
);
737 void DolphinViewActionHandler::slotAdjustViewProperties()
739 Q_EMIT
actionBeingHandled();
740 QPointer
<ViewPropertiesDialog
> dialog
= new ViewPropertiesDialog(m_currentView
);
745 void DolphinViewActionHandler::slotDuplicate()
747 if (m_currentView
->selectedItemsCount() == 0) {
748 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DuplicateContents
);
750 Q_EMIT
actionBeingHandled();
751 m_currentView
->duplicateSelectedItems();
752 Q_EMIT
selectionModeChangeTriggered(false);
756 void DolphinViewActionHandler::slotProperties()
758 KPropertiesDialog
*dialog
= nullptr;
759 const KFileItemList list
= m_currentView
->selectedItems();
760 if (list
.isEmpty()) {
761 const QUrl url
= m_currentView
->url();
762 dialog
= new KPropertiesDialog(url
, m_currentView
);
764 dialog
= new KPropertiesDialog(list
, m_currentView
);
767 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
770 dialog
->activateWindow();
773 void DolphinViewActionHandler::slotCopyPath()
775 if (m_currentView
->selectedItemsCount() == 0) {
776 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::CopyLocationContents
);
778 m_currentView
->copyPathToClipboard();
779 Q_EMIT
selectionModeChangeTriggered(false);
783 void DolphinViewActionHandler::slotSelectionChanged(const KFileItemList
&selection
)
785 QString basicActionsMenuText
;
786 if (selection
.isEmpty()) {
787 basicActionsMenuText
= i18nc("@action:inmenu menu with actions like copy, paste, rename. The user's selection is empty when this text is shown.",
788 "Actions for Current View");
791 QFontMetrics fontMetrics
= QMenu().fontMetrics();
792 // i18n: @action:inmenu menu with actions like copy, paste, rename.
793 // %1 is a textual representation of the currently selected files or folders. This can be the name of
794 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
795 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
796 // and a fallback will be used.
797 basicActionsMenuText
= i18n("Actions for %1", fileItemListToString(selection
, fontMetrics
.averageCharWidth() * 40, fontMetrics
, ItemsState::Selected
));
801 if (basicActionsMenuText
== QStringLiteral("NULL")) {
802 const KFileItemListProperties
properties(selection
);
803 basicActionsMenuText
= i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
804 "Actions for One Selected Item",
805 "Actions for %1 Selected Items",
809 QAction
*basicActionsMenu
= m_actionCollection
->action(QStringLiteral("basic_actions"));
810 basicActionsMenu
->setText(basicActionsMenuText
);
812 // Add or remove contextual actions
813 while (!basicActionsMenu
->menu()->actions().constLast()->isSeparator()) {
814 basicActionsMenu
->menu()->removeAction(basicActionsMenu
->menu()->actions().last());
816 if (selection
.count() == 1) {
817 if (selection
.first().isLink()) {
818 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("show_target")));
820 if (selection
.first().isDir()) {
821 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("add_to_places")));
826 #include "moc_dolphinviewactionhandler.cpp"