Some more todos.
[kineticlist.git] / kineticview.cpp
blob07118a8cde33b2a4a32de55e8f8ee2f6388c929e
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 /* FIXME: make this portably */
27 #include <unistd.h>
28 #include <QGraphicsGridLayout>
30 /* TODO:
31 * - kill the fscking timer in overshut
32 * - bump effect when rolling (twice should be enough)
33 * - add a scrollbar for reference
34 * - kinetic velocity parameterized upon count of list items
35 * - ringbuffer (minimize number of items in the QGV).
36 * - parameterize dimensions
39 KineticView::KineticView( QGraphicsWidget *parent )
40 : QGraphicsWidget( parent ), mScrollVelocity( 0 ), newclick( 0 ),
41 timerID( 0 ), overshut( 30 )
43 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding);
45 mViewPort = new QGraphicsWidget( this );
46 mViewPort->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
47 mViewPort->setFlag( QGraphicsItem::ItemClipsChildrenToShape, true);
48 layout = new QGraphicsGridLayout( this );
49 layout->addItem( mViewPort, 0, 0 );
51 setAcceptedMouseButtons( Qt::LeftButton );
54 KineticView::~KineticView()
56 delete mWindow;
59 void KineticView::startTimer(int interval)
61 timerID = QObject::startTimer( interval );
63 void KineticView::setWidget( QGraphicsWidget *item)
65 mWindow = item;
66 mWindow->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
67 mWindow->setParentItem( mViewPort );
68 mWindow->setPos( 0, 0 );
69 mWindow->setAcceptedMouseButtons( Qt::LeftButton );
71 update();
74 void KineticView::bounceTimer()
76 int delta = -5;
77 if ( direction == DOWN )
78 delta = 5;
79 cposition += delta;
80 mWindow->setPos( 0, cposition );
82 if ( ( direction == UP ) && ( cposition < 0 ) ) {
83 qDebug() << "Done UP bouncing.....!!!!!!!!!";
84 direction = NONE;
85 mScrollVelocity = 0;
86 cposition = 0;
87 newclick = 1;
88 killTimer( timerID );
89 timerID = 0;
90 } else if ( ( direction == DOWN ) &&
91 ( ( cposition - overshut) > minimalPos ) ) {
92 qDebug() << "Done DOWN bouncing.....!!!!!!!!!";
93 direction = NONE;
94 mScrollVelocity = 0;
95 cposition = minimalPos + overshut;
96 newclick = 1;
97 killTimer( timerID );
98 timerID = 0;
101 qDebug() << "cposition: " << cposition;
104 void KineticView::setScrollValue( int value )
106 minimalPos = -mWindow->size().height() + mViewPort->size().height() - overshut;
107 minimalPos = qMin( overshut, minimalPos );
108 maximumPos = mWindow->pos().y() - value;
110 cposition = qBound( minimalPos, maximumPos, overshut );
111 mWindow->setPos( 0, cposition );
113 if ( cposition == overshut ) {
114 qDebug() << "UPPER edge: animate overshut";
115 mScrollVelocity = 5;
116 direction = UP;
117 startTimer( 20 );
119 } else if ( cposition == minimalPos ) {
120 qDebug() << "LOWER edge: animate overshut";
121 mScrollVelocity = 5;
122 direction = DOWN;
123 startTimer( 20 );
124 } else
125 direction = NONE;
128 void KineticView::mousePressEvent( QGraphicsSceneMouseEvent *event )
130 event->accept();
131 newclick = 1;
132 killTimer( timerID );
133 timerID = 0;
134 KineticScrolling::mousePressEvent( static_cast<QGraphicsSceneMouseEvent*>(event) );
137 void KineticView::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
139 newclick = 0;
140 KineticScrolling::mouseMoveEvent( static_cast<QGraphicsSceneMouseEvent*>(event) );
141 QGraphicsWidget::mouseMoveEvent( static_cast<QGraphicsSceneMouseEvent*>(event) );
142 setScrollValue( movement() );
145 void KineticView::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
147 newclick = 0;
148 KineticScrolling::mouseReleaseEvent(
149 static_cast<QGraphicsSceneMouseEvent*>( event ) );
150 mScrollVelocity = kin_movement();
151 startTimer( 50 );
154 void KineticView::timerEvent( QTimerEvent *event )
156 qDebug() << "timer......" << "direction: " << direction
157 << "\tnewclick: " << newclick
158 << "\tvelocity: " << mScrollVelocity
159 << "\tcposition: " << cposition;
161 if ( newclick ) {
162 if ( timerID )
163 killTimer( timerID );
164 return;
167 if (direction == NONE ) {
168 setScrollValue( qRound( mScrollVelocity ) );
170 mScrollVelocity *= 0.9;
171 if ( qAbs( mScrollVelocity ) < 5.0 ) {
172 if ( timerID ) {
173 killTimer( timerID );
176 } else
177 bounceTimer();