1 /***************************************************************************
2 * Copyright (C) 2008 by David Faure <faure@kde.org> *
3 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "dolphinviewactionhandler.h"
23 #include "settings/viewpropertiesdialog.h"
24 #include "views/dolphinview.h"
25 #include "views/zoomlevelinfo.h"
26 #include <konq_operations.h>
29 #include <KActionCollection>
30 #include <KActionMenu>
31 #include <kitemviews/kfileitemmodel.h>
34 #include <KNewFileMenu>
35 #include <KSelectAction>
36 #include <KToggleAction>
38 #include <KPropertiesDialog>
42 DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection
* collection
, QObject
* parent
) :
44 m_actionCollection(collection
),
49 Q_ASSERT(m_actionCollection
);
53 void DolphinViewActionHandler::setCurrentView(DolphinView
* view
)
58 disconnect(m_currentView
, 0, this, 0);
63 connect(view
, SIGNAL(modeChanged(DolphinView::Mode
,DolphinView::Mode
)),
64 this, SLOT(updateViewActions()));
65 connect(view
, SIGNAL(previewsShownChanged(bool)),
66 this, SLOT(slotPreviewsShownChanged(bool)));
67 connect(view
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
68 this, SLOT(slotSortOrderChanged(Qt::SortOrder
)));
69 connect(view
, SIGNAL(sortFoldersFirstChanged(bool)),
70 this, SLOT(slotSortFoldersFirstChanged(bool)));
71 connect(view
, SIGNAL(visibleRolesChanged(QList
<QByteArray
>,QList
<QByteArray
>)),
72 this, SLOT(slotVisibleRolesChanged(QList
<QByteArray
>,QList
<QByteArray
>)));
73 connect(view
, SIGNAL(groupedSortingChanged(bool)),
74 this, SLOT(slotGroupedSortingChanged(bool)));
75 connect(view
, SIGNAL(hiddenFilesShownChanged(bool)),
76 this, SLOT(slotHiddenFilesShownChanged(bool)));
77 connect(view
, SIGNAL(sortRoleChanged(QByteArray
)),
78 this, SLOT(slotSortRoleChanged(QByteArray
)));
79 connect(view
, SIGNAL(zoomLevelChanged(int,int)),
80 this, SLOT(slotZoomLevelChanged(int,int)));
83 DolphinView
* DolphinViewActionHandler::currentView()
88 void DolphinViewActionHandler::createActions()
90 // This action doesn't appear in the GUI, it's for the shortcut only.
91 // KNewFileMenu takes care of the GUI stuff.
92 KAction
* newDirAction
= m_actionCollection
->addAction("create_dir");
93 newDirAction
->setText(i18nc("@action", "Create Folder..."));
94 newDirAction
->setShortcut(Qt::Key_F10
);
95 newDirAction
->setIcon(KIcon("folder-new"));
96 connect(newDirAction
, SIGNAL(triggered()), this, SIGNAL(createDirectory()));
100 KAction
* rename
= m_actionCollection
->addAction("rename");
101 rename
->setText(i18nc("@action:inmenu File", "Rename..."));
102 rename
->setShortcut(Qt::Key_F2
);
103 rename
->setIcon(KIcon("edit-rename"));
104 connect(rename
, SIGNAL(triggered()), this, SLOT(slotRename()));
106 KAction
* moveToTrash
= m_actionCollection
->addAction("move_to_trash");
107 moveToTrash
->setText(i18nc("@action:inmenu File", "Move to Trash"));
108 moveToTrash
->setIcon(KIcon("user-trash"));
109 moveToTrash
->setShortcut(QKeySequence::Delete
);
110 connect(moveToTrash
, SIGNAL(triggered(Qt::MouseButtons
,Qt::KeyboardModifiers
)),
111 this, SLOT(slotTrashActivated(Qt::MouseButtons
,Qt::KeyboardModifiers
)));
113 KAction
* deleteAction
= m_actionCollection
->addAction("delete");
114 deleteAction
->setIcon(KIcon("edit-delete"));
115 deleteAction
->setText(i18nc("@action:inmenu File", "Delete"));
116 deleteAction
->setShortcut(Qt::SHIFT
| Qt::Key_Delete
);
117 connect(deleteAction
, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
119 // This action is useful for being enabled when "move_to_trash" should be
120 // disabled and "delete" 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 KAction
* deleteWithTrashShortcut
= m_actionCollection
->addAction("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 deleteWithTrashShortcut
->setShortcut(QKeySequence::Delete
);
127 deleteWithTrashShortcut
->setEnabled(false);
128 connect(deleteWithTrashShortcut
, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
130 KAction
*propertiesAction
= m_actionCollection
->addAction( "properties" );
131 // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
132 propertiesAction
->setText( i18nc("@action:inmenu File", "Properties") );
133 propertiesAction
->setIcon(KIcon("document-properties"));
134 propertiesAction
->setShortcut(Qt::ALT
| Qt::Key_Return
);
135 connect(propertiesAction
, SIGNAL(triggered()), SLOT(slotProperties()));
138 KToggleAction
* iconsAction
= iconsModeAction();
139 KToggleAction
* compactAction
= compactModeAction();
140 KToggleAction
* detailsAction
= detailsModeAction();
142 KSelectAction
* viewModeActions
= m_actionCollection
->add
<KSelectAction
>("view_mode");
143 viewModeActions
->setText(i18nc("@action:intoolbar", "View Mode"));
144 viewModeActions
->addAction(iconsAction
);
145 viewModeActions
->addAction(compactAction
);
146 viewModeActions
->addAction(detailsAction
);
147 viewModeActions
->setToolBarMode(KSelectAction::MenuMode
);
148 connect(viewModeActions
, SIGNAL(triggered(QAction
*)), this, SLOT(slotViewModeActionTriggered(QAction
*)));
150 KStandardAction::zoomIn(this,
154 KStandardAction::zoomOut(this,
158 KToggleAction
* showPreview
= m_actionCollection
->add
<KToggleAction
>("show_preview");
159 showPreview
->setText(i18nc("@action:intoolbar", "Preview"));
160 showPreview
->setToolTip(i18nc("@info", "Show preview of files and folders"));
161 showPreview
->setIcon(KIcon("view-preview"));
162 connect(showPreview
, SIGNAL(triggered(bool)), this, SLOT(togglePreview(bool)));
164 KToggleAction
* sortDescending
= m_actionCollection
->add
<KToggleAction
>("descending");
165 sortDescending
->setText(i18nc("@action:inmenu Sort", "Descending"));
166 connect(sortDescending
, SIGNAL(triggered()), this, SLOT(toggleSortOrder()));
168 KToggleAction
* sortFoldersFirst
= m_actionCollection
->add
<KToggleAction
>("folders_first");
169 sortFoldersFirst
->setText(i18nc("@action:inmenu Sort", "Folders First"));
170 connect(sortFoldersFirst
, SIGNAL(triggered()), this, SLOT(toggleSortFoldersFirst()));
173 QActionGroup
* sortByActionGroup
= createFileItemRolesActionGroup("sort_by_");
175 KActionMenu
* sortByActionMenu
= m_actionCollection
->add
<KActionMenu
>("sort");
176 sortByActionMenu
->setText(i18nc("@action:inmenu View", "Sort By"));
177 sortByActionMenu
->setDelayed(false);
179 foreach (QAction
* action
, sortByActionGroup
->actions()) {
180 sortByActionMenu
->addAction(action
);
182 sortByActionMenu
->addSeparator();
183 sortByActionMenu
->addAction(sortDescending
);
184 sortByActionMenu
->addAction(sortFoldersFirst
);
186 // View -> Additional Information
187 QActionGroup
* visibleRolesGroup
= createFileItemRolesActionGroup("show_");
189 KActionMenu
* visibleRolesMenu
= m_actionCollection
->add
<KActionMenu
>("additional_info");
190 visibleRolesMenu
->setText(i18nc("@action:inmenu View", "Additional Information"));
191 visibleRolesMenu
->setDelayed(false);
193 foreach (QAction
* action
, visibleRolesGroup
->actions()) {
194 visibleRolesMenu
->addAction(action
);
197 KToggleAction
* showInGroups
= m_actionCollection
->add
<KToggleAction
>("show_in_groups");
198 showInGroups
->setIcon(KIcon("view-group"));
199 showInGroups
->setText(i18nc("@action:inmenu View", "Show in Groups"));
200 connect(showInGroups
, SIGNAL(triggered(bool)), this, SLOT(toggleGroupedSorting(bool)));
202 KToggleAction
* showHiddenFiles
= m_actionCollection
->add
<KToggleAction
>("show_hidden_files");
203 showHiddenFiles
->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
204 showHiddenFiles
->setShortcuts(QList
<QKeySequence
>() << Qt::ALT
+ Qt::Key_Period
<< Qt::Key_F8
);
205 connect(showHiddenFiles
, SIGNAL(triggered(bool)), this, SLOT(toggleShowHiddenFiles(bool)));
207 KAction
* adjustViewProps
= m_actionCollection
->addAction("view_properties");
208 adjustViewProps
->setText(i18nc("@action:inmenu View", "Adjust View Properties..."));
209 connect(adjustViewProps
, SIGNAL(triggered()), this, SLOT(slotAdjustViewProperties()));
212 QActionGroup
* DolphinViewActionHandler::createFileItemRolesActionGroup(const QString
& groupPrefix
)
214 const bool isSortGroup
= (groupPrefix
== QLatin1String("sort_by_"));
215 Q_ASSERT(isSortGroup
|| (!isSortGroup
&& groupPrefix
== QLatin1String("show_")));
217 QActionGroup
* rolesActionGroup
= new QActionGroup(m_actionCollection
);
218 rolesActionGroup
->setExclusive(isSortGroup
);
220 connect(rolesActionGroup
, SIGNAL(triggered(QAction
*)),
221 this, SLOT(slotSortTriggered(QAction
*)));
223 connect(rolesActionGroup
, SIGNAL(triggered(QAction
*)),
224 this, SLOT(toggleVisibleRole(QAction
*)));
228 KActionMenu
* groupMenu
= 0;
229 QActionGroup
* groupMenuGroup
= 0;
231 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
232 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
233 if (!isSortGroup
&& info
.role
== "name") {
234 // It should not be possible to hide the "name" role
238 KToggleAction
* action
= 0;
239 const QString name
= groupPrefix
+ info
.role
;
240 if (info
.group
.isEmpty()) {
241 action
= m_actionCollection
->add
<KToggleAction
>(name
);
242 action
->setActionGroup(rolesActionGroup
);
244 if (!groupMenu
|| info
.group
!= groupName
) {
245 groupName
= info
.group
;
246 groupMenu
= m_actionCollection
->add
<KActionMenu
>(groupName
);
247 groupMenu
->setText(groupName
);
248 groupMenu
->setActionGroup(rolesActionGroup
);
250 groupMenuGroup
= new QActionGroup(groupMenu
);
251 groupMenuGroup
->setExclusive(isSortGroup
);
253 connect(groupMenuGroup
, SIGNAL(triggered(QAction
*)),
254 this, SLOT(slotSortTriggered(QAction
*)));
256 connect(groupMenuGroup
, SIGNAL(triggered(QAction
*)),
257 this, SLOT(toggleVisibleRole(QAction
*)));
261 action
= new KToggleAction(groupMenu
);
262 action
->setActionGroup(groupMenuGroup
);
263 groupMenu
->addAction(action
);
265 action
->setText(info
.translation
);
266 action
->setData(info
.role
);
269 m_sortByActions
.insert(info
.role
, action
);
271 m_visibleRoles
.insert(info
.role
, action
);
275 return rolesActionGroup
;
278 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction
* action
)
280 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
281 m_currentView
->setMode(mode
);
283 QAction
* viewModeMenu
= m_actionCollection
->action("view_mode");
284 viewModeMenu
->setIcon(KIcon(action
->icon()));
287 void DolphinViewActionHandler::slotRename()
289 emit
actionBeingHandled();
290 m_currentView
->renameSelectedItems();
293 void DolphinViewActionHandler::slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers modifiers
)
295 emit
actionBeingHandled();
296 // Note: kde3's konq_mainwindow.cpp used to check
297 // reason == KAction::PopupMenuActivation && ...
298 // but this isn't supported anymore
299 if (modifiers
& Qt::ShiftModifier
) {
300 m_currentView
->deleteSelectedItems();
302 m_currentView
->trashSelectedItems();
306 void DolphinViewActionHandler::slotDeleteItems()
308 emit
actionBeingHandled();
309 m_currentView
->deleteSelectedItems();
312 void DolphinViewActionHandler::togglePreview(bool show
)
314 emit
actionBeingHandled();
315 m_currentView
->setPreviewsShown(show
);
318 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown
)
321 // It is not enough to update the 'Show Preview' action, also
322 // the 'Zoom In' and 'Zoom Out' actions must be adapted.
326 QString
DolphinViewActionHandler::currentViewModeActionName() const
328 switch (m_currentView
->mode()) {
329 case DolphinView::IconsView
:
331 case DolphinView::DetailsView
:
333 case DolphinView::CompactView
:
339 return QString(); // can't happen
342 KActionCollection
* DolphinViewActionHandler::actionCollection()
344 return m_actionCollection
;
347 void DolphinViewActionHandler::updateViewActions()
349 QAction
* viewModeAction
= m_actionCollection
->action(currentViewModeActionName());
350 if (viewModeAction
) {
351 viewModeAction
->setChecked(true);
353 QAction
* viewModeMenu
= m_actionCollection
->action("view_mode");
354 viewModeMenu
->setIcon(KIcon(viewModeAction
->icon()));
357 QAction
* showPreviewAction
= m_actionCollection
->action("show_preview");
358 showPreviewAction
->setChecked(m_currentView
->previewsShown());
360 slotSortOrderChanged(m_currentView
->sortOrder());
361 slotSortFoldersFirstChanged(m_currentView
->sortFoldersFirst());
362 slotVisibleRolesChanged(m_currentView
->visibleRoles(), QList
<QByteArray
>());
363 slotGroupedSortingChanged(m_currentView
->groupedSorting());
364 slotSortRoleChanged(m_currentView
->sortRole());
365 slotZoomLevelChanged(m_currentView
->zoomLevel(), -1);
367 QAction
* showHiddenFilesAction
= m_actionCollection
->action("show_hidden_files");
368 showHiddenFilesAction
->setChecked(m_currentView
->hiddenFilesShown());
371 void DolphinViewActionHandler::zoomIn()
373 const int level
= m_currentView
->zoomLevel();
374 m_currentView
->setZoomLevel(level
+ 1);
378 void DolphinViewActionHandler::zoomOut()
380 const int level
= m_currentView
->zoomLevel();
381 m_currentView
->setZoomLevel(level
- 1);
385 void DolphinViewActionHandler::toggleSortOrder()
387 const Qt::SortOrder order
= (m_currentView
->sortOrder() == Qt::AscendingOrder
) ?
388 Qt::DescendingOrder
:
390 m_currentView
->setSortOrder(order
);
393 void DolphinViewActionHandler::toggleSortFoldersFirst()
395 const bool sortFirst
= m_currentView
->sortFoldersFirst();
396 m_currentView
->setSortFoldersFirst(!sortFirst
);
399 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order
)
401 QAction
* descending
= m_actionCollection
->action("descending");
402 const bool sortDescending
= (order
== Qt::DescendingOrder
);
403 descending
->setChecked(sortDescending
);
406 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst
)
408 m_actionCollection
->action("folders_first")->setChecked(foldersFirst
);
411 void DolphinViewActionHandler::toggleVisibleRole(QAction
* action
)
413 emit
actionBeingHandled();
415 const QByteArray toggledRole
= action
->data().toByteArray();
417 QList
<QByteArray
> roles
= m_currentView
->visibleRoles();
419 const bool show
= action
->isChecked();
421 const int index
= roles
.indexOf(toggledRole
);
422 const bool containsInfo
= (index
>= 0);
423 if (show
&& !containsInfo
) {
424 roles
.append(toggledRole
);
425 m_currentView
->setVisibleRoles(roles
);
426 } else if (!show
&& containsInfo
) {
427 roles
.removeAt(index
);
428 m_currentView
->setVisibleRoles(roles
);
429 Q_ASSERT(roles
.indexOf(toggledRole
) < 0);
433 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList
<QByteArray
>& current
,
434 const QList
<QByteArray
>& previous
)
438 const QSet
<QByteArray
> checkedRoles
= current
.toSet();
439 QHashIterator
<QByteArray
, KToggleAction
*> it(m_visibleRoles
);
440 while (it
.hasNext()) {
442 const QByteArray
& role
= it
.key();
443 KToggleAction
* action
= it
.value();
444 action
->setChecked(checkedRoles
.contains(role
));
448 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped
)
450 m_currentView
->setGroupedSorting(grouped
);
453 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting
)
455 QAction
* showInGroupsAction
= m_actionCollection
->action("show_in_groups");
456 showInGroupsAction
->setChecked(groupedSorting
);
459 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show
)
461 emit
actionBeingHandled();
462 m_currentView
->setHiddenFilesShown(show
);
465 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown
)
467 QAction
* showHiddenFilesAction
= m_actionCollection
->action("show_hidden_files");
468 showHiddenFilesAction
->setChecked(shown
);
471 KToggleAction
* DolphinViewActionHandler::iconsModeAction()
473 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>("icons");
474 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
475 iconsView
->setToolTip(i18nc("@info", "Icons view mode"));
476 iconsView
->setShortcut(Qt::CTRL
| Qt::Key_1
);
477 iconsView
->setIcon(KIcon("view-list-icons"));
478 iconsView
->setData(QVariant::fromValue(DolphinView::IconsView
));
482 KToggleAction
* DolphinViewActionHandler::compactModeAction()
484 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>("compact");
485 iconsView
->setText(i18nc("@action:inmenu View Mode", "Compact"));
486 iconsView
->setToolTip(i18nc("@info", "Compact view mode"));
487 iconsView
->setShortcut(Qt::CTRL
| Qt::Key_2
);
488 iconsView
->setIcon(KIcon("view-list-details")); // TODO: discuss with Oxygen-team the wrong (?) name
489 iconsView
->setData(QVariant::fromValue(DolphinView::CompactView
));
493 KToggleAction
* DolphinViewActionHandler::detailsModeAction()
495 KToggleAction
* detailsView
= m_actionCollection
->add
<KToggleAction
>("details");
496 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
497 detailsView
->setToolTip(i18nc("@info", "Details view mode"));
498 detailsView
->setShortcut(Qt::CTRL
| Qt::Key_3
);
499 detailsView
->setIcon(KIcon("view-list-tree"));
500 detailsView
->setData(QVariant::fromValue(DolphinView::DetailsView
));
504 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray
& role
)
506 KToggleAction
* action
= m_sortByActions
.value(role
);
508 action
->setChecked(true);
510 QAction
* sortByMenu
= m_actionCollection
->action("sort");
511 sortByMenu
->setIcon(KIcon(action
->icon()));
515 void DolphinViewActionHandler::slotZoomLevelChanged(int current
, int previous
)
519 QAction
* zoomInAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomIn
));
521 zoomInAction
->setEnabled(current
< ZoomLevelInfo::maximumLevel());
524 QAction
* zoomOutAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomOut
));
526 zoomOutAction
->setEnabled(current
> ZoomLevelInfo::minimumLevel());
530 void DolphinViewActionHandler::slotSortTriggered(QAction
* action
)
532 // The radiobuttons of the "Sort By"-menu are split between the main-menu
533 // and several sub-menus. Because of this they don't have a common
534 // action-group that assures an exclusive toggle-state between the main-menu
535 // actions and the sub-menu-actions. If an action gets checked, it must
536 // be assured that all other actions get unchecked.
537 QAction
* sortByMenu
= m_actionCollection
->action("sort");
538 foreach (QAction
* groupAction
, sortByMenu
->menu()->actions()) {
539 KActionMenu
* actionMenu
= qobject_cast
<KActionMenu
*>(groupAction
);
541 foreach (QAction
* subAction
, actionMenu
->menu()->actions()) {
542 subAction
->setChecked(false);
544 } else if (groupAction
->actionGroup()) {
545 groupAction
->setChecked(false);
548 action
->setChecked(true);
550 // Apply the activated sort-role to the view
551 const QByteArray role
= action
->data().toByteArray();
552 m_currentView
->setSortRole(role
);
555 void DolphinViewActionHandler::slotAdjustViewProperties()
557 emit
actionBeingHandled();
558 QPointer
<ViewPropertiesDialog
> dialog
= new ViewPropertiesDialog(m_currentView
);
563 void DolphinViewActionHandler::slotProperties()
565 KPropertiesDialog
* dialog
= 0;
566 const KFileItemList list
= m_currentView
->selectedItems();
567 if (list
.isEmpty()) {
568 const KUrl url
= m_currentView
->url();
569 dialog
= new KPropertiesDialog(url
, m_currentView
);
571 dialog
= new KPropertiesDialog(list
, m_currentView
);
574 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
577 dialog
->activateWindow();