summaryrefslogtreecommitdiff
path: root/src/declarative/graphicsitems/qdeclarativepathview.cpp
diff options
context:
space:
mode:
authorPaul Olav Tvete <paul.tvete@nokia.com>2010-06-18 13:10:26 +0200
committerPaul Olav Tvete <paul.tvete@nokia.com>2010-06-18 13:10:26 +0200
commita3fd00796dafe52d4ff138b271564daf70d1adee (patch)
tree77ca481c06c1b2c3c7822a4cd5864702e341b680 /src/declarative/graphicsitems/qdeclarativepathview.cpp
parent793d1ed8d3a03eefdd487facdacf66ba575e1a07 (diff)
parent6aa50af000f85cc4497749fcf0860c8ed244a60e (diff)
downloadqt4-tools-a3fd00796dafe52d4ff138b271564daf70d1adee.tar.gz
Merge remote branch 'qt/4.7' into lighthouse
Conflicts: configure mkspecs/common/qws.conf src/corelib/io/qresource.cpp src/gui/image/qpixmapdata_p.h src/gui/kernel/qapplication.cpp src/gui/kernel/qapplication_p.h src/gui/painting/qpaintengine_raster.cpp src/gui/text/qfontdatabase.cpp src/opengl/qgl_p.h src/plugins/mediaservices/gstreamer/gstreamer.pro
Diffstat (limited to 'src/declarative/graphicsitems/qdeclarativepathview.cpp')
-rw-r--r--src/declarative/graphicsitems/qdeclarativepathview.cpp156
1 files changed, 127 insertions, 29 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativepathview.cpp b/src/declarative/graphicsitems/qdeclarativepathview.cpp
index d0a3cd1584..0c2d249f7c 100644
--- a/src/declarative/graphicsitems/qdeclarativepathview.cpp
+++ b/src/declarative/graphicsitems/qdeclarativepathview.cpp
@@ -49,6 +49,7 @@
#include <qlistmodelinterface_p.h>
#include <QGraphicsSceneEvent>
+#include <qmath.h>
#include <math.h>
QT_BEGIN_NAMESPACE
@@ -98,9 +99,8 @@ QDeclarativeItem *QDeclarativePathViewPrivate::getItem(int modelIndex)
if (!attType) {
// pre-create one metatype to share with all attached objects
attType = new QDeclarativeOpenMetaObjectType(&QDeclarativePathViewAttached::staticMetaObject, qmlEngine(q));
- foreach(const QString &attr, path->attributes()) {
+ foreach(const QString &attr, path->attributes())
attType->createProperty(attr.toUtf8());
- }
}
qPathViewAttachedType = attType;
QDeclarativePathViewAttached *att = static_cast<QDeclarativePathViewAttached *>(qmlAttachedPropertiesObject<QDeclarativePathView>(item));
@@ -110,6 +110,8 @@ QDeclarativeItem *QDeclarativePathViewPrivate::getItem(int modelIndex)
att->setOnPath(true);
}
item->setParentItem(q);
+ QDeclarativeItemPrivate *itemPrivate = static_cast<QDeclarativeItemPrivate*>(QGraphicsItemPrivate::get(item));
+ itemPrivate->addItemChangeListener(this, QDeclarativeItemPrivate::Geometry);
}
requestedIndex = -1;
return item;
@@ -121,6 +123,8 @@ void QDeclarativePathViewPrivate::releaseItem(QDeclarativeItem *item)
return;
if (QDeclarativePathViewAttached *att = attached(item))
att->setOnPath(false);
+ QDeclarativeItemPrivate *itemPrivate = static_cast<QDeclarativeItemPrivate*>(QGraphicsItemPrivate::get(item));
+ itemPrivate->removeItemChangeListener(this, QDeclarativeItemPrivate::Geometry);
model->release(item);
}
@@ -280,8 +284,8 @@ void QDeclarativePathViewPrivate::updateItem(QDeclarativeItem *item, qreal perce
att->setValue(attr.toUtf8(), path->attributeAt(attr, percent));
}
QPointF pf = path->pointAt(percent);
- item->setX(pf.x() - item->width()*item->scale()/2);
- item->setY(pf.y() - item->height()*item->scale()/2);
+ item->setX(qRound(pf.x() - item->width()*item->scale()/2));
+ item->setY(qRound(pf.y() - item->height()*item->scale()/2));
}
void QDeclarativePathViewPrivate::regenerate()
@@ -306,14 +310,28 @@ void QDeclarativePathViewPrivate::regenerate()
\brief The PathView element lays out model-provided items on a path.
\inherits Item
- The model is typically provided by a QAbstractListModel "C++ model object", but can also be created directly in QML.
+ A PathView displays data from models created from built-in QML elements like ListModel
+ and XmlListModel, or custom model classes defined in C++ that inherit from
+ QAbstractListModel.
+
+ A ListView has a \l model, which defines the data to be displayed, and
+ a \l delegate, which defines how the data should be displayed.
+ The \l delegate is instantiated for each item on the \l path.
+ The items may be flicked to move them along the path.
+
+ For example, if there is a simple list model defined in a file \c ContactModel.qml like this:
+
+ \snippet doc/src/snippets/declarative/pathview/ContactModel.qml 0
- The items are laid out along a path defined by a \l Path and may be flicked to scroll.
+ This data can be represented as a PathView, like this:
\snippet doc/src/snippets/declarative/pathview/pathview.qml 0
\image pathview.gif
+ Delegates are instantiated as needed and may be destroyed at any time.
+ State should \e never be stored in a delegate.
+
\bold Note that views do not enable \e clip automatically. If the view
is not clipped by another item or the screen, it will be necessary
to set \e {clip: true} in order to have the out of view items clipped
@@ -340,6 +358,13 @@ QDeclarativePathView::~QDeclarativePathView()
}
/*!
+ \qmlattachedproperty PathView PathView::view
+ This attached property holds the view that manages this delegate instance.
+
+ It is attached to each instance of the delegate.
+*/
+
+/*!
\qmlattachedproperty bool PathView::onPath
This attached property holds whether the item is currently on the path.
@@ -414,7 +439,7 @@ void QDeclarativePathView::setModel(const QVariant &model)
d->model = vim;
} else {
if (!d->ownModel) {
- d->model = new QDeclarativeVisualDataModel(qmlContext(this));
+ d->model = new QDeclarativeVisualDataModel(qmlContext(this), this);
d->ownModel = true;
}
if (QDeclarativeVisualDataModel *dataModel = qobject_cast<QDeclarativeVisualDataModel*>(d->model))
@@ -467,12 +492,14 @@ void QDeclarativePathView::setPath(QDeclarativePath *path)
disconnect(d->path, SIGNAL(changed()), this, SLOT(refill()));
d->path = path;
connect(d->path, SIGNAL(changed()), this, SLOT(refill()));
- d->clear();
- if (d->attType) {
- d->attType->release();
- d->attType = 0;
+ if (d->isValid() && isComponentComplete()) {
+ d->clear();
+ if (d->attType) {
+ d->attType->release();
+ d->attType = 0;
+ }
+ d->regenerate();
}
- d->regenerate();
emit pathChanged();
}
@@ -522,6 +549,33 @@ void QDeclarativePathView::setCurrentIndex(int idx)
}
/*!
+ \qmlmethod PathView::incrementCurrentIndex()
+
+ Increments the current index.
+*/
+void QDeclarativePathView::incrementCurrentIndex()
+{
+ setCurrentIndex(currentIndex()+1);
+}
+
+
+/*!
+ \qmlmethod PathView::decrementCurrentIndex()
+
+ Decrements the current index.
+*/
+void QDeclarativePathView::decrementCurrentIndex()
+{
+ Q_D(QDeclarativePathView);
+ if (d->model && d->model->count()) {
+ int idx = currentIndex()-1;
+ if (idx < 0)
+ idx = d->model->count() - 1;
+ setCurrentIndex(idx);
+ }
+}
+
+/*!
\qmlproperty real PathView::offset
The offset specifies how far along the path the items are from their initial positions.
@@ -557,7 +611,7 @@ void QDeclarativePathViewPrivate::setOffset(qreal o)
}
/*!
- \qmlproperty component PathView::highlight
+ \qmlproperty Component PathView::highlight
This property holds the component to use as the highlight.
An instance of the highlight component will be created for each view.
@@ -618,12 +672,12 @@ QDeclarativeItem *QDeclarativePathView::highlightItem()
These properties set the preferred range of the highlight (current item)
within the view. The preferred values must be in the range 0.0-1.0.
- If highlightRangeMode is set to \e ApplyRange the view will
+ If highlightRangeMode is set to \e PathView.ApplyRange the view will
attempt to maintain the highlight within the range, however
the highlight can move outside of the range at the ends of the path
or due to a mouse interaction.
- If highlightRangeMode is set to \e StrictlyEnforceRange the highlight will never
+ If highlightRangeMode is set to \e PathView.StrictlyEnforceRange the highlight will never
move outside of the range. This means that the current item will change
if a keyboard or mouse action would cause the highlight to move
outside of the range.
@@ -631,14 +685,14 @@ QDeclarativeItem *QDeclarativePathView::highlightItem()
Note that this is the correct way to influence where the
current item ends up when the view moves. For example, if you want the
currently selected item to be in the middle of the path, then set the
- highlight range to be 0.5,0.5 and highlightRangeMode to StrictlyEnforceRange.
+ highlight range to be 0.5,0.5 and highlightRangeMode to PathView.StrictlyEnforceRange.
Then, when the path scrolls,
the currently selected item will be the item at that position. This also applies to
when the currently selected item changes - it will scroll to within the preferred
highlight range. Furthermore, the behaviour of the current item index will occur
whether or not a highlight exists.
- The default value is \e StrictlyEnforceRange.
+ The default value is \e PathView.StrictlyEnforceRange.
Note that a valid range requires preferredHighlightEnd to be greater
than or equal to preferredHighlightBegin.
@@ -786,12 +840,17 @@ void QDeclarativePathView::setInteractive(bool interactive)
}
/*!
- \qmlproperty component PathView::delegate
+ \qmlproperty Component PathView::delegate
The delegate provides a template defining each item instantiated by the view.
The index is exposed as an accessible \c index property. Properties of the
model are also available depending upon the type of \l {qmlmodels}{Data Model}.
+ The number of elements in the delegate has a direct effect on the
+ flicking performance of the view when pathItemCount is specified. If at all possible, place functionality
+ that is not needed for the normal display of the delegate in a \l Loader which
+ can load additional elements when needed.
+
Note that the PathView will layout the items based on the size of the root
item in the delegate.
@@ -843,6 +902,7 @@ void QDeclarativePathView::setPathItemCount(int i)
if (i < 1)
i = 1;
d->pathItems = i;
+ d->updateMappedRange();
if (d->isValid() && isComponentComplete()) {
d->regenerate();
}
@@ -906,7 +966,7 @@ void QDeclarativePathView::mousePressEvent(QGraphicsSceneMouseEvent *event)
void QDeclarativePathView::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
Q_D(QDeclarativePathView);
- if (!d->interactive || d->lastPosTime.isNull())
+ if (!d->interactive || !d->lastPosTime.isValid())
return;
if (!d->stealMouse) {
@@ -940,7 +1000,7 @@ void QDeclarativePathView::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
Q_D(QDeclarativePathView);
d->stealMouse = false;
setKeepMouseGrab(false);
- if (!d->interactive || d->lastPosTime.isNull())
+ if (!d->interactive || !d->lastPosTime.isValid())
return;
qreal elapsed = qreal(d->lastElapsed + QDeclarativeItemPrivate::elapsed(d->lastPosTime)) / 1000.;
@@ -961,7 +1021,12 @@ void QDeclarativePathView::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
else
dist = qRound(dist - d->offset) + d->offset;
// Calculate accel required to stop on item boundary
- accel = v2 / (2.0f * qAbs(dist));
+ if (dist <= 0.) {
+ dist = 0.;
+ accel = 0.;
+ } else {
+ accel = v2 / (2.0f * qAbs(dist));
+ }
}
d->moveOffset.setValue(d->offset);
d->tl.accel(d->moveOffset, velocity, accel, dist);
@@ -970,7 +1035,7 @@ void QDeclarativePathView::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
d->fixOffset();
}
- d->lastPosTime = QTime();
+ d->lastPosTime.invalidate();
ungrabMouse();
}
@@ -1012,8 +1077,8 @@ bool QDeclarativePathView::sendMouseEvent(QGraphicsSceneMouseEvent *event)
grabMouse();
return d->stealMouse;
- } else if (!d->lastPosTime.isNull()) {
- d->lastPosTime = QTime();
+ } else if (d->lastPosTime.isValid()) {
+ d->lastPosTime.invalidate();
}
return false;
}
@@ -1041,12 +1106,26 @@ bool QDeclarativePathView::sceneEventFilter(QGraphicsItem *i, QEvent *e)
return QDeclarativeItem::sceneEventFilter(i, e);
}
+bool QDeclarativePathView::event(QEvent *event)
+{
+ if (event->type() == QEvent::User) {
+ refill();
+ return true;
+ }
+
+ return QDeclarativeItem::event(event);
+}
+
void QDeclarativePathView::componentComplete()
{
Q_D(QDeclarativePathView);
QDeclarativeItem::componentComplete();
d->createHighlight();
- d->regenerate();
+ // It is possible that a refill has already happended to to Path
+ // bindings being handled in the componentComplete(). If so
+ // don't do it again.
+ if (d->items.count() == 0)
+ d->regenerate();
d->updateHighlight();
}
@@ -1056,6 +1135,7 @@ void QDeclarativePathView::refill()
if (!d->isValid() || !isComponentComplete())
return;
+ d->layoutScheduled = false;
bool currentVisible = false;
// first move existing items and remove items off path
@@ -1101,7 +1181,8 @@ void QDeclarativePathView::refill()
while ((pos > startPos || !d->items.count()) && d->items.count() < count) {
// qDebug() << "append" << idx;
QDeclarativeItem *item = d->getItem(idx);
- item->setZValue(idx+1);
+ if (d->model->completePending())
+ item->setZValue(idx+1);
if (d->currentIndex == idx) {
item->setFocus(true);
if (QDeclarativePathViewAttached *att = d->attached(item))
@@ -1114,7 +1195,8 @@ void QDeclarativePathView::refill()
d->firstIndex = idx;
d->items.append(item);
d->updateItem(item, pos);
- d->model->completeItem();
+ if (d->model->completePending())
+ d->model->completeItem();
++idx;
if (idx >= d->model->count())
idx = 0;
@@ -1128,7 +1210,8 @@ void QDeclarativePathView::refill()
while (pos >= 0.0 && pos < startPos) {
// qDebug() << "prepend" << idx;
QDeclarativeItem *item = d->getItem(idx);
- item->setZValue(idx+1);
+ if (d->model->completePending())
+ item->setZValue(idx+1);
if (d->currentIndex == idx) {
item->setFocus(true);
if (QDeclarativePathViewAttached *att = d->attached(item))
@@ -1139,7 +1222,8 @@ void QDeclarativePathView::refill()
}
d->items.prepend(item);
d->updateItem(item, pos);
- d->model->completeItem();
+ if (d->model->completePending())
+ d->model->completeItem();
d->firstIndex = idx;
idx = d->firstIndex - 1;
if (idx < 0)
@@ -1251,6 +1335,19 @@ void QDeclarativePathView::createdItem(int index, QDeclarativeItem *item)
{
Q_D(QDeclarativePathView);
if (d->requestedIndex != index) {
+ if (!d->attType) {
+ // pre-create one metatype to share with all attached objects
+ d->attType = new QDeclarativeOpenMetaObjectType(&QDeclarativePathViewAttached::staticMetaObject, qmlEngine(this));
+ foreach(const QString &attr, d->path->attributes())
+ d->attType->createProperty(attr.toUtf8());
+ }
+ qPathViewAttachedType = d->attType;
+ QDeclarativePathViewAttached *att = static_cast<QDeclarativePathViewAttached *>(qmlAttachedPropertiesObject<QDeclarativePathView>(item));
+ qPathViewAttachedType = 0;
+ if (att) {
+ att->m_view = this;
+ att->setOnPath(false);
+ }
item->setParentItem(this);
d->updateItem(item, index < d->firstIndex ? 0.0 : 1.0);
}
@@ -1276,6 +1373,7 @@ int QDeclarativePathViewPrivate::calcCurrentIndex()
if (offset < 0)
offset += model->count();
current = qRound(qAbs(qmlMod(model->count() - offset, model->count())));
+ current = current % model->count();
}
return current;