summaryrefslogtreecommitdiffstats
path: root/src/declarative
diff options
context:
space:
mode:
authorRhys Weatherley <rhys.weatherley@nokia.com>2009-08-21 03:17:18 (GMT)
committerRhys Weatherley <rhys.weatherley@nokia.com>2009-08-21 03:17:18 (GMT)
commit549b45cd6b8201daaf889986278c75a6c7f62cf8 (patch)
tree6fb5ad47ff891d8dd54aa9aaa55c29ba635d2e8a /src/declarative
parent7eb52559f44de7666c31ea28ce557eaca9177e9d (diff)
downloadQt-549b45cd6b8201daaf889986278c75a6c7f62cf8.zip
Qt-549b45cd6b8201daaf889986278c75a6c7f62cf8.tar.gz
Qt-549b45cd6b8201daaf889986278c75a6c7f62cf8.tar.bz2
Add vector() function as a built-in to QML
This makes it easier and more declarative to specify rotation axes: axis: vector(0, 1, 0) instead of: axis.x: 0; axis.y: 1; axis.z: 0 Reviewed-by: Martin Jones
Diffstat (limited to 'src/declarative')
-rw-r--r--src/declarative/qml/qmlengine.cpp41
-rw-r--r--src/declarative/qml/qmlengine_p.h1
2 files changed, 42 insertions, 0 deletions
diff --git a/src/declarative/qml/qmlengine.cpp b/src/declarative/qml/qmlengine.cpp
index 84c9553..7f2a3e1 100644
--- a/src/declarative/qml/qmlengine.cpp
+++ b/src/declarative/qml/qmlengine.cpp
@@ -158,6 +158,8 @@ void QmlEnginePrivate::init()
scriptEngine.newFunction(QmlEnginePrivate::createQmlObject, 1));
scriptEngine.globalObject().setProperty(QLatin1String("createComponent"),
scriptEngine.newFunction(QmlEnginePrivate::createComponent, 1));
+ scriptEngine.globalObject().setProperty(QLatin1String("vector"),
+ scriptEngine.newFunction(QmlEnginePrivate::vector, 1));
if (QCoreApplication::instance()->thread() == q->thread() &&
QmlEngineDebugServer::isDebuggingEnabled()) {
@@ -692,6 +694,45 @@ QScriptValue QmlEnginePrivate::createQmlObject(QScriptContext *ctxt, QScriptEngi
return engine->nullValue();
}
+/*!
+ This function is intended for use inside QML only. In C++ just create a
+ QVector3D as usual.
+
+ This function takes three numeric components and combines them into a
+ QMLJS string that can be used with any property that takes a
+ QVector3D argument. The following QML code:
+
+ \code
+ transform: Rotation {
+ id: Rotation
+ origin.x: Container.width / 2;
+ axis: vector(0, 1, 1)
+ }
+ \endcode
+
+ is equivalent to:
+
+ \code
+ transform: Rotation {
+ id: Rotation
+ origin.x: Container.width / 2;
+ axis.x: 0; axis.y: 1; axis.z: 0
+ }
+ \endcode
+*/
+QScriptValue QmlEnginePrivate::vector(QScriptContext *ctxt, QScriptEngine *engine)
+{
+ if(ctxt->argumentCount() < 3)
+ return engine->nullValue();
+ qsreal x = ctxt->argument(0).toNumber();
+ qsreal y = ctxt->argument(1).toNumber();
+ qsreal z = ctxt->argument(2).toNumber();
+ QString s = QString::number(x) + QLatin1Char(',') +
+ QString::number(y) + QLatin1Char(',') +
+ QString::number(z);
+ return QScriptValue(s);
+}
+
QmlScriptClass::QmlScriptClass(QmlEngine *bindengine)
: QScriptClass(QmlEnginePrivate::getScriptEngine(bindengine)),
engine(bindengine)
diff --git a/src/declarative/qml/qmlengine_p.h b/src/declarative/qml/qmlengine_p.h
index aaf679f..4142872 100644
--- a/src/declarative/qml/qmlengine_p.h
+++ b/src/declarative/qml/qmlengine_p.h
@@ -204,6 +204,7 @@ public:
static QScriptValue qmlScriptObject(QObject*, QmlEngine*);
static QScriptValue createComponent(QScriptContext*, QScriptEngine*);
static QScriptValue createQmlObject(QScriptContext*, QScriptEngine*);
+ static QScriptValue vector(QScriptContext*, QScriptEngine*);
static QScriptEngine *getScriptEngine(QmlEngine *e) { return &e->d_func()->scriptEngine; }
static QmlEnginePrivate *get(QmlEngine *e) { return e->d_func(); }