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 "dolphindebug.h"
11 #include "kitemviews/kfileitemlisttostring.h"
12 #include "kitemviews/kfileitemmodel.h"
13 #include "settings/viewpropertiesdialog.h"
14 #include "views/zoomlevelinfo.h"
15 #include "kconfig_version.h"
18 #include <Baloo/IndexerConfig>
20 #include <KActionCollection>
21 #include <KActionMenu>
22 #include <KFileItemListProperties>
23 #include <KLocalizedString>
24 #include <KNewFileMenu>
25 #include <KPropertiesDialog>
26 #include <KProtocolManager>
28 #include <QActionGroup>
32 DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection
* collection
, QObject
* parent
) :
34 m_actionCollection(collection
),
35 m_currentView(nullptr),
39 Q_ASSERT(m_actionCollection
);
43 void DolphinViewActionHandler::setCurrentView(DolphinView
* view
)
48 disconnect(m_currentView
, nullptr, this, nullptr);
53 connect(view
, &DolphinView::modeChanged
,
54 this, &DolphinViewActionHandler::updateViewActions
);
55 connect(view
, &DolphinView::previewsShownChanged
,
56 this, &DolphinViewActionHandler::slotPreviewsShownChanged
);
57 connect(view
, &DolphinView::sortOrderChanged
,
58 this, &DolphinViewActionHandler::slotSortOrderChanged
);
59 connect(view
, &DolphinView::sortFoldersFirstChanged
,
60 this, &DolphinViewActionHandler::slotSortFoldersFirstChanged
);
61 connect(view
, &DolphinView::sortHiddenLastChanged
,
62 this, &DolphinViewActionHandler::slotSortHiddenLastChanged
);
63 connect(view
, &DolphinView::visibleRolesChanged
,
64 this, &DolphinViewActionHandler::slotVisibleRolesChanged
);
65 connect(view
, &DolphinView::groupedSortingChanged
,
66 this, &DolphinViewActionHandler::slotGroupedSortingChanged
);
67 connect(view
, &DolphinView::hiddenFilesShownChanged
,
68 this, &DolphinViewActionHandler::slotHiddenFilesShownChanged
);
69 connect(view
, &DolphinView::sortRoleChanged
,
70 this, &DolphinViewActionHandler::slotSortRoleChanged
);
71 connect(view
, &DolphinView::zoomLevelChanged
,
72 this, &DolphinViewActionHandler::slotZoomLevelChanged
);
73 connect(view
, &DolphinView::writeStateChanged
,
74 this, &DolphinViewActionHandler::slotWriteStateChanged
);
75 connect(view
, &DolphinView::selectionModeRequested
,
76 this, [this]() { Q_EMIT
setSelectionMode(true); });
77 connect(view
, &DolphinView::selectionChanged
,
78 this, &DolphinViewActionHandler::slotSelectionChanged
);
79 slotSelectionChanged(m_currentView
->selectedItems());
82 DolphinView
* DolphinViewActionHandler::currentView()
87 void DolphinViewActionHandler::createActions()
89 // This action doesn't appear in the GUI, it's for the shortcut only.
90 // KNewFileMenu takes care of the GUI stuff.
91 QAction
* newDirAction
= m_actionCollection
->addAction(QStringLiteral("create_dir"));
92 newDirAction
->setText(i18nc("@action", "Create Folder..."));
93 m_actionCollection
->setDefaultShortcuts(newDirAction
, KStandardShortcut::createFolder());
94 newDirAction
->setIcon(QIcon::fromTheme(QStringLiteral("folder-new")));
95 newDirAction
->setEnabled(false); // Will be enabled in slotWriteStateChanged(bool) if the current URL is writable
96 connect(newDirAction
, &QAction::triggered
, this, &DolphinViewActionHandler::createDirectoryTriggered
);
100 auto renameAction
= KStandardAction::renameFile(this, &DolphinViewActionHandler::slotRename
, m_actionCollection
);
101 renameAction
->setWhatsThis(xi18nc("@info:whatsthis", "This renames the "
102 "items in your current selection.<nl/>Renaming multiple items "
103 "at once amounts to their new names differing only in a number."));
105 auto trashAction
= KStandardAction::moveToTrash(this, &DolphinViewActionHandler::slotTrashActivated
, m_actionCollection
);
106 auto trashShortcuts
= trashAction
->shortcuts();
107 if (!trashShortcuts
.contains(QKeySequence::Delete
)) {
108 trashShortcuts
.append(QKeySequence::Delete
);
109 m_actionCollection
->setDefaultShortcuts(trashAction
, trashShortcuts
);
111 trashAction
->setWhatsThis(xi18nc("@info:whatsthis", "This moves the "
112 "items in your current selection to the <filename>Trash"
113 "</filename>.<nl/>The trash is a temporary storage where "
114 "items can be deleted from if disk space is needed."));
116 auto deleteAction
= KStandardAction::deleteFile(this, &DolphinViewActionHandler::slotDeleteItems
, m_actionCollection
);
117 auto deleteShortcuts
= deleteAction
->shortcuts();
118 if (!deleteShortcuts
.contains(Qt::SHIFT
| Qt::Key_Delete
)) {
119 deleteShortcuts
.append(Qt::SHIFT
| Qt::Key_Delete
);
120 m_actionCollection
->setDefaultShortcuts(deleteAction
, deleteShortcuts
);
122 deleteAction
->setWhatsThis(xi18nc("@info:whatsthis", "This deletes "
123 "the items in your current selection completely. They can "
124 "not 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",
160 "This will copy the path of the first selected item into the clipboard."
163 copyPathAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy-path")));
164 m_actionCollection
->setDefaultShortcuts(copyPathAction
, {Qt::CTRL
| Qt::ALT
| Qt::Key_C
});
165 connect(copyPathAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotCopyPath
);
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::triggered
, this, &DolphinViewActionHandler::slotViewModeActionTriggered
);
221 QAction
* zoomInAction
= KStandardAction::zoomIn(this,
222 &DolphinViewActionHandler::zoomIn
,
224 zoomInAction
->setWhatsThis(i18nc("@info:whatsthis zoom in", "This increases the icon size."));
226 QAction
* zoomResetAction
= m_actionCollection
->addAction(QStringLiteral("view_zoom_reset"));
227 zoomResetAction
->setText(i18nc("@action:inmenu View", "Reset Zoom Level"));
228 zoomResetAction
->setToolTip(i18n("Zoom To Default"));
229 zoomResetAction
->setWhatsThis(i18nc("@info:whatsthis zoom reset", "This resets the icon size to default."));
230 zoomResetAction
->setIcon(QIcon::fromTheme(QStringLiteral("zoom-original")));
231 m_actionCollection
->setDefaultShortcuts(zoomResetAction
, {Qt::CTRL
| Qt::Key_0
});
232 connect(zoomResetAction
, &QAction::triggered
, this, &DolphinViewActionHandler::zoomReset
);
234 QAction
* zoomOutAction
= KStandardAction::zoomOut(this,
235 &DolphinViewActionHandler::zoomOut
,
237 zoomOutAction
->setWhatsThis(i18nc("@info:whatsthis zoom out", "This reduces the icon size."));
239 KActionMenu
* zoomMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("zoom"));
240 zoomMenu
->setText(i18nc("@action:inmenu menu of zoom actions", "Zoom"));
241 zoomMenu
->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
242 zoomMenu
->setPopupMode(QToolButton::InstantPopup
);
243 zoomMenu
->addAction(zoomInAction
);
244 zoomMenu
->addAction(zoomResetAction
);
245 zoomMenu
->addAction(zoomOutAction
);
247 KToggleAction
* showPreview
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_preview"));
248 showPreview
->setText(i18nc("@action:intoolbar", "Show Previews"));
249 showPreview
->setToolTip(i18nc("@info", "Show preview of files and folders"));
250 showPreview
->setWhatsThis(xi18nc("@info:whatsthis", "When this is "
251 "enabled, the icons are based on the actual file or folder "
252 "contents.<nl/>For example the icons of images become scaled "
253 "down versions of the images."));
254 showPreview
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
255 connect(showPreview
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::togglePreview
);
257 KToggleAction
* sortFoldersFirst
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("folders_first"));
258 sortFoldersFirst
->setText(i18nc("@action:inmenu Sort", "Folders First"));
259 connect(sortFoldersFirst
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleSortFoldersFirst
);
261 KToggleAction
* sortHiddenLast
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("hidden_last"));
262 sortHiddenLast
->setText(i18nc("@action:inmenu Sort", "Hidden Files Last"));
263 connect(sortHiddenLast
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleSortHiddenLast
);
266 QActionGroup
* sortByActionGroup
= createFileItemRolesActionGroup(QStringLiteral("sort_by_"));
268 KActionMenu
* sortByActionMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("sort"));
269 sortByActionMenu
->setIcon(QIcon::fromTheme(QStringLiteral("view-sort")));
270 sortByActionMenu
->setText(i18nc("@action:inmenu View", "Sort By"));
271 sortByActionMenu
->setPopupMode(QToolButton::InstantPopup
);
273 const auto sortByActionGroupActions
= sortByActionGroup
->actions();
274 for (QAction
* action
: sortByActionGroupActions
) {
275 sortByActionMenu
->addAction(action
);
278 sortByActionMenu
->addSeparator();
280 QActionGroup
* group
= new QActionGroup(sortByActionMenu
);
281 group
->setExclusive(true);
283 KToggleAction
* ascendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("ascending"));
284 ascendingAction
->setActionGroup(group
);
285 connect(ascendingAction
, &QAction::triggered
, this, [this] {
286 m_currentView
->setSortOrder(Qt::AscendingOrder
);
289 KToggleAction
* descendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("descending"));
290 descendingAction
->setActionGroup(group
);
291 connect(descendingAction
, &QAction::triggered
, this, [this] {
292 m_currentView
->setSortOrder(Qt::DescendingOrder
);
295 sortByActionMenu
->addAction(ascendingAction
);
296 sortByActionMenu
->addAction(descendingAction
);
297 sortByActionMenu
->addSeparator();
298 sortByActionMenu
->addAction(sortFoldersFirst
);
299 sortByActionMenu
->addAction(sortHiddenLast
);
301 // View -> Additional Information
302 QActionGroup
* visibleRolesGroup
= createFileItemRolesActionGroup(QStringLiteral("show_"));
304 KActionMenu
* visibleRolesMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("additional_info"));
305 visibleRolesMenu
->setText(i18nc("@action:inmenu View", "Show Additional Information"));
306 visibleRolesMenu
->setIcon(QIcon::fromTheme(QStringLiteral("documentinfo")));
307 visibleRolesMenu
->setPopupMode(QToolButton::InstantPopup
);
309 const auto visibleRolesGroupActions
= visibleRolesGroup
->actions();
310 for (QAction
* action
: visibleRolesGroupActions
) {
311 visibleRolesMenu
->addAction(action
);
314 KToggleAction
* showInGroups
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_in_groups"));
315 showInGroups
->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
316 showInGroups
->setText(i18nc("@action:inmenu View", "Show in Groups"));
317 showInGroups
->setWhatsThis(i18nc("@info:whatsthis", "This groups files and folders by their first letter."));
318 connect(showInGroups
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleGroupedSorting
);
320 KToggleAction
* showHiddenFiles
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_hidden_files"));
321 showHiddenFiles
->setIcon(QIcon::fromTheme(QStringLiteral("view-visible")));
322 showHiddenFiles
->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
323 showHiddenFiles
->setWhatsThis(xi18nc("@info:whatsthis", "<para>When "
324 "this is enabled <emphasis>hidden</emphasis> files and folders "
325 "are visible. They will be displayed semi-transparent.</para>"
326 "<para>Hidden items only differ from other ones in that their "
327 "name starts with a \".\". In general there is no need for "
328 "users to access them which is why they are hidden.</para>"));
329 m_actionCollection
->setDefaultShortcuts(showHiddenFiles
, KStandardShortcut::showHideHiddenFiles());
330 connect(showHiddenFiles
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleShowHiddenFiles
);
332 QAction
* adjustViewProps
= m_actionCollection
->addAction(QStringLiteral("view_properties"));
333 adjustViewProps
->setText(i18nc("@action:inmenu View", "Adjust View Display Style..."));
334 adjustViewProps
->setIcon(QIcon::fromTheme(QStringLiteral("view-choose")));
335 adjustViewProps
->setWhatsThis(i18nc("@info:whatsthis", "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
,
349 this, &DolphinViewActionHandler::slotSortTriggered
);
351 connect(rolesActionGroup
, &QActionGroup::triggered
,
352 this, &DolphinViewActionHandler::toggleVisibleRole
);
356 KActionMenu
* groupMenu
= nullptr;
357 QActionGroup
* groupMenuGroup
= nullptr;
359 bool indexingEnabled
= false;
361 Baloo::IndexerConfig config
;
362 indexingEnabled
= config
.fileIndexingEnabled();
365 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
366 for (const KFileItemModel::RoleInfo
& info
: rolesInfo
) {
367 if (!isSortGroup
&& info
.role
== "text") {
368 // It should not be possible to hide the "text" role
372 KToggleAction
* action
= nullptr;
373 const QString name
= groupPrefix
+ info
.role
;
374 if (info
.group
.isEmpty()) {
375 action
= m_actionCollection
->add
<KToggleAction
>(name
);
376 action
->setActionGroup(rolesActionGroup
);
378 if (!groupMenu
|| info
.group
!= groupName
) {
379 groupName
= info
.group
;
380 groupMenu
= m_actionCollection
->add
<KActionMenu
>(groupName
);
381 groupMenu
->setText(groupName
);
382 groupMenu
->setActionGroup(rolesActionGroup
);
384 groupMenuGroup
= new QActionGroup(groupMenu
);
385 groupMenuGroup
->setExclusive(isSortGroup
);
387 connect(groupMenuGroup
, &QActionGroup::triggered
,
388 this, &DolphinViewActionHandler::slotSortTriggered
);
390 connect(groupMenuGroup
, &QActionGroup::triggered
,
391 this, &DolphinViewActionHandler::toggleVisibleRole
);
395 action
= new KToggleAction(groupMenu
);
396 action
->setActionGroup(groupMenuGroup
);
397 groupMenu
->addAction(action
);
399 action
->setText(info
.translation
);
400 action
->setData(info
.role
);
402 const bool enable
= (!info
.requiresBaloo
&& !info
.requiresIndexer
) ||
403 (info
.requiresBaloo
) ||
404 (info
.requiresIndexer
&& indexingEnabled
);
405 action
->setEnabled(enable
);
408 m_sortByActions
.insert(info
.role
, action
);
410 m_visibleRoles
.insert(info
.role
, action
);
414 return rolesActionGroup
;
417 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction
* action
)
419 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
420 m_currentView
->setViewMode(mode
);
422 QAction
* viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
423 viewModeMenu
->setIcon(action
->icon());
426 void DolphinViewActionHandler::slotRename()
428 if (m_currentView
->selectedItemsCount() == 0) {
429 Q_EMIT
setSelectionMode(true, SelectionModeBottomBar::Contents::RenameContents
);
431 Q_EMIT
actionBeingHandled();
432 m_currentView
->renameSelectedItems();
436 void DolphinViewActionHandler::slotTrashActivated()
438 if (m_currentView
->selectedItemsCount() == 0) {
439 Q_EMIT
setSelectionMode(true, SelectionModeBottomBar::Contents::MoveToTrashContents
);
441 Q_EMIT
actionBeingHandled();
442 m_currentView
->trashSelectedItems();
443 Q_EMIT
setSelectionMode(false);
447 void DolphinViewActionHandler::slotDeleteItems()
449 if (m_currentView
->selectedItemsCount() == 0) {
450 Q_EMIT
setSelectionMode(true, SelectionModeBottomBar::Contents::DeleteContents
);
452 Q_EMIT
actionBeingHandled();
453 m_currentView
->deleteSelectedItems();
454 Q_EMIT
setSelectionMode(false);
458 void DolphinViewActionHandler::togglePreview(bool show
)
460 Q_EMIT
actionBeingHandled();
461 m_currentView
->setPreviewsShown(show
);
464 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown
)
467 // It is not enough to update the 'Show Preview' action, also
468 // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
472 QString
DolphinViewActionHandler::currentViewModeActionName() const
474 switch (m_currentView
->viewMode()) {
475 case DolphinView::IconsView
:
476 return QStringLiteral("icons");
477 case DolphinView::DetailsView
:
478 return QStringLiteral("details");
479 case DolphinView::CompactView
:
480 return QStringLiteral("compact");
485 return QString(); // can't happen
488 KActionCollection
* DolphinViewActionHandler::actionCollection()
490 return m_actionCollection
;
493 void DolphinViewActionHandler::updateViewActions()
495 QAction
* viewModeAction
= m_actionCollection
->action(currentViewModeActionName());
496 if (viewModeAction
) {
497 viewModeAction
->setChecked(true);
499 QAction
* viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
500 viewModeMenu
->setIcon(viewModeAction
->icon());
503 QAction
* showPreviewAction
= m_actionCollection
->action(QStringLiteral("show_preview"));
504 showPreviewAction
->setChecked(m_currentView
->previewsShown());
506 slotSortOrderChanged(m_currentView
->sortOrder());
507 slotSortFoldersFirstChanged(m_currentView
->sortFoldersFirst());
508 slotSortHiddenLastChanged(m_currentView
->sortHiddenLast());
509 slotVisibleRolesChanged(m_currentView
->visibleRoles(), QList
<QByteArray
>());
510 slotGroupedSortingChanged(m_currentView
->groupedSorting());
511 slotSortRoleChanged(m_currentView
->sortRole());
512 slotZoomLevelChanged(m_currentView
->zoomLevel(), -1);
514 // Updates the "show_hidden_files" action state and icon
515 slotHiddenFilesShownChanged(m_currentView
->hiddenFilesShown());
518 void DolphinViewActionHandler::zoomIn()
520 const int level
= m_currentView
->zoomLevel();
521 m_currentView
->setZoomLevel(level
+ 1);
525 void DolphinViewActionHandler::zoomOut()
527 const int level
= m_currentView
->zoomLevel();
528 m_currentView
->setZoomLevel(level
- 1);
532 void DolphinViewActionHandler::zoomReset()
534 m_currentView
->resetZoomLevel();
538 void DolphinViewActionHandler::toggleSortFoldersFirst()
540 const bool sortFirst
= m_currentView
->sortFoldersFirst();
541 m_currentView
->setSortFoldersFirst(!sortFirst
);
544 void DolphinViewActionHandler::toggleSortHiddenLast()
546 const bool sortHiddenLast
= m_currentView
->sortHiddenLast();
547 m_currentView
->setSortHiddenLast(!sortHiddenLast
);
550 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order
)
552 QAction
* descending
= m_actionCollection
->action(QStringLiteral("descending"));
553 QAction
* ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
554 const bool sortDescending
= (order
== Qt::DescendingOrder
);
555 descending
->setChecked(sortDescending
);
556 ascending
->setChecked(!sortDescending
);
559 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst
)
561 m_actionCollection
->action(QStringLiteral("folders_first"))->setChecked(foldersFirst
);
564 void DolphinViewActionHandler::slotSortHiddenLastChanged(bool hiddenLast
)
566 m_actionCollection
->action(QStringLiteral("hidden_last"))->setChecked(hiddenLast
);
569 void DolphinViewActionHandler::toggleVisibleRole(QAction
* action
)
571 Q_EMIT
actionBeingHandled();
573 const QByteArray toggledRole
= action
->data().toByteArray();
575 QList
<QByteArray
> roles
= m_currentView
->visibleRoles();
577 const bool show
= action
->isChecked();
579 const int index
= roles
.indexOf(toggledRole
);
580 const bool containsInfo
= (index
>= 0);
581 if (show
&& !containsInfo
) {
582 roles
.append(toggledRole
);
583 m_currentView
->setVisibleRoles(roles
);
584 } else if (!show
&& containsInfo
) {
585 roles
.removeAt(index
);
586 m_currentView
->setVisibleRoles(roles
);
587 Q_ASSERT(roles
.indexOf(toggledRole
) < 0);
591 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList
<QByteArray
>& current
,
592 const QList
<QByteArray
>& previous
)
596 const auto checkedRoles
= QSet
<QByteArray
>(current
.constBegin(), current
.constEnd());
597 QHashIterator
<QByteArray
, KToggleAction
*> it(m_visibleRoles
);
598 while (it
.hasNext()) {
600 const QByteArray
& role
= it
.key();
601 KToggleAction
* action
= it
.value();
602 action
->setChecked(checkedRoles
.contains(role
));
606 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped
)
608 m_currentView
->setGroupedSorting(grouped
);
611 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting
)
613 QAction
* showInGroupsAction
= m_actionCollection
->action(QStringLiteral("show_in_groups"));
614 showInGroupsAction
->setChecked(groupedSorting
);
617 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show
)
619 Q_EMIT
actionBeingHandled();
620 m_currentView
->setHiddenFilesShown(show
);
623 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown
)
625 QAction
* showHiddenFilesAction
= m_actionCollection
->action(QStringLiteral("show_hidden_files"));
626 showHiddenFilesAction
->setChecked(shown
);
629 void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable
)
631 m_actionCollection
->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable
&&
632 KProtocolManager::supportsMakeDir(currentView()->url()));
635 KToggleAction
* DolphinViewActionHandler::iconsModeAction()
637 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("icons"));
638 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
639 iconsView
->setToolTip(i18nc("@info", "Icons view mode"));
640 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_1
);
641 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
642 iconsView
->setData(QVariant::fromValue(DolphinView::IconsView
));
646 KToggleAction
* DolphinViewActionHandler::compactModeAction()
648 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("compact"));
649 iconsView
->setText(i18nc("@action:inmenu View Mode", "Compact"));
650 iconsView
->setToolTip(i18nc("@info", "Compact view mode"));
651 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_2
);
652 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
653 iconsView
->setData(QVariant::fromValue(DolphinView::CompactView
));
657 KToggleAction
* DolphinViewActionHandler::detailsModeAction()
659 KToggleAction
* detailsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("details"));
660 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
661 detailsView
->setToolTip(i18nc("@info", "Details view mode"));
662 m_actionCollection
->setDefaultShortcut(detailsView
, Qt::CTRL
| Qt::Key_3
);
663 detailsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
664 detailsView
->setData(QVariant::fromValue(DolphinView::DetailsView
));
668 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray
& role
)
670 KToggleAction
* action
= m_sortByActions
.value(role
);
672 action
->setChecked(true);
674 if (!action
->icon().isNull()) {
675 QAction
* sortByMenu
= m_actionCollection
->action(QStringLiteral("sort"));
676 sortByMenu
->setIcon(action
->icon());
680 QAction
* descending
= m_actionCollection
->action(QStringLiteral("descending"));
681 QAction
* ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
683 if (role
== "text" || role
== "type" || role
== "extension" || role
== "tags" || role
== "comment") {
684 descending
->setText(i18nc("Sort descending", "Z-A"));
685 ascending
->setText(i18nc("Sort ascending", "A-Z"));
686 } else if (role
== "size") {
687 descending
->setText(i18nc("Sort descending", "Largest First"));
688 ascending
->setText(i18nc("Sort ascending", "Smallest First"));
689 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
690 descending
->setText(i18nc("Sort descending", "Newest First"));
691 ascending
->setText(i18nc("Sort ascending", "Oldest First"));
692 } else if (role
== "rating") {
693 descending
->setText(i18nc("Sort descending", "Highest First"));
694 ascending
->setText(i18nc("Sort ascending", "Lowest First"));
696 descending
->setText(i18nc("Sort descending", "Descending"));
697 ascending
->setText(i18nc("Sort ascending", "Ascending"));
700 slotSortOrderChanged(m_currentView
->sortOrder());
703 void DolphinViewActionHandler::slotZoomLevelChanged(int current
, int previous
)
707 QAction
* zoomInAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomIn
));
709 zoomInAction
->setEnabled(current
< ZoomLevelInfo::maximumLevel());
712 QAction
* zoomOutAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomOut
));
714 zoomOutAction
->setEnabled(current
> ZoomLevelInfo::minimumLevel());
718 void DolphinViewActionHandler::slotSortTriggered(QAction
* action
)
720 // The radiobuttons of the "Sort By"-menu are split between the main-menu
721 // and several sub-menus. Because of this they don't have a common
722 // action-group that assures an exclusive toggle-state between the main-menu
723 // actions and the sub-menu-actions. If an action gets checked, it must
724 // be assured that all other actions get unchecked, except the ascending/
725 // descending actions
726 for (QAction
*groupAction
: qAsConst(m_sortByActions
)) {
727 KActionMenu
* actionMenu
= qobject_cast
<KActionMenu
*>(groupAction
);
729 const auto actions
= actionMenu
->menu()->actions();
730 for (QAction
* subAction
: actions
) {
731 subAction
->setChecked(false);
733 } else if (groupAction
->actionGroup()) {
734 groupAction
->setChecked(false);
737 action
->setChecked(true);
739 // Apply the activated sort-role to the view
740 const QByteArray role
= action
->data().toByteArray();
741 m_currentView
->setSortRole(role
);
744 void DolphinViewActionHandler::slotAdjustViewProperties()
746 Q_EMIT
actionBeingHandled();
747 QPointer
<ViewPropertiesDialog
> dialog
= new ViewPropertiesDialog(m_currentView
);
752 void DolphinViewActionHandler::slotDuplicate()
754 if (m_currentView
->selectedItemsCount() == 0) {
755 Q_EMIT
setSelectionMode(true, SelectionModeBottomBar::Contents::DuplicateContents
);
757 Q_EMIT
actionBeingHandled();
758 m_currentView
->duplicateSelectedItems();
759 Q_EMIT
setSelectionMode(false);
763 void DolphinViewActionHandler::slotProperties()
765 KPropertiesDialog
* dialog
= nullptr;
766 const KFileItemList list
= m_currentView
->selectedItems();
767 if (list
.isEmpty()) {
768 const QUrl url
= m_currentView
->url();
769 dialog
= new KPropertiesDialog(url
, m_currentView
);
771 dialog
= new KPropertiesDialog(list
, m_currentView
);
774 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
777 dialog
->activateWindow();
780 void DolphinViewActionHandler::slotCopyPath()
782 if (m_currentView
->selectedItemsCount() == 0) {
783 Q_EMIT
setSelectionMode(true, SelectionModeBottomBar::Contents::CopyLocationContents
);
785 m_currentView
->copyPathToClipboard();
786 Q_EMIT
setSelectionMode(false);
790 void DolphinViewActionHandler::slotSelectionChanged(const KFileItemList
& selection
)
792 QString basicActionsMenuText
;
793 if (selection
.isEmpty()) {
794 basicActionsMenuText
=
795 i18nc("@action:inmenu menu with actions like copy, paste, rename. The user's selection is empty when this text is shown.",
796 "Actions for Current View");
798 QFontMetrics fontMetrics
= QMenu().fontMetrics();
799 // i18n: @action:inmenu menu with actions like copy, paste, rename.
800 // %1 is a textual representation of the currently selected files or folders. This can be the name of
801 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
802 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
803 // and a fallback will be used.
804 basicActionsMenuText
= i18n("Actions for %1", fileItemListToString(selection
, fontMetrics
.averageCharWidth() * 40, fontMetrics
, ItemsState::Selected
));
807 if (basicActionsMenuText
== QStringLiteral("NULL")) {
808 const KFileItemListProperties
properties(selection
);
809 basicActionsMenuText
=
810 i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
811 "Actions for One Selected Item", "Actions for %1 Selected Items", selection
.count());
814 QAction
*basicActionsMenu
= m_actionCollection
->action(QStringLiteral("basic_actions"));
815 basicActionsMenu
->setText(basicActionsMenuText
);
817 // Add or remove contextual actions
818 while (!basicActionsMenu
->menu()->actions().constLast()->isSeparator()) {
819 basicActionsMenu
->menu()->removeAction(basicActionsMenu
->menu()->actions().last());
821 if (selection
.count() == 1) {
822 if (selection
.first().isLink()) {
823 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("show_target")));
825 if (selection
.first().isDir()) {
826 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("add_to_places")));