diff options
-rw-r--r-- | doc/src/declarative/globalobject.qdoc | 3 | ||||
-rw-r--r-- | src/declarative/qml/qmlengine.cpp | 20 | ||||
-rw-r--r-- | src/declarative/qml/qmlengine_p.h | 1 | ||||
-rw-r--r-- | tests/auto/declarative/qmlqt/data/closestangle.qml | 12 | ||||
-rw-r--r-- | tests/auto/declarative/qmlqt/tst_qmlqt.cpp | 18 |
5 files changed, 54 insertions, 0 deletions
diff --git a/doc/src/declarative/globalobject.qdoc b/doc/src/declarative/globalobject.qdoc index f1d440f..06f6bdc 100644 --- a/doc/src/declarative/globalobject.qdoc +++ b/doc/src/declarative/globalobject.qdoc @@ -114,6 +114,9 @@ This function returns a color 50% darker than \c baseColor. See QColor::lighter( \image declarative-rect_tint.png Tint is most useful when a subtle change is intended to be conveyed due to some event; you can then use tinting to more effectively tune the visible color. +\section3 Qt.closestAngle(number fromAngle, number toAngle) +This function returns an equivalent angle to toAngle, such that the difference between fromAngle and toAngle is never more than 180 degrees. This is useful when animating angles using a NumberAnimation, which does not know about equivalent angles, when you always want to take the shortest path. + \section3 Qt.playSound(url soundLocation) This function plays the audio file located at \c soundLocation. Only .wav files are supported. diff --git a/src/declarative/qml/qmlengine.cpp b/src/declarative/qml/qmlengine.cpp index c562e02..2926791 100644 --- a/src/declarative/qml/qmlengine.cpp +++ b/src/declarative/qml/qmlengine.cpp @@ -147,6 +147,7 @@ QmlEnginePrivate::QmlEnginePrivate(QmlEngine *e) qtObject.setProperty(QLatin1String("tint"), scriptEngine.newFunction(QmlEnginePrivate::tint, 2)); //misc methods + qtObject.setProperty(QLatin1String("closestAngle"), scriptEngine.newFunction(QmlEnginePrivate::closestAngle, 2)); qtObject.setProperty(QLatin1String("playSound"), scriptEngine.newFunction(QmlEnginePrivate::playSound, 1)); qtObject.setProperty(QLatin1String("openUrlExternally"),scriptEngine.newFunction(desktopOpenUrl, 1)); @@ -810,6 +811,25 @@ QScriptValue QmlEnginePrivate::desktopOpenUrl(QScriptContext *ctxt, QScriptEngin return e->newVariant(QVariant(ret)); } +QScriptValue QmlEnginePrivate::closestAngle(QScriptContext *ctxt, QScriptEngine *e) +{ + if(ctxt->argumentCount() < 2) + return e->newVariant(QVariant(0.0)); + qreal a = ctxt->argument(0).toNumber(); + qreal b = ctxt->argument(1).toNumber(); + qreal ret = b; + qreal diff = b-a; + while(diff > 180.0){ + ret -= 360.0; + diff -= 360.0; + } + while(diff < -180.0){ + ret += 360.0; + diff += 360.0; + } + return e->newVariant(QVariant(ret)); +} + QScriptValue QmlEnginePrivate::tint(QScriptContext *ctxt, QScriptEngine *engine) { if(ctxt->argumentCount() != 2) diff --git a/src/declarative/qml/qmlengine_p.h b/src/declarative/qml/qmlengine_p.h index b050ef6..3c60b5c 100644 --- a/src/declarative/qml/qmlengine_p.h +++ b/src/declarative/qml/qmlengine_p.h @@ -269,6 +269,7 @@ public: static QScriptValue darker(QScriptContext*, QScriptEngine*); static QScriptValue tint(QScriptContext*, QScriptEngine*); + static QScriptValue closestAngle(QScriptContext*, QScriptEngine*); static QScriptValue playSound(QScriptContext*, QScriptEngine*); static QScriptValue desktopOpenUrl(QScriptContext*, QScriptEngine*); diff --git a/tests/auto/declarative/qmlqt/data/closestangle.qml b/tests/auto/declarative/qmlqt/data/closestangle.qml new file mode 100644 index 0000000..8f999e3 --- /dev/null +++ b/tests/auto/declarative/qmlqt/data/closestangle.qml @@ -0,0 +1,12 @@ +import Qt 4.6 + +Object { + property var testSame: Qt.closestAngle(0,1) + property var testLess: Qt.closestAngle(0,-359) + property var testMore: Qt.closestAngle(0,361) + property var testFail: Qt.closestAngle(0) + property var test5: Qt.closestAngle(0,1,2) + property var test6: Qt.closestAngle(123.45465768,1.11) + property var test7: Qt.closestAngle(-3.1415,1.11) +} + diff --git a/tests/auto/declarative/qmlqt/tst_qmlqt.cpp b/tests/auto/declarative/qmlqt/tst_qmlqt.cpp index 10402ae..cc9b94d 100644 --- a/tests/auto/declarative/qmlqt/tst_qmlqt.cpp +++ b/tests/auto/declarative/qmlqt/tst_qmlqt.cpp @@ -64,6 +64,7 @@ private slots: void lighter(); void darker(); void tint(); + void closestAngle(); void playSound(); void openUrlExternally(); @@ -249,6 +250,23 @@ void tst_qmlqt::tint() delete object; } +void tst_qmlqt::closestAngle() +{ + QmlComponent component(&engine, TEST_FILE("closestangle.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(qvariant_cast<qreal>(object->property("testSame")), 1.0); + QCOMPARE(qvariant_cast<qreal>(object->property("testLess")), 1.0); + QCOMPARE(qvariant_cast<qreal>(object->property("testMore")), 1.0); + QCOMPARE(qvariant_cast<qreal>(object->property("testFail")), 0.0); + QCOMPARE(qvariant_cast<qreal>(object->property("test5")), 1.0); + QCOMPARE(qvariant_cast<qreal>(object->property("test6")), 1.11); + QCOMPARE(qvariant_cast<qreal>(object->property("test7")), 1.11); + + delete object; +} + void tst_qmlqt::playSound() { QEXPECT_FAIL("", "How do we test this?", Abort); |