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.h>
30 #include <kactionmenu.h>
31 #include <kfileitemdelegate.h>
34 #include <kselectaction.h>
35 #include <ktoggleaction.h>
37 #include <kpropertiesdialog.h>
40 DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection
* collection
, QObject
* parent
)
42 m_actionCollection(collection
),
45 Q_ASSERT(m_actionCollection
);
49 void DolphinViewActionHandler::setCurrentView(DolphinView
* view
)
54 disconnect(m_currentView
, 0, this, 0);
58 connect(view
, SIGNAL(modeChanged()),
59 this, SLOT(updateViewActions()));
60 connect(view
, SIGNAL(showPreviewChanged()),
61 this, SLOT(slotShowPreviewChanged()));
62 connect(view
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
63 this, SLOT(slotSortOrderChanged(Qt::SortOrder
)));
64 connect(view
, SIGNAL(sortFoldersFirstChanged(bool)),
65 this, SLOT(slotSortFoldersFirstChanged(bool)));
66 connect(view
, SIGNAL(additionalInfoChanged()),
67 this, SLOT(slotAdditionalInfoChanged()));
68 connect(view
, SIGNAL(categorizedSortingChanged()),
69 this, SLOT(slotCategorizedSortingChanged()));
70 connect(view
, SIGNAL(showHiddenFilesChanged()),
71 this, SLOT(slotShowHiddenFilesChanged()));
72 connect(view
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
73 this, SLOT(slotSortingChanged(DolphinView::Sorting
)));
74 connect(view
, SIGNAL(zoomLevelChanged(int)),
75 this, SLOT(slotZoomLevelChanged(int)));
78 DolphinView
* DolphinViewActionHandler::currentView()
83 void DolphinViewActionHandler::createActions()
85 // This action doesn't appear in the GUI, it's for the shortcut only.
86 // KNewFileMenu takes care of the GUI stuff.
87 KAction
* newDirAction
= m_actionCollection
->addAction("create_dir");
88 newDirAction
->setText(i18nc("@action", "Create Folder..."));
89 newDirAction
->setShortcut(Qt::Key_F10
);
90 newDirAction
->setIcon(KIcon("folder-new"));
91 connect(newDirAction
, SIGNAL(triggered()), this, SIGNAL(createDirectory()));
95 KAction
* rename
= m_actionCollection
->addAction("rename");
96 rename
->setText(i18nc("@action:inmenu File", "Rename..."));
97 rename
->setShortcut(Qt::Key_F2
);
98 rename
->setIcon(KIcon("edit-rename"));
99 connect(rename
, SIGNAL(triggered()), this, SLOT(slotRename()));
101 KAction
* moveToTrash
= m_actionCollection
->addAction("move_to_trash");
102 moveToTrash
->setText(i18nc("@action:inmenu File", "Move to Trash"));
103 moveToTrash
->setIcon(KIcon("user-trash"));
104 moveToTrash
->setShortcut(QKeySequence::Delete
);
105 connect(moveToTrash
, SIGNAL(triggered(Qt::MouseButtons
, Qt::KeyboardModifiers
)),
106 this, SLOT(slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers
)));
108 KAction
* deleteAction
= m_actionCollection
->addAction("delete");
109 deleteAction
->setIcon(KIcon("edit-delete"));
110 deleteAction
->setText(i18nc("@action:inmenu File", "Delete"));
111 deleteAction
->setShortcut(Qt::SHIFT
| Qt::Key_Delete
);
112 connect(deleteAction
, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
114 // This action is useful for being enabled when "move_to_trash" should be
115 // disabled and "delete" is enabled (e.g. non-local files), so that Key_Del
116 // can be used for deleting the file (#76016). It needs to be a separate action
117 // so that the Edit menu isn't affected.
118 KAction
* deleteWithTrashShortcut
= m_actionCollection
->addAction("delete_shortcut");
119 // The descriptive text is just for the shortcuts editor.
120 deleteWithTrashShortcut
->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
121 deleteWithTrashShortcut
->setShortcut(QKeySequence::Delete
);
122 deleteWithTrashShortcut
->setEnabled(false);
123 connect(deleteWithTrashShortcut
, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
125 KAction
*propertiesAction
= m_actionCollection
->addAction( "properties" );
126 // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
127 propertiesAction
->setText( i18nc("@action:inmenu File", "Properties") );
128 propertiesAction
->setIcon(KIcon("document-properties"));
129 propertiesAction
->setShortcut(Qt::ALT
| Qt::Key_Return
);
130 connect(propertiesAction
, SIGNAL(triggered()), SLOT(slotProperties()));
133 KToggleAction
* iconsAction
= iconsModeAction();
134 KToggleAction
* detailsAction
= detailsModeAction();
135 KToggleAction
* columnsAction
= columnsModeAction();
137 KSelectAction
* viewModeActions
= m_actionCollection
->add
<KSelectAction
>("view_mode");
138 viewModeActions
->setText(i18nc("@action:intoolbar", "View Mode"));
139 viewModeActions
->addAction(iconsAction
);
140 viewModeActions
->addAction(detailsAction
);
141 viewModeActions
->addAction(columnsAction
);
142 viewModeActions
->setToolBarMode(KSelectAction::MenuMode
);
143 connect(viewModeActions
, SIGNAL(triggered(QAction
*)), this, SLOT(slotViewModeActionTriggered(QAction
*)));
145 KStandardAction::zoomIn(this,
149 KStandardAction::zoomOut(this,
153 KToggleAction
* showPreview
= m_actionCollection
->add
<KToggleAction
>("show_preview");
154 showPreview
->setText(i18nc("@action:intoolbar", "Preview"));
155 showPreview
->setToolTip(i18nc("@info", "Show preview of files and folders"));
156 showPreview
->setIcon(KIcon("view-preview"));
157 connect(showPreview
, SIGNAL(triggered(bool)), this, SLOT(togglePreview(bool)));
159 KToggleAction
* sortDescending
= m_actionCollection
->add
<KToggleAction
>("descending");
160 sortDescending
->setText(i18nc("@action:inmenu Sort", "Descending"));
161 connect(sortDescending
, SIGNAL(triggered()), this, SLOT(toggleSortOrder()));
163 KToggleAction
* sortFoldersFirst
= m_actionCollection
->add
<KToggleAction
>("folders_first");
164 sortFoldersFirst
->setText(i18nc("@action:inmenu Sort", "Folders First"));
165 connect(sortFoldersFirst
, SIGNAL(triggered()), this, SLOT(toggleSortFoldersFirst()));
168 QActionGroup
* sortByActionGroup
= createSortByActionGroup();
169 connect(sortByActionGroup
, SIGNAL(triggered(QAction
*)), this, SLOT(slotSortTriggered(QAction
*)));
171 KActionMenu
* sortByActionMenu
= m_actionCollection
->add
<KActionMenu
>("sort");
172 sortByActionMenu
->setText(i18nc("@action:inmenu View", "Sort By"));
173 sortByActionMenu
->setDelayed(false);
175 foreach (QAction
* action
, sortByActionGroup
->actions()) {
176 sortByActionMenu
->addAction(action
);
178 sortByActionMenu
->addSeparator();
179 sortByActionMenu
->addAction(sortDescending
);
180 sortByActionMenu
->addAction(sortFoldersFirst
);
182 // View -> Additional Information
183 QActionGroup
* additionalInfoGroup
= createAdditionalInformationActionGroup();
184 connect(additionalInfoGroup
, SIGNAL(triggered(QAction
*)), this, SLOT(toggleAdditionalInfo(QAction
*)));
186 KActionMenu
* additionalInfoMenu
= m_actionCollection
->add
<KActionMenu
>("additional_info");
187 additionalInfoMenu
->setText(i18nc("@action:inmenu View", "Additional Information"));
188 additionalInfoMenu
->setDelayed(false);
189 foreach (QAction
* action
, additionalInfoGroup
->actions()) {
190 additionalInfoMenu
->addAction(action
);
193 KToggleAction
* showInGroups
= m_actionCollection
->add
<KToggleAction
>("show_in_groups");
194 showInGroups
->setText(i18nc("@action:inmenu View", "Show in Groups"));
195 connect(showInGroups
, SIGNAL(triggered(bool)), this, SLOT(toggleSortCategorization(bool)));
197 KToggleAction
* showHiddenFiles
= m_actionCollection
->add
<KToggleAction
>("show_hidden_files");
198 showHiddenFiles
->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
199 showHiddenFiles
->setShortcuts(QList
<QKeySequence
>() << Qt::ALT
+ Qt::Key_Period
<< Qt::Key_F8
);
200 connect(showHiddenFiles
, SIGNAL(triggered(bool)), this, SLOT(toggleShowHiddenFiles(bool)));
202 KAction
* adjustViewProps
= m_actionCollection
->addAction("view_properties");
203 adjustViewProps
->setText(i18nc("@action:inmenu View", "Adjust View Properties..."));
204 connect(adjustViewProps
, SIGNAL(triggered()), this, SLOT(slotAdjustViewProperties()));
207 QActionGroup
* DolphinViewActionHandler::createAdditionalInformationActionGroup()
209 QActionGroup
* additionalInfoGroup
= new QActionGroup(m_actionCollection
);
210 additionalInfoGroup
->setExclusive(false);
212 KActionMenu
* showInformationMenu
= m_actionCollection
->add
<KActionMenu
>("additional_info");
213 showInformationMenu
->setText(i18nc("@action:inmenu View", "Additional Information"));
214 showInformationMenu
->setDelayed(false);
216 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
218 const KFileItemDelegate::InformationList infoKeys
= infoAccessor
.keys();
219 foreach (KFileItemDelegate::Information info
, infoKeys
) {
220 const QString name
= infoAccessor
.actionCollectionName(info
, AdditionalInfoAccessor::AdditionalInfoType
);
221 KToggleAction
* action
= m_actionCollection
->add
<KToggleAction
>(name
);
222 action
->setText(infoAccessor
.translation(info
));
223 action
->setData(info
);
224 action
->setActionGroup(additionalInfoGroup
);
227 return additionalInfoGroup
;
230 Q_DECLARE_METATYPE(DolphinView::Sorting
)
232 QActionGroup
* DolphinViewActionHandler::createSortByActionGroup()
234 QActionGroup
* sortByActionGroup
= new QActionGroup(m_actionCollection
);
235 sortByActionGroup
->setExclusive(true);
237 KToggleAction
* sortByName
= m_actionCollection
->add
<KToggleAction
>("sort_by_name");
238 sortByName
->setText(i18nc("@action:inmenu Sort By", "Name"));
239 sortByName
->setData(QVariant::fromValue(DolphinView::SortByName
));
240 sortByActionGroup
->addAction(sortByName
);
242 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
243 const KFileItemDelegate::InformationList infoKeys
= infoAccessor
.keys();
244 foreach (KFileItemDelegate::Information info
, infoKeys
) {
245 const QString name
= infoAccessor
.actionCollectionName(info
, AdditionalInfoAccessor::SortByType
);
246 KToggleAction
* action
= m_actionCollection
->add
<KToggleAction
>(name
);
247 action
->setText(infoAccessor
.translation(info
));
248 const DolphinView::Sorting sorting
= infoAccessor
.sorting(info
);
249 action
->setData(QVariant::fromValue(sorting
));
250 sortByActionGroup
->addAction(action
);
253 return sortByActionGroup
;
256 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction
* action
)
258 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
259 m_currentView
->setMode(mode
);
261 QAction
* viewModeMenu
= m_actionCollection
->action("view_mode");
262 viewModeMenu
->setIcon(KIcon(action
->icon()));
265 void DolphinViewActionHandler::slotRename()
267 emit
actionBeingHandled();
268 m_currentView
->renameSelectedItems();
271 void DolphinViewActionHandler::slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers modifiers
)
273 emit
actionBeingHandled();
274 // Note: kde3's konq_mainwindow.cpp used to check
275 // reason == KAction::PopupMenuActivation && ...
276 // but this isn't supported anymore
277 if (modifiers
& Qt::ShiftModifier
) {
278 m_currentView
->deleteSelectedItems();
280 m_currentView
->trashSelectedItems();
284 void DolphinViewActionHandler::slotDeleteItems()
286 emit
actionBeingHandled();
287 m_currentView
->deleteSelectedItems();
290 void DolphinViewActionHandler::togglePreview(bool show
)
292 emit
actionBeingHandled();
293 m_currentView
->setShowPreview(show
);
296 void DolphinViewActionHandler::slotShowPreviewChanged()
298 // It is not enough to update the 'Show Preview' action, also
299 // the 'Zoom In' and 'Zoom Out' actions must be adapted.
303 QString
DolphinViewActionHandler::currentViewModeActionName() const
305 switch (m_currentView
->mode()) {
306 case DolphinView::IconsView
:
308 case DolphinView::DetailsView
:
310 case DolphinView::ColumnView
:
313 return QString(); // can't happen
316 KActionCollection
* DolphinViewActionHandler::actionCollection()
318 return m_actionCollection
;
321 void DolphinViewActionHandler::updateViewActions()
323 QAction
* viewModeAction
= m_actionCollection
->action(currentViewModeActionName());
324 if (viewModeAction
!= 0) {
325 viewModeAction
->setChecked(true);
327 QAction
* viewModeMenu
= m_actionCollection
->action("view_mode");
328 viewModeMenu
->setIcon(KIcon(viewModeAction
->icon()));
331 QAction
* showPreviewAction
= m_actionCollection
->action("show_preview");
332 showPreviewAction
->setChecked(m_currentView
->showPreview());
334 slotSortOrderChanged(m_currentView
->sortOrder());
335 slotSortFoldersFirstChanged(m_currentView
->sortFoldersFirst());
336 slotAdditionalInfoChanged();
337 slotCategorizedSortingChanged();
338 slotSortingChanged(m_currentView
->sorting());
339 slotZoomLevelChanged(m_currentView
->zoomLevel());
341 QAction
* showHiddenFilesAction
= m_actionCollection
->action("show_hidden_files");
342 showHiddenFilesAction
->setChecked(m_currentView
->showHiddenFiles());
345 void DolphinViewActionHandler::zoomIn()
347 const int level
= m_currentView
->zoomLevel();
348 m_currentView
->setZoomLevel(level
+ 1);
352 void DolphinViewActionHandler::zoomOut()
354 const int level
= m_currentView
->zoomLevel();
355 m_currentView
->setZoomLevel(level
- 1);
359 void DolphinViewActionHandler::toggleSortOrder()
361 const Qt::SortOrder order
= (m_currentView
->sortOrder() == Qt::AscendingOrder
) ?
362 Qt::DescendingOrder
:
364 m_currentView
->setSortOrder(order
);
367 void DolphinViewActionHandler::toggleSortFoldersFirst()
369 const bool sortFirst
= m_currentView
->sortFoldersFirst();
370 m_currentView
->setSortFoldersFirst(!sortFirst
);
373 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order
)
375 QAction
* descending
= m_actionCollection
->action("descending");
376 const bool sortDescending
= (order
== Qt::DescendingOrder
);
377 descending
->setChecked(sortDescending
);
380 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst
)
382 m_actionCollection
->action("folders_first")->setChecked(foldersFirst
);
385 void DolphinViewActionHandler::toggleAdditionalInfo(QAction
* action
)
387 emit
actionBeingHandled();
389 const KFileItemDelegate::Information info
=
390 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
392 KFileItemDelegate::InformationList list
= m_currentView
->additionalInfo();
394 const bool show
= action
->isChecked();
396 const int index
= list
.indexOf(info
);
397 const bool containsInfo
= (index
>= 0);
398 if (show
&& !containsInfo
) {
400 m_currentView
->setAdditionalInfo(list
);
401 } else if (!show
&& containsInfo
) {
402 list
.removeAt(index
);
403 m_currentView
->setAdditionalInfo(list
);
404 Q_ASSERT(list
.indexOf(info
) < 0);
408 void DolphinViewActionHandler::slotAdditionalInfoChanged()
410 m_currentView
->updateAdditionalInfoActions(m_actionCollection
);
413 void DolphinViewActionHandler::toggleSortCategorization(bool categorizedSorting
)
415 m_currentView
->setCategorizedSorting(categorizedSorting
);
418 void DolphinViewActionHandler::slotCategorizedSortingChanged()
420 QAction
* showInGroupsAction
= m_actionCollection
->action("show_in_groups");
421 showInGroupsAction
->setChecked(m_currentView
->categorizedSorting());
422 showInGroupsAction
->setEnabled(m_currentView
->supportsCategorizedSorting());
425 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show
)
427 emit
actionBeingHandled();
428 m_currentView
->setShowHiddenFiles(show
);
431 void DolphinViewActionHandler::slotShowHiddenFilesChanged()
433 QAction
* showHiddenFilesAction
= m_actionCollection
->action("show_hidden_files");
434 showHiddenFilesAction
->setChecked(m_currentView
->showHiddenFiles());
438 KToggleAction
* DolphinViewActionHandler::iconsModeAction()
440 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>("icons");
441 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
442 iconsView
->setToolTip(i18nc("@info", "Icons view mode"));
443 iconsView
->setShortcut(Qt::CTRL
| Qt::Key_1
);
444 iconsView
->setIcon(KIcon("view-list-icons"));
445 iconsView
->setData(QVariant::fromValue(DolphinView::IconsView
));
449 KToggleAction
* DolphinViewActionHandler::detailsModeAction()
451 KToggleAction
* detailsView
= m_actionCollection
->add
<KToggleAction
>("details");
452 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
453 detailsView
->setToolTip(i18nc("@info", "Details view mode"));
454 detailsView
->setShortcut(Qt::CTRL
| Qt::Key_2
);
455 detailsView
->setIcon(KIcon("view-list-details"));
456 detailsView
->setData(QVariant::fromValue(DolphinView::DetailsView
));
460 KToggleAction
* DolphinViewActionHandler::columnsModeAction()
462 KToggleAction
* columnView
= m_actionCollection
->add
<KToggleAction
>("columns");
463 columnView
->setText(i18nc("@action:inmenu View Mode", "Columns"));
464 columnView
->setToolTip(i18nc("@info", "Columns view mode"));
465 columnView
->setShortcut(Qt::CTRL
| Qt::Key_3
);
466 columnView
->setIcon(KIcon("view-file-columns"));
467 columnView
->setData(QVariant::fromValue(DolphinView::ColumnView
));
471 void DolphinViewActionHandler::slotSortingChanged(DolphinView::Sorting sorting
)
474 if (sorting
== DolphinView::SortByName
) {
475 action
= m_actionCollection
->action("sort_by_name");
477 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
478 const KFileItemDelegate::InformationList infoKeys
= infoAccessor
.keys();
479 foreach (const KFileItemDelegate::Information info
, infoKeys
) {
480 if (sorting
== infoAccessor
.sorting(info
)) {
481 const QString name
= infoAccessor
.actionCollectionName(info
, AdditionalInfoAccessor::SortByType
);
482 action
= m_actionCollection
->action(name
);
489 action
->setChecked(true);
491 QAction
* sortByMenu
= m_actionCollection
->action("sort");
492 sortByMenu
->setIcon(KIcon(action
->icon()));
496 void DolphinViewActionHandler::slotZoomLevelChanged(int level
)
498 QAction
* zoomInAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomIn
));
499 if (zoomInAction
!= 0) {
500 zoomInAction
->setEnabled(level
< ZoomLevelInfo::maximumLevel());
503 QAction
* zoomOutAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomOut
));
504 if (zoomOutAction
!= 0) {
505 zoomOutAction
->setEnabled(level
> ZoomLevelInfo::minimumLevel());
509 void DolphinViewActionHandler::slotSortTriggered(QAction
* action
)
511 const DolphinView::Sorting sorting
= action
->data().value
<DolphinView::Sorting
>();
512 m_currentView
->setSorting(sorting
);
515 void DolphinViewActionHandler::slotAdjustViewProperties()
517 emit
actionBeingHandled();
518 QPointer
<ViewPropertiesDialog
> dialog
= new ViewPropertiesDialog(m_currentView
);
523 void DolphinViewActionHandler::slotProperties()
525 KPropertiesDialog
* dialog
= 0;
526 const KFileItemList list
= m_currentView
->selectedItems();
527 if (list
.isEmpty()) {
528 const KUrl url
= m_currentView
->url();
529 dialog
= new KPropertiesDialog(url
, m_currentView
);
531 dialog
= new KPropertiesDialog(list
, m_currentView
);
534 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
537 dialog
->activateWindow();