From ae8e4afcadc9ff084e1d1859c29fbc8b629e3392 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Thu, 13 May 2010 15:07:21 +1000 Subject: Add an example spinner. Also add missing increment/decrementCurrentIndex() slots to PathView, and tweak the number of points cached along a Path. --- examples/declarative/declarative.pro | 1 + examples/declarative/spinner/content/Spinner.qml | 25 ++++++++++++++++ .../declarative/spinner/content/spinner-bg.png | Bin 0 -> 345 bytes .../declarative/spinner/content/spinner-select.png | Bin 0 -> 320 bytes examples/declarative/spinner/main.qml | 18 +++++++++++ examples/declarative/spinner/spinner.qmlproject | 16 ++++++++++ src/declarative/graphicsitems/qdeclarativepath.cpp | 4 ++- .../graphicsitems/qdeclarativepathview.cpp | 33 +++++++++++++++++++-- .../graphicsitems/qdeclarativepathview_p.h | 4 +++ .../tst_qdeclarativepathview.cpp | 30 +++++++++++++++++-- 10 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 examples/declarative/spinner/content/Spinner.qml create mode 100644 examples/declarative/spinner/content/spinner-bg.png create mode 100644 examples/declarative/spinner/content/spinner-select.png create mode 100644 examples/declarative/spinner/main.qml create mode 100644 examples/declarative/spinner/spinner.qmlproject diff --git a/examples/declarative/declarative.pro b/examples/declarative/declarative.pro index ba9b628..913b2b0 100644 --- a/examples/declarative/declarative.pro +++ b/examples/declarative/declarative.pro @@ -37,6 +37,7 @@ sources.files = \ scrollbar \ searchbox \ slideswitch \ + spinner \ sql \ states \ tabwidget \ diff --git a/examples/declarative/spinner/content/Spinner.qml b/examples/declarative/spinner/content/Spinner.qml new file mode 100644 index 0000000..8145a28 --- /dev/null +++ b/examples/declarative/spinner/content/Spinner.qml @@ -0,0 +1,25 @@ +import Qt 4.7 + +Image { + property alias model: view.model + property alias delegate: view.delegate + property alias currentIndex: view.currentIndex + property real itemHeight: 30 + source: "spinner-bg.png" + clip: true + PathView { + id: view + anchors.fill: parent + pathItemCount: height/itemHeight + preferredHighlightBegin: 0.5 + preferredHighlightEnd: 0.5 + highlight: Image { source: "spinner-select.png"; width: view.width; height: itemHeight+4 } + dragMargin: view.width/2 + path: Path { + startX: view.width/2; startY: -itemHeight/2 + PathLine { x: view.width/2; y: view.pathItemCount*itemHeight + itemHeight } + } + } + Keys.onDownPressed: view.incrementCurrentIndex() + Keys.onUpPressed: view.decrementCurrentIndex() +} diff --git a/examples/declarative/spinner/content/spinner-bg.png b/examples/declarative/spinner/content/spinner-bg.png new file mode 100644 index 0000000..b3556f1 Binary files /dev/null and b/examples/declarative/spinner/content/spinner-bg.png differ diff --git a/examples/declarative/spinner/content/spinner-select.png b/examples/declarative/spinner/content/spinner-select.png new file mode 100644 index 0000000..95a17a1 Binary files /dev/null and b/examples/declarative/spinner/content/spinner-select.png differ diff --git a/examples/declarative/spinner/main.qml b/examples/declarative/spinner/main.qml new file mode 100644 index 0000000..6be567a --- /dev/null +++ b/examples/declarative/spinner/main.qml @@ -0,0 +1,18 @@ +import Qt 4.7 +import "content" + +Rectangle { + width: 240; height: 320 + Column { + y: 20; x: 20; spacing: 20 + Spinner { + id: spinner + width: 200; height: 240 + focus: true + model: 20 + itemHeight: 30 + delegate: Text { font.pixelSize: 25; text: index; height: 30 } + } + Text { text: "Current item index: " + spinner.currentIndex } + } +} diff --git a/examples/declarative/spinner/spinner.qmlproject b/examples/declarative/spinner/spinner.qmlproject new file mode 100644 index 0000000..d4909f8 --- /dev/null +++ b/examples/declarative/spinner/spinner.qmlproject @@ -0,0 +1,16 @@ +import QmlProject 1.0 + +Project { + /* Include .qml, .js, and image files from current directory and subdirectories */ + QmlFiles { + directory: "." + } + JavaScriptFiles { + directory: "." + } + ImageFiles { + directory: "." + } + /* List of plugin directories passed to QML runtime */ + // importPaths: [ " ../exampleplugin " ] +} diff --git a/src/declarative/graphicsitems/qdeclarativepath.cpp b/src/declarative/graphicsitems/qdeclarativepath.cpp index 3d0df87..2d08c7c 100644 --- a/src/declarative/graphicsitems/qdeclarativepath.cpp +++ b/src/declarative/graphicsitems/qdeclarativepath.cpp @@ -377,7 +377,9 @@ void QDeclarativePath::createPointCache() const { Q_D(const QDeclarativePath); qreal pathLength = d->_path.length(); - const int points = int(pathLength*2); + // more points means less jitter between items as they move along the + // path, but takes longer to generate + const int points = int(pathLength*5); const int lastElement = d->_path.elementCount() - 1; d->_pointCache.resize(points+1); diff --git a/src/declarative/graphicsitems/qdeclarativepathview.cpp b/src/declarative/graphicsitems/qdeclarativepathview.cpp index 503d096..207cc25 100644 --- a/src/declarative/graphicsitems/qdeclarativepathview.cpp +++ b/src/declarative/graphicsitems/qdeclarativepathview.cpp @@ -49,6 +49,7 @@ #include #include +#include #include QT_BEGIN_NAMESPACE @@ -279,8 +280,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() @@ -527,6 +528,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. @@ -1312,6 +1340,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; diff --git a/src/declarative/graphicsitems/qdeclarativepathview_p.h b/src/declarative/graphicsitems/qdeclarativepathview_p.h index 85f47fd..349a01c 100644 --- a/src/declarative/graphicsitems/qdeclarativepathview_p.h +++ b/src/declarative/graphicsitems/qdeclarativepathview_p.h @@ -132,6 +132,10 @@ public: static QDeclarativePathViewAttached *qmlAttachedProperties(QObject *); +public Q_SLOTS: + void incrementCurrentIndex(); + void decrementCurrentIndex(); + Q_SIGNALS: void currentIndexChanged(); void offsetChanged(); diff --git a/tests/auto/declarative/qdeclarativepathview/tst_qdeclarativepathview.cpp b/tests/auto/declarative/qdeclarativepathview/tst_qdeclarativepathview.cpp index 62d0b89..0e16f66 100644 --- a/tests/auto/declarative/qdeclarativepathview/tst_qdeclarativepathview.cpp +++ b/tests/auto/declarative/qdeclarativepathview/tst_qdeclarativepathview.cpp @@ -438,7 +438,8 @@ void tst_QDeclarativePathView::pathMoved() for(int i=0; i(pathview, "wrapper", i); - QCOMPARE(curItem->pos() + offset, path->pointAt(0.25 + i*0.25)); + QPointF itemPos(path->pointAt(0.25 + i*0.25)); + QCOMPARE(curItem->pos() + offset, QPointF(qRound(itemPos.x()), qRound(itemPos.y()))); } pathview->setOffset(0.0); @@ -479,13 +480,36 @@ void tst_QDeclarativePathView::setCurrentIndex() QCOMPARE(canvas->rootObject()->property("currentB").toInt(), 0); pathview->setCurrentIndex(2); - QTest::qWait(1000); firstItem = findItem(pathview, "wrapper", 2); - QCOMPARE(firstItem->pos() + offset, start); + QTRY_COMPARE(firstItem->pos() + offset, start); QCOMPARE(canvas->rootObject()->property("currentA").toInt(), 2); QCOMPARE(canvas->rootObject()->property("currentB").toInt(), 2); + pathview->decrementCurrentIndex(); + QTRY_COMPARE(pathview->currentIndex(), 1); + firstItem = findItem(pathview, "wrapper", 1); + QVERIFY(firstItem); + QTRY_COMPARE(firstItem->pos() + offset, start); + + pathview->decrementCurrentIndex(); + QTRY_COMPARE(pathview->currentIndex(), 0); + firstItem = findItem(pathview, "wrapper", 0); + QVERIFY(firstItem); + QTRY_COMPARE(firstItem->pos() + offset, start); + + pathview->decrementCurrentIndex(); + QTRY_COMPARE(pathview->currentIndex(), 3); + firstItem = findItem(pathview, "wrapper", 3); + QVERIFY(firstItem); + QTRY_COMPARE(firstItem->pos() + offset, start); + + pathview->incrementCurrentIndex(); + QTRY_COMPARE(pathview->currentIndex(), 0); + firstItem = findItem(pathview, "wrapper", 0); + QVERIFY(firstItem); + QTRY_COMPARE(firstItem->pos() + offset, start); + delete canvas; } -- cgit v0.12