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"
26 #include <QGraphicsGridLayout>
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()
56 void KineticView::startTimer(int interval
)
58 timerID
= QObject::startTimer( interval
);
60 void KineticView::setWidget( QGraphicsWidget
*item
)
63 mWindow
->setSizePolicy( QSizePolicy::Expanding
, QSizePolicy::Fixed
);
64 mWindow
->setParentItem( mViewPort
);
65 mWindow
->setPos( 0, 0 );
66 mWindow
->setAcceptedMouseButtons( Qt::LeftButton
);
71 void KineticView::bounceTimer()
74 if ( direction
== DOWN
)
77 mWindow
->setPos( 0, cposition
);
79 if ( ( direction
== UP
) && ( cposition
< 0 ) ) {
83 } else if ( ( direction
== DOWN
) &&
84 ( ( cposition
- overshut
) > minimalPos
) ) {
85 cposition
= minimalPos
+ overshut
;
98 void KineticView::setScrollValue( int value
)
100 minimalPos
= -mWindow
->size().height() + mViewPort
->size().height()
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
) {
111 } else if ( cposition
== minimalPos
) {
125 void KineticView::mousePressEvent( QGraphicsSceneMouseEvent
*event
)
128 killTimer( timerID
);
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();
148 void KineticView::timerEvent( QTimerEvent
*event
)
150 if ( ( direction
== NONE
) && ( timerID
!= 0 ) ) {
151 mScrollVelocity
*= 0.9;
152 if ( qAbs( mScrollVelocity
) < 5.0 ) {
154 killTimer( timerID
);
157 setScrollValue( qRound( mScrollVelocity
) );
158 } else if ( timerID
!= 0 ) {
161 killTimer( event
->timerId() );