]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewactionhandler.cpp
--deprecated
[dolphin.git] / src / dolphinviewactionhandler.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by David Faure <faure@kde.org> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "dolphinviewactionhandler.h"
21 #include <kdebug.h>
22
23 #include "dolphinview.h"
24
25 #include <konq_operations.h>
26
27 #include <kaction.h>
28 #include <kactioncollection.h>
29 #include <klocale.h>
30 #include <ktoggleaction.h>
31
32 DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection* collection, QObject* parent)
33 : QObject(parent),
34 m_actionCollection(collection),
35 m_currentView(0)
36 {
37 Q_ASSERT(m_actionCollection);
38 createActions();
39 }
40
41 void DolphinViewActionHandler::setCurrentView(DolphinView* view)
42 {
43 Q_ASSERT(view);
44
45 if (m_currentView)
46 disconnect(m_currentView, 0, this, 0);
47
48 m_currentView = view;
49
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()));
62 }
63
64 void DolphinViewActionHandler::createActions()
65 {
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()));
72
73 // Edit menu
74
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()));
79
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)));
86
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()));
92
93 // View menu
94
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*)));
100
101 KStandardAction::zoomIn(this,
102 SLOT(zoomIn()),
103 m_actionCollection);
104
105 KStandardAction::zoomOut(this,
106 SLOT(zoomOut()),
107 m_actionCollection);
108
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)));
113
114 KToggleAction* sortDescending = m_actionCollection->add<KToggleAction>("descending");
115 sortDescending->setText(i18nc("@action:inmenu Sort", "Descending"));
116 connect(sortDescending, SIGNAL(triggered()), this, SLOT(toggleSortOrder()));
117
118 QActionGroup* showInformationActionGroup = createAdditionalInformationActionGroup();
119 connect(showInformationActionGroup, SIGNAL(triggered(QAction*)), this, SLOT(toggleAdditionalInfo(QAction*)));
120
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)));
124
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)));
129
130 }
131
132 QActionGroup* DolphinViewActionHandler::createAdditionalInformationActionGroup()
133 {
134 QActionGroup* showInformationGroup = new QActionGroup(m_actionCollection);
135 showInformationGroup->setExclusive(false);
136
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);
141
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);
146
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);
151
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);
156
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);
161
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);
166
167 return showInformationGroup;
168 }
169
170 void DolphinViewActionHandler::slotCreateDir()
171 {
172 Q_ASSERT(m_currentView);
173 KonqOperations::newDir(m_currentView, m_currentView->url());
174 }
175
176 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction* action)
177 {
178 const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
179 m_currentView->setMode(mode);
180 }
181
182 void DolphinViewActionHandler::slotRename()
183 {
184 emit actionBeingHandled();
185 m_currentView->renameSelectedItems();
186 }
187
188 void DolphinViewActionHandler::slotTrashActivated(Qt::MouseButtons, Qt::KeyboardModifiers modifiers)
189 {
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();
196 else
197 m_currentView->trashSelectedItems();
198 }
199
200 void DolphinViewActionHandler::slotDeleteItems()
201 {
202 emit actionBeingHandled();
203 m_currentView->deleteSelectedItems();
204 }
205
206 void DolphinViewActionHandler::togglePreview(bool show)
207 {
208 emit actionBeingHandled();
209 m_currentView->setShowPreview(show);
210 }
211
212 void DolphinViewActionHandler::slotShowPreviewChanged()
213 {
214 // It is not enough to update the 'Show Preview' action, also
215 // the 'Zoom In' and 'Zoom Out' actions must be adapted.
216 updateViewActions();
217 }
218
219 QString DolphinViewActionHandler::currentViewModeActionName() const
220 {
221 switch (m_currentView->mode()) {
222 case DolphinView::IconsView:
223 return "icons";
224 case DolphinView::DetailsView:
225 return "details";
226 case DolphinView::ColumnView:
227 return "columns";
228 }
229 return QString(); // can't happen
230 }
231
232 void DolphinViewActionHandler::updateViewActions()
233 {
234 QAction* viewModeAction = m_actionCollection->action(currentViewModeActionName());
235 if (viewModeAction != 0) {
236 viewModeAction->setChecked(true);
237 }
238
239 QAction* zoomInAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomIn));
240 if (zoomInAction != 0) {
241 zoomInAction->setEnabled(m_currentView->isZoomInPossible());
242 }
243
244 QAction* zoomOutAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomOut));
245 if (zoomOutAction != 0) {
246 zoomOutAction->setEnabled(m_currentView->isZoomOutPossible());
247 }
248
249 QAction* showPreviewAction = m_actionCollection->action("show_preview");
250 showPreviewAction->setChecked(m_currentView->showPreview());
251
252 slotSortOrderChanged(m_currentView->sortOrder());
253 slotAdditionalInfoChanged();
254 slotCategorizedSortingChanged();
255
256 QAction* showHiddenFilesAction = m_actionCollection->action("show_hidden_files");
257 showHiddenFilesAction->setChecked(m_currentView->showHiddenFiles());
258
259 }
260
261 void DolphinViewActionHandler::zoomIn()
262 {
263 m_currentView->zoomIn();
264 updateViewActions();
265 }
266
267 void DolphinViewActionHandler::zoomOut()
268 {
269 m_currentView->zoomOut();
270 updateViewActions();
271 }
272
273 void DolphinViewActionHandler::toggleSortOrder()
274 {
275 m_currentView->toggleSortOrder();
276 }
277
278 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order)
279 {
280 QAction* descending = m_actionCollection->action("descending");
281 const bool sortDescending = (order == Qt::DescendingOrder);
282 descending->setChecked(sortDescending);
283 }
284
285 void DolphinViewActionHandler::toggleAdditionalInfo(QAction* action)
286 {
287 emit actionBeingHandled();
288 m_currentView->toggleAdditionalInfo(action);
289 }
290
291 void DolphinViewActionHandler::slotAdditionalInfoChanged()
292 {
293 m_currentView->updateAdditionalInfoActions(m_actionCollection);
294 }
295
296 void DolphinViewActionHandler::toggleSortCategorization(bool categorizedSorting)
297 {
298 m_currentView->setCategorizedSorting(categorizedSorting);
299 }
300
301 void DolphinViewActionHandler::slotCategorizedSortingChanged()
302 {
303 QAction* showInGroupsAction = m_actionCollection->action("show_in_groups");
304 showInGroupsAction->setChecked(m_currentView->categorizedSorting());
305 showInGroupsAction->setEnabled(m_currentView->supportsCategorizedSorting());
306 }
307
308 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show)
309 {
310 emit actionBeingHandled();
311 m_currentView->setShowHiddenFiles(show);
312 }
313
314 void DolphinViewActionHandler::slotShowHiddenFilesChanged()
315 {
316 QAction* showHiddenFilesAction = m_actionCollection->action("show_hidden_files");
317 showHiddenFilesAction->setChecked(m_currentView->showHiddenFiles());
318 }
319
320
321 KToggleAction* DolphinViewActionHandler::iconsModeAction()
322 {
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));
328 return iconsView;
329 }
330
331 KToggleAction* DolphinViewActionHandler::detailsModeAction()
332 {
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));
338 return detailsView;
339 }
340
341 KToggleAction* DolphinViewActionHandler::columnsModeAction()
342 {
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));
348 return columnView;
349 }