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