From c452c44d8cee138dcdd62f8c0f0db48b4ada5c40 Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Thu, 3 Sep 2009 10:52:45 +1000 Subject: Fix a bug in the qmlvme, which lead to an assert on valid QML. As QmlComponents are created outside the CreateObject instruction, they could be created with an uninitalized declarative data member. Also this patch sets the column on CreateObject instructions too. Reviewed-by: Aaron Kennedy --- src/declarative/qml/qmlcompiler.cpp | 2 ++ src/declarative/qml/qmlinstruction_p.h | 1 + src/declarative/qml/qmlvme.cpp | 8 ++++++++ 3 files changed, 11 insertions(+) diff --git a/src/declarative/qml/qmlcompiler.cpp b/src/declarative/qml/qmlcompiler.cpp index c0f7bfd..c411b8d 100644 --- a/src/declarative/qml/qmlcompiler.cpp +++ b/src/declarative/qml/qmlcompiler.cpp @@ -781,6 +781,7 @@ void QmlCompiler::genObject(QmlParser::Object *obj) QmlInstruction create; create.type = QmlInstruction::CreateObject; create.line = obj->location.start.line; + create.create.column = obj->location.start.column; create.create.data = -1; if (!obj->custom.isEmpty()) create.create.data = output->indexForByteArray(obj->custom); @@ -937,6 +938,7 @@ void QmlCompiler::genComponent(QmlParser::Object *obj) QmlInstruction create; create.type = QmlInstruction::CreateComponent; create.line = root->location.start.line; + create.createComponent.column = root->location.start.column; create.createComponent.endLine = root->location.end.line; output->bytecode << create; int count = output->bytecode.count(); diff --git a/src/declarative/qml/qmlinstruction_p.h b/src/declarative/qml/qmlinstruction_p.h index ede06a2..3c6af1b 100644 --- a/src/declarative/qml/qmlinstruction_p.h +++ b/src/declarative/qml/qmlinstruction_p.h @@ -284,6 +284,7 @@ public: } assignSignalObject; struct { int count; + ushort column; int endLine; int metaObject; } createComponent; diff --git a/src/declarative/qml/qmlvme.cpp b/src/declarative/qml/qmlvme.cpp index a49cbd3..4d133e3 100644 --- a/src/declarative/qml/qmlvme.cpp +++ b/src/declarative/qml/qmlvme.cpp @@ -222,6 +222,14 @@ QObject *QmlVME::run(QStack &stack, QmlContext *ctxt, QmlCompiledData case QmlInstruction::CreateComponent: { QObject *qcomp = new QmlComponent(ctxt->engine(), comp, ii + 1, instr.createComponent.count, stack.isEmpty() ? 0 : stack.top()); + + QmlEngine::setContextForObject(qcomp, ctxt); + QmlDeclarativeData *ddata = QmlDeclarativeData::get(qcomp); + Q_ASSERT(ddata); + ddata->outerContext = ctxt; + ddata->lineNumber = instr.line; + ddata->columnNumber = instr.create.column; + stack.push(qcomp); ii += instr.createComponent.count; } -- cgit v0.12 From 369509bd4c1caeda50d3e6e15f98ffafb136a17e Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Thu, 3 Sep 2009 11:22:48 +1000 Subject: Get animation autotests working again, and add a few more. --- src/declarative/util/qmlanimation.h | 10 +- .../declarative/animations/data/badproperty1.qml | 6 +- .../auto/declarative/animations/data/badtype1.qml | 4 +- .../auto/declarative/animations/data/badtype2.qml | 4 +- .../auto/declarative/animations/data/badtype3.qml | 4 +- .../auto/declarative/animations/data/badtype4.qml | 9 +- tests/auto/declarative/animations/data/color.qml | 12 -- .../declarative/animations/data/dotproperty.qml | 6 +- .../declarative/animations/data/mixedtype1.qml | 8 +- .../declarative/animations/data/mixedtype2.qml | 6 +- tests/auto/declarative/animations/data/number.qml | 12 -- .../auto/declarative/animations/tst_animations.cpp | 136 +++++++++++++++++---- 12 files changed, 138 insertions(+), 79 deletions(-) delete mode 100644 tests/auto/declarative/animations/data/color.qml delete mode 100644 tests/auto/declarative/animations/data/number.qml diff --git a/src/declarative/util/qmlanimation.h b/src/declarative/util/qmlanimation.h index a898be8..7104f60 100644 --- a/src/declarative/util/qmlanimation.h +++ b/src/declarative/util/qmlanimation.h @@ -58,7 +58,7 @@ QT_MODULE(Declarative) class QmlAbstractAnimationPrivate; class QmlAnimationGroup; -class QmlAbstractAnimation : public QObject, public QmlPropertyValueSource, public QmlParserStatus +class Q_AUTOTEST_EXPORT QmlAbstractAnimation : public QObject, public QmlPropertyValueSource, public QmlParserStatus { Q_OBJECT Q_DECLARE_PRIVATE(QmlAbstractAnimation) @@ -68,8 +68,6 @@ class QmlAbstractAnimation : public QObject, public QmlPropertyValueSource, publ Q_PROPERTY(bool paused READ isPaused WRITE setPaused NOTIFY pausedChanged) Q_PROPERTY(bool alwaysRunToEnd READ alwaysRunToEnd WRITE setAlwaysRunToEnd NOTIFY alwaysRunToEndChanged()) Q_PROPERTY(bool repeat READ repeat WRITE setRepeat NOTIFY repeatChanged) - //Q_PROPERTY(QObject *target READ target WRITE setTarget NOTIFY targetChanged) - //Q_PROPERTY(QString property READ property WRITE setProperty NOTIFY targetChanged) Q_CLASSINFO("DefaultMethod", "start()") Q_INTERFACES(QmlParserStatus) @@ -252,7 +250,7 @@ protected: }; class QmlPropertyAnimationPrivate; -class QmlPropertyAnimation : public QmlAbstractAnimation +class Q_AUTOTEST_EXPORT QmlPropertyAnimation : public QmlAbstractAnimation { Q_OBJECT Q_DECLARE_PRIVATE(QmlPropertyAnimation) @@ -304,7 +302,7 @@ Q_SIGNALS: void propertiesChanged(const QString &); }; -class QmlColorAnimation : public QmlPropertyAnimation +class Q_AUTOTEST_EXPORT QmlColorAnimation : public QmlPropertyAnimation { Q_OBJECT Q_DECLARE_PRIVATE(QmlPropertyAnimation) @@ -322,7 +320,7 @@ public: void setTo(const QColor &); }; -class QmlNumberAnimation : public QmlPropertyAnimation +class Q_AUTOTEST_EXPORT QmlNumberAnimation : public QmlPropertyAnimation { Q_OBJECT Q_DECLARE_PRIVATE(QmlPropertyAnimation) diff --git a/tests/auto/declarative/animations/data/badproperty1.qml b/tests/auto/declarative/animations/data/badproperty1.qml index b88cd6f..a01753e 100644 --- a/tests/auto/declarative/animations/data/badproperty1.qml +++ b/tests/auto/declarative/animations/data/badproperty1.qml @@ -1,10 +1,10 @@ import Qt 4.6 -Rect { +Rectangle { id: Wrapper width: 240 height: 320 - Rect { + Rectangle { id: MyRect color: "red" width: 50; height: 50 @@ -16,7 +16,7 @@ Rect { } states: State { name: "state1" - SetProperties { target: MyRect; pen.color: "blue" } + PropertyChanges { target: MyRect; pen.color: "blue" } } transitions: Transition { ColorAnimation { target: MyRect; to: "red"; properties: "pen.colr"; duration: 1000 } diff --git a/tests/auto/declarative/animations/data/badtype1.qml b/tests/auto/declarative/animations/data/badtype1.qml index 00db39e..1e689d5 100644 --- a/tests/auto/declarative/animations/data/badtype1.qml +++ b/tests/auto/declarative/animations/data/badtype1.qml @@ -1,9 +1,9 @@ import Qt 4.6 -Rect { +Rectangle { width: 240 height: 320 - Rect { + Rectangle { color: "red" width: 50; height: 50 x: 100; y: 100 diff --git a/tests/auto/declarative/animations/data/badtype2.qml b/tests/auto/declarative/animations/data/badtype2.qml index eeaaefc..e97194d 100644 --- a/tests/auto/declarative/animations/data/badtype2.qml +++ b/tests/auto/declarative/animations/data/badtype2.qml @@ -1,9 +1,9 @@ import Qt 4.6 -Rect { +Rectangle { width: 240 height: 320 - Rect { + Rectangle { color: "red" width: 50; height: 50 x: 100; y: 100 diff --git a/tests/auto/declarative/animations/data/badtype3.qml b/tests/auto/declarative/animations/data/badtype3.qml index cbb1a4d..b2d738f 100644 --- a/tests/auto/declarative/animations/data/badtype3.qml +++ b/tests/auto/declarative/animations/data/badtype3.qml @@ -1,9 +1,9 @@ import Qt 4.6 -Rect { +Rectangle { width: 240 height: 320 - Rect { + Rectangle { color: "red" color: ColorAnimation { from: 10; to: 15; running: true; } width: 50; height: 50 diff --git a/tests/auto/declarative/animations/data/badtype4.qml b/tests/auto/declarative/animations/data/badtype4.qml index 3e046fc..0c0a636 100644 --- a/tests/auto/declarative/animations/data/badtype4.qml +++ b/tests/auto/declarative/animations/data/badtype4.qml @@ -1,10 +1,10 @@ import Qt 4.6 -Rect { +Rectangle { id: Wrapper width: 240 height: 320 - Rect { + Rectangle { id: MyRect color: "red" width: 50; height: 50 @@ -16,10 +16,11 @@ Rect { } states: State { name: "state1" - SetProperties { target: MyRect; x: 200; color: "blue" } + PropertyChanges { target: MyRect; x: 200; color: "blue" } } transitions: Transition { + //comment out each in turn to make sure each only animates the relevant property ColorAnimation { properties: "x,color"; duration: 1000 } //x is real, color is color - //NumberAnimation { properties: "x,color"; duration: 1000 } //x is real, color is color + NumberAnimation { properties: "x,color"; duration: 1000 } //x is real, color is color } } diff --git a/tests/auto/declarative/animations/data/color.qml b/tests/auto/declarative/animations/data/color.qml deleted file mode 100644 index 8a9ab8a..0000000 --- a/tests/auto/declarative/animations/data/color.qml +++ /dev/null @@ -1,12 +0,0 @@ -import Qt 4.6 - -Rect { - width: 240 - height: 320 - Rect { - color: "red" - color: PropertyAnimation { to: "green"; running: true } - width: 50; height: 50 - x: 100; y: 100 - } -} diff --git a/tests/auto/declarative/animations/data/dotproperty.qml b/tests/auto/declarative/animations/data/dotproperty.qml index 25076b8..ee076c2 100644 --- a/tests/auto/declarative/animations/data/dotproperty.qml +++ b/tests/auto/declarative/animations/data/dotproperty.qml @@ -1,10 +1,10 @@ import Qt 4.6 -Rect { +Rectangle { id: Wrapper width: 240 height: 320 - Rect { + Rectangle { id: MyRect color: "red" width: 50; height: 50 @@ -16,7 +16,7 @@ Rect { } states: State { name: "state1" - SetProperties { target: MyRect; pen.color: "blue" } + PropertyChanges { target: MyRect; pen.color: "blue" } } transitions: Transition { ColorAnimation { properties: "pen.color"; duration: 1000 } diff --git a/tests/auto/declarative/animations/data/mixedtype1.qml b/tests/auto/declarative/animations/data/mixedtype1.qml index 8b8262b..ed50582 100644 --- a/tests/auto/declarative/animations/data/mixedtype1.qml +++ b/tests/auto/declarative/animations/data/mixedtype1.qml @@ -1,10 +1,10 @@ import Qt 4.6 -Rect { +Rectangle { id: Wrapper width: 240 height: 320 - Rect { + Rectangle { id: MyRect color: "red" width: 50; height: 50 @@ -16,9 +16,9 @@ Rect { } states: State { name: "state1" - SetProperties { target: MyRect; x: 200; width: 40 } + PropertyChanges { target: MyRect; x: 200; border.width: 10 } } transitions: Transition { - PropertyAnimation { properties: "x,width"; duration: 1000 } //x is real, width is int + PropertyAnimation { properties: "x,border.width"; duration: 1000 } //x is real, border.width is int } } diff --git a/tests/auto/declarative/animations/data/mixedtype2.qml b/tests/auto/declarative/animations/data/mixedtype2.qml index 95b8ed7..4854c2e 100644 --- a/tests/auto/declarative/animations/data/mixedtype2.qml +++ b/tests/auto/declarative/animations/data/mixedtype2.qml @@ -1,10 +1,10 @@ import Qt 4.6 -Rect { +Rectangle { id: Wrapper width: 240 height: 320 - Rect { + Rectangle { id: MyRect color: "red" width: 50; height: 50 @@ -16,7 +16,7 @@ Rect { } states: State { name: "state1" - SetProperties { target: MyRect; x: 200; color: "blue" } + PropertyChanges { target: MyRect; x: 200; color: "blue" } } transitions: Transition { PropertyAnimation { properties: "x,color"; duration: 1000 } //x is real, color is color diff --git a/tests/auto/declarative/animations/data/number.qml b/tests/auto/declarative/animations/data/number.qml deleted file mode 100644 index bde1168..0000000 --- a/tests/auto/declarative/animations/data/number.qml +++ /dev/null @@ -1,12 +0,0 @@ -import Qt 4.6 - -Rect { - width: 240 - height: 320 - Rect { - color: "red" - width: 50; height: 50 - x: 100; y: 100 - x: PropertyAnimation { from: 0; to: 200; duration: 1000; running: true } - } -} diff --git a/tests/auto/declarative/animations/tst_animations.cpp b/tests/auto/declarative/animations/tst_animations.cpp index 889d9b7..ff06a04 100644 --- a/tests/auto/declarative/animations/tst_animations.cpp +++ b/tests/auto/declarative/animations/tst_animations.cpp @@ -3,6 +3,7 @@ #include #include #include +#include class tst_animations : public QObject { @@ -11,11 +12,68 @@ public: tst_animations() {} private slots: + void simpleNumber(); + void simpleColor(); + void alwaysRunToEnd(); void badTypes(); void badProperties(); - //void mixedTypes(); + void mixedTypes(); }; +void tst_animations::simpleNumber() +{ + QFxRect rect; + QmlNumberAnimation animation; + animation.setTarget(&rect); + animation.setProperty("x"); + animation.setTo(200); + animation.start(); + QTest::qWait(animation.duration() + 50); + QCOMPARE(rect.x(), qreal(200)); + + rect.setX(0); + animation.start(); + animation.pause(); + animation.setCurrentTime(125); + QCOMPARE(rect.x(), qreal(100)); +} + +void tst_animations::simpleColor() +{ + QFxRect rect; + QmlColorAnimation animation; + animation.setTarget(&rect); + animation.setProperty("color"); + animation.setTo(QColor("red")); + animation.start(); + QTest::qWait(animation.duration() + 50); + QCOMPARE(rect.color(), QColor("red")); + + rect.setColor(QColor("blue")); + animation.start(); + animation.pause(); + animation.setCurrentTime(125); + QCOMPARE(rect.color(), QColor::fromRgbF(0.498039, 0, 0.498039, 1)); +} + +void tst_animations::alwaysRunToEnd() +{ + QFxRect rect; + QmlPropertyAnimation animation; + animation.setTarget(&rect); + animation.setProperty("x"); + animation.setTo(200); + animation.setDuration(1000); + animation.setRepeat(true); + animation.setAlwaysRunToEnd(true); + animation.start(); + QTest::qWait(1500); + animation.stop(); + QVERIFY(rect.x() != qreal(200)); + QTest::qWait(500 + 50); + QCOMPARE(rect.x(), qreal(200)); +} + void tst_animations::badTypes() { //don't crash @@ -50,47 +108,73 @@ void tst_animations::badTypes() QVERIFY(c.errors().count() == 1); QCOMPARE(c.errors().at(0).description(), QLatin1String("Invalid property assignment: color expected")); } + + //don't crash + { + QmlEngine engine; + QmlComponent c(&engine, QUrl("file://" SRCDIR "/data/badtype4.qml")); + QFxRect *rect = qobject_cast(c.create()); + QVERIFY(rect); + + rect->setState("state1"); + QTest::qWait(1000 + 50); + QFxRect *myRect = qobject_cast(rect->QGraphicsObject::children().at(3)); //### not robust + QVERIFY(myRect); + QCOMPARE(myRect->x(),qreal(200)); + } } void tst_animations::badProperties() { - //don't crash (should be runtime error) + //make sure we get a runtime error { - QFxView *view = new QFxView; - view->setUrl(QUrl("file://" SRCDIR "/data/badproperty1.qml")); - - view->execute(); - qApp->processEvents(); + QmlEngine engine; + QmlComponent c(&engine, QUrl("file://" SRCDIR "/data/badproperty1.qml")); + QFxRect *rect = qobject_cast(c.create()); + QVERIFY(rect); - delete view; + QTest::ignoreMessage(QtWarningMsg, "QML QmlColorAnimation (file://" SRCDIR "/data/badproperty1.qml:22:9) Cannot animate non-existant property \"pen.colr\" "); + rect->setState("state1"); } } -/*//test animating mixed types with property animation - //for example, int + real; color + real; etc +//test animating mixed types with property animation in a transition +//for example, int + real; color + real; etc void tst_animations::mixedTypes() { - //### this one isn't real robust because width will likely change to real as well + //assumes border.width stats a real -- not real robust { - QFxView *view = new QFxView; - view->setUrl(QUrl("file://" SRCDIR "/data/mixedtype1.qml")); - - view->execute(); - qApp->processEvents(); - - delete view; + QmlEngine engine; + QmlComponent c(&engine, QUrl("file://" SRCDIR "/data/mixedtype1.qml")); + QFxRect *rect = qobject_cast(c.create()); + QVERIFY(rect); + + rect->setState("state1"); + QTest::qWait(500); + QFxRect *myRect = qobject_cast(rect->QGraphicsObject::children().at(3)); //### not robust + QVERIFY(myRect); + + //rather inexact -- is there a better way? + QVERIFY(myRect->x() > 100 && myRect->x() < 200); + QVERIFY(myRect->border()->width() > 1 && myRect->border()->width() < 10); } { - QFxView *view = new QFxView; - view->setUrl(QUrl("file://" SRCDIR "/data/mixedtype2.qml")); - - view->execute(); - qApp->processEvents(); - - delete view; + QmlEngine engine; + QmlComponent c(&engine, QUrl("file://" SRCDIR "/data/mixedtype2.qml")); + QFxRect *rect = qobject_cast(c.create()); + QVERIFY(rect); + + rect->setState("state1"); + QTest::qWait(500); + QFxRect *myRect = qobject_cast(rect->QGraphicsObject::children().at(3)); //### not robust + QVERIFY(myRect); + + //rather inexact -- is there a better way? + QVERIFY(myRect->x() > 100 && myRect->x() < 200); + QVERIFY(myRect->color() != QColor("red") && myRect->color() != QColor("blue")); } -}*/ +} QTEST_MAIN(tst_animations) -- cgit v0.12 From 0a73357d3851b4eb20483a05f434455f96ded10b Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Thu, 3 Sep 2009 11:27:32 +1000 Subject: Add one more animation test. --- tests/auto/declarative/animations/tst_animations.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/auto/declarative/animations/tst_animations.cpp b/tests/auto/declarative/animations/tst_animations.cpp index ff06a04..8e1abc6 100644 --- a/tests/auto/declarative/animations/tst_animations.cpp +++ b/tests/auto/declarative/animations/tst_animations.cpp @@ -15,6 +15,7 @@ private slots: void simpleNumber(); void simpleColor(); void alwaysRunToEnd(); + void dotProperty(); void badTypes(); void badProperties(); void mixedTypes(); @@ -74,6 +75,24 @@ void tst_animations::alwaysRunToEnd() QCOMPARE(rect.x(), qreal(200)); } +void tst_animations::dotProperty() +{ + QFxRect rect; + QmlNumberAnimation animation; + animation.setTarget(&rect); + animation.setProperty("border.width"); + animation.setTo(10); + animation.start(); + QTest::qWait(animation.duration() + 50); + QCOMPARE(rect.border()->width(), 10); + + rect.border()->setWidth(1); + animation.start(); + animation.pause(); + animation.setCurrentTime(125); + QCOMPARE(rect.border()->width(), 5); +} + void tst_animations::badTypes() { //don't crash -- cgit v0.12 From d393e9794566578f4c7a34a00ee9b534337dfd3c Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Thu, 3 Sep 2009 11:32:53 +1000 Subject: Flickable visible area properties moved into visibleArea as dot props. --- examples/declarative/scrollbar/ScrollBar.qml | 4 +- examples/declarative/scrollbar/display.qml | 10 +- src/declarative/fx/qfxflickable.cpp | 215 +++++++++++++++++---------- src/declarative/fx/qfxflickable.h | 14 +- src/declarative/fx/qfxflickable_p.h | 8 +- 5 files changed, 154 insertions(+), 97 deletions(-) diff --git a/examples/declarative/scrollbar/ScrollBar.qml b/examples/declarative/scrollbar/ScrollBar.qml index c1c4a4f..b42563d 100644 --- a/examples/declarative/scrollbar/ScrollBar.qml +++ b/examples/declarative/scrollbar/ScrollBar.qml @@ -7,8 +7,8 @@ Item { // height of the page, i.e. a pageSize of 0.5 means that you can see 50% // of the height of the view. // orientation can be either 'Vertical' or 'Horizontal' - property var position - property var pageSize + property real position + property real pageSize property var orientation : "Vertical" // A light, semi-transparent background diff --git a/examples/declarative/scrollbar/display.qml b/examples/declarative/scrollbar/display.qml index 48cceb7..0180bda 100644 --- a/examples/declarative/scrollbar/display.qml +++ b/examples/declarative/scrollbar/display.qml @@ -38,22 +38,20 @@ Rectangle { id: SBV opacity: 0 orientation: "Vertical" - position: View.pageYPosition - pageSize: View.pageHeight + position: View.visibleArea.yPosition + pageSize: View.visibleArea.heightRatio width: 12 height: View.height-12 anchors.right: View.right } - /* ScrollBar { id: SBH opacity: 0 orientation: "Horizontal" - position: View.pageXPosition - pageSize: View.pageWidth + position: View.visibleArea.xPosition + pageSize: View.visibleArea.widthRatio height: 12 width: View.width-12 anchors.bottom: View.bottom } - */ } diff --git a/src/declarative/fx/qfxflickable.cpp b/src/declarative/fx/qfxflickable.cpp index 0690df1..16c7fff 100644 --- a/src/declarative/fx/qfxflickable.cpp +++ b/src/declarative/fx/qfxflickable.cpp @@ -92,13 +92,108 @@ void ElasticValue::updateCurrentTime(int) emit updated(); } + +class QFxFlickableVisibleArea : public QObject +{ + Q_OBJECT + + Q_PROPERTY(qreal xPosition READ xPosition NOTIFY pageChanged) + Q_PROPERTY(qreal yPosition READ yPosition NOTIFY pageChanged) + Q_PROPERTY(qreal widthRatio READ widthRatio NOTIFY pageChanged) + Q_PROPERTY(qreal heightRatio READ heightRatio NOTIFY pageChanged) + +public: + QFxFlickableVisibleArea(QFxFlickable *parent=0); + + qreal xPosition() const; + qreal widthRatio() const; + qreal yPosition() const; + qreal heightRatio() const; + + void updateVisible(); + +signals: + void pageChanged(); + +private: + QFxFlickable *flickable; + qreal m_xPosition; + qreal m_widthRatio; + qreal m_yPosition; + qreal m_heightRatio; +}; + +QFxFlickableVisibleArea::QFxFlickableVisibleArea(QFxFlickable *parent) + : QObject(parent), flickable(parent), m_xPosition(0.), m_widthRatio(0.) + , m_yPosition(0.), m_heightRatio(0.) +{ +} + +qreal QFxFlickableVisibleArea::widthRatio() const +{ + return m_widthRatio; +} + +qreal QFxFlickableVisibleArea::xPosition() const +{ + return m_xPosition; +} + +qreal QFxFlickableVisibleArea::heightRatio() const +{ + return m_heightRatio; +} + +qreal QFxFlickableVisibleArea::yPosition() const +{ + return m_yPosition; +} + +void QFxFlickableVisibleArea::updateVisible() +{ + QFxFlickablePrivate *p = static_cast(QGraphicsItemPrivate::get(flickable)); + bool pageChange = false; + + // Vertical + const qreal viewheight = flickable->height(); + const qreal maxyextent = -flickable->maxYExtent(); + qreal pagePos = -p->_moveY.value() / (maxyextent + viewheight); + qreal pageSize = viewheight / (maxyextent + viewheight); + + if (pageSize != m_heightRatio) { + m_heightRatio = pageSize; + pageChange = true; + } + if (pagePos != m_yPosition) { + m_yPosition = pagePos; + pageChange = true; + } + + // Horizontal + const qreal viewwidth = flickable->width(); + const qreal maxxextent = -flickable->maxXExtent(); + pagePos = -p->_moveX.value() / (maxxextent + viewwidth); + pageSize = viewwidth / (maxxextent + viewwidth); + + if (pageSize != m_widthRatio) { + m_widthRatio = pageSize; + pageChange = true; + } + if (pagePos != m_xPosition) { + m_xPosition = pagePos; + pageChange = true; + } + if (pageChange) + emit pageChanged(); +} + + QFxFlickablePrivate::QFxFlickablePrivate() : _flick(new QFxItem), _moveX(_flick, &QFxItem::setX), _moveY(_flick, &QFxItem::setY) , vWidth(-1), vHeight(-1), overShoot(true), flicked(false), moving(false), stealMouse(false) , pressed(false), maxVelocity(-1), interactive(true), dragMode(QFxFlickable::Hard) , elasticY(_moveY), elasticX(_moveX), reportedVelocitySmoothing(100), horizontalVelocity(this), verticalVelocity(this) - , vTime(0), atXEnd(false), atXBeginning(true), pageXPosition(0.), pageWidth(0.) - , atYEnd(false), atYBeginning(true), pageYPosition(0.), pageHeight(0.) + , vTime(0), atXEnd(false), atXBeginning(true), atYEnd(false), atYBeginning(true), visibleArea(0) { fixupXEvent = QmlTimeLineEvent::timeLineEvent(&_moveX, this); fixupYEvent = QmlTimeLineEvent::timeLineEvent(&_moveY, this); @@ -200,13 +295,13 @@ void QFxFlickablePrivate::fixupX() vTime = _tl.time(); - if (_moveX.value() > q->minXExtent() || (q->maxXExtent() > q->maxXExtent())) { - _tl.reset(_moveY); + if (_moveX.value() > q->minXExtent() || (q->maxXExtent() > q->minXExtent())) { + _tl.reset(_moveX); if (_moveX.value() != q->minXExtent()) _tl.move(_moveX, q->minXExtent(), QEasingCurve(QEasingCurve::InOutQuad), 200); //emit flickingChanged(); } else if (_moveX.value() < q->maxXExtent()) { - _tl.reset(_moveY); + _tl.reset(_moveX); _tl.move(_moveX, q->maxXExtent(), QEasingCurve(QEasingCurve::InOutQuad), 200); //emit flickingChanged(); } else { @@ -239,26 +334,14 @@ void QFxFlickablePrivate::fixupY() void QFxFlickablePrivate::updateBeginningEnd() { Q_Q(QFxFlickable); - bool pageChange = false; bool atBoundaryChange = false; // Vertical - const int viewheight = q->height(); const int maxyextent = int(-q->maxYExtent()); const qreal ypos = -_moveY.value(); - qreal pagePos = ((ypos * 100.0) / (maxyextent + viewheight)) / 100.0; - qreal pageSize = ((viewheight * 100.0) / (maxyextent + viewheight)) / 100.0; bool atBeginning = (ypos <= 0.0); bool atEnd = (maxyextent <= ypos); - if (pageSize != pageHeight) { - pageHeight = pageSize; - pageChange = true; - } - if (pagePos != pageYPosition) { - pageYPosition = pagePos; - pageChange = true; - } if (atBeginning != atYBeginning) { atYBeginning = atBeginning; atBoundaryChange = true; @@ -269,22 +352,11 @@ void QFxFlickablePrivate::updateBeginningEnd() } // Horizontal - const int viewwidth = q->width(); const int maxxextent = int(-q->maxXExtent()); const qreal xpos = -_moveX.value(); - pagePos = ((xpos * 100.0) / (maxxextent + viewwidth)) / 100.0; - pageSize = ((viewwidth * 100.0) / (maxxextent + viewwidth)) / 100.0; atBeginning = (xpos <= 0.0); atEnd = (maxxextent <= xpos); - if (pageSize != pageWidth) { - pageWidth = pageSize; - pageChange = true; - } - if (pagePos != pageXPosition) { - pageXPosition = pagePos; - pageChange = true; - } if (atBeginning != atXBeginning) { atXBeginning = atBeginning; atBoundaryChange = true; @@ -294,10 +366,11 @@ void QFxFlickablePrivate::updateBeginningEnd() atBoundaryChange = true; } - if (pageChange) - emit q->pageChanged(); if (atBoundaryChange) emit q->isAtBoundaryChanged(); + + if (visibleArea) + visibleArea->updateVisible(); } static const int FlickThreshold = 5; @@ -350,6 +423,27 @@ Flickable { */ +/*! + \qmlproperty real Flickable::visibleArea.xPosition + \qmlproperty real Flickable::visibleArea.widthRatio + \qmlproperty real Flickable::visibleArea.yPosition + \qmlproperty real Flickable::visibleArea.heightRatio + + These properties describe the position and size of the currently viewed area. + The size is defined as the percentage of the full view currently visible, + scaled to 0.0 - 1.0. The page position is in the range 0.0 (beginning) to + size ratio (end), i.e. yPosition is in the range 0.0 - heightRatio. + + These properties are typically used to draw a scrollbar, for example: + \code + Rectangle { + opacity: 0.5; anchors.right: MyListView.right-2; width: 6 + y: MyListView.visibleArea.yPosition * MyListView.height + height: MyListView.visibleArea.heightRatio * MyListView.height + } + \endcode +*/ + QFxFlickable::QFxFlickable(QFxItem *parent) : QFxItem(*(new QFxFlickablePrivate), parent) { @@ -514,50 +608,6 @@ bool QFxFlickable::isAtYBeginning() const return d->atYBeginning; } -/*! - \qmlproperty real Flickable::pageXPosition - \qmlproperty real Flickable::pageWidth - \qmlproperty real Flickable::pageYPosition - \qmlproperty real Flickable::pageHeight - - These properties describe the position and size of the currently viewed page. - The page size is defined as the percentage of the full view currently visible, - scaled to 0.0 - 1.0. The page position is also in the range 0.0 (beginning) to - 1.0 (end). - - These properties are typically used to draw a scrollbar, for example: - \code - Rectangle { - opacity: 0.5; anchors.right: MyListView.right-2; width: 6 - y: MyListView.pageYPosition * MyListView.height - height: MyListView.pageHeight * MyListView.height - } - \endcode -*/ -qreal QFxFlickable::pageWidth() const -{ - Q_D(const QFxFlickable); - return d->pageWidth; -} - -qreal QFxFlickable::pageXPosition() const -{ - Q_D(const QFxFlickable); - return d->pageXPosition; -} - -qreal QFxFlickable::pageHeight() const -{ - Q_D(const QFxFlickable); - return d->pageHeight; -} - -qreal QFxFlickable::pageYPosition() const -{ - Q_D(const QFxFlickable); - return d->pageYPosition; -} - void QFxFlickable::ticked() { viewportMoved(); @@ -581,6 +631,14 @@ qreal QFxFlickable::visibleY() const return -d->_moveY.value(); } +QFxFlickableVisibleArea *QFxFlickable::visibleArea() +{ + Q_D(QFxFlickable); + if (!d->visibleArea) + d->visibleArea = new QFxFlickableVisibleArea(this); + return d->visibleArea; +} + void QFxFlickablePrivate::handleMousePressEvent(QGraphicsSceneMouseEvent *event) { if (interactive && _tl.isActive() && (qAbs(velocityX) > 10 || qAbs(velocityY) > 10)) @@ -931,8 +989,8 @@ void QFxFlickable::widthChange() if (d->vWidth < 0) { d->_flick->setWidth(width()); emit viewportWidthChanged(); - d->updateBeginningEnd(); } + d->updateBeginningEnd(); } void QFxFlickable::heightChange() @@ -941,8 +999,8 @@ void QFxFlickable::heightChange() if (d->vHeight < 0) { d->_flick->setHeight(height()); emit viewportHeightChanged(); - d->updateBeginningEnd(); } + d->updateBeginningEnd(); } qreal QFxFlickable::viewportHeight() const @@ -1136,3 +1194,8 @@ void QFxFlickablePrivate::updateVelocity() } QT_END_NAMESPACE + +QML_DECLARE_TYPE(QFxFlickableVisibleArea) +QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,VisibleArea,QFxFlickableVisibleArea) + +#include "qfxflickable.moc" diff --git a/src/declarative/fx/qfxflickable.h b/src/declarative/fx/qfxflickable.h index 57a01d7..17f2107 100644 --- a/src/declarative/fx/qfxflickable.h +++ b/src/declarative/fx/qfxflickable.h @@ -51,6 +51,7 @@ QT_BEGIN_NAMESPACE QT_MODULE(Declarative) class QFxFlickablePrivate; +class QFxFlickableVisibleArea; class Q_DECLARATIVE_EXPORT QFxFlickable : public QFxItem { Q_OBJECT @@ -77,10 +78,7 @@ class Q_DECLARATIVE_EXPORT QFxFlickable : public QFxItem Q_PROPERTY(bool atXBeginning READ isAtXBeginning NOTIFY isAtBoundaryChanged) Q_PROPERTY(bool atYBeginning READ isAtYBeginning NOTIFY isAtBoundaryChanged) - Q_PROPERTY(qreal pageXPosition READ pageXPosition NOTIFY pageChanged) //### visibleArea.xPosition - Q_PROPERTY(qreal pageYPosition READ pageYPosition NOTIFY pageChanged) //### visibleArea.yPosition - Q_PROPERTY(qreal pageWidth READ pageWidth NOTIFY pageChanged) //### visibleArea.widthRatio - Q_PROPERTY(qreal pageHeight READ pageHeight NOTIFY pageChanged) //### visibleArea.heightRatio + Q_PROPERTY(QFxFlickableVisibleArea *visibleArea READ visibleArea CONSTANT) Q_PROPERTY(QmlList* flickableData READ flickableData) Q_PROPERTY(QmlList* flickableChildren READ flickableChildren) @@ -130,13 +128,8 @@ public: bool isAtXEnd() const; bool isAtXBeginning() const; - qreal pageXPosition() const; - qreal pageWidth() const; - bool isAtYEnd() const; bool isAtYBeginning() const; - qreal pageYPosition() const; - qreal pageHeight() const; QFxItem *viewport(); @@ -165,6 +158,8 @@ protected: qreal visibleX() const; qreal visibleY() const; + QFxFlickableVisibleArea *visibleArea(); + protected Q_SLOTS: virtual void ticked(); void movementStarting(); @@ -191,6 +186,7 @@ protected: private: Q_DISABLE_COPY(QFxFlickable) Q_DECLARE_PRIVATE_D(QGraphicsItem::d_ptr.data(), QFxFlickable) + friend class QFxFlickableVisibleArea; }; QT_END_NAMESPACE diff --git a/src/declarative/fx/qfxflickable_p.h b/src/declarative/fx/qfxflickable_p.h index ad9484f..882bec8 100644 --- a/src/declarative/fx/qfxflickable_p.h +++ b/src/declarative/fx/qfxflickable_p.h @@ -85,6 +85,7 @@ private: QTime _startTime; }; +class QFxFlickableVisibleArea; class QFxFlickablePrivate : public QFxItemPrivate { Q_DECLARE_PUBLIC(QFxFlickable) @@ -148,12 +149,9 @@ public: QmlTimeLine velocityTimeline; bool atXEnd; bool atXBeginning; - qreal pageXPosition; - qreal pageWidth; bool atYEnd; bool atYBeginning; - qreal pageYPosition; - qreal pageHeight; + QFxFlickableVisibleArea *visibleArea; void handleMousePressEvent(QGraphicsSceneMouseEvent *); void handleMouseMoveEvent(QGraphicsSceneMouseEvent *); @@ -166,6 +164,8 @@ public: void data_insert(int, QObject *); QObject *data_at(int) const; void data_clear(); + + friend class QFxFlickableVisibleArea; QML_DECLARE_LIST_PROXY(QFxFlickablePrivate, QObject *, data) }; -- cgit v0.12 From 9a8a4697928e1585b7bedf0681e70e71d955517f Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Thu, 3 Sep 2009 11:58:41 +1000 Subject: Update changes file. --- src/declarative/QmlChanges.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/declarative/QmlChanges.txt b/src/declarative/QmlChanges.txt index a401a70..98eb660 100644 --- a/src/declarative/QmlChanges.txt +++ b/src/declarative/QmlChanges.txt @@ -48,6 +48,10 @@ Flickable: xVelocity -> horizontalVelocity Flickable: yVelocity -> verticalVelocity Flickable: velocityDecay -> reportedVelocitySmoothing Flickable: locked -> interactive (note reversal of meaning) +Flickable: pageXPosition -> visibleArea.xPosition +Flickable: pageYPosition -> visibleArea.yPosition +Flickable: pageWidth -> visibleArea.widthRatio +Flickable: pageHeight -> visibleArea.heightRatio WebView: idealWidth -> preferredWidth WebView: idealHeight -> preferredHeight WebView: status -> statusText -- cgit v0.12 From eb4b61fac134c4b05d6137afb466bc62e34e39d1 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Thu, 3 Sep 2009 12:39:48 +1000 Subject: Fix bug when cancelling anchor change mid-transition. --- src/declarative/util/qmlstate.cpp | 3 +++ src/declarative/util/qmlstate.h | 1 + src/declarative/util/qmlstateoperations.cpp | 8 +++++++- src/declarative/util/qmlstateoperations.h | 1 + 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/declarative/util/qmlstate.cpp b/src/declarative/util/qmlstate.cpp index 508ef43..da7d429 100644 --- a/src/declarative/util/qmlstate.cpp +++ b/src/declarative/util/qmlstate.cpp @@ -359,6 +359,9 @@ void QmlState::apply(QmlStateGroup *group, QmlTransition *trans, QmlState *rever } } } + if (!found) + action.event->saveForwardBindings(); + //### do we ever need to do saveForwardBindings when found == true? } else { action.fromBinding = action.property.binding(); diff --git a/src/declarative/util/qmlstate.h b/src/declarative/util/qmlstate.h index 0b48449..255051f 100644 --- a/src/declarative/util/qmlstate.h +++ b/src/declarative/util/qmlstate.h @@ -95,6 +95,7 @@ public: virtual QList extraActions(); virtual bool changesBindings(); + virtual void saveForwardBindings() {} virtual void clearForwardBindings(); virtual void clearReverseBindings(); virtual bool override(ActionEvent*other); diff --git a/src/declarative/util/qmlstateoperations.cpp b/src/declarative/util/qmlstateoperations.cpp index 858a527..2ba6619 100644 --- a/src/declarative/util/qmlstateoperations.cpp +++ b/src/declarative/util/qmlstateoperations.cpp @@ -498,13 +498,19 @@ bool QmlAnchorChanges::changesBindings() return true; } -void QmlAnchorChanges::clearForwardBindings() +void QmlAnchorChanges::saveForwardBindings() { Q_D(QmlAnchorChanges); d->origLeft = d->target->anchors()->left(); d->origRight = d->target->anchors()->right(); d->origTop = d->target->anchors()->top(); d->origBottom = d->target->anchors()->bottom(); + +} + +void QmlAnchorChanges::clearForwardBindings() +{ + Q_D(QmlAnchorChanges); d->origX = d->target->x(); d->origY = d->target->y(); d->origWidth = d->target->width(); diff --git a/src/declarative/util/qmlstateoperations.h b/src/declarative/util/qmlstateoperations.h index afe0bd5..b2a37a6 100644 --- a/src/declarative/util/qmlstateoperations.h +++ b/src/declarative/util/qmlstateoperations.h @@ -147,6 +147,7 @@ public: virtual bool override(ActionEvent*other); virtual QList extraActions(); virtual bool changesBindings(); + virtual void saveForwardBindings(); virtual void clearForwardBindings(); virtual void clearReverseBindings(); }; -- cgit v0.12 From 844fdfe196885e4554e77c37ff4680f2bdd5c36e Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Thu, 3 Sep 2009 12:45:38 +1000 Subject: Remove dragMode property from Flickable. --- src/declarative/QmlChanges.txt | 1 + src/declarative/fx/qfxflickable.cpp | 182 ++++++++++-------------------------- src/declarative/fx/qfxflickable.h | 6 -- src/declarative/fx/qfxflickable_p.h | 50 +++------- src/declarative/fx/qfxlistview.cpp | 18 ++-- 5 files changed, 71 insertions(+), 186 deletions(-) diff --git a/src/declarative/QmlChanges.txt b/src/declarative/QmlChanges.txt index 98eb660..b24f6fe 100644 --- a/src/declarative/QmlChanges.txt +++ b/src/declarative/QmlChanges.txt @@ -71,6 +71,7 @@ Column/VerticalPositioner: lost "margins" property Row/HorizontalPositioner: lost "margins" property Grid/Positioner/Layout: lost "margins" property WebView: lost "interactive" property (always true now) +Flickable: removed "dragMode" property Other Changes: Drag: axis becomes an enum with values "XAxis", "YAxis", "XandYAxis" diff --git a/src/declarative/fx/qfxflickable.cpp b/src/declarative/fx/qfxflickable.cpp index 16c7fff..0ea50b8 100644 --- a/src/declarative/fx/qfxflickable.cpp +++ b/src/declarative/fx/qfxflickable.cpp @@ -48,51 +48,6 @@ QT_BEGIN_NAMESPACE -ElasticValue::ElasticValue(QmlTimeLineValue &val) - : _value(val) -{ - _to = _value.value(); - _myValue = _to; - _velocity = 0; -} - -void ElasticValue::setValue(qreal to) -{ - if (_to != to) { - _to = to; - _startTime.start(); - if (state() != Running) - start(); - } -} - -void ElasticValue::clear() -{ - stop(); - _velocity = 0.0; - _myValue = _value.value(); -} - -void ElasticValue::updateCurrentTime(int) -{ - const qreal Tension = 0.1; - int elapsed = _startTime.restart(); - if (!elapsed) - return; - qreal dist = _to - _value.value(); - qreal move = Tension * dist * qAbs(dist); - if (elapsed < 100 && _velocity != 0.0) - move = (elapsed * move + (100 - elapsed) * _velocity) / 100; - _myValue += move * elapsed / 1000; - _value.setValue(qRound(_myValue)); // moving sub-pixel can be ugly. -// _value.setValue(_myValue); - _velocity = move; - if (qAbs(_velocity) < 5.0) - clear(); - emit updated(); -} - - class QFxFlickableVisibleArea : public QObject { Q_OBJECT @@ -189,11 +144,11 @@ void QFxFlickableVisibleArea::updateVisible() QFxFlickablePrivate::QFxFlickablePrivate() - : _flick(new QFxItem), _moveX(_flick, &QFxItem::setX), _moveY(_flick, &QFxItem::setY) + : viewport(new QFxItem), _moveX(viewport, &QFxItem::setX), _moveY(viewport, &QFxItem::setY) , vWidth(-1), vHeight(-1), overShoot(true), flicked(false), moving(false), stealMouse(false) - , pressed(false), maxVelocity(-1), interactive(true), dragMode(QFxFlickable::Hard) - , elasticY(_moveY), elasticX(_moveX), reportedVelocitySmoothing(100), horizontalVelocity(this), verticalVelocity(this) - , vTime(0), atXEnd(false), atXBeginning(true), atYEnd(false), atYBeginning(true), visibleArea(0) + , pressed(false), atXEnd(false), atXBeginning(true), atYEnd(false), atYBeginning(true) + , interactive(true), maxVelocity(-1), reportedVelocitySmoothing(100) + , horizontalVelocity(this), verticalVelocity(this), vTime(0), visibleArea(0) { fixupXEvent = QmlTimeLineEvent::timeLineEvent(&_moveX, this); fixupYEvent = QmlTimeLineEvent::timeLineEvent(&_moveY, this); @@ -202,15 +157,13 @@ QFxFlickablePrivate::QFxFlickablePrivate() void QFxFlickablePrivate::init() { Q_Q(QFxFlickable); - _flick->setParent(q); - QObject::connect(&_tl, SIGNAL(updated()), q, SLOT(ticked())); - QObject::connect(&_tl, SIGNAL(completed()), q, SLOT(movementEnding())); + viewport->setParent(q); + QObject::connect(&timeline, SIGNAL(updated()), q, SLOT(ticked())); + QObject::connect(&timeline, SIGNAL(completed()), q, SLOT(movementEnding())); q->setAcceptedMouseButtons(Qt::LeftButton); q->setFiltersChildEvents(true); - QObject::connect(_flick, SIGNAL(xChanged()), q, SIGNAL(positionXChanged())); - QObject::connect(_flick, SIGNAL(yChanged()), q, SIGNAL(positionYChanged())); - QObject::connect(&elasticX, SIGNAL(updated()), q, SLOT(ticked())); - QObject::connect(&elasticY, SIGNAL(updated()), q, SLOT(ticked())); + QObject::connect(viewport, SIGNAL(xChanged()), q, SIGNAL(positionXChanged())); + QObject::connect(viewport, SIGNAL(yChanged()), q, SIGNAL(positionYChanged())); QObject::connect(q, SIGNAL(heightChanged()), q, SLOT(heightChange())); QObject::connect(q, SIGNAL(widthChanged()), q, SLOT(widthChange())); } @@ -237,16 +190,16 @@ void QFxFlickablePrivate::flickX(qreal velocity) else v = maxVelocity; } - _tl.reset(_moveX); - _tl.accel(_moveX, v, 500, maxDistance); - _tl.execute(fixupXEvent); + timeline.reset(_moveX); + timeline.accel(_moveX, v, 500, maxDistance); + timeline.execute(fixupXEvent); if (!flicked) { flicked = true; emit q->flickingChanged(); emit q->flickStarted(); } } else { - _tl.reset(_moveX); + timeline.reset(_moveX); fixupX(); } } @@ -273,16 +226,16 @@ void QFxFlickablePrivate::flickY(qreal velocity) else v = maxVelocity; } - _tl.reset(_moveY); - _tl.accel(_moveY, v, 500, maxDistance); - _tl.execute(fixupYEvent); + timeline.reset(_moveY); + timeline.accel(_moveY, v, 500, maxDistance); + timeline.execute(fixupYEvent); if (!flicked) { flicked = true; emit q->flickingChanged(); emit q->flickStarted(); } } else { - _tl.reset(_moveY); + timeline.reset(_moveY); fixupY(); } } @@ -293,16 +246,16 @@ void QFxFlickablePrivate::fixupX() if (!q->xflick() || _moveX.timeLine()) return; - vTime = _tl.time(); + vTime = timeline.time(); if (_moveX.value() > q->minXExtent() || (q->maxXExtent() > q->minXExtent())) { - _tl.reset(_moveX); + timeline.reset(_moveX); if (_moveX.value() != q->minXExtent()) - _tl.move(_moveX, q->minXExtent(), QEasingCurve(QEasingCurve::InOutQuad), 200); + timeline.move(_moveX, q->minXExtent(), QEasingCurve(QEasingCurve::InOutQuad), 200); //emit flickingChanged(); } else if (_moveX.value() < q->maxXExtent()) { - _tl.reset(_moveX); - _tl.move(_moveX, q->maxXExtent(), QEasingCurve(QEasingCurve::InOutQuad), 200); + timeline.reset(_moveX); + timeline.move(_moveX, q->maxXExtent(), QEasingCurve(QEasingCurve::InOutQuad), 200); //emit flickingChanged(); } else { flicked = false; @@ -315,16 +268,16 @@ void QFxFlickablePrivate::fixupY() if (!q->yflick() || _moveY.timeLine()) return; - vTime = _tl.time(); + vTime = timeline.time(); if (_moveY.value() > q->minYExtent() || (q->maxYExtent() > q->minYExtent())) { - _tl.reset(_moveY); + timeline.reset(_moveY); if (_moveY.value() != q->minYExtent()) - _tl.move(_moveY, q->minYExtent(), QEasingCurve(QEasingCurve::InOutQuad), 200); + timeline.move(_moveY, q->minYExtent(), QEasingCurve(QEasingCurve::InOutQuad), 200); //emit flickingChanged(); } else if (_moveY.value() < q->maxYExtent()) { - _tl.reset(_moveY); - _tl.move(_moveY, q->maxYExtent(), QEasingCurve(QEasingCurve::InOutQuad), 200); + timeline.reset(_moveY); + timeline.move(_moveY, q->maxYExtent(), QEasingCurve(QEasingCurve::InOutQuad), 200); //emit flickingChanged(); } else { flicked = false; @@ -481,7 +434,7 @@ void QFxFlickable::setViewportX(qreal pos) Q_D(QFxFlickable); pos = qRound(pos); if (-pos != d->_moveX.value()) { - d->_tl.reset(d->_moveX); + d->timeline.reset(d->_moveX); d->_moveX.setValue(-pos); viewportMoved(); } @@ -498,7 +451,7 @@ void QFxFlickable::setViewportY(qreal pos) Q_D(QFxFlickable); pos = qRound(pos); if (-pos != d->_moveY.value()) { - d->_tl.reset(d->_moveY); + d->timeline.reset(d->_moveY); d->_moveY.setValue(-pos); viewportMoved(); } @@ -524,7 +477,7 @@ void QFxFlickable::setInteractive(bool interactive) Q_D(QFxFlickable); d->interactive = interactive; if (!interactive && d->flicked) { - d->_tl.clear(); + d->timeline.clear(); d->flicked = false; emit flickingChanged(); emit flickEnded(); @@ -532,28 +485,6 @@ void QFxFlickable::setInteractive(bool interactive) } /*! - \qmlproperty enumeration Flickable::dragMode - This property contains the kind of 'physics' applied when dragging the surface. - - Two modes are supported: - \list - \i Hard - the view follows the user's input exactly. - \i Elastic - the view moves elastically in response to the user's input. - \endlist -*/ -QFxFlickable::DragMode QFxFlickable::dragMode() const -{ - Q_D(const QFxFlickable); - return d->dragMode; -} - -void QFxFlickable::setDragMode(DragMode mode) -{ - Q_D(QFxFlickable); - d->dragMode = mode; -} - -/*! \qmlproperty real Flickable::horizontalVelocity \qmlproperty real Flickable::verticalVelocity \qmlproperty real Flickable::reportedVelocitySmoothing @@ -616,7 +547,7 @@ void QFxFlickable::ticked() QFxItem *QFxFlickable::viewport() { Q_D(QFxFlickable); - return d->_flick; + return d->viewport; } qreal QFxFlickable::visibleX() const @@ -641,12 +572,12 @@ QFxFlickableVisibleArea *QFxFlickable::visibleArea() void QFxFlickablePrivate::handleMousePressEvent(QGraphicsSceneMouseEvent *event) { - if (interactive && _tl.isActive() && (qAbs(velocityX) > 10 || qAbs(velocityY) > 10)) + if (interactive && timeline.isActive() && (qAbs(velocityX) > 10 || qAbs(velocityY) > 10)) stealMouse = true; // If we've been flicked then steal the click. else stealMouse = false; pressed = true; - _tl.clear(); + timeline.clear(); velocityX = -1; velocityY = -1; lastPos = QPoint(); @@ -656,10 +587,6 @@ void QFxFlickablePrivate::handleMousePressEvent(QGraphicsSceneMouseEvent *event) pressY = _moveY.value(); flicked = false; pressTime.start(); - if (dragMode == QFxFlickable::Elastic) { - elasticX.clear(); - elasticY.clear(); - } velocityTime.start(); } @@ -683,10 +610,7 @@ void QFxFlickablePrivate::handleMouseMoveEvent(QGraphicsSceneMouseEvent *event) if (newY < maxY && maxY - minY < 0) newY = maxY + (newY - maxY) / 2; if (q->overShoot() || (newY <= minY && newY >= maxY)) { - if (dragMode == QFxFlickable::Hard) - _moveY.setValue(newY); - else - elasticY.setValue(newY); + _moveY.setValue(newY); moved = true; } else if (!q->overShoot()) rejectY = true; @@ -706,10 +630,7 @@ void QFxFlickablePrivate::handleMouseMoveEvent(QGraphicsSceneMouseEvent *event) if (newX < maxX && maxX - minX < 0) newX = maxX + (newX - maxX) / 2; if (q->overShoot() || (newX <= minX && newX >= maxX)) { - if (dragMode == QFxFlickable::Hard) - _moveX.setValue(newX); - else - elasticX.setValue(newX); + _moveX.setValue(newX); moved = true; } else if (!q->overShoot()) rejectX = true; @@ -751,12 +672,7 @@ void QFxFlickablePrivate::handleMouseReleaseEvent(QGraphicsSceneMouseEvent *) if (lastPosTime.isNull()) return; - if (dragMode == QFxFlickable::Elastic) { - elasticY.clear(); - elasticX.clear(); - } - - vTime = _tl.time(); + vTime = timeline.time(); if (qAbs(velocityY) > 10) flickY(velocityY); else @@ -770,7 +686,7 @@ void QFxFlickablePrivate::handleMouseReleaseEvent(QGraphicsSceneMouseEvent *) stealMouse = false; lastPosTime = QTime(); - if (!_tl.isActive()) + if (!timeline.isActive()) q->movementEnding(); } @@ -835,13 +751,13 @@ void QFxFlickable::viewportMoved() d->velocityTimeline.move(d->verticalVelocity, verticalVelocity, d->reportedVelocitySmoothing); d->velocityTimeline.move(d->verticalVelocity, 0, d->reportedVelocitySmoothing); } else { - if (d->_tl.time() != d->vTime) { - qreal horizontalVelocity = (prevX - d->_moveX.value()) * 1000 / (d->_tl.time() - d->vTime); - qreal verticalVelocity = (prevY - d->_moveY.value()) * 1000 / (d->_tl.time() - d->vTime); + if (d->timeline.time() != d->vTime) { + qreal horizontalVelocity = (prevX - d->_moveX.value()) * 1000 / (d->timeline.time() - d->vTime); + qreal verticalVelocity = (prevY - d->_moveY.value()) * 1000 / (d->timeline.time() - d->vTime); d->horizontalVelocity.setValue(horizontalVelocity); d->verticalVelocity.setValue(verticalVelocity); } - d->vTime = d->_tl.time(); + d->vTime = d->timeline.time(); } } @@ -890,7 +806,7 @@ void QFxFlickablePrivate::data_append(QObject *o) Q_Q(QFxFlickable); QFxItem *i = qobject_cast(o); if (i) - _flick->children()->append(i); + viewport->children()->append(i); else o->setParent(q); } @@ -921,7 +837,7 @@ QmlList *QFxFlickable::flickableData() QmlList *QFxFlickable::flickableChildren() { Q_D(QFxFlickable); - return d->_flick->children(); + return d->viewport->children(); } /*! @@ -973,9 +889,9 @@ void QFxFlickable::setViewportWidth(qreal w) return; d->vWidth = w; if (w < 0) - d->_flick->setWidth(width()); + d->viewport->setWidth(width()); else - d->_flick->setWidth(w); + d->viewport->setWidth(w); // Make sure that we're entirely in view. if (!d->pressed) d->fixupX(); @@ -987,7 +903,7 @@ void QFxFlickable::widthChange() { Q_D(QFxFlickable); if (d->vWidth < 0) { - d->_flick->setWidth(width()); + d->viewport->setWidth(width()); emit viewportWidthChanged(); } d->updateBeginningEnd(); @@ -997,7 +913,7 @@ void QFxFlickable::heightChange() { Q_D(QFxFlickable); if (d->vHeight < 0) { - d->_flick->setHeight(height()); + d->viewport->setHeight(height()); emit viewportHeightChanged(); } d->updateBeginningEnd(); @@ -1016,9 +932,9 @@ void QFxFlickable::setViewportHeight(qreal h) return; d->vHeight = h; if (h < 0) - d->_flick->setHeight(height()); + d->viewport->setHeight(height()); else - d->_flick->setHeight(h); + d->viewport->setHeight(h); // Make sure that we're entirely in view. if (!d->pressed) d->fixupY(); diff --git a/src/declarative/fx/qfxflickable.h b/src/declarative/fx/qfxflickable.h index 17f2107..4905101 100644 --- a/src/declarative/fx/qfxflickable.h +++ b/src/declarative/fx/qfxflickable.h @@ -71,7 +71,6 @@ class Q_DECLARATIVE_EXPORT QFxFlickable : public QFxItem Q_PROPERTY(bool flicking READ isFlicking NOTIFY flickingChanged) Q_PROPERTY(bool interactive READ isInteractive WRITE setInteractive) - Q_PROPERTY(DragMode dragMode READ dragMode WRITE setDragMode) //### remove. Consider a better way to implement different drag behavior Q_PROPERTY(bool atXEnd READ isAtXEnd NOTIFY isAtBoundaryChanged) Q_PROPERTY(bool atYEnd READ isAtYEnd NOTIFY isAtBoundaryChanged) @@ -118,11 +117,6 @@ public: bool isInteractive() const; void setInteractive(bool); - Q_ENUMS(DragMode) - enum DragMode { Hard, Elastic }; - DragMode dragMode() const; - void setDragMode(DragMode mode); - qreal horizontalVelocity() const; qreal verticalVelocity() const; diff --git a/src/declarative/fx/qfxflickable_p.h b/src/declarative/fx/qfxflickable_p.h index 882bec8..a506027 100644 --- a/src/declarative/fx/qfxflickable_p.h +++ b/src/declarative/fx/qfxflickable_p.h @@ -62,29 +62,6 @@ QT_BEGIN_NAMESPACE -class ElasticValue : public QAbstractAnimation { - Q_OBJECT -public: - ElasticValue(QmlTimeLineValue &); - void setValue(qreal to); - void clear(); - - virtual int duration() const { return 10000; } - -protected: - virtual void updateCurrentTime(int); - -Q_SIGNALS: - void updated(); - -private: - qreal _to; - qreal _myValue; - qreal _velocity; - QmlTimeLineValue &_value; - QTime _startTime; -}; - class QFxFlickableVisibleArea; class QFxFlickablePrivate : public QFxItemPrivate { @@ -100,17 +77,22 @@ public: void updateBeginningEnd(); public: - QFxItem *_flick; + QFxItem *viewport; QmlTimeLineValueProxy _moveX; QmlTimeLineValueProxy _moveY; - QmlTimeLine _tl; + QmlTimeLine timeline; qreal vWidth; qreal vHeight; - bool overShoot; - bool flicked; - bool moving; - bool stealMouse; - bool pressed; + bool overShoot : 1; + bool flicked : 1; + bool moving : 1; + bool stealMouse : 1; + bool pressed : 1; + bool atXEnd : 1; + bool atXBeginning : 1; + bool atYEnd : 1; + bool atYBeginning : 1; + bool interactive : 1; QTime lastPosTime; QPointF lastPos; QPointF pressPos; @@ -122,10 +104,6 @@ public: QmlTimeLineEvent fixupXEvent; QmlTimeLineEvent fixupYEvent; qreal maxVelocity; - bool interactive; - QFxFlickable::DragMode dragMode; - ElasticValue elasticY; - ElasticValue elasticX; QTime velocityTime; QPointF lastFlickablePosition; qreal reportedVelocitySmoothing; @@ -147,10 +125,6 @@ public: Velocity verticalVelocity; int vTime; QmlTimeLine velocityTimeline; - bool atXEnd; - bool atXBeginning; - bool atYEnd; - bool atYBeginning; QFxFlickableVisibleArea *visibleArea; void handleMousePressEvent(QGraphicsSceneMouseEvent *); diff --git a/src/declarative/fx/qfxlistview.cpp b/src/declarative/fx/qfxlistview.cpp index 34fe827..523786f 100644 --- a/src/declarative/fx/qfxlistview.cpp +++ b/src/declarative/fx/qfxlistview.cpp @@ -801,8 +801,8 @@ void QFxListViewPrivate::fixupY() if (currItemMode == QFxListView::SnapAuto) { if (currentItem) { moveReason = Mouse; - _tl.clear(); - _tl.move(_moveY, -(currentItem->position() - snapPos), QEasingCurve(QEasingCurve::InOutQuad), 200); + timeline.clear(); + timeline.move(_moveY, -(currentItem->position() - snapPos), QEasingCurve(QEasingCurve::InOutQuad), 200); } } else if (currItemMode == QFxListView::Snap) { moveReason = Mouse; @@ -813,8 +813,8 @@ void QFxListViewPrivate::fixupY() pos = -q->maxYExtent(); else if (pos < -q->minYExtent()) pos = -q->minYExtent(); - _tl.clear(); - _tl.move(_moveY, -(pos), QEasingCurve(QEasingCurve::InOutQuad), 200); + timeline.clear(); + timeline.move(_moveY, -(pos), QEasingCurve(QEasingCurve::InOutQuad), 200); } } } @@ -828,8 +828,8 @@ void QFxListViewPrivate::fixupX() if (currItemMode == QFxListView::SnapAuto) { if (currentItem) { moveReason = Mouse; - _tl.clear(); - _tl.move(_moveX, -(currentItem->position() - snapPos), QEasingCurve(QEasingCurve::InOutQuad), 200); + timeline.clear(); + timeline.move(_moveX, -(currentItem->position() - snapPos), QEasingCurve(QEasingCurve::InOutQuad), 200); } } else if (currItemMode == QFxListView::Snap) { moveReason = Mouse; @@ -840,8 +840,8 @@ void QFxListViewPrivate::fixupX() pos = -q->maxXExtent(); else if (pos < -q->minXExtent()) pos = -q->minXExtent(); - _tl.clear(); - _tl.move(_moveX, -(pos), QEasingCurve(QEasingCurve::InOutQuad), 200); + timeline.clear(); + timeline.move(_moveX, -(pos), QEasingCurve(QEasingCurve::InOutQuad), 200); } } } @@ -1600,7 +1600,7 @@ void QFxListView::itemsRemoved(int modelIndex, int count) if (d->visibleItems.isEmpty()) { d->visibleIndex = 0; d->visiblePos = 0; - d->_tl.clear(); + d->timeline.clear(); d->setPosition(0); if (d->model->count() == 0) update(); -- cgit v0.12 From b1d0b92d6c9c150299a0180349ccc50114ae9273 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Thu, 3 Sep 2009 13:32:22 +1000 Subject: Round out AnchorChanges API. --- src/declarative/util/qmlstateoperations.cpp | 93 +++++++++++++++++++++++++++-- src/declarative/util/qmlstateoperations.h | 12 ++++ 2 files changed, 101 insertions(+), 4 deletions(-) diff --git a/src/declarative/util/qmlstateoperations.cpp b/src/declarative/util/qmlstateoperations.cpp index 2ba6619..50f80f6 100644 --- a/src/declarative/util/qmlstateoperations.cpp +++ b/src/declarative/util/qmlstateoperations.cpp @@ -305,12 +305,19 @@ public: QStringList resetList; QFxAnchorLine left; QFxAnchorLine right; + QFxAnchorLine horizontalCenter; QFxAnchorLine top; QFxAnchorLine bottom; + QFxAnchorLine verticalCenter; + QFxAnchorLine baseline; + QFxAnchorLine origLeft; QFxAnchorLine origRight; + QFxAnchorLine origHCenter; QFxAnchorLine origTop; QFxAnchorLine origBottom; + QFxAnchorLine origVCenter; + QFxAnchorLine origBaseline; qreal origX; qreal origY; qreal origWidth; @@ -364,11 +371,15 @@ void QmlAnchorChanges::setReset(const QString &reset) } /*! - \qmlproperty AnchorLine AnchorChanges::top - \qmlproperty AnchorLine AnchorChanges::bottom \qmlproperty AnchorLine AnchorChanges::left \qmlproperty AnchorLine AnchorChanges::right - These properties change the \e left, \e top, \e right and \e bottom anchors of the item + \qmlproperty AnchorLine AnchorChanges::horizontalCenter + \qmlproperty AnchorLine AnchorChanges::top + \qmlproperty AnchorLine AnchorChanges::bottom + \qmlproperty AnchorLine AnchorChanges::verticalCenter + \qmlproperty AnchorLine AnchorChanges::baseline + + These properties change the respective anchors of the item. */ QFxAnchorLine QmlAnchorChanges::left() const @@ -395,6 +406,18 @@ void QmlAnchorChanges::setRight(const QFxAnchorLine &edge) d->right = edge; } +QFxAnchorLine QmlAnchorChanges::horizontalCenter() const +{ + Q_D(const QmlAnchorChanges); + return d->horizontalCenter; +} + +void QmlAnchorChanges::setHorizontalCenter(const QFxAnchorLine &edge) +{ + Q_D(QmlAnchorChanges); + d->horizontalCenter = edge; +} + QFxAnchorLine QmlAnchorChanges::top() const { Q_D(const QmlAnchorChanges); @@ -419,6 +442,30 @@ void QmlAnchorChanges::setBottom(const QFxAnchorLine &edge) d->bottom = edge; } +QFxAnchorLine QmlAnchorChanges::verticalCenter() const +{ + Q_D(const QmlAnchorChanges); + return d->verticalCenter; +} + +void QmlAnchorChanges::setVerticalCenter(const QFxAnchorLine &edge) +{ + Q_D(QmlAnchorChanges); + d->verticalCenter = edge; +} + +QFxAnchorLine QmlAnchorChanges::baseline() const +{ + Q_D(const QmlAnchorChanges); + return d->baseline; +} + +void QmlAnchorChanges::setBaseline(const QFxAnchorLine &edge) +{ + Q_D(QmlAnchorChanges); + d->baseline = edge; +} + void QmlAnchorChanges::execute() { Q_D(QmlAnchorChanges); @@ -430,10 +477,16 @@ void QmlAnchorChanges::execute() d->target->anchors()->setLeft(d->left); if (d->right.anchorLine != QFxAnchorLine::Invalid) d->target->anchors()->setRight(d->right); + if (d->horizontalCenter.anchorLine != QFxAnchorLine::Invalid) + d->target->anchors()->setHorizontalCenter(d->horizontalCenter); if (d->top.anchorLine != QFxAnchorLine::Invalid) d->target->anchors()->setTop(d->top); if (d->bottom.anchorLine != QFxAnchorLine::Invalid) d->target->anchors()->setBottom(d->bottom); + if (d->verticalCenter.anchorLine != QFxAnchorLine::Invalid) + d->target->anchors()->setVerticalCenter(d->verticalCenter); + if (d->baseline.anchorLine != QFxAnchorLine::Invalid) + d->target->anchors()->setBaseline(d->baseline); } bool QmlAnchorChanges::isReversable() @@ -452,10 +505,16 @@ void QmlAnchorChanges::reverse() d->target->anchors()->setLeft(d->origLeft); if (d->origRight.anchorLine != QFxAnchorLine::Invalid) d->target->anchors()->setRight(d->origRight); + if (d->origHCenter.anchorLine != QFxAnchorLine::Invalid) + d->target->anchors()->setHorizontalCenter(d->origHCenter); if (d->origTop.anchorLine != QFxAnchorLine::Invalid) d->target->anchors()->setTop(d->origTop); if (d->origBottom.anchorLine != QFxAnchorLine::Invalid) d->target->anchors()->setBottom(d->origBottom); + if (d->origVCenter.anchorLine != QFxAnchorLine::Invalid) + d->target->anchors()->setVerticalCenter(d->origVCenter); + if (d->origBaseline.anchorLine != QFxAnchorLine::Invalid) + d->target->anchors()->setBaseline(d->origBaseline); } QString QmlAnchorChanges::typeName() const @@ -503,9 +562,11 @@ void QmlAnchorChanges::saveForwardBindings() Q_D(QmlAnchorChanges); d->origLeft = d->target->anchors()->left(); d->origRight = d->target->anchors()->right(); + d->origHCenter = d->target->anchors()->horizontalCenter(); d->origTop = d->target->anchors()->top(); d->origBottom = d->target->anchors()->bottom(); - + d->origVCenter = d->target->anchors()->verticalCenter(); + d->origBaseline = d->target->anchors()->baseline(); } void QmlAnchorChanges::clearForwardBindings() @@ -521,20 +582,32 @@ void QmlAnchorChanges::clearForwardBindings() d->target->anchors()->resetLeft(); if (d->resetList.contains(QLatin1String("right"))) d->target->anchors()->resetRight(); + if (d->resetList.contains(QLatin1String("horizontalCenter"))) + d->target->anchors()->resetHorizontalCenter(); if (d->resetList.contains(QLatin1String("top"))) d->target->anchors()->resetTop(); if (d->resetList.contains(QLatin1String("bottom"))) d->target->anchors()->resetBottom(); + if (d->resetList.contains(QLatin1String("verticalCenter"))) + d->target->anchors()->resetVerticalCenter(); + if (d->resetList.contains(QLatin1String("baseline"))) + d->target->anchors()->resetBaseline(); //reset any anchors that we'll be setting in the state if (d->left.anchorLine != QFxAnchorLine::Invalid) d->target->anchors()->resetLeft(); if (d->right.anchorLine != QFxAnchorLine::Invalid) d->target->anchors()->resetRight(); + if (d->horizontalCenter.anchorLine != QFxAnchorLine::Invalid) + d->target->anchors()->resetHorizontalCenter(); if (d->top.anchorLine != QFxAnchorLine::Invalid) d->target->anchors()->resetTop(); if (d->bottom.anchorLine != QFxAnchorLine::Invalid) d->target->anchors()->resetBottom(); + if (d->verticalCenter.anchorLine != QFxAnchorLine::Invalid) + d->target->anchors()->resetVerticalCenter(); + if (d->baseline.anchorLine != QFxAnchorLine::Invalid) + d->target->anchors()->resetBaseline(); } void QmlAnchorChanges::clearReverseBindings() @@ -550,20 +623,32 @@ void QmlAnchorChanges::clearReverseBindings() d->target->anchors()->resetLeft(); if (d->right.anchorLine != QFxAnchorLine::Invalid) d->target->anchors()->resetRight(); + if (d->horizontalCenter.anchorLine != QFxAnchorLine::Invalid) + d->target->anchors()->resetHorizontalCenter(); if (d->top.anchorLine != QFxAnchorLine::Invalid) d->target->anchors()->resetTop(); if (d->bottom.anchorLine != QFxAnchorLine::Invalid) d->target->anchors()->resetBottom(); + if (d->verticalCenter.anchorLine != QFxAnchorLine::Invalid) + d->target->anchors()->resetVerticalCenter(); + if (d->baseline.anchorLine != QFxAnchorLine::Invalid) + d->target->anchors()->resetBaseline(); //reset any anchors that were set in the original state if (d->origLeft.anchorLine != QFxAnchorLine::Invalid) d->target->anchors()->resetLeft(); if (d->origRight.anchorLine != QFxAnchorLine::Invalid) d->target->anchors()->resetRight(); + if (d->origHCenter.anchorLine != QFxAnchorLine::Invalid) + d->target->anchors()->resetHorizontalCenter(); if (d->origTop.anchorLine != QFxAnchorLine::Invalid) d->target->anchors()->resetTop(); if (d->origBottom.anchorLine != QFxAnchorLine::Invalid) d->target->anchors()->resetBottom(); + if (d->origVCenter.anchorLine != QFxAnchorLine::Invalid) + d->target->anchors()->resetVerticalCenter(); + if (d->origBaseline.anchorLine != QFxAnchorLine::Invalid) + d->target->anchors()->resetBaseline(); } bool QmlAnchorChanges::override(ActionEvent*other) diff --git a/src/declarative/util/qmlstateoperations.h b/src/declarative/util/qmlstateoperations.h index b2a37a6..a3943e7 100644 --- a/src/declarative/util/qmlstateoperations.h +++ b/src/declarative/util/qmlstateoperations.h @@ -113,8 +113,11 @@ class Q_DECLARATIVE_EXPORT QmlAnchorChanges : public QmlStateOperation, public A Q_PROPERTY(QString reset READ reset WRITE setReset) Q_PROPERTY(QFxAnchorLine left READ left WRITE setLeft) Q_PROPERTY(QFxAnchorLine right READ right WRITE setRight) + Q_PROPERTY(QFxAnchorLine horizontalCenter READ horizontalCenter WRITE setHorizontalCenter) Q_PROPERTY(QFxAnchorLine top READ top WRITE setTop) Q_PROPERTY(QFxAnchorLine bottom READ bottom WRITE setBottom) + Q_PROPERTY(QFxAnchorLine verticalCenter READ verticalCenter WRITE setVerticalCenter) + Q_PROPERTY(QFxAnchorLine baseline READ baseline WRITE setBaseline) public: QmlAnchorChanges(QObject *parent=0); @@ -134,12 +137,21 @@ public: QFxAnchorLine right() const; void setRight(const QFxAnchorLine &edge); + QFxAnchorLine horizontalCenter() const; + void setHorizontalCenter(const QFxAnchorLine &edge); + QFxAnchorLine top() const; void setTop(const QFxAnchorLine &edge); QFxAnchorLine bottom() const; void setBottom(const QFxAnchorLine &edge); + QFxAnchorLine verticalCenter() const; + void setVerticalCenter(const QFxAnchorLine &edge); + + QFxAnchorLine baseline() const; + void setBaseline(const QFxAnchorLine &edge); + virtual void execute(); virtual bool isReversable(); virtual void reverse(); -- cgit v0.12 From b4fdd60aeb66520d55a8cf4710c9eb648ed7937d Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Thu, 3 Sep 2009 14:38:39 +1000 Subject: Cleanup minehunt example --- examples/declarative/minehunt/Explosion.qml | 4 +- examples/declarative/minehunt/minehunt.qml | 102 +++++++++++++++------------- 2 files changed, 57 insertions(+), 49 deletions(-) diff --git a/examples/declarative/minehunt/Explosion.qml b/examples/declarative/minehunt/Explosion.qml index 9b8bd6e..a997048 100644 --- a/examples/declarative/minehunt/Explosion.qml +++ b/examples/declarative/minehunt/Explosion.qml @@ -5,8 +5,8 @@ Item { Particles { id: particles - width: 38 - height: 21 + width: 40 + height: 40 lifeSpan: 1000 lifeSpanDeviation: 0 source: "pics/star.png" diff --git a/examples/declarative/minehunt/minehunt.qml b/examples/declarative/minehunt/minehunt.qml index 4bae64d..fa169aa 100644 --- a/examples/declarative/minehunt/minehunt.qml +++ b/examples/declarative/minehunt/minehunt.qml @@ -60,9 +60,6 @@ Item { } Explosion { id: expl - anchors.horizontalCenter: parent.horizontalCenter - anchors.verticalCenter: parent.verticalCenter - //explode: modelData.hasMine && modelData.flipped//Doesn't wait for the pause } } states: [ @@ -77,16 +74,20 @@ Item { SequentialAnimation { PauseAnimation { duration: { - var ret = Math.abs(flipable.parent.x-field.clickx) + var ret; + if(flipable.parent != null) + ret = Math.abs(flipable.parent.x-field.clickx) + Math.abs(flipable.parent.y-field.clicky); + else + ret = 0; if (ret > 0) { if (modelData.hasMine && modelData.flipped) { - ret*3 + ret*3; } else { - ret + ret; } } else { - 0 + 0; } } } @@ -95,14 +96,14 @@ Item { properties: "angle" } ScriptAction{ - script: if(modelData.hasMine && modelData.flipped) - {expl.explode = true;} + script: "if(modelData.hasMine && modelData.flipped){expl.explode = true;}" } } } ] MouseRegion { anchors.fill: parent + acceptedButtons: Qt.LeftButton | Qt.RightButton onPressed: { field.clickx = flipable.parent.x; field.clicky = flipable.parent.y; @@ -141,47 +142,54 @@ Item { } } } - Item { + Row { id: gamedata - width: 370 - height: 100 - y: 380 - Text { - color: "white" - font.pointSize: 18 - x: 20 - y: 20 - } - Image { - x: 100 - y: 20 - source: "pics/bomb-color.png" - } - Text { - x: 100 - y: 60 - color: "white" - text: numMines - } - Image { - x: 140 - y: 20 - source: "pics/flag-color.png" - } - Text { - x: 140 - y: 60 - color: "white" - text: numFlags + // width: 370 + // height: 100 + y: 400 + x: 20 + spacing: 20 + Column { + spacing: 2 + width: childrenRect.width + Image { + // x: 100 + // y: 20 + source: "pics/bomb-color.png" + } + Text { + // x: 100 + // y: 60 + anchors.horizontalCenter: parent.horizontalCenter + color: "white" + text: numMines + } } - Image { - x: 280 - y: 10 - source: isPlaying ? 'pics/face-smile.png' : hasWon ? 'pics/face-smile-big.png': 'pics/face-sad.png' - MouseRegion { - anchors.fill: parent - onPressed: { reset() } + Column { + spacing: 2 + width: childrenRect.width + Image { + // x: 140 + // y: 20 + source: "pics/flag-color.png" + } + Text { + // x: 140 + // y: 60 + anchors.horizontalCenter: parent.horizontalCenter + color: "white" + text: numFlags } } } + Image { + y: 390 + anchors.right: field.right + anchors.rightMargin: 20 + source: isPlaying ? 'pics/face-smile.png' : hasWon ? 'pics/face-smile-big.png': 'pics/face-sad.png' + MouseRegion { + anchors.fill: parent + onPressed: { reset() } + } + } } -- cgit v0.12 From 3732fb8ac1ef79b47a4e8df9c37154618a1adc5e Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Thu, 3 Sep 2009 14:46:25 +1000 Subject: Move minehunt and twitter to demos directory Minehunt is more a demo than an example, and the twitter app goes with the flickr app. --- demos/declarative/minehunt/Description.qml | 34 +++ demos/declarative/minehunt/Explosion.qml | 29 ++ demos/declarative/minehunt/main.cpp | 309 +++++++++++++++++++++ demos/declarative/minehunt/minehunt.pro | 9 + demos/declarative/minehunt/minehunt.qml | 195 +++++++++++++ .../minehunt/pics/No-Ones-Laughing-3.jpg | Bin 0 -> 30730 bytes demos/declarative/minehunt/pics/back.png | Bin 0 -> 558 bytes demos/declarative/minehunt/pics/bomb-color.png | Bin 0 -> 284 bytes demos/declarative/minehunt/pics/bomb.png | Bin 0 -> 535 bytes demos/declarative/minehunt/pics/face-sad.png | Bin 0 -> 14844 bytes demos/declarative/minehunt/pics/face-smile-big.png | Bin 0 -> 13810 bytes demos/declarative/minehunt/pics/face-smile.png | Bin 0 -> 15408 bytes demos/declarative/minehunt/pics/flag-color.png | Bin 0 -> 219 bytes demos/declarative/minehunt/pics/flag.png | Bin 0 -> 196 bytes demos/declarative/minehunt/pics/front.png | Bin 0 -> 580 bytes demos/declarative/minehunt/pics/star.png | Bin 0 -> 2677 bytes demos/declarative/twitter/content/AuthView.qml | 92 ++++++ demos/declarative/twitter/content/FatDelegate.qml | 47 ++++ demos/declarative/twitter/content/HomeTitleBar.qml | 125 +++++++++ .../declarative/twitter/content/MultiTitleBar.qml | 25 ++ demos/declarative/twitter/content/RssModel.qml | 42 +++ demos/declarative/twitter/content/UserModel.qml | 26 ++ demos/declarative/twitter/twitter.qml | 87 ++++++ examples/declarative/minehunt/Description.qml | 34 --- examples/declarative/minehunt/Explosion.qml | 29 -- examples/declarative/minehunt/main.cpp | 309 --------------------- examples/declarative/minehunt/minehunt.pro | 9 - examples/declarative/minehunt/minehunt.qml | 195 ------------- .../minehunt/pics/No-Ones-Laughing-3.jpg | Bin 30730 -> 0 bytes examples/declarative/minehunt/pics/back.png | Bin 558 -> 0 bytes examples/declarative/minehunt/pics/bomb-color.png | Bin 284 -> 0 bytes examples/declarative/minehunt/pics/bomb.png | Bin 535 -> 0 bytes examples/declarative/minehunt/pics/face-sad.png | Bin 14844 -> 0 bytes .../declarative/minehunt/pics/face-smile-big.png | Bin 13810 -> 0 bytes examples/declarative/minehunt/pics/face-smile.png | Bin 15408 -> 0 bytes examples/declarative/minehunt/pics/flag-color.png | Bin 219 -> 0 bytes examples/declarative/minehunt/pics/flag.png | Bin 196 -> 0 bytes examples/declarative/minehunt/pics/front.png | Bin 580 -> 0 bytes examples/declarative/minehunt/pics/star.png | Bin 2677 -> 0 bytes 39 files changed, 1020 insertions(+), 576 deletions(-) create mode 100644 demos/declarative/minehunt/Description.qml create mode 100644 demos/declarative/minehunt/Explosion.qml create mode 100644 demos/declarative/minehunt/main.cpp create mode 100644 demos/declarative/minehunt/minehunt.pro create mode 100644 demos/declarative/minehunt/minehunt.qml create mode 100644 demos/declarative/minehunt/pics/No-Ones-Laughing-3.jpg create mode 100644 demos/declarative/minehunt/pics/back.png create mode 100644 demos/declarative/minehunt/pics/bomb-color.png create mode 100644 demos/declarative/minehunt/pics/bomb.png create mode 100644 demos/declarative/minehunt/pics/face-sad.png create mode 100644 demos/declarative/minehunt/pics/face-smile-big.png create mode 100644 demos/declarative/minehunt/pics/face-smile.png create mode 100644 demos/declarative/minehunt/pics/flag-color.png create mode 100644 demos/declarative/minehunt/pics/flag.png create mode 100644 demos/declarative/minehunt/pics/front.png create mode 100644 demos/declarative/minehunt/pics/star.png create mode 100644 demos/declarative/twitter/content/AuthView.qml create mode 100644 demos/declarative/twitter/content/FatDelegate.qml create mode 100644 demos/declarative/twitter/content/HomeTitleBar.qml create mode 100644 demos/declarative/twitter/content/MultiTitleBar.qml create mode 100644 demos/declarative/twitter/content/RssModel.qml create mode 100644 demos/declarative/twitter/content/UserModel.qml create mode 100644 demos/declarative/twitter/twitter.qml delete mode 100644 examples/declarative/minehunt/Description.qml delete mode 100644 examples/declarative/minehunt/Explosion.qml delete mode 100644 examples/declarative/minehunt/main.cpp delete mode 100644 examples/declarative/minehunt/minehunt.pro delete mode 100644 examples/declarative/minehunt/minehunt.qml delete mode 100644 examples/declarative/minehunt/pics/No-Ones-Laughing-3.jpg delete mode 100644 examples/declarative/minehunt/pics/back.png delete mode 100644 examples/declarative/minehunt/pics/bomb-color.png delete mode 100644 examples/declarative/minehunt/pics/bomb.png delete mode 100644 examples/declarative/minehunt/pics/face-sad.png delete mode 100644 examples/declarative/minehunt/pics/face-smile-big.png delete mode 100644 examples/declarative/minehunt/pics/face-smile.png delete mode 100644 examples/declarative/minehunt/pics/flag-color.png delete mode 100644 examples/declarative/minehunt/pics/flag.png delete mode 100644 examples/declarative/minehunt/pics/front.png delete mode 100644 examples/declarative/minehunt/pics/star.png diff --git a/demos/declarative/minehunt/Description.qml b/demos/declarative/minehunt/Description.qml new file mode 100644 index 0000000..df4881c --- /dev/null +++ b/demos/declarative/minehunt/Description.qml @@ -0,0 +1,34 @@ +import Qt 4.6 + +Item { + id: Page + height: MyText.height + 20 + property var text + MouseRegion { + anchors.fill: parent + drag.target: Page + drag.axis: "XandYAxis" + drag.minimumX: 0 + drag.maximumX: 1000 + drag.minimumY: 0 + drag.maximumY: 1000 + } + Rectangle { + radius: 10 + anchors.fill: parent + color: "lightsteelblue" + } + Item { + x: 10 + y: 10 + width: parent.width - 20 + height: parent.height - 20 + Text { + id: MyText + text: Page.text + width: parent.width + clip: true + wrap: true + } + } +} diff --git a/demos/declarative/minehunt/Explosion.qml b/demos/declarative/minehunt/Explosion.qml new file mode 100644 index 0000000..a997048 --- /dev/null +++ b/demos/declarative/minehunt/Explosion.qml @@ -0,0 +1,29 @@ +import Qt 4.6 + +Item { + property bool explode : false + + Particles { + id: particles + width: 40 + height: 40 + lifeSpan: 1000 + lifeSpanDeviation: 0 + source: "pics/star.png" + count: 0 + angle: 270 + angleDeviation: 360 + velocity: 100 + velocityDeviation: 20 + z: 100 + opacity: 0 + streamIn: false + } + states: [ State { name: "exploding"; when: explode == true + PropertyChanges { target: particles; count: 200 } + PropertyChanges { target: particles; opacity: 1 } + PropertyChanges { target: particles; emitting: false } // i.e. emit only once + } + ] + +} diff --git a/demos/declarative/minehunt/main.cpp b/demos/declarative/minehunt/main.cpp new file mode 100644 index 0000000..20f7492 --- /dev/null +++ b/demos/declarative/minehunt/main.cpp @@ -0,0 +1,309 @@ +#include "qmlengine.h" +#include "qmlcontext.h" +#include "qml.h" +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +QString fileName = "minehunt.qml"; + +class Tile : public QObject +{ + Q_OBJECT +public: + Tile() : _hasFlag(false), _hasMine(false), _hint(-1), _flipped(false) {} + + Q_PROPERTY(bool hasFlag READ hasFlag WRITE setHasFlag NOTIFY hasFlagChanged); + bool hasFlag() const { return _hasFlag; } + + Q_PROPERTY(bool hasMine READ hasMine NOTIFY hasMineChanged); + bool hasMine() const { return _hasMine; } + + Q_PROPERTY(int hint READ hint NOTIFY hintChanged); + int hint() const { return _hint; } + + Q_PROPERTY(bool flipped READ flipped NOTIFY flippedChanged()); + bool flipped() const { return _flipped; } + + void setHasFlag(bool flag) {if(flag==_hasFlag) return; _hasFlag = flag; emit hasFlagChanged();} + void setHasMine(bool mine) {if(mine==_hasMine) return; _hasMine = mine; emit hasMineChanged();} + void setHint(int hint) { if(hint == _hint) return; _hint = hint; emit hintChanged(); } + void flip() { if (_flipped) return; _flipped = true; emit flippedChanged(); } + void unflip() { if(!_flipped) return; _flipped = false; emit flippedChanged(); } + +signals: + void flippedChanged(); + void hasFlagChanged(); + void hintChanged(); + void hasMineChanged(); + +private: + bool _hasFlag; + bool _hasMine; + int _hint; + bool _flipped; +}; + +QML_DECLARE_TYPE(Tile); +QML_DEFINE_TYPE(0,0,0,0,Tile,Tile); + +class MyWidget : public QWidget +{ +Q_OBJECT +public: + MyWidget(int = 370, int = 480, QWidget *parent=0, Qt::WindowFlags flags=0); + ~MyWidget(); + + Q_PROPERTY(QList *tiles READ tiles CONSTANT); + QList *tiles() { return &_tiles; } + + Q_PROPERTY(bool isPlaying READ isPlaying NOTIFY isPlayingChanged); + bool isPlaying() {return playing;} + + Q_PROPERTY(bool hasWon READ hasWon NOTIFY hasWonChanged); + bool hasWon() {return won;} + + Q_PROPERTY(int numMines READ numMines NOTIFY numMinesChanged); + int numMines() const{return nMines;} + + Q_PROPERTY(int numFlags READ numFlags NOTIFY numFlagsChanged); + int numFlags() const{return nFlags;} + +public slots: + Q_INVOKABLE void flip(int row, int col); + Q_INVOKABLE void flag(int row, int col); + void setBoard(); + void reset(); + +signals: + void isPlayingChanged(); + void hasWonChanged(); + void numMinesChanged(); + void numFlagsChanged(); + +private: + bool onBoard( int r, int c ) const { return r >= 0 && r < numRows && c >= 0 && c < numCols; } + Tile *tile( int row, int col ) { return onBoard(row, col) ? _tiles[col+numRows*row] : 0; } + int getHint(int row, int col); + void setPlaying(bool b){if(b==playing) return; playing=b; emit isPlayingChanged();} + + QFxView *canvas; + + QList _tiles; + int numCols; + int numRows; + bool playing; + bool won; + int remaining; + int nMines; + int nFlags; +}; + +MyWidget::MyWidget(int width, int height, QWidget *parent, Qt::WindowFlags flags) +: QWidget(parent, flags), canvas(0), numCols(9), numRows(9), playing(true), won(false) +{ + setObjectName("mainWidget"); + srand(QTime(0,0,0).secsTo(QTime::currentTime())); + + //initialize array + for(int ii = 0; ii < numRows * numCols; ++ii) { + _tiles << new Tile; + } + + reset(); + + QVBoxLayout *vbox = new QVBoxLayout; + vbox->setMargin(0); + setLayout(vbox); + + canvas = new QFxView(this); + canvas->setFixedSize(width, height); + vbox->addWidget(canvas); + + QFile file(fileName); + file.open(QFile::ReadOnly); + QString qml = file.readAll(); + canvas->setQml(qml, fileName); + + QmlContext *ctxt = canvas->rootContext(); + ctxt->addDefaultObject(this); + + canvas->execute(); +} + +MyWidget::~MyWidget() +{ +} + +void MyWidget::setBoard() +{ + foreach(Tile* t, _tiles){ + t->setHasMine(false); + t->setHint(-1); + } + //place mines + int mines = nMines; + remaining = numRows*numCols-mines; + while ( mines ) { + int col = int((double(rand()) / double(RAND_MAX)) * numCols); + int row = int((double(rand()) / double(RAND_MAX)) * numRows); + + Tile* t = tile( row, col ); + + if (t && !t->hasMine()) { + t->setHasMine( true ); + mines--; + } + } + + //set hints + for (int r = 0; r < numRows; r++) + for (int c = 0; c < numCols; c++) { + Tile* t = tile(r, c); + if (t && !t->hasMine()) { + int hint = getHint(r,c); + t->setHint(hint); + } + } + + setPlaying(true); +} + +void MyWidget::reset() +{ + foreach(Tile* t, _tiles){ + t->unflip(); + t->setHasFlag(false); + } + nMines = 12; + nFlags = 0; + setPlaying(false); + QTimer::singleShot(900,this, SLOT(setBoard())); +} + +int MyWidget::getHint(int row, int col) +{ + int hint = 0; + for (int c = col-1; c <= col+1; c++) + for (int r = row-1; r <= row+1; r++) { + Tile* t = tile(r, c); + if (t && t->hasMine()) + hint++; + } + return hint; +} + +void MyWidget::flip(int row, int col) +{ + if(!playing) + return; + + Tile *t = tile(row, col); + if (!t || t->hasFlag()) + return; + + if(t->flipped()){ + int flags = 0; + for (int c = col-1; c <= col+1; c++) + for (int r = row-1; r <= row+1; r++) { + Tile *nearT = tile(r, c); + if(!nearT || nearT == t) + continue; + if(nearT->hasFlag()) + flags++; + } + if(!t->hint() || t->hint() != flags) + return; + for (int c = col-1; c <= col+1; c++) + for (int r = row-1; r <= row+1; r++) { + Tile *nearT = tile(r, c); + if (nearT && !nearT->flipped() && !nearT->hasFlag()) { + flip( r, c ); + } + } + return; + } + + t->flip(); + + if (t->hint() == 0) { + for (int c = col-1; c <= col+1; c++) + for (int r = row-1; r <= row+1; r++) { + Tile* t = tile(r, c); + if (t && !t->flipped()) { + flip( r, c ); + } + } + } + + if(t->hasMine()){ + for (int r = 0; r < numRows; r++)//Flip all other mines + for (int c = 0; c < numCols; c++) { + Tile* t = tile(r, c); + if (t && t->hasMine()) { + flip(r, c); + } + } + won = false; + hasWonChanged(); + setPlaying(false); + } + + remaining--; + if(!remaining){ + won = true; + hasWonChanged(); + setPlaying(false); + } +} + +void MyWidget::flag(int row, int col) +{ + Tile *t = tile(row, col); + if(!t) + return; + + t->setHasFlag(!t->hasFlag()); + nFlags += (t->hasFlag()?1:-1); + emit numFlagsChanged(); +} +///////////////////////////////////////////////////////// + +int main(int argc, char ** argv) +{ + QApplication app(argc, argv); + + bool frameless = false; + + int width = 370; + int height = 480; + + for (int i = 1; i < argc; ++i) { + QString arg = argv[i]; + if (arg == "-frameless") { + frameless = true; + } else if(arg == "-width" && i < (argc - 1)) { + ++i; + width = ::atoi(argv[i]); + } else if(arg == "-height" && i < (argc - 1)) { + ++i; + height = ::atoi(argv[i]); + } else if (arg[0] != '-') { + fileName = arg; + } + } + + MyWidget wid(width, height, 0, frameless ? Qt::FramelessWindowHint : Qt::Widget); + wid.show(); + + return app.exec(); +} + +#include "main.moc" diff --git a/demos/declarative/minehunt/minehunt.pro b/demos/declarative/minehunt/minehunt.pro new file mode 100644 index 0000000..01791b1 --- /dev/null +++ b/demos/declarative/minehunt/minehunt.pro @@ -0,0 +1,9 @@ +SOURCES = main.cpp + +QT += script declarative +contains(QT_CONFIG, opengles2)|contains(QT_CONFIG, opengles1): QT += opengl + +target.path = $$[QT_INSTALL_EXAMPLES]/declarative/minehunt +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS minehunt.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/declarative/minehunt +INSTALLS += target sources diff --git a/demos/declarative/minehunt/minehunt.qml b/demos/declarative/minehunt/minehunt.qml new file mode 100644 index 0000000..fa169aa --- /dev/null +++ b/demos/declarative/minehunt/minehunt.qml @@ -0,0 +1,195 @@ +import Qt 4.6 + +Item { + id: field + width: 370 + height: 480 + + property int clickx : 0 + property int clicky : 0 + + resources: [ + Component { + id: tile + Flipable { + id: flipable + width: 40 + height: 40 + property int angle: 0; + transform: Rotation { + origin.x: 20 + origin.y: 20 + axis.x: 1 + axis.z: 0 + angle: flipable.angle; + } + front: Image { + source: "pics/front.png" + width: 40 + height: 40 + Image { + anchors.horizontalCenter: parent.horizontalCenter + anchors.verticalCenter: parent.verticalCenter + source: "pics/flag.png" + opacity: modelData.hasFlag + opacity: Behavior { + NumberAnimation { + property: "opacity" + duration: 250 + } + } + } + } + back: Image { + source: "pics/back.png" + width: 40 + height: 40 + Text { + anchors.horizontalCenter: parent.horizontalCenter + anchors.verticalCenter: parent.verticalCenter + text: modelData.hint + color: "white" + font.bold: true + opacity: !modelData.hasMine && modelData.hint > 0 + } + Image { + anchors.horizontalCenter: parent.horizontalCenter + anchors.verticalCenter: parent.verticalCenter + source: "pics/bomb.png" + opacity: modelData.hasMine + } + Explosion { + id: expl + } + } + states: [ + State { + name: "back" + when: modelData.flipped + PropertyChanges { target: flipable; angle: 180 } + } + ] + transitions: [ + Transition { + SequentialAnimation { + PauseAnimation { + duration: { + var ret; + if(flipable.parent != null) + ret = Math.abs(flipable.parent.x-field.clickx) + + Math.abs(flipable.parent.y-field.clicky); + else + ret = 0; + if (ret > 0) { + if (modelData.hasMine && modelData.flipped) { + ret*3; + } else { + ret; + } + } else { + 0; + } + } + } + NumberAnimation { + easing: "easeInOutQuad" + properties: "angle" + } + ScriptAction{ + script: "if(modelData.hasMine && modelData.flipped){expl.explode = true;}" + } + } + } + ] + MouseRegion { + anchors.fill: parent + acceptedButtons: Qt.LeftButton | Qt.RightButton + onPressed: { + field.clickx = flipable.parent.x; + field.clicky = flipable.parent.y; + row = Math.floor(index/9); + col = index - (Math.floor(index/9) * 9); + if (mouse.button==undefined || mouse.button==Qt.RightButton) { + flag(row,col); + } else { + flip(row,col); + } + } + } + } + } + ] + Image { + source: "pics/No-Ones-Laughing-3.jpg" + fillMode: "Tile" + } + Description { + text: "Use the 'minehunt' executable to run this demo!" + width: 300 + opacity: tiles?0:1 + anchors.horizontalCenter: parent.horizontalCenter + anchors.verticalCenter: parent.verticalCenter + } + Repeater { + model: tiles + x: 1 + y: 1 + Component { + ComponentInstance { + component: tile + x: (index - (Math.floor(index/9) * 9)) * 41 + y: Math.floor(index/9) * 41 + } + } + } + Row { + id: gamedata + // width: 370 + // height: 100 + y: 400 + x: 20 + spacing: 20 + Column { + spacing: 2 + width: childrenRect.width + Image { + // x: 100 + // y: 20 + source: "pics/bomb-color.png" + } + Text { + // x: 100 + // y: 60 + anchors.horizontalCenter: parent.horizontalCenter + color: "white" + text: numMines + } + } + Column { + spacing: 2 + width: childrenRect.width + Image { + // x: 140 + // y: 20 + source: "pics/flag-color.png" + } + Text { + // x: 140 + // y: 60 + anchors.horizontalCenter: parent.horizontalCenter + color: "white" + text: numFlags + } + } + } + Image { + y: 390 + anchors.right: field.right + anchors.rightMargin: 20 + source: isPlaying ? 'pics/face-smile.png' : hasWon ? 'pics/face-smile-big.png': 'pics/face-sad.png' + MouseRegion { + anchors.fill: parent + onPressed: { reset() } + } + } +} diff --git a/demos/declarative/minehunt/pics/No-Ones-Laughing-3.jpg b/demos/declarative/minehunt/pics/No-Ones-Laughing-3.jpg new file mode 100644 index 0000000..445567f Binary files /dev/null and b/demos/declarative/minehunt/pics/No-Ones-Laughing-3.jpg differ diff --git a/demos/declarative/minehunt/pics/back.png b/demos/declarative/minehunt/pics/back.png new file mode 100644 index 0000000..f6b3f0b Binary files /dev/null and b/demos/declarative/minehunt/pics/back.png differ diff --git a/demos/declarative/minehunt/pics/bomb-color.png b/demos/declarative/minehunt/pics/bomb-color.png new file mode 100644 index 0000000..61ad0a9 Binary files /dev/null and b/demos/declarative/minehunt/pics/bomb-color.png differ diff --git a/demos/declarative/minehunt/pics/bomb.png b/demos/declarative/minehunt/pics/bomb.png new file mode 100644 index 0000000..a992575 Binary files /dev/null and b/demos/declarative/minehunt/pics/bomb.png differ diff --git a/demos/declarative/minehunt/pics/face-sad.png b/demos/declarative/minehunt/pics/face-sad.png new file mode 100644 index 0000000..cf00aaf Binary files /dev/null and b/demos/declarative/minehunt/pics/face-sad.png differ diff --git a/demos/declarative/minehunt/pics/face-smile-big.png b/demos/declarative/minehunt/pics/face-smile-big.png new file mode 100644 index 0000000..f9c2335 Binary files /dev/null and b/demos/declarative/minehunt/pics/face-smile-big.png differ diff --git a/demos/declarative/minehunt/pics/face-smile.png b/demos/declarative/minehunt/pics/face-smile.png new file mode 100644 index 0000000..3d66d72 Binary files /dev/null and b/demos/declarative/minehunt/pics/face-smile.png differ diff --git a/demos/declarative/minehunt/pics/flag-color.png b/demos/declarative/minehunt/pics/flag-color.png new file mode 100644 index 0000000..aadad0f Binary files /dev/null and b/demos/declarative/minehunt/pics/flag-color.png differ diff --git a/demos/declarative/minehunt/pics/flag.png b/demos/declarative/minehunt/pics/flag.png new file mode 100644 index 0000000..39cde4d Binary files /dev/null and b/demos/declarative/minehunt/pics/flag.png differ diff --git a/demos/declarative/minehunt/pics/front.png b/demos/declarative/minehunt/pics/front.png new file mode 100644 index 0000000..834331b Binary files /dev/null and b/demos/declarative/minehunt/pics/front.png differ diff --git a/demos/declarative/minehunt/pics/star.png b/demos/declarative/minehunt/pics/star.png new file mode 100644 index 0000000..3772359 Binary files /dev/null and b/demos/declarative/minehunt/pics/star.png differ diff --git a/demos/declarative/twitter/content/AuthView.qml b/demos/declarative/twitter/content/AuthView.qml new file mode 100644 index 0000000..6bd98b9 --- /dev/null +++ b/demos/declarative/twitter/content/AuthView.qml @@ -0,0 +1,92 @@ +import Qt 4.6 +import "../../flickr/common" +import "../../flickr/mobile" + +Item { + id: wrapper + Column { + width: childrenRect.width; height:childrenRect.height; anchors.centerIn: parent + spacing: 20 + Row{ + width: childrenRect.width; height:childrenRect.height; + spacing: 4 + Text { + width: 100 + text: "Screen name:" + font.pointSize: 10; font.bold: true; color: "white"; style: "Raised"; styleColor: "black" + anchors.verticalCenter: parent.verticalCenter + horizontalAlignment: Qt.AlignRight + } + Item { + width: 160 + height: 28 + BorderImage { source: "../../flickr/mobile/images/lineedit.sci"; anchors.fill: parent } + TextInput{ + id: nameIn + width: parent.width - 8 + height: parent.height - 12 + anchors.centerIn: parent + maximumLength:21 + font.bold: true + color: "#151515"; highlightColor: "green" + KeyProxy { + id: Proxy + targets: [(tabber), (nameIn)] + focus: true + } + Item { + id: tabber + //Note: it's not working yet + Keys.onPressed: {if(event.key == Qt.Key_Tab){print('Tab works!'); passIn.focus = true; accept(); }} + } + } + } + } + Row{ + width: childrenRect.width; height:childrenRect.height; + spacing: 4 + Text { + width: 100 + text: "Password:" + font.pointSize: 10; font.bold: true; color: "white"; style: "Raised"; styleColor: "black" + anchors.verticalCenter: parent.verticalCenter + horizontalAlignment: Qt.AlignRight + } + Item { + width: 160 + height: 28 + BorderImage { source: "../../flickr/mobile/images/lineedit.sci"; anchors.fill: parent } + TextInput{ + id: passIn + width: parent.width - 8 + height: parent.height - 12 + anchors.centerIn: parent + maximumLength:21 + echoMode: 2 + font.bold: true + color: "#151515"; highlightColor: "green" + } + } + } + Item{ + width: childrenRect.width; anchors.horizontalCenter: parent.horizontalCenter + height: childrenRect.height + Button { + x: 10 + width: 100 + height: 32 + id: login + text: "Log in" + onClicked: {RssModel.authName=nameIn.text; RssModel.authPass=passIn.text; RssModel.tags='my timeline';} + } + Button { + x: 120 + width: 100 + height: 32 + id: guest + text: "Guest" + onClicked: RssModel.authName='inv' + } + } + } +} diff --git a/demos/declarative/twitter/content/FatDelegate.qml b/demos/declarative/twitter/content/FatDelegate.qml new file mode 100644 index 0000000..a2e9c39 --- /dev/null +++ b/demos/declarative/twitter/content/FatDelegate.qml @@ -0,0 +1,47 @@ +import Qt 4.6 +import "../../flickr/common" + +Component { + id: ListDelegate + Item { + id: Wrapper; width: Wrapper.ListView.view.width; height: if(txt.height > 58){txt.height+8}else{58}//50+4+4 + Script { + function handleLink(link){ + if(link.slice(0,3) == 'app'){ + setUser(link.slice(7)); + Screen.setMode(true); + }else if(link.slice(0,4) == 'http'){ + Qt.DesktopServices.openUrl(link); + } + } + function addTags(str){ + ret = str.replace(/@[a-zA-Z0-9_]+/g, '$&');//click to jump to user? + ret2 = ret.replace(/http:\/\/[^ \n\t]+/g, '$&');//surrounds http links with html link tags + return ret2; + } + } + Item { + id: MoveMe; height: parent.height + Rectangle { + id: BlackRect + color: "black"; opacity: Wrapper.ListView.index % 2 ? 0.2 : 0.3; height: Wrapper.height-2; width: Wrapper.width; y: 1 + } + Rectangle { + id: WhiteRect; x: 6; width: 50; height: 50; color: "white"; smooth: true + anchors.verticalCenter: parent.verticalCenter + + Loading { x: 1; y: 1; width: 48; height: 48; visible: RealImage.status != 1 } + Image { id: RealImage; source: userImage; x: 1; y: 1; width:48; height:48 } + } + Text { id:txt; y:4; x: 56 + text: '' + + ''+userScreenName + " from " +source + + "
" + addTags(statusText) + ""; + textFormat: Qt.RichText + color: "white"; color: "#cccccc"; style: "Raised"; styleColor: "black"; wrap: true + anchors.left: WhiteRect.right; anchors.right: BlackRect.right; anchors.leftMargin: 6; anchors.rightMargin: 6 + onLinkActivated: handleLink(link) + } + } + } +} diff --git a/demos/declarative/twitter/content/HomeTitleBar.qml b/demos/declarative/twitter/content/HomeTitleBar.qml new file mode 100644 index 0000000..878a83e --- /dev/null +++ b/demos/declarative/twitter/content/HomeTitleBar.qml @@ -0,0 +1,125 @@ +import Qt 4.6 +import "../../flickr/mobile" +import "../../flickr/common" + +Item { + id: TitleBar + + signal update() + onYChanged: state="" //When switching titlebars + + BorderImage { source: "../../flickr/mobile/images/titlebar.sci"; width: parent.width; height: parent.height + 14; y: -7 } + Item { + id: Container + width: (parent.width * 2) - 55 ; height: parent.height + + Script { + function accept() { + if(RssModel.authName == '' || RssModel.authPass == '') + return false;//Can't login like that + + var postData = "status=" + Editor.text; + var postman = new XMLHttpRequest(); + postman.open("POST", "http://twitter.com/statuses/update.xml", true, RssModel.authName, RssModel.authPass); + postman.onreadystatechange = function() { + if (postman.readyState == postman.DONE) { + TitleBar.update(); + } + } + postman.send(postData); + + TitleBar.state = "" + } + } + + Rectangle { + id: WhiteRect; x: 6; width: 50; height: 50; color: "white"; smooth: true + anchors.verticalCenter: parent.verticalCenter + + UserModel { user: RssModel.authName; id: userModel } + Component { id: imgDelegate; + Item { id: Wrapper + Loading { width:48; height:48; visible: realImage.status != 1 } + Image { source: image; width:48; height:48; id: realImage } + } + } + ListView { model: userModel.model; x:1; y:1; delegate: imgDelegate } + } + + Text { + id: CategoryText + anchors.left: parent.left; anchors.right: TagButton.left + anchors.leftMargin: 58; anchors.rightMargin: 10 + anchors.verticalCenter: parent.verticalCenter + elide: "ElideLeft" + text: "Timeline for " + RssModel.authName + font.pointSize: 10; font.bold: true; color: "white"; style: "Raised"; styleColor: "black" + } + + Button { + id: TagButton; x: TitleBar.width - 90; width: 85; height: 32; text: "New Post..." + anchors.verticalCenter: parent.verticalCenter; + onClicked: if (TitleBar.state == "Posting") accept(); else TitleBar.state = "Posting" + } + + Text { + id: charsLeftText; anchors.horizontalCenter: TagButton.horizontalCenter; + anchors.top: TagButton.bottom; anchors.topMargin: 2 + text: {140 - Editor.text.length;} visible: TitleBar.state == "Posting" + font.pointSize: 10; font.bold: true; color: "white"; style: "Raised"; styleColor: "black" + } + Item { + id: txtEdit; + anchors.left: TagButton.right; anchors.leftMargin: 5; y: 4 + anchors.right: parent.right; anchors.rightMargin: 40; height: parent.height - 9 + BorderImage { source: "../../flickr/mobile/images/lineedit.sci"; anchors.fill: parent } + + Binding {//TODO: Can this be a function, which also resets the cursor? And flashes? + when: Editor.text.length > 140 + target: Editor + property: "text" + value: Editor.text.slice(0,140) + } + TextEdit { + id: Editor + anchors.left: parent.left; + anchors.leftMargin: 8; + anchors.bottom: parent.bottom + anchors.bottomMargin: 4; + cursorVisible: true; font.bold: true + width: parent.width - 12 + height: parent.height - 8 + font.pointSize: 10 + wrap:true + color: "#151515"; highlightColor: "green" + } + KeyProxy { + id: Proxy + anchors.fill: parent + targets: [(ReturnKey), (Editor)] + } + Item { + id: ReturnKey + Keys.onReturnPressed: accept() + Keys.onEscapePressed: TitleBar.state = "" + } + } + } + states: [ + State { + name: "Posting" + PropertyChanges { target: Container; x: -TagButton.x + 5 } + PropertyChanges { target: TitleBar; height: 80 } + PropertyChanges { target: TagButton; text: "OK" } + PropertyChanges { target: TagButton; width: 28 } + PropertyChanges { target: TagButton; height: 24 } + PropertyChanges { target: Proxy; focus: true } + } + ] + transitions: [ + Transition { + from: "*"; to: "*" + NumberAnimation { properties: "x,y,width,height"; easing: "easeInOutQuad" } + } + ] +} diff --git a/demos/declarative/twitter/content/MultiTitleBar.qml b/demos/declarative/twitter/content/MultiTitleBar.qml new file mode 100644 index 0000000..6a6a28f --- /dev/null +++ b/demos/declarative/twitter/content/MultiTitleBar.qml @@ -0,0 +1,25 @@ +import Qt 4.6 +import "../../flickr/mobile" + +Item { + height: HomeBar.height + HomeTitleBar { id: HomeBar; width: parent.width; height: 60; + onUpdate: RssModel.reload() + } + TitleBar { id: TitleBar; width: parent.width; height: 60; + y: -80 + untaggedString: "Latest tweets from everyone" + taggedString: "Latest tweets from " + } + states: [ + State { + name: "search"; when: Screen.userView + PropertyChanges { target: TitleBar; y: 0 } + PropertyChanges { target: HomeBar; y: -80 } + } + ] + transitions: [ + Transition { NumberAnimation { properties: "x,y"; duration: 500; easing: "easeInOutQuad" } } + ] +} + diff --git a/demos/declarative/twitter/content/RssModel.qml b/demos/declarative/twitter/content/RssModel.qml new file mode 100644 index 0000000..144d7af --- /dev/null +++ b/demos/declarative/twitter/content/RssModel.qml @@ -0,0 +1,42 @@ +import Qt 4.6 + +Item { id: wrapper + property var model: xmlModel + property string tags : "" + property string authName : "" + property string authPass : "" + property string mode : "everyone" + property int status: xmlModel.status + function reload() { xmlModel.reload(); } +XmlListModel { + id: xmlModel + + source: if (wrapper.authName == ""){ + ""; //Avoid worthless calls to twitter servers + }else if(wrapper.mode == 'user'){ + "https://"+ ((wrapper.authName!="" && wrapper.authPass!="")? (wrapper.authName+":"+wrapper.authPass+"@") : "" )+"twitter.com/statuses/user_timeline.xml?screen_name="+wrapper.tags; + }else if(wrapper.mode == 'self'){ + "https://"+ ((wrapper.authName!="" && wrapper.authPass!="")? (wrapper.authName+":"+wrapper.authPass+"@") : "" )+"twitter.com/statuses/friends_timeline.xml"; + }else{//everyone/public + "http://twitter.com/statuses/public_timeline.xml"; + } + query: "/statuses/status" + + XmlRole { name: "statusText"; query: "text/string()" } + XmlRole { name: "timestamp"; query: "created_at/string()" } + XmlRole { name: "source"; query: "source/string()" } + XmlRole { name: "userName"; query: "user/name/string()" } + XmlRole { name: "userScreenName"; query: "user/screen_name/string()" } + XmlRole { name: "userImage"; query: "user/profile_image_url/string()" } + XmlRole { name: "userLocation"; query: "user/location/string()" } + XmlRole { name: "userDescription"; query: "user/description/string()" } + XmlRole { name: "userFollowers"; query: "user/followers_count/string()" } + XmlRole { name: "userStatuses"; query: "user/statuses_count/string()" } + //TODO: Could also get the user's color scheme, timezone and a few other things +} +Binding { + property: "mode" + target: wrapper + value: {if(wrapper.tags==''){"everyone";}else if(wrapper.tags=='my timeline'){"self";}else{"user";}} +} +} diff --git a/demos/declarative/twitter/content/UserModel.qml b/demos/declarative/twitter/content/UserModel.qml new file mode 100644 index 0000000..c146b84 --- /dev/null +++ b/demos/declarative/twitter/content/UserModel.qml @@ -0,0 +1,26 @@ +import Qt 4.6 + +//This "model" gets the user information about the searched user. Mainly for the icon. +//Copied from RssModel + +Item { id: wrapper + property var model: xmlModel + property string user : "" + property int status: xmlModel.status + function reload() { xmlModel.reload(); } +XmlListModel { + id: xmlModel + + source: {if(user!="") {"http://twitter.com/users/show.xml?screen_name="+user;}else{"";}} + query: "/user" + + XmlRole { name: "name"; query: "name/string()" } + XmlRole { name: "screenName"; query: "screen_name/string()" } + XmlRole { name: "image"; query: "profile_image_url/string()" } + XmlRole { name: "location"; query: "location/string()" } + XmlRole { name: "description"; query: "description/string()" } + XmlRole { name: "followers"; query: "followers_count/string()" } + //XmlRole { name: "protected"; query: "protected/bool()" } + //TODO: Could also get the user's color scheme, timezone and a few other things +} +} diff --git a/demos/declarative/twitter/twitter.qml b/demos/declarative/twitter/twitter.qml new file mode 100644 index 0000000..566832b --- /dev/null +++ b/demos/declarative/twitter/twitter.qml @@ -0,0 +1,87 @@ +import Qt 4.6 +import "content" as Twitter +import "../flickr/common" as Common +import "../flickr/mobile" as Mobile + +Item { + id: Screen; width: 320; height: 480 + property bool userView : false + function setMode(m){ + Screen.userView = m; + if(m == false){ + RssModel.tags='my timeline'; + RssModel.reload(); + ToolBar.button2Label = "View others"; + } else { + ToolBar.button2Label = "Return home"; + } + } + //Workaround for bug 260266 + Timer{ interval: 1; running: false; repeat: false; onTriggered: reallySetUser(); id:hack } + Script{ + var tmpStr; + function setUser(str){hack.running = true; tmpStr = str} + function reallySetUser(){RssModel.tags = tmpStr;} + } + + Rectangle { + id: Background + anchors.fill: parent; color: "#343434"; + + Image { source: "mobile/images/stripes.png"; fillMode: "Tile"; anchors.fill: parent; opacity: 0.3 } + + Twitter.RssModel { id: RssModel } + Common.Loading { anchors.centerIn: parent; visible: RssModel.status && state!='unauthed'} + + Item { + id: Views + x: 2; width: parent.width - 4 + y:60 //Below the title bars + height: 320 + + Twitter.AuthView{ + id: authView + anchors.verticalCenter: parent.verticalCenter + width: parent.width; height: parent.height-60; + x: -(Screen.width * 1.5) + } + + Twitter.FatDelegate { id: FatDelegate } + ListView { + id: MainView; model: RssModel.model; delegate: FatDelegate; + width: parent.width; height: parent.height; x: 0; cacheBuffer: 100; + } + } + + Twitter.MultiTitleBar { id: TitleBar; width: parent.width } + Mobile.ToolBar { id: ToolBar; height: 40; + //anchors.bottom: parent.bottom; + //TODO: Use anchor changes instead of hard coding + y: 440 + width: parent.width; opacity: 0.9 + button1Label: "Update" + button2Label: "View others" + onButton2Clicked: + { + if(Screen.userView == true){ + Screen.setMode(false); + }else{ + Screen.setMode(true); + } + } + } + + states: [ + State { + name: "unauthed"; when: RssModel.authName=="" + PropertyChanges { target: authView; x: 0 } + PropertyChanges { target: MainView; x: -(parent.width * 1.5) } + PropertyChanges { target: TitleBar; y: -80 } + PropertyChanges { target: ToolBar; y: Screen.height + 80 } + } + ] + transitions: [ + Transition { NumberAnimation { properties: "x,y"; duration: 500; easing: "easeInOutQuad" } } + ] + } +} diff --git a/examples/declarative/minehunt/Description.qml b/examples/declarative/minehunt/Description.qml deleted file mode 100644 index df4881c..0000000 --- a/examples/declarative/minehunt/Description.qml +++ /dev/null @@ -1,34 +0,0 @@ -import Qt 4.6 - -Item { - id: Page - height: MyText.height + 20 - property var text - MouseRegion { - anchors.fill: parent - drag.target: Page - drag.axis: "XandYAxis" - drag.minimumX: 0 - drag.maximumX: 1000 - drag.minimumY: 0 - drag.maximumY: 1000 - } - Rectangle { - radius: 10 - anchors.fill: parent - color: "lightsteelblue" - } - Item { - x: 10 - y: 10 - width: parent.width - 20 - height: parent.height - 20 - Text { - id: MyText - text: Page.text - width: parent.width - clip: true - wrap: true - } - } -} diff --git a/examples/declarative/minehunt/Explosion.qml b/examples/declarative/minehunt/Explosion.qml deleted file mode 100644 index a997048..0000000 --- a/examples/declarative/minehunt/Explosion.qml +++ /dev/null @@ -1,29 +0,0 @@ -import Qt 4.6 - -Item { - property bool explode : false - - Particles { - id: particles - width: 40 - height: 40 - lifeSpan: 1000 - lifeSpanDeviation: 0 - source: "pics/star.png" - count: 0 - angle: 270 - angleDeviation: 360 - velocity: 100 - velocityDeviation: 20 - z: 100 - opacity: 0 - streamIn: false - } - states: [ State { name: "exploding"; when: explode == true - PropertyChanges { target: particles; count: 200 } - PropertyChanges { target: particles; opacity: 1 } - PropertyChanges { target: particles; emitting: false } // i.e. emit only once - } - ] - -} diff --git a/examples/declarative/minehunt/main.cpp b/examples/declarative/minehunt/main.cpp deleted file mode 100644 index 20f7492..0000000 --- a/examples/declarative/minehunt/main.cpp +++ /dev/null @@ -1,309 +0,0 @@ -#include "qmlengine.h" -#include "qmlcontext.h" -#include "qml.h" -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -QString fileName = "minehunt.qml"; - -class Tile : public QObject -{ - Q_OBJECT -public: - Tile() : _hasFlag(false), _hasMine(false), _hint(-1), _flipped(false) {} - - Q_PROPERTY(bool hasFlag READ hasFlag WRITE setHasFlag NOTIFY hasFlagChanged); - bool hasFlag() const { return _hasFlag; } - - Q_PROPERTY(bool hasMine READ hasMine NOTIFY hasMineChanged); - bool hasMine() const { return _hasMine; } - - Q_PROPERTY(int hint READ hint NOTIFY hintChanged); - int hint() const { return _hint; } - - Q_PROPERTY(bool flipped READ flipped NOTIFY flippedChanged()); - bool flipped() const { return _flipped; } - - void setHasFlag(bool flag) {if(flag==_hasFlag) return; _hasFlag = flag; emit hasFlagChanged();} - void setHasMine(bool mine) {if(mine==_hasMine) return; _hasMine = mine; emit hasMineChanged();} - void setHint(int hint) { if(hint == _hint) return; _hint = hint; emit hintChanged(); } - void flip() { if (_flipped) return; _flipped = true; emit flippedChanged(); } - void unflip() { if(!_flipped) return; _flipped = false; emit flippedChanged(); } - -signals: - void flippedChanged(); - void hasFlagChanged(); - void hintChanged(); - void hasMineChanged(); - -private: - bool _hasFlag; - bool _hasMine; - int _hint; - bool _flipped; -}; - -QML_DECLARE_TYPE(Tile); -QML_DEFINE_TYPE(0,0,0,0,Tile,Tile); - -class MyWidget : public QWidget -{ -Q_OBJECT -public: - MyWidget(int = 370, int = 480, QWidget *parent=0, Qt::WindowFlags flags=0); - ~MyWidget(); - - Q_PROPERTY(QList *tiles READ tiles CONSTANT); - QList *tiles() { return &_tiles; } - - Q_PROPERTY(bool isPlaying READ isPlaying NOTIFY isPlayingChanged); - bool isPlaying() {return playing;} - - Q_PROPERTY(bool hasWon READ hasWon NOTIFY hasWonChanged); - bool hasWon() {return won;} - - Q_PROPERTY(int numMines READ numMines NOTIFY numMinesChanged); - int numMines() const{return nMines;} - - Q_PROPERTY(int numFlags READ numFlags NOTIFY numFlagsChanged); - int numFlags() const{return nFlags;} - -public slots: - Q_INVOKABLE void flip(int row, int col); - Q_INVOKABLE void flag(int row, int col); - void setBoard(); - void reset(); - -signals: - void isPlayingChanged(); - void hasWonChanged(); - void numMinesChanged(); - void numFlagsChanged(); - -private: - bool onBoard( int r, int c ) const { return r >= 0 && r < numRows && c >= 0 && c < numCols; } - Tile *tile( int row, int col ) { return onBoard(row, col) ? _tiles[col+numRows*row] : 0; } - int getHint(int row, int col); - void setPlaying(bool b){if(b==playing) return; playing=b; emit isPlayingChanged();} - - QFxView *canvas; - - QList _tiles; - int numCols; - int numRows; - bool playing; - bool won; - int remaining; - int nMines; - int nFlags; -}; - -MyWidget::MyWidget(int width, int height, QWidget *parent, Qt::WindowFlags flags) -: QWidget(parent, flags), canvas(0), numCols(9), numRows(9), playing(true), won(false) -{ - setObjectName("mainWidget"); - srand(QTime(0,0,0).secsTo(QTime::currentTime())); - - //initialize array - for(int ii = 0; ii < numRows * numCols; ++ii) { - _tiles << new Tile; - } - - reset(); - - QVBoxLayout *vbox = new QVBoxLayout; - vbox->setMargin(0); - setLayout(vbox); - - canvas = new QFxView(this); - canvas->setFixedSize(width, height); - vbox->addWidget(canvas); - - QFile file(fileName); - file.open(QFile::ReadOnly); - QString qml = file.readAll(); - canvas->setQml(qml, fileName); - - QmlContext *ctxt = canvas->rootContext(); - ctxt->addDefaultObject(this); - - canvas->execute(); -} - -MyWidget::~MyWidget() -{ -} - -void MyWidget::setBoard() -{ - foreach(Tile* t, _tiles){ - t->setHasMine(false); - t->setHint(-1); - } - //place mines - int mines = nMines; - remaining = numRows*numCols-mines; - while ( mines ) { - int col = int((double(rand()) / double(RAND_MAX)) * numCols); - int row = int((double(rand()) / double(RAND_MAX)) * numRows); - - Tile* t = tile( row, col ); - - if (t && !t->hasMine()) { - t->setHasMine( true ); - mines--; - } - } - - //set hints - for (int r = 0; r < numRows; r++) - for (int c = 0; c < numCols; c++) { - Tile* t = tile(r, c); - if (t && !t->hasMine()) { - int hint = getHint(r,c); - t->setHint(hint); - } - } - - setPlaying(true); -} - -void MyWidget::reset() -{ - foreach(Tile* t, _tiles){ - t->unflip(); - t->setHasFlag(false); - } - nMines = 12; - nFlags = 0; - setPlaying(false); - QTimer::singleShot(900,this, SLOT(setBoard())); -} - -int MyWidget::getHint(int row, int col) -{ - int hint = 0; - for (int c = col-1; c <= col+1; c++) - for (int r = row-1; r <= row+1; r++) { - Tile* t = tile(r, c); - if (t && t->hasMine()) - hint++; - } - return hint; -} - -void MyWidget::flip(int row, int col) -{ - if(!playing) - return; - - Tile *t = tile(row, col); - if (!t || t->hasFlag()) - return; - - if(t->flipped()){ - int flags = 0; - for (int c = col-1; c <= col+1; c++) - for (int r = row-1; r <= row+1; r++) { - Tile *nearT = tile(r, c); - if(!nearT || nearT == t) - continue; - if(nearT->hasFlag()) - flags++; - } - if(!t->hint() || t->hint() != flags) - return; - for (int c = col-1; c <= col+1; c++) - for (int r = row-1; r <= row+1; r++) { - Tile *nearT = tile(r, c); - if (nearT && !nearT->flipped() && !nearT->hasFlag()) { - flip( r, c ); - } - } - return; - } - - t->flip(); - - if (t->hint() == 0) { - for (int c = col-1; c <= col+1; c++) - for (int r = row-1; r <= row+1; r++) { - Tile* t = tile(r, c); - if (t && !t->flipped()) { - flip( r, c ); - } - } - } - - if(t->hasMine()){ - for (int r = 0; r < numRows; r++)//Flip all other mines - for (int c = 0; c < numCols; c++) { - Tile* t = tile(r, c); - if (t && t->hasMine()) { - flip(r, c); - } - } - won = false; - hasWonChanged(); - setPlaying(false); - } - - remaining--; - if(!remaining){ - won = true; - hasWonChanged(); - setPlaying(false); - } -} - -void MyWidget::flag(int row, int col) -{ - Tile *t = tile(row, col); - if(!t) - return; - - t->setHasFlag(!t->hasFlag()); - nFlags += (t->hasFlag()?1:-1); - emit numFlagsChanged(); -} -///////////////////////////////////////////////////////// - -int main(int argc, char ** argv) -{ - QApplication app(argc, argv); - - bool frameless = false; - - int width = 370; - int height = 480; - - for (int i = 1; i < argc; ++i) { - QString arg = argv[i]; - if (arg == "-frameless") { - frameless = true; - } else if(arg == "-width" && i < (argc - 1)) { - ++i; - width = ::atoi(argv[i]); - } else if(arg == "-height" && i < (argc - 1)) { - ++i; - height = ::atoi(argv[i]); - } else if (arg[0] != '-') { - fileName = arg; - } - } - - MyWidget wid(width, height, 0, frameless ? Qt::FramelessWindowHint : Qt::Widget); - wid.show(); - - return app.exec(); -} - -#include "main.moc" diff --git a/examples/declarative/minehunt/minehunt.pro b/examples/declarative/minehunt/minehunt.pro deleted file mode 100644 index 01791b1..0000000 --- a/examples/declarative/minehunt/minehunt.pro +++ /dev/null @@ -1,9 +0,0 @@ -SOURCES = main.cpp - -QT += script declarative -contains(QT_CONFIG, opengles2)|contains(QT_CONFIG, opengles1): QT += opengl - -target.path = $$[QT_INSTALL_EXAMPLES]/declarative/minehunt -sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS minehunt.pro -sources.path = $$[QT_INSTALL_EXAMPLES]/declarative/minehunt -INSTALLS += target sources diff --git a/examples/declarative/minehunt/minehunt.qml b/examples/declarative/minehunt/minehunt.qml deleted file mode 100644 index fa169aa..0000000 --- a/examples/declarative/minehunt/minehunt.qml +++ /dev/null @@ -1,195 +0,0 @@ -import Qt 4.6 - -Item { - id: field - width: 370 - height: 480 - - property int clickx : 0 - property int clicky : 0 - - resources: [ - Component { - id: tile - Flipable { - id: flipable - width: 40 - height: 40 - property int angle: 0; - transform: Rotation { - origin.x: 20 - origin.y: 20 - axis.x: 1 - axis.z: 0 - angle: flipable.angle; - } - front: Image { - source: "pics/front.png" - width: 40 - height: 40 - Image { - anchors.horizontalCenter: parent.horizontalCenter - anchors.verticalCenter: parent.verticalCenter - source: "pics/flag.png" - opacity: modelData.hasFlag - opacity: Behavior { - NumberAnimation { - property: "opacity" - duration: 250 - } - } - } - } - back: Image { - source: "pics/back.png" - width: 40 - height: 40 - Text { - anchors.horizontalCenter: parent.horizontalCenter - anchors.verticalCenter: parent.verticalCenter - text: modelData.hint - color: "white" - font.bold: true - opacity: !modelData.hasMine && modelData.hint > 0 - } - Image { - anchors.horizontalCenter: parent.horizontalCenter - anchors.verticalCenter: parent.verticalCenter - source: "pics/bomb.png" - opacity: modelData.hasMine - } - Explosion { - id: expl - } - } - states: [ - State { - name: "back" - when: modelData.flipped - PropertyChanges { target: flipable; angle: 180 } - } - ] - transitions: [ - Transition { - SequentialAnimation { - PauseAnimation { - duration: { - var ret; - if(flipable.parent != null) - ret = Math.abs(flipable.parent.x-field.clickx) - + Math.abs(flipable.parent.y-field.clicky); - else - ret = 0; - if (ret > 0) { - if (modelData.hasMine && modelData.flipped) { - ret*3; - } else { - ret; - } - } else { - 0; - } - } - } - NumberAnimation { - easing: "easeInOutQuad" - properties: "angle" - } - ScriptAction{ - script: "if(modelData.hasMine && modelData.flipped){expl.explode = true;}" - } - } - } - ] - MouseRegion { - anchors.fill: parent - acceptedButtons: Qt.LeftButton | Qt.RightButton - onPressed: { - field.clickx = flipable.parent.x; - field.clicky = flipable.parent.y; - row = Math.floor(index/9); - col = index - (Math.floor(index/9) * 9); - if (mouse.button==undefined || mouse.button==Qt.RightButton) { - flag(row,col); - } else { - flip(row,col); - } - } - } - } - } - ] - Image { - source: "pics/No-Ones-Laughing-3.jpg" - fillMode: "Tile" - } - Description { - text: "Use the 'minehunt' executable to run this demo!" - width: 300 - opacity: tiles?0:1 - anchors.horizontalCenter: parent.horizontalCenter - anchors.verticalCenter: parent.verticalCenter - } - Repeater { - model: tiles - x: 1 - y: 1 - Component { - ComponentInstance { - component: tile - x: (index - (Math.floor(index/9) * 9)) * 41 - y: Math.floor(index/9) * 41 - } - } - } - Row { - id: gamedata - // width: 370 - // height: 100 - y: 400 - x: 20 - spacing: 20 - Column { - spacing: 2 - width: childrenRect.width - Image { - // x: 100 - // y: 20 - source: "pics/bomb-color.png" - } - Text { - // x: 100 - // y: 60 - anchors.horizontalCenter: parent.horizontalCenter - color: "white" - text: numMines - } - } - Column { - spacing: 2 - width: childrenRect.width - Image { - // x: 140 - // y: 20 - source: "pics/flag-color.png" - } - Text { - // x: 140 - // y: 60 - anchors.horizontalCenter: parent.horizontalCenter - color: "white" - text: numFlags - } - } - } - Image { - y: 390 - anchors.right: field.right - anchors.rightMargin: 20 - source: isPlaying ? 'pics/face-smile.png' : hasWon ? 'pics/face-smile-big.png': 'pics/face-sad.png' - MouseRegion { - anchors.fill: parent - onPressed: { reset() } - } - } -} diff --git a/examples/declarative/minehunt/pics/No-Ones-Laughing-3.jpg b/examples/declarative/minehunt/pics/No-Ones-Laughing-3.jpg deleted file mode 100644 index 445567f..0000000 Binary files a/examples/declarative/minehunt/pics/No-Ones-Laughing-3.jpg and /dev/null differ diff --git a/examples/declarative/minehunt/pics/back.png b/examples/declarative/minehunt/pics/back.png deleted file mode 100644 index f6b3f0b..0000000 Binary files a/examples/declarative/minehunt/pics/back.png and /dev/null differ diff --git a/examples/declarative/minehunt/pics/bomb-color.png b/examples/declarative/minehunt/pics/bomb-color.png deleted file mode 100644 index 61ad0a9..0000000 Binary files a/examples/declarative/minehunt/pics/bomb-color.png and /dev/null differ diff --git a/examples/declarative/minehunt/pics/bomb.png b/examples/declarative/minehunt/pics/bomb.png deleted file mode 100644 index a992575..0000000 Binary files a/examples/declarative/minehunt/pics/bomb.png and /dev/null differ diff --git a/examples/declarative/minehunt/pics/face-sad.png b/examples/declarative/minehunt/pics/face-sad.png deleted file mode 100644 index cf00aaf..0000000 Binary files a/examples/declarative/minehunt/pics/face-sad.png and /dev/null differ diff --git a/examples/declarative/minehunt/pics/face-smile-big.png b/examples/declarative/minehunt/pics/face-smile-big.png deleted file mode 100644 index f9c2335..0000000 Binary files a/examples/declarative/minehunt/pics/face-smile-big.png and /dev/null differ diff --git a/examples/declarative/minehunt/pics/face-smile.png b/examples/declarative/minehunt/pics/face-smile.png deleted file mode 100644 index 3d66d72..0000000 Binary files a/examples/declarative/minehunt/pics/face-smile.png and /dev/null differ diff --git a/examples/declarative/minehunt/pics/flag-color.png b/examples/declarative/minehunt/pics/flag-color.png deleted file mode 100644 index aadad0f..0000000 Binary files a/examples/declarative/minehunt/pics/flag-color.png and /dev/null differ diff --git a/examples/declarative/minehunt/pics/flag.png b/examples/declarative/minehunt/pics/flag.png deleted file mode 100644 index 39cde4d..0000000 Binary files a/examples/declarative/minehunt/pics/flag.png and /dev/null differ diff --git a/examples/declarative/minehunt/pics/front.png b/examples/declarative/minehunt/pics/front.png deleted file mode 100644 index 834331b..0000000 Binary files a/examples/declarative/minehunt/pics/front.png and /dev/null differ diff --git a/examples/declarative/minehunt/pics/star.png b/examples/declarative/minehunt/pics/star.png deleted file mode 100644 index 3772359..0000000 Binary files a/examples/declarative/minehunt/pics/star.png and /dev/null differ -- cgit v0.12