1 /////////////////////////////////////////////////////////////////////////
4 // Copyright(C) 2009 Igor Trindade Oliveira <igor.oliveira@indt.org.br>//
5 // Copyright(C) 2009 Adenilson Cavalcanti <adenilson.silva@idnt.org.br>//
7 // This library is free software; you can redistribute it and/or //
8 // modify it under the terms of the GNU Lesser General Public //
9 // License as published by the Free Software Foundation; either //
10 // version 2.1 of the License, or (at your option) any later version. //
12 // This library 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 GNU //
15 // Lesser General Public License for more details. //
17 // You should have received a copy of the GNU Lesser General Public //
18 // License along with this library; if not, write to the Free Software //
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA //
21 /////////////////////////////////////////////////////////////////////////
22 #include "kineticview.h"
23 #include "scrollbar.h"
27 #include <QGraphicsGridLayout>
29 #include <QPropertyAnimation>
33 * - fix velocity( create a new easing curve? )
34 * - kinetic velocity parameterized upon count of list items
35 * - horizontal scrolling
36 * - parameterize dimensions
37 * - ringbuffer (minimize number of items in the QGV).
40 class KineticScrollingPrivate
43 KineticScrollingPrivate(): mScrollVelocity(0), timerID(0),
44 overshoot(40), bounceFlag(0), bounceStatus( Finished
)
49 t
= QTime::currentTime();
52 unsigned int elapsed()
54 return t
.msecsTo( QTime::currentTime() );
57 void verticalScroll(int value
)
59 widget
->setPos(QPoint(0, -value
*10));
62 void horizontalScroll(int value
)
64 widget
->setPos(QPoint(-value
*10, 0));
67 /* Just for backport sake */
83 void mousePressEvent(QGraphicsSceneMouseEvent
*event
)
92 void mouseMoveEvent(QGraphicsSceneMouseEvent
*event
)
94 int temp
= event
->lastPos().y() - event
->pos().y();
101 void mouseReleaseEvent(QGraphicsSceneMouseEvent
*event
)
103 timeDelta
= elapsed();
104 int temp
= event
->lastPos().y() - event
->pos().y();
106 kin_movement
+= temp
;
108 if (timeDelta
> 200) {
109 if (kin_movement
> 0)
116 void wheelReleaseEvent(QGraphicsSceneWheelEvent
*event
)
118 timeDelta
= elapsed();
119 int temp
= event
->delta();
121 kin_movement
+= temp
;
123 /* Just for backport sake */
125 unsigned int timeDelta
;
126 qreal scrollVelocity
;
130 qreal mScrollVelocity
;
131 enum { None
, Up
, Down
};
132 enum BounceStatus
{ Running
, Finished
};
133 int timerID
, overshoot
, cposition
, direction
, minimalPos
, maximumPos
;
135 BounceStatus bounceStatus
;
137 QGraphicsWidget
*widget
;
138 QGraphicsWidget
*scrollingWidget
;
144 KineticView::KineticView(QGraphicsWidget
*parent
)
145 : QGraphicsWidget(parent
)
147 d
= new KineticScrollingPrivate
;
148 setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Expanding
);
149 d
->scrollingWidget
= new QGraphicsWidget(this);
150 d
->scrollingWidget
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Expanding
);
151 d
->scrollingWidget
->setFlag(QGraphicsItem::ItemClipsChildrenToShape
, true);
152 layout
= new QGraphicsGridLayout(this);
153 layout
->addItem(d
->scrollingWidget
, 0, 0);
155 verticalScrollbar
= new ScrollBar(this);
156 connect(verticalScrollbar
, SIGNAL(valueChanged(int)), this, SLOT(setVerticalScrollValue(int)));
157 layout
->addItem(verticalScrollbar
, 0, 1);
159 setAcceptedMouseButtons(Qt::LeftButton
);
162 KineticView::~KineticView()
166 delete scrollAnimation
;
169 void KineticView::adjustScrollBar()
171 verticalScrollbar
->setMaximum( qMax( 0,
172 int( d
->widget
->size().height() - d
->scrollingWidget
->size().height())));
175 void KineticView::setWidget(QGraphicsWidget
*item
)
178 d
->widget
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
179 d
->widget
->setParentItem(d
->scrollingWidget
);
180 d
->widget
->setPos(0, 0);
181 d
->widget
->setAcceptedMouseButtons(Qt::LeftButton
);
183 scrollAnimation
= new QPropertyAnimation(d
->widget
, "geometry");
184 connect(scrollAnimation
, SIGNAL(finished()), this, SLOT(overshoot()));
185 scrollAnimation
->setEasingCurve(QEasingCurve::OutCirc
);
187 d
->widget
->installEventFilter( this );
192 void KineticView::overshoot()
194 /* Detect if bouncer */
195 if( d
->bounceStatus
!= KineticScrollingPrivate::Running
) {
196 if( ( d
->cposition
> 0 ) || ( d
->cposition
< d
->minimalPos
+ d
->overshoot
) ) {
198 scrollAnimation
->setEasingCurve( QEasingCurve::OutBounce
);
200 if (d
->cposition
> 0)
203 finalPosition
= -d
->widget
->size().height( ) + d
->scrollingWidget
->size().height();
205 resetAnimation( finalPosition
, 900 );
206 d
->bounceStatus
= KineticScrollingPrivate::Running
;
209 d
->bounceStatus
= KineticScrollingPrivate::Finished
;
210 scrollAnimation
->setEasingCurve( QEasingCurve::OutCirc
);
215 void KineticView::setVerticalScrollValue(int value
)
217 const int pos
= thresholdPosition( -value
);
218 d
->widget
->setPos(0, pos
);
220 if( ( pos
== d
->overshoot
) || ( pos
== d
->minimalPos
) )
224 int KineticView::thresholdPosition(int value
)
227 d
->minimalPos
= -d
->widget
->size().height() + d
->scrollingWidget
->size().height()
229 d
->minimalPos
= qMin(d
->overshoot
, d
->minimalPos
);
230 d
->maximumPos
= value
;
232 d
->cposition
= qBound(d
->minimalPos
, d
->maximumPos
, d
->overshoot
);
237 void KineticView::resetAnimation(int finalPosition
, int duration
)
239 if (scrollAnimation
->state() != QAbstractAnimation::Stopped
)
240 scrollAnimation
->stop();
241 d
->cposition
= finalPosition
;
242 QRectF tmpGeometry
= d
->widget
->geometry();
243 scrollAnimation
->setStartValue(tmpGeometry
);
244 tmpGeometry
.setY(d
->cposition
);
245 scrollAnimation
->setEndValue(tmpGeometry
);
246 scrollAnimation
->setDuration(duration
);
247 scrollAnimation
->start();
252 void KineticView::mousePressEvent(QGraphicsSceneMouseEvent
*event
)
254 if (scrollAnimation
->state() != QAbstractAnimation::Stopped
)
255 scrollAnimation
->stop();
258 d
->mousePressEvent(event
);
261 void KineticView::mouseMoveEvent(QGraphicsSceneMouseEvent
*event
)
263 d
->mouseMoveEvent(event
);
264 setVerticalScrollValue(-(d
->widget
->pos().y() - d
->normalMovement()));
267 void KineticView::mouseReleaseEvent(QGraphicsSceneMouseEvent
*event
)
270 if( scrollAnimation
->state() != QAbstractAnimation::Running
) {
271 d
->mouseReleaseEvent(event
);
273 const int finalPos
= thresholdPosition(d
->widget
->pos().y() - d
->kinMovement()*6);
274 resetAnimation( finalPos
, d
->duration()*8 );
278 void KineticView::wheelEvent(QGraphicsSceneWheelEvent
*event
)
281 d
->mousePressEvent(NULL
);
282 d
->wheelReleaseEvent(event
);
283 const int finalPos
= thresholdPosition(d
->widget
->pos().y() - d
->kinMovement()*6);
284 resetAnimation( finalPos
, 900 );
287 void KineticView::resizeEvent(QGraphicsSceneResizeEvent
*event
)
291 QGraphicsWidget::resizeEvent(event
);
294 bool KineticView::eventFilter(QObject
*watched
, QEvent
*event
)
300 if (watched
== d
->widget
&& event
->type() == QEvent::GraphicsSceneMove
) {
301 verticalScrollbar
->blockSignals(true);
302 verticalScrollbar
->setValue(d
->widget
->pos().y());
303 verticalScrollbar
->blockSignals(false);