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
)
98 scroller
->mousePressEvent( event
);
101 void KineticView::mouseMoveEvent( QGraphicsSceneMouseEvent
*event
)
103 scroller
->mouseMoveEvent( event
);
104 setVerticalScrollValue( -( mWindow
->pos().y() - scroller
->movement() ) );
105 QGraphicsWidget::mouseMoveEvent( event
);
108 int KineticView::thresholdPosition( int value
)
111 int minimalPos
= -mWindow
->size( ).height( ) + mViewPort
->size( ).height( );
112 minimalPos
= qMin( 0, minimalPos
);
113 int maximumPos
= value
;
115 int cposition
= qBound( minimalPos
, maximumPos
, 0);
120 void KineticView::mouseReleaseEvent( QGraphicsSceneMouseEvent
*event
)
122 if( scrollAnimation
->state() != QAbstractAnimation::Stopped
)
123 scrollAnimation
->stop();
125 scroller
->mouseReleaseEvent( event
);
127 QRectF tmpGeometry
= mWindow
->geometry();
128 scrollAnimation
->setStartValue( tmpGeometry
);
130 int position
= thresholdPosition( tmpGeometry
.y() - scroller
->kin_movement()*6 );
131 tmpGeometry
.setY( position
);
132 scrollAnimation
->setEndValue( tmpGeometry
);
133 scrollAnimation
->setDuration( scroller
->duration()*5 );
134 scrollAnimation
->start();
137 void KineticView::wheelEvent(QGraphicsSceneWheelEvent
*event
)
139 if( scrollAnimation
->state() != QAbstractAnimation::Stopped
)
140 scrollAnimation
->stop();
142 scroller
->mousePressEvent( NULL
);
143 scroller
->wheelReleaseEvent( event
);
144 mScrollVelocity
= scroller
->kin_movement();
146 QRectF tmpGeometry
= mWindow
->geometry();
147 scrollAnimation
->setStartValue( tmpGeometry
);
149 int position
= thresholdPosition( tmpGeometry
.y() - scroller
->kin_movement()*6 );
150 tmpGeometry
.setY( position
);
151 scrollAnimation
->setEndValue( tmpGeometry
);
152 scrollAnimation
->setDuration( 900 );
154 scrollAnimation
->start();
157 void KineticView::resizeEvent( QGraphicsSceneResizeEvent
*event
)
161 QGraphicsWidget::resizeEvent( event
);