Removing debugging messages.
[kineticlist.git] / kineticview.cpp
blobbc5970ea4a9d001dca52df79978406452c592230
1 /////////////////////////////////////////////////////////////////////////
2 // kineticview.cpp //
3 // //
4 // Copyright(C) 2009 Igor Trindade Oliveira <igor.oliveira@indt.org.br>//
5 // Copyright(C) 2009 Adenilson Cavalcanti <adenilson.silva@idnt.org.br>//
6 // //
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. //
11 // //
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. //
16 // //
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 //
20 // 02110-1301 USA //
21 /////////////////////////////////////////////////////////////////////////
22 #include "kineticview.h"
24 #include <QObject>
25 #include <QDebug>
26 #include <QGraphicsGridLayout>
28 /* TODO:
29 * - bump effect when rolling (twice should be enough)
30 * - add a scrollbar for reference
31 * - kinetic velocity parameterized upon count of list items
32 * - ringbuffer (minimize number of items in the QGV).
33 * - parameterize dimensions
36 KineticView::KineticView( QGraphicsWidget *parent )
37 : QGraphicsWidget( parent ), mScrollVelocity( 0 ),
38 timerID( 0 ), overshut( 30 )
40 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding);
42 mViewPort = new QGraphicsWidget( this );
43 mViewPort->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
44 mViewPort->setFlag( QGraphicsItem::ItemClipsChildrenToShape, true);
45 layout = new QGraphicsGridLayout( this );
46 layout->addItem( mViewPort, 0, 0 );
48 setAcceptedMouseButtons( Qt::LeftButton );
51 KineticView::~KineticView()
53 delete mWindow;
56 void KineticView::startTimer(int interval)
58 timerID = QObject::startTimer( interval );
60 void KineticView::setWidget( QGraphicsWidget *item)
62 mWindow = item;
63 mWindow->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
64 mWindow->setParentItem( mViewPort );
65 mWindow->setPos( 0, 0 );
66 mWindow->setAcceptedMouseButtons( Qt::LeftButton );
68 update();
71 void KineticView::bounceTimer()
73 int delta = -5;
74 if ( direction == DOWN )
75 delta = 5;
76 cposition += delta;
77 mWindow->setPos( 0, cposition );
79 if ( ( direction == UP ) && ( cposition < 0 ) ) {
80 cposition = 0;
81 goto done;
83 } else if ( ( direction == DOWN ) &&
84 ( ( cposition - overshut) > minimalPos ) ) {
85 cposition = minimalPos + overshut;
86 goto done;
89 return;
91 done:
92 direction = NONE;
93 mScrollVelocity = 0;
94 killTimer( timerID );
95 timerID = 0;
98 void KineticView::setScrollValue( int value )
100 minimalPos = -mWindow->size().height() + mViewPort->size().height()
101 - overshut;
102 minimalPos = qMin( overshut, minimalPos );
103 maximumPos = mWindow->pos().y() - value;
105 cposition = qBound( minimalPos, maximumPos, overshut );
106 mWindow->setPos( 0, cposition );
108 if ( cposition == overshut ) {
109 direction = UP;
110 goto overshut;
111 } else if ( cposition == minimalPos ) {
112 direction = DOWN;
113 goto overshut;
114 } else
115 direction = NONE;
117 return;
120 overshut:
121 mScrollVelocity = 5;
122 startTimer( 20 );
125 void KineticView::mousePressEvent( QGraphicsSceneMouseEvent *event )
127 event->accept();
128 killTimer( timerID );
129 timerID = 0;
130 KineticScrolling::mousePressEvent( static_cast<QGraphicsSceneMouseEvent*>(event) );
133 void KineticView::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
135 KineticScrolling::mouseMoveEvent( static_cast<QGraphicsSceneMouseEvent*>(event) );
136 QGraphicsWidget::mouseMoveEvent( static_cast<QGraphicsSceneMouseEvent*>(event) );
137 setScrollValue( movement() );
140 void KineticView::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
142 KineticScrolling::mouseReleaseEvent(
143 static_cast<QGraphicsSceneMouseEvent*>( event ) );
144 mScrollVelocity = kin_movement();
145 startTimer( 50 );
148 void KineticView::timerEvent( QTimerEvent *event )
150 if ( ( direction == NONE ) && ( timerID != 0 ) ) {
151 mScrollVelocity *= 0.9;
152 if ( qAbs( mScrollVelocity ) < 5.0 ) {
153 if ( timerID ) {
154 killTimer( timerID );
157 setScrollValue( qRound( mScrollVelocity ) );
158 } else if ( timerID != 0 ) {
159 bounceTimer();
160 } else {
161 killTimer( event->timerId() );