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 * - 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()
57 void KineticView::startTimer(int interval
)
59 timerID
= QObject::startTimer( interval
);
61 void KineticView::setWidget( QGraphicsWidget
*item
)
64 mWindow
->setSizePolicy( QSizePolicy::Expanding
, QSizePolicy::Fixed
);
65 mWindow
->setParentItem( mViewPort
);
66 mWindow
->setPos( 0, 0 );
67 mWindow
->setAcceptedMouseButtons( Qt::LeftButton
);
72 void KineticView::bounceTimer()
75 if ( direction
== DOWN
)
78 mWindow
->setPos( 0, cposition
);
80 if ( ( direction
== UP
) && ( cposition
< 0 ) ) {
81 qDebug() << "Done UP bouncing.....!!!!!!!!!";
87 } else if ( ( direction
== DOWN
) &&
88 ( ( cposition
- overshut
) > minimalPos
) ) {
89 qDebug() << "Done DOWN bouncing.....!!!!!!!!!";
92 cposition
= minimalPos
+ overshut
;
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";
115 } else if ( cposition
== minimalPos
) {
116 qDebug() << "LOWER edge: animate overshut";
124 void KineticView::mousePressEvent( QGraphicsSceneMouseEvent
*event
)
127 killTimer( timerID
);
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();
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 ) {
159 killTimer( timerID
);