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/kfileitemmodel.h"
12 #include "settings/viewpropertiesdialog.h"
13 #include "views/zoomlevelinfo.h"
14 #include "kconfig_version.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
, QObject
* parent
) :
33 m_actionCollection(collection
),
34 m_currentView(nullptr),
38 Q_ASSERT(m_actionCollection
);
42 void DolphinViewActionHandler::setCurrentView(DolphinView
* view
)
47 disconnect(m_currentView
, nullptr, this, nullptr);
52 connect(view
, &DolphinView::modeChanged
,
53 this, &DolphinViewActionHandler::updateViewActions
);
54 connect(view
, &DolphinView::previewsShownChanged
,
55 this, &DolphinViewActionHandler::slotPreviewsShownChanged
);
56 connect(view
, &DolphinView::sortOrderChanged
,
57 this, &DolphinViewActionHandler::slotSortOrderChanged
);
58 connect(view
, &DolphinView::sortFoldersFirstChanged
,
59 this, &DolphinViewActionHandler::slotSortFoldersFirstChanged
);
60 connect(view
, &DolphinView::sortHiddenLastChanged
,
61 this, &DolphinViewActionHandler::slotSortHiddenLastChanged
);
62 connect(view
, &DolphinView::visibleRolesChanged
,
63 this, &DolphinViewActionHandler::slotVisibleRolesChanged
);
64 connect(view
, &DolphinView::groupedSortingChanged
,
65 this, &DolphinViewActionHandler::slotGroupedSortingChanged
);
66 connect(view
, &DolphinView::hiddenFilesShownChanged
,
67 this, &DolphinViewActionHandler::slotHiddenFilesShownChanged
);
68 connect(view
, &DolphinView::sortRoleChanged
,
69 this, &DolphinViewActionHandler::slotSortRoleChanged
);
70 connect(view
, &DolphinView::zoomLevelChanged
,
71 this, &DolphinViewActionHandler::slotZoomLevelChanged
);
72 connect(view
, &DolphinView::writeStateChanged
,
73 this, &DolphinViewActionHandler::slotWriteStateChanged
);
74 connect(view
, &DolphinView::selectionChanged
,
75 this, &DolphinViewActionHandler::slotSelectionChanged
);
76 slotSelectionChanged(m_currentView
->selectedItems());
79 DolphinView
* DolphinViewActionHandler::currentView()
84 void DolphinViewActionHandler::createActions()
86 // This action doesn't appear in the GUI, it's for the shortcut only.
87 // KNewFileMenu takes care of the GUI stuff.
88 QAction
* newDirAction
= m_actionCollection
->addAction(QStringLiteral("create_dir"));
89 newDirAction
->setText(i18nc("@action", "Create Folder..."));
90 m_actionCollection
->setDefaultShortcuts(newDirAction
, KStandardShortcut::createFolder());
91 newDirAction
->setIcon(QIcon::fromTheme(QStringLiteral("folder-new")));
92 newDirAction
->setEnabled(false); // Will be enabled in slotWriteStateChanged(bool) if the current URL is writable
93 connect(newDirAction
, &QAction::triggered
, this, &DolphinViewActionHandler::createDirectoryTriggered
);
97 auto renameAction
= KStandardAction::renameFile(this, &DolphinViewActionHandler::slotRename
, m_actionCollection
);
98 renameAction
->setWhatsThis(xi18nc("@info:whatsthis", "This renames the "
99 "items in your current selection.<nl/>Renaming multiple items "
100 "at once amounts to their new names differing only in a number."));
102 auto trashAction
= KStandardAction::moveToTrash(this, &DolphinViewActionHandler::slotTrashActivated
, m_actionCollection
);
103 auto trashShortcuts
= trashAction
->shortcuts();
104 if (!trashShortcuts
.contains(QKeySequence::Delete
)) {
105 trashShortcuts
.append(QKeySequence::Delete
);
106 m_actionCollection
->setDefaultShortcuts(trashAction
, trashShortcuts
);
108 trashAction
->setWhatsThis(xi18nc("@info:whatsthis", "This moves the "
109 "items in your current selection to the <filename>Trash"
110 "</filename>.<nl/>The trash is a temporary storage where "
111 "items can be deleted from if disk space is needed."));
113 auto deleteAction
= KStandardAction::deleteFile(this, &DolphinViewActionHandler::slotDeleteItems
, m_actionCollection
);
114 auto deleteShortcuts
= deleteAction
->shortcuts();
115 if (!deleteShortcuts
.contains(Qt::SHIFT
| Qt::Key_Delete
)) {
116 deleteShortcuts
.append(Qt::SHIFT
| Qt::Key_Delete
);
117 m_actionCollection
->setDefaultShortcuts(deleteAction
, deleteShortcuts
);
119 deleteAction
->setWhatsThis(xi18nc("@info:whatsthis", "This deletes "
120 "the items in your current selection completely. They can "
121 "not be recovered by normal means."));
123 // This action is useful for being enabled when KStandardAction::MoveToTrash should be
124 // disabled and KStandardAction::DeleteFile is enabled (e.g. non-local files), so that Key_Del
125 // can be used for deleting the file (#76016). It needs to be a separate action
126 // so that the Edit menu isn't affected.
127 QAction
* deleteWithTrashShortcut
= m_actionCollection
->addAction(QStringLiteral("delete_shortcut"));
128 // The descriptive text is just for the shortcuts editor.
129 deleteWithTrashShortcut
->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
130 m_actionCollection
->setDefaultShortcuts(deleteWithTrashShortcut
, KStandardShortcut::moveToTrash());
131 deleteWithTrashShortcut
->setEnabled(false);
132 connect(deleteWithTrashShortcut
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDeleteItems
);
134 QAction
* duplicateAction
= m_actionCollection
->addAction(QStringLiteral("duplicate"));
135 duplicateAction
->setText(i18nc("@action:inmenu File", "Duplicate Here"));
136 duplicateAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-duplicate")));
137 m_actionCollection
->setDefaultShortcut(duplicateAction
, Qt::CTRL
| Qt::Key_D
);
138 duplicateAction
->setEnabled(false);
139 connect(duplicateAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDuplicate
);
141 QAction
*propertiesAction
= m_actionCollection
->addAction( QStringLiteral("properties") );
142 // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
143 propertiesAction
->setText( i18nc("@action:inmenu File", "Properties") );
144 propertiesAction
->setWhatsThis(xi18nc("@info:whatsthis properties",
145 "This shows a complete list of properties of the currently "
146 "selected items in a new window.<nl/>If nothing is selected the "
147 "window will be about the currently viewed folder instead.<nl/>"
148 "You can configure advanced options there like managing "
149 "read- and write-permissions."));
150 propertiesAction
->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
151 m_actionCollection
->setDefaultShortcuts(propertiesAction
, {Qt::ALT
| Qt::Key_Return
, Qt::ALT
| Qt::Key_Enter
});
152 connect(propertiesAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotProperties
);
154 QAction
*copyPathAction
= m_actionCollection
->addAction( QStringLiteral("copy_location") );
155 copyPathAction
->setText(i18nc("@action:incontextmenu", "Copy Location"));
156 copyPathAction
->setWhatsThis(i18nc("@info:whatsthis copy_location",
157 "This will copy the path of the first selected item into the clipboard."
160 copyPathAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy-path")));
161 m_actionCollection
->setDefaultShortcuts(copyPathAction
, {Qt::CTRL
| Qt::ALT
| Qt::Key_C
});
162 connect(copyPathAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotCopyPath
);
164 // This menu makes sure that users who don't know how to open a context menu and haven't
165 // figured out how to enable the menu bar can still perform basic file manipulation.
166 // This only works if they know how to select a file.
167 // The text when nothing is selected at least implies that a selection can /somehow/ be made.
168 // This menu is by default only used in the hamburger menu but created here so users can put
169 // it on their toolbar.
170 KActionMenu
*basicActionsMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("basic_actions"), this);
171 // The text is set later depending on the selection in the currently active view.
172 basicActionsMenu
->setPopupMode(QToolButton::InstantPopup
);
173 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Cut
)));
174 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Copy
)));
175 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Paste
)));
176 basicActionsMenu
->addSeparator();
177 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
178 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
179 basicActionsMenu
->addSeparator();
180 basicActionsMenu
->addAction(m_actionCollection
->action(QStringLiteral("properties")));
181 basicActionsMenu
->addSeparator(); // We add one more separator because we sometimes add contextual
182 // actions in slotSelectionChanged() after the static ones above.
185 KToggleAction
* iconsAction
= iconsModeAction();
186 KToggleAction
* compactAction
= compactModeAction();
187 KToggleAction
* detailsAction
= detailsModeAction();
189 iconsAction
->setWhatsThis(xi18nc("@info:whatsthis Icons view mode",
190 "<para>This switches to a view mode that focuses on the folder "
191 "and file icons. This mode makes it easy to distinguish folders "
192 "from files and to detect items with distinctive <emphasis>"
193 "file types</emphasis>.</para><para> This mode is handy to "
194 "browse through pictures when the <interface>Preview"
195 "</interface> option is enabled.</para>"));
196 compactAction
->setWhatsThis(xi18nc("@info:whatsthis Compact view mode",
197 "<para>This switches to a compact view mode that lists the folders "
198 "and files in columns with the names beside the icons.</para><para>"
199 "This helps to keep the overview in folders with many items.</para>"));
200 detailsAction
->setWhatsThis(xi18nc("@info:whatsthis Details view mode",
201 "<para>This switches to a list view mode that focuses on folder "
202 "and file details.</para><para>Click on a detail in the column "
203 "header to sort the items by it. Click again to sort the other "
204 "way around. To select which details should be displayed click "
205 "the header with the right mouse button.</para><para>You can "
206 "view the contents of a folder without leaving the current "
207 "location by clicking to the left of it. This way you can view "
208 "the contents of multiple folders in the same list.</para>"));
210 KSelectAction
* viewModeActions
= m_actionCollection
->add
<KSelectAction
>(QStringLiteral("view_mode"));
211 viewModeActions
->setText(i18nc("@action:intoolbar", "View Mode"));
212 viewModeActions
->addAction(iconsAction
);
213 viewModeActions
->addAction(compactAction
);
214 viewModeActions
->addAction(detailsAction
);
215 viewModeActions
->setToolBarMode(KSelectAction::MenuMode
);
216 connect(viewModeActions
, &KSelectAction::triggered
, this, &DolphinViewActionHandler::slotViewModeActionTriggered
);
218 QAction
* zoomInAction
= KStandardAction::zoomIn(this,
219 &DolphinViewActionHandler::zoomIn
,
221 zoomInAction
->setWhatsThis(i18nc("@info:whatsthis zoom in", "This increases the icon size."));
223 QAction
* zoomResetAction
= m_actionCollection
->addAction(QStringLiteral("view_zoom_reset"));
224 zoomResetAction
->setText(i18nc("@action:inmenu View", "Reset Zoom Level"));
225 zoomResetAction
->setToolTip(i18n("Zoom To Default"));
226 zoomResetAction
->setWhatsThis(i18nc("@info:whatsthis zoom reset", "This resets the icon size to default."));
227 zoomResetAction
->setIcon(QIcon::fromTheme(QStringLiteral("zoom-original")));
228 m_actionCollection
->setDefaultShortcuts(zoomResetAction
, {Qt::CTRL
| Qt::Key_0
});
229 connect(zoomResetAction
, &QAction::triggered
, this, &DolphinViewActionHandler::zoomReset
);
231 QAction
* zoomOutAction
= KStandardAction::zoomOut(this,
232 &DolphinViewActionHandler::zoomOut
,
234 zoomOutAction
->setWhatsThis(i18nc("@info:whatsthis zoom out", "This reduces the icon size."));
236 KActionMenu
* zoomMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("zoom"));
237 zoomMenu
->setText(i18nc("@action:inmenu menu of zoom actions", "Zoom"));
238 zoomMenu
->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
239 zoomMenu
->setPopupMode(QToolButton::InstantPopup
);
240 zoomMenu
->addAction(zoomInAction
);
241 zoomMenu
->addAction(zoomResetAction
);
242 zoomMenu
->addAction(zoomOutAction
);
244 KToggleAction
* showPreview
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_preview"));
245 showPreview
->setText(i18nc("@action:intoolbar", "Show Previews"));
246 showPreview
->setToolTip(i18nc("@info", "Show preview of files and folders"));
247 showPreview
->setWhatsThis(xi18nc("@info:whatsthis", "When this is "
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 connect(showPreview
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::togglePreview
);
254 KToggleAction
* sortFoldersFirst
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("folders_first"));
255 sortFoldersFirst
->setText(i18nc("@action:inmenu Sort", "Folders First"));
256 connect(sortFoldersFirst
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleSortFoldersFirst
);
258 KToggleAction
* sortHiddenLast
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("hidden_last"));
259 sortHiddenLast
->setText(i18nc("@action:inmenu Sort", "Hidden Files Last"));
260 connect(sortHiddenLast
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleSortHiddenLast
);
263 QActionGroup
* sortByActionGroup
= createFileItemRolesActionGroup(QStringLiteral("sort_by_"));
265 KActionMenu
* sortByActionMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("sort"));
266 sortByActionMenu
->setIcon(QIcon::fromTheme(QStringLiteral("view-sort")));
267 sortByActionMenu
->setText(i18nc("@action:inmenu View", "Sort By"));
268 sortByActionMenu
->setPopupMode(QToolButton::InstantPopup
);
270 const auto sortByActionGroupActions
= sortByActionGroup
->actions();
271 for (QAction
* action
: sortByActionGroupActions
) {
272 sortByActionMenu
->addAction(action
);
275 sortByActionMenu
->addSeparator();
277 QActionGroup
* group
= new QActionGroup(sortByActionMenu
);
278 group
->setExclusive(true);
280 KToggleAction
* ascendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("ascending"));
281 ascendingAction
->setActionGroup(group
);
282 connect(ascendingAction
, &QAction::triggered
, this, [this] {
283 m_currentView
->setSortOrder(Qt::AscendingOrder
);
286 KToggleAction
* descendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("descending"));
287 descendingAction
->setActionGroup(group
);
288 connect(descendingAction
, &QAction::triggered
, this, [this] {
289 m_currentView
->setSortOrder(Qt::DescendingOrder
);
292 sortByActionMenu
->addAction(ascendingAction
);
293 sortByActionMenu
->addAction(descendingAction
);
294 sortByActionMenu
->addSeparator();
295 sortByActionMenu
->addAction(sortFoldersFirst
);
296 sortByActionMenu
->addAction(sortHiddenLast
);
298 // View -> Additional Information
299 QActionGroup
* visibleRolesGroup
= createFileItemRolesActionGroup(QStringLiteral("show_"));
301 KActionMenu
* visibleRolesMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("additional_info"));
302 visibleRolesMenu
->setText(i18nc("@action:inmenu View", "Show Additional Information"));
303 visibleRolesMenu
->setIcon(QIcon::fromTheme(QStringLiteral("documentinfo")));
304 visibleRolesMenu
->setPopupMode(QToolButton::InstantPopup
);
306 const auto visibleRolesGroupActions
= visibleRolesGroup
->actions();
307 for (QAction
* action
: visibleRolesGroupActions
) {
308 visibleRolesMenu
->addAction(action
);
311 KToggleAction
* showInGroups
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_in_groups"));
312 showInGroups
->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
313 showInGroups
->setText(i18nc("@action:inmenu View", "Show in Groups"));
314 showInGroups
->setWhatsThis(i18nc("@info:whatsthis", "This groups files and folders by their first letter."));
315 connect(showInGroups
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleGroupedSorting
);
317 KToggleAction
* showHiddenFiles
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_hidden_files"));
318 showHiddenFiles
->setIcon(QIcon::fromTheme(QStringLiteral("view-visible")));
319 showHiddenFiles
->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
320 showHiddenFiles
->setWhatsThis(xi18nc("@info:whatsthis", "<para>When "
321 "this is enabled <emphasis>hidden</emphasis> files and folders "
322 "are visible. They will be displayed semi-transparent.</para>"
323 "<para>Hidden items only differ from other ones in that their "
324 "name starts with a \".\". In general there is no need for "
325 "users to access them which is why they are hidden.</para>"));
326 m_actionCollection
->setDefaultShortcuts(showHiddenFiles
, KStandardShortcut::showHideHiddenFiles());
327 connect(showHiddenFiles
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleShowHiddenFiles
);
329 QAction
* adjustViewProps
= m_actionCollection
->addAction(QStringLiteral("view_properties"));
330 adjustViewProps
->setText(i18nc("@action:inmenu View", "Adjust View Display Style..."));
331 adjustViewProps
->setIcon(QIcon::fromTheme(QStringLiteral("view-choose")));
332 adjustViewProps
->setWhatsThis(i18nc("@info:whatsthis", "This opens a window "
333 "in which all folder view properties can be adjusted."));
334 connect(adjustViewProps
, &QAction::triggered
, this, &DolphinViewActionHandler::slotAdjustViewProperties
);
337 QActionGroup
* DolphinViewActionHandler::createFileItemRolesActionGroup(const QString
& groupPrefix
)
339 const bool isSortGroup
= (groupPrefix
== QLatin1String("sort_by_"));
340 Q_ASSERT(isSortGroup
|| groupPrefix
== QLatin1String("show_"));
342 QActionGroup
* rolesActionGroup
= new QActionGroup(m_actionCollection
);
343 rolesActionGroup
->setExclusive(isSortGroup
);
345 connect(rolesActionGroup
, &QActionGroup::triggered
,
346 this, &DolphinViewActionHandler::slotSortTriggered
);
348 connect(rolesActionGroup
, &QActionGroup::triggered
,
349 this, &DolphinViewActionHandler::toggleVisibleRole
);
353 KActionMenu
* groupMenu
= nullptr;
354 QActionGroup
* groupMenuGroup
= nullptr;
356 bool indexingEnabled
= false;
358 Baloo::IndexerConfig config
;
359 indexingEnabled
= config
.fileIndexingEnabled();
362 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
363 for (const KFileItemModel::RoleInfo
& info
: rolesInfo
) {
364 if (!isSortGroup
&& info
.role
== "text") {
365 // It should not be possible to hide the "text" role
369 KToggleAction
* action
= nullptr;
370 const QString name
= groupPrefix
+ info
.role
;
371 if (info
.group
.isEmpty()) {
372 action
= m_actionCollection
->add
<KToggleAction
>(name
);
373 action
->setActionGroup(rolesActionGroup
);
375 if (!groupMenu
|| info
.group
!= groupName
) {
376 groupName
= info
.group
;
377 groupMenu
= m_actionCollection
->add
<KActionMenu
>(groupName
);
378 groupMenu
->setText(groupName
);
379 groupMenu
->setActionGroup(rolesActionGroup
);
381 groupMenuGroup
= new QActionGroup(groupMenu
);
382 groupMenuGroup
->setExclusive(isSortGroup
);
384 connect(groupMenuGroup
, &QActionGroup::triggered
,
385 this, &DolphinViewActionHandler::slotSortTriggered
);
387 connect(groupMenuGroup
, &QActionGroup::triggered
,
388 this, &DolphinViewActionHandler::toggleVisibleRole
);
392 action
= new KToggleAction(groupMenu
);
393 action
->setActionGroup(groupMenuGroup
);
394 groupMenu
->addAction(action
);
396 action
->setText(info
.translation
);
397 action
->setData(info
.role
);
399 const bool enable
= (!info
.requiresBaloo
&& !info
.requiresIndexer
) ||
400 (info
.requiresBaloo
) ||
401 (info
.requiresIndexer
&& indexingEnabled
);
402 action
->setEnabled(enable
);
405 m_sortByActions
.insert(info
.role
, action
);
407 m_visibleRoles
.insert(info
.role
, action
);
411 return rolesActionGroup
;
414 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction
* action
)
416 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
417 m_currentView
->setMode(mode
);
419 QAction
* viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
420 viewModeMenu
->setIcon(action
->icon());
423 void DolphinViewActionHandler::slotRename()
425 Q_EMIT
actionBeingHandled();
426 m_currentView
->renameSelectedItems();
429 void DolphinViewActionHandler::slotTrashActivated()
431 Q_EMIT
actionBeingHandled();
432 m_currentView
->trashSelectedItems();
435 void DolphinViewActionHandler::slotDeleteItems()
437 Q_EMIT
actionBeingHandled();
438 m_currentView
->deleteSelectedItems();
441 void DolphinViewActionHandler::togglePreview(bool show
)
443 Q_EMIT
actionBeingHandled();
444 m_currentView
->setPreviewsShown(show
);
447 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown
)
450 // It is not enough to update the 'Show Preview' action, also
451 // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
455 QString
DolphinViewActionHandler::currentViewModeActionName() const
457 switch (m_currentView
->mode()) {
458 case DolphinView::IconsView
:
459 return QStringLiteral("icons");
460 case DolphinView::DetailsView
:
461 return QStringLiteral("details");
462 case DolphinView::CompactView
:
463 return QStringLiteral("compact");
468 return QString(); // can't happen
471 KActionCollection
* DolphinViewActionHandler::actionCollection()
473 return m_actionCollection
;
476 void DolphinViewActionHandler::updateViewActions()
478 QAction
* viewModeAction
= m_actionCollection
->action(currentViewModeActionName());
479 if (viewModeAction
) {
480 viewModeAction
->setChecked(true);
482 QAction
* viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
483 viewModeMenu
->setIcon(viewModeAction
->icon());
486 QAction
* showPreviewAction
= m_actionCollection
->action(QStringLiteral("show_preview"));
487 showPreviewAction
->setChecked(m_currentView
->previewsShown());
489 slotSortOrderChanged(m_currentView
->sortOrder());
490 slotSortFoldersFirstChanged(m_currentView
->sortFoldersFirst());
491 slotSortHiddenLastChanged(m_currentView
->sortHiddenLast());
492 slotVisibleRolesChanged(m_currentView
->visibleRoles(), QList
<QByteArray
>());
493 slotGroupedSortingChanged(m_currentView
->groupedSorting());
494 slotSortRoleChanged(m_currentView
->sortRole());
495 slotZoomLevelChanged(m_currentView
->zoomLevel(), -1);
497 // Updates the "show_hidden_files" action state and icon
498 slotHiddenFilesShownChanged(m_currentView
->hiddenFilesShown());
501 void DolphinViewActionHandler::zoomIn()
503 const int level
= m_currentView
->zoomLevel();
504 m_currentView
->setZoomLevel(level
+ 1);
508 void DolphinViewActionHandler::zoomOut()
510 const int level
= m_currentView
->zoomLevel();
511 m_currentView
->setZoomLevel(level
- 1);
515 void DolphinViewActionHandler::zoomReset()
517 m_currentView
->resetZoomLevel();
521 void DolphinViewActionHandler::toggleSortFoldersFirst()
523 const bool sortFirst
= m_currentView
->sortFoldersFirst();
524 m_currentView
->setSortFoldersFirst(!sortFirst
);
527 void DolphinViewActionHandler::toggleSortHiddenLast()
529 const bool sortHiddenLast
= m_currentView
->sortHiddenLast();
530 m_currentView
->setSortHiddenLast(!sortHiddenLast
);
533 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order
)
535 QAction
* descending
= m_actionCollection
->action(QStringLiteral("descending"));
536 QAction
* ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
537 const bool sortDescending
= (order
== Qt::DescendingOrder
);
538 descending
->setChecked(sortDescending
);
539 ascending
->setChecked(!sortDescending
);
542 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst
)
544 m_actionCollection
->action(QStringLiteral("folders_first"))->setChecked(foldersFirst
);
547 void DolphinViewActionHandler::slotSortHiddenLastChanged(bool hiddenLast
)
549 m_actionCollection
->action(QStringLiteral("hidden_last"))->setChecked(hiddenLast
);
552 void DolphinViewActionHandler::toggleVisibleRole(QAction
* action
)
554 Q_EMIT
actionBeingHandled();
556 const QByteArray toggledRole
= action
->data().toByteArray();
558 QList
<QByteArray
> roles
= m_currentView
->visibleRoles();
560 const bool show
= action
->isChecked();
562 const int index
= roles
.indexOf(toggledRole
);
563 const bool containsInfo
= (index
>= 0);
564 if (show
&& !containsInfo
) {
565 roles
.append(toggledRole
);
566 m_currentView
->setVisibleRoles(roles
);
567 } else if (!show
&& containsInfo
) {
568 roles
.removeAt(index
);
569 m_currentView
->setVisibleRoles(roles
);
570 Q_ASSERT(roles
.indexOf(toggledRole
) < 0);
574 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList
<QByteArray
>& current
,
575 const QList
<QByteArray
>& previous
)
579 const auto checkedRoles
= QSet
<QByteArray
>(current
.constBegin(), current
.constEnd());
580 QHashIterator
<QByteArray
, KToggleAction
*> it(m_visibleRoles
);
581 while (it
.hasNext()) {
583 const QByteArray
& role
= it
.key();
584 KToggleAction
* action
= it
.value();
585 action
->setChecked(checkedRoles
.contains(role
));
589 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped
)
591 m_currentView
->setGroupedSorting(grouped
);
594 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting
)
596 QAction
* showInGroupsAction
= m_actionCollection
->action(QStringLiteral("show_in_groups"));
597 showInGroupsAction
->setChecked(groupedSorting
);
600 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show
)
602 Q_EMIT
actionBeingHandled();
603 m_currentView
->setHiddenFilesShown(show
);
606 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown
)
608 QAction
* showHiddenFilesAction
= m_actionCollection
->action(QStringLiteral("show_hidden_files"));
609 showHiddenFilesAction
->setChecked(shown
);
612 void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable
)
614 m_actionCollection
->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable
&&
615 KProtocolManager::supportsMakeDir(currentView()->url()));
618 KToggleAction
* DolphinViewActionHandler::iconsModeAction()
620 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("icons"));
621 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
622 iconsView
->setToolTip(i18nc("@info", "Icons view mode"));
623 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_1
);
624 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
625 iconsView
->setData(QVariant::fromValue(DolphinView::IconsView
));
629 KToggleAction
* DolphinViewActionHandler::compactModeAction()
631 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("compact"));
632 iconsView
->setText(i18nc("@action:inmenu View Mode", "Compact"));
633 iconsView
->setToolTip(i18nc("@info", "Compact view mode"));
634 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_2
);
635 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
636 iconsView
->setData(QVariant::fromValue(DolphinView::CompactView
));
640 KToggleAction
* DolphinViewActionHandler::detailsModeAction()
642 KToggleAction
* detailsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("details"));
643 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
644 detailsView
->setToolTip(i18nc("@info", "Details view mode"));
645 m_actionCollection
->setDefaultShortcut(detailsView
, Qt::CTRL
| Qt::Key_3
);
646 detailsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
647 detailsView
->setData(QVariant::fromValue(DolphinView::DetailsView
));
651 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray
& role
)
653 KToggleAction
* action
= m_sortByActions
.value(role
);
655 action
->setChecked(true);
657 if (!action
->icon().isNull()) {
658 QAction
* sortByMenu
= m_actionCollection
->action(QStringLiteral("sort"));
659 sortByMenu
->setIcon(action
->icon());
663 QAction
* descending
= m_actionCollection
->action(QStringLiteral("descending"));
664 QAction
* ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
666 if (role
== "text" || role
== "type" || role
== "tags" || role
== "comment") {
667 descending
->setText(i18nc("Sort descending", "Z-A"));
668 ascending
->setText(i18nc("Sort ascending", "A-Z"));
669 } else if (role
== "size") {
670 descending
->setText(i18nc("Sort descending", "Largest First"));
671 ascending
->setText(i18nc("Sort ascending", "Smallest First"));
672 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
673 descending
->setText(i18nc("Sort descending", "Newest First"));
674 ascending
->setText(i18nc("Sort ascending", "Oldest First"));
675 } else if (role
== "rating") {
676 descending
->setText(i18nc("Sort descending", "Highest First"));
677 ascending
->setText(i18nc("Sort ascending", "Lowest First"));
679 descending
->setText(i18nc("Sort descending", "Descending"));
680 ascending
->setText(i18nc("Sort ascending", "Ascending"));
683 slotSortOrderChanged(m_currentView
->sortOrder());
686 void DolphinViewActionHandler::slotZoomLevelChanged(int current
, int previous
)
690 QAction
* zoomInAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomIn
));
692 zoomInAction
->setEnabled(current
< ZoomLevelInfo::maximumLevel());
695 QAction
* zoomOutAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomOut
));
697 zoomOutAction
->setEnabled(current
> ZoomLevelInfo::minimumLevel());
701 void DolphinViewActionHandler::slotSortTriggered(QAction
* action
)
703 // The radiobuttons of the "Sort By"-menu are split between the main-menu
704 // and several sub-menus. Because of this they don't have a common
705 // action-group that assures an exclusive toggle-state between the main-menu
706 // actions and the sub-menu-actions. If an action gets checked, it must
707 // be assured that all other actions get unchecked, except the ascending/
708 // descending actions
709 for (QAction
*groupAction
: qAsConst(m_sortByActions
)) {
710 KActionMenu
* actionMenu
= qobject_cast
<KActionMenu
*>(groupAction
);
712 const auto actions
= actionMenu
->menu()->actions();
713 for (QAction
* subAction
: actions
) {
714 subAction
->setChecked(false);
716 } else if (groupAction
->actionGroup()) {
717 groupAction
->setChecked(false);
720 action
->setChecked(true);
722 // Apply the activated sort-role to the view
723 const QByteArray role
= action
->data().toByteArray();
724 m_currentView
->setSortRole(role
);
727 void DolphinViewActionHandler::slotAdjustViewProperties()
729 Q_EMIT
actionBeingHandled();
730 QPointer
<ViewPropertiesDialog
> dialog
= new ViewPropertiesDialog(m_currentView
);
735 void DolphinViewActionHandler::slotDuplicate()
737 Q_EMIT
actionBeingHandled();
738 m_currentView
->duplicateSelectedItems();
741 void DolphinViewActionHandler::slotProperties()
743 KPropertiesDialog
* dialog
= nullptr;
744 const KFileItemList list
= m_currentView
->selectedItems();
745 if (list
.isEmpty()) {
746 const QUrl url
= m_currentView
->url();
747 dialog
= new KPropertiesDialog(url
, m_currentView
);
749 dialog
= new KPropertiesDialog(list
, m_currentView
);
752 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
755 dialog
->activateWindow();
758 void DolphinViewActionHandler::slotCopyPath()
760 m_currentView
->copyPathToClipboard();
763 void DolphinViewActionHandler::slotSelectionChanged(const KFileItemList
& selection
)
765 QString basicActionsMenuText
;
766 switch (selection
.count()) {
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");
773 basicActionsMenuText
=
774 i18nc("@action:inmenu menu with actions like copy, paste, rename. %1 is the name of the singular selected file/folder.",
775 "Actions for \"%1\"", selection
.first().name());
778 basicActionsMenuText
=
779 i18nc("@action:inmenu menu with actions like copy, paste, rename. %1 and %2 are names of files/folders.",
780 "Actions for \"%1\" and \"%2\"", selection
.first().name(), selection
.last().name());
783 basicActionsMenuText
=
784 i18nc("@action:inmenu menu with actions like copy, paste, rename. %1, %2 and %3 are names of files/folders.",
785 "Actions for \"%1\", \"%2\" and \"%3\"",
786 selection
.first().name(), selection
.at(1).name(), selection
.last().name());
789 basicActionsMenuText
= QString();
793 // At some point the added clarity from the text starts being less important than the menu width.
794 if (basicActionsMenuText
.isEmpty() || basicActionsMenuText
.length() > 40) {
795 const KFileItemListProperties
properties(selection
);
796 if (properties
.isFile()) {
797 basicActionsMenuText
=
798 i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
799 "Actions for One Selected File", "Actions for %1 Selected Files", selection
.count());
800 } else if (properties
.isDirectory()) {
801 basicActionsMenuText
=
802 i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
803 "Actions for One Selected Folder", "Actions for %1 Selected Folders", selection
.count());
805 basicActionsMenuText
=
806 i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
807 "Actions for One Selected Item", "Actions for %1 Selected Items", selection
.count());
811 QAction
*basicActionsMenu
= m_actionCollection
->action(QStringLiteral("basic_actions"));
812 basicActionsMenu
->setText(basicActionsMenuText
);
814 // Add or remove contextual actions
815 while (!basicActionsMenu
->menu()->actions().constLast()->isSeparator()) {
816 basicActionsMenu
->menu()->removeAction(basicActionsMenu
->menu()->actions().last());
818 if (selection
.count() == 1) {
819 if (selection
.first().isLink()) {
820 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("show_target")));
822 if (selection
.first().isDir()) {
823 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("add_to_places")));