delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / apps / dolphin / src / dolphinviewactionhandler.cpp
blob45603c28682fc59d06157f2f87ad332f5c8468f1
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 ***************************************************************************/
20 #include "dolphinviewactionhandler.h"
22 #include "settings/viewpropertiesdialog.h"
23 #include "dolphinview.h"
24 #include "zoomlevelinfo.h"
26 #include <konq_operations.h>
28 #include <kaction.h>
29 #include <kactioncollection.h>
30 #include <klocale.h>
31 #include <ktoggleaction.h>
32 #include <krun.h>
33 #include <kpropertiesdialog.h>
35 DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection* collection, QObject* parent)
36 : QObject(parent),
37 m_actionCollection(collection),
38 m_currentView(0)
40 Q_ASSERT(m_actionCollection);
41 createActions();
44 void DolphinViewActionHandler::setCurrentView(DolphinView* view)
46 Q_ASSERT(view);
48 if (m_currentView)
49 disconnect(m_currentView, 0, this, 0);
51 m_currentView = view;
53 connect(view, SIGNAL(modeChanged()),
54 this, SLOT(updateViewActions()));
55 connect(view, SIGNAL(showPreviewChanged()),
56 this, SLOT(slotShowPreviewChanged()));
57 connect(view, SIGNAL(sortOrderChanged(Qt::SortOrder)),
58 this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
59 connect(view, SIGNAL(additionalInfoChanged()),
60 this, SLOT(slotAdditionalInfoChanged()));
61 connect(view, SIGNAL(categorizedSortingChanged()),
62 this, SLOT(slotCategorizedSortingChanged()));
63 connect(view, SIGNAL(showHiddenFilesChanged()),
64 this, SLOT(slotShowHiddenFilesChanged()));
65 connect(view, SIGNAL(sortingChanged(DolphinView::Sorting)),
66 this, SLOT(slotSortingChanged(DolphinView::Sorting)));
67 connect(view, SIGNAL(zoomLevelChanged(int)),
68 this, SLOT(slotZoomLevelChanged(int)));
71 void DolphinViewActionHandler::createActions()
73 // This action doesn't appear in the GUI, it's for the shortcut only.
74 // KNewMenu takes care of the GUI stuff.
75 KAction* newDirAction = m_actionCollection->addAction("create_dir");
76 newDirAction->setText(i18nc("@action", "Create Folder..."));
77 newDirAction->setShortcut(Qt::Key_F10);
78 newDirAction->setIcon(KIcon("folder-new"));
79 connect(newDirAction, SIGNAL(triggered()), SLOT(slotCreateDir()));
81 // Edit menu
83 KAction* rename = m_actionCollection->addAction("rename");
84 rename->setText(i18nc("@action:inmenu File", "Rename..."));
85 rename->setShortcut(Qt::Key_F2);
86 connect(rename, SIGNAL(triggered()), this, SLOT(slotRename()));
88 KAction* moveToTrash = m_actionCollection->addAction("move_to_trash");
89 moveToTrash->setText(i18nc("@action:inmenu File", "Move to Trash"));
90 moveToTrash->setIcon(KIcon("user-trash"));
91 moveToTrash->setShortcut(QKeySequence::Delete);
92 connect(moveToTrash, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)),
93 this, SLOT(slotTrashActivated(Qt::MouseButtons, Qt::KeyboardModifiers)));
95 KAction* deleteAction = m_actionCollection->addAction("delete");
96 deleteAction->setIcon(KIcon("edit-delete"));
97 deleteAction->setText(i18nc("@action:inmenu File", "Delete"));
98 deleteAction->setShortcut(Qt::SHIFT | Qt::Key_Delete);
99 connect(deleteAction, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
101 // This action is useful for being enabled when "move_to_trash" should be
102 // disabled and "delete" is enabled (e.g. non-local files), so that Key_Del
103 // can be used for deleting the file (#76016). It needs to be a separate action
104 // so that the Edit menu isn't affected.
105 KAction* deleteWithTrashShortcut = m_actionCollection->addAction("delete_shortcut");
106 // The descriptive text is just for the shortcuts editor.
107 deleteWithTrashShortcut->setText(i18nc("@action:inmenu File", "Delete (using shortcut for Trash)"));
108 deleteWithTrashShortcut->setShortcut(QKeySequence::Delete);
109 deleteWithTrashShortcut->setEnabled(false);
110 connect(deleteWithTrashShortcut, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
112 KAction *propertiesAction = m_actionCollection->addAction( "properties" );
113 // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
114 propertiesAction->setText( i18nc("@action:inmenu File", "Properties") );
115 propertiesAction->setShortcut(Qt::ALT | Qt::Key_Return);
116 connect(propertiesAction, SIGNAL(triggered()), SLOT(slotProperties()));
118 // View menu
120 QActionGroup* viewModeActions = new QActionGroup(this);
121 viewModeActions->addAction(iconsModeAction());
122 viewModeActions->addAction(detailsModeAction());
123 viewModeActions->addAction(columnsModeAction());
124 connect(viewModeActions, SIGNAL(triggered(QAction*)), this, SLOT(slotViewModeActionTriggered(QAction*)));
126 KStandardAction::zoomIn(this,
127 SLOT(zoomIn()),
128 m_actionCollection);
130 KStandardAction::zoomOut(this,
131 SLOT(zoomOut()),
132 m_actionCollection);
134 KToggleAction* showPreview = m_actionCollection->add<KToggleAction>("show_preview");
135 showPreview->setText(i18nc("@action:intoolbar", "Preview"));
136 showPreview->setIcon(KIcon("view-preview"));
137 connect(showPreview, SIGNAL(triggered(bool)), this, SLOT(togglePreview(bool)));
139 KToggleAction* sortDescending = m_actionCollection->add<KToggleAction>("descending");
140 sortDescending->setText(i18nc("@action:inmenu Sort", "Descending"));
141 connect(sortDescending, SIGNAL(triggered()), this, SLOT(toggleSortOrder()));
143 QActionGroup* sortByActionGroup = createSortByActionGroup();
144 connect(sortByActionGroup, SIGNAL(triggered(QAction*)), this, SLOT(slotSortTriggered(QAction*)));
146 QActionGroup* showInformationActionGroup = createAdditionalInformationActionGroup();
147 connect(showInformationActionGroup, SIGNAL(triggered(QAction*)), this, SLOT(toggleAdditionalInfo(QAction*)));
149 KToggleAction* showInGroups = m_actionCollection->add<KToggleAction>("show_in_groups");
150 showInGroups->setText(i18nc("@action:inmenu View", "Show in Groups"));
151 connect(showInGroups, SIGNAL(triggered(bool)), this, SLOT(toggleSortCategorization(bool)));
153 KToggleAction* showHiddenFiles = m_actionCollection->add<KToggleAction>("show_hidden_files");
154 showHiddenFiles->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
155 showHiddenFiles->setShortcut(Qt::ALT | Qt::Key_Period);
156 connect(showHiddenFiles, SIGNAL(triggered(bool)), this, SLOT(toggleShowHiddenFiles(bool)));
158 KAction* adjustViewProps = m_actionCollection->addAction("view_properties");
159 adjustViewProps->setText(i18nc("@action:inmenu View", "Adjust View Properties..."));
160 connect(adjustViewProps, SIGNAL(triggered()), this, SLOT(slotAdjustViewProperties()));
162 // Tools menu
164 KAction* findFile = m_actionCollection->addAction("find_file");
165 findFile->setText(i18nc("@action:inmenu Tools", "Find File..."));
166 findFile->setShortcut(Qt::CTRL | Qt::Key_F);
167 findFile->setIcon(KIcon("edit-find"));
168 connect(findFile, SIGNAL(triggered()), this, SLOT(slotFindFile()));
171 QActionGroup* DolphinViewActionHandler::createAdditionalInformationActionGroup()
173 QActionGroup* showInformationGroup = new QActionGroup(m_actionCollection);
174 showInformationGroup->setExclusive(false);
176 KToggleAction* showSizeInfo = m_actionCollection->add<KToggleAction>("show_size_info");
177 showSizeInfo->setText(i18nc("@action:inmenu Additional information", "Size"));
178 showSizeInfo->setData(KFileItemDelegate::Size);
179 showSizeInfo->setActionGroup(showInformationGroup);
181 KToggleAction* showDateInfo = m_actionCollection->add<KToggleAction>("show_date_info");
182 showDateInfo->setText(i18nc("@action:inmenu Additional information", "Date"));
183 showDateInfo->setData(KFileItemDelegate::ModificationTime);
184 showDateInfo->setActionGroup(showInformationGroup);
186 KToggleAction* showPermissionsInfo = m_actionCollection->add<KToggleAction>("show_permissions_info");
187 showPermissionsInfo->setText(i18nc("@action:inmenu Additional information", "Permissions"));
188 showPermissionsInfo->setData(KFileItemDelegate::Permissions);
189 showPermissionsInfo->setActionGroup(showInformationGroup);
191 KToggleAction* showOwnerInfo = m_actionCollection->add<KToggleAction>("show_owner_info");
192 showOwnerInfo->setText(i18nc("@action:inmenu Additional information", "Owner"));
193 showOwnerInfo->setData(KFileItemDelegate::Owner);
194 showOwnerInfo->setActionGroup(showInformationGroup);
196 KToggleAction* showGroupInfo = m_actionCollection->add<KToggleAction>("show_group_info");
197 showGroupInfo->setText(i18nc("@action:inmenu Additional information", "Group"));
198 showGroupInfo->setData(KFileItemDelegate::OwnerAndGroup);
199 showGroupInfo->setActionGroup(showInformationGroup);
201 KToggleAction* showMimeInfo = m_actionCollection->add<KToggleAction>("show_mime_info");
202 showMimeInfo->setText(i18nc("@action:inmenu Additional information", "Type"));
203 showMimeInfo->setData(KFileItemDelegate::FriendlyMimeType);
204 showMimeInfo->setActionGroup(showInformationGroup);
206 return showInformationGroup;
209 Q_DECLARE_METATYPE(DolphinView::Sorting)
211 QActionGroup* DolphinViewActionHandler::createSortByActionGroup()
213 QActionGroup* sortByActionGroup = new QActionGroup(m_actionCollection);
214 sortByActionGroup->setExclusive(true);
216 KToggleAction* sortByName = m_actionCollection->add<KToggleAction>("sort_by_name");
217 sortByName->setText(i18nc("@action:inmenu Sort By", "Name"));
218 sortByName->setData(QVariant::fromValue(DolphinView::SortByName));
219 sortByActionGroup->addAction(sortByName);
221 KToggleAction* sortBySize = m_actionCollection->add<KToggleAction>("sort_by_size");
222 sortBySize->setText(i18nc("@action:inmenu Sort By", "Size"));
223 sortBySize->setData(QVariant::fromValue(DolphinView::SortBySize));
224 sortByActionGroup->addAction(sortBySize);
226 KToggleAction* sortByDate = m_actionCollection->add<KToggleAction>("sort_by_date");
227 sortByDate->setText(i18nc("@action:inmenu Sort By", "Date"));
228 sortByDate->setData(QVariant::fromValue(DolphinView::SortByDate));
229 sortByActionGroup->addAction(sortByDate);
231 KToggleAction* sortByPermissions = m_actionCollection->add<KToggleAction>("sort_by_permissions");
232 sortByPermissions->setText(i18nc("@action:inmenu Sort By", "Permissions"));
233 sortByPermissions->setData(QVariant::fromValue(DolphinView::SortByPermissions));
234 sortByActionGroup->addAction(sortByPermissions);
236 KToggleAction* sortByOwner = m_actionCollection->add<KToggleAction>("sort_by_owner");
237 sortByOwner->setText(i18nc("@action:inmenu Sort By", "Owner"));
238 sortByOwner->setData(QVariant::fromValue(DolphinView::SortByOwner));
239 sortByActionGroup->addAction(sortByOwner);
241 KToggleAction* sortByGroup = m_actionCollection->add<KToggleAction>("sort_by_group");
242 sortByGroup->setText(i18nc("@action:inmenu Sort By", "Group"));
243 sortByGroup->setData(QVariant::fromValue(DolphinView::SortByGroup));
244 sortByActionGroup->addAction(sortByGroup);
246 KToggleAction* sortByType = m_actionCollection->add<KToggleAction>("sort_by_type");
247 sortByType->setText(i18nc("@action:inmenu Sort By", "Type"));
248 sortByType->setData(QVariant::fromValue(DolphinView::SortByType));
249 sortByActionGroup->addAction(sortByType);
251 // TODO: Hid "sort by rating" and "sort by tags" as without caching the performance
252 // is too slow currently (Nepomuk will support caching in future releases).
254 // KToggleAction* sortByRating = m_actionCollection->add<KToggleAction>("sort_by_rating");
255 // sortByRating->setData(QVariant::fromValue(DolphinView::SortByRating));
256 // sortByRating->setText(i18nc("@action:inmenu Sort By", "Rating"));
257 // sortByActionGroup->addAction(sortByRating);
259 // KToggleAction* sortByTags = m_actionCollection->add<KToggleAction>("sort_by_tags");
260 // sortByTags->setData(QVariant::fromValue(DolphinView::SortByTags));
261 // sortByTags->setText(i18nc("@action:inmenu Sort By", "Tags"));
262 // sortByActionGroup->addAction(sortByTags);
264 #ifdef HAVE_NEPOMUK
265 // if (!MetaDataWidget::metaDataAvailable()) {
266 // sortByRating->setEnabled(false);
267 // sortByTags->setEnabled(false);
268 // }
269 #else
270 // sortByRating->setEnabled(false);
271 // sortByTags->setEnabled(false);
272 #endif
276 return sortByActionGroup;
279 void DolphinViewActionHandler::slotCreateDir()
281 Q_ASSERT(m_currentView);
282 KonqOperations::newDir(m_currentView, m_currentView->url());
285 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction* action)
287 const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
288 m_currentView->setMode(mode);
291 void DolphinViewActionHandler::slotRename()
293 emit actionBeingHandled();
294 m_currentView->renameSelectedItems();
297 void DolphinViewActionHandler::slotTrashActivated(Qt::MouseButtons, Qt::KeyboardModifiers modifiers)
299 emit actionBeingHandled();
300 // Note: kde3's konq_mainwindow.cpp used to check
301 // reason == KAction::PopupMenuActivation && ...
302 // but this isn't supported anymore
303 if (modifiers & Qt::ShiftModifier)
304 m_currentView->deleteSelectedItems();
305 else
306 m_currentView->trashSelectedItems();
309 void DolphinViewActionHandler::slotDeleteItems()
311 emit actionBeingHandled();
312 m_currentView->deleteSelectedItems();
315 void DolphinViewActionHandler::togglePreview(bool show)
317 emit actionBeingHandled();
318 m_currentView->setShowPreview(show);
321 void DolphinViewActionHandler::slotShowPreviewChanged()
323 // It is not enough to update the 'Show Preview' action, also
324 // the 'Zoom In' and 'Zoom Out' actions must be adapted.
325 updateViewActions();
328 QString DolphinViewActionHandler::currentViewModeActionName() const
330 switch (m_currentView->mode()) {
331 case DolphinView::IconsView:
332 return "icons";
333 case DolphinView::DetailsView:
334 return "details";
335 case DolphinView::ColumnView:
336 return "columns";
338 return QString(); // can't happen
341 void DolphinViewActionHandler::updateViewActions()
343 QAction* viewModeAction = m_actionCollection->action(currentViewModeActionName());
344 if (viewModeAction != 0) {
345 viewModeAction->setChecked(true);
348 QAction* showPreviewAction = m_actionCollection->action("show_preview");
349 showPreviewAction->setChecked(m_currentView->showPreview());
351 slotSortOrderChanged(m_currentView->sortOrder());
352 slotAdditionalInfoChanged();
353 slotCategorizedSortingChanged();
354 slotSortingChanged(m_currentView->sorting());
355 slotZoomLevelChanged(m_currentView->zoomLevel());
357 QAction* showHiddenFilesAction = m_actionCollection->action("show_hidden_files");
358 showHiddenFilesAction->setChecked(m_currentView->showHiddenFiles());
362 void DolphinViewActionHandler::zoomIn()
364 const int level = m_currentView->zoomLevel();
365 m_currentView->setZoomLevel(level + 1);
366 updateViewActions();
369 void DolphinViewActionHandler::zoomOut()
371 const int level = m_currentView->zoomLevel();
372 m_currentView->setZoomLevel(level - 1);
373 updateViewActions();
376 void DolphinViewActionHandler::toggleSortOrder()
378 m_currentView->toggleSortOrder();
381 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order)
383 QAction* descending = m_actionCollection->action("descending");
384 const bool sortDescending = (order == Qt::DescendingOrder);
385 descending->setChecked(sortDescending);
388 void DolphinViewActionHandler::toggleAdditionalInfo(QAction* action)
390 emit actionBeingHandled();
391 m_currentView->toggleAdditionalInfo(action);
394 void DolphinViewActionHandler::slotAdditionalInfoChanged()
396 m_currentView->updateAdditionalInfoActions(m_actionCollection);
399 void DolphinViewActionHandler::toggleSortCategorization(bool categorizedSorting)
401 m_currentView->setCategorizedSorting(categorizedSorting);
404 void DolphinViewActionHandler::slotCategorizedSortingChanged()
406 QAction* showInGroupsAction = m_actionCollection->action("show_in_groups");
407 showInGroupsAction->setChecked(m_currentView->categorizedSorting());
408 showInGroupsAction->setEnabled(m_currentView->supportsCategorizedSorting());
411 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show)
413 emit actionBeingHandled();
414 m_currentView->setShowHiddenFiles(show);
417 void DolphinViewActionHandler::slotShowHiddenFilesChanged()
419 QAction* showHiddenFilesAction = m_actionCollection->action("show_hidden_files");
420 showHiddenFilesAction->setChecked(m_currentView->showHiddenFiles());
424 KToggleAction* DolphinViewActionHandler::iconsModeAction()
426 KToggleAction* iconsView = m_actionCollection->add<KToggleAction>("icons");
427 iconsView->setText(i18nc("@action:inmenu View Mode", "Icons"));
428 iconsView->setShortcut(Qt::CTRL | Qt::Key_1);
429 iconsView->setIcon(KIcon("view-list-icons"));
430 iconsView->setData(QVariant::fromValue(DolphinView::IconsView));
431 return iconsView;
434 KToggleAction* DolphinViewActionHandler::detailsModeAction()
436 KToggleAction* detailsView = m_actionCollection->add<KToggleAction>("details");
437 detailsView->setText(i18nc("@action:inmenu View Mode", "Details"));
438 detailsView->setShortcut(Qt::CTRL | Qt::Key_2);
439 detailsView->setIcon(KIcon("view-list-details"));
440 detailsView->setData(QVariant::fromValue(DolphinView::DetailsView));
441 return detailsView;
444 KToggleAction* DolphinViewActionHandler::columnsModeAction()
446 KToggleAction* columnView = m_actionCollection->add<KToggleAction>("columns");
447 columnView->setText(i18nc("@action:inmenu View Mode", "Columns"));
448 columnView->setShortcut(Qt::CTRL | Qt::Key_3);
449 columnView->setIcon(KIcon("view-file-columns"));
450 columnView->setData(QVariant::fromValue(DolphinView::ColumnView));
451 return columnView;
454 void DolphinViewActionHandler::slotSortingChanged(DolphinView::Sorting sorting)
456 QAction* action = 0;
457 switch (sorting) {
458 case DolphinView::SortByName:
459 action = m_actionCollection->action("sort_by_name");
460 break;
461 case DolphinView::SortBySize:
462 action = m_actionCollection->action("sort_by_size");
463 break;
464 case DolphinView::SortByDate:
465 action = m_actionCollection->action("sort_by_date");
466 break;
467 case DolphinView::SortByPermissions:
468 action = m_actionCollection->action("sort_by_permissions");
469 break;
470 case DolphinView::SortByOwner:
471 action = m_actionCollection->action("sort_by_owner");
472 break;
473 case DolphinView::SortByGroup:
474 action = m_actionCollection->action("sort_by_group");
475 break;
476 case DolphinView::SortByType:
477 action = m_actionCollection->action("sort_by_type");
478 break;
479 #ifdef HAVE_NEPOMUK
480 case DolphinView::SortByRating:
481 action = m_actionCollection->action("sort_by_rating");
482 break;
483 case DolphinView::SortByTags:
484 action = m_actionCollection->action("sort_by_tags");
485 break;
486 #endif
487 default:
488 break;
491 if (action != 0) {
492 action->setChecked(true);
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 ViewPropertiesDialog dlg(m_currentView);
519 dlg.exec();
522 void DolphinViewActionHandler::slotFindFile()
524 KRun::run("kfind %u", m_currentView->url(), m_currentView->window());
527 void DolphinViewActionHandler::slotProperties()
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);
538 dialog->setAttribute(Qt::WA_DeleteOnClose);
539 dialog->show();
540 dialog->raise();
541 dialog->activateWindow();