Removing a useless flag.
[kineticlist.git] / kineticview.cpp
blob12aa2c14590cbe40bb7dd92d9fa6525d9f327b47
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 * - kill the fscking timer in overshut
30 * - bump effect when rolling (twice should be enough)
31 * - add a scrollbar for reference
32 * - kinetic velocity parameterized upon count of list items
33 * - ringbuffer (minimize number of items in the QGV).
34 * - parameterize dimensions
37 KineticView::KineticView( QGraphicsWidget *parent )
38 : QGraphicsWidget( parent ), mScrollVelocity( 0 ),
39 timerID( 0 ), overshut( 30 )
41 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding);
43 mViewPort = new QGraphicsWidget( this );
44 mViewPort->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
45 mViewPort->setFlag( QGraphicsItem::ItemClipsChildrenToShape, true);
46 layout = new QGraphicsGridLayout( this );
47 layout->addItem( mViewPort, 0, 0 );
49 setAcceptedMouseButtons( Qt::LeftButton );
52 KineticView::~KineticView()
54 delete mWindow;
57 void KineticView::startTimer(int interval)
59 timerID = QObject::startTimer( interval );
61 void KineticView::setWidget( QGraphicsWidget *item)
63 mWindow = item;
64 mWindow->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
65 mWindow->setParentItem( mViewPort );
66 mWindow->setPos( 0, 0 );
67 mWindow->setAcceptedMouseButtons( Qt::LeftButton );
69 update();
72 void KineticView::bounceTimer()
74 int delta = -5;
75 if ( direction == DOWN )
76 delta = 5;
77 cposition += delta;
78 mWindow->setPos( 0, cposition );
80 if ( ( direction == UP ) && ( cposition < 0 ) ) {
81 qDebug() << "Done UP bouncing.....!!!!!!!!!";
82 direction = NONE;
83 mScrollVelocity = 0;
84 cposition = 0;
85 killTimer( timerID );
86 timerID = 0;
87 } else if ( ( direction == DOWN ) &&
88 ( ( cposition - overshut) > minimalPos ) ) {
89 qDebug() << "Done DOWN bouncing.....!!!!!!!!!";
90 direction = NONE;
91 mScrollVelocity = 0;
92 cposition = minimalPos + overshut;
93 killTimer( timerID );
94 timerID = 0;
97 qDebug() << "cposition: " << cposition;
100 void KineticView::setScrollValue( int value )
102 minimalPos = -mWindow->size().height() + mViewPort->size().height() - overshut;
103 minimalPos = qMin( overshut, minimalPos );
104 maximumPos = mWindow->pos().y() - value;
106 cposition = qBound( minimalPos, maximumPos, overshut );
107 mWindow->setPos( 0, cposition );
109 if ( cposition == overshut ) {
110 qDebug() << "UPPER edge: animate overshut";
111 mScrollVelocity = 5;
112 direction = UP;
113 startTimer( 20 );
115 } else if ( cposition == minimalPos ) {
116 qDebug() << "LOWER edge: animate overshut";
117 mScrollVelocity = 5;
118 direction = DOWN;
119 startTimer( 20 );
120 } else
121 direction = NONE;
124 void KineticView::mousePressEvent( QGraphicsSceneMouseEvent *event )
126 event->accept();
127 killTimer( timerID );
128 timerID = 0;
129 KineticScrolling::mousePressEvent( static_cast<QGraphicsSceneMouseEvent*>(event) );
132 void KineticView::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
134 KineticScrolling::mouseMoveEvent( static_cast<QGraphicsSceneMouseEvent*>(event) );
135 QGraphicsWidget::mouseMoveEvent( static_cast<QGraphicsSceneMouseEvent*>(event) );
136 setScrollValue( movement() );
139 void KineticView::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
141 KineticScrolling::mouseReleaseEvent(
142 static_cast<QGraphicsSceneMouseEvent*>( event ) );
143 mScrollVelocity = kin_movement();
144 startTimer( 50 );
147 void KineticView::timerEvent( QTimerEvent *event )
149 qDebug() << "timer......" << "direction: " << direction
150 << "\tvelocity: " << mScrollVelocity
151 << "\tcposition: " << cposition;
153 if (direction == NONE ) {
154 setScrollValue( qRound( mScrollVelocity ) );
156 mScrollVelocity *= 0.9;
157 if ( qAbs( mScrollVelocity ) < 5.0 ) {
158 if ( timerID ) {
159 killTimer( timerID );
162 } else
163 bounceTimer();