]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinviewactionhandler.cpp
Remove RolesInfoAccessor
[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 "settings/viewpropertiesdialog.h"
23 #include "views/dolphinview.h"
24 #include "views/zoomlevelinfo.h"
25 #include <konq_operations.h>
26
27 #include <KAction>
28 #include <KActionCollection>
29 #include <KActionMenu>
30 #include <KFileItemDelegate>
31 #include <kitemviews/kfileitemmodel.h>
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 QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
220 foreach (const KFileItemModel::RoleInfo& info, rolesInfo) {
221 if (info.role == "name") {
222 // It should not be possible to hide the "name" role
223 continue;
224 }
225
226 const QString name = QLatin1String("show_") + info.role;
227 KToggleAction* action = m_actionCollection->add<KToggleAction>(name);
228 action->setText(info.translation);
229 action->setData(info.role);
230 action->setActionGroup(additionalInfoGroup);
231 }
232
233 return additionalInfoGroup;
234 }
235
236 QActionGroup* DolphinViewActionHandler::createSortByActionGroup()
237 {
238 QActionGroup* sortByActionGroup = new QActionGroup(m_actionCollection);
239 sortByActionGroup->setExclusive(true);
240
241 const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
242 foreach (const KFileItemModel::RoleInfo& info, rolesInfo) {
243 const QString name = QLatin1String("sort_by_") + info.role;
244 KToggleAction* action = m_actionCollection->add<KToggleAction>(name);
245 action->setText(info.translation);
246 action->setData(info.role);
247 sortByActionGroup->addAction(action);
248 }
249
250 return sortByActionGroup;
251 }
252
253 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction* action)
254 {
255 const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
256 m_currentView->setMode(mode);
257
258 QAction* viewModeMenu = m_actionCollection->action("view_mode");
259 viewModeMenu->setIcon(KIcon(action->icon()));
260 }
261
262 void DolphinViewActionHandler::slotRename()
263 {
264 emit actionBeingHandled();
265 m_currentView->renameSelectedItems();
266 }
267
268 void DolphinViewActionHandler::slotTrashActivated(Qt::MouseButtons, Qt::KeyboardModifiers modifiers)
269 {
270 emit actionBeingHandled();
271 // Note: kde3's konq_mainwindow.cpp used to check
272 // reason == KAction::PopupMenuActivation && ...
273 // but this isn't supported anymore
274 if (modifiers & Qt::ShiftModifier) {
275 m_currentView->deleteSelectedItems();
276 } else {
277 m_currentView->trashSelectedItems();
278 }
279 }
280
281 void DolphinViewActionHandler::slotDeleteItems()
282 {
283 emit actionBeingHandled();
284 m_currentView->deleteSelectedItems();
285 }
286
287 void DolphinViewActionHandler::togglePreview(bool show)
288 {
289 emit actionBeingHandled();
290 m_currentView->setPreviewsShown(show);
291 }
292
293 void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown)
294 {
295 Q_UNUSED(shown);
296 // It is not enough to update the 'Show Preview' action, also
297 // the 'Zoom In' and 'Zoom Out' actions must be adapted.
298 updateViewActions();
299 }
300
301 QString DolphinViewActionHandler::currentViewModeActionName() const
302 {
303 switch (m_currentView->mode()) {
304 case DolphinView::IconsView:
305 return "icons";
306 case DolphinView::DetailsView:
307 return "details";
308 case DolphinView::CompactView:
309 return "compact";
310 default:
311 Q_ASSERT(false);
312 break;
313 }
314 return QString(); // can't happen
315 }
316
317 KActionCollection* DolphinViewActionHandler::actionCollection()
318 {
319 return m_actionCollection;
320 }
321
322 void DolphinViewActionHandler::updateViewActions()
323 {
324 QAction* viewModeAction = m_actionCollection->action(currentViewModeActionName());
325 if (viewModeAction) {
326 viewModeAction->setChecked(true);
327
328 QAction* viewModeMenu = m_actionCollection->action("view_mode");
329 viewModeMenu->setIcon(KIcon(viewModeAction->icon()));
330 }
331
332 QAction* showPreviewAction = m_actionCollection->action("show_preview");
333 showPreviewAction->setChecked(m_currentView->previewsShown());
334
335 slotSortOrderChanged(m_currentView->sortOrder());
336 slotSortFoldersFirstChanged(m_currentView->sortFoldersFirst());
337 slotVisibleRolesChanged(m_currentView->visibleRoles(), QList<QByteArray>());
338 slotGroupedSortingChanged(m_currentView->groupedSorting());
339 slotSortRoleChanged(m_currentView->sortRole());
340 slotZoomLevelChanged(m_currentView->zoomLevel(), -1);
341
342 QAction* showHiddenFilesAction = m_actionCollection->action("show_hidden_files");
343 showHiddenFilesAction->setChecked(m_currentView->hiddenFilesShown());
344 }
345
346 void DolphinViewActionHandler::zoomIn()
347 {
348 const int level = m_currentView->zoomLevel();
349 m_currentView->setZoomLevel(level + 1);
350 updateViewActions();
351 }
352
353 void DolphinViewActionHandler::zoomOut()
354 {
355 const int level = m_currentView->zoomLevel();
356 m_currentView->setZoomLevel(level - 1);
357 updateViewActions();
358 }
359
360 void DolphinViewActionHandler::toggleSortOrder()
361 {
362 const Qt::SortOrder order = (m_currentView->sortOrder() == Qt::AscendingOrder) ?
363 Qt::DescendingOrder :
364 Qt::AscendingOrder;
365 m_currentView->setSortOrder(order);
366 }
367
368 void DolphinViewActionHandler::toggleSortFoldersFirst()
369 {
370 const bool sortFirst = m_currentView->sortFoldersFirst();
371 m_currentView->setSortFoldersFirst(!sortFirst);
372 }
373
374 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order)
375 {
376 QAction* descending = m_actionCollection->action("descending");
377 const bool sortDescending = (order == Qt::DescendingOrder);
378 descending->setChecked(sortDescending);
379 }
380
381 void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst)
382 {
383 m_actionCollection->action("folders_first")->setChecked(foldersFirst);
384 }
385
386 void DolphinViewActionHandler::toggleAdditionalInfo(QAction* action)
387 {
388 emit actionBeingHandled();
389
390 const QByteArray toggledRole = action->data().toByteArray();
391
392 QList<QByteArray> roles = m_currentView->visibleRoles();
393
394 const bool show = action->isChecked();
395
396 const int index = roles.indexOf(toggledRole);
397 const bool containsInfo = (index >= 0);
398 if (show && !containsInfo) {
399 roles.append(toggledRole);
400 m_currentView->setVisibleRoles(roles);
401 } else if (!show && containsInfo) {
402 roles.removeAt(index);
403 m_currentView->setVisibleRoles(roles);
404 Q_ASSERT(roles.indexOf(toggledRole) < 0);
405 }
406 }
407
408 void DolphinViewActionHandler::slotVisibleRolesChanged(const QList<QByteArray>& current,
409 const QList<QByteArray>& previous)
410 {
411 Q_UNUSED(previous);
412
413 const QSet<QByteArray> checkedRoles = current.toSet();
414 const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
415 foreach (const KFileItemModel::RoleInfo& info, rolesInfo) {
416 const QString name = QLatin1String("show_") + info.role;
417 QAction* action = m_actionCollection->action(name);
418 if (action) {
419 action->setChecked(checkedRoles.contains(info.role));
420 }
421 }
422 }
423
424 void DolphinViewActionHandler::toggleGroupedSorting(bool grouped)
425 {
426 m_currentView->setGroupedSorting(grouped);
427 }
428
429 void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting)
430 {
431 QAction* showInGroupsAction = m_actionCollection->action("show_in_groups");
432 showInGroupsAction->setChecked(groupedSorting);
433 }
434
435 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show)
436 {
437 emit actionBeingHandled();
438 m_currentView->setHiddenFilesShown(show);
439 }
440
441 void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown)
442 {
443 QAction* showHiddenFilesAction = m_actionCollection->action("show_hidden_files");
444 showHiddenFilesAction->setChecked(shown);
445 }
446
447 KToggleAction* DolphinViewActionHandler::iconsModeAction()
448 {
449 KToggleAction* iconsView = m_actionCollection->add<KToggleAction>("icons");
450 iconsView->setText(i18nc("@action:inmenu View Mode", "Icons"));
451 iconsView->setToolTip(i18nc("@info", "Icons view mode"));
452 iconsView->setShortcut(Qt::CTRL | Qt::Key_1);
453 iconsView->setIcon(KIcon("view-list-icons"));
454 iconsView->setData(QVariant::fromValue(DolphinView::IconsView));
455 return iconsView;
456 }
457
458 KToggleAction* DolphinViewActionHandler::compactModeAction()
459 {
460 KToggleAction* iconsView = m_actionCollection->add<KToggleAction>("compact");
461 iconsView->setText(i18nc("@action:inmenu View Mode", "Compact"));
462 iconsView->setToolTip(i18nc("@info", "Compact view mode"));
463 iconsView->setShortcut(Qt::CTRL | Qt::Key_2);
464 iconsView->setIcon(KIcon("view-list-details")); // TODO: discuss with Oxygen-team the wrong (?) name
465 iconsView->setData(QVariant::fromValue(DolphinView::CompactView));
466 return iconsView;
467 }
468
469 KToggleAction* DolphinViewActionHandler::detailsModeAction()
470 {
471 KToggleAction* detailsView = m_actionCollection->add<KToggleAction>("details");
472 detailsView->setText(i18nc("@action:inmenu View Mode", "Details"));
473 detailsView->setToolTip(i18nc("@info", "Details view mode"));
474 detailsView->setShortcut(Qt::CTRL | Qt::Key_3);
475 detailsView->setIcon(KIcon("view-list-tree"));
476 detailsView->setData(QVariant::fromValue(DolphinView::DetailsView));
477 return detailsView;
478 }
479
480 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray& role)
481 {
482 const QString name = QLatin1String("sort_by_") + role;
483 QAction* action = m_actionCollection->action(name);
484 if (action) {
485 action->setChecked(true);
486
487 QAction* sortByMenu = m_actionCollection->action("sort");
488 sortByMenu->setIcon(KIcon(action->icon()));
489 }
490 }
491
492 void DolphinViewActionHandler::slotZoomLevelChanged(int current, int previous)
493 {
494 Q_UNUSED(previous);
495
496 QAction* zoomInAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomIn));
497 if (zoomInAction) {
498 zoomInAction->setEnabled(current < ZoomLevelInfo::maximumLevel());
499 }
500
501 QAction* zoomOutAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomOut));
502 if (zoomOutAction) {
503 zoomOutAction->setEnabled(current > ZoomLevelInfo::minimumLevel());
504 }
505 }
506
507 void DolphinViewActionHandler::slotSortTriggered(QAction* action)
508 {
509 const QByteArray role = action->data().toByteArray();
510 m_currentView->setSortRole(role);
511 }
512
513 void DolphinViewActionHandler::slotAdjustViewProperties()
514 {
515 emit actionBeingHandled();
516 QPointer<ViewPropertiesDialog> dialog = new ViewPropertiesDialog(m_currentView);
517 dialog->exec();
518 delete dialog;
519 }
520
521 void DolphinViewActionHandler::slotProperties()
522 {
523 KPropertiesDialog* dialog = 0;
524 const KFileItemList list = m_currentView->selectedItems();
525 if (list.isEmpty()) {
526 const KUrl url = m_currentView->url();
527 dialog = new KPropertiesDialog(url, m_currentView);
528 } else {
529 dialog = new KPropertiesDialog(list, m_currentView);
530 }
531
532 dialog->setAttribute(Qt::WA_DeleteOnClose);
533 dialog->show();
534 dialog->raise();
535 dialog->activateWindow();
536 }