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"
23 #include "dolphinview.h"
25 #include <konq_operations.h>
28 #include <kactioncollection.h>
30 #include <ktoggleaction.h>
32 DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection
* collection
, QObject
* parent
)
34 m_actionCollection(collection
),
37 Q_ASSERT(m_actionCollection
);
41 void DolphinViewActionHandler::setCurrentView(DolphinView
* view
)
46 disconnect(m_currentView
, 0, this, 0);
50 connect(view
, SIGNAL(modeChanged()),
51 this, SLOT(updateViewActions()));
52 connect(view
, SIGNAL(showPreviewChanged()),
53 this, SLOT(slotShowPreviewChanged()));
54 connect(view
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
55 this, SLOT(slotSortOrderChanged(Qt::SortOrder
)));
56 connect(view
, SIGNAL(additionalInfoChanged()),
57 this, SLOT(slotAdditionalInfoChanged()));
58 connect(view
, SIGNAL(categorizedSortingChanged()),
59 this, SLOT(slotCategorizedSortingChanged()));
60 connect(view
, SIGNAL(showHiddenFilesChanged()),
61 this, SLOT(slotShowHiddenFilesChanged()));
64 void DolphinViewActionHandler::createActions()
66 // This action doesn't appear in the GUI, it's for the shortcut only.
67 // KNewMenu takes care of the GUI stuff.
68 KAction
* newDirAction
= m_actionCollection
->addAction("create_dir");
69 newDirAction
->setText(i18n("Create Folder..."));
70 newDirAction
->setShortcut(Qt::Key_F10
);
71 connect(newDirAction
, SIGNAL(triggered()), SLOT(slotCreateDir()));
75 KAction
* rename
= m_actionCollection
->addAction("rename");
76 rename
->setText(i18nc("@action:inmenu File", "Rename..."));
77 rename
->setShortcut(Qt::Key_F2
);
78 connect(rename
, SIGNAL(triggered()), this, SLOT(slotRename()));
80 KAction
* moveToTrash
= m_actionCollection
->addAction("move_to_trash");
81 moveToTrash
->setText(i18nc("@action:inmenu File", "Move to Trash"));
82 moveToTrash
->setIcon(KIcon("user-trash"));
83 moveToTrash
->setShortcut(QKeySequence::Delete
);
84 connect(moveToTrash
, SIGNAL(triggered(Qt::MouseButtons
, Qt::KeyboardModifiers
)),
85 this, SLOT(slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers
)));
87 KAction
* deleteAction
= m_actionCollection
->addAction("delete");
88 deleteAction
->setIcon(KIcon("edit-delete"));
89 deleteAction
->setText(i18nc("@action:inmenu File", "Delete"));
90 deleteAction
->setShortcut(Qt::SHIFT
| Qt::Key_Delete
);
91 connect(deleteAction
, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
95 QActionGroup
* viewModeActions
= new QActionGroup(this);
96 viewModeActions
->addAction(iconsModeAction());
97 viewModeActions
->addAction(detailsModeAction());
98 viewModeActions
->addAction(columnsModeAction());
99 connect(viewModeActions
, SIGNAL(triggered(QAction
*)), this, SLOT(slotViewModeActionTriggered(QAction
*)));
101 KStandardAction::zoomIn(this,
105 KStandardAction::zoomOut(this,
109 KToggleAction
* showPreview
= m_actionCollection
->add
<KToggleAction
>("show_preview");
110 showPreview
->setText(i18nc("@action:intoolbar", "Preview"));
111 showPreview
->setIcon(KIcon("view-preview"));
112 connect(showPreview
, SIGNAL(triggered(bool)), this, SLOT(togglePreview(bool)));
114 KToggleAction
* sortDescending
= m_actionCollection
->add
<KToggleAction
>("descending");
115 sortDescending
->setText(i18nc("@action:inmenu Sort", "Descending"));
116 connect(sortDescending
, SIGNAL(triggered()), this, SLOT(toggleSortOrder()));
118 QActionGroup
* showInformationActionGroup
= createAdditionalInformationActionGroup();
119 connect(showInformationActionGroup
, SIGNAL(triggered(QAction
*)), this, SLOT(toggleAdditionalInfo(QAction
*)));
121 KToggleAction
* showInGroups
= m_actionCollection
->add
<KToggleAction
>("show_in_groups");
122 showInGroups
->setText(i18nc("@action:inmenu View", "Show in Groups"));
123 connect(showInGroups
, SIGNAL(triggered(bool)), this, SLOT(toggleSortCategorization(bool)));
125 KToggleAction
* showHiddenFiles
= m_actionCollection
->add
<KToggleAction
>("show_hidden_files");
126 showHiddenFiles
->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
127 showHiddenFiles
->setShortcut(Qt::ALT
| Qt::Key_Period
);
128 connect(showHiddenFiles
, SIGNAL(triggered(bool)), this, SLOT(toggleShowHiddenFiles(bool)));
132 QActionGroup
* DolphinViewActionHandler::createAdditionalInformationActionGroup()
134 QActionGroup
* showInformationGroup
= new QActionGroup(m_actionCollection
);
135 showInformationGroup
->setExclusive(false);
137 KToggleAction
* showSizeInfo
= m_actionCollection
->add
<KToggleAction
>("show_size_info");
138 showSizeInfo
->setText(i18nc("@action:inmenu Additional information", "Size"));
139 showSizeInfo
->setData(KFileItemDelegate::Size
);
140 showSizeInfo
->setActionGroup(showInformationGroup
);
142 KToggleAction
* showDateInfo
= m_actionCollection
->add
<KToggleAction
>("show_date_info");
143 showDateInfo
->setText(i18nc("@action:inmenu Additional information", "Date"));
144 showDateInfo
->setData(KFileItemDelegate::ModificationTime
);
145 showDateInfo
->setActionGroup(showInformationGroup
);
147 KToggleAction
* showPermissionsInfo
= m_actionCollection
->add
<KToggleAction
>("show_permissions_info");
148 showPermissionsInfo
->setText(i18nc("@action:inmenu Additional information", "Permissions"));
149 showPermissionsInfo
->setData(KFileItemDelegate::Permissions
);
150 showPermissionsInfo
->setActionGroup(showInformationGroup
);
152 KToggleAction
* showOwnerInfo
= m_actionCollection
->add
<KToggleAction
>("show_owner_info");
153 showOwnerInfo
->setText(i18nc("@action:inmenu Additional information", "Owner"));
154 showOwnerInfo
->setData(KFileItemDelegate::Owner
);
155 showOwnerInfo
->setActionGroup(showInformationGroup
);
157 KToggleAction
* showGroupInfo
= m_actionCollection
->add
<KToggleAction
>("show_group_info");
158 showGroupInfo
->setText(i18nc("@action:inmenu Additional information", "Group"));
159 showGroupInfo
->setData(KFileItemDelegate::OwnerAndGroup
);
160 showGroupInfo
->setActionGroup(showInformationGroup
);
162 KToggleAction
* showMimeInfo
= m_actionCollection
->add
<KToggleAction
>("show_mime_info");
163 showMimeInfo
->setText(i18nc("@action:inmenu Additional information", "Type"));
164 showMimeInfo
->setData(KFileItemDelegate::FriendlyMimeType
);
165 showMimeInfo
->setActionGroup(showInformationGroup
);
167 return showInformationGroup
;
170 void DolphinViewActionHandler::slotCreateDir()
172 Q_ASSERT(m_currentView
);
173 KonqOperations::newDir(m_currentView
, m_currentView
->url());
176 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction
* action
)
178 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
179 m_currentView
->setMode(mode
);
182 void DolphinViewActionHandler::slotRename()
184 emit
actionBeingHandled();
185 m_currentView
->renameSelectedItems();
188 void DolphinViewActionHandler::slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers modifiers
)
190 emit
actionBeingHandled();
191 // Note: kde3's konq_mainwindow.cpp used to check
192 // reason == KAction::PopupMenuActivation && ...
193 // but this isn't supported anymore
194 if (modifiers
& Qt::ShiftModifier
)
195 m_currentView
->deleteSelectedItems();
197 m_currentView
->trashSelectedItems();
200 void DolphinViewActionHandler::slotDeleteItems()
202 emit
actionBeingHandled();
203 m_currentView
->deleteSelectedItems();
206 void DolphinViewActionHandler::togglePreview(bool show
)
208 emit
actionBeingHandled();
209 m_currentView
->setShowPreview(show
);
212 void DolphinViewActionHandler::slotShowPreviewChanged()
214 // It is not enough to update the 'Show Preview' action, also
215 // the 'Zoom In' and 'Zoom Out' actions must be adapted.
219 QString
DolphinViewActionHandler::currentViewModeActionName() const
221 switch (m_currentView
->mode()) {
222 case DolphinView::IconsView
:
224 case DolphinView::DetailsView
:
226 case DolphinView::ColumnView
:
229 return QString(); // can't happen
232 void DolphinViewActionHandler::updateViewActions()
234 QAction
* viewModeAction
= m_actionCollection
->action(currentViewModeActionName());
235 if (viewModeAction
!= 0) {
236 viewModeAction
->setChecked(true);
239 QAction
* zoomInAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomIn
));
240 if (zoomInAction
!= 0) {
241 zoomInAction
->setEnabled(m_currentView
->isZoomInPossible());
244 QAction
* zoomOutAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::ZoomOut
));
245 if (zoomOutAction
!= 0) {
246 zoomOutAction
->setEnabled(m_currentView
->isZoomOutPossible());
249 QAction
* showPreviewAction
= m_actionCollection
->action("show_preview");
250 showPreviewAction
->setChecked(m_currentView
->showPreview());
252 slotSortOrderChanged(m_currentView
->sortOrder());
253 slotAdditionalInfoChanged();
254 slotCategorizedSortingChanged();
256 QAction
* showHiddenFilesAction
= m_actionCollection
->action("show_hidden_files");
257 showHiddenFilesAction
->setChecked(m_currentView
->showHiddenFiles());
261 void DolphinViewActionHandler::zoomIn()
263 m_currentView
->zoomIn();
267 void DolphinViewActionHandler::zoomOut()
269 m_currentView
->zoomOut();
273 void DolphinViewActionHandler::toggleSortOrder()
275 m_currentView
->toggleSortOrder();
278 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order
)
280 QAction
* descending
= m_actionCollection
->action("descending");
281 const bool sortDescending
= (order
== Qt::DescendingOrder
);
282 descending
->setChecked(sortDescending
);
285 void DolphinViewActionHandler::toggleAdditionalInfo(QAction
* action
)
287 emit
actionBeingHandled();
288 m_currentView
->toggleAdditionalInfo(action
);
291 void DolphinViewActionHandler::slotAdditionalInfoChanged()
293 m_currentView
->updateAdditionalInfoActions(m_actionCollection
);
296 void DolphinViewActionHandler::toggleSortCategorization(bool categorizedSorting
)
298 m_currentView
->setCategorizedSorting(categorizedSorting
);
301 void DolphinViewActionHandler::slotCategorizedSortingChanged()
303 QAction
* showInGroupsAction
= m_actionCollection
->action("show_in_groups");
304 showInGroupsAction
->setChecked(m_currentView
->categorizedSorting());
305 showInGroupsAction
->setEnabled(m_currentView
->supportsCategorizedSorting());
308 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show
)
310 emit
actionBeingHandled();
311 m_currentView
->setShowHiddenFiles(show
);
314 void DolphinViewActionHandler::slotShowHiddenFilesChanged()
316 QAction
* showHiddenFilesAction
= m_actionCollection
->action("show_hidden_files");
317 showHiddenFilesAction
->setChecked(m_currentView
->showHiddenFiles());
321 KToggleAction
* DolphinViewActionHandler::iconsModeAction()
323 KToggleAction
* iconsView
= m_actionCollection
->add
<KToggleAction
>("icons");
324 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
325 iconsView
->setShortcut(Qt::CTRL
| Qt::Key_1
);
326 iconsView
->setIcon(KIcon("view-list-icons"));
327 iconsView
->setData(QVariant::fromValue(DolphinView::IconsView
));
331 KToggleAction
* DolphinViewActionHandler::detailsModeAction()
333 KToggleAction
* detailsView
= m_actionCollection
->add
<KToggleAction
>("details");
334 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
335 detailsView
->setShortcut(Qt::CTRL
| Qt::Key_2
);
336 detailsView
->setIcon(KIcon("view-list-details"));
337 detailsView
->setData(QVariant::fromValue(DolphinView::DetailsView
));
341 KToggleAction
* DolphinViewActionHandler::columnsModeAction()
343 KToggleAction
* columnView
= m_actionCollection
->add
<KToggleAction
>("columns");
344 columnView
->setText(i18nc("@action:inmenu View Mode", "Columns"));
345 columnView
->setShortcut(Qt::CTRL
| Qt::Key_3
);
346 columnView
->setIcon(KIcon("view-file-columns"));
347 columnView
->setData(QVariant::fromValue(DolphinView::ColumnView
));