From 2d3323d239c9d20c98e09c9398931e60bb799cca Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Mon, 8 Mar 2010 12:38:17 +1000 Subject: Add PathView.isCurrentIndex and PathView.view attached properties Adding PathView attached properties to be in line with other views. Task-number: QT-319 --- .../graphicsitems/qdeclarativepathview.cpp | 196 +++++++++++++-------- .../graphicsitems/qdeclarativepathview_p.h | 51 +++++- .../graphicsitems/qdeclarativepathview_p_p.h | 7 +- .../qdeclarativepathview/data/pathview.qml | 11 +- .../tst_qdeclarativepathview.cpp | 46 ++++- 5 files changed, 235 insertions(+), 76 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativepathview.cpp b/src/declarative/graphicsitems/qdeclarativepathview.cpp index 50aa9ef..cc17157 100644 --- a/src/declarative/graphicsitems/qdeclarativepathview.cpp +++ b/src/declarative/graphicsitems/qdeclarativepathview.cpp @@ -44,7 +44,6 @@ #include #include - #include #include #include @@ -64,48 +63,31 @@ inline qreal qmlMod(qreal x, qreal y) return fmod(x, y); } +static QDeclarativeOpenMetaObjectType *qPathViewAttachedType = 0; -class QDeclarativePathViewAttached : public QObject +QDeclarativePathViewAttached::QDeclarativePathViewAttached(QObject *parent) +: QObject(parent), m_view(0), m_onPath(false), m_isCurrent(false) { - Q_OBJECT - - Q_PROPERTY(bool onPath READ isOnPath NOTIFY onPathChanged) -public: - QDeclarativePathViewAttached(QObject *parent) - : QObject(parent), mo(new QDeclarativeOpenMetaObject(this)), onPath(false) - { - } - - ~QDeclarativePathViewAttached() - { - QDeclarativePathView::attachedProperties.remove(parent()); - } - - QVariant value(const QByteArray &name) const - { - return mo->value(name); - } - void setValue(const QByteArray &name, const QVariant &val) - { - mo->setValue(name, val); - } - - bool isOnPath() const { return onPath; } - void setOnPath(bool on) { - if (on != onPath) { - onPath = on; - emit onPathChanged(); - } + if (qPathViewAttachedType) { + m_metaobject = new QDeclarativeOpenMetaObject(this, qPathViewAttachedType); + m_metaobject->setCached(true); + } else { + m_metaobject = new QDeclarativeOpenMetaObject(this); } +} -Q_SIGNALS: - void onPathChanged(); - -private: - QDeclarativeOpenMetaObject *mo; - bool onPath; -}; +QDeclarativePathViewAttached::~QDeclarativePathViewAttached() +{ +} +QVariant QDeclarativePathViewAttached::value(const QByteArray &name) const +{ + return m_metaobject->value(name); +} +void QDeclarativePathViewAttached::setValue(const QByteArray &name, const QVariant &val) +{ + m_metaobject->setValue(name, val); +} QDeclarativeItem *QDeclarativePathViewPrivate::getItem(int modelIndex) { @@ -113,8 +95,20 @@ QDeclarativeItem *QDeclarativePathViewPrivate::getItem(int modelIndex) requestedIndex = modelIndex; QDeclarativeItem *item = model->item(modelIndex, false); if (item) { - if (QObject *obj = QDeclarativePathView::qmlAttachedProperties(item)) - static_cast(obj)->setOnPath(true); + 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()) { + attType->createProperty(attr.toUtf8()); + } + } + qPathViewAttachedType = attType; + QDeclarativePathViewAttached *att = static_cast(qmlAttachedPropertiesObject(item)); + qPathViewAttachedType = 0; + if (att) { + att->m_view = q; + att->setOnPath(true); + } item->setParentItem(q); } requestedIndex = -1; @@ -125,14 +119,26 @@ void QDeclarativePathViewPrivate::releaseItem(QDeclarativeItem *item) { if (!item || !model) return; - if (QObject *obj = QDeclarativePathView::qmlAttachedProperties(item)) - static_cast(obj)->setOnPath(false); - if (model->release(item) == 0) { - if (QObject *obj = QDeclarativePathView::qmlAttachedProperties(item)) - static_cast(obj)->setOnPath(false); + if (QDeclarativePathViewAttached *att = attached(item)) + att->setOnPath(false); + model->release(item); +} + +QDeclarativePathViewAttached *QDeclarativePathViewPrivate::attached(QDeclarativeItem *item) +{ + return static_cast(qmlAttachedPropertiesObject(item, false)); +} + +void QDeclarativePathViewPrivate::clear() +{ + for (int i=0; iclear(); + if (d->attType) + d->attType->release(); if (d->ownModel) delete d->model; } @@ -185,6 +199,15 @@ QDeclarativePathView::~QDeclarativePathView() */ /*! + \qmlattachedproperty bool PathView::isCurrentItem + This attached property is true if this delegate is the current item; otherwise false. + + It is attached to each instance of the delegate. + + This property may be used to adjust the appearance of the current item. +*/ + +/*! \qmlproperty model PathView::model This property holds the model providing data for the view. @@ -275,8 +298,15 @@ void QDeclarativePathView::setPath(QDeclarativePath *path) Q_D(QDeclarativePathView); if (d->path == path) return; + if (d->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; + } d->regenerate(); emit pathChanged(); } @@ -297,12 +327,25 @@ void QDeclarativePathView::setCurrentIndex(int idx) if (d->model && d->model->count()) idx = qAbs(idx % d->model->count()); if (d->model && idx != d->currentIndex) { + if (d->model->count()) { + int itemIndex = (d->currentIndex - d->firstIndex + d->model->count()) % d->model->count(); + if (itemIndex < d->items.count()) { + if (QDeclarativeItem *item = d->items.at(d->currentIndex)) { + if (QDeclarativePathViewAttached *att = d->attached(item)) + att->setIsCurrentItem(false); + } + } + } d->currentIndex = idx; if (d->model->count()) { d->snapToCurrent(); int itemIndex = (idx - d->firstIndex + d->model->count()) % d->model->count(); - if (itemIndex < d->items.count()) - d->items.at(itemIndex)->setFocus(true); + if (itemIndex < d->items.count()) { + QDeclarativeItem *item = d->items.at(itemIndex); + item->setFocus(true); + if (QDeclarativePathViewAttached *att = d->attached(item)) + att->setIsCurrentItem(true); + } } emit currentIndexChanged(); } @@ -649,11 +692,7 @@ void QDeclarativePathViewPrivate::regenerate() if (!q->isComponentComplete()) return; - for (int i=0; isetZValue(i); + qreal percent = i * (100. / numItems) + _offset; + percent = qAbs(qmlMod(percent, qreal(100.0))/100.0); + updateItem(item, percent); model->completeItem(); - if (currentIndex == index) + if (currentIndex == index) { item->setFocus(true); + if (QDeclarativePathViewAttached *att = attached(item)) + att->setIsCurrentItem(true); + } } - q->refill(); + if (pathItems != -1) + q->refill(); } void QDeclarativePathViewPrivate::updateItem(QDeclarativeItem *item, qreal percent) { - if (QObject *obj = QDeclarativePathView::qmlAttachedProperties(item)) { + if (QDeclarativePathViewAttached *att = attached(item)) { foreach(const QString &attr, path->attributes()) - static_cast(obj)->setValue(attr.toUtf8(), path->attributeAt(attr, percent)); + att->setValue(attr.toUtf8(), path->attributeAt(attr, percent)); } QPointF pf = path->pointAt(percent); item->setX(pf.x() - item->width()*item->scale()/2); @@ -735,8 +781,11 @@ void QDeclarativePathView::refill() QDeclarativeItem *item = d->getItem(index); item->setZValue(wrapIndex); d->model->completeItem(); - if (d->currentIndex == index) + if (d->currentIndex == index) { item->setFocus(true); + if (QDeclarativePathViewAttached *att = d->attached(item)) + att->setIsCurrentItem(true); + } d->items << item; d->pathOffset++; d->pathOffset=d->pathOffset % d->items.count(); @@ -752,8 +801,11 @@ void QDeclarativePathView::refill() QDeclarativeItem *item = d->getItem(d->firstIndex); item->setZValue(d->firstIndex); d->model->completeItem(); - if (d->currentIndex == d->firstIndex) + if (d->currentIndex == d->firstIndex) { item->setFocus(true); + if (QDeclarativePathViewAttached *att = d->attached(item)) + att->setIsCurrentItem(true); + } d->items.prepend(item); d->pathOffset--; if (d->pathOffset < 0) @@ -909,10 +961,21 @@ void QDeclarativePathViewPrivate::updateCurrent() return; int idx = calcCurrentIndex(); if (model && idx != currentIndex) { + int itemIndex = (currentIndex - firstIndex + model->count()) % model->count(); + if (itemIndex < items.count()) { + if (QDeclarativeItem *item = items.at(itemIndex)) { + if (QDeclarativePathViewAttached *att = attached(item)) + att->setIsCurrentItem(false); + } + } currentIndex = idx; - int itemIndex = (idx - firstIndex + model->count()) % model->count(); - if (itemIndex < items.count()) - items.at(itemIndex)->setFocus(true); + itemIndex = (idx - firstIndex + model->count()) % model->count(); + if (itemIndex < items.count()) { + QDeclarativeItem *item = items.at(itemIndex); + item->setFocus(true); + if (QDeclarativePathViewAttached *att = attached(item)) + att->setIsCurrentItem(true); + } emit q->currentIndexChanged(); } } @@ -993,17 +1056,10 @@ void QDeclarativePathViewPrivate::snapToCurrent() } } -QHash QDeclarativePathView::attachedProperties; -QObject *QDeclarativePathView::qmlAttachedProperties(QObject *obj) +QDeclarativePathViewAttached *QDeclarativePathView::qmlAttachedProperties(QObject *obj) { - QObject *rv = attachedProperties.value(obj); - if (!rv) { - rv = new QDeclarativePathViewAttached(obj); - attachedProperties.insert(obj, rv); - } - return rv; + return new QDeclarativePathViewAttached(obj); } QT_END_NAMESPACE -#include diff --git a/src/declarative/graphicsitems/qdeclarativepathview_p.h b/src/declarative/graphicsitems/qdeclarativepathview_p.h index df9c6ae..6dbd044 100644 --- a/src/declarative/graphicsitems/qdeclarativepathview_p.h +++ b/src/declarative/graphicsitems/qdeclarativepathview_p.h @@ -52,6 +52,7 @@ QT_BEGIN_NAMESPACE QT_MODULE(Declarative) class QDeclarativePathViewPrivate; +class QDeclarativePathViewAttached; class Q_DECLARATIVE_EXPORT QDeclarativePathView : public QDeclarativeItem { Q_OBJECT @@ -96,7 +97,7 @@ public: int pathItemCount() const; void setPathItemCount(int); - static QObject *qmlAttachedProperties(QObject *); + static QDeclarativePathViewAttached *qmlAttachedProperties(QObject *); Q_SIGNALS: void currentIndexChanged(); @@ -127,11 +128,57 @@ private Q_SLOTS: private: friend class QDeclarativePathViewAttached; - static QHash attachedProperties; Q_DISABLE_COPY(QDeclarativePathView) Q_DECLARE_PRIVATE_D(QGraphicsItem::d_ptr.data(), QDeclarativePathView) }; +class QDeclarativeOpenMetaObject; +class QDeclarativePathViewAttached : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QDeclarativePathView *view READ view CONSTANT) + Q_PROPERTY(bool isCurrentItem READ isCurrentItem NOTIFY currentItemChanged) + Q_PROPERTY(bool onPath READ isOnPath NOTIFY pathChanged) + +public: + QDeclarativePathViewAttached(QObject *parent); + ~QDeclarativePathViewAttached(); + + QDeclarativePathView *view() { return m_view; } + + bool isCurrentItem() const { return m_isCurrent; } + void setIsCurrentItem(bool c) { + if (m_isCurrent != c) { + m_isCurrent = c; + emit currentItemChanged(); + } + } + + QVariant value(const QByteArray &name) const; + void setValue(const QByteArray &name, const QVariant &val); + + bool isOnPath() const { return m_onPath; } + void setOnPath(bool on) { + if (on != m_onPath) { + m_onPath = on; + emit pathChanged(); + } + } + +Q_SIGNALS: + void currentItemChanged(); + void pathChanged(); + +private: + friend class QDeclarativePathViewPrivate; + QDeclarativePathView *m_view; + QDeclarativeOpenMetaObject *m_metaobject; + bool m_onPath : 1; + bool m_isCurrent : 1; +}; + + QT_END_NAMESPACE QML_DECLARE_TYPE(QDeclarativePathView) diff --git a/src/declarative/graphicsitems/qdeclarativepathview_p_p.h b/src/declarative/graphicsitems/qdeclarativepathview_p_p.h index 6344a8a..4083ab5 100644 --- a/src/declarative/graphicsitems/qdeclarativepathview_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativepathview_p_p.h @@ -71,6 +71,8 @@ typedef struct PathViewItem{ QDeclarativeItem* item; }PathViewItem; +class QDeclarativeOpenMetaObjectType; +class QDeclarativePathViewAttached; class QDeclarativePathViewPrivate : public QDeclarativeItemPrivate { Q_DECLARE_PUBLIC(QDeclarativePathView) @@ -81,7 +83,7 @@ public: , lastElapsed(0), stealMouse(false), ownModel(false), activeItem(0) , snapPos(0), dragMargin(0), moveOffset(this, &QDeclarativePathViewPrivate::setOffset) , firstIndex(0), pathItems(-1), pathOffset(0), requestedIndex(-1) - , moveReason(Other) + , moveReason(Other), attType(0) { } @@ -97,6 +99,8 @@ public: QDeclarativeItem *getItem(int modelIndex); void releaseItem(QDeclarativeItem *item); + QDeclarativePathViewAttached *attached(QDeclarativeItem *item); + void clear(); bool isValid() const { return model && model->count() > 0 && model->isValid() && path; @@ -137,6 +141,7 @@ public: QVariant modelVariant; enum MovementReason { Other, Key, Mouse }; MovementReason moveReason; + QDeclarativeOpenMetaObjectType *attType; }; QT_END_NAMESPACE diff --git a/tests/auto/declarative/qdeclarativepathview/data/pathview.qml b/tests/auto/declarative/qdeclarativepathview/data/pathview.qml index c5d88cd..ae0c86a 100644 --- a/tests/auto/declarative/qdeclarativepathview/data/pathview.qml +++ b/tests/auto/declarative/qdeclarativepathview/data/pathview.qml @@ -1,6 +1,9 @@ import Qt 4.6 Rectangle { + id: root + property int currentA: -1 + property int currentB: -1 width: 240 height: 320 color: "#ffffff" @@ -12,7 +15,7 @@ Rectangle { objectName: "wrapper" height: 20 width: 60 - color: "white" + color: PathView.isCurrentItem ? "lightsteelblue" : "white" border.color: "black" Text { text: index @@ -29,6 +32,12 @@ Rectangle { objectName: "textNumber" text: number } + PathView.onCurrentItemChanged: { + if (PathView.isCurrentItem) { + root.currentA = index; + root.currentB = wrapper.PathView.view.currentIndex; + } + } } } ] diff --git a/tests/auto/declarative/qdeclarativepathview/tst_qdeclarativepathview.cpp b/tests/auto/declarative/qdeclarativepathview/tst_qdeclarativepathview.cpp index fa4e9d3..62eb8c3 100644 --- a/tests/auto/declarative/qdeclarativepathview/tst_qdeclarativepathview.cpp +++ b/tests/auto/declarative/qdeclarativepathview/tst_qdeclarativepathview.cpp @@ -71,6 +71,7 @@ private slots: void pathview3(); void path(); void pathMoved(); + void setCurrentIndex(); void resetModel(); void propertyChanges(); void pathChanges(); @@ -207,6 +208,7 @@ void tst_QDeclarativePathView::items() model.addItem("Fred", "12345"); model.addItem("John", "2345"); model.addItem("Bob", "54321"); + model.addItem("Bill", "4321"); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); @@ -421,7 +423,6 @@ void tst_QDeclarativePathView::pathMoved() offset.setY(firstItem->height()/2); QCOMPARE(firstItem->pos() + offset, start); pathview->setOffset(10); - QTest::qWait(1000);//Moving is animated? for(int i=0; i(pathview, "wrapper", i); @@ -429,12 +430,53 @@ void tst_QDeclarativePathView::pathMoved() } pathview->setOffset(100); - QTest::qWait(1000);//Moving is animated? QCOMPARE(firstItem->pos() + offset, start); delete canvas; } +void tst_QDeclarativePathView::setCurrentIndex() +{ + QDeclarativeView *canvas = createView(); + + TestModel model; + model.addItem("Ben", "12345"); + model.addItem("Bohn", "2345"); + model.addItem("Bob", "54321"); + model.addItem("Bill", "4321"); + + QDeclarativeContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pathview.qml")); + qApp->processEvents(); + + QDeclarativePathView *pathview = findItem(canvas->rootObject(), "view"); + QVERIFY(pathview != 0); + + QDeclarativeRectangle *firstItem = findItem(pathview, "wrapper", 0); + QVERIFY(firstItem); + QDeclarativePath *path = qobject_cast(pathview->path()); + QVERIFY(path); + QPointF start = path->pointAt(0.0); + QPointF offset;//Center of item is at point, but pos is from corner + offset.setX(firstItem->width()/2); + offset.setY(firstItem->height()/2); + QCOMPARE(firstItem->pos() + offset, start); + QCOMPARE(canvas->rootObject()->property("currentA").toInt(), 0); + QCOMPARE(canvas->rootObject()->property("currentB").toInt(), 0); + + pathview->setCurrentIndex(2); + QTest::qWait(1000); + + firstItem = findItem(pathview, "wrapper", 2); + QCOMPARE(firstItem->pos() + offset, start); + QCOMPARE(canvas->rootObject()->property("currentA").toInt(), 2); + QCOMPARE(canvas->rootObject()->property("currentB").toInt(), 2); + + delete canvas; +} + void tst_QDeclarativePathView::resetModel() { QDeclarativeView *canvas = createView(); -- cgit v0.12