1 /***************************************************************************
2 * Copyright (C) 2008 by David Faure <faure@kde.org> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "dolphinviewactionhandler.h"
22 #include "additionalinfoaccessor.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 <KFileItemDelegate>
33 #include <KNewFileMenu>
34 #include <KSelectAction>
35 #include <KToggleAction>
37 #include <KPropertiesDialog>
39 DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection
* collection
, QObject
* parent
)
41 m_actionCollection(collection
),
44 Q_ASSERT(m_actionCollection
);
48 void DolphinViewActionHandler::setCurrentView(DolphinView
* view
)
53 disconnect(m_currentView
, 0, this, 0);
57 connect(view
, SIGNAL(modeChanged()),
58 this, SLOT(updateViewActions()));
59 connect(view
, SIGNAL(showPreviewChanged()),
60 this, SLOT(slotShowPreviewChanged()));
61 connect(view
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
62 this, SLOT(slotSortOrderChanged(Qt::SortOrder
)));
63 connect(view
, SIGNAL(sortFoldersFirstChanged(bool)),
64 this, SLOT(slotSortFoldersFirstChanged(bool)));
65 connect(view
, SIGNAL(additionalInfoChanged()),
66 this, SLOT(slotAdditionalInfoChanged()));
67 connect(view
, SIGNAL(categorizedSortingChanged()),
68 this, SLOT(slotCategorizedSortingChanged()));
69 connect(view
, SIGNAL(showHiddenFilesChanged()),
70 this, SLOT(slotShowHiddenFilesChanged()));
71 connect(view
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
72 this, SLOT(slotSortingChanged(DolphinView::Sorting
)));
73 connect(view
, SIGNAL(zoomLevelChanged(int)),
74 this, SLOT(slotZoomLevelChanged(int)));
77 DolphinView
* DolphinViewActionHandler::currentView()
82 void DolphinViewActionHandler::createActions()
84 // This action doesn't appear in the GUI, it's for the shortcut only.
85 // KNewFileMenu takes care of the GUI stuff.
86 KAction
* newDirAction
= m_actionCollection
->addAction("create_dir");
87 newDirAction
->setText(i18nc("@action", "Create Folder..."));
88 newDirAction
->setShortcut(Qt::Key_F10
);
89 newDirAction
->setIcon(KIcon("folder-new"));
90 connect(newDirAction
, SIGNAL(triggered()), this, SIGNAL(createDirectory()));
94 KAction
* rename
= m_actionCollection
->addAction("rename");
95 rename
->setText(i18nc("@action:inmenu File", "Rename..."));
96 rename
->setShortcut(Qt::Key_F2
);
97 rename
->setIcon(KIcon("edit-rename"));
98 connect(rename
, SIGNAL(triggered()), this, SLOT(slotRename()));
100 KAction
* moveToTrash
= m_actionCollection
->addAction("move_to_trash");
101 moveToTrash
->setText(i18nc("@action:inmenu File", "Move to Trash"));
102 moveToTrash
->setIcon(KIcon("user-trash"));
103 moveToTrash
->setShortcut(QKeySequence::Delete
);
104 connect(moveToTrash
, SIGNAL(triggered(Qt::MouseButtons
, Qt::KeyboardModifiers
)),
105 this, SLOT(slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers
)));
107 KAction
* deleteAction
= m_actionCollection
->addAction("delete");
108 deleteAction
->setIcon(KIcon("edit-delete"));
109 deleteAction
->setText(i18nc("@action:inmenu File", "Delete"));
110 deleteAction
->setShortcut(Qt::SHIFT
| Qt::Key_Delete
);
111 connect(deleteAction
, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
113 // This action is useful for being enabled when "move_to_trash" should be
114 // disabled and "delete" is enabled (e.g. non-local files), so that Key_Del
115 // can be used for deleting the file (#76016). It needs to be a separate action
116 // so that the Edit menu isn't affected.
117 KAction
* deleteWithTrashShortcut
= m_actionCollection
->addAction("delete_shortcut");
118 // The descriptive text is just for the shortcuts editor.
119 deleteWithTrashShortcut
->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
120 deleteWithTrashShortcut
->setShortcut(QKeySequence::Delete
);
121 deleteWithTrashShortcut
->setEnabled(false);
122 connect(deleteWithTrashShortcut
, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
124 KAction
*propertiesAction
= m_actionCollection
->addAction( "properties" );
125 // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
126 propertiesAction
->setText( i18nc("@action:inmenu File", "Properties") );
127 propertiesAction
->setIcon(KIcon("document-properties"));
128 propertiesAction
->setShortcut(Qt::ALT
| Qt::Key_Return
);
129 connect(propertiesAction
, SIGNAL(triggered()), SLOT(slotProperties()));
132 KToggleAction
* iconsAction
= iconsModeAction();
133 KToggleAction
* detailsAction
= detailsModeAction();
134 KToggleAction
* columnsAction
= columnsModeAction();
136 KSelectAction
* viewModeActions
= m_actionCollection
->add
<KSelectAction
>("view_mode");
137 viewModeActions
->setText(i18nc("@action:intoolbar", "View Mode"));
138 viewModeActions
->addAction(iconsAction
);
139 viewModeActions
->addAction(detailsAction
);
140 viewModeActions
->addAction(columnsAction
);
141 viewModeActions
->setToolBarMode(KSelectAction::MenuMode
);
142 connect(viewModeActions
, SIGNAL(triggered(QAction
*)), this, SLOT(slotViewModeActionTriggered(QAction
*)));
144 KStandardAction::zoomIn(this,
148 KStandardAction::zoomOut(this,
152 KToggleAction
* showPreview
= m_actionCollection
->add
<KToggleAction
>("show_preview");
153 showPreview
->setText(i18nc("@action:intoolbar", "Preview"));
154 showPreview
->setToolTip(i18nc("@info", "Show preview of files and folders"));
155 showPreview
->setIcon(KIcon("view-preview"));
156 connect(showPreview
, SIGNAL(triggered(bool)), this, SLOT(togglePreview(bool)));
158 KToggleAction
* sortDescending
= m_actionCollection
->add
<KToggleAction
>("descending");
159 sortDescending
->setText(i18nc("@action:inmenu Sort", "Descending"));
160 connect(sortDescending
, SIGNAL(triggered()), this, SLOT(toggleSortOrder()));
162 KToggleAction
* sortFoldersFirst
= m_actionCollection
->add
<KToggleAction
>("folders_first");
163 sortFoldersFirst
->setText(i18nc("@action:inmenu Sort", "Folders First"));
164 connect(sortFoldersFirst
, SIGNAL(triggered()), this, SLOT(toggleSortFoldersFirst()));
167 QActionGroup
* sortByActionGroup
= createSortByActionGroup();
168 connect(sortByActionGroup
, SIGNAL(triggered(QAction
*)), this, SLOT(slotSortTriggered(QAction
*)));
170 KActionMenu
* sortByActionMenu
= m_actionCollection
->add
<KActionMenu
>("sort");
171 sortByActionMenu
->setText(i18nc("@action:inmenu View", "Sort By"));
172 sortByActionMenu
->setDelayed(false);
174 foreach (QAction
* action
, sortByActionGroup
->actions()) {
175 sortByActionMenu
->addAction(action
);
177 sortByActionMenu
->addSeparator();
178 sortByActionMenu
->addAction(sortDescending
);
179 sortByActionMenu
->addAction(sortFoldersFirst
);
181 // View -> Additional Information
182 QActionGroup
* additionalInfoGroup
= createAdditionalInformationActionGroup();
183 connect(additionalInfoGroup
, SIGNAL(triggered(QAction
*)), this, SLOT(toggleAdditionalInfo(QAction
*)));
185 KActionMenu
* additionalInfoMenu
= m_actionCollection
->add
<KActionMenu
>("additional_info");
186 additionalInfoMenu
->setText(i18nc("@action:inmenu View", "Additional Information"));
187 additionalInfoMenu
->setDelayed(false);
188 foreach (QAction
* action
, additionalInfoGroup
->actions()) {
189 additionalInfoMenu
->addAction(action
);
192 KToggleAction
* showInGroups
= m_actionCollection
->add
<KToggleAction
>("show_in_groups");
193 showInGroups
->setText(i18nc("@action:inmenu View", "Show in Groups"));
194 connect(showInGroups
, SIGNAL(triggered(bool)), this, SLOT(toggleSortCategorization(bool)));
196 KToggleAction
* showHiddenFiles
= m_actionCollection
->add
<KToggleAction
>("show_hidden_files");
197 showHiddenFiles
->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
198 showHiddenFiles
->setShortcuts(QList
<QKeySequence
>() << Qt::ALT
+ Qt::Key_Period
<< Qt::Key_F8
);
199 connect(showHiddenFiles
, SIGNAL(triggered(bool)), this, SLOT(toggleShowHiddenFiles(bool)));
201 KAction
* adjustViewProps
= m_actionCollection
->addAction("view_properties");
202 adjustViewProps
->setText(i18nc("@action:inmenu View", "Adjust View Properties..."));
203 connect(adjustViewProps
, SIGNAL(triggered()), this, SLOT(slotAdjustViewProperties()));
206 QActionGroup
* DolphinViewActionHandler::createAdditionalInformationActionGroup()
208 QActionGroup
* additionalInfoGroup
= new QActionGroup(m_actionCollection
);
209 additionalInfoGroup
->setExclusive(false);
211 KActionMenu
* showInformationMenu
= m_actionCollection
->add
<KActionMenu
>("additional_info");
212 showInformationMenu
->setText(i18nc("@action:inmenu View", "Additional Information"));
213 showInformationMenu
->setDelayed(false);
215 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
217 const KFileItemDelegate::InformationList infoKeys
= infoAccessor
.keys();
218 foreach (KFileItemDelegate::Information info
, infoKeys
) {
219 const QString name
= infoAccessor
.actionCollectionName(info
, AdditionalInfoAccessor::AdditionalInfoType
);
220 KToggleAction
* action
= m_actionCollection
->add
<KToggleAction
>(name
);
221 action
->setText(infoAccessor
.translation(info
));
222 action
->setData(info
);
223 action
->setActionGroup(additionalInfoGroup
);
226 return additionalInfoGroup
;
229 Q_DECLARE_METATYPE(DolphinView::Sorting
)
231 QActionGroup
* DolphinViewActionHandler::createSortByActionGroup()
233 QActionGroup
* sortByActionGroup
= new QActionGroup(m_actionCollection
);
234 sortByActionGroup
->setExclusive(true);
236 KToggleAction
* sortByName
= m_actionCollection
->add
<KToggleAction
>("sort_by_name");
237 sortByName
->setText(i18nc("@action:inmenu Sort By", "Name"));
238 sortByName
->setData(QVariant::fromValue(DolphinView::SortByName
));
239 sortByActionGroup
->addAction(sortByName
);
241 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
242 const KFileItemDelegate::InformationList infoKeys
= infoAccessor
.keys();
243 foreach (KFileItemDelegate::Information info
, infoKeys
) {
244 const QString name
= infoAccessor
.actionCollectionName(info
, AdditionalInfoAccessor::SortByType
);
245 KToggleAction
* action
= m_actionCollection
->add
<KToggleAction
>(name
);
246 action
->setText(infoAccessor
.translation(info
));
247 const DolphinView::Sorting sorting
= infoAccessor
.sorting(info
);
248 action
->setData(QVariant::fromValue(sorting
));
249 sortByActionGroup
->addAction(action
);
252 return sortByActionGroup
;
255 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction
* action
)
257 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
258 m_currentView
->setMode(mode
);
260 QAction
* viewModeMenu
= m_actionCollection
->action("view_mode");
261 viewModeMenu
->setIcon(KIcon(action
->icon()));
264 void DolphinViewActionHandler::slotRename()
266 emit
actionBeingHandled();
267 m_currentView
->renameSelectedItems();
270 void DolphinViewActionHandler::slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers modifiers
)
272 emit
actionBeingHandled();
273 // Note: kde3's konq_mainwindow.cpp used to check
274 // reason == KAction::PopupMenuActivation && ...
275 // but this isn't supported anymore
276 if (modifiers
& Qt::ShiftModifier
) {
277 m_currentView
->deleteSelectedItems();
279 m_currentView
->trashSelectedItems();
283 void DolphinViewActionHandler::slotDeleteItems()
285 emit
actionBeingHandled();
286 m_currentView
->deleteSelectedItems();
289 void DolphinViewActionHandler::togglePreview(bool show
)
291 emit
actionBeingHandled();
292 m_currentView
->setShowPreview(show
);
295 void DolphinViewActionHandler::slotShowPreviewChanged()
297 // It is not enough to update the 'Show Preview' action, also
298 // the 'Zoom In' and 'Zoom Out' actions must be adapted.
302 QString
DolphinViewActionHandler::currentViewModeActionName() const
304 switch (m_currentView
->mode()) {
305 case DolphinView::IconsView
:
307 case DolphinView::DetailsView
:
309 case DolphinView::ColumnView
:
312 return QString(); // can't happen
315 KActionCollection
* DolphinViewActionHandler::actionCollection()
317 return m_actionCollection
;
320 void DolphinViewActionHandler::updateViewActions()
322 QAction
* viewModeAction
= m_actionCollection
->action(currentViewModeActionName());
323 if (viewModeAction
) {
324 viewModeAction
->setChecked(true);
326 QAction
* viewModeMenu
= m_actionCollection
->action("view_mode");
327 viewModeMenu
->setIcon(KIcon(viewModeAction
->icon()));
330 QAction
* showPreviewAction
= m_actionCollection
->action("show_preview");
331 showPreviewAction
->setChecked(m_currentView
->showPreview());
333 slotSortOrderChanged(m_currentView
->sortOrder());
334 slotSortFoldersFirstChanged(m_currentView
->sortFoldersFirst());
335 slotAdditionalInfoChanged();
336 slotCategorizedSortingChanged();
337 slotSortingChanged(m_currentView
->sorting());
338 slotZoomLevelChanged(m_currentView
->zoomLevel());
340 QAction
* showHiddenFilesAction
= m_actionCollection
->action("show_hidden_files");
341 showHiddenFilesAction
->setChecked(m_currentView
->showHiddenFiles());
344 void DolphinViewActionHandler::zoomIn()
346 const int level
= m_currentView
->zoomLevel();
347 m_currentView
->setZoomLevel(level
+ 1);
351 void DolphinViewActionHandler::zoomOut()
353 const int level
= m_currentView
->zoomLevel();
354 m_currentView
->setZoomLevel(level
- 1);
358 void DolphinViewActionHandler::toggleSortOrder()
360 const Qt::SortOrder order
= (m_currentView
->sortOrder() == Qt::AscendingOrder
) ?
361 Qt::DescendingOrder
:
363 m_currentView
->setSortOrder(order
);
366 void DolphinViewActionHandler::toggleSortFoldersFirst()
368 const bool sortFirst
= m_currentView
->sortFoldersFirst();
369 m_currentView
->setSortFoldersFirst(!sortFirst
);
372 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order
)
374 QAction
* descending
= m_actionCollection
->action("descending");
375 const bool sortDescending
= (order
== Qt::DescendingOrder
);
376 descending
->setChecked(sortDescending
);
379 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst
)
381 m_actionCollection
->action("folders_first")->setChecked(foldersFirst
);
384 void DolphinViewActionHandler::toggleAdditionalInfo(QAction
* action
)
386 emit
actionBeingHandled();
388 const KFileItemDelegate::Information info
=
389 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
391 KFileItemDelegate::InformationList list
= m_currentView
->additionalInfo();
393 const bool show
= action
->isChecked();
395 const int index
= list
.indexOf(info
);
396 const bool containsInfo
= (index
>= 0);
397 if (show
&& !containsInfo
) {
399 m_currentView
->setAdditionalInfo(list
);
400 } else if (!show
&& containsInfo
) {
401 list
.removeAt(index
);
402 m_currentView
->setAdditionalInfo(list
);
403 Q_ASSERT(list
.indexOf(info
) < 0);
407 void DolphinViewActionHandler::slotAdditionalInfoChanged()
409 m_currentView
->updateAdditionalInfoActions(m_actionCollection
);
412 void DolphinViewActionHandler::toggleSortCategorization(bool categorizedSorting
)
414 m_currentView
->setCategorizedSorting(categorizedSorting
);
417 void DolphinViewActionHandler::slotCategorizedSortingChanged()
419 QAction
* showInGroupsAction
= m_actionCollection
->action("show_in_groups");
420 showInGroupsAction
->setChecked(m_currentView
->categorizedSorting());
421 showInGroupsAction
->setEnabled(m_currentView
->supportsCategorizedSorting());
424 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show
)
426 emit
actionBeingHandled();
427 m_currentView
->setShowHiddenFiles(show
);
430 void DolphinViewActionHandler::slotShowHiddenFilesChanged()
432 QAction
* showHiddenFilesAction
= m_actionCollection
->action("show_hidden_files");
433 showHiddenFilesAction
->setChecked(m_currentView
->showHiddenFiles());
437 KToggleAction
* DolphinViewActionHandler::iconsModeAction()
439 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>("icons");
440 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
441 iconsView
->setToolTip(i18nc("@info", "Icons view mode"));
442 iconsView
->setShortcut(Qt::CTRL
| Qt::Key_1
);
443 iconsView
->setIcon(KIcon("view-list-icons"));
444 iconsView
->setData(QVariant::fromValue(DolphinView::IconsView
));
448 KToggleAction
* DolphinViewActionHandler::detailsModeAction()
450 KToggleAction
* detailsView
= m_actionCollection
->add
<KToggleAction
>("details");
451 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
452 detailsView
->setToolTip(i18nc("@info", "Details view mode"));
453 detailsView
->setShortcut(Qt::CTRL
| Qt::Key_2
);
454 detailsView
->setIcon(KIcon("view-list-details"));
455 detailsView
->setData(QVariant::fromValue(DolphinView::DetailsView
));
459 KToggleAction
* DolphinViewActionHandler::columnsModeAction()
461 KToggleAction
* columnView
= m_actionCollection
->add
<KToggleAction
>("columns");
462 columnView
->setText(i18nc("@action:inmenu View Mode", "Columns"));
463 columnView
->setToolTip(i18nc("@info", "Columns view mode"));
464 columnView
->setShortcut(Qt::CTRL
| Qt::Key_3
);
465 columnView
->setIcon(KIcon("view-file-columns"));
466 columnView
->setData(QVariant::fromValue(DolphinView::ColumnView
));
470 void DolphinViewActionHandler::slotSortingChanged(DolphinView::Sorting sorting
)
473 if (sorting
== DolphinView::SortByName
) {
474 action
= m_actionCollection
->action("sort_by_name");
476 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
477 const KFileItemDelegate::InformationList infoKeys
= infoAccessor
.keys();
478 foreach (const KFileItemDelegate::Information info
, infoKeys
) {
479 if (sorting
== infoAccessor
.sorting(info
)) {
480 const QString name
= infoAccessor
.actionCollectionName(info
, AdditionalInfoAccessor::SortByType
);
481 action
= m_actionCollection
->action(name
);
488 action
->setChecked(true);
490 QAction
* sortByMenu
= m_actionCollection
->action("sort");
491 sortByMenu
->setIcon(KIcon(action
->icon()));
495 void DolphinViewActionHandler::slotZoomLevelChanged(int level
)
497 QAction
* zoomInAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomIn
));
499 zoomInAction
->setEnabled(level
< ZoomLevelInfo::maximumLevel());
502 QAction
* zoomOutAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomOut
));
504 zoomOutAction
->setEnabled(level
> ZoomLevelInfo::minimumLevel());
508 void DolphinViewActionHandler::slotSortTriggered(QAction
* action
)
510 const DolphinView::Sorting sorting
= action
->data().value
<DolphinView::Sorting
>();
511 m_currentView
->setSorting(sorting
);
514 void DolphinViewActionHandler::slotAdjustViewProperties()
516 emit
actionBeingHandled();
517 QPointer
<ViewPropertiesDialog
> dialog
= new ViewPropertiesDialog(m_currentView
);
522 void DolphinViewActionHandler::slotProperties()
524 KPropertiesDialog
* dialog
= 0;
525 const KFileItemList list
= m_currentView
->selectedItems();
526 if (list
.isEmpty()) {
527 const KUrl url
= m_currentView
->url();
528 dialog
= new KPropertiesDialog(url
, m_currentView
);
530 dialog
= new KPropertiesDialog(list
, m_currentView
);
533 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
536 dialog
->activateWindow();