]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontainer.cpp
Fix possible endless recursion when using the rubberband
[dolphin.git] / src / kitemviews / kitemlistcontainer.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 "kitemlistcontainer.h"
24
25 #include "kitemlistcontroller.h"
26 #include "kitemlistview.h"
27 #include "kitemmodelbase.h"
28
29 #include <QApplication>
30 #include <QGraphicsScene>
31 #include <QGraphicsView>
32 #include <QPropertyAnimation>
33 #include <QScrollBar>
34 #include <QStyle>
35
36 #include <KDebug>
37
38 class KItemListContainerViewport : public QGraphicsView
39 {
40 public:
41 KItemListContainerViewport(QGraphicsScene* scene, QWidget* parent)
42 : QGraphicsView(scene, parent)
43 {
44 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
45 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
46 setViewportMargins(0, 0, 0, 0);
47 setFrameShape(QFrame::NoFrame);
48 }
49
50 void scrollContentsBy(int dx, int dy)
51 {
52 Q_UNUSED(dx);
53 Q_UNUSED(dy);
54 // Do nothing. This prevents that e.g. the wheel-event
55 // results in a moving of the scene items.
56 }
57 };
58
59 KItemListContainer::KItemListContainer(KItemListController* controller, QWidget* parent) :
60 QAbstractScrollArea(parent),
61 m_controller(controller),
62 m_smoothScrolling(false),
63 m_smoothScrollingAnimation(0)
64 {
65 Q_ASSERT(controller);
66 controller->setParent(this);
67 initialize();
68 }
69
70 KItemListContainer::KItemListContainer(QWidget* parent) :
71 QAbstractScrollArea(parent),
72 m_controller(0),
73 m_smoothScrolling(false),
74 m_smoothScrollingAnimation(0)
75 {
76 initialize();
77 }
78
79 KItemListContainer::~KItemListContainer()
80 {
81 }
82
83 KItemListController* KItemListContainer::controller() const
84 {
85 return m_controller;
86 }
87
88 void KItemListContainer::keyPressEvent(QKeyEvent* event)
89 {
90 // TODO: We should find a better way to handle the key press events in the view.
91 // The reasons why we need this hack are:
92 // 1. Without reimplementing keyPressEvent() here, the event would not reach the QGraphicsView.
93 // 2. By default, the KItemListView does not have the keyboard focus in the QGraphicsScene, so
94 // simply sending the event to the QGraphicsView which is the KItemListContainer's viewport
95 // does not work.
96 KItemListView* view = m_controller->view();
97 if (view) {
98 QApplication::sendEvent(view, event);
99 }
100 }
101
102 void KItemListContainer::showEvent(QShowEvent* event)
103 {
104 QAbstractScrollArea::showEvent(event);
105 updateGeometries();
106 }
107
108 void KItemListContainer::resizeEvent(QResizeEvent* event)
109 {
110 QAbstractScrollArea::resizeEvent(event);
111 updateGeometries();
112 }
113
114 void KItemListContainer::scrollContentsBy(int dx, int dy)
115 {
116 KItemListView* view = m_controller->view();
117 if (!view) {
118 return;
119 }
120
121 const qreal currentOffset = view->offset();
122 qreal offsetDiff = (view->scrollOrientation() == Qt::Vertical) ? dy : dx;
123
124 const bool animRunning = (m_smoothScrollingAnimation->state() == QAbstractAnimation::Running);
125 if (animRunning) {
126 // Stopping a running animation means skipping the range from the current offset
127 // until the target offset. To prevent skipping of the range the difference
128 // is added to the new target offset.
129 const qreal targetOffset = m_smoothScrollingAnimation->endValue().toReal();
130 offsetDiff += (currentOffset - targetOffset);
131 }
132
133 const qreal newOffset = currentOffset - offsetDiff;
134
135 if (m_smoothScrolling || animRunning) {
136 m_smoothScrollingAnimation->stop();
137 m_smoothScrollingAnimation->setStartValue(currentOffset);
138 m_smoothScrollingAnimation->setEndValue(newOffset);
139 m_smoothScrollingAnimation->setEasingCurve(animRunning ? QEasingCurve::OutQuad : QEasingCurve::InOutQuad);
140 m_smoothScrollingAnimation->start();
141 view->setOffset(currentOffset);
142 } else {
143 view->setOffset(newOffset);
144 }
145 }
146
147 bool KItemListContainer::eventFilter(QObject* obj, QEvent* event)
148 {
149 Q_ASSERT(obj == horizontalScrollBar() || obj == verticalScrollBar());
150
151 // Check whether the scrollbar has been adjusted by a mouse-event
152 // triggered by the user and remember this in m_smoothScrolling.
153 // The smooth scrolling will only get active if m_smoothScrolling
154 // is true (see scrollContentsBy()).
155 const bool scrollVertical = (m_controller->view()->scrollOrientation() == Qt::Vertical);
156 const bool checkEvent = ( scrollVertical && obj == verticalScrollBar()) ||
157 (!scrollVertical && obj == horizontalScrollBar());
158 if (checkEvent) {
159 switch (event->type()) {
160 case QEvent::MouseButtonPress:
161 m_smoothScrolling = true;
162 break;
163
164 case QEvent::MouseButtonRelease:
165 m_smoothScrolling = false;
166 break;
167
168 case QEvent::Wheel:
169 wheelEvent(static_cast<QWheelEvent*>(event));
170 break;
171
172 default:
173 break;
174 }
175 }
176
177 return QAbstractScrollArea::eventFilter(obj, event);
178 }
179
180 void KItemListContainer::wheelEvent(QWheelEvent* event)
181 {
182 KItemListView* view = m_controller->view();
183 if (!view || event->orientation() != view->scrollOrientation()) {
184 return;
185 }
186
187 const int numDegrees = event->delta() / 8;
188 const int numSteps = numDegrees / 15;
189
190 const bool previous = m_smoothScrolling;
191 m_smoothScrolling = true;
192 if (view->scrollOrientation() == Qt::Vertical) {
193 const int value = verticalScrollBar()->value();
194 verticalScrollBar()->setValue(value - numSteps * view->size().height());
195 } else {
196 const int value = horizontalScrollBar()->value();
197 horizontalScrollBar()->setValue(value - numSteps * view->size().width());
198 }
199 m_smoothScrolling = previous;
200
201 event->accept();
202 }
203
204 void KItemListContainer::slotModelChanged(KItemModelBase* current, KItemModelBase* previous)
205 {
206 Q_UNUSED(current);
207 Q_UNUSED(previous);
208 }
209
210 void KItemListContainer::slotViewChanged(KItemListView* current, KItemListView* previous)
211 {
212 QGraphicsScene* scene = static_cast<QGraphicsView*>(viewport())->scene();
213 if (previous) {
214 scene->removeItem(previous);
215 disconnect(previous, SIGNAL(offsetChanged(qreal,qreal)), this, SLOT(updateScrollBars()));
216 disconnect(previous, SIGNAL(maximumOffsetChanged(qreal,qreal)), this, SLOT(updateScrollBars()));
217 disconnect(previous, SIGNAL(scrollTo(qreal)), this, SLOT(scrollTo(qreal)));
218 m_smoothScrollingAnimation->setTargetObject(0);
219 }
220 if (current) {
221 scene->addItem(current);
222 connect(current, SIGNAL(offsetChanged(qreal,qreal)), this, SLOT(updateScrollBars()));
223 connect(current, SIGNAL(maximumOffsetChanged(qreal,qreal)), this, SLOT(updateScrollBars()));
224 connect(current, SIGNAL(scrollTo(qreal)), this, SLOT(scrollTo(qreal)));
225 m_smoothScrollingAnimation->setTargetObject(current);
226 }
227 }
228
229 void KItemListContainer::slotAnimationStateChanged(QAbstractAnimation::State newState,
230 QAbstractAnimation::State oldState)
231 {
232 Q_UNUSED(oldState);
233 if (newState == QAbstractAnimation::Stopped) {
234 m_smoothScrolling = false;
235 }
236 }
237
238
239 void KItemListContainer::scrollTo(qreal offset)
240 {
241 const KItemListView* view = m_controller->view();
242 if (!view) {
243 return;
244 }
245
246 m_smoothScrolling = true;
247 QScrollBar* scrollBar = (view->scrollOrientation() == Qt::Vertical)
248 ? verticalScrollBar() : horizontalScrollBar();
249 scrollBar->setValue(offset);
250 }
251
252 void KItemListContainer::updateScrollBars()
253 {
254 const KItemListView* view = m_controller->view();
255 if (!view) {
256 return;
257 }
258
259 QScrollBar* scrollBar = 0;
260 int singleStep = 0;
261 int pageStep = 0;
262 QScrollBar* otherScrollBar = 0;
263 if (view->scrollOrientation() == Qt::Vertical) {
264 scrollBar = verticalScrollBar();
265 singleStep = view->itemSize().height();
266 pageStep = view->size().height();
267 otherScrollBar = horizontalScrollBar();
268 } else {
269 scrollBar = horizontalScrollBar();
270 singleStep = view->itemSize().width();
271 pageStep = view->size().width();
272 otherScrollBar = verticalScrollBar();
273 }
274
275 const int value = view->offset();
276 const int maximum = qMax(0, int(view->maximumOffset() - pageStep));
277 if (m_smoothScrollingAnimation->state() == QAbstractAnimation::Running) {
278 if (maximum == scrollBar->maximum()) {
279 // The value has been changed by the animation, no update
280 // of the scrollbars is required as their target state will be
281 // reached with the end of the animation.
282 return;
283 }
284
285 // The maximum has been changed which indicates that the content
286 // of the view has been changed. Stop the animation in any case and
287 // update the scrollbars immediately.
288 m_smoothScrollingAnimation->stop();
289 }
290
291 scrollBar->setSingleStep(singleStep);
292 scrollBar->setPageStep(pageStep);
293 scrollBar->setMinimum(0);
294 scrollBar->setMaximum(maximum);
295 scrollBar->setValue(value);
296
297 // Make sure that the other scroll bar is hidden
298 otherScrollBar->setMaximum(0);
299 otherScrollBar->setValue(0);
300 }
301
302 void KItemListContainer::updateGeometries()
303 {
304 QRect rect = geometry();
305
306 int widthDec = frameWidth() * 2;
307 if (verticalScrollBar()->isVisible()) {
308 widthDec += style()->pixelMetric(QStyle::PM_ScrollBarExtent);
309 }
310
311 int heightDec = frameWidth() * 2;
312 if (horizontalScrollBar()->isVisible()) {
313 heightDec += style()->pixelMetric(QStyle::PM_ScrollBarExtent);
314 }
315
316 rect.adjust(0, 0, -widthDec, -heightDec);
317
318 m_controller->view()->setGeometry(QRect(0, 0, rect.width(), rect.height()));
319
320 static_cast<KItemListContainerViewport*>(viewport())->scene()->setSceneRect(0, 0, rect.width(), rect.height());
321 static_cast<KItemListContainerViewport*>(viewport())->viewport()->setGeometry(QRect(0, 0, rect.width(), rect.height()));
322
323 updateScrollBars();
324 }
325
326 void KItemListContainer::initialize()
327 {
328 if (!m_controller) {
329 m_controller = new KItemListController(this);
330 }
331
332 connect(m_controller, SIGNAL(modelChanged(KItemModelBase*,KItemModelBase*)),
333 this, SLOT(slotModelChanged(KItemModelBase*,KItemModelBase*)));
334 connect(m_controller, SIGNAL(viewChanged(KItemListView*,KItemListView*)),
335 this, SLOT(slotViewChanged(KItemListView*,KItemListView*)));
336
337 QGraphicsView* graphicsView = new KItemListContainerViewport(new QGraphicsScene(this), this);
338 setViewport(graphicsView);
339
340 m_smoothScrollingAnimation = new QPropertyAnimation(this, "offset");
341 m_smoothScrollingAnimation->setDuration(300);
342 connect(m_smoothScrollingAnimation, SIGNAL(stateChanged(QAbstractAnimation::State,QAbstractAnimation::State)),
343 this, SLOT(slotAnimationStateChanged(QAbstractAnimation::State,QAbstractAnimation::State)));
344
345 horizontalScrollBar()->installEventFilter(this);
346 verticalScrollBar()->installEventFilter(this);
347 }
348
349 #include "kitemlistcontainer.moc"