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::selectionChanged
,
76 this, &DolphinViewActionHandler::slotSelectionChanged
);
77 slotSelectionChanged(m_currentView
->selectedItems());
80 DolphinView
* DolphinViewActionHandler::currentView()
85 void DolphinViewActionHandler::createActions()
87 // This action doesn't appear in the GUI, it's for the shortcut only.
88 // KNewFileMenu takes care of the GUI stuff.
89 QAction
* newDirAction
= m_actionCollection
->addAction(QStringLiteral("create_dir"));
90 newDirAction
->setText(i18nc("@action", "Create Folder..."));
91 m_actionCollection
->setDefaultShortcuts(newDirAction
, KStandardShortcut::createFolder());
92 newDirAction
->setIcon(QIcon::fromTheme(QStringLiteral("folder-new")));
93 newDirAction
->setEnabled(false); // Will be enabled in slotWriteStateChanged(bool) if the current URL is writable
94 connect(newDirAction
, &QAction::triggered
, this, &DolphinViewActionHandler::createDirectoryTriggered
);
98 auto renameAction
= KStandardAction::renameFile(this, &DolphinViewActionHandler::slotRename
, m_actionCollection
);
99 renameAction
->setWhatsThis(xi18nc("@info:whatsthis", "This renames the "
100 "items in your current selection.<nl/>Renaming multiple items "
101 "at once amounts to their new names differing only in a number."));
103 auto trashAction
= KStandardAction::moveToTrash(this, &DolphinViewActionHandler::slotTrashActivated
, m_actionCollection
);
104 auto trashShortcuts
= trashAction
->shortcuts();
105 if (!trashShortcuts
.contains(QKeySequence::Delete
)) {
106 trashShortcuts
.append(QKeySequence::Delete
);
107 m_actionCollection
->setDefaultShortcuts(trashAction
, trashShortcuts
);
109 trashAction
->setWhatsThis(xi18nc("@info:whatsthis", "This moves the "
110 "items in your current selection to the <filename>Trash"
111 "</filename>.<nl/>The trash is a temporary storage where "
112 "items can be deleted from if disk space is needed."));
114 auto deleteAction
= KStandardAction::deleteFile(this, &DolphinViewActionHandler::slotDeleteItems
, m_actionCollection
);
115 auto deleteShortcuts
= deleteAction
->shortcuts();
116 if (!deleteShortcuts
.contains(Qt::SHIFT
| Qt::Key_Delete
)) {
117 deleteShortcuts
.append(Qt::SHIFT
| Qt::Key_Delete
);
118 m_actionCollection
->setDefaultShortcuts(deleteAction
, deleteShortcuts
);
120 deleteAction
->setWhatsThis(xi18nc("@info:whatsthis", "This deletes "
121 "the items in your current selection completely. They can "
122 "not be recovered by normal means."));
124 // This action is useful for being enabled when KStandardAction::MoveToTrash should be
125 // disabled and KStandardAction::DeleteFile is enabled (e.g. non-local files), so that Key_Del
126 // can be used for deleting the file (#76016). It needs to be a separate action
127 // so that the Edit menu isn't affected.
128 QAction
* deleteWithTrashShortcut
= m_actionCollection
->addAction(QStringLiteral("delete_shortcut"));
129 // The descriptive text is just for the shortcuts editor.
130 deleteWithTrashShortcut
->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
131 m_actionCollection
->setDefaultShortcuts(deleteWithTrashShortcut
, KStandardShortcut::moveToTrash());
132 deleteWithTrashShortcut
->setEnabled(false);
133 connect(deleteWithTrashShortcut
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDeleteItems
);
135 QAction
* duplicateAction
= m_actionCollection
->addAction(QStringLiteral("duplicate"));
136 duplicateAction
->setText(i18nc("@action:inmenu File", "Duplicate Here"));
137 duplicateAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-duplicate")));
138 m_actionCollection
->setDefaultShortcut(duplicateAction
, Qt::CTRL
| Qt::Key_D
);
139 duplicateAction
->setEnabled(false);
140 connect(duplicateAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDuplicate
);
142 QAction
*propertiesAction
= m_actionCollection
->addAction( QStringLiteral("properties") );
143 // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
144 propertiesAction
->setText( i18nc("@action:inmenu File", "Properties") );
145 propertiesAction
->setWhatsThis(xi18nc("@info:whatsthis properties",
146 "This shows a complete list of properties of the currently "
147 "selected items in a new window.<nl/>If nothing is selected the "
148 "window will be about the currently viewed folder instead.<nl/>"
149 "You can configure advanced options there like managing "
150 "read- and write-permissions."));
151 propertiesAction
->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
152 m_actionCollection
->setDefaultShortcuts(propertiesAction
, {Qt::ALT
| Qt::Key_Return
, Qt::ALT
| Qt::Key_Enter
});
153 connect(propertiesAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotProperties
);
155 QAction
*copyPathAction
= m_actionCollection
->addAction( QStringLiteral("copy_location") );
156 copyPathAction
->setText(i18nc("@action:incontextmenu", "Copy Location"));
157 copyPathAction
->setWhatsThis(i18nc("@info:whatsthis copy_location",
158 "This will copy the path of the first selected item into the clipboard."
161 copyPathAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy-path")));
162 m_actionCollection
->setDefaultShortcuts(copyPathAction
, {Qt::CTRL
| Qt::ALT
| Qt::Key_C
});
163 connect(copyPathAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotCopyPath
);
165 // This menu makes sure that users who don't know how to open a context menu and haven't
166 // figured out how to enable the menu bar can still perform basic file manipulation.
167 // This only works if they know how to select a file.
168 // The text when nothing is selected at least implies that a selection can /somehow/ be made.
169 // This menu is by default only used in the hamburger menu but created here so users can put
170 // it on their toolbar.
171 KActionMenu
*basicActionsMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("basic_actions"), this);
172 // The text is set later depending on the selection in the currently active view.
173 basicActionsMenu
->setPopupMode(QToolButton::InstantPopup
);
174 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Cut
)));
175 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Copy
)));
176 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Paste
)));
177 basicActionsMenu
->addSeparator();
178 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
179 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
180 basicActionsMenu
->addSeparator();
181 basicActionsMenu
->addAction(m_actionCollection
->action(QStringLiteral("properties")));
182 basicActionsMenu
->addSeparator(); // We add one more separator because we sometimes add contextual
183 // actions in slotSelectionChanged() after the static ones above.
186 KToggleAction
* iconsAction
= iconsModeAction();
187 KToggleAction
* compactAction
= compactModeAction();
188 KToggleAction
* detailsAction
= detailsModeAction();
190 iconsAction
->setWhatsThis(xi18nc("@info:whatsthis Icons view mode",
191 "<para>This switches to a view mode that focuses on the folder "
192 "and file icons. This mode makes it easy to distinguish folders "
193 "from files and to detect items with distinctive <emphasis>"
194 "file types</emphasis>.</para><para> This mode is handy to "
195 "browse through pictures when the <interface>Preview"
196 "</interface> option is enabled.</para>"));
197 compactAction
->setWhatsThis(xi18nc("@info:whatsthis Compact view mode",
198 "<para>This switches to a compact view mode that lists the folders "
199 "and files in columns with the names beside the icons.</para><para>"
200 "This helps to keep the overview in folders with many items.</para>"));
201 detailsAction
->setWhatsThis(xi18nc("@info:whatsthis Details view mode",
202 "<para>This switches to a list view mode that focuses on folder "
203 "and file details.</para><para>Click on a detail in the column "
204 "header to sort the items by it. Click again to sort the other "
205 "way around. To select which details should be displayed click "
206 "the header with the right mouse button.</para><para>You can "
207 "view the contents of a folder without leaving the current "
208 "location by clicking to the left of it. This way you can view "
209 "the contents of multiple folders in the same list.</para>"));
211 KSelectAction
* viewModeActions
= m_actionCollection
->add
<KSelectAction
>(QStringLiteral("view_mode"));
212 viewModeActions
->setText(i18nc("@action:intoolbar", "View Mode"));
213 viewModeActions
->addAction(iconsAction
);
214 viewModeActions
->addAction(compactAction
);
215 viewModeActions
->addAction(detailsAction
);
216 viewModeActions
->setToolBarMode(KSelectAction::MenuMode
);
217 connect(viewModeActions
, &KSelectAction::triggered
, this, &DolphinViewActionHandler::slotViewModeActionTriggered
);
219 QAction
* zoomInAction
= KStandardAction::zoomIn(this,
220 &DolphinViewActionHandler::zoomIn
,
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,
233 &DolphinViewActionHandler::zoomOut
,
235 zoomOutAction
->setWhatsThis(i18nc("@info:whatsthis zoom out", "This reduces the icon size."));
237 KActionMenu
* zoomMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("zoom"));
238 zoomMenu
->setText(i18nc("@action:inmenu menu of zoom actions", "Zoom"));
239 zoomMenu
->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
240 zoomMenu
->setPopupMode(QToolButton::InstantPopup
);
241 zoomMenu
->addAction(zoomInAction
);
242 zoomMenu
->addAction(zoomResetAction
);
243 zoomMenu
->addAction(zoomOutAction
);
245 KToggleAction
* showPreview
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_preview"));
246 showPreview
->setText(i18nc("@action:intoolbar", "Show Previews"));
247 showPreview
->setToolTip(i18nc("@info", "Show preview of files and folders"));
248 showPreview
->setWhatsThis(xi18nc("@info:whatsthis", "When this is "
249 "enabled, the icons are based on the actual file or folder "
250 "contents.<nl/>For example the icons of images become scaled "
251 "down versions of the images."));
252 showPreview
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
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", "<para>When "
322 "this is enabled <emphasis>hidden</emphasis> files and folders "
323 "are visible. They will be displayed semi-transparent.</para>"
324 "<para>Hidden items only differ from other ones in that their "
325 "name starts with a \".\". In general there is no need for "
326 "users to access them which is why they are hidden.</para>"));
327 m_actionCollection
->setDefaultShortcuts(showHiddenFiles
, KStandardShortcut::showHideHiddenFiles());
328 connect(showHiddenFiles
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleShowHiddenFiles
);
330 QAction
* adjustViewProps
= m_actionCollection
->addAction(QStringLiteral("view_properties"));
331 adjustViewProps
->setText(i18nc("@action:inmenu View", "Adjust View Display Style..."));
332 adjustViewProps
->setIcon(QIcon::fromTheme(QStringLiteral("view-choose")));
333 adjustViewProps
->setWhatsThis(i18nc("@info:whatsthis", "This opens a window "
334 "in which all folder view properties can be adjusted."));
335 connect(adjustViewProps
, &QAction::triggered
, this, &DolphinViewActionHandler::slotAdjustViewProperties
);
338 QActionGroup
* DolphinViewActionHandler::createFileItemRolesActionGroup(const QString
& groupPrefix
)
340 const bool isSortGroup
= (groupPrefix
== QLatin1String("sort_by_"));
341 Q_ASSERT(isSortGroup
|| groupPrefix
== QLatin1String("show_"));
343 QActionGroup
* rolesActionGroup
= new QActionGroup(m_actionCollection
);
344 rolesActionGroup
->setExclusive(isSortGroup
);
346 connect(rolesActionGroup
, &QActionGroup::triggered
,
347 this, &DolphinViewActionHandler::slotSortTriggered
);
349 connect(rolesActionGroup
, &QActionGroup::triggered
,
350 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
,
386 this, &DolphinViewActionHandler::slotSortTriggered
);
388 connect(groupMenuGroup
, &QActionGroup::triggered
,
389 this, &DolphinViewActionHandler::toggleVisibleRole
);
393 action
= new KToggleAction(groupMenu
);
394 action
->setActionGroup(groupMenuGroup
);
395 groupMenu
->addAction(action
);
397 action
->setText(info
.translation
);
398 action
->setData(info
.role
);
400 const bool enable
= (!info
.requiresBaloo
&& !info
.requiresIndexer
) ||
401 (info
.requiresBaloo
) ||
402 (info
.requiresIndexer
&& indexingEnabled
);
403 action
->setEnabled(enable
);
406 m_sortByActions
.insert(info
.role
, action
);
408 m_visibleRoles
.insert(info
.role
, action
);
412 return rolesActionGroup
;
415 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction
* action
)
417 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
418 m_currentView
->setMode(mode
);
420 QAction
* viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
421 viewModeMenu
->setIcon(action
->icon());
424 void DolphinViewActionHandler::slotRename()
426 Q_EMIT
actionBeingHandled();
427 m_currentView
->renameSelectedItems();
430 void DolphinViewActionHandler::slotTrashActivated()
432 Q_EMIT
actionBeingHandled();
433 m_currentView
->trashSelectedItems();
436 void DolphinViewActionHandler::slotDeleteItems()
438 Q_EMIT
actionBeingHandled();
439 m_currentView
->deleteSelectedItems();
442 void DolphinViewActionHandler::togglePreview(bool show
)
444 Q_EMIT
actionBeingHandled();
445 m_currentView
->setPreviewsShown(show
);
448 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown
)
451 // It is not enough to update the 'Show Preview' action, also
452 // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
456 QString
DolphinViewActionHandler::currentViewModeActionName() const
458 switch (m_currentView
->mode()) {
459 case DolphinView::IconsView
:
460 return QStringLiteral("icons");
461 case DolphinView::DetailsView
:
462 return QStringLiteral("details");
463 case DolphinView::CompactView
:
464 return QStringLiteral("compact");
469 return QString(); // can't happen
472 KActionCollection
* DolphinViewActionHandler::actionCollection()
474 return m_actionCollection
;
477 void DolphinViewActionHandler::updateViewActions()
479 QAction
* viewModeAction
= m_actionCollection
->action(currentViewModeActionName());
480 if (viewModeAction
) {
481 viewModeAction
->setChecked(true);
483 QAction
* viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
484 viewModeMenu
->setIcon(viewModeAction
->icon());
487 QAction
* showPreviewAction
= m_actionCollection
->action(QStringLiteral("show_preview"));
488 showPreviewAction
->setChecked(m_currentView
->previewsShown());
490 slotSortOrderChanged(m_currentView
->sortOrder());
491 slotSortFoldersFirstChanged(m_currentView
->sortFoldersFirst());
492 slotSortHiddenLastChanged(m_currentView
->sortHiddenLast());
493 slotVisibleRolesChanged(m_currentView
->visibleRoles(), QList
<QByteArray
>());
494 slotGroupedSortingChanged(m_currentView
->groupedSorting());
495 slotSortRoleChanged(m_currentView
->sortRole());
496 slotZoomLevelChanged(m_currentView
->zoomLevel(), -1);
498 // Updates the "show_hidden_files" action state and icon
499 slotHiddenFilesShownChanged(m_currentView
->hiddenFilesShown());
502 void DolphinViewActionHandler::zoomIn()
504 const int level
= m_currentView
->zoomLevel();
505 m_currentView
->setZoomLevel(level
+ 1);
509 void DolphinViewActionHandler::zoomOut()
511 const int level
= m_currentView
->zoomLevel();
512 m_currentView
->setZoomLevel(level
- 1);
516 void DolphinViewActionHandler::zoomReset()
518 m_currentView
->resetZoomLevel();
522 void DolphinViewActionHandler::toggleSortFoldersFirst()
524 const bool sortFirst
= m_currentView
->sortFoldersFirst();
525 m_currentView
->setSortFoldersFirst(!sortFirst
);
528 void DolphinViewActionHandler::toggleSortHiddenLast()
530 const bool sortHiddenLast
= m_currentView
->sortHiddenLast();
531 m_currentView
->setSortHiddenLast(!sortHiddenLast
);
534 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order
)
536 QAction
* descending
= m_actionCollection
->action(QStringLiteral("descending"));
537 QAction
* ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
538 const bool sortDescending
= (order
== Qt::DescendingOrder
);
539 descending
->setChecked(sortDescending
);
540 ascending
->setChecked(!sortDescending
);
543 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst
)
545 m_actionCollection
->action(QStringLiteral("folders_first"))->setChecked(foldersFirst
);
548 void DolphinViewActionHandler::slotSortHiddenLastChanged(bool hiddenLast
)
550 m_actionCollection
->action(QStringLiteral("hidden_last"))->setChecked(hiddenLast
);
553 void DolphinViewActionHandler::toggleVisibleRole(QAction
* action
)
555 Q_EMIT
actionBeingHandled();
557 const QByteArray toggledRole
= action
->data().toByteArray();
559 QList
<QByteArray
> roles
= m_currentView
->visibleRoles();
561 const bool show
= action
->isChecked();
563 const int index
= roles
.indexOf(toggledRole
);
564 const bool containsInfo
= (index
>= 0);
565 if (show
&& !containsInfo
) {
566 roles
.append(toggledRole
);
567 m_currentView
->setVisibleRoles(roles
);
568 } else if (!show
&& containsInfo
) {
569 roles
.removeAt(index
);
570 m_currentView
->setVisibleRoles(roles
);
571 Q_ASSERT(roles
.indexOf(toggledRole
) < 0);
575 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList
<QByteArray
>& current
,
576 const QList
<QByteArray
>& previous
)
580 const auto checkedRoles
= QSet
<QByteArray
>(current
.constBegin(), current
.constEnd());
581 QHashIterator
<QByteArray
, KToggleAction
*> it(m_visibleRoles
);
582 while (it
.hasNext()) {
584 const QByteArray
& role
= it
.key();
585 KToggleAction
* action
= it
.value();
586 action
->setChecked(checkedRoles
.contains(role
));
590 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped
)
592 m_currentView
->setGroupedSorting(grouped
);
595 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting
)
597 QAction
* showInGroupsAction
= m_actionCollection
->action(QStringLiteral("show_in_groups"));
598 showInGroupsAction
->setChecked(groupedSorting
);
601 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show
)
603 Q_EMIT
actionBeingHandled();
604 m_currentView
->setHiddenFilesShown(show
);
607 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown
)
609 QAction
* showHiddenFilesAction
= m_actionCollection
->action(QStringLiteral("show_hidden_files"));
610 showHiddenFilesAction
->setChecked(shown
);
613 void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable
)
615 m_actionCollection
->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable
&&
616 KProtocolManager::supportsMakeDir(currentView()->url()));
619 KToggleAction
* DolphinViewActionHandler::iconsModeAction()
621 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("icons"));
622 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
623 iconsView
->setToolTip(i18nc("@info", "Icons view mode"));
624 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_1
);
625 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
626 iconsView
->setData(QVariant::fromValue(DolphinView::IconsView
));
630 KToggleAction
* DolphinViewActionHandler::compactModeAction()
632 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("compact"));
633 iconsView
->setText(i18nc("@action:inmenu View Mode", "Compact"));
634 iconsView
->setToolTip(i18nc("@info", "Compact view mode"));
635 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_2
);
636 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
637 iconsView
->setData(QVariant::fromValue(DolphinView::CompactView
));
641 KToggleAction
* DolphinViewActionHandler::detailsModeAction()
643 KToggleAction
* detailsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("details"));
644 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
645 detailsView
->setToolTip(i18nc("@info", "Details view mode"));
646 m_actionCollection
->setDefaultShortcut(detailsView
, Qt::CTRL
| Qt::Key_3
);
647 detailsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
648 detailsView
->setData(QVariant::fromValue(DolphinView::DetailsView
));
652 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray
& role
)
654 KToggleAction
* action
= m_sortByActions
.value(role
);
656 action
->setChecked(true);
658 if (!action
->icon().isNull()) {
659 QAction
* sortByMenu
= m_actionCollection
->action(QStringLiteral("sort"));
660 sortByMenu
->setIcon(action
->icon());
664 QAction
* descending
= m_actionCollection
->action(QStringLiteral("descending"));
665 QAction
* ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
667 if (role
== "text" || role
== "type" || role
== "extension" || role
== "tags" || role
== "comment") {
668 descending
->setText(i18nc("Sort descending", "Z-A"));
669 ascending
->setText(i18nc("Sort ascending", "A-Z"));
670 } else if (role
== "size") {
671 descending
->setText(i18nc("Sort descending", "Largest First"));
672 ascending
->setText(i18nc("Sort ascending", "Smallest First"));
673 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
674 descending
->setText(i18nc("Sort descending", "Newest First"));
675 ascending
->setText(i18nc("Sort ascending", "Oldest First"));
676 } else if (role
== "rating") {
677 descending
->setText(i18nc("Sort descending", "Highest First"));
678 ascending
->setText(i18nc("Sort ascending", "Lowest First"));
680 descending
->setText(i18nc("Sort descending", "Descending"));
681 ascending
->setText(i18nc("Sort ascending", "Ascending"));
684 slotSortOrderChanged(m_currentView
->sortOrder());
687 void DolphinViewActionHandler::slotZoomLevelChanged(int current
, int previous
)
691 QAction
* zoomInAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomIn
));
693 zoomInAction
->setEnabled(current
< ZoomLevelInfo::maximumLevel());
696 QAction
* zoomOutAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomOut
));
698 zoomOutAction
->setEnabled(current
> ZoomLevelInfo::minimumLevel());
702 void DolphinViewActionHandler::slotSortTriggered(QAction
* action
)
704 // The radiobuttons of the "Sort By"-menu are split between the main-menu
705 // and several sub-menus. Because of this they don't have a common
706 // action-group that assures an exclusive toggle-state between the main-menu
707 // actions and the sub-menu-actions. If an action gets checked, it must
708 // be assured that all other actions get unchecked, except the ascending/
709 // descending actions
710 for (QAction
*groupAction
: qAsConst(m_sortByActions
)) {
711 KActionMenu
* actionMenu
= qobject_cast
<KActionMenu
*>(groupAction
);
713 const auto actions
= actionMenu
->menu()->actions();
714 for (QAction
* subAction
: actions
) {
715 subAction
->setChecked(false);
717 } else if (groupAction
->actionGroup()) {
718 groupAction
->setChecked(false);
721 action
->setChecked(true);
723 // Apply the activated sort-role to the view
724 const QByteArray role
= action
->data().toByteArray();
725 m_currentView
->setSortRole(role
);
728 void DolphinViewActionHandler::slotAdjustViewProperties()
730 Q_EMIT
actionBeingHandled();
731 QPointer
<ViewPropertiesDialog
> dialog
= new ViewPropertiesDialog(m_currentView
);
736 void DolphinViewActionHandler::slotDuplicate()
738 Q_EMIT
actionBeingHandled();
739 m_currentView
->duplicateSelectedItems();
742 void DolphinViewActionHandler::slotProperties()
744 KPropertiesDialog
* dialog
= nullptr;
745 const KFileItemList list
= m_currentView
->selectedItems();
746 if (list
.isEmpty()) {
747 const QUrl url
= m_currentView
->url();
748 dialog
= new KPropertiesDialog(url
, m_currentView
);
750 dialog
= new KPropertiesDialog(list
, m_currentView
);
753 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
756 dialog
->activateWindow();
759 void DolphinViewActionHandler::slotCopyPath()
761 m_currentView
->copyPathToClipboard();
764 void DolphinViewActionHandler::slotSelectionChanged(const KFileItemList
& selection
)
766 QString basicActionsMenuText
;
767 if (selection
.isEmpty()) {
768 basicActionsMenuText
=
769 i18nc("@action:inmenu menu with actions like copy, paste, rename. The user's selection is empty when this text is shown.",
770 "Actions for Current View");
772 QFontMetrics fontMetrics
= QMenu().fontMetrics();
773 // i18n: @action:inmenu menu with actions like copy, paste, rename.
774 // %1 is a textual representation of the currently selected files or folders. This can be the name of
775 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
776 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
777 // and a fallback will be used.
778 basicActionsMenuText
= i18n("Actions for %1", fileItemListToString(selection
, fontMetrics
.averageCharWidth() * 40, fontMetrics
, ItemsState::Selected
));
781 if (basicActionsMenuText
== QStringLiteral("NULL")) {
782 const KFileItemListProperties
properties(selection
);
783 basicActionsMenuText
=
784 i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
785 "Actions for One Selected Item", "Actions for %1 Selected Items", selection
.count());
788 QAction
*basicActionsMenu
= m_actionCollection
->action(QStringLiteral("basic_actions"));
789 basicActionsMenu
->setText(basicActionsMenuText
);
791 // Add or remove contextual actions
792 while (!basicActionsMenu
->menu()->actions().constLast()->isSeparator()) {
793 basicActionsMenu
->menu()->removeAction(basicActionsMenu
->menu()->actions().last());
795 if (selection
.count() == 1) {
796 if (selection
.first().isLink()) {
797 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("show_target")));
799 if (selection
.first().isDir()) {
800 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("add_to_places")));