summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/declarative/qml/qmlengine.cpp66
-rw-r--r--src/declarative/qml/qmlengine_p.h5
-rw-r--r--tests/auto/declarative/qmlengine/functions.qml6
-rw-r--r--tests/auto/declarative/qmlengine/tst_qmlengine.cpp87
4 files changed, 145 insertions, 19 deletions
diff --git a/src/declarative/qml/qmlengine.cpp b/src/declarative/qml/qmlengine.cpp
index ea00d65..fbd9cf1 100644
--- a/src/declarative/qml/qmlengine.cpp
+++ b/src/declarative/qml/qmlengine.cpp
@@ -71,6 +71,7 @@
#include <QtCore/qthread.h>
#include <QtCore/qcoreapplication.h>
#include <QtCore/qdir.h>
+#include <QtGui/qcolor.h>
#include <QtGui/qvector3d.h>
#include <qmlcomponent.h>
#include "private/qmlcomponentjs_p.h"
@@ -104,6 +105,12 @@ QmlEnginePrivate::QmlEnginePrivate(QmlEngine *e)
QScriptValue qtObject =
scriptEngine.newQMetaObject(StaticQtMetaObject::get());
scriptEngine.globalObject().setProperty(QLatin1String("Qt"), qtObject);
+ qtObject.setProperty(QLatin1String("rgba"), scriptEngine.newFunction(QmlEnginePrivate::rgba, 4));
+ qtObject.setProperty(QLatin1String("hsla"), scriptEngine.newFunction(QmlEnginePrivate::hsla, 4));
+ qtObject.setProperty(QLatin1String("rect"), scriptEngine.newFunction(QmlEnginePrivate::rect, 4));
+ qtObject.setProperty(QLatin1String("point"), scriptEngine.newFunction(QmlEnginePrivate::point, 2));
+ qtObject.setProperty(QLatin1String("size"), scriptEngine.newFunction(QmlEnginePrivate::size, 2));
+ qtObject.setProperty(QLatin1String("vector3d"), scriptEngine.newFunction(QmlEnginePrivate::vector, 3));
}
QmlEnginePrivate::~QmlEnginePrivate()
@@ -160,7 +167,7 @@ void QmlEnginePrivate::init()
scriptEngine.globalObject().setProperty(QLatin1String("createComponent"),
scriptEngine.newFunction(QmlEnginePrivate::createComponent, 1));
scriptEngine.globalObject().setProperty(QLatin1String("vector"),
- scriptEngine.newFunction(QmlEnginePrivate::vector, 1));
+ scriptEngine.newFunction(QmlEnginePrivate::vector, 3));
if (QCoreApplication::instance()->thread() == q->thread() &&
QmlEngineDebugServer::isDebuggingEnabled()) {
@@ -698,7 +705,7 @@ QScriptValue QmlEnginePrivate::createQmlObject(QScriptContext *ctxt, QScriptEngi
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 value that can be used with any property that takes a
QVector3D argument. The following QML code:
\code
@@ -729,6 +736,59 @@ QScriptValue QmlEnginePrivate::vector(QScriptContext *ctxt, QScriptEngine *engin
return engine->newVariant(qVariantFromValue(QVector3D(x, y, z)));
}
+QScriptValue QmlEnginePrivate::rgba(QScriptContext *ctxt, QScriptEngine *engine)
+{
+ int argCount = ctxt->argumentCount();
+ if(argCount < 3)
+ return engine->nullValue();
+ qsreal r = ctxt->argument(0).toNumber();
+ qsreal g = ctxt->argument(1).toNumber();
+ qsreal b = ctxt->argument(2).toNumber();
+ qsreal a = (argCount == 4) ? ctxt->argument(3).toNumber() : 1;
+ return qScriptValueFromValue(engine, qVariantFromValue(QColor::fromRgbF(r, g, b, a)));
+}
+
+QScriptValue QmlEnginePrivate::hsla(QScriptContext *ctxt, QScriptEngine *engine)
+{
+ int argCount = ctxt->argumentCount();
+ if(argCount < 3)
+ return engine->nullValue();
+ qsreal h = ctxt->argument(0).toNumber();
+ qsreal s = ctxt->argument(1).toNumber();
+ qsreal l = ctxt->argument(2).toNumber();
+ qsreal a = (argCount == 4) ? ctxt->argument(3).toNumber() : 1;
+ return qScriptValueFromValue(engine, qVariantFromValue(QColor::fromHslF(h, s, l, a)));
+}
+
+QScriptValue QmlEnginePrivate::rect(QScriptContext *ctxt, QScriptEngine *engine)
+{
+ if(ctxt->argumentCount() < 4)
+ return engine->nullValue();
+ qsreal x = ctxt->argument(0).toNumber();
+ qsreal y = ctxt->argument(1).toNumber();
+ qsreal w = ctxt->argument(2).toNumber();
+ qsreal h = ctxt->argument(3).toNumber();
+ return qScriptValueFromValue(engine, qVariantFromValue(QRectF(x, y, w, h)));
+}
+
+QScriptValue QmlEnginePrivate::point(QScriptContext *ctxt, QScriptEngine *engine)
+{
+ if(ctxt->argumentCount() < 2)
+ return engine->nullValue();
+ qsreal x = ctxt->argument(0).toNumber();
+ qsreal y = ctxt->argument(1).toNumber();
+ return qScriptValueFromValue(engine, qVariantFromValue(QPointF(x, y)));
+}
+
+QScriptValue QmlEnginePrivate::size(QScriptContext *ctxt, QScriptEngine *engine)
+{
+ if(ctxt->argumentCount() < 2)
+ return engine->nullValue();
+ qsreal w = ctxt->argument(0).toNumber();
+ qsreal h = ctxt->argument(1).toNumber();
+ return qScriptValueFromValue(engine, qVariantFromValue(QSizeF(w, h)));
+}
+
QmlScriptClass::QmlScriptClass(QmlEngine *bindengine)
: QScriptClass(QmlEnginePrivate::getScriptEngine(bindengine)),
engine(bindengine)
@@ -971,7 +1031,7 @@ QScriptValue QmlObjectToString(QScriptContext *context, QScriptEngine *engine)
ret += QString::number((quintptr)obj,16);
ret += QLatin1String(")");
}else{
- ret += "null";
+ ret += QLatin1String("null");
}
return engine->newVariant(ret);
}
diff --git a/src/declarative/qml/qmlengine_p.h b/src/declarative/qml/qmlengine_p.h
index 53b2967..c2de72b 100644
--- a/src/declarative/qml/qmlengine_p.h
+++ b/src/declarative/qml/qmlengine_p.h
@@ -214,6 +214,11 @@ public:
static QScriptValue createComponent(QScriptContext*, QScriptEngine*);
static QScriptValue createQmlObject(QScriptContext*, QScriptEngine*);
static QScriptValue vector(QScriptContext*, QScriptEngine*);
+ static QScriptValue rgba(QScriptContext*, QScriptEngine*);
+ static QScriptValue hsla(QScriptContext*, QScriptEngine*);
+ static QScriptValue point(QScriptContext*, QScriptEngine*);
+ static QScriptValue size(QScriptContext*, QScriptEngine*);
+ static QScriptValue rect(QScriptContext*, QScriptEngine*);
static QScriptEngine *getScriptEngine(QmlEngine *e) { return &e->d_func()->scriptEngine; }
static QmlEnginePrivate *get(QmlEngine *e) { return e->d_func(); }
diff --git a/tests/auto/declarative/qmlengine/functions.qml b/tests/auto/declarative/qmlengine/functions.qml
new file mode 100644
index 0000000..28e8ed4
--- /dev/null
+++ b/tests/auto/declarative/qmlengine/functions.qml
@@ -0,0 +1,6 @@
+import Test 1.0
+
+MyTypeObject {
+ rectProperty: Qt.rect(0,0,100,100)
+ rectFProperty: Qt.rect(0,0.5,100,99.5)
+}
diff --git a/tests/auto/declarative/qmlengine/tst_qmlengine.cpp b/tests/auto/declarative/qmlengine/tst_qmlengine.cpp
index 9a04c61..8c050cb 100644
--- a/tests/auto/declarative/qmlengine/tst_qmlengine.cpp
+++ b/tests/auto/declarative/qmlengine/tst_qmlengine.cpp
@@ -1,4 +1,6 @@
#include <qtest.h>
+#include <QtDeclarative/qml.h>
+#include <QtDeclarative/QmlComponent>
#include <QtDeclarative/QmlEngine>
#include <QtCore/QDebug>
@@ -12,30 +14,83 @@ public:
tst_qmlengine() {}
private slots:
- void componentSearchPath();
-};
+ void valueTypeFunctions();
+private:
+ QmlEngine engine;
+};
-void tst_qmlengine::componentSearchPath()
+class MyTypeObject : public QObject
{
- QFile file(SRCDIR "/imports.qml");
- QVERIFY(file.open(QIODevice::ReadOnly));
+ Q_OBJECT
+ Q_PROPERTY(QPoint pointProperty READ pointProperty WRITE setPointProperty);
+ Q_PROPERTY(QPointF pointFProperty READ pointFProperty WRITE setPointFProperty);
+ Q_PROPERTY(QSize sizeProperty READ sizeProperty WRITE setSizeProperty);
+ Q_PROPERTY(QSizeF sizeFProperty READ sizeFProperty WRITE setSizeFProperty);
+ Q_PROPERTY(QRect rectProperty READ rectProperty WRITE setRectProperty NOTIFY rectPropertyChanged);
+ Q_PROPERTY(QRectF rectFProperty READ rectFProperty WRITE setRectFProperty);
+
+public:
+ MyTypeObject() {}
- QmlEngine engine;
+ QPoint pointPropertyValue;
+ QPoint pointProperty() const {
+ return pointPropertyValue;
+ }
+ void setPointProperty(const QPoint &v) {
+ pointPropertyValue = v;
+ }
- QList<QUrl> searchPath = engine.componentSearchPath(file.readAll(),
- QUrl::fromLocalFile(file.fileName()));
+ QPointF pointFPropertyValue;
+ QPointF pointFProperty() const {
+ return pointFPropertyValue;
+ }
+ void setPointFProperty(const QPointF &v) {
+ pointFPropertyValue = v;
+ }
- QList<QUrl> expected;
- expected << QUrl::fromLocalFile(SRCDIR);
- expected << QUrl::fromLocalFile(file.fileName()).resolved(QUrl("import1"));
- expected << QUrl::fromLocalFile(file.fileName()).resolved(QUrl("import2"));
+ QSize sizePropertyValue;
+ QSize sizeProperty() const {
+ return sizePropertyValue;
+ }
+ void setSizeProperty(const QSize &v) {
+ sizePropertyValue = v;
+ }
- QCOMPARE(searchPath.size(), expected.size());
- for (int i = 0; i < expected.size(); ++i) {
- QCOMPARE(searchPath.at(i).toString(QUrl::StripTrailingSlash),
- expected.at(i).toString(QUrl::StripTrailingSlash));
+ QSizeF sizeFPropertyValue;
+ QSizeF sizeFProperty() const {
+ return sizeFPropertyValue;
}
+ void setSizeFProperty(const QSizeF &v) {
+ sizeFPropertyValue = v;
+ }
+
+ QRect rectPropertyValue;
+ QRect rectProperty() const {
+ return rectPropertyValue;
+ }
+ void setRectProperty(const QRect &v) {
+ rectPropertyValue = v;
+ }
+
+ QRectF rectFPropertyValue;
+ QRectF rectFProperty() const {
+ return rectFPropertyValue;
+ }
+ void setRectFProperty(const QRectF &v) {
+ rectFPropertyValue = v;
+ }
+
+};
+QML_DECLARE_TYPE(MyTypeObject);
+QML_DEFINE_TYPE(Test, 1, 0, 0, MyTypeObject, MyTypeObject);
+
+void tst_qmlengine::valueTypeFunctions()
+{
+ QmlComponent component(&engine, SRCDIR "/functions.qml");
+ MyTypeObject *obj = qobject_cast<MyTypeObject*>(component.create());
+ QCOMPARE(obj->rectProperty(), QRect(0,0,100,100));
+ QCOMPARE(obj->rectFProperty(), QRectF(0,0.5,100,99.5));
}
QTEST_MAIN(tst_qmlengine)