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