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"
23 #include "kineticscroll.h"
24 #include "scrollbar.h"
28 #include <QGraphicsGridLayout>
30 #include <QPropertyAnimation>
33 * - fix velocity( create a new easing curve? )
35 * - kinetic velocity parameterized upon count of list items
36 * - horizontal scrolling
37 * - parameterize dimensions
38 * - ringbuffer (minimize number of items in the QGV).
41 KineticView::KineticView( QGraphicsWidget
*parent
)
42 : QGraphicsWidget( parent
), mWindow( 0 ), mScrollVelocity( 0 ),
43 timerID( 0 ), overshut( 40 ), bounceFlag( 0 )
45 setSizePolicy( QSizePolicy::Expanding
, QSizePolicy::Expanding
);
47 mViewPort
= new QGraphicsWidget( this );
48 mViewPort
->setSizePolicy( QSizePolicy::Expanding
, QSizePolicy::Expanding
);
49 mViewPort
->setFlag( QGraphicsItem::ItemClipsChildrenToShape
, true);
50 layout
= new QGraphicsGridLayout( this );
51 layout
->addItem( mViewPort
, 0, 0 );
53 verticalScrollbar
= new ScrollBar( this );
54 connect( verticalScrollbar
, SIGNAL( valueChanged( int ) ), this, SLOT( setVerticalScrollValue( int ) ) );
56 layout
->addItem( verticalScrollbar
, 0, 1 );
58 setAcceptedMouseButtons( Qt::LeftButton
);
59 scroller
= new KineticScrolling
;
62 KineticView::~KineticView()
66 delete scrollAnimation
;
69 void KineticView::adjustScrollBar()
71 verticalScrollbar
->setMaximum( qMax( 0,
72 int( mWindow
->size().height() - mViewPort
->size().height() ) ) );
75 void KineticView::setWidget( QGraphicsWidget
*item
)
78 mWindow
->setSizePolicy( QSizePolicy::Expanding
, QSizePolicy::Fixed
);
79 mWindow
->setParentItem( mViewPort
);
80 mWindow
->setPos( 0, 0 );
81 mWindow
->setAcceptedMouseButtons( Qt::LeftButton
);
83 scrollAnimation
= new QPropertyAnimation( mWindow
, "geometry" );
84 scrollAnimation
->setEasingCurve( QEasingCurve::OutCirc
);
90 void KineticView::setVerticalScrollValue( int value
)
92 mWindow
->setPos( 0, -value
);
95 void KineticView::mousePressEvent( QGraphicsSceneMouseEvent
*event
)
97 if ( scrollAnimation
->state() != QAbstractAnimation::Stopped
)
98 scrollAnimation
->stop();
101 scroller
->mousePressEvent( event
);
104 void KineticView::mouseMoveEvent( QGraphicsSceneMouseEvent
*event
)
106 scroller
->mouseMoveEvent( event
);
107 setVerticalScrollValue( -( mWindow
->pos().y() - scroller
->movement() ) );
108 QGraphicsWidget::mouseMoveEvent( event
);
111 int KineticView::thresholdPosition( int value
)
114 int minimalPos
= -mWindow
->size( ).height( ) + mViewPort
->size( ).height( );
115 minimalPos
= qMin( 0, minimalPos
);
116 int maximumPos
= value
;
118 int cposition
= qBound( minimalPos
, maximumPos
, 0);
123 void KineticView::resetAnimation( int duration
)
125 if ( scrollAnimation
->state() != QAbstractAnimation::Stopped
)
126 scrollAnimation
->stop();
127 QRectF tmpGeometry
= mWindow
->geometry();
128 scrollAnimation
->setStartValue( tmpGeometry
);
129 int position
= thresholdPosition( tmpGeometry
.y() - scroller
->kin_movement()*6 );
130 tmpGeometry
.setY( position
);
131 scrollAnimation
->setEndValue( tmpGeometry
);
132 scrollAnimation
->setEndValue( tmpGeometry
);
133 scrollAnimation
->setDuration( duration
);
134 scrollAnimation
->start();
137 void KineticView::mouseReleaseEvent( QGraphicsSceneMouseEvent
*event
)
140 scroller
->mouseReleaseEvent( event
);
141 resetAnimation( scroller
->duration()*5 );
145 void KineticView::wheelEvent(QGraphicsSceneWheelEvent
*event
)
148 scroller
->mousePressEvent( NULL
);
149 scroller
->wheelReleaseEvent( event
);
150 resetAnimation( 900 );
153 void KineticView::resizeEvent( QGraphicsSceneResizeEvent
*event
)
157 QGraphicsWidget::resizeEvent( event
);