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>
39 #include <KPropertiesDialog>
43 #include <Nepomuk/ResourceManager>
48 DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection
* collection
, QObject
* parent
) :
50 m_actionCollection(collection
),
55 Q_ASSERT(m_actionCollection
);
59 void DolphinViewActionHandler::setCurrentView(DolphinView
* view
)
64 disconnect(m_currentView
, 0, this, 0);
69 connect(view
, SIGNAL(modeChanged(DolphinView::Mode
,DolphinView::Mode
)),
70 this, SLOT(updateViewActions()));
71 connect(view
, SIGNAL(previewsShownChanged(bool)),
72 this, SLOT(slotPreviewsShownChanged(bool)));
73 connect(view
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
74 this, SLOT(slotSortOrderChanged(Qt::SortOrder
)));
75 connect(view
, SIGNAL(sortFoldersFirstChanged(bool)),
76 this, SLOT(slotSortFoldersFirstChanged(bool)));
77 connect(view
, SIGNAL(visibleRolesChanged(QList
<QByteArray
>,QList
<QByteArray
>)),
78 this, SLOT(slotVisibleRolesChanged(QList
<QByteArray
>,QList
<QByteArray
>)));
79 connect(view
, SIGNAL(groupedSortingChanged(bool)),
80 this, SLOT(slotGroupedSortingChanged(bool)));
81 connect(view
, SIGNAL(hiddenFilesShownChanged(bool)),
82 this, SLOT(slotHiddenFilesShownChanged(bool)));
83 connect(view
, SIGNAL(sortRoleChanged(QByteArray
)),
84 this, SLOT(slotSortRoleChanged(QByteArray
)));
85 connect(view
, SIGNAL(zoomLevelChanged(int,int)),
86 this, SLOT(slotZoomLevelChanged(int,int)));
89 DolphinView
* DolphinViewActionHandler::currentView()
94 void DolphinViewActionHandler::createActions()
96 // This action doesn't appear in the GUI, it's for the shortcut only.
97 // KNewFileMenu takes care of the GUI stuff.
98 KAction
* newDirAction
= m_actionCollection
->addAction("create_dir");
99 newDirAction
->setText(i18nc("@action", "Create Folder..."));
100 newDirAction
->setShortcut(Qt::Key_F10
);
101 newDirAction
->setIcon(KIcon("folder-new"));
102 connect(newDirAction
, SIGNAL(triggered()), this, SIGNAL(createDirectory()));
106 KAction
* rename
= m_actionCollection
->addAction("rename");
107 rename
->setText(i18nc("@action:inmenu File", "Rename..."));
108 rename
->setShortcut(Qt::Key_F2
);
109 rename
->setIcon(KIcon("edit-rename"));
110 connect(rename
, SIGNAL(triggered()), this, SLOT(slotRename()));
112 KAction
* moveToTrash
= m_actionCollection
->addAction("move_to_trash");
113 moveToTrash
->setText(i18nc("@action:inmenu File", "Move to Trash"));
114 moveToTrash
->setIcon(KIcon("user-trash"));
115 moveToTrash
->setShortcut(QKeySequence::Delete
);
116 connect(moveToTrash
, SIGNAL(triggered(Qt::MouseButtons
,Qt::KeyboardModifiers
)),
117 this, SLOT(slotTrashActivated(Qt::MouseButtons
,Qt::KeyboardModifiers
)));
119 KAction
* deleteAction
= m_actionCollection
->addAction("delete");
120 deleteAction
->setIcon(KIcon("edit-delete"));
121 deleteAction
->setText(i18nc("@action:inmenu File", "Delete"));
122 deleteAction
->setShortcut(Qt::SHIFT
| Qt::Key_Delete
);
123 connect(deleteAction
, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
125 // This action is useful for being enabled when "move_to_trash" should be
126 // disabled and "delete" is enabled (e.g. non-local files), so that Key_Del
127 // can be used for deleting the file (#76016). It needs to be a separate action
128 // so that the Edit menu isn't affected.
129 KAction
* deleteWithTrashShortcut
= m_actionCollection
->addAction("delete_shortcut");
130 // The descriptive text is just for the shortcuts editor.
131 deleteWithTrashShortcut
->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
132 deleteWithTrashShortcut
->setShortcut(QKeySequence::Delete
);
133 deleteWithTrashShortcut
->setEnabled(false);
134 connect(deleteWithTrashShortcut
, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
136 KAction
*propertiesAction
= m_actionCollection
->addAction( "properties" );
137 // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
138 propertiesAction
->setText( i18nc("@action:inmenu File", "Properties") );
139 propertiesAction
->setIcon(KIcon("document-properties"));
140 propertiesAction
->setShortcut(Qt::ALT
| Qt::Key_Return
);
141 connect(propertiesAction
, SIGNAL(triggered()), SLOT(slotProperties()));
144 KToggleAction
* iconsAction
= iconsModeAction();
145 KToggleAction
* compactAction
= compactModeAction();
146 KToggleAction
* detailsAction
= detailsModeAction();
148 KSelectAction
* viewModeActions
= m_actionCollection
->add
<KSelectAction
>("view_mode");
149 viewModeActions
->setText(i18nc("@action:intoolbar", "View Mode"));
150 viewModeActions
->addAction(iconsAction
);
151 viewModeActions
->addAction(compactAction
);
152 viewModeActions
->addAction(detailsAction
);
153 viewModeActions
->setToolBarMode(KSelectAction::MenuMode
);
154 connect(viewModeActions
, SIGNAL(triggered(QAction
*)), this, SLOT(slotViewModeActionTriggered(QAction
*)));
156 KStandardAction::zoomIn(this,
160 KStandardAction::zoomOut(this,
164 KToggleAction
* showPreview
= m_actionCollection
->add
<KToggleAction
>("show_preview");
165 showPreview
->setText(i18nc("@action:intoolbar", "Preview"));
166 showPreview
->setToolTip(i18nc("@info", "Show preview of files and folders"));
167 showPreview
->setIcon(KIcon("view-preview"));
168 connect(showPreview
, SIGNAL(triggered(bool)), this, SLOT(togglePreview(bool)));
170 KToggleAction
* sortDescending
= m_actionCollection
->add
<KToggleAction
>("descending");
171 sortDescending
->setText(i18nc("@action:inmenu Sort", "Descending"));
172 connect(sortDescending
, SIGNAL(triggered()), this, SLOT(toggleSortOrder()));
174 KToggleAction
* sortFoldersFirst
= m_actionCollection
->add
<KToggleAction
>("folders_first");
175 sortFoldersFirst
->setText(i18nc("@action:inmenu Sort", "Folders First"));
176 connect(sortFoldersFirst
, SIGNAL(triggered()), this, SLOT(toggleSortFoldersFirst()));
179 QActionGroup
* sortByActionGroup
= createFileItemRolesActionGroup("sort_by_");
181 KActionMenu
* sortByActionMenu
= m_actionCollection
->add
<KActionMenu
>("sort");
182 sortByActionMenu
->setText(i18nc("@action:inmenu View", "Sort By"));
183 sortByActionMenu
->setDelayed(false);
185 foreach (QAction
* action
, sortByActionGroup
->actions()) {
186 sortByActionMenu
->addAction(action
);
188 sortByActionMenu
->addSeparator();
189 sortByActionMenu
->addAction(sortDescending
);
190 sortByActionMenu
->addAction(sortFoldersFirst
);
192 // View -> Additional Information
193 QActionGroup
* visibleRolesGroup
= createFileItemRolesActionGroup("show_");
195 KActionMenu
* visibleRolesMenu
= m_actionCollection
->add
<KActionMenu
>("additional_info");
196 visibleRolesMenu
->setText(i18nc("@action:inmenu View", "Additional Information"));
197 visibleRolesMenu
->setDelayed(false);
199 foreach (QAction
* action
, visibleRolesGroup
->actions()) {
200 visibleRolesMenu
->addAction(action
);
203 KToggleAction
* showInGroups
= m_actionCollection
->add
<KToggleAction
>("show_in_groups");
204 showInGroups
->setIcon(KIcon("view-group"));
205 showInGroups
->setText(i18nc("@action:inmenu View", "Show in Groups"));
206 connect(showInGroups
, SIGNAL(triggered(bool)), this, SLOT(toggleGroupedSorting(bool)));
208 KToggleAction
* showHiddenFiles
= m_actionCollection
->add
<KToggleAction
>("show_hidden_files");
209 showHiddenFiles
->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
210 showHiddenFiles
->setShortcuts(QList
<QKeySequence
>() << Qt::ALT
+ Qt::Key_Period
<< Qt::Key_F8
);
211 connect(showHiddenFiles
, SIGNAL(triggered(bool)), this, SLOT(toggleShowHiddenFiles(bool)));
213 KAction
* adjustViewProps
= m_actionCollection
->addAction("view_properties");
214 adjustViewProps
->setText(i18nc("@action:inmenu View", "Adjust View Properties..."));
215 connect(adjustViewProps
, SIGNAL(triggered()), this, SLOT(slotAdjustViewProperties()));
218 QActionGroup
* DolphinViewActionHandler::createFileItemRolesActionGroup(const QString
& groupPrefix
)
220 const bool isSortGroup
= (groupPrefix
== QLatin1String("sort_by_"));
221 Q_ASSERT(isSortGroup
|| (!isSortGroup
&& groupPrefix
== QLatin1String("show_")));
223 QActionGroup
* rolesActionGroup
= new QActionGroup(m_actionCollection
);
224 rolesActionGroup
->setExclusive(isSortGroup
);
226 connect(rolesActionGroup
, SIGNAL(triggered(QAction
*)),
227 this, SLOT(slotSortTriggered(QAction
*)));
229 connect(rolesActionGroup
, SIGNAL(triggered(QAction
*)),
230 this, SLOT(toggleVisibleRole(QAction
*)));
234 KActionMenu
* groupMenu
= 0;
235 QActionGroup
* groupMenuGroup
= 0;
237 bool nepomukRunning
= false;
238 bool indexingEnabled
= false;
240 nepomukRunning
= (Nepomuk::ResourceManager::instance()->init() == 0);
241 if (nepomukRunning
) {
242 KConfig
config("nepomukserverrc");
243 indexingEnabled
= config
.group("Service-nepomukfileindexer").readEntry("autostart", false);
247 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
248 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
249 if (!isSortGroup
&& info
.role
== "text") {
250 // It should not be possible to hide the "text" role
254 KToggleAction
* action
= 0;
255 const QString name
= groupPrefix
+ info
.role
;
256 if (info
.group
.isEmpty()) {
257 action
= m_actionCollection
->add
<KToggleAction
>(name
);
258 action
->setActionGroup(rolesActionGroup
);
260 if (!groupMenu
|| info
.group
!= groupName
) {
261 groupName
= info
.group
;
262 groupMenu
= m_actionCollection
->add
<KActionMenu
>(groupName
);
263 groupMenu
->setText(groupName
);
264 groupMenu
->setActionGroup(rolesActionGroup
);
266 groupMenuGroup
= new QActionGroup(groupMenu
);
267 groupMenuGroup
->setExclusive(isSortGroup
);
269 connect(groupMenuGroup
, SIGNAL(triggered(QAction
*)),
270 this, SLOT(slotSortTriggered(QAction
*)));
272 connect(groupMenuGroup
, SIGNAL(triggered(QAction
*)),
273 this, SLOT(toggleVisibleRole(QAction
*)));
277 action
= new KToggleAction(groupMenu
);
278 action
->setActionGroup(groupMenuGroup
);
279 groupMenu
->addAction(action
);
281 action
->setText(info
.translation
);
282 action
->setData(info
.role
);
284 const bool enable
= (!info
.requiresNepomuk
&& !info
.requiresIndexer
) ||
285 (info
.requiresNepomuk
&& nepomukRunning
) ||
286 (info
.requiresIndexer
&& indexingEnabled
);
287 action
->setEnabled(enable
);
290 m_sortByActions
.insert(info
.role
, action
);
292 m_visibleRoles
.insert(info
.role
, action
);
296 return rolesActionGroup
;
299 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction
* action
)
301 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
302 m_currentView
->setMode(mode
);
304 QAction
* viewModeMenu
= m_actionCollection
->action("view_mode");
305 viewModeMenu
->setIcon(KIcon(action
->icon()));
308 void DolphinViewActionHandler::slotRename()
310 emit
actionBeingHandled();
311 m_currentView
->renameSelectedItems();
314 void DolphinViewActionHandler::slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers modifiers
)
316 emit
actionBeingHandled();
317 // Note: kde3's konq_mainwindow.cpp used to check
318 // reason == KAction::PopupMenuActivation && ...
319 // but this isn't supported anymore
320 if (modifiers
& Qt::ShiftModifier
) {
321 m_currentView
->deleteSelectedItems();
323 m_currentView
->trashSelectedItems();
327 void DolphinViewActionHandler::slotDeleteItems()
329 emit
actionBeingHandled();
330 m_currentView
->deleteSelectedItems();
333 void DolphinViewActionHandler::togglePreview(bool show
)
335 emit
actionBeingHandled();
336 m_currentView
->setPreviewsShown(show
);
339 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown
)
342 // It is not enough to update the 'Show Preview' action, also
343 // the 'Zoom In' and 'Zoom Out' actions must be adapted.
347 QString
DolphinViewActionHandler::currentViewModeActionName() const
349 switch (m_currentView
->mode()) {
350 case DolphinView::IconsView
:
352 case DolphinView::DetailsView
:
354 case DolphinView::CompactView
:
360 return QString(); // can't happen
363 KActionCollection
* DolphinViewActionHandler::actionCollection()
365 return m_actionCollection
;
368 void DolphinViewActionHandler::updateViewActions()
370 QAction
* viewModeAction
= m_actionCollection
->action(currentViewModeActionName());
371 if (viewModeAction
) {
372 viewModeAction
->setChecked(true);
374 QAction
* viewModeMenu
= m_actionCollection
->action("view_mode");
375 viewModeMenu
->setIcon(KIcon(viewModeAction
->icon()));
378 QAction
* showPreviewAction
= m_actionCollection
->action("show_preview");
379 showPreviewAction
->setChecked(m_currentView
->previewsShown());
381 slotSortOrderChanged(m_currentView
->sortOrder());
382 slotSortFoldersFirstChanged(m_currentView
->sortFoldersFirst());
383 slotVisibleRolesChanged(m_currentView
->visibleRoles(), QList
<QByteArray
>());
384 slotGroupedSortingChanged(m_currentView
->groupedSorting());
385 slotSortRoleChanged(m_currentView
->sortRole());
386 slotZoomLevelChanged(m_currentView
->zoomLevel(), -1);
388 QAction
* showHiddenFilesAction
= m_actionCollection
->action("show_hidden_files");
389 showHiddenFilesAction
->setChecked(m_currentView
->hiddenFilesShown());
392 void DolphinViewActionHandler::zoomIn()
394 const int level
= m_currentView
->zoomLevel();
395 m_currentView
->setZoomLevel(level
+ 1);
399 void DolphinViewActionHandler::zoomOut()
401 const int level
= m_currentView
->zoomLevel();
402 m_currentView
->setZoomLevel(level
- 1);
406 void DolphinViewActionHandler::toggleSortOrder()
408 const Qt::SortOrder order
= (m_currentView
->sortOrder() == Qt::AscendingOrder
) ?
409 Qt::DescendingOrder
:
411 m_currentView
->setSortOrder(order
);
414 void DolphinViewActionHandler::toggleSortFoldersFirst()
416 const bool sortFirst
= m_currentView
->sortFoldersFirst();
417 m_currentView
->setSortFoldersFirst(!sortFirst
);
420 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order
)
422 QAction
* descending
= m_actionCollection
->action("descending");
423 const bool sortDescending
= (order
== Qt::DescendingOrder
);
424 descending
->setChecked(sortDescending
);
427 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst
)
429 m_actionCollection
->action("folders_first")->setChecked(foldersFirst
);
432 void DolphinViewActionHandler::toggleVisibleRole(QAction
* action
)
434 emit
actionBeingHandled();
436 const QByteArray toggledRole
= action
->data().toByteArray();
438 QList
<QByteArray
> roles
= m_currentView
->visibleRoles();
440 const bool show
= action
->isChecked();
442 const int index
= roles
.indexOf(toggledRole
);
443 const bool containsInfo
= (index
>= 0);
444 if (show
&& !containsInfo
) {
445 roles
.append(toggledRole
);
446 m_currentView
->setVisibleRoles(roles
);
447 } else if (!show
&& containsInfo
) {
448 roles
.removeAt(index
);
449 m_currentView
->setVisibleRoles(roles
);
450 Q_ASSERT(roles
.indexOf(toggledRole
) < 0);
454 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList
<QByteArray
>& current
,
455 const QList
<QByteArray
>& previous
)
459 const QSet
<QByteArray
> checkedRoles
= current
.toSet();
460 QHashIterator
<QByteArray
, KToggleAction
*> it(m_visibleRoles
);
461 while (it
.hasNext()) {
463 const QByteArray
& role
= it
.key();
464 KToggleAction
* action
= it
.value();
465 action
->setChecked(checkedRoles
.contains(role
));
469 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped
)
471 m_currentView
->setGroupedSorting(grouped
);
474 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting
)
476 QAction
* showInGroupsAction
= m_actionCollection
->action("show_in_groups");
477 showInGroupsAction
->setChecked(groupedSorting
);
480 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show
)
482 emit
actionBeingHandled();
483 m_currentView
->setHiddenFilesShown(show
);
486 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown
)
488 QAction
* showHiddenFilesAction
= m_actionCollection
->action("show_hidden_files");
489 showHiddenFilesAction
->setChecked(shown
);
492 KToggleAction
* DolphinViewActionHandler::iconsModeAction()
494 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>("icons");
495 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
496 iconsView
->setToolTip(i18nc("@info", "Icons view mode"));
497 iconsView
->setShortcut(Qt::CTRL
| Qt::Key_1
);
498 iconsView
->setIcon(KIcon("view-list-icons"));
499 iconsView
->setData(QVariant::fromValue(DolphinView::IconsView
));
503 KToggleAction
* DolphinViewActionHandler::compactModeAction()
505 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>("compact");
506 iconsView
->setText(i18nc("@action:inmenu View Mode", "Compact"));
507 iconsView
->setToolTip(i18nc("@info", "Compact view mode"));
508 iconsView
->setShortcut(Qt::CTRL
| Qt::Key_2
);
509 iconsView
->setIcon(KIcon("view-list-details")); // TODO: discuss with Oxygen-team the wrong (?) name
510 iconsView
->setData(QVariant::fromValue(DolphinView::CompactView
));
514 KToggleAction
* DolphinViewActionHandler::detailsModeAction()
516 KToggleAction
* detailsView
= m_actionCollection
->add
<KToggleAction
>("details");
517 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
518 detailsView
->setToolTip(i18nc("@info", "Details view mode"));
519 detailsView
->setShortcut(Qt::CTRL
| Qt::Key_3
);
520 detailsView
->setIcon(KIcon("view-list-tree"));
521 detailsView
->setData(QVariant::fromValue(DolphinView::DetailsView
));
525 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray
& role
)
527 KToggleAction
* action
= m_sortByActions
.value(role
);
529 action
->setChecked(true);
531 QAction
* sortByMenu
= m_actionCollection
->action("sort");
532 sortByMenu
->setIcon(KIcon(action
->icon()));
536 void DolphinViewActionHandler::slotZoomLevelChanged(int current
, int previous
)
540 QAction
* zoomInAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomIn
));
542 zoomInAction
->setEnabled(current
< ZoomLevelInfo::maximumLevel());
545 QAction
* zoomOutAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomOut
));
547 zoomOutAction
->setEnabled(current
> ZoomLevelInfo::minimumLevel());
551 void DolphinViewActionHandler::slotSortTriggered(QAction
* action
)
553 // The radiobuttons of the "Sort By"-menu are split between the main-menu
554 // and several sub-menus. Because of this they don't have a common
555 // action-group that assures an exclusive toggle-state between the main-menu
556 // actions and the sub-menu-actions. If an action gets checked, it must
557 // be assured that all other actions get unchecked.
558 QAction
* sortByMenu
= m_actionCollection
->action("sort");
559 foreach (QAction
* groupAction
, sortByMenu
->menu()->actions()) {
560 KActionMenu
* actionMenu
= qobject_cast
<KActionMenu
*>(groupAction
);
562 foreach (QAction
* subAction
, actionMenu
->menu()->actions()) {
563 subAction
->setChecked(false);
565 } else if (groupAction
->actionGroup()) {
566 groupAction
->setChecked(false);
569 action
->setChecked(true);
571 // Apply the activated sort-role to the view
572 const QByteArray role
= action
->data().toByteArray();
573 m_currentView
->setSortRole(role
);
576 void DolphinViewActionHandler::slotAdjustViewProperties()
578 emit
actionBeingHandled();
579 QPointer
<ViewPropertiesDialog
> dialog
= new ViewPropertiesDialog(m_currentView
);
584 void DolphinViewActionHandler::slotProperties()
586 KPropertiesDialog
* dialog
= 0;
587 const KFileItemList list
= m_currentView
->selectedItems();
588 if (list
.isEmpty()) {
589 const KUrl url
= m_currentView
->url();
590 dialog
= new KPropertiesDialog(url
, m_currentView
);
592 dialog
= new KPropertiesDialog(list
, m_currentView
);
595 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
598 dialog
->activateWindow();