]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewactionhandler.cpp
Forwardport 773570:
[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
22 #include "dolphinview.h"
23
24 #include <konq_operations.h>
25
26 #include <kaction.h>
27 #include <kactioncollection.h>
28 #include <klocale.h>
29 #include <ktoggleaction.h>
30
31 DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection* collection, QObject* parent)
32 : QObject(parent),
33 m_actionCollection(collection),
34 m_currentView(0)
35 {
36 Q_ASSERT(m_actionCollection);
37 createActions();
38 }
39
40 void DolphinViewActionHandler::setCurrentView(DolphinView* view)
41 {
42 Q_ASSERT(view);
43
44 if (m_currentView)
45 disconnect(m_currentView, 0, this, 0);
46
47 m_currentView = view;
48
49 connect(view, SIGNAL(showPreviewChanged()),
50 this, SLOT(slotShowPreviewChanged()));
51 connect(view, SIGNAL(sortOrderChanged(Qt::SortOrder)),
52 this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
53 connect(view, SIGNAL(additionalInfoChanged()),
54 this, SLOT(slotAdditionalInfoChanged()));
55 connect(view, SIGNAL(categorizedSortingChanged()),
56 this, SLOT(slotCategorizedSortingChanged()));
57 connect(view, SIGNAL(showHiddenFilesChanged()),
58 this, SLOT(slotShowHiddenFilesChanged()));
59 }
60
61 void DolphinViewActionHandler::createActions()
62 {
63 // This action doesn't appear in the GUI, it's for the shortcut only.
64 // KNewMenu takes care of the GUI stuff.
65 KAction* newDirAction = m_actionCollection->addAction("create_dir");
66 newDirAction->setText(i18n("Create Folder..."));
67 newDirAction->setShortcut(Qt::Key_F10);
68 connect(newDirAction, SIGNAL(triggered()), SLOT(slotCreateDir()));
69
70 // Edit menu
71
72 KAction* rename = m_actionCollection->addAction("rename");
73 rename->setText(i18nc("@action:inmenu File", "Rename..."));
74 rename->setShortcut(Qt::Key_F2);
75 connect(rename, SIGNAL(triggered()), this, SLOT(slotRename()));
76
77 KAction* moveToTrash = m_actionCollection->addAction("move_to_trash");
78 moveToTrash->setText(i18nc("@action:inmenu File", "Move to Trash"));
79 moveToTrash->setIcon(KIcon("user-trash"));
80 moveToTrash->setShortcut(QKeySequence::Delete);
81 connect(moveToTrash, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)),
82 this, SLOT(slotTrashActivated(Qt::MouseButtons, Qt::KeyboardModifiers)));
83
84 KAction* deleteAction = m_actionCollection->addAction("delete");
85 deleteAction->setIcon(KIcon("edit-delete"));
86 deleteAction->setText(i18nc("@action:inmenu File", "Delete"));
87 deleteAction->setShortcut(Qt::SHIFT | Qt::Key_Delete);
88 connect(deleteAction, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
89
90 // View menu
91
92 KStandardAction::zoomIn(this,
93 SLOT(zoomIn()),
94 m_actionCollection);
95
96 KStandardAction::zoomOut(this,
97 SLOT(zoomOut()),
98 m_actionCollection);
99
100 KToggleAction* showPreview = m_actionCollection->add<KToggleAction>("show_preview");
101 showPreview->setText(i18nc("@action:intoolbar", "Preview"));
102 showPreview->setIcon(KIcon("view-preview"));
103 connect(showPreview, SIGNAL(triggered(bool)), this, SLOT(togglePreview(bool)));
104
105 KToggleAction* sortDescending = m_actionCollection->add<KToggleAction>("descending");
106 sortDescending->setText(i18nc("@action:inmenu Sort", "Descending"));
107 connect(sortDescending, SIGNAL(triggered()), this, SLOT(toggleSortOrder()));
108
109 QActionGroup* showInformationActionGroup = createAdditionalInformationActionGroup();
110 connect(showInformationActionGroup, SIGNAL(triggered(QAction*)), this, SLOT(toggleAdditionalInfo(QAction*)));
111
112 KToggleAction* showInGroups = m_actionCollection->add<KToggleAction>("show_in_groups");
113 showInGroups->setText(i18nc("@action:inmenu View", "Show in Groups"));
114 connect(showInGroups, SIGNAL(triggered(bool)), this, SLOT(toggleSortCategorization(bool)));
115
116 KToggleAction* showHiddenFiles = m_actionCollection->add<KToggleAction>("show_hidden_files");
117 showHiddenFiles->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
118 showHiddenFiles->setShortcut(Qt::ALT | Qt::Key_Period);
119 connect(showHiddenFiles, SIGNAL(triggered(bool)), this, SLOT(toggleShowHiddenFiles(bool)));
120
121 }
122
123 QActionGroup* DolphinViewActionHandler::createAdditionalInformationActionGroup()
124 {
125 QActionGroup* showInformationGroup = new QActionGroup(m_actionCollection);
126 showInformationGroup->setExclusive(false);
127
128 KToggleAction* showSizeInfo = m_actionCollection->add<KToggleAction>("show_size_info");
129 showSizeInfo->setText(i18nc("@action:inmenu Additional information", "Size"));
130 showSizeInfo->setData(KFileItemDelegate::Size);
131 showSizeInfo->setActionGroup(showInformationGroup);
132
133 KToggleAction* showDateInfo = m_actionCollection->add<KToggleAction>("show_date_info");
134 showDateInfo->setText(i18nc("@action:inmenu Additional information", "Date"));
135 showDateInfo->setData(KFileItemDelegate::ModificationTime);
136 showDateInfo->setActionGroup(showInformationGroup);
137
138 KToggleAction* showPermissionsInfo = m_actionCollection->add<KToggleAction>("show_permissions_info");
139 showPermissionsInfo->setText(i18nc("@action:inmenu Additional information", "Permissions"));
140 showPermissionsInfo->setData(KFileItemDelegate::Permissions);
141 showPermissionsInfo->setActionGroup(showInformationGroup);
142
143 KToggleAction* showOwnerInfo = m_actionCollection->add<KToggleAction>("show_owner_info");
144 showOwnerInfo->setText(i18nc("@action:inmenu Additional information", "Owner"));
145 showOwnerInfo->setData(KFileItemDelegate::Owner);
146 showOwnerInfo->setActionGroup(showInformationGroup);
147
148 KToggleAction* showGroupInfo = m_actionCollection->add<KToggleAction>("show_group_info");
149 showGroupInfo->setText(i18nc("@action:inmenu Additional information", "Group"));
150 showGroupInfo->setData(KFileItemDelegate::OwnerAndGroup);
151 showGroupInfo->setActionGroup(showInformationGroup);
152
153 KToggleAction* showMimeInfo = m_actionCollection->add<KToggleAction>("show_mime_info");
154 showMimeInfo->setText(i18nc("@action:inmenu Additional information", "Type"));
155 showMimeInfo->setData(KFileItemDelegate::FriendlyMimeType);
156 showMimeInfo->setActionGroup(showInformationGroup);
157
158 return showInformationGroup;
159 }
160
161 void DolphinViewActionHandler::slotCreateDir()
162 {
163 Q_ASSERT(m_currentView);
164 KonqOperations::newDir(m_currentView, m_currentView->url());
165 }
166
167 void DolphinViewActionHandler::slotRename()
168 {
169 emit actionBeingHandled();
170 m_currentView->renameSelectedItems();
171 }
172
173 void DolphinViewActionHandler::slotTrashActivated(Qt::MouseButtons, Qt::KeyboardModifiers modifiers)
174 {
175 emit actionBeingHandled();
176 // Note: kde3's konq_mainwindow.cpp used to check
177 // reason == KAction::PopupMenuActivation && ...
178 // but this isn't supported anymore
179 if (modifiers & Qt::ShiftModifier)
180 m_currentView->deleteSelectedItems();
181 else
182 m_currentView->trashSelectedItems();
183 }
184
185 void DolphinViewActionHandler::slotDeleteItems()
186 {
187 emit actionBeingHandled();
188 m_currentView->deleteSelectedItems();
189 }
190
191 void DolphinViewActionHandler::togglePreview(bool show)
192 {
193 emit actionBeingHandled();
194 m_currentView->setShowPreview(show);
195 }
196
197 void DolphinViewActionHandler::slotShowPreviewChanged()
198 {
199 // It is not enough to update the 'Show Preview' action, also
200 // the 'Zoom In' and 'Zoom Out' actions must be adapted.
201 updateViewActions();
202 }
203
204 void DolphinViewActionHandler::updateViewActions()
205 {
206 QAction* zoomInAction = m_actionCollection->action(KStandardAction::stdName(KStandardAction::ZoomIn));
207 if (zoomInAction != 0) {
208 zoomInAction->setEnabled(m_currentView->isZoomInPossible());
209 }
210
211 QAction* zoomOutAction = m_actionCollection->action(KStandardAction::stdName(KStandardAction::ZoomOut));
212 if (zoomOutAction != 0) {
213 zoomOutAction->setEnabled(m_currentView->isZoomOutPossible());
214 }
215
216 QAction* showPreviewAction = m_actionCollection->action("show_preview");
217 showPreviewAction->setChecked(m_currentView->showPreview());
218
219 slotSortOrderChanged(m_currentView->sortOrder());
220 slotAdditionalInfoChanged();
221 slotCategorizedSortingChanged();
222
223 QAction* showHiddenFilesAction = m_actionCollection->action("show_hidden_files");
224 showHiddenFilesAction->setChecked(m_currentView->showHiddenFiles());
225
226 }
227
228 void DolphinViewActionHandler::zoomIn()
229 {
230 m_currentView->zoomIn();
231 updateViewActions();
232 }
233
234 void DolphinViewActionHandler::zoomOut()
235 {
236 m_currentView->zoomOut();
237 updateViewActions();
238 }
239
240 void DolphinViewActionHandler::toggleSortOrder()
241 {
242 m_currentView->toggleSortOrder();
243 }
244
245 void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order)
246 {
247 QAction* descending = m_actionCollection->action("descending");
248 const bool sortDescending = (order == Qt::DescendingOrder);
249 descending->setChecked(sortDescending);
250 }
251
252 void DolphinViewActionHandler::toggleAdditionalInfo(QAction* action)
253 {
254 emit actionBeingHandled();
255 m_currentView->toggleAdditionalInfo(action);
256 }
257
258 void DolphinViewActionHandler::slotAdditionalInfoChanged()
259 {
260 m_currentView->updateAdditionalInfoActions(m_actionCollection);
261 }
262
263 void DolphinViewActionHandler::toggleSortCategorization(bool categorizedSorting)
264 {
265 m_currentView->setCategorizedSorting(categorizedSorting);
266 }
267
268 void DolphinViewActionHandler::slotCategorizedSortingChanged()
269 {
270 QAction* showInGroupsAction = m_actionCollection->action("show_in_groups");
271 showInGroupsAction->setChecked(m_currentView->categorizedSorting());
272 showInGroupsAction->setEnabled(m_currentView->supportsCategorizedSorting());
273 }
274
275 void DolphinViewActionHandler::toggleShowHiddenFiles(bool show)
276 {
277 emit actionBeingHandled();
278 m_currentView->setShowHiddenFiles(show);
279 }
280
281 void DolphinViewActionHandler::slotShowHiddenFilesChanged()
282 {
283 QAction* showHiddenFilesAction = m_actionCollection->action("show_hidden_files");
284 showHiddenFilesAction->setChecked(m_currentView->showHiddenFiles());
285 }
286