Using integers to do the calculus of position.
[kineticlist.git] / kineticview.cpp
blob46dfb1b2a41edfe708cb51f4e03158e2854edaec
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>
27 KineticView::KineticView( QGraphicsWidget *parent )
28 : QGraphicsWidget( parent ), mScrollVelocity( 0 ), newclick( 0 ),
29 timerID( 0 )
31 setFlag( QGraphicsItem::ItemIsSelectable, true );
32 setAcceptedMouseButtons( Qt::LeftButton );
34 mViewPort = new QGraphicsWidget( this );
36 resize(300, 700);
39 KineticView::~KineticView()
41 delete mWindow;
44 void KineticView::startTimer()
46 timerID = QObject::startTimer( 50 );
48 void KineticView::setWidget( QGraphicsWidget *item)
50 mWindow = item;
51 item->setParentItem( mViewPort );
52 item->setParentLayoutItem( mViewPort );
54 QRectF tmpGeometry = mWindow->geometry();
55 mWindow->setGeometry( QRectF( tmpGeometry.x(), tmpGeometry.y() +
56 tmpGeometry.height(),
57 mWindow->geometry().width(),
58 mWindow->geometry().height() ) );
60 update();
63 bool KineticView::event ( QEvent * event )
65 int tmp1, tmp2;
66 switch( event->type() ) {
68 case QEvent::GraphicsSceneMousePress:
69 newclick = 1;
70 killTimer( timerID );
71 timerID = 0;
72 KineticScrolling::mousePressEvent( static_cast<QGraphicsSceneMouseEvent*>(event) );
73 QGraphicsWidget::mousePressEvent( static_cast<QGraphicsSceneMouseEvent*>(event) );
74 break;
76 case QEvent::GraphicsSceneMouseMove:
77 newclick = 0;
78 KineticScrolling::mouseMoveEvent( static_cast<QGraphicsSceneMouseEvent*>(event) );
79 QGraphicsWidget::mouseMoveEvent( static_cast<QGraphicsSceneMouseEvent*>(event) );
80 tmp1 = -mWindow->size().height() + mViewPort->size().height();
81 tmp1 = qMin( 0, tmp1 );
82 tmp2 = mWindow->pos().y() - qRound( movement() );
83 mWindow->setPos( 0, qBound( tmp1, tmp2, 0 ) );
84 break;
86 case QEvent::GraphicsSceneMouseRelease:
87 newclick = 0;
88 KineticScrolling::mouseReleaseEvent(
89 static_cast<QGraphicsSceneMouseEvent*>( event ) );
90 mScrollVelocity = kin_movement();
91 startTimer();
92 break;
94 case QEvent::Timer:
95 setScrollValue( mScrollVelocity );
96 mScrollVelocity *= 0.9;
97 if( !mScrollVelocity || newclick ) {
98 if ( timerID ) {
99 killTimer( timerID );
102 break;
103 default:
104 break;
107 return true;
110 void KineticView::setScrollValue( int value )
112 mWindow->setPos( 0, qBound( qMin( 0.0,
113 -mWindow->size().height()
114 + mViewPort->size().height() ),
115 mWindow->pos().y() - value, 0.0) );