]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinviewactionhandler.cpp
First step to introduce dynamic roles
[dolphin.git] / src / views / 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
22 #include "rolesaccessor.h"
23 #include "settings/viewpropertiesdialog.h"
24 #include "views/dolphinview.h"
25 #include "views/zoomlevelinfo.h"
26 #include <konq_operations.h>
27
28 #include <KAction>
29 #include <KActionCollection>
30 #include <KActionMenu>
31 #include <KFileItemDelegate>
32 #include <KLocale>
33 #include <KNewFileMenu>
34 #include <KSelectAction>
35 #include <KToggleAction>
36 #include <KRun>
37 #include <KPropertiesDialog>
38
39 #include <KDebug>
40
41 DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection* collection, QObject* parent)
42 : QObject(parent),
43 m_actionCollection(collection),
44 m_currentView(0)
45 {
46 Q_ASSERT(m_actionCollection);
47 createActions();
48 }
49
50 void DolphinViewActionHandler::setCurrentView(DolphinView* view)
51 {
52 Q_ASSERT(view);
53
54 if (m_currentView) {
55 disconnect(m_currentView, 0, this, 0);
56 }
57
58 m_currentView = view;
59
60 connect(view, SIGNAL(modeChanged(DolphinView::Mode,DolphinView::Mode)),
61 this, SLOT(updateViewActions()));
62 connect(view, SIGNAL(previewsShownChanged(bool)),
63 this, SLOT(slotPreviewsShownChanged(bool)));
64 connect(view, SIGNAL(sortOrderChanged(Qt::SortOrder)),
65 this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
66 connect(view, SIGNAL(sortFoldersFirstChanged(bool)),
67 this, SLOT(slotSortFoldersFirstChanged(bool)));
68 connect(view, SIGNAL(visibleRolesChanged(QList<QByteArray>,QList<QByteArray>)),
69 this, SLOT(slotVisibleRolesChanged(QList<QByteArray>,QList<QByteArray>)));
70 connect(view, SIGNAL(groupedSortingChanged(bool)),
71 this, SLOT(slotGroupedSortingChanged(bool)));
72 connect(view, SIGNAL(hiddenFilesShownChanged(bool)),
73 this, SLOT(slotHiddenFilesShownChanged(bool)));
74 connect(view, SIGNAL(sortRoleChanged(QByteArray)),
75 this, SLOT(slotSortRoleChanged(QByteArray)));
76 connect(view, SIGNAL(zoomLevelChanged(int,int)),
77 this, SLOT(slotZoomLevelChanged(int,int)));
78 }
79
80 DolphinView* DolphinViewActionHandler::currentView()
81 {
82 return m_currentView;
83 }
84
85 void DolphinViewActionHandler::createActions()
86 {
87 // This action doesn't appear in the GUI, it's for the shortcut only.
88 // KNewFileMenu takes care of the GUI stuff.
89 KAction* newDirAction = m_actionCollection->addAction("create_dir");
90 newDirAction->setText(i18nc("@action", "Create Folder..."));
91 newDirAction->setShortcut(Qt::Key_F10);
92 newDirAction->setIcon(KIcon("folder-new"));
93 connect(newDirAction, SIGNAL(triggered()), this, SIGNAL(createDirectory()));
94
95 // File menu
96
97 KAction* rename = m_actionCollection->addAction("rename");
98 rename->setText(i18nc("@action:inmenu File", "Rename..."));
99 rename->setShortcut(Qt::Key_F2);
100 rename->setIcon(KIcon("edit-rename"));
101 connect(rename, SIGNAL(triggered()), this, SLOT(slotRename()));
102
103 KAction* moveToTrash = m_actionCollection->addAction("move_to_trash");
104 moveToTrash->setText(i18nc("@action:inmenu File", "Move to Trash"));
105 moveToTrash->setIcon(KIcon("user-trash"));
106 moveToTrash->setShortcut(QKeySequence::Delete);
107 connect(moveToTrash, SIGNAL(triggered(Qt::MouseButtons,Qt::KeyboardModifiers)),
108 this, SLOT(slotTrashActivated(Qt::MouseButtons,Qt::KeyboardModifiers)));
109
110 KAction* deleteAction = m_actionCollection->addAction("delete");
111 deleteAction->setIcon(KIcon("edit-delete"));
112 deleteAction->setText(i18nc("@action:inmenu File", "Delete"));
113 deleteAction->setShortcut(Qt::SHIFT | Qt::Key_Delete);
114 connect(deleteAction, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
115
116 // This action is useful for being enabled when "move_to_trash" should be
117 // disabled and "delete" is enabled (e.g. non-local files), so that Key_Del
118 // can be used for deleting the file (#76016). It needs to be a separate action
119 // so that the Edit menu isn't affected.
120 KAction* deleteWithTrashShortcut = m_actionCollection->addAction("delete_shortcut");
121 // The descriptive text is just for the shortcuts editor.
122 deleteWithTrashShortcut->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
123 deleteWithTrashShortcut->setShortcut(QKeySequence::Delete);
124 deleteWithTrashShortcut->setEnabled(false);
125 connect(deleteWithTrashShortcut, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
126
127 KAction *propertiesAction = m_actionCollection->addAction( "properties" );
128 // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
129 propertiesAction->setText( i18nc("@action:inmenu File", "Properties") );
130 propertiesAction->setIcon(KIcon("document-properties"));
131 propertiesAction->setShortcut(Qt::ALT | Qt::Key_Return);
132 connect(propertiesAction, SIGNAL(triggered()), SLOT(slotProperties()));
133
134 // View menu
135 KToggleAction* iconsAction = iconsModeAction();
136 KToggleAction* compactAction = compactModeAction();
137 KToggleAction* detailsAction = detailsModeAction();
138
139 KSelectAction* viewModeActions = m_actionCollection->add<KSelectAction>("view_mode");
140 viewModeActions->setText(i18nc("@action:intoolbar", "View Mode"));
141 viewModeActions->addAction(iconsAction);
142 viewModeActions->addAction(compactAction);
143 viewModeActions->addAction(detailsAction);
144 viewModeActions->setToolBarMode(KSelectAction::MenuMode);
145 connect(viewModeActions, SIGNAL(triggered(QAction*)), this, SLOT(slotViewModeActionTriggered(QAction*)));
146
147 KStandardAction::zoomIn(this,
148 SLOT(zoomIn()),
149 m_actionCollection);
150
151 KStandardAction::zoomOut(this,
152 SLOT(zoomOut()),
153 m_actionCollection);
154
155 KToggleAction* showPreview = m_actionCollection->add<KToggleAction>("show_preview");
156 showPreview->setText(i18nc("@action:intoolbar", "Preview"));
157 showPreview->setToolTip(i18nc("@info", "Show preview of files and folders"));
158 showPreview->setIcon(KIcon("view-preview"));
159 connect(showPreview, SIGNAL(triggered(bool)), this, SLOT(togglePreview(bool)));
160
161 KToggleAction* sortDescending = m_actionCollection->add<KToggleAction>("descending");
162 sortDescending->setText(i18nc("@action:inmenu Sort", "Descending"));
163 connect(sortDescending, SIGNAL(triggered()), this, SLOT(toggleSortOrder()));
164
165 KToggleAction* sortFoldersFirst = m_actionCollection->add<KToggleAction>("folders_first");
166 sortFoldersFirst->setText(i18nc("@action:inmenu Sort", "Folders First"));
167 connect(sortFoldersFirst, SIGNAL(triggered()), this, SLOT(toggleSortFoldersFirst()));
168
169 // View -> Sort By
170 QActionGroup* sortByActionGroup = createSortByActionGroup();
171 connect(sortByActionGroup, SIGNAL(triggered(QAction*)), this, SLOT(slotSortTriggered(QAction*)));
172
173 KActionMenu* sortByActionMenu = m_actionCollection->add<KActionMenu>("sort");
174 sortByActionMenu->setText(i18nc("@action:inmenu View", "Sort By"));
175 sortByActionMenu->setDelayed(false);
176
177 foreach (QAction* action, sortByActionGroup->actions()) {
178 sortByActionMenu->addAction(action);
179 }
180 sortByActionMenu->addSeparator();
181 sortByActionMenu->addAction(sortDescending);
182 sortByActionMenu->addAction(sortFoldersFirst);
183
184 // View -> Additional Information
185 QActionGroup* additionalInfoGroup = createAdditionalInformationActionGroup();
186 connect(additionalInfoGroup, SIGNAL(triggered(QAction*)), this, SLOT(toggleAdditionalInfo(QAction*)));
187
188 KActionMenu* additionalInfoMenu = m_actionCollection->add<KActionMenu>("additional_info");
189 additionalInfoMenu->setText(i18nc("@action:inmenu View", "Additional Information"));
190 additionalInfoMenu->setDelayed(false);
191 foreach (QAction* action, additionalInfoGroup->actions()) {
192 additionalInfoMenu->addAction(action);
193 }
194
195 KToggleAction* showInGroups = m_actionCollection->add<KToggleAction>("show_in_groups");
196 showInGroups->setIcon(KIcon("view-group"));
197 showInGroups->setText(i18nc("@action:inmenu View", "Show in Groups"));
198 connect(showInGroups, SIGNAL(triggered(bool)), this, SLOT(toggleGroupedSorting(bool)));
199
200 KToggleAction* showHiddenFiles = m_actionCollection->add<KToggleAction>("show_hidden_files");
201 showHiddenFiles->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
202 showHiddenFiles->setShortcuts(QList<QKeySequence>() << Qt::ALT + Qt::Key_Period << Qt::Key_F8);
203 connect(showHiddenFiles, SIGNAL(triggered(bool)), this, SLOT(toggleShowHiddenFiles(bool)));
204
205 KAction* adjustViewProps = m_actionCollection->addAction("view_properties");
206 adjustViewProps->setText(i18nc("@action:inmenu View", "Adjust View Properties..."));
207 connect(adjustViewProps, SIGNAL(triggered()), this, SLOT(slotAdjustViewProperties()));
208 }
209
210 QActionGroup* DolphinViewActionHandler::createAdditionalInformationActionGroup()
211 {
212 QActionGroup* additionalInfoGroup = new QActionGroup(m_actionCollection);
213 additionalInfoGroup->setExclusive(false);
214
215 KActionMenu* showInformationMenu = m_actionCollection->add<KActionMenu>("additional_info");
216 showInformationMenu->setText(i18nc("@action:inmenu View", "Additional Information"));
217 showInformationMenu->setDelayed(false);
218
219 const RolesAccessor& rolesAccessor = RolesAccessor::instance();
220
221 const QList<QByteArray> roles = rolesAccessor.roles();
222 foreach (const QByteArray& role, roles) {
223 if (role == "name") {
224 // It should not be possible to hide the "name" role
225 continue;
226 }
227
228 const QString name = QLatin1String("show_") + role;
229 KToggleAction* action = m_actionCollection->add<KToggleAction>(name);
230 action->setText(rolesAccessor.translation(role));
231 action->setData(role);
232 action->setActionGroup(additionalInfoGroup);
233 }
234
235 return additionalInfoGroup;
236 }
237
238 QActionGroup* DolphinViewActionHandler::createSortByActionGroup()
239 {
240 QActionGroup* sortByActionGroup = new QActionGroup(m_actionCollection);
241 sortByActionGroup->setExclusive(true);
242
243 const RolesAccessor& rolesAccessor = RolesAccessor::instance();
244 const QList<QByteArray> roles = rolesAccessor.roles();
245 foreach (const QByteArray& role, roles) {
246 const QString name = QLatin1String("sort_by_") + role;
247 KToggleAction* action = m_actionCollection->add<KToggleAction>(name);
248 action->setText(rolesAccessor.translation(role));
249 action->setData(role);
250 sortByActionGroup->addAction(action);
251 }
252
253 return sortByActionGroup;
254 }
255
256 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction* action)
257 {
258 const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
259 m_currentView->setMode(mode);
260
261 QAction* viewModeMenu = m_actionCollection->action("view_mode");
262 viewModeMenu->setIcon(KIcon(action->icon()));
263 }
264
265 void DolphinViewActionHandler::slotRename()
266 {
267 emit actionBeingHandled();
268 m_currentView->renameSelectedItems();
269 }
270
271 void DolphinViewActionHandler::slotTrashActivated(Qt::MouseButtons, Qt::KeyboardModifiers modifiers)
272 {
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();
279 } else {
280 m_currentView->trashSelectedItems();
281 }
282 }
283
284 void DolphinViewActionHandler::slotDeleteItems()
285 {
286 emit actionBeingHandled();
287 m_currentView->deleteSelectedItems();
288 }
289
290 void DolphinViewActionHandler::togglePreview(bool show)
291 {
292 emit actionBeingHandled();
293 m_currentView->setPreviewsShown(show);
294 }
295
296 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown)
297 {
298 Q_UNUSED(shown);
299 // It is not enough to update the 'Show Preview' action, also
300 // the 'Zoom In' and 'Zoom Out' actions must be adapted.
301 updateViewActions();
302 }
303
304 QString DolphinViewActionHandler::currentViewModeActionName() const
305 {
306 switch (m_currentView->mode()) {
307 case DolphinView::IconsView:
308 return "icons";
309 case DolphinView::DetailsView:
310 return "details";
311 case DolphinView::CompactView:
312 return "compact";
313 default:
314 Q_ASSERT(false);
315 break;
316 }
317 return QString(); // can't happen
318 }
319
320 KActionCollection* DolphinViewActionHandler::actionCollection()
321 {
322 return m_actionCollection;
323 }
324
325 void DolphinViewActionHandler::updateViewActions()
326 {
327 QAction* viewModeAction = m_actionCollection->action(currentViewModeActionName());
328 if (viewModeAction) {
329 viewModeAction->setChecked(true);
330
331 QAction* viewModeMenu = m_actionCollection->action("view_mode");
332 viewModeMenu->setIcon(KIcon(viewModeAction->icon()));
333 }
334
335 QAction* showPreviewAction = m_actionCollection->action("show_preview");
336 showPreviewAction->setChecked(m_currentView->previewsShown());
337
338 slotSortOrderChanged(m_currentView->sortOrder());
339 slotSortFoldersFirstChanged(m_currentView->sortFoldersFirst());
340 slotVisibleRolesChanged(m_currentView->visibleRoles(), QList<QByteArray>());
341 slotGroupedSortingChanged(m_currentView->groupedSorting());
342 slotSortRoleChanged(m_currentView->sortRole());
343 slotZoomLevelChanged(m_currentView->zoomLevel(), -1);
344
345 QAction* showHiddenFilesAction = m_actionCollection->action("show_hidden_files");
346 showHiddenFilesAction->setChecked(m_currentView->hiddenFilesShown());
347 }
348
349 void DolphinViewActionHandler::zoomIn()
350 {
351 const int level = m_currentView->zoomLevel();
352 m_currentView->setZoomLevel(level + 1);
353 updateViewActions();
354 }
355
356 void DolphinViewActionHandler::zoomOut()
357 {
358 const int level = m_currentView->zoomLevel();
359 m_currentView->setZoomLevel(level - 1);
360 updateViewActions();
361 }
362
363 void DolphinViewActionHandler::toggleSortOrder()
364 {
365 const Qt::SortOrder order = (m_currentView->sortOrder() == Qt::AscendingOrder) ?
366 Qt::DescendingOrder :
367 Qt::AscendingOrder;
368 m_currentView->setSortOrder(order);
369 }
370
371 void DolphinViewActionHandler::toggleSortFoldersFirst()
372 {
373 const bool sortFirst = m_currentView->sortFoldersFirst();
374 m_currentView->setSortFoldersFirst(!sortFirst);
375 }
376
377 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order)
378 {
379 QAction* descending = m_actionCollection->action("descending");
380 const bool sortDescending = (order == Qt::DescendingOrder);
381 descending->setChecked(sortDescending);
382 }
383
384 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst)
385 {
386 m_actionCollection->action("folders_first")->setChecked(foldersFirst);
387 }
388
389 void DolphinViewActionHandler::toggleAdditionalInfo(QAction* action)
390 {
391 emit actionBeingHandled();
392
393 const QByteArray toggledRole = action->data().toByteArray();
394
395 QList<QByteArray> roles = m_currentView->visibleRoles();
396
397 const bool show = action->isChecked();
398
399 const int index = roles.indexOf(toggledRole);
400 const bool containsInfo = (index >= 0);
401 if (show && !containsInfo) {
402 roles.append(toggledRole);
403 m_currentView->setVisibleRoles(roles);
404 } else if (!show && containsInfo) {
405 roles.removeAt(index);
406 m_currentView->setVisibleRoles(roles);
407 Q_ASSERT(roles.indexOf(toggledRole) < 0);
408 }
409 }
410
411 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList<QByteArray>& current,
412 const QList<QByteArray>& previous)
413 {
414 Q_UNUSED(previous);
415
416 const RolesAccessor& rolesAccessor = RolesAccessor::instance();
417
418 const QSet<QByteArray> checkedRoles = current.toSet();
419 const QList<QByteArray> roles = rolesAccessor.roles();
420
421 foreach (const QByteArray& role, roles) {
422 const QString name = QLatin1String("show_") + role;
423 QAction* action = m_actionCollection->action(name);
424 if (action) {
425 action->setChecked(checkedRoles.contains(role));
426 }
427 }
428 }
429
430 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped)
431 {
432 m_currentView->setGroupedSorting(grouped);
433 }
434
435 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting)
436 {
437 QAction* showInGroupsAction = m_actionCollection->action("show_in_groups");
438 showInGroupsAction->setChecked(groupedSorting);
439 }
440
441 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show)
442 {
443 emit actionBeingHandled();
444 m_currentView->setHiddenFilesShown(show);
445 }
446
447 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown)
448 {
449 QAction* showHiddenFilesAction = m_actionCollection->action("show_hidden_files");
450 showHiddenFilesAction->setChecked(shown);
451 }
452
453 KToggleAction* DolphinViewActionHandler::iconsModeAction()
454 {
455 KToggleAction* iconsView = m_actionCollection->add<KToggleAction>("icons");
456 iconsView->setText(i18nc("@action:inmenu View Mode", "Icons"));
457 iconsView->setToolTip(i18nc("@info", "Icons view mode"));
458 iconsView->setShortcut(Qt::CTRL | Qt::Key_1);
459 iconsView->setIcon(KIcon("view-list-icons"));
460 iconsView->setData(QVariant::fromValue(DolphinView::IconsView));
461 return iconsView;
462 }
463
464 KToggleAction* DolphinViewActionHandler::compactModeAction()
465 {
466 KToggleAction* iconsView = m_actionCollection->add<KToggleAction>("compact");
467 iconsView->setText(i18nc("@action:inmenu View Mode", "Compact"));
468 iconsView->setToolTip(i18nc("@info", "Compact view mode"));
469 iconsView->setShortcut(Qt::CTRL | Qt::Key_2);
470 iconsView->setIcon(KIcon("view-list-details")); // TODO: discuss with Oxygen-team the wrong (?) name
471 iconsView->setData(QVariant::fromValue(DolphinView::CompactView));
472 return iconsView;
473 }
474
475 KToggleAction* DolphinViewActionHandler::detailsModeAction()
476 {
477 KToggleAction* detailsView = m_actionCollection->add<KToggleAction>("details");
478 detailsView->setText(i18nc("@action:inmenu View Mode", "Details"));
479 detailsView->setToolTip(i18nc("@info", "Details view mode"));
480 detailsView->setShortcut(Qt::CTRL | Qt::Key_3);
481 detailsView->setIcon(KIcon("view-list-tree"));
482 detailsView->setData(QVariant::fromValue(DolphinView::DetailsView));
483 return detailsView;
484 }
485
486 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray& role)
487 {
488 const QString name = QLatin1String("sort_by_") + role;
489 QAction* action = m_actionCollection->action(name);
490 if (action) {
491 action->setChecked(true);
492
493 QAction* sortByMenu = m_actionCollection->action("sort");
494 sortByMenu->setIcon(KIcon(action->icon()));
495 }
496 }
497
498 void DolphinViewActionHandler::slotZoomLevelChanged(int current, int previous)
499 {
500 Q_UNUSED(previous);
501
502 QAction* zoomInAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomIn));
503 if (zoomInAction) {
504 zoomInAction->setEnabled(current < ZoomLevelInfo::maximumLevel());
505 }
506
507 QAction* zoomOutAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomOut));
508 if (zoomOutAction) {
509 zoomOutAction->setEnabled(current > ZoomLevelInfo::minimumLevel());
510 }
511 }
512
513 void DolphinViewActionHandler::slotSortTriggered(QAction* action)
514 {
515 const QByteArray role = action->data().toByteArray();
516 m_currentView->setSortRole(role);
517 }
518
519 void DolphinViewActionHandler::slotAdjustViewProperties()
520 {
521 emit actionBeingHandled();
522 QPointer<ViewPropertiesDialog> dialog = new ViewPropertiesDialog(m_currentView);
523 dialog->exec();
524 delete dialog;
525 }
526
527 void DolphinViewActionHandler::slotProperties()
528 {
529 KPropertiesDialog* dialog = 0;
530 const KFileItemList list = m_currentView->selectedItems();
531 if (list.isEmpty()) {
532 const KUrl url = m_currentView->url();
533 dialog = new KPropertiesDialog(url, m_currentView);
534 } else {
535 dialog = new KPropertiesDialog(list, m_currentView);
536 }
537
538 dialog->setAttribute(Qt::WA_DeleteOnClose);
539 dialog->show();
540 dialog->raise();
541 dialog->activateWindow();
542 }