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::visibleRolesChanged
,
61 this, &DolphinViewActionHandler::slotVisibleRolesChanged
);
62 connect(view
, &DolphinView::groupedSortingChanged
,
63 this, &DolphinViewActionHandler::slotGroupedSortingChanged
);
64 connect(view
, &DolphinView::hiddenFilesShownChanged
,
65 this, &DolphinViewActionHandler::slotHiddenFilesShownChanged
);
66 connect(view
, &DolphinView::sortRoleChanged
,
67 this, &DolphinViewActionHandler::slotSortRoleChanged
);
68 connect(view
, &DolphinView::zoomLevelChanged
,
69 this, &DolphinViewActionHandler::slotZoomLevelChanged
);
70 connect(view
, &DolphinView::writeStateChanged
,
71 this, &DolphinViewActionHandler::slotWriteStateChanged
);
72 connect(view
, &DolphinView::selectionChanged
,
73 this, &DolphinViewActionHandler::slotSelectionChanged
);
74 slotSelectionChanged(m_currentView
->selectedItems());
77 DolphinView
* DolphinViewActionHandler::currentView()
82 void DolphinViewActionHandler::createActions()
84 // This action doesn't appear in the GUI, it's for the shortcut only.
85 // KNewFileMenu takes care of the GUI stuff.
86 QAction
* newDirAction
= m_actionCollection
->addAction(QStringLiteral("create_dir"));
87 newDirAction
->setText(i18nc("@action", "Create Folder..."));
88 m_actionCollection
->setDefaultShortcuts(newDirAction
, KStandardShortcut::createFolder());
89 newDirAction
->setIcon(QIcon::fromTheme(QStringLiteral("folder-new")));
90 newDirAction
->setEnabled(false); // Will be enabled in slotWriteStateChanged(bool) if the current URL is writable
91 connect(newDirAction
, &QAction::triggered
, this, &DolphinViewActionHandler::createDirectoryTriggered
);
95 auto renameAction
= KStandardAction::renameFile(this, &DolphinViewActionHandler::slotRename
, m_actionCollection
);
96 renameAction
->setWhatsThis(xi18nc("@info:whatsthis", "This renames the "
97 "items in your current selection.<nl/>Renaming multiple items "
98 "at once amounts to their new names differing only in a number."));
100 auto trashAction
= KStandardAction::moveToTrash(this, &DolphinViewActionHandler::slotTrashActivated
, m_actionCollection
);
101 auto trashShortcuts
= trashAction
->shortcuts();
102 if (!trashShortcuts
.contains(QKeySequence::Delete
)) {
103 trashShortcuts
.append(QKeySequence::Delete
);
104 m_actionCollection
->setDefaultShortcuts(trashAction
, trashShortcuts
);
106 trashAction
->setWhatsThis(xi18nc("@info:whatsthis", "This moves the "
107 "items in your current selection to the <filename>Trash"
108 "</filename>.<nl/>The trash is a temporary storage where "
109 "items can be deleted from if disk space is needed."));
111 auto deleteAction
= KStandardAction::deleteFile(this, &DolphinViewActionHandler::slotDeleteItems
, m_actionCollection
);
112 auto deleteShortcuts
= deleteAction
->shortcuts();
113 if (!deleteShortcuts
.contains(Qt::SHIFT
| Qt::Key_Delete
)) {
114 deleteShortcuts
.append(Qt::SHIFT
| Qt::Key_Delete
);
115 m_actionCollection
->setDefaultShortcuts(deleteAction
, deleteShortcuts
);
117 deleteAction
->setWhatsThis(xi18nc("@info:whatsthis", "This deletes "
118 "the items in your current selection completely. They can "
119 "not be recovered by normal means."));
121 // This action is useful for being enabled when KStandardAction::MoveToTrash should be
122 // disabled and KStandardAction::DeleteFile is enabled (e.g. non-local files), so that Key_Del
123 // can be used for deleting the file (#76016). It needs to be a separate action
124 // so that the Edit menu isn't affected.
125 QAction
* deleteWithTrashShortcut
= m_actionCollection
->addAction(QStringLiteral("delete_shortcut"));
126 // The descriptive text is just for the shortcuts editor.
127 deleteWithTrashShortcut
->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
128 m_actionCollection
->setDefaultShortcuts(deleteWithTrashShortcut
, KStandardShortcut::moveToTrash());
129 deleteWithTrashShortcut
->setEnabled(false);
130 connect(deleteWithTrashShortcut
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDeleteItems
);
132 QAction
* duplicateAction
= m_actionCollection
->addAction(QStringLiteral("duplicate"));
133 duplicateAction
->setText(i18nc("@action:inmenu File", "Duplicate Here"));
134 duplicateAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-duplicate")));
135 m_actionCollection
->setDefaultShortcut(duplicateAction
, Qt::CTRL
| Qt::Key_D
);
136 duplicateAction
->setEnabled(false);
137 connect(duplicateAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDuplicate
);
139 QAction
*propertiesAction
= m_actionCollection
->addAction( QStringLiteral("properties") );
140 // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
141 propertiesAction
->setText( i18nc("@action:inmenu File", "Properties") );
142 propertiesAction
->setWhatsThis(xi18nc("@info:whatsthis properties",
143 "This shows a complete list of properties of the currently "
144 "selected items in a new window.<nl/>If nothing is selected the "
145 "window will be about the currently viewed folder instead.<nl/>"
146 "You can configure advanced options there like managing "
147 "read- and write-permissions."));
148 propertiesAction
->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
149 m_actionCollection
->setDefaultShortcuts(propertiesAction
, {Qt::ALT
| Qt::Key_Return
, Qt::ALT
| Qt::Key_Enter
});
150 connect(propertiesAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotProperties
);
152 QAction
*copyPathAction
= m_actionCollection
->addAction( QStringLiteral("copy_location") );
153 copyPathAction
->setText(i18nc("@action:incontextmenu", "Copy Location"));
154 copyPathAction
->setWhatsThis(i18nc("@info:whatsthis copy_location",
155 "This will copy the path of the first selected item into the clipboard."
158 copyPathAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy")));
159 m_actionCollection
->setDefaultShortcuts(copyPathAction
, {Qt::CTRL
| Qt::ALT
| Qt::Key_C
});
160 connect(copyPathAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotCopyPath
);
162 // This menu makes sure that users who don't know how to open a context menu and haven't
163 // figured out how to enable the menu bar can still perform basic file manipulation.
164 // This only works if they know how to select a file.
165 // The text when nothing is selected at least implies that a selection can /somehow/ be made.
166 // This menu is by default only used in the hamburger menu but created here so users can put
167 // it on their toolbar.
168 KActionMenu
*basicActionsMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("basic_actions"), this);
169 // The text is set later depending on the selection in the currently active view.
170 basicActionsMenu
->setPopupMode(QToolButton::InstantPopup
);
171 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Cut
)));
172 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Copy
)));
173 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Paste
)));
174 basicActionsMenu
->addSeparator();
175 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
176 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
177 basicActionsMenu
->addSeparator();
178 basicActionsMenu
->addAction(m_actionCollection
->action(QStringLiteral("properties")));
179 basicActionsMenu
->addSeparator(); // We add one more separator because we sometimes add contextual
180 // actions in slotSelectionChanged() after the static ones above.
183 KToggleAction
* iconsAction
= iconsModeAction();
184 KToggleAction
* compactAction
= compactModeAction();
185 KToggleAction
* detailsAction
= detailsModeAction();
187 iconsAction
->setWhatsThis(xi18nc("@info:whatsthis Icons view mode",
188 "<para>This switches to a view mode that focuses on the folder "
189 "and file icons. This mode makes it easy to distinguish folders "
190 "from files and to detect items with distinctive <emphasis>"
191 "file types</emphasis>.</para><para> This mode is handy to "
192 "browse through pictures when the <interface>Preview"
193 "</interface> option is enabled.</para>"));
194 compactAction
->setWhatsThis(xi18nc("@info:whatsthis Compact view mode",
195 "<para>This switches to a compact view mode that lists the folders "
196 "and files in columns with the names beside the icons.</para><para>"
197 "This helps to keep the overview in folders with many items.</para>"));
198 detailsAction
->setWhatsThis(xi18nc("@info:whatsthis Details view mode",
199 "<para>This switches to a list view mode that focuses on folder "
200 "and file details.</para><para>Click on a detail in the column "
201 "header to sort the items by it. Click again to sort the other "
202 "way around. To select which details should be displayed click "
203 "the header with the right mouse button.</para><para>You can "
204 "view the contents of a folder without leaving the current "
205 "location by clicking to the left of it. This way you can view "
206 "the contents of multiple folders in the same list.</para>"));
208 KSelectAction
* viewModeActions
= m_actionCollection
->add
<KSelectAction
>(QStringLiteral("view_mode"));
209 viewModeActions
->setText(i18nc("@action:intoolbar", "View Mode"));
210 viewModeActions
->addAction(iconsAction
);
211 viewModeActions
->addAction(compactAction
);
212 viewModeActions
->addAction(detailsAction
);
213 viewModeActions
->setToolBarMode(KSelectAction::MenuMode
);
214 connect(viewModeActions
, QOverload
<QAction
*>::of(&KSelectAction::triggered
), this, &DolphinViewActionHandler::slotViewModeActionTriggered
);
216 QAction
* zoomInAction
= KStandardAction::zoomIn(this,
217 &DolphinViewActionHandler::zoomIn
,
219 zoomInAction
->setWhatsThis(i18nc("@info:whatsthis zoom in", "This increases the icon size."));
221 QAction
* zoomResetAction
= m_actionCollection
->addAction(QStringLiteral("view_zoom_reset"));
222 zoomResetAction
->setText(i18nc("@action:inmenu View", "Reset Zoom Level"));
223 zoomResetAction
->setToolTip(i18n("Zoom To Default"));
224 zoomResetAction
->setWhatsThis(i18nc("@info:whatsthis zoom reset", "This resets the icon size to default."));
225 zoomResetAction
->setIcon(QIcon::fromTheme(QStringLiteral("zoom-original")));
226 m_actionCollection
->setDefaultShortcuts(zoomResetAction
, {Qt::CTRL
| Qt::Key_0
});
227 connect(zoomResetAction
, &QAction::triggered
, this, &DolphinViewActionHandler::zoomReset
);
229 QAction
* zoomOutAction
= KStandardAction::zoomOut(this,
230 &DolphinViewActionHandler::zoomOut
,
232 zoomOutAction
->setWhatsThis(i18nc("@info:whatsthis zoom out", "This reduces the icon size."));
234 KActionMenu
* zoomMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("zoom"));
235 zoomMenu
->setText(i18nc("@action:inmenu menu of zoom actions", "Zoom"));
236 zoomMenu
->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
237 zoomMenu
->setPopupMode(QToolButton::InstantPopup
);
238 zoomMenu
->addAction(zoomInAction
);
239 zoomMenu
->addAction(zoomResetAction
);
240 zoomMenu
->addAction(zoomOutAction
);
242 KToggleAction
* showPreview
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_preview"));
243 showPreview
->setText(i18nc("@action:intoolbar", "Show Previews"));
244 showPreview
->setToolTip(i18nc("@info", "Show preview of files and folders"));
245 showPreview
->setWhatsThis(xi18nc("@info:whatsthis", "When this is "
246 "enabled, the icons are based on the actual file or folder "
247 "contents.<nl/>For example the icons of images become scaled "
248 "down versions of the images."));
249 showPreview
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
250 connect(showPreview
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::togglePreview
);
252 KToggleAction
* sortFoldersFirst
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("folders_first"));
253 sortFoldersFirst
->setText(i18nc("@action:inmenu Sort", "Folders First"));
254 connect(sortFoldersFirst
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleSortFoldersFirst
);
257 QActionGroup
* sortByActionGroup
= createFileItemRolesActionGroup(QStringLiteral("sort_by_"));
259 KActionMenu
* sortByActionMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("sort"));
260 sortByActionMenu
->setIcon(QIcon::fromTheme(QStringLiteral("view-sort")));
261 sortByActionMenu
->setText(i18nc("@action:inmenu View", "Sort By"));
262 sortByActionMenu
->setPopupMode(QToolButton::InstantPopup
);
264 const auto sortByActionGroupActions
= sortByActionGroup
->actions();
265 for (QAction
* action
: sortByActionGroupActions
) {
266 sortByActionMenu
->addAction(action
);
269 sortByActionMenu
->addSeparator();
271 QActionGroup
* group
= new QActionGroup(sortByActionMenu
);
272 group
->setExclusive(true);
274 KToggleAction
* ascendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("ascending"));
275 ascendingAction
->setActionGroup(group
);
276 connect(ascendingAction
, &QAction::triggered
, this, [this] {
277 m_currentView
->setSortOrder(Qt::AscendingOrder
);
280 KToggleAction
* descendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("descending"));
281 descendingAction
->setActionGroup(group
);
282 connect(descendingAction
, &QAction::triggered
, this, [this] {
283 m_currentView
->setSortOrder(Qt::DescendingOrder
);
286 sortByActionMenu
->addAction(ascendingAction
);
287 sortByActionMenu
->addAction(descendingAction
);
288 sortByActionMenu
->addSeparator();
289 sortByActionMenu
->addAction(sortFoldersFirst
);
291 // View -> Additional Information
292 QActionGroup
* visibleRolesGroup
= createFileItemRolesActionGroup(QStringLiteral("show_"));
294 KActionMenu
* visibleRolesMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("additional_info"));
295 visibleRolesMenu
->setText(i18nc("@action:inmenu View", "Show Additional Information"));
296 visibleRolesMenu
->setIcon(QIcon::fromTheme(QStringLiteral("documentinfo")));
297 visibleRolesMenu
->setPopupMode(QToolButton::InstantPopup
);
299 const auto visibleRolesGroupActions
= visibleRolesGroup
->actions();
300 for (QAction
* action
: visibleRolesGroupActions
) {
301 visibleRolesMenu
->addAction(action
);
304 KToggleAction
* showInGroups
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_in_groups"));
305 showInGroups
->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
306 showInGroups
->setText(i18nc("@action:inmenu View", "Show in Groups"));
307 showInGroups
->setWhatsThis(i18nc("@info:whatsthis", "This groups files and folders by their first letter."));
308 connect(showInGroups
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleGroupedSorting
);
310 KToggleAction
* showHiddenFiles
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_hidden_files"));
311 showHiddenFiles
->setIcon(QIcon::fromTheme(QStringLiteral("view-visible")));
312 showHiddenFiles
->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
313 showHiddenFiles
->setWhatsThis(xi18nc("@info:whatsthis", "<para>When "
314 "this is enabled <emphasis>hidden</emphasis> files and folders "
315 "are visible. They will be displayed semi-transparent.</para>"
316 "<para>Hidden items only differ from other ones in that their "
317 "name starts with a \".\". In general there is no need for "
318 "users to access them which is why they are hidden.</para>"));
319 m_actionCollection
->setDefaultShortcuts(showHiddenFiles
, KStandardShortcut::showHideHiddenFiles());
320 connect(showHiddenFiles
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleShowHiddenFiles
);
322 QAction
* adjustViewProps
= m_actionCollection
->addAction(QStringLiteral("view_properties"));
323 adjustViewProps
->setText(i18nc("@action:inmenu View", "Adjust View Display Style..."));
324 adjustViewProps
->setIcon(QIcon::fromTheme(QStringLiteral("view-choose")));
325 adjustViewProps
->setWhatsThis(i18nc("@info:whatsthis", "This opens a window "
326 "in which all folder view properties can be adjusted."));
327 connect(adjustViewProps
, &QAction::triggered
, this, &DolphinViewActionHandler::slotAdjustViewProperties
);
330 QActionGroup
* DolphinViewActionHandler::createFileItemRolesActionGroup(const QString
& groupPrefix
)
332 const bool isSortGroup
= (groupPrefix
== QLatin1String("sort_by_"));
333 Q_ASSERT(isSortGroup
|| groupPrefix
== QLatin1String("show_"));
335 QActionGroup
* rolesActionGroup
= new QActionGroup(m_actionCollection
);
336 rolesActionGroup
->setExclusive(isSortGroup
);
338 connect(rolesActionGroup
, &QActionGroup::triggered
,
339 this, &DolphinViewActionHandler::slotSortTriggered
);
341 connect(rolesActionGroup
, &QActionGroup::triggered
,
342 this, &DolphinViewActionHandler::toggleVisibleRole
);
346 KActionMenu
* groupMenu
= nullptr;
347 QActionGroup
* groupMenuGroup
= nullptr;
349 bool indexingEnabled
= false;
351 Baloo::IndexerConfig config
;
352 indexingEnabled
= config
.fileIndexingEnabled();
355 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
356 for (const KFileItemModel::RoleInfo
& info
: rolesInfo
) {
357 if (!isSortGroup
&& info
.role
== "text") {
358 // It should not be possible to hide the "text" role
362 KToggleAction
* action
= nullptr;
363 const QString name
= groupPrefix
+ info
.role
;
364 if (info
.group
.isEmpty()) {
365 action
= m_actionCollection
->add
<KToggleAction
>(name
);
366 action
->setActionGroup(rolesActionGroup
);
368 if (!groupMenu
|| info
.group
!= groupName
) {
369 groupName
= info
.group
;
370 groupMenu
= m_actionCollection
->add
<KActionMenu
>(groupName
);
371 groupMenu
->setText(groupName
);
372 groupMenu
->setActionGroup(rolesActionGroup
);
374 groupMenuGroup
= new QActionGroup(groupMenu
);
375 groupMenuGroup
->setExclusive(isSortGroup
);
377 connect(groupMenuGroup
, &QActionGroup::triggered
,
378 this, &DolphinViewActionHandler::slotSortTriggered
);
380 connect(groupMenuGroup
, &QActionGroup::triggered
,
381 this, &DolphinViewActionHandler::toggleVisibleRole
);
385 action
= new KToggleAction(groupMenu
);
386 action
->setActionGroup(groupMenuGroup
);
387 groupMenu
->addAction(action
);
389 action
->setText(info
.translation
);
390 action
->setData(info
.role
);
392 const bool enable
= (!info
.requiresBaloo
&& !info
.requiresIndexer
) ||
393 (info
.requiresBaloo
) ||
394 (info
.requiresIndexer
&& indexingEnabled
);
395 action
->setEnabled(enable
);
398 m_sortByActions
.insert(info
.role
, action
);
400 m_visibleRoles
.insert(info
.role
, action
);
404 return rolesActionGroup
;
407 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction
* action
)
409 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
410 m_currentView
->setMode(mode
);
412 QAction
* viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
413 viewModeMenu
->setIcon(action
->icon());
416 void DolphinViewActionHandler::slotRename()
418 Q_EMIT
actionBeingHandled();
419 m_currentView
->renameSelectedItems();
422 void DolphinViewActionHandler::slotTrashActivated()
424 Q_EMIT
actionBeingHandled();
425 m_currentView
->trashSelectedItems();
428 void DolphinViewActionHandler::slotDeleteItems()
430 Q_EMIT
actionBeingHandled();
431 m_currentView
->deleteSelectedItems();
434 void DolphinViewActionHandler::togglePreview(bool show
)
436 Q_EMIT
actionBeingHandled();
437 m_currentView
->setPreviewsShown(show
);
440 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown
)
443 // It is not enough to update the 'Show Preview' action, also
444 // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
448 QString
DolphinViewActionHandler::currentViewModeActionName() const
450 switch (m_currentView
->mode()) {
451 case DolphinView::IconsView
:
452 return QStringLiteral("icons");
453 case DolphinView::DetailsView
:
454 return QStringLiteral("details");
455 case DolphinView::CompactView
:
456 return QStringLiteral("compact");
461 return QString(); // can't happen
464 KActionCollection
* DolphinViewActionHandler::actionCollection()
466 return m_actionCollection
;
469 void DolphinViewActionHandler::updateViewActions()
471 QAction
* viewModeAction
= m_actionCollection
->action(currentViewModeActionName());
472 if (viewModeAction
) {
473 viewModeAction
->setChecked(true);
475 QAction
* viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
476 viewModeMenu
->setIcon(viewModeAction
->icon());
479 QAction
* showPreviewAction
= m_actionCollection
->action(QStringLiteral("show_preview"));
480 showPreviewAction
->setChecked(m_currentView
->previewsShown());
482 slotSortOrderChanged(m_currentView
->sortOrder());
483 slotSortFoldersFirstChanged(m_currentView
->sortFoldersFirst());
484 slotVisibleRolesChanged(m_currentView
->visibleRoles(), QList
<QByteArray
>());
485 slotGroupedSortingChanged(m_currentView
->groupedSorting());
486 slotSortRoleChanged(m_currentView
->sortRole());
487 slotZoomLevelChanged(m_currentView
->zoomLevel(), -1);
489 // Updates the "show_hidden_files" action state and icon
490 slotHiddenFilesShownChanged(m_currentView
->hiddenFilesShown());
493 void DolphinViewActionHandler::zoomIn()
495 const int level
= m_currentView
->zoomLevel();
496 m_currentView
->setZoomLevel(level
+ 1);
500 void DolphinViewActionHandler::zoomOut()
502 const int level
= m_currentView
->zoomLevel();
503 m_currentView
->setZoomLevel(level
- 1);
507 void DolphinViewActionHandler::zoomReset()
509 m_currentView
->resetZoomLevel();
513 void DolphinViewActionHandler::toggleSortFoldersFirst()
515 const bool sortFirst
= m_currentView
->sortFoldersFirst();
516 m_currentView
->setSortFoldersFirst(!sortFirst
);
519 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order
)
521 QAction
* descending
= m_actionCollection
->action(QStringLiteral("descending"));
522 QAction
* ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
523 const bool sortDescending
= (order
== Qt::DescendingOrder
);
524 descending
->setChecked(sortDescending
);
525 ascending
->setChecked(!sortDescending
);
528 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst
)
530 m_actionCollection
->action(QStringLiteral("folders_first"))->setChecked(foldersFirst
);
533 void DolphinViewActionHandler::toggleVisibleRole(QAction
* action
)
535 Q_EMIT
actionBeingHandled();
537 const QByteArray toggledRole
= action
->data().toByteArray();
539 QList
<QByteArray
> roles
= m_currentView
->visibleRoles();
541 const bool show
= action
->isChecked();
543 const int index
= roles
.indexOf(toggledRole
);
544 const bool containsInfo
= (index
>= 0);
545 if (show
&& !containsInfo
) {
546 roles
.append(toggledRole
);
547 m_currentView
->setVisibleRoles(roles
);
548 } else if (!show
&& containsInfo
) {
549 roles
.removeAt(index
);
550 m_currentView
->setVisibleRoles(roles
);
551 Q_ASSERT(roles
.indexOf(toggledRole
) < 0);
555 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList
<QByteArray
>& current
,
556 const QList
<QByteArray
>& previous
)
560 const auto checkedRoles
= QSet
<QByteArray
>(current
.constBegin(), current
.constEnd());
561 QHashIterator
<QByteArray
, KToggleAction
*> it(m_visibleRoles
);
562 while (it
.hasNext()) {
564 const QByteArray
& role
= it
.key();
565 KToggleAction
* action
= it
.value();
566 action
->setChecked(checkedRoles
.contains(role
));
570 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped
)
572 m_currentView
->setGroupedSorting(grouped
);
575 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting
)
577 QAction
* showInGroupsAction
= m_actionCollection
->action(QStringLiteral("show_in_groups"));
578 showInGroupsAction
->setChecked(groupedSorting
);
581 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show
)
583 Q_EMIT
actionBeingHandled();
584 m_currentView
->setHiddenFilesShown(show
);
587 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown
)
589 QAction
* showHiddenFilesAction
= m_actionCollection
->action(QStringLiteral("show_hidden_files"));
590 showHiddenFilesAction
->setChecked(shown
);
593 void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable
)
595 m_actionCollection
->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable
&&
596 KProtocolManager::supportsMakeDir(currentView()->url()));
599 KToggleAction
* DolphinViewActionHandler::iconsModeAction()
601 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("icons"));
602 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
603 iconsView
->setToolTip(i18nc("@info", "Icons view mode"));
604 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_1
);
605 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
606 iconsView
->setData(QVariant::fromValue(DolphinView::IconsView
));
610 KToggleAction
* DolphinViewActionHandler::compactModeAction()
612 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("compact"));
613 iconsView
->setText(i18nc("@action:inmenu View Mode", "Compact"));
614 iconsView
->setToolTip(i18nc("@info", "Compact view mode"));
615 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_2
);
616 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
617 iconsView
->setData(QVariant::fromValue(DolphinView::CompactView
));
621 KToggleAction
* DolphinViewActionHandler::detailsModeAction()
623 KToggleAction
* detailsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("details"));
624 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
625 detailsView
->setToolTip(i18nc("@info", "Details view mode"));
626 m_actionCollection
->setDefaultShortcut(detailsView
, Qt::CTRL
| Qt::Key_3
);
627 detailsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
628 detailsView
->setData(QVariant::fromValue(DolphinView::DetailsView
));
632 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray
& role
)
634 KToggleAction
* action
= m_sortByActions
.value(role
);
636 action
->setChecked(true);
638 if (!action
->icon().isNull()) {
639 QAction
* sortByMenu
= m_actionCollection
->action(QStringLiteral("sort"));
640 sortByMenu
->setIcon(action
->icon());
644 QAction
* descending
= m_actionCollection
->action(QStringLiteral("descending"));
645 QAction
* ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
647 if (role
== "text" || role
== "type" || role
== "tags" || role
== "comment") {
648 descending
->setText(i18nc("Sort descending", "Z-A"));
649 ascending
->setText(i18nc("Sort ascending", "A-Z"));
650 } else if (role
== "size") {
651 descending
->setText(i18nc("Sort descending", "Largest First"));
652 ascending
->setText(i18nc("Sort ascending", "Smallest First"));
653 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
654 descending
->setText(i18nc("Sort descending", "Newest First"));
655 ascending
->setText(i18nc("Sort ascending", "Oldest First"));
656 } else if (role
== "rating") {
657 descending
->setText(i18nc("Sort descending", "Highest First"));
658 ascending
->setText(i18nc("Sort ascending", "Lowest First"));
660 descending
->setText(i18nc("Sort descending", "Descending"));
661 ascending
->setText(i18nc("Sort ascending", "Ascending"));
664 slotSortOrderChanged(m_currentView
->sortOrder());
667 void DolphinViewActionHandler::slotZoomLevelChanged(int current
, int previous
)
671 QAction
* zoomInAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomIn
));
673 zoomInAction
->setEnabled(current
< ZoomLevelInfo::maximumLevel());
676 QAction
* zoomOutAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomOut
));
678 zoomOutAction
->setEnabled(current
> ZoomLevelInfo::minimumLevel());
682 void DolphinViewActionHandler::slotSortTriggered(QAction
* action
)
684 // The radiobuttons of the "Sort By"-menu are split between the main-menu
685 // and several sub-menus. Because of this they don't have a common
686 // action-group that assures an exclusive toggle-state between the main-menu
687 // actions and the sub-menu-actions. If an action gets checked, it must
688 // be assured that all other actions get unchecked, except the ascending/
689 // descending actions
690 for (QAction
*groupAction
: qAsConst(m_sortByActions
)) {
691 KActionMenu
* actionMenu
= qobject_cast
<KActionMenu
*>(groupAction
);
693 const auto actions
= actionMenu
->menu()->actions();
694 for (QAction
* subAction
: actions
) {
695 subAction
->setChecked(false);
697 } else if (groupAction
->actionGroup()) {
698 groupAction
->setChecked(false);
701 action
->setChecked(true);
703 // Apply the activated sort-role to the view
704 const QByteArray role
= action
->data().toByteArray();
705 m_currentView
->setSortRole(role
);
708 void DolphinViewActionHandler::slotAdjustViewProperties()
710 Q_EMIT
actionBeingHandled();
711 QPointer
<ViewPropertiesDialog
> dialog
= new ViewPropertiesDialog(m_currentView
);
716 void DolphinViewActionHandler::slotDuplicate()
718 Q_EMIT
actionBeingHandled();
719 m_currentView
->duplicateSelectedItems();
722 void DolphinViewActionHandler::slotProperties()
724 KPropertiesDialog
* dialog
= nullptr;
725 const KFileItemList list
= m_currentView
->selectedItems();
726 if (list
.isEmpty()) {
727 const QUrl url
= m_currentView
->url();
728 dialog
= new KPropertiesDialog(url
, m_currentView
);
730 dialog
= new KPropertiesDialog(list
, m_currentView
);
733 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
736 dialog
->activateWindow();
739 void DolphinViewActionHandler::slotCopyPath()
741 m_currentView
->copyPathToClipboard();
744 void DolphinViewActionHandler::slotSelectionChanged(const KFileItemList
& selection
)
746 QString basicActionsMenuText
;
747 switch (selection
.count()) {
749 basicActionsMenuText
=
750 i18nc("@action:inmenu menu with actions like copy, paste, rename. The user's selection is empty when this text is shown.",
751 "Actions for Current View");
754 basicActionsMenuText
=
755 i18nc("@action:inmenu menu with actions like copy, paste, rename. %1 is the name of the singular selected file/folder.",
756 "Actions for \"%1\"", selection
.first().name());
759 basicActionsMenuText
=
760 i18nc("@action:inmenu menu with actions like copy, paste, rename. %1 and %2 are names of files/folders.",
761 "Actions for \"%1\" and \"%2\"", selection
.first().name(), selection
.last().name());
764 basicActionsMenuText
=
765 i18nc("@action:inmenu menu with actions like copy, paste, rename. %1, %2 and %3 are names of files/folders.",
766 "Actions for \"%1\", \"%2\" and \"%3\"",
767 selection
.first().name(), selection
.at(1).name(), selection
.last().name());
770 basicActionsMenuText
= QString();
774 // At some point the added clarity from the text starts being less important than the menu width.
775 if (basicActionsMenuText
.isEmpty() || basicActionsMenuText
.length() > 40) {
776 const KFileItemListProperties
properties(selection
);
777 if (properties
.isFile()) {
778 basicActionsMenuText
=
779 i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
780 "Actions for One Selected File", "Actions for %1 Selected Files", selection
.count());
781 } else if (properties
.isDirectory()) {
782 basicActionsMenuText
=
783 i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
784 "Actions for One Selected Folder", "Actions for %1 Selected Folders", selection
.count());
786 basicActionsMenuText
=
787 i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
788 "Actions for One Selected Item", "Actions for %1 Selected Items", selection
.count());
792 QAction
*basicActionsMenu
= m_actionCollection
->action(QStringLiteral("basic_actions"));
793 basicActionsMenu
->setText(basicActionsMenuText
);
795 // Add or remove contextual actions
796 while (!basicActionsMenu
->menu()->actions().constLast()->isSeparator()) {
797 basicActionsMenu
->menu()->removeAction(basicActionsMenu
->menu()->actions().last());
799 if (selection
.count() == 1) {
800 if (selection
.first().isLink()) {
801 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("show_target")));
803 if (selection
.first().isDir()) {
804 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("add_to_places")));