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 <config-nepomuk.h>
25 #include "settings/viewpropertiesdialog.h"
26 #include "views/dolphinview.h"
27 #include "views/zoomlevelinfo.h"
28 #include <konq_operations.h>
31 #include <KActionCollection>
32 #include <KActionMenu>
33 #include <kitemviews/kfileitemmodel.h>
36 #include <KNewFileMenu>
37 #include <KSelectAction>
38 #include <KToggleAction>
40 #include <KPropertiesDialog>
44 #include <Nepomuk/ResourceManager>
49 DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection
* collection
, QObject
* parent
) :
51 m_actionCollection(collection
),
56 Q_ASSERT(m_actionCollection
);
60 void DolphinViewActionHandler::setCurrentView(DolphinView
* view
)
65 disconnect(m_currentView
, 0, this, 0);
70 connect(view
, SIGNAL(modeChanged(DolphinView::Mode
,DolphinView::Mode
)),
71 this, SLOT(updateViewActions()));
72 connect(view
, SIGNAL(previewsShownChanged(bool)),
73 this, SLOT(slotPreviewsShownChanged(bool)));
74 connect(view
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
75 this, SLOT(slotSortOrderChanged(Qt::SortOrder
)));
76 connect(view
, SIGNAL(sortFoldersFirstChanged(bool)),
77 this, SLOT(slotSortFoldersFirstChanged(bool)));
78 connect(view
, SIGNAL(visibleRolesChanged(QList
<QByteArray
>,QList
<QByteArray
>)),
79 this, SLOT(slotVisibleRolesChanged(QList
<QByteArray
>,QList
<QByteArray
>)));
80 connect(view
, SIGNAL(groupedSortingChanged(bool)),
81 this, SLOT(slotGroupedSortingChanged(bool)));
82 connect(view
, SIGNAL(hiddenFilesShownChanged(bool)),
83 this, SLOT(slotHiddenFilesShownChanged(bool)));
84 connect(view
, SIGNAL(sortRoleChanged(QByteArray
)),
85 this, SLOT(slotSortRoleChanged(QByteArray
)));
86 connect(view
, SIGNAL(zoomLevelChanged(int,int)),
87 this, SLOT(slotZoomLevelChanged(int,int)));
90 DolphinView
* DolphinViewActionHandler::currentView()
95 void DolphinViewActionHandler::createActions()
97 // This action doesn't appear in the GUI, it's for the shortcut only.
98 // KNewFileMenu takes care of the GUI stuff.
99 KAction
* newDirAction
= m_actionCollection
->addAction("create_dir");
100 newDirAction
->setText(i18nc("@action", "Create Folder..."));
101 newDirAction
->setShortcut(Qt::Key_F10
);
102 newDirAction
->setIcon(KIcon("folder-new"));
103 connect(newDirAction
, SIGNAL(triggered()), this, SIGNAL(createDirectory()));
107 KAction
* rename
= m_actionCollection
->addAction("rename");
108 rename
->setText(i18nc("@action:inmenu File", "Rename..."));
109 rename
->setShortcut(Qt::Key_F2
);
110 rename
->setIcon(KIcon("edit-rename"));
111 connect(rename
, SIGNAL(triggered()), this, SLOT(slotRename()));
113 KAction
* moveToTrash
= m_actionCollection
->addAction("move_to_trash");
114 moveToTrash
->setText(i18nc("@action:inmenu File", "Move to Trash"));
115 moveToTrash
->setIcon(KIcon("user-trash"));
116 moveToTrash
->setShortcut(QKeySequence::Delete
);
117 connect(moveToTrash
, SIGNAL(triggered(Qt::MouseButtons
,Qt::KeyboardModifiers
)),
118 this, SLOT(slotTrashActivated(Qt::MouseButtons
,Qt::KeyboardModifiers
)));
120 KAction
* deleteAction
= m_actionCollection
->addAction("delete");
121 deleteAction
->setIcon(KIcon("edit-delete"));
122 deleteAction
->setText(i18nc("@action:inmenu File", "Delete"));
123 deleteAction
->setShortcut(Qt::SHIFT
| Qt::Key_Delete
);
124 connect(deleteAction
, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
126 // This action is useful for being enabled when "move_to_trash" should be
127 // disabled and "delete" is enabled (e.g. non-local files), so that Key_Del
128 // can be used for deleting the file (#76016). It needs to be a separate action
129 // so that the Edit menu isn't affected.
130 KAction
* deleteWithTrashShortcut
= m_actionCollection
->addAction("delete_shortcut");
131 // The descriptive text is just for the shortcuts editor.
132 deleteWithTrashShortcut
->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
133 deleteWithTrashShortcut
->setShortcut(QKeySequence::Delete
);
134 deleteWithTrashShortcut
->setEnabled(false);
135 connect(deleteWithTrashShortcut
, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
137 KAction
*propertiesAction
= m_actionCollection
->addAction( "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
->setIcon(KIcon("document-properties"));
141 propertiesAction
->setShortcut(Qt::ALT
| Qt::Key_Return
);
142 connect(propertiesAction
, SIGNAL(triggered()), SLOT(slotProperties()));
145 KToggleAction
* iconsAction
= iconsModeAction();
146 KToggleAction
* compactAction
= compactModeAction();
147 KToggleAction
* detailsAction
= detailsModeAction();
149 KSelectAction
* viewModeActions
= m_actionCollection
->add
<KSelectAction
>("view_mode");
150 viewModeActions
->setText(i18nc("@action:intoolbar", "View Mode"));
151 viewModeActions
->addAction(iconsAction
);
152 viewModeActions
->addAction(compactAction
);
153 viewModeActions
->addAction(detailsAction
);
154 viewModeActions
->setToolBarMode(KSelectAction::MenuMode
);
155 connect(viewModeActions
, SIGNAL(triggered(QAction
*)), this, SLOT(slotViewModeActionTriggered(QAction
*)));
157 KStandardAction::zoomIn(this,
161 KStandardAction::zoomOut(this,
165 KToggleAction
* showPreview
= m_actionCollection
->add
<KToggleAction
>("show_preview");
166 showPreview
->setText(i18nc("@action:intoolbar", "Preview"));
167 showPreview
->setToolTip(i18nc("@info", "Show preview of files and folders"));
168 showPreview
->setIcon(KIcon("view-preview"));
169 connect(showPreview
, SIGNAL(triggered(bool)), this, SLOT(togglePreview(bool)));
171 KToggleAction
* sortDescending
= m_actionCollection
->add
<KToggleAction
>("descending");
172 sortDescending
->setText(i18nc("@action:inmenu Sort", "Descending"));
173 connect(sortDescending
, SIGNAL(triggered()), this, SLOT(toggleSortOrder()));
175 KToggleAction
* sortFoldersFirst
= m_actionCollection
->add
<KToggleAction
>("folders_first");
176 sortFoldersFirst
->setText(i18nc("@action:inmenu Sort", "Folders First"));
177 connect(sortFoldersFirst
, SIGNAL(triggered()), this, SLOT(toggleSortFoldersFirst()));
180 QActionGroup
* sortByActionGroup
= createFileItemRolesActionGroup("sort_by_");
182 KActionMenu
* sortByActionMenu
= m_actionCollection
->add
<KActionMenu
>("sort");
183 sortByActionMenu
->setText(i18nc("@action:inmenu View", "Sort By"));
184 sortByActionMenu
->setDelayed(false);
186 foreach (QAction
* action
, sortByActionGroup
->actions()) {
187 sortByActionMenu
->addAction(action
);
189 sortByActionMenu
->addSeparator();
190 sortByActionMenu
->addAction(sortDescending
);
191 sortByActionMenu
->addAction(sortFoldersFirst
);
193 // View -> Additional Information
194 QActionGroup
* visibleRolesGroup
= createFileItemRolesActionGroup("show_");
196 KActionMenu
* visibleRolesMenu
= m_actionCollection
->add
<KActionMenu
>("additional_info");
197 visibleRolesMenu
->setText(i18nc("@action:inmenu View", "Additional Information"));
198 visibleRolesMenu
->setDelayed(false);
200 foreach (QAction
* action
, visibleRolesGroup
->actions()) {
201 visibleRolesMenu
->addAction(action
);
204 KToggleAction
* showInGroups
= m_actionCollection
->add
<KToggleAction
>("show_in_groups");
205 showInGroups
->setIcon(KIcon("view-group"));
206 showInGroups
->setText(i18nc("@action:inmenu View", "Show in Groups"));
207 connect(showInGroups
, SIGNAL(triggered(bool)), this, SLOT(toggleGroupedSorting(bool)));
209 KToggleAction
* showHiddenFiles
= m_actionCollection
->add
<KToggleAction
>("show_hidden_files");
210 showHiddenFiles
->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
211 showHiddenFiles
->setShortcuts(QList
<QKeySequence
>() << Qt::ALT
+ Qt::Key_Period
<< Qt::Key_F8
);
212 connect(showHiddenFiles
, SIGNAL(triggered(bool)), this, SLOT(toggleShowHiddenFiles(bool)));
214 KAction
* adjustViewProps
= m_actionCollection
->addAction("view_properties");
215 adjustViewProps
->setText(i18nc("@action:inmenu View", "Adjust View Properties..."));
216 connect(adjustViewProps
, SIGNAL(triggered()), this, SLOT(slotAdjustViewProperties()));
219 QActionGroup
* DolphinViewActionHandler::createFileItemRolesActionGroup(const QString
& groupPrefix
)
221 const bool isSortGroup
= (groupPrefix
== QLatin1String("sort_by_"));
222 Q_ASSERT(isSortGroup
|| (!isSortGroup
&& groupPrefix
== QLatin1String("show_")));
224 QActionGroup
* rolesActionGroup
= new QActionGroup(m_actionCollection
);
225 rolesActionGroup
->setExclusive(isSortGroup
);
227 connect(rolesActionGroup
, SIGNAL(triggered(QAction
*)),
228 this, SLOT(slotSortTriggered(QAction
*)));
230 connect(rolesActionGroup
, SIGNAL(triggered(QAction
*)),
231 this, SLOT(toggleVisibleRole(QAction
*)));
235 KActionMenu
* groupMenu
= 0;
236 QActionGroup
* groupMenuGroup
= 0;
238 bool nepomukRunning
= false;
239 bool indexingEnabled
= false;
241 nepomukRunning
= (Nepomuk::ResourceManager::instance()->init() == 0);
242 if (nepomukRunning
) {
243 KConfig
config("nepomukserverrc");
244 indexingEnabled
= config
.group("Service-nepomukfileindexer").readEntry("autostart", false);
248 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
249 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
250 if (!isSortGroup
&& info
.role
== "name") {
251 // It should not be possible to hide the "name" role
255 KToggleAction
* action
= 0;
256 const QString name
= groupPrefix
+ info
.role
;
257 if (info
.group
.isEmpty()) {
258 action
= m_actionCollection
->add
<KToggleAction
>(name
);
259 action
->setActionGroup(rolesActionGroup
);
261 if (!groupMenu
|| info
.group
!= groupName
) {
262 groupName
= info
.group
;
263 groupMenu
= m_actionCollection
->add
<KActionMenu
>(groupName
);
264 groupMenu
->setText(groupName
);
265 groupMenu
->setActionGroup(rolesActionGroup
);
267 groupMenuGroup
= new QActionGroup(groupMenu
);
268 groupMenuGroup
->setExclusive(isSortGroup
);
270 connect(groupMenuGroup
, SIGNAL(triggered(QAction
*)),
271 this, SLOT(slotSortTriggered(QAction
*)));
273 connect(groupMenuGroup
, SIGNAL(triggered(QAction
*)),
274 this, SLOT(toggleVisibleRole(QAction
*)));
278 action
= new KToggleAction(groupMenu
);
279 action
->setActionGroup(groupMenuGroup
);
280 groupMenu
->addAction(action
);
282 action
->setText(info
.translation
);
283 action
->setData(info
.role
);
285 const bool enable
= (!info
.requiresNepomuk
&& !info
.requiresIndexer
) ||
286 (info
.requiresNepomuk
&& nepomukRunning
) ||
287 (info
.requiresIndexer
&& indexingEnabled
);
288 action
->setEnabled(enable
);
291 m_sortByActions
.insert(info
.role
, action
);
293 m_visibleRoles
.insert(info
.role
, action
);
297 return rolesActionGroup
;
300 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction
* action
)
302 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
303 m_currentView
->setMode(mode
);
305 QAction
* viewModeMenu
= m_actionCollection
->action("view_mode");
306 viewModeMenu
->setIcon(KIcon(action
->icon()));
309 void DolphinViewActionHandler::slotRename()
311 emit
actionBeingHandled();
312 m_currentView
->renameSelectedItems();
315 void DolphinViewActionHandler::slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers modifiers
)
317 emit
actionBeingHandled();
318 // Note: kde3's konq_mainwindow.cpp used to check
319 // reason == KAction::PopupMenuActivation && ...
320 // but this isn't supported anymore
321 if (modifiers
& Qt::ShiftModifier
) {
322 m_currentView
->deleteSelectedItems();
324 m_currentView
->trashSelectedItems();
328 void DolphinViewActionHandler::slotDeleteItems()
330 emit
actionBeingHandled();
331 m_currentView
->deleteSelectedItems();
334 void DolphinViewActionHandler::togglePreview(bool show
)
336 emit
actionBeingHandled();
337 m_currentView
->setPreviewsShown(show
);
340 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown
)
343 // It is not enough to update the 'Show Preview' action, also
344 // the 'Zoom In' and 'Zoom Out' actions must be adapted.
348 QString
DolphinViewActionHandler::currentViewModeActionName() const
350 switch (m_currentView
->mode()) {
351 case DolphinView::IconsView
:
353 case DolphinView::DetailsView
:
355 case DolphinView::CompactView
:
361 return QString(); // can't happen
364 KActionCollection
* DolphinViewActionHandler::actionCollection()
366 return m_actionCollection
;
369 void DolphinViewActionHandler::updateViewActions()
371 QAction
* viewModeAction
= m_actionCollection
->action(currentViewModeActionName());
372 if (viewModeAction
) {
373 viewModeAction
->setChecked(true);
375 QAction
* viewModeMenu
= m_actionCollection
->action("view_mode");
376 viewModeMenu
->setIcon(KIcon(viewModeAction
->icon()));
379 QAction
* showPreviewAction
= m_actionCollection
->action("show_preview");
380 showPreviewAction
->setChecked(m_currentView
->previewsShown());
382 slotSortOrderChanged(m_currentView
->sortOrder());
383 slotSortFoldersFirstChanged(m_currentView
->sortFoldersFirst());
384 slotVisibleRolesChanged(m_currentView
->visibleRoles(), QList
<QByteArray
>());
385 slotGroupedSortingChanged(m_currentView
->groupedSorting());
386 slotSortRoleChanged(m_currentView
->sortRole());
387 slotZoomLevelChanged(m_currentView
->zoomLevel(), -1);
389 QAction
* showHiddenFilesAction
= m_actionCollection
->action("show_hidden_files");
390 showHiddenFilesAction
->setChecked(m_currentView
->hiddenFilesShown());
393 void DolphinViewActionHandler::zoomIn()
395 const int level
= m_currentView
->zoomLevel();
396 m_currentView
->setZoomLevel(level
+ 1);
400 void DolphinViewActionHandler::zoomOut()
402 const int level
= m_currentView
->zoomLevel();
403 m_currentView
->setZoomLevel(level
- 1);
407 void DolphinViewActionHandler::toggleSortOrder()
409 const Qt::SortOrder order
= (m_currentView
->sortOrder() == Qt::AscendingOrder
) ?
410 Qt::DescendingOrder
:
412 m_currentView
->setSortOrder(order
);
415 void DolphinViewActionHandler::toggleSortFoldersFirst()
417 const bool sortFirst
= m_currentView
->sortFoldersFirst();
418 m_currentView
->setSortFoldersFirst(!sortFirst
);
421 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order
)
423 QAction
* descending
= m_actionCollection
->action("descending");
424 const bool sortDescending
= (order
== Qt::DescendingOrder
);
425 descending
->setChecked(sortDescending
);
428 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst
)
430 m_actionCollection
->action("folders_first")->setChecked(foldersFirst
);
433 void DolphinViewActionHandler::toggleVisibleRole(QAction
* action
)
435 emit
actionBeingHandled();
437 const QByteArray toggledRole
= action
->data().toByteArray();
439 QList
<QByteArray
> roles
= m_currentView
->visibleRoles();
441 const bool show
= action
->isChecked();
443 const int index
= roles
.indexOf(toggledRole
);
444 const bool containsInfo
= (index
>= 0);
445 if (show
&& !containsInfo
) {
446 roles
.append(toggledRole
);
447 m_currentView
->setVisibleRoles(roles
);
448 } else if (!show
&& containsInfo
) {
449 roles
.removeAt(index
);
450 m_currentView
->setVisibleRoles(roles
);
451 Q_ASSERT(roles
.indexOf(toggledRole
) < 0);
455 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList
<QByteArray
>& current
,
456 const QList
<QByteArray
>& previous
)
460 const QSet
<QByteArray
> checkedRoles
= current
.toSet();
461 QHashIterator
<QByteArray
, KToggleAction
*> it(m_visibleRoles
);
462 while (it
.hasNext()) {
464 const QByteArray
& role
= it
.key();
465 KToggleAction
* action
= it
.value();
466 action
->setChecked(checkedRoles
.contains(role
));
470 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped
)
472 m_currentView
->setGroupedSorting(grouped
);
475 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting
)
477 QAction
* showInGroupsAction
= m_actionCollection
->action("show_in_groups");
478 showInGroupsAction
->setChecked(groupedSorting
);
481 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show
)
483 emit
actionBeingHandled();
484 m_currentView
->setHiddenFilesShown(show
);
487 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown
)
489 QAction
* showHiddenFilesAction
= m_actionCollection
->action("show_hidden_files");
490 showHiddenFilesAction
->setChecked(shown
);
493 KToggleAction
* DolphinViewActionHandler::iconsModeAction()
495 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>("icons");
496 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
497 iconsView
->setToolTip(i18nc("@info", "Icons view mode"));
498 iconsView
->setShortcut(Qt::CTRL
| Qt::Key_1
);
499 iconsView
->setIcon(KIcon("view-list-icons"));
500 iconsView
->setData(QVariant::fromValue(DolphinView::IconsView
));
504 KToggleAction
* DolphinViewActionHandler::compactModeAction()
506 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>("compact");
507 iconsView
->setText(i18nc("@action:inmenu View Mode", "Compact"));
508 iconsView
->setToolTip(i18nc("@info", "Compact view mode"));
509 iconsView
->setShortcut(Qt::CTRL
| Qt::Key_2
);
510 iconsView
->setIcon(KIcon("view-list-details")); // TODO: discuss with Oxygen-team the wrong (?) name
511 iconsView
->setData(QVariant::fromValue(DolphinView::CompactView
));
515 KToggleAction
* DolphinViewActionHandler::detailsModeAction()
517 KToggleAction
* detailsView
= m_actionCollection
->add
<KToggleAction
>("details");
518 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
519 detailsView
->setToolTip(i18nc("@info", "Details view mode"));
520 detailsView
->setShortcut(Qt::CTRL
| Qt::Key_3
);
521 detailsView
->setIcon(KIcon("view-list-tree"));
522 detailsView
->setData(QVariant::fromValue(DolphinView::DetailsView
));
526 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray
& role
)
528 KToggleAction
* action
= m_sortByActions
.value(role
);
530 action
->setChecked(true);
532 QAction
* sortByMenu
= m_actionCollection
->action("sort");
533 sortByMenu
->setIcon(KIcon(action
->icon()));
537 void DolphinViewActionHandler::slotZoomLevelChanged(int current
, int previous
)
541 QAction
* zoomInAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomIn
));
543 zoomInAction
->setEnabled(current
< ZoomLevelInfo::maximumLevel());
546 QAction
* zoomOutAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomOut
));
548 zoomOutAction
->setEnabled(current
> ZoomLevelInfo::minimumLevel());
552 void DolphinViewActionHandler::slotSortTriggered(QAction
* action
)
554 // The radiobuttons of the "Sort By"-menu are split between the main-menu
555 // and several sub-menus. Because of this they don't have a common
556 // action-group that assures an exclusive toggle-state between the main-menu
557 // actions and the sub-menu-actions. If an action gets checked, it must
558 // be assured that all other actions get unchecked.
559 QAction
* sortByMenu
= m_actionCollection
->action("sort");
560 foreach (QAction
* groupAction
, sortByMenu
->menu()->actions()) {
561 KActionMenu
* actionMenu
= qobject_cast
<KActionMenu
*>(groupAction
);
563 foreach (QAction
* subAction
, actionMenu
->menu()->actions()) {
564 subAction
->setChecked(false);
566 } else if (groupAction
->actionGroup()) {
567 groupAction
->setChecked(false);
570 action
->setChecked(true);
572 // Apply the activated sort-role to the view
573 const QByteArray role
= action
->data().toByteArray();
574 m_currentView
->setSortRole(role
);
577 void DolphinViewActionHandler::slotAdjustViewProperties()
579 emit
actionBeingHandled();
580 QPointer
<ViewPropertiesDialog
> dialog
= new ViewPropertiesDialog(m_currentView
);
585 void DolphinViewActionHandler::slotProperties()
587 KPropertiesDialog
* dialog
= 0;
588 const KFileItemList list
= m_currentView
->selectedItems();
589 if (list
.isEmpty()) {
590 const KUrl url
= m_currentView
->url();
591 dialog
= new KPropertiesDialog(url
, m_currentView
);
593 dialog
= new KPropertiesDialog(list
, m_currentView
);
596 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
599 dialog
->activateWindow();