2 * SPDX-FileCopyrightText: 2008 David Faure <faure@kde.org>
3 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
5 * SPDX-License-Identifier: GPL-2.0-or-later
8 #include "dolphinviewactionhandler.h"
10 #include "kitemviews/kfileitemlisttostring.h"
11 #include "kitemviews/kfileitemmodel.h"
12 #include "selectionmode/actiontexthelper.h"
13 #include "settings/viewpropertiesdialog.h"
14 #include "views/zoomlevelinfo.h"
17 #include <Baloo/IndexerConfig>
19 #include <KActionCollection>
20 #include <KActionMenu>
21 #include <KFileItemListProperties>
22 #include <KLocalizedString>
23 #include <KNewFileMenu>
24 #include <KPropertiesDialog>
25 #include <KProtocolManager>
27 #include <QActionGroup>
31 DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection
*collection
, SelectionMode::ActionTextHelper
*actionTextHelper
, QObject
*parent
)
33 , m_actionCollection(collection
)
34 , m_currentView(nullptr)
38 Q_ASSERT(m_actionCollection
);
39 createActions(actionTextHelper
);
42 void DolphinViewActionHandler::setCurrentView(DolphinView
*view
)
47 disconnect(m_currentView
, nullptr, this, nullptr);
52 connect(view
, &DolphinView::modeChanged
, this, &DolphinViewActionHandler::updateViewActions
);
53 connect(view
, &DolphinView::previewsShownChanged
, this, &DolphinViewActionHandler::slotPreviewsShownChanged
);
54 connect(view
, &DolphinView::sortOrderChanged
, this, &DolphinViewActionHandler::slotSortOrderChanged
);
55 connect(view
, &DolphinView::sortFoldersFirstChanged
, this, &DolphinViewActionHandler::slotSortFoldersFirstChanged
);
56 connect(view
, &DolphinView::sortHiddenLastChanged
, this, &DolphinViewActionHandler::slotSortHiddenLastChanged
);
57 connect(view
, &DolphinView::visibleRolesChanged
, this, &DolphinViewActionHandler::slotVisibleRolesChanged
);
58 connect(view
, &DolphinView::groupedSortingChanged
, this, &DolphinViewActionHandler::slotGroupedSortingChanged
);
59 connect(view
, &DolphinView::hiddenFilesShownChanged
, this, &DolphinViewActionHandler::slotHiddenFilesShownChanged
);
60 connect(view
, &DolphinView::sortRoleChanged
, this, &DolphinViewActionHandler::slotSortRoleChanged
);
61 connect(view
, &DolphinView::zoomLevelChanged
, this, &DolphinViewActionHandler::slotZoomLevelChanged
);
62 connect(view
, &DolphinView::writeStateChanged
, this, &DolphinViewActionHandler::slotWriteStateChanged
);
63 connect(view
, &DolphinView::selectionModeChangeRequested
, this, [this](bool enabled
) {
64 Q_EMIT
selectionModeChangeTriggered(enabled
);
66 connect(view
, &DolphinView::selectionChanged
, this, &DolphinViewActionHandler::slotSelectionChanged
);
67 slotSelectionChanged(m_currentView
->selectedItems());
70 DolphinView
*DolphinViewActionHandler::currentView()
75 void DolphinViewActionHandler::createActions(SelectionMode::ActionTextHelper
*actionTextHelper
)
77 // This action doesn't appear in the GUI, it's for the shortcut only.
78 // KNewFileMenu takes care of the GUI stuff.
79 QAction
*newDirAction
= m_actionCollection
->addAction(QStringLiteral("create_dir"));
80 newDirAction
->setText(i18nc("@action", "Create Folder…"));
81 m_actionCollection
->setDefaultShortcuts(newDirAction
, KStandardShortcut::createFolder());
82 newDirAction
->setIcon(QIcon::fromTheme(QStringLiteral("folder-new")));
83 newDirAction
->setEnabled(false); // Will be enabled in slotWriteStateChanged(bool) if the current URL is writable
84 connect(newDirAction
, &QAction::triggered
, this, &DolphinViewActionHandler::createDirectoryTriggered
);
88 auto renameAction
= KStandardAction::renameFile(this, &DolphinViewActionHandler::slotRename
, m_actionCollection
);
89 renameAction
->setWhatsThis(xi18nc("@info:whatsthis",
91 "items in your current selection.<nl/>Renaming multiple items "
92 "at once amounts to their new names differing only in a number."));
94 auto trashAction
= KStandardAction::moveToTrash(this, &DolphinViewActionHandler::slotTrashActivated
, m_actionCollection
);
95 auto trashShortcuts
= trashAction
->shortcuts();
96 trashAction
->setAutoRepeat(false);
97 if (!trashShortcuts
.contains(QKeySequence::Delete
)) {
98 trashShortcuts
.append(QKeySequence::Delete
);
99 m_actionCollection
->setDefaultShortcuts(trashAction
, trashShortcuts
);
101 trashAction
->setWhatsThis(xi18nc("@info:whatsthis",
103 "items in your current selection to the <filename>Trash"
104 "</filename>.<nl/>The trash is a temporary storage where "
105 "items can be deleted from if disk space is needed."));
107 auto deleteAction
= KStandardAction::deleteFile(this, &DolphinViewActionHandler::slotDeleteItems
, m_actionCollection
);
108 auto deleteShortcuts
= deleteAction
->shortcuts();
109 deleteAction
->setAutoRepeat(false);
110 if (!deleteShortcuts
.contains(Qt::SHIFT
| Qt::Key_Delete
)) {
111 deleteShortcuts
.append(Qt::SHIFT
| Qt::Key_Delete
);
112 m_actionCollection
->setDefaultShortcuts(deleteAction
, deleteShortcuts
);
114 deleteAction
->setWhatsThis(xi18nc("@info:whatsthis",
116 "the items in your current selection completely. They can "
117 "not be recovered by normal means."));
119 // This action is useful for being enabled when KStandardAction::MoveToTrash should be
120 // disabled and KStandardAction::DeleteFile is enabled (e.g. non-local files), so that Key_Del
121 // can be used for deleting the file (#76016). It needs to be a separate action
122 // so that the Edit menu isn't affected.
123 QAction
*deleteWithTrashShortcut
= m_actionCollection
->addAction(QStringLiteral("delete_shortcut"));
124 // The descriptive text is just for the shortcuts editor.
125 deleteWithTrashShortcut
->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
126 m_actionCollection
->setDefaultShortcuts(deleteWithTrashShortcut
, KStandardShortcut::moveToTrash());
127 deleteWithTrashShortcut
->setEnabled(false);
128 connect(deleteWithTrashShortcut
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDeleteItems
);
130 QAction
*duplicateAction
= m_actionCollection
->addAction(QStringLiteral("duplicate"));
131 duplicateAction
->setText(i18nc("@action:inmenu File", "Duplicate Here"));
132 duplicateAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-duplicate")));
133 m_actionCollection
->setDefaultShortcut(duplicateAction
, Qt::CTRL
| Qt::Key_D
);
134 duplicateAction
->setEnabled(false);
135 connect(duplicateAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotDuplicate
);
137 QAction
*propertiesAction
= m_actionCollection
->addAction(QStringLiteral("properties"));
138 // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
139 propertiesAction
->setText(i18nc("@action:inmenu File", "Properties"));
140 propertiesAction
->setWhatsThis(xi18nc("@info:whatsthis properties",
141 "This shows a complete list of properties of the currently "
142 "selected items in a new window.<nl/>If nothing is selected the "
143 "window will be about the currently viewed folder instead.<nl/>"
144 "You can configure advanced options there like managing "
145 "read- and write-permissions."));
146 propertiesAction
->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
147 m_actionCollection
->setDefaultShortcuts(propertiesAction
, {Qt::ALT
| Qt::Key_Return
, Qt::ALT
| Qt::Key_Enter
});
148 connect(propertiesAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotProperties
);
150 QAction
*copyPathAction
= m_actionCollection
->addAction(QStringLiteral("copy_location"));
151 copyPathAction
->setText(i18nc("@action:incontextmenu", "Copy Location"));
152 copyPathAction
->setWhatsThis(i18nc("@info:whatsthis copy_location", "This will copy the path of the first selected item into the clipboard."));
154 copyPathAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy-path")));
155 m_actionCollection
->setDefaultShortcuts(copyPathAction
, {Qt::CTRL
| Qt::ALT
| Qt::Key_C
});
156 connect(copyPathAction
, &QAction::triggered
, this, &DolphinViewActionHandler::slotCopyPath
);
158 if (actionTextHelper
) {
159 // The "…" at the end make clear that they won't trigger their respective actions directly.
160 actionTextHelper
->registerTextWhenNothingIsSelected(trashAction
, i18nc("@action:inmenu File", "Move to Trash…"));
161 actionTextHelper
->registerTextWhenNothingIsSelected(deleteAction
, i18nc("@action:inmenu File", "Delete…"));
162 actionTextHelper
->registerTextWhenNothingIsSelected(duplicateAction
, i18nc("@action:inmenu File", "Duplicate Here…"));
163 actionTextHelper
->registerTextWhenNothingIsSelected(copyPathAction
, i18nc("@action:incontextmenu", "Copy Location…"));
166 // This menu makes sure that users who don't know how to open a context menu and haven't
167 // figured out how to enable the menu bar can still perform basic file manipulation.
168 // This only works if they know how to select a file.
169 // The text when nothing is selected at least implies that a selection can /somehow/ be made.
170 // This menu is by default only used in the hamburger menu but created here so users can put
171 // it on their toolbar.
172 KActionMenu
*basicActionsMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("basic_actions"), this);
173 // The text is set later depending on the selection in the currently active view.
174 basicActionsMenu
->setPopupMode(QToolButton::InstantPopup
);
175 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Cut
)));
176 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Copy
)));
177 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::Paste
)));
178 basicActionsMenu
->addSeparator();
179 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
180 basicActionsMenu
->addAction(m_actionCollection
->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
181 basicActionsMenu
->addSeparator();
182 basicActionsMenu
->addAction(m_actionCollection
->action(QStringLiteral("properties")));
183 basicActionsMenu
->addSeparator(); // We add one more separator because we sometimes add contextual
184 // actions in slotSelectionChanged() after the static ones above.
187 KToggleAction
*iconsAction
= iconsModeAction();
188 KToggleAction
*compactAction
= compactModeAction();
189 KToggleAction
*detailsAction
= detailsModeAction();
191 iconsAction
->setWhatsThis(xi18nc("@info:whatsthis Icons view mode",
192 "<para>This switches to a view mode that focuses on the folder "
193 "and file icons. This mode makes it easy to distinguish folders "
194 "from files and to detect items with distinctive <emphasis>"
195 "file types</emphasis>.</para><para> This mode is handy to "
196 "browse through pictures when the <interface>Preview"
197 "</interface> option is enabled.</para>"));
198 compactAction
->setWhatsThis(xi18nc("@info:whatsthis Compact view mode",
199 "<para>This switches to a compact view mode that lists the folders "
200 "and files in columns with the names beside the icons.</para><para>"
201 "This helps to keep the overview in folders with many items.</para>"));
202 detailsAction
->setWhatsThis(xi18nc("@info:whatsthis Details view mode",
203 "<para>This switches to a list view mode that focuses on folder "
204 "and file details.</para><para>Click on a detail in the column "
205 "header to sort the items by it. Click again to sort the other "
206 "way around. To select which details should be displayed click "
207 "the header with the right mouse button.</para><para>You can "
208 "view the contents of a folder without leaving the current "
209 "location by clicking to the left of it. This way you can view "
210 "the contents of multiple folders in the same list.</para>"));
212 KSelectAction
*viewModeActions
= m_actionCollection
->add
<KSelectAction
>(QStringLiteral("view_mode"));
213 viewModeActions
->setText(i18nc("@action:intoolbar", "View Mode"));
214 viewModeActions
->addAction(iconsAction
);
215 viewModeActions
->addAction(compactAction
);
216 viewModeActions
->addAction(detailsAction
);
217 viewModeActions
->setToolBarMode(KSelectAction::MenuMode
);
218 connect(viewModeActions
, &KSelectAction::actionTriggered
, this, &DolphinViewActionHandler::slotViewModeActionTriggered
);
220 QAction
*zoomInAction
= KStandardAction::zoomIn(this, &DolphinViewActionHandler::zoomIn
, m_actionCollection
);
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, &DolphinViewActionHandler::zoomOut
, m_actionCollection
);
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",
247 "enabled, the icons are based on the actual file or folder "
248 "contents.<nl/>For example the icons of images become scaled "
249 "down versions of the images."));
250 showPreview
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
251 connect(showPreview
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::togglePreview
);
253 KToggleAction
*sortFoldersFirst
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("folders_first"));
254 sortFoldersFirst
->setText(i18nc("@action:inmenu Sort", "Folders First"));
255 connect(sortFoldersFirst
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleSortFoldersFirst
);
257 KToggleAction
*sortHiddenLast
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("hidden_last"));
258 sortHiddenLast
->setText(i18nc("@action:inmenu Sort", "Hidden Files Last"));
259 connect(sortHiddenLast
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleSortHiddenLast
);
262 QActionGroup
*sortByActionGroup
= createFileItemRolesActionGroup(QStringLiteral("sort_by_"));
264 KActionMenu
*sortByActionMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("sort"));
265 sortByActionMenu
->setIcon(QIcon::fromTheme(QStringLiteral("view-sort")));
266 sortByActionMenu
->setText(i18nc("@action:inmenu View", "Sort By"));
267 sortByActionMenu
->setPopupMode(QToolButton::InstantPopup
);
269 const auto sortByActionGroupActions
= sortByActionGroup
->actions();
270 for (QAction
*action
: sortByActionGroupActions
) {
271 sortByActionMenu
->addAction(action
);
274 sortByActionMenu
->addSeparator();
276 QActionGroup
*group
= new QActionGroup(sortByActionMenu
);
277 group
->setExclusive(true);
279 KToggleAction
*ascendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("ascending"));
280 ascendingAction
->setActionGroup(group
);
281 connect(ascendingAction
, &QAction::triggered
, this, [this] {
282 m_currentView
->setSortOrder(Qt::AscendingOrder
);
285 KToggleAction
*descendingAction
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("descending"));
286 descendingAction
->setActionGroup(group
);
287 connect(descendingAction
, &QAction::triggered
, this, [this] {
288 m_currentView
->setSortOrder(Qt::DescendingOrder
);
291 sortByActionMenu
->addAction(ascendingAction
);
292 sortByActionMenu
->addAction(descendingAction
);
293 sortByActionMenu
->addSeparator();
294 sortByActionMenu
->addAction(sortFoldersFirst
);
295 sortByActionMenu
->addAction(sortHiddenLast
);
297 // View -> Additional Information
298 QActionGroup
*visibleRolesGroup
= createFileItemRolesActionGroup(QStringLiteral("show_"));
300 KActionMenu
*visibleRolesMenu
= m_actionCollection
->add
<KActionMenu
>(QStringLiteral("additional_info"));
301 visibleRolesMenu
->setText(i18nc("@action:inmenu View", "Show Additional Information"));
302 visibleRolesMenu
->setIcon(QIcon::fromTheme(QStringLiteral("documentinfo")));
303 visibleRolesMenu
->setPopupMode(QToolButton::InstantPopup
);
305 const auto visibleRolesGroupActions
= visibleRolesGroup
->actions();
306 for (QAction
*action
: visibleRolesGroupActions
) {
307 visibleRolesMenu
->addAction(action
);
310 KToggleAction
*showInGroups
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_in_groups"));
311 showInGroups
->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
312 showInGroups
->setText(i18nc("@action:inmenu View", "Show in Groups"));
313 showInGroups
->setWhatsThis(i18nc("@info:whatsthis", "This groups files and folders by their first letter."));
314 connect(showInGroups
, &KToggleAction::triggered
, this, &DolphinViewActionHandler::toggleGroupedSorting
);
316 KToggleAction
*showHiddenFiles
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("show_hidden_files"));
317 showHiddenFiles
->setIcon(QIcon::fromTheme(QStringLiteral("view-visible")));
318 showHiddenFiles
->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
319 showHiddenFiles
->setWhatsThis(xi18nc("@info:whatsthis",
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",
333 "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
, this, &DolphinViewActionHandler::slotSortTriggered
);
348 connect(rolesActionGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::toggleVisibleRole
);
352 KActionMenu
*groupMenu
= nullptr;
353 QActionGroup
*groupMenuGroup
= nullptr;
355 bool indexingEnabled
= false;
357 Baloo::IndexerConfig config
;
358 indexingEnabled
= config
.fileIndexingEnabled();
361 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
362 for (const KFileItemModel::RoleInfo
&info
: rolesInfo
) {
363 if (!isSortGroup
&& info
.role
== "text") {
364 // It should not be possible to hide the "text" role
368 KToggleAction
*action
= nullptr;
369 const QString name
= groupPrefix
+ info
.role
;
370 if (info
.group
.isEmpty()) {
371 action
= m_actionCollection
->add
<KToggleAction
>(name
);
372 action
->setActionGroup(rolesActionGroup
);
374 if (!groupMenu
|| info
.group
!= groupName
) {
375 groupName
= info
.group
;
376 groupMenu
= m_actionCollection
->add
<KActionMenu
>(groupName
);
377 groupMenu
->setText(groupName
);
378 groupMenu
->setActionGroup(rolesActionGroup
);
380 groupMenuGroup
= new QActionGroup(groupMenu
);
381 groupMenuGroup
->setExclusive(isSortGroup
);
383 connect(groupMenuGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::slotSortTriggered
);
385 connect(groupMenuGroup
, &QActionGroup::triggered
, this, &DolphinViewActionHandler::toggleVisibleRole
);
389 action
= new KToggleAction(groupMenu
);
390 action
->setActionGroup(groupMenuGroup
);
391 groupMenu
->addAction(action
);
393 action
->setText(info
.translation
);
394 action
->setData(info
.role
);
396 const bool enable
= (!info
.requiresBaloo
&& !info
.requiresIndexer
) || (info
.requiresBaloo
) || (info
.requiresIndexer
&& indexingEnabled
);
397 action
->setEnabled(enable
);
400 m_sortByActions
.insert(info
.role
, action
);
402 m_visibleRoles
.insert(info
.role
, action
);
406 return rolesActionGroup
;
409 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction
*action
)
411 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
412 m_currentView
->setViewMode(mode
);
414 QAction
*viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
415 viewModeMenu
->setIcon(action
->icon());
418 void DolphinViewActionHandler::slotRename()
420 if (m_currentView
->selectedItemsCount() == 0) {
421 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::RenameContents
);
423 Q_EMIT
actionBeingHandled();
424 m_currentView
->renameSelectedItems();
425 // We don't exit selectionMode here because users might want to rename more items.
429 void DolphinViewActionHandler::slotTrashActivated()
431 if (m_currentView
->selectedItemsCount() == 0) {
432 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::MoveToTrashContents
);
434 Q_EMIT
actionBeingHandled();
435 m_currentView
->trashSelectedItems();
436 Q_EMIT
selectionModeChangeTriggered(false);
440 void DolphinViewActionHandler::slotDeleteItems()
442 if (m_currentView
->selectedItemsCount() == 0) {
443 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DeleteContents
);
445 Q_EMIT
actionBeingHandled();
446 m_currentView
->deleteSelectedItems();
447 Q_EMIT
selectionModeChangeTriggered(false);
451 void DolphinViewActionHandler::togglePreview(bool show
)
453 Q_EMIT
actionBeingHandled();
454 m_currentView
->setPreviewsShown(show
);
457 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown
)
460 // It is not enough to update the 'Show Preview' action, also
461 // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
465 QString
DolphinViewActionHandler::currentViewModeActionName() const
467 switch (m_currentView
->viewMode()) {
468 case DolphinView::IconsView
:
469 return QStringLiteral("icons");
470 case DolphinView::DetailsView
:
471 return QStringLiteral("details");
472 case DolphinView::CompactView
:
473 return QStringLiteral("compact");
478 return QString(); // can't happen
481 KActionCollection
*DolphinViewActionHandler::actionCollection()
483 return m_actionCollection
;
486 void DolphinViewActionHandler::updateViewActions()
488 QAction
*viewModeAction
= m_actionCollection
->action(currentViewModeActionName());
489 if (viewModeAction
) {
490 viewModeAction
->setChecked(true);
492 QAction
*viewModeMenu
= m_actionCollection
->action(QStringLiteral("view_mode"));
493 viewModeMenu
->setIcon(viewModeAction
->icon());
496 QAction
*showPreviewAction
= m_actionCollection
->action(QStringLiteral("show_preview"));
497 showPreviewAction
->setChecked(m_currentView
->previewsShown());
499 slotSortOrderChanged(m_currentView
->sortOrder());
500 slotSortFoldersFirstChanged(m_currentView
->sortFoldersFirst());
501 slotSortHiddenLastChanged(m_currentView
->sortHiddenLast());
502 slotVisibleRolesChanged(m_currentView
->visibleRoles(), QList
<QByteArray
>());
503 slotGroupedSortingChanged(m_currentView
->groupedSorting());
504 slotSortRoleChanged(m_currentView
->sortRole());
505 slotZoomLevelChanged(m_currentView
->zoomLevel(), -1);
507 // Updates the "show_hidden_files" action state and icon
508 slotHiddenFilesShownChanged(m_currentView
->hiddenFilesShown());
511 void DolphinViewActionHandler::zoomIn()
513 const int level
= m_currentView
->zoomLevel();
514 m_currentView
->setZoomLevel(level
+ 1);
518 void DolphinViewActionHandler::zoomOut()
520 const int level
= m_currentView
->zoomLevel();
521 m_currentView
->setZoomLevel(level
- 1);
525 void DolphinViewActionHandler::zoomReset()
527 m_currentView
->resetZoomLevel();
531 void DolphinViewActionHandler::toggleSortFoldersFirst()
533 const bool sortFirst
= m_currentView
->sortFoldersFirst();
534 m_currentView
->setSortFoldersFirst(!sortFirst
);
537 void DolphinViewActionHandler::toggleSortHiddenLast()
539 const bool sortHiddenLast
= m_currentView
->sortHiddenLast();
540 m_currentView
->setSortHiddenLast(!sortHiddenLast
);
543 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order
)
545 QAction
*descending
= m_actionCollection
->action(QStringLiteral("descending"));
546 QAction
*ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
547 const bool sortDescending
= (order
== Qt::DescendingOrder
);
548 descending
->setChecked(sortDescending
);
549 ascending
->setChecked(!sortDescending
);
552 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst
)
554 m_actionCollection
->action(QStringLiteral("folders_first"))->setChecked(foldersFirst
);
557 void DolphinViewActionHandler::slotSortHiddenLastChanged(bool hiddenLast
)
559 m_actionCollection
->action(QStringLiteral("hidden_last"))->setChecked(hiddenLast
);
562 void DolphinViewActionHandler::toggleVisibleRole(QAction
*action
)
564 Q_EMIT
actionBeingHandled();
566 const QByteArray toggledRole
= action
->data().toByteArray();
568 QList
<QByteArray
> roles
= m_currentView
->visibleRoles();
570 const bool show
= action
->isChecked();
572 const int index
= roles
.indexOf(toggledRole
);
573 const bool containsInfo
= (index
>= 0);
574 if (show
&& !containsInfo
) {
575 roles
.append(toggledRole
);
576 m_currentView
->setVisibleRoles(roles
);
577 } else if (!show
&& containsInfo
) {
578 roles
.removeAt(index
);
579 m_currentView
->setVisibleRoles(roles
);
580 Q_ASSERT(roles
.indexOf(toggledRole
) < 0);
584 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList
<QByteArray
> ¤t
, const QList
<QByteArray
> &previous
)
588 const auto checkedRoles
= QSet
<QByteArray
>(current
.constBegin(), current
.constEnd());
589 QHashIterator
<QByteArray
, KToggleAction
*> it(m_visibleRoles
);
590 while (it
.hasNext()) {
592 const QByteArray
&role
= it
.key();
593 KToggleAction
*action
= it
.value();
594 action
->setChecked(checkedRoles
.contains(role
));
598 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped
)
600 m_currentView
->setGroupedSorting(grouped
);
603 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting
)
605 QAction
*showInGroupsAction
= m_actionCollection
->action(QStringLiteral("show_in_groups"));
606 showInGroupsAction
->setChecked(groupedSorting
);
609 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show
)
611 Q_EMIT
actionBeingHandled();
612 m_currentView
->setHiddenFilesShown(show
);
615 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown
)
617 QAction
*showHiddenFilesAction
= m_actionCollection
->action(QStringLiteral("show_hidden_files"));
618 showHiddenFilesAction
->setChecked(shown
);
621 void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable
)
623 m_actionCollection
->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable
&& KProtocolManager::supportsMakeDir(currentView()->url()));
626 KToggleAction
*DolphinViewActionHandler::iconsModeAction()
628 KToggleAction
*iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("icons"));
629 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
630 iconsView
->setToolTip(i18nc("@info", "Icons view mode"));
631 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_1
);
632 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
633 iconsView
->setData(QVariant::fromValue(DolphinView::IconsView
));
637 KToggleAction
*DolphinViewActionHandler::compactModeAction()
639 KToggleAction
*iconsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("compact"));
640 iconsView
->setText(i18nc("@action:inmenu View Mode", "Compact"));
641 iconsView
->setToolTip(i18nc("@info", "Compact view mode"));
642 m_actionCollection
->setDefaultShortcut(iconsView
, Qt::CTRL
| Qt::Key_2
);
643 iconsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
644 iconsView
->setData(QVariant::fromValue(DolphinView::CompactView
));
648 KToggleAction
*DolphinViewActionHandler::detailsModeAction()
650 KToggleAction
*detailsView
= m_actionCollection
->add
<KToggleAction
>(QStringLiteral("details"));
651 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
652 detailsView
->setToolTip(i18nc("@info", "Details view mode"));
653 m_actionCollection
->setDefaultShortcut(detailsView
, Qt::CTRL
| Qt::Key_3
);
654 detailsView
->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
655 detailsView
->setData(QVariant::fromValue(DolphinView::DetailsView
));
659 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray
&role
)
661 KToggleAction
*action
= m_sortByActions
.value(role
);
663 action
->setChecked(true);
665 if (!action
->icon().isNull()) {
666 QAction
*sortByMenu
= m_actionCollection
->action(QStringLiteral("sort"));
667 sortByMenu
->setIcon(action
->icon());
671 QAction
*descending
= m_actionCollection
->action(QStringLiteral("descending"));
672 QAction
*ascending
= m_actionCollection
->action(QStringLiteral("ascending"));
674 if (role
== "text" || role
== "type" || role
== "extension" || role
== "tags" || role
== "comment") {
675 descending
->setText(i18nc("Sort descending", "Z-A"));
676 ascending
->setText(i18nc("Sort ascending", "A-Z"));
677 } else if (role
== "size") {
678 descending
->setText(i18nc("Sort descending", "Largest First"));
679 ascending
->setText(i18nc("Sort ascending", "Smallest First"));
680 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
681 descending
->setText(i18nc("Sort descending", "Newest First"));
682 ascending
->setText(i18nc("Sort ascending", "Oldest First"));
683 } else if (role
== "rating") {
684 descending
->setText(i18nc("Sort descending", "Highest First"));
685 ascending
->setText(i18nc("Sort ascending", "Lowest First"));
687 descending
->setText(i18nc("Sort descending", "Descending"));
688 ascending
->setText(i18nc("Sort ascending", "Ascending"));
691 slotSortOrderChanged(m_currentView
->sortOrder());
694 void DolphinViewActionHandler::slotZoomLevelChanged(int current
, int previous
)
698 QAction
*zoomInAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomIn
));
700 zoomInAction
->setEnabled(current
< ZoomLevelInfo::maximumLevel());
703 QAction
*zoomOutAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomOut
));
705 zoomOutAction
->setEnabled(current
> ZoomLevelInfo::minimumLevel());
709 void DolphinViewActionHandler::slotSortTriggered(QAction
*action
)
711 // The radiobuttons of the "Sort By"-menu are split between the main-menu
712 // and several sub-menus. Because of this they don't have a common
713 // action-group that assures an exclusive toggle-state between the main-menu
714 // actions and the sub-menu-actions. If an action gets checked, it must
715 // be assured that all other actions get unchecked, except the ascending/
716 // descending actions
717 for (QAction
*groupAction
: qAsConst(m_sortByActions
)) {
718 KActionMenu
*actionMenu
= qobject_cast
<KActionMenu
*>(groupAction
);
720 const auto actions
= actionMenu
->menu()->actions();
721 for (QAction
*subAction
: actions
) {
722 subAction
->setChecked(false);
724 } else if (groupAction
->actionGroup()) {
725 groupAction
->setChecked(false);
728 action
->setChecked(true);
730 // Apply the activated sort-role to the view
731 const QByteArray role
= action
->data().toByteArray();
732 m_currentView
->setSortRole(role
);
735 void DolphinViewActionHandler::slotAdjustViewProperties()
737 Q_EMIT
actionBeingHandled();
738 QPointer
<ViewPropertiesDialog
> dialog
= new ViewPropertiesDialog(m_currentView
);
743 void DolphinViewActionHandler::slotDuplicate()
745 if (m_currentView
->selectedItemsCount() == 0) {
746 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DuplicateContents
);
748 Q_EMIT
actionBeingHandled();
749 m_currentView
->duplicateSelectedItems();
750 Q_EMIT
selectionModeChangeTriggered(false);
754 void DolphinViewActionHandler::slotProperties()
756 KPropertiesDialog
*dialog
= nullptr;
757 const KFileItemList list
= m_currentView
->selectedItems();
758 if (list
.isEmpty()) {
759 const QUrl url
= m_currentView
->url();
760 dialog
= new KPropertiesDialog(url
, m_currentView
);
762 dialog
= new KPropertiesDialog(list
, m_currentView
);
765 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
768 dialog
->activateWindow();
771 void DolphinViewActionHandler::slotCopyPath()
773 if (m_currentView
->selectedItemsCount() == 0) {
774 Q_EMIT
selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::CopyLocationContents
);
776 m_currentView
->copyPathToClipboard();
777 Q_EMIT
selectionModeChangeTriggered(false);
781 void DolphinViewActionHandler::slotSelectionChanged(const KFileItemList
&selection
)
783 QString basicActionsMenuText
;
784 if (selection
.isEmpty()) {
785 basicActionsMenuText
= i18nc("@action:inmenu menu with actions like copy, paste, rename. The user's selection is empty when this text is shown.",
786 "Actions for Current View");
789 QFontMetrics fontMetrics
= QMenu().fontMetrics();
790 // i18n: @action:inmenu menu with actions like copy, paste, rename.
791 // %1 is a textual representation of the currently selected files or folders. This can be the name of
792 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
793 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
794 // and a fallback will be used.
795 basicActionsMenuText
= i18n("Actions for %1", fileItemListToString(selection
, fontMetrics
.averageCharWidth() * 40, fontMetrics
, ItemsState::Selected
));
799 if (basicActionsMenuText
== QStringLiteral("NULL")) {
800 const KFileItemListProperties
properties(selection
);
801 basicActionsMenuText
= i18ncp("@action:inmenu menu with actions like copy, paste, rename. %1 is the amount of selected files/folders.",
802 "Actions for One Selected Item",
803 "Actions for %1 Selected Items",
807 QAction
*basicActionsMenu
= m_actionCollection
->action(QStringLiteral("basic_actions"));
808 basicActionsMenu
->setText(basicActionsMenuText
);
810 // Add or remove contextual actions
811 while (!basicActionsMenu
->menu()->actions().constLast()->isSeparator()) {
812 basicActionsMenu
->menu()->removeAction(basicActionsMenu
->menu()->actions().last());
814 if (selection
.count() == 1) {
815 if (selection
.first().isLink()) {
816 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("show_target")));
818 if (selection
.first().isDir()) {
819 basicActionsMenu
->menu()->addAction(m_actionCollection
->action(QStringLiteral("add_to_places")));
824 #include "moc_dolphinviewactionhandler.cpp"