diff options
author | Marius Bugge Monsen <mmonsen@trolltech.com> | 2009-08-12 18:45:48 +0200 |
---|---|---|
committer | Marius Bugge Monsen <mmonsen@trolltech.com> | 2009-08-12 18:45:48 +0200 |
commit | b84dc392a5b18c9c5279c0d579710d8577e0f047 (patch) | |
tree | 72ff97cffdea1776f4515c8ebf608245dd827c7c | |
parent | 935a2c15d38a60e43da50e7744d5faff2e81be8f (diff) |
Add experimental code in QtListController for animated per-item scrolling.
-rw-r--r-- | src/qlistcontroller.cpp | 57 | ||||
-rw-r--r-- | src/qlistcontroller.h | 1 | ||||
-rw-r--r-- | src/qlistcontroller_p.h | 6 |
3 files changed, 61 insertions, 3 deletions
diff --git a/src/qlistcontroller.cpp b/src/qlistcontroller.cpp index c6101eb..cd3e57e 100644 --- a/src/qlistcontroller.cpp +++ b/src/qlistcontroller.cpp @@ -30,6 +30,7 @@ #include <qtransform.h> #include <qevent.h> #include <qgraphicssceneevent.h> +#include <qpropertyanimation.h> #include <qdebug.h> QT_BEGIN_NAMESPACE @@ -44,6 +45,8 @@ QtListControllerPrivate::QtListControllerPrivate() model(0), selectionManager(0), view(0), + animation(0), + firstIndex(0), scrollPerItem(true) { } @@ -99,6 +102,44 @@ void QtListControllerPrivate::_q_offsetChanged(qreal offset) emit q->scrollValueChanged(offset); } +/*! + \internal +*/ +void QtListControllerPrivate::_q_animationFinished() +{ + view->setOffset(0); + view->setFirstIndex(firstIndex); +} + +/*! + \internal +*/ +void QtListControllerPrivate::animateFirstIndex(int index) +{ + // animate the offset when scrolling per item + int previous = view->firstIndex(); + if (animation && index != previous) { + animation->stop(); + if (index > previous) { // scrolling down + qreal offset = 0; + for (int i = previous; i < index; ++i) + offset += view->itemGeometry(i).height(); + animation->setStartValue(view->offset()); + animation->setEndValue(offset); + } else { // scrolling up + qreal offset = view->offset(); + view->setFirstIndex(index); + for (int i = index; i < previous; ++i) + offset += view->itemGeometry(i).height(); + view->setOffset(offset); + animation->setStartValue(offset); + animation->setEndValue(0); + } + firstIndex = index; + animation->start(); + } +} + // QtListController /*! @@ -250,6 +291,13 @@ void QtListController::setView(QtGraphicsListView *view) QtGraphicsListView *old = d->view; + if (!d->animation) { + d->animation = new QPropertyAnimation(0, "offset", this); + d->animation->setDuration(100); + connect(d->animation, SIGNAL(finished()), this, SLOT(_q_animationFinished())); + } + d->animation->setTargetObject(view); + if (d->view) { disconnect(d->view, SIGNAL(destroyed()), this, SLOT(_q_viewDestroyed())); disconnect(d->view, SIGNAL(firstIndexChanged(int)), this, SLOT(_q_firstIndexChanged(int))); @@ -300,6 +348,7 @@ void QtListController::setScrollValue(qreal value) if (!d->view) return; if (d->scrollPerItem) + //d->animateFirstIndex(value); d->view->setFirstIndex(static_cast<int>(value)); else d->view->setOffset(value); @@ -554,9 +603,11 @@ bool QtListController::wheelEvent(QGraphicsSceneWheelEvent *event, const QTransf if (d->scrollPerItem) { const int index = d->view->firstIndex() - event->delta() / 120; d->view->setFirstIndex(qBound(0, index, d->view->maximumFirstIndex())); - return true; - } // ### FIXME: set offset - return false; + } else { + const qreal offset = d->view->offset() - event->delta() / 120; + d->view->setOffset(qBound(.0, offset, d->view->maximumOffset())); + } + return true; } /*! diff --git a/src/qlistcontroller.h b/src/qlistcontroller.h index 1c3232c..ad7f7c6 100644 --- a/src/qlistcontroller.h +++ b/src/qlistcontroller.h @@ -117,6 +117,7 @@ private: Q_PRIVATE_SLOT(d_func(), void _q_selectionsDestroyed()) Q_PRIVATE_SLOT(d_func(), void _q_firstIndexChanged(int index)) Q_PRIVATE_SLOT(d_func(), void _q_offsetChanged(qreal offset)) + Q_PRIVATE_SLOT(d_func(), void _q_animationFinished()) }; QT_END_NAMESPACE diff --git a/src/qlistcontroller_p.h b/src/qlistcontroller_p.h index d8b396d..13825e0 100644 --- a/src/qlistcontroller_p.h +++ b/src/qlistcontroller_p.h @@ -44,6 +44,7 @@ QT_BEGIN_NAMESPACE class QtListController; class QtListSelectionManager; class QtGraphicsListView; +class QPropertyAnimation; class QtListControllerPrivate { @@ -57,7 +58,9 @@ public: void _q_selectionsDestroyed(); void _q_firstIndexChanged(int index); void _q_offsetChanged(qreal offset); + void _q_animationFinished(); + void animateFirstIndex(int index); // ### hack - not implemented in Qt QVariant inputMethodQueryHelper(Qt::InputMethodQuery query) const { Q_UNUSED(query); return QVariant(); } @@ -67,6 +70,9 @@ public: QPointer<QtListSelectionManager> selectionManager; QtGraphicsListView *view; + QPropertyAnimation *animation; + int firstIndex; + bool scrollPerItem; }; |