]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontroller.cpp
Underline the current item in KFileItemListView
[dolphin.git] / src / kitemviews / kitemlistcontroller.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
22
23 #include "kitemlistcontroller.h"
24
25 #include "kitemlistview.h"
26 #include "kitemlistselectionmanager.h"
27
28 #include <QEvent>
29 #include <QGraphicsSceneEvent>
30 #include <QTransform>
31
32 #include <KDebug>
33
34 KItemListController::KItemListController(QObject* parent) :
35 QObject(parent),
36 m_selectionBehavior(NoSelection),
37 m_model(0),
38 m_view(0),
39 m_selectionManager(new KItemListSelectionManager(this)),
40 m_pressedIndex(-1)
41 {
42 }
43
44 KItemListController::~KItemListController()
45 {
46 }
47
48 void KItemListController::setModel(KItemModelBase* model)
49 {
50 if (m_model == model) {
51 return;
52 }
53
54 KItemModelBase* oldModel = m_model;
55 m_model = model;
56
57 if (m_view) {
58 m_view->setModel(m_model);
59 }
60
61 m_selectionManager->setModel(m_model);
62
63 emit modelChanged(m_model, oldModel);
64 }
65
66 KItemModelBase* KItemListController::model() const
67 {
68 return m_model;
69 }
70
71 KItemListSelectionManager* KItemListController::selectionManager() const
72 {
73 return m_selectionManager;
74 }
75
76 void KItemListController::setView(KItemListView* view)
77 {
78 if (m_view == view) {
79 return;
80 }
81
82 KItemListView* oldView = m_view;
83 m_view = view;
84
85 if (oldView) {
86 disconnect(m_selectionManager, SIGNAL(currentChanged(int,int)), oldView, SLOT(currentChanged(int,int)));
87 }
88
89 if (m_view) {
90 m_view->setController(this);
91 m_view->setModel(m_model);
92 connect(m_selectionManager, SIGNAL(currentChanged(int,int)), m_view, SLOT(currentChanged(int,int)));
93 }
94
95 emit viewChanged(m_view, oldView);
96 }
97
98 KItemListView* KItemListController::view() const
99 {
100 return m_view;
101 }
102
103 void KItemListController::setSelectionBehavior(SelectionBehavior behavior)
104 {
105 m_selectionBehavior = behavior;
106 }
107
108 KItemListController::SelectionBehavior KItemListController::selectionBehavior() const
109 {
110 return m_selectionBehavior;
111 }
112
113 bool KItemListController::showEvent(QShowEvent* event)
114 {
115 Q_UNUSED(event);
116 return false;
117 }
118
119 bool KItemListController::hideEvent(QHideEvent* event)
120 {
121 Q_UNUSED(event);
122 return false;
123 }
124
125 bool KItemListController::keyPressEvent(QKeyEvent* event)
126 {
127 Q_UNUSED(event);
128 return false;
129 }
130
131 bool KItemListController::inputMethodEvent(QInputMethodEvent* event)
132 {
133 Q_UNUSED(event);
134 return false;
135 }
136
137 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
138 {
139 const QPointF pos = transform.map(event->pos());
140 m_pressedIndex = m_view->itemAt(pos);
141
142 m_selectionManager->setCurrentItem(m_pressedIndex);
143
144 // The anchor for the current selection is updated except for Shift+LeftButton events
145 // (the current selection is continued with the previous anchor in that case).
146 if (!(event->buttons() & Qt::LeftButton && event->modifiers() & Qt::ShiftModifier)) {
147 m_selectionManager->setAnchorItem(m_pressedIndex);
148 }
149
150 return false;
151 }
152
153 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
154 {
155 Q_UNUSED(event);
156 Q_UNUSED(transform);
157 return false;
158 }
159
160 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
161 {
162 if (m_view) {
163 const QPointF pos = transform.map(event->pos());
164 const int index = m_view->itemAt(pos);
165 if (index >= 0 && index == m_pressedIndex) {
166 bool emitItemClicked = true;
167 if (event->button() & Qt::LeftButton) {
168 if (m_view->isAboveExpansionToggle(index, pos)) {
169 emit itemExpansionToggleClicked(index);
170 emitItemClicked = false;
171 }
172 else if (event->modifiers() & Qt::ShiftModifier || event->modifiers() & Qt::ControlModifier) {
173 // The mouse click should only update the selection, not trigger the item.
174 emitItemClicked = false;
175 }
176 }
177
178 if (emitItemClicked) {
179 emit itemClicked(index, event->button());
180 }
181 }
182 }
183
184 m_pressedIndex = -1;
185 return false;
186 }
187
188 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
189 {
190 Q_UNUSED(event);
191 Q_UNUSED(transform);
192 return false;
193 }
194
195 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
196 {
197 Q_UNUSED(event);
198 Q_UNUSED(transform);
199 return false;
200 }
201
202 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
203 {
204 Q_UNUSED(event);
205 Q_UNUSED(transform);
206 return false;
207 }
208
209 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
210 {
211 Q_UNUSED(event);
212 Q_UNUSED(transform);
213 return false;
214 }
215
216 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
217 {
218 Q_UNUSED(event);
219 Q_UNUSED(transform);
220 return false;
221 }
222
223 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform)
224 {
225 Q_UNUSED(event);
226 Q_UNUSED(transform);
227 return false;
228 }
229
230 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform)
231 {
232 Q_UNUSED(event);
233 Q_UNUSED(transform);
234 return false;
235 }
236
237 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform)
238 {
239 Q_UNUSED(event);
240 Q_UNUSED(transform);
241 return false;
242 }
243
244 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent* event, const QTransform& transform)
245 {
246 Q_UNUSED(event);
247 Q_UNUSED(transform);
248 return false;
249 }
250
251 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent* event, const QTransform& transform)
252 {
253 Q_UNUSED(event);
254 Q_UNUSED(transform);
255 return false;
256 }
257
258 bool KItemListController::processEvent(QEvent* event, const QTransform& transform)
259 {
260 if (!event) {
261 return false;
262 }
263
264 switch (event->type()) {
265 // case QEvent::FocusIn:
266 // case QEvent::FocusOut:
267 // return focusEvent(static_cast<QFocusEvent*>(event));
268 case QEvent::KeyPress:
269 return keyPressEvent(static_cast<QKeyEvent*>(event));
270 case QEvent::InputMethod:
271 return inputMethodEvent(static_cast<QInputMethodEvent*>(event));
272 case QEvent::GraphicsSceneMousePress:
273 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent*>(event), QTransform());
274 case QEvent::GraphicsSceneMouseMove:
275 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent*>(event), QTransform());
276 case QEvent::GraphicsSceneMouseRelease:
277 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent*>(event), QTransform());
278 case QEvent::GraphicsSceneWheel:
279 return wheelEvent(static_cast<QGraphicsSceneWheelEvent*>(event), QTransform());
280 case QEvent::GraphicsSceneDragEnter:
281 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent*>(event), QTransform());
282 case QEvent::GraphicsSceneDragLeave:
283 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent*>(event), QTransform());
284 case QEvent::GraphicsSceneDragMove:
285 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent*>(event), QTransform());
286 case QEvent::GraphicsSceneDrop:
287 return dropEvent(static_cast<QGraphicsSceneDragDropEvent*>(event), QTransform());
288 case QEvent::GraphicsSceneHoverEnter:
289 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent*>(event), QTransform());
290 case QEvent::GraphicsSceneHoverMove:
291 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent*>(event), QTransform());
292 case QEvent::GraphicsSceneHoverLeave:
293 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent*>(event), QTransform());
294 case QEvent::GraphicsSceneResize:
295 return resizeEvent(static_cast<QGraphicsSceneResizeEvent*>(event), transform);
296 default:
297 break;
298 }
299
300 return false;
301 }
302
303 #include "kitemlistcontroller.moc"