From 6eea4cc05ac2bc903e3708a8219a22bbcccb7140 Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Wed, 28 Oct 2009 13:52:12 +1000 Subject: Minor cleanup of global object removed desktopServices member, changed openUrl to openUrlExternally and placed it on the Qt object. --- src/declarative/qml/qmlengine.cpp | 19 ++++++++----------- src/declarative/qml/qmlengine_p.h | 1 + 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/declarative/qml/qmlengine.cpp b/src/declarative/qml/qmlengine.cpp index 0e239ce..6c303e3 100644 --- a/src/declarative/qml/qmlengine.cpp +++ b/src/declarative/qml/qmlengine.cpp @@ -110,14 +110,6 @@ struct StaticQtMetaObject : public QObject { return &static_cast (0)->staticQtMetaObject; } }; -QScriptValue desktopOpenUrl(QScriptContext *ctxt, QScriptEngine *e) -{ - if(!ctxt->argumentCount()) - return e->newVariant(QVariant(false)); - bool ret = QDesktopServices::openUrl(QUrl(ctxt->argument(0).toString())); - return e->newVariant(QVariant(ret)); -} - static QString userLocalDataPath(const QString& app) { return QDesktopServices::storageLocation(QDesktopServices::DataLocation) + QLatin1String("/") + app; @@ -131,9 +123,6 @@ QmlEnginePrivate::QmlEnginePrivate(QmlEngine *e) { QScriptValue qtObject = scriptEngine.newQMetaObject(StaticQtMetaObject::get()); - QScriptValue desktopObject = scriptEngine.newObject(); - desktopObject.setProperty(QLatin1String("openUrl"),scriptEngine.newFunction(desktopOpenUrl, 1)); - qtObject.setProperty(QLatin1String("DesktopServices"), desktopObject); scriptEngine.globalObject().setProperty(QLatin1String("Qt"), qtObject); offlineStoragePath = userLocalDataPath(QLatin1String("QML/OfflineStorage")); @@ -155,6 +144,7 @@ QmlEnginePrivate::QmlEnginePrivate(QmlEngine *e) //misc methods qtObject.setProperty(QLatin1String("playSound"), scriptEngine.newFunction(QmlEnginePrivate::playSound, 1)); + qtObject.setProperty(QLatin1String("openUrlExternally"),scriptEngine.newFunction(desktopOpenUrl, 1)); scriptEngine.globalObject().setProperty(QLatin1String("createQmlObject"), scriptEngine.newFunction(QmlEnginePrivate::createQmlObject, 1)); @@ -897,6 +887,13 @@ QScriptValue QmlEnginePrivate::playSound(QScriptContext *ctxt, QScriptEngine *en return engine->undefinedValue(); } +QScriptValue QmlEnginePrivate::desktopOpenUrl(QScriptContext *ctxt, QScriptEngine *e) +{ + if(ctxt->argumentCount() < 1) + return e->newVariant(QVariant(false)); + bool ret = QDesktopServices::openUrl(QUrl(ctxt->argument(0).toString())); + return e->newVariant(QVariant(ret)); +} /*! This function allows tinting one color with another. diff --git a/src/declarative/qml/qmlengine_p.h b/src/declarative/qml/qmlengine_p.h index 69b121e..7629dc1 100644 --- a/src/declarative/qml/qmlengine_p.h +++ b/src/declarative/qml/qmlengine_p.h @@ -264,6 +264,7 @@ public: static QScriptValue tint(QScriptContext*, QScriptEngine*); static QScriptValue playSound(QScriptContext*, QScriptEngine*); + static QScriptValue desktopOpenUrl(QScriptContext*, QScriptEngine*); static QScriptEngine *getScriptEngine(QmlEngine *e) { return &e->d_func()->scriptEngine; } static QmlEngine *getEngine(QScriptEngine *e) { return static_cast(e)->p->q_func(); } -- cgit v0.12 From 1d14bbef27945d7823e9c32745e51d187093f1b7 Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Wed, 28 Oct 2009 18:14:18 +1000 Subject: Fill out QML global object documentation Moves the documentation for functions on the global object from qmlengine.cpp to globalobject.qdoc (so that qdoc actually generates them). The XmlHttpRequest documentation is still just a stub. --- doc/src/declarative/globalobject.qdoc | 178 ++++++++++++++++++++++++++++++++- doc/src/declarative/qtdeclarative.qdoc | 1 + src/declarative/qml/qmlengine.cpp | 118 +--------------------- src/declarative/qml/qmlengine_p.h | 1 + 4 files changed, 180 insertions(+), 118 deletions(-) diff --git a/doc/src/declarative/globalobject.qdoc b/doc/src/declarative/globalobject.qdoc index 2328c8a..fc5d988 100644 --- a/doc/src/declarative/globalobject.qdoc +++ b/doc/src/declarative/globalobject.qdoc @@ -42,7 +42,6 @@ /*! \page qmlglobalobject.html \title QML Global Object - Contains all the properties of the ECMAScript global object, plus: \list @@ -53,11 +52,186 @@ Contains all the properties of the ECMAScript global object, plus: \o openDatabase \endlist +Contents: +\tableofcontents \section1 Qt Object -\section1 Asynchronous JavaScript and XML +The Qt object provides useful enums and functions from Qt, for use in all QML +files. + +\section2 Enums +The Qt object contains all enums in the Qt namespace. For example, you can +access the AlignLeft member of the Qt::AlignmentFlag enum with \c Qt.AlignLeft. + +For a full list of enums, see the Qt Namespace documentation. + +\section2 Types +The Qt object also contains helper functions for creating objects of specific +data types. This is primarily useful when setting the properties of an item +when the property has one of the following types: + +\list +\o Color +\o Rect +\o Point +\o Size +\o Vector3D +\endlist + +There are also string based constructors for these types, see \l{basicqmltypes.html}{Qml Types}. + +\section3 Qt.rgba(int red, int green, int blue, int alpha) +This function returns a Color with the specified \c red, \c green, \c blue and \c alpha components. All components should be in the range 0-255 inclusive. + +\section3 Qt.hsla(int hue, int saturation, int lightness, int alpha) +This function returns a Color with the specified \c hue, \c saturation, \c lightness and \c alpha components. All components should be in the range 0-255 inclusive. + +\section3 Qt.rect(int x, int y, int width, int height) +This function returns a Rect with the top-left corner at \c x,\c y and the specified \c width and \c height. +\section3 Qt.point(int x, int y) +This function returns a Point with the specified \c x and \c y coordinates. +\section3 Qt.size(int width, int height) +returns as Size with the specified width and height. +\section3 Qt.vector(real x, real y, real z) + 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 + QVector3D value 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 + + +\section2 Functions +The Qt object also contains the following miscellaneous functions which expose Qt functionality for use in QML. + +\section3 Qt.lighter(color baseColor) +This function returns a color 50% lighter than \c baseColor. See QColor::lighter() for further details. +\section3 Qt.darker(color baseColor) +This function returns a color 50% darker than \c baseColor. See QColor::lighter() for further details. +\section3 Qt.tint(color baseColor, color tintColor) + This function allows tinting one color with another. + The tint color should usually be mostly transparent, or you will not be able to see the underlying color. The below example provides a slight red tint by having the tint color be pure red which is only 1/16th opaque. + + \qml + Rectangle { x: 0; width: 80; height: 80; color: "lightsteelblue" } + Rectangle { x: 100; width: 80; height: 80; color: Qt.tint("lightsteelblue", "#10FF0000") } + \endqml + \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.playSound(url soundLocation) +This function plays the audio file located at \c soundLocation. Only .wav files are supported. + +\section3 Qt.openUrlExternally(url target) +This function attempts to open the specified \c target url in an external application, based on the user's desktop preferences. It will return true if it succeeds, and false otherwise. +\endlist + +\section1 Dynamic Object Creation +The following functions on the global object allow you to dynamically create QML +items from files or strings. + +You can also dynamically create objects in a declarative manner, using items +such as ListView, Repeater and Loader. + +\section2 createComponent(url file) + This function takes the URL of a QML file as its only argument. It returns + a component object which can be used to create and load that QML file. + + Example QML script is below. Remember that QML files that might be loaded + over the network cannot be expected to be ready immediately. + \code + var component; + var sprite; + function finishCreation(){ + if(component.isReady()){ + sprite = component.createObject(); + if(sprite == 0){ + // Error Handling + }else{ + sprite.parent = page; + sprite.x = 200; + //... + } + }else if(component.isError()){ + // Error Handling + } + } + + component = createComponent("Sprite.qml"); + if(component.isReady()){ + finishCreation(); + }else{ + component.statusChanged.connect(finishCreation); + } + \endcode + + If you are certain the files will be local, you could simplify to + + \code + component = createComponent("Sprite.qml"); + sprite = component.createObject(); + if(sprite == 0){ + // Error Handling + print(component.errorsString()); + }else{ + sprite.parent = page; + sprite.x = 200; + //... + } + \endcode + + If you want to just create an arbitrary string of QML, instead of + loading a QML file, consider the createQmlObject() function. + +\section2 createQmlObject(string qml, object parent, string filepath) + Creates a new object from the specified string of QML. It requires a + second argument, which is the id of an existing QML object to use as + the new object's parent. If a third argument is provided, this is used + for error reporting as the filepath that the QML came from. + + Example (where targetItem is the id of an existing QML item): + \code + newObject = createQmlObject('import Qt 4.6; Rectangle {color: "red"; width: 20; height: 20}', + targetItem, "dynamicSnippet1"); + \endcode + + This function is intended for use inside QML only. It is intended to behave + similarly to eval, but for creating QML elements. + + Returns the created object, or null if there is an error. In the case of an + error, details of the error are output using qWarning(). + + Note that this function returns immediately, and therefore may not work if + the QML loads new components. If you are trying to load a new component, + for example from a QML file, consider the createComponent() function + instead. 'New components' refers to external QML files that have not yet + been loaded, and so it is safe to use createQmlObject to load built-in + components. + +\section1 Asynchronous JavaScript and XML +QML script supports the XMLHttpRequest object, which can be used to asynchronously obtain data from over a network. +\section2 XMLHttpRequest() +In QML you can construct an XMLHttpRequest object just like in a web browser! TODO: Real documentation for this object. \section1 Offline Storage API The \c openDatabase() and related functions diff --git a/doc/src/declarative/qtdeclarative.qdoc b/doc/src/declarative/qtdeclarative.qdoc index 5d8623b..3be90f7 100644 --- a/doc/src/declarative/qtdeclarative.qdoc +++ b/doc/src/declarative/qtdeclarative.qdoc @@ -98,6 +98,7 @@ completely new applications. QML is fully \l {Extending QML}{extensible from C+ \section1 Reference: \list \o \l {QML Elements} +\o \l {QML Global Object} \o \l {Extending QML} \endlist diff --git a/src/declarative/qml/qmlengine.cpp b/src/declarative/qml/qmlengine.cpp index 6c303e3..8581751 100644 --- a/src/declarative/qml/qmlengine.cpp +++ b/src/declarative/qml/qmlengine.cpp @@ -121,6 +121,8 @@ QmlEnginePrivate::QmlEnginePrivate(QmlEngine *e) nodeListClass(0), namedNodeMapClass(0), sqlQueryClass(0), cleanup(0), scriptEngine(this), componentAttacheds(0), rootComponent(0), networkAccessManager(0), typeManager(e), uniqueId(1) { + // Note that all documentation for stuff put on the global object goes in + // doc/src/declarative/globalobject.qdoc QScriptValue qtObject = scriptEngine.newQMetaObject(StaticQtMetaObject::get()); scriptEngine.globalObject().setProperty(QLatin1String("Qt"), qtObject); @@ -584,59 +586,6 @@ QmlContext *QmlEnginePrivate::getContext(QScriptContext *ctxt) return contextClass->contextFromValue(scopeNode); } -/*! - This function is intended for use inside QML only. In C++ just create a - component object as usual. - - This function takes the URL of a QML file as its only argument. It returns - a component object which can be used to create and load that QML file. - - Example QmlJS is below, remember that QML files that might be loaded - over the network cannot be expected to be ready immediately. - \code - var component; - var sprite; - function finishCreation(){ - if(component.isReady()){ - sprite = component.createObject(); - if(sprite == 0){ - // Error Handling - }else{ - sprite.parent = page; - sprite.x = 200; - //... - } - }else if(component.isError()){ - // Error Handling - } - } - - component = createComponent("Sprite.qml"); - if(component.isReady()){ - finishCreation(); - }else{ - component.statusChanged.connect(finishCreation); - } - \endcode - - If you are certain the files will be local, you could simplify to - - \code - component = createComponent("Sprite.qml"); - sprite = component.createObject(); - if(sprite == 0){ - // Error Handling - print(component.errorsString()); - }else{ - sprite.parent = page; - sprite.x = 200; - //... - } - \endcode - - If you want to just create an arbitrary string of QML, instead of - loading a qml file, consider the createQmlObject() function. -*/ QScriptValue QmlEnginePrivate::createComponent(QScriptContext *ctxt, QScriptEngine *engine) { @@ -659,31 +608,6 @@ QScriptValue QmlEnginePrivate::createComponent(QScriptContext *ctxt, return engine->newQObject(c); } -/*! - Creates a new object from the specified string of QML. It requires a - second argument, which is the id of an existing QML object to use as - the new object's parent. If a third argument is provided, this is used - as the filepath that the qml came from. - - Example (where targetItem is the id of an existing QML item): - \code - newObject = createQmlObject('import Qt 4.6; Rectangle {color: "red"; width: 20; height: 20}', - targetItem, "dynamicSnippet1"); - \endcode - - This function is intended for use inside QML only. It is intended to behave - similarly to eval, but for creating QML elements. - - Returns the created object, or null if there is an error. In the case of an - error, details of the error are output using qWarning(). - - Note that this function returns immediately, and therefore may not work if - the QML loads new components. If you are trying to load a new component, - for example from a QML file, consider the createComponent() function - instead. 'New components' refers to external QML files that have not yet - been loaded, and so it is safe to use createQmlObject to load built-in - components. -*/ QScriptValue QmlEnginePrivate::createQmlObject(QScriptContext *ctxt, QScriptEngine *engine) { QmlEnginePrivate *activeEnginePriv = @@ -736,32 +660,6 @@ 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 - QVector3D value 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) @@ -894,19 +792,7 @@ QScriptValue QmlEnginePrivate::desktopOpenUrl(QScriptContext *ctxt, QScriptEngin bool ret = QDesktopServices::openUrl(QUrl(ctxt->argument(0).toString())); return e->newVariant(QVariant(ret)); } -/*! - This function allows tinting one color with another. - - The tint color should usually be mostly transparent, or you will not be able to see the underlying color. The below example provides a slight red tint by having the tint color be pure red which is only 1/16th opaque. - \qml - Rectangle { x: 0; width: 80; height: 80; color: "lightsteelblue" } - Rectangle { x: 100; width: 80; height: 80; color: Qt.tint("lightsteelblue", "#10FF0000") } - \endqml - \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. -*/ 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 7629dc1..f5bc015 100644 --- a/src/declarative/qml/qmlengine_p.h +++ b/src/declarative/qml/qmlengine_p.h @@ -250,6 +250,7 @@ public: QVariant scriptValueToVariant(const QScriptValue &); static QScriptValue qmlScriptObject(QObject*, QmlEngine*); + static QScriptValue createComponent(QScriptContext*, QScriptEngine*); static QScriptValue createQmlObject(QScriptContext*, QScriptEngine*); static QScriptValue vector(QScriptContext*, QScriptEngine*); -- cgit v0.12 From 220eea1b5a978cf62e27878e7d9b2e1f4d10e91e Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Thu, 29 Oct 2009 13:52:36 +1000 Subject: Merge QmlComponentJS into QmlComponent createComponent() and Component{} are now the same, and so Component{}s can now be passed around for use in script. Also this commit fixes the minor bug QT-2386 --- examples/declarative/dynamic/dynamic.qml | 2 +- src/declarative/qml/qml.pri | 3 - src/declarative/qml/qmlcomponent.cpp | 48 +++++++++++ src/declarative/qml/qmlcomponent.h | 8 ++ src/declarative/qml/qmlcomponent_p.h | 3 +- src/declarative/qml/qmlcomponentjs.cpp | 131 ------------------------------- src/declarative/qml/qmlcomponentjs_p.h | 98 ----------------------- src/declarative/qml/qmlcomponentjs_p_p.h | 77 ------------------ src/declarative/qml/qmlengine.cpp | 18 +++-- 9 files changed, 69 insertions(+), 319 deletions(-) delete mode 100644 src/declarative/qml/qmlcomponentjs.cpp delete mode 100644 src/declarative/qml/qmlcomponentjs_p.h delete mode 100644 src/declarative/qml/qmlcomponentjs_p_p.h diff --git a/examples/declarative/dynamic/dynamic.qml b/examples/declarative/dynamic/dynamic.qml index ea4e0cd..9bfab0e 100644 --- a/examples/declarative/dynamic/dynamic.qml +++ b/examples/declarative/dynamic/dynamic.qml @@ -108,7 +108,7 @@ Item { } Button { text: "Create" - onClicked: {var obj=createQmlObject(qmlText.text, window, 'CustomObject'); obj.parent=window;} + onClicked: createQmlObject(qmlText.text, window, 'CustomObject'); } } } diff --git a/src/declarative/qml/qml.pri b/src/declarative/qml/qml.pri index 6c00beb..4c02f95 100644 --- a/src/declarative/qml/qml.pri +++ b/src/declarative/qml/qml.pri @@ -5,7 +5,6 @@ SOURCES += qml/qmlparser.cpp \ qml/qmlexpression.cpp \ qml/qmlbinding.cpp \ qml/qmlmetaproperty.cpp \ - qml/qmlcomponentjs.cpp \ qml/qmlcomponent.cpp \ qml/qmlcontext.cpp \ qml/qmlcustomparser.cpp \ @@ -56,8 +55,6 @@ HEADERS += qml/qmlparser_p.h \ qml/qmlbinding.h \ qml/qmlbinding_p.h \ qml/qmlmetaproperty.h \ - qml/qmlcomponentjs_p.h \ - qml/qmlcomponentjs_p_p.h \ qml/qmlcomponent.h \ qml/qmlcomponent_p.h \ qml/qmlcustomparser_p.h \ diff --git a/src/declarative/qml/qmlcomponent.cpp b/src/declarative/qml/qmlcomponent.cpp index 6181f41..72ead87 100644 --- a/src/declarative/qml/qmlcomponent.cpp +++ b/src/declarative/qml/qmlcomponent.cpp @@ -409,6 +409,9 @@ valid for components created directly from QML. */ QmlContext *QmlComponent::creationContext() const { + Q_D(const QmlComponent); + if(d->creationContext) + return d->creationContext; QmlDeclarativeData *ddata = QmlDeclarativeData::get(this); if (ddata) return ddata->context; @@ -417,6 +420,17 @@ QmlContext *QmlComponent::creationContext() const } /*! + \internal + Sets the QmlContext the component was created in. This is only + desirable for components created in QML script. +*/ +void QmlComponent::setCreationContext(QmlContext* c) +{ + Q_D(QmlComponent); + d->creationContext = c; +} + +/*! Load the QmlComponent from the provided \a url. */ void QmlComponent::loadUrl(const QUrl &url) @@ -462,6 +476,24 @@ QList QmlComponent::errors() const } /*! + \internal + errorsString is only meant as a way to get the errors in script +*/ +QString QmlComponent::errorsString() const +{ + Q_D(const QmlComponent); + QString ret; + if(!isError()) + return ret; + foreach(const QmlError &e, d->errors) { + ret += e.url().toString() + QLatin1String(":") + + QString::number(e.line()) + QLatin1String(" ") + + e.description() + QLatin1String("\n"); + } + return ret; +} + +/*! Return the component URL. This is the URL passed to either the constructor, or the loadUrl() or setData() methods. */ @@ -481,6 +513,22 @@ QmlComponent::QmlComponent(QmlComponentPrivate &dd, QObject *parent) /*! + \internal + A version of create which returns a scriptObject, for use in script +*/ +QScriptValue QmlComponent::createObject() +{ + Q_D(QmlComponent); + QmlContext* ctxt = creationContext(); + if(!ctxt){ + qWarning() << QLatin1String("createObject can only be used in QML"); + return QScriptValue(); + } + QObject* ret = create(ctxt); + return QmlEnginePrivate::qmlScriptObject(ret, d->engine); +} + +/*! Create an object instance from this component. Returns 0 if creation failed. \a context specifies the context within which to create the object instance. diff --git a/src/declarative/qml/qmlcomponent.h b/src/declarative/qml/qmlcomponent.h index a72aa4e..382b335 100644 --- a/src/declarative/qml/qmlcomponent.h +++ b/src/declarative/qml/qmlcomponent.h @@ -64,6 +64,10 @@ class Q_DECLARATIVE_EXPORT QmlComponent : public QObject { Q_OBJECT Q_DECLARE_PRIVATE(QmlComponent) + Q_PROPERTY(bool isNull READ isNull NOTIFY statusChanged) + Q_PROPERTY(bool isReady READ isReady NOTIFY statusChanged) + Q_PROPERTY(bool isError READ isError NOTIFY statusChanged) + Q_PROPERTY(bool isLoading READ isLoading NOTIFY statusChanged) public: QmlComponent(QObject *parent = 0); @@ -84,6 +88,7 @@ public: bool isLoading() const; QList errors() const; + Q_INVOKABLE QString errorsString() const; qreal progress() const; @@ -93,9 +98,12 @@ public: virtual QObject *beginCreate(QmlContext *); virtual void completeCreate(); + Q_INVOKABLE QScriptValue createObject(); + void loadUrl(const QUrl &url); void setData(const QByteArray &, const QUrl &baseUrl); + void setCreationContext(QmlContext*); QmlContext *creationContext() const; static QmlComponentAttached *qmlAttachedProperties(QObject *); diff --git a/src/declarative/qml/qmlcomponent_p.h b/src/declarative/qml/qmlcomponent_p.h index f90502f..cd886d5 100644 --- a/src/declarative/qml/qmlcomponent_p.h +++ b/src/declarative/qml/qmlcomponent_p.h @@ -76,7 +76,7 @@ class QmlComponentPrivate : public QObjectPrivate Q_DECLARE_PUBLIC(QmlComponent) public: - QmlComponentPrivate() : typeData(0), progress(0.), start(-1), count(-1), cc(0), componentAttacheds(0), completePending(false), engine(0) {} + QmlComponentPrivate() : typeData(0), progress(0.), start(-1), count(-1), cc(0), componentAttacheds(0), completePending(false), engine(0), creationContext(0) {} QObject *create(QmlContext *context, const QBitField &); @@ -104,6 +104,7 @@ public: bool completePending; QmlEngine *engine; + QmlContext *creationContext; void clear(); diff --git a/src/declarative/qml/qmlcomponentjs.cpp b/src/declarative/qml/qmlcomponentjs.cpp deleted file mode 100644 index df3e834..0000000 --- a/src/declarative/qml/qmlcomponentjs.cpp +++ /dev/null @@ -1,131 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** Contact: Qt Software Information (qt-info@nokia.com) -** -** This file is part of the QtDeclarative module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the either Technology Preview License Agreement or the -** Beta Release License Agreement. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain -** additional rights. These rights are described in the Nokia Qt LGPL -** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this -** package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** If you are unsure which license is appropriate for your use, please -** contact the sales department at qt-sales@nokia.com. -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qmlcomponentjs_p.h" -#include "qmlcomponentjs_p_p.h" -#include "qmlengine_p.h" -#include "qmlcomponent.h" - -QT_BEGIN_NAMESPACE - -QmlComponentJS::QmlComponentJS(QmlEngine *engine, QObject *parent) - : QmlComponent(*(new QmlComponentJSPrivate), parent) -{ - Q_D(QmlComponentJS); - d->engine = engine; - connect(this, SIGNAL(statusChanged(QmlComponent::Status)), - this, SLOT(statusChange(QmlComponent::Status))); -} - -QmlComponentJS::QmlComponentJS(QmlEngine *engine, const QUrl &url, QObject *parent) - : QmlComponent(*(new QmlComponentJSPrivate), parent) -{ - Q_D(QmlComponentJS); - d->engine = engine; - loadUrl(url); - connect(this, SIGNAL(statusChanged(QmlComponent::Status)), - this, SLOT(statusChange(QmlComponent::Status))); -} - -void QmlComponentJS::setContext(QmlContext* c) -{ - Q_D(QmlComponentJS); - d->ctxt =c; -} -/*! - Create a script object instance from this component. Returns a null - script object if creation failed. It will create the instance in the - same context that it was created it. QmlComponentJS is only - meant to be created from with script - C++ developers should just use - QmlComponent directly. - - Similar to QmlComponent::create(), but creates an object suitable - for usage within scripts. -*/ -QScriptValue QmlComponentJS::createObject() -{ - Q_D(QmlComponentJS); - QObject* ret = create(d->ctxt); - return QmlEnginePrivate::qmlScriptObject(ret, d->engine); -} - -/*! - Return the list of errors that occured during the last compile or create - operation, as a single string. An empty string is returned if isError() - is not set. - - This function is similar to errors(), except more useful when called from - QML. C++ code should usually use errors(). - - \sa errors() -*/ -QString QmlComponentJS::errorsString() const -{ - Q_D(const QmlComponentJS); - QString ret; - if(!isError()) - return ret; - foreach(const QmlError &e, d->errors) { - ret += e.url().toString() + QLatin1String(":") + - QString::number(e.line()) + QLatin1String(" ") + - e.description() + QLatin1String("\n"); - } - return ret; -} - - -void QmlComponentJS::statusChange(QmlComponent::Status newStatus) -{ - Q_D(QmlComponentJS); - if(newStatus == d->prevStatus) - return; - if(newStatus == QmlComponent::Null || d->prevStatus == QmlComponent::Null) - emit isNullChanged(); - if(newStatus == QmlComponent::Ready || d->prevStatus == QmlComponent::Ready) - emit isReadyChanged(); - if(newStatus == QmlComponent::Loading || d->prevStatus == QmlComponent::Loading) - emit isLoadingChanged(); - if(newStatus == QmlComponent::Error || d->prevStatus == QmlComponent::Error) - emit isErrorChanged(); - d->prevStatus = newStatus; -} - -QT_END_NAMESPACE diff --git a/src/declarative/qml/qmlcomponentjs_p.h b/src/declarative/qml/qmlcomponentjs_p.h deleted file mode 100644 index 3213929..0000000 --- a/src/declarative/qml/qmlcomponentjs_p.h +++ /dev/null @@ -1,98 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** Contact: Qt Software Information (qt-info@nokia.com) -** -** This file is part of the QtDeclarative module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the either Technology Preview License Agreement or the -** Beta Release License Agreement. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain -** additional rights. These rights are described in the Nokia Qt LGPL -** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this -** package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** If you are unsure which license is appropriate for your use, please -** contact the sales department at qt-sales@nokia.com. -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QMLCOMPONENTJS_P_H -#define QMLCOMPONENTJS_P_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include -#include -#include -#include -#include -#include - -QT_BEGIN_NAMESPACE - -class QmlComponentJSPrivate; -class QmlEngine; -class QmlContext; -class Q_DECLARATIVE_EXPORT QmlComponentJS : public QmlComponent -{ - Q_OBJECT - Q_DECLARE_PRIVATE(QmlComponentJS) - Q_PROPERTY(bool isNull READ isNull NOTIFY isNullChanged) - Q_PROPERTY(bool isReady READ isReady NOTIFY isReadyChanged) - Q_PROPERTY(bool isError READ isError NOTIFY isErrorChanged) - Q_PROPERTY(bool isLoading READ isLoading NOTIFY isLoadingChanged) - friend class QmlEngine; -public: - QmlComponentJS(QmlEngine *, const QUrl &url, QObject *parent = 0); - QmlComponentJS(QmlEngine *, QObject *parent=0); - - Q_INVOKABLE QScriptValue createObject(); - Q_INVOKABLE QString errorsString() const; - - void setContext(QmlContext* c); -Q_SIGNALS: - void isNullChanged(); - void isErrorChanged(); - void isReadyChanged(); - void isLoadingChanged(); -private Q_SLOTS: - void statusChange(QmlComponent::Status newStatus); -}; - -QT_END_NAMESPACE - -QML_DECLARE_TYPE(QmlComponentJS) - -#endif // QMLCOMPONENTJS_P_H diff --git a/src/declarative/qml/qmlcomponentjs_p_p.h b/src/declarative/qml/qmlcomponentjs_p_p.h deleted file mode 100644 index 47ce491..0000000 --- a/src/declarative/qml/qmlcomponentjs_p_p.h +++ /dev/null @@ -1,77 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** Contact: Qt Software Information (qt-info@nokia.com) -** -** This file is part of the QtDeclarative module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the either Technology Preview License Agreement or the -** Beta Release License Agreement. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain -** additional rights. These rights are described in the Nokia Qt LGPL -** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this -** package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** If you are unsure which license is appropriate for your use, please -** contact the sales department at qt-sales@nokia.com. -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QMLCOMPONENTJS_P_P_H -#define QMLCOMPONENTJS_P_P_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include "qmlcomponent.h" -#include "qmlcomponentjs_p.h" -#include "qmlcomponent_p.h" - -QT_BEGIN_NAMESPACE - -class QmlContext; -class QmlComponentJSPrivate : public QmlComponentPrivate -{ - Q_DECLARE_PUBLIC(QmlComponentJS) -public: - QmlComponentJSPrivate() : QmlComponentPrivate(), - prevStatus(QmlComponentJS::Null), ctxt(0) - { } - - QmlComponent::Status prevStatus; - QmlContext* ctxt; -}; - -QT_END_NAMESPACE - -#endif // QMLCOMPONENTJS_P_P_H diff --git a/src/declarative/qml/qmlengine.cpp b/src/declarative/qml/qmlengine.cpp index 3da8aa9..84186d4 100644 --- a/src/declarative/qml/qmlengine.cpp +++ b/src/declarative/qml/qmlengine.cpp @@ -75,8 +75,8 @@ #include #include #include +#include #include -#include #include #include #include @@ -651,7 +651,7 @@ QmlContext *QmlEnginePrivate::getContext(QScriptContext *ctxt) QScriptValue QmlEnginePrivate::createComponent(QScriptContext *ctxt, QScriptEngine *engine) { - QmlComponentJS* c; + QmlComponent* c; QmlEnginePrivate *activeEnginePriv = static_cast(engine)->p; @@ -659,14 +659,14 @@ QScriptValue QmlEnginePrivate::createComponent(QScriptContext *ctxt, QmlContext* context = activeEnginePriv->getContext(ctxt); if(ctxt->argumentCount() != 1) { - c = new QmlComponentJS(activeEngine); + c = new QmlComponent(activeEngine); }else{ QUrl url = QUrl(context->resolvedUrl(ctxt->argument(0).toString())); if(!url.isValid()) url = QUrl(ctxt->argument(0).toString()); - c = new QmlComponentJS(activeEngine, url, activeEngine); + c = new QmlComponent(activeEngine, url, activeEngine); } - c->setContext(context); + c->setCreationContext(context); return engine->newQObject(c); } @@ -708,8 +708,6 @@ QScriptValue QmlEnginePrivate::createQmlObject(QScriptContext *ctxt, QScriptEngi QUrl url; if(ctxt->argumentCount() > 2) url = QUrl(ctxt->argument(2).toString()); - else - url = QUrl(QLatin1String("DynamicQML")); QObject *parentArg = activeEnginePriv->objectClass->toQObject(ctxt->argument(1)); QmlContext *qmlCtxt = qmlContext(parentArg); if (url.isEmpty()) { @@ -719,6 +717,7 @@ QScriptValue QmlEnginePrivate::createQmlObject(QScriptContext *ctxt, QScriptEngi } QmlComponent component(activeEngine, qml.toUtf8(), url); + if(component.isError()) { QList errors = component.errors(); qWarning() <<"QmlEngine::createQmlObject():"; @@ -741,7 +740,10 @@ QScriptValue QmlEnginePrivate::createQmlObject(QScriptContext *ctxt, QScriptEngi if(obj) { obj->setParent(parentArg); - obj->setProperty("parent", QVariant::fromValue(parentArg)); + QGraphicsObject* gobj = qobject_cast(obj); + QGraphicsObject* gparent = qobject_cast(parentArg); + if(gobj && gparent) + gobj->setParentItem(gparent); return qmlScriptObject(obj, activeEngine); } return engine->nullValue(); -- cgit v0.12 From e532322a7bb58f6b2527fa42063b5bccde318105 Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Thu, 29 Oct 2009 14:08:18 +1000 Subject: Remove duplicated doc global object functions are now in globalobject.qdoc --- src/declarative/qml/qmlengine.cpp | 78 --------------------------------------- 1 file changed, 78 deletions(-) diff --git a/src/declarative/qml/qmlengine.cpp b/src/declarative/qml/qmlengine.cpp index 84186d4..9d69f94 100644 --- a/src/declarative/qml/qmlengine.cpp +++ b/src/declarative/qml/qmlengine.cpp @@ -595,59 +595,6 @@ QmlContext *QmlEnginePrivate::getContext(QScriptContext *ctxt) return contextClass->contextFromValue(scopeNode); } -/*! - This function is intended for use inside QML only. In C++ just create a - component object as usual. - - This function takes the URL of a QML file as its only argument. It returns - a component object which can be used to create and load that QML file. - - Example QmlJS is below, remember that QML files that might be loaded - over the network cannot be expected to be ready immediately. - \code - var component; - var sprite; - function finishCreation(){ - if(component.isReady()){ - sprite = component.createObject(); - if(sprite == 0){ - // Error Handling - }else{ - sprite.parent = page; - sprite.x = 200; - //... - } - }else if(component.isError()){ - // Error Handling - } - } - - component = createComponent("Sprite.qml"); - if(component.isReady()){ - finishCreation(); - }else{ - component.statusChanged.connect(finishCreation); - } - \endcode - - If you are certain the files will be local, you could simplify to - - \code - component = createComponent("Sprite.qml"); - sprite = component.createObject(); - if(sprite == 0){ - // Error Handling - print(component.errorsString()); - }else{ - sprite.parent = page; - sprite.x = 200; - //... - } - \endcode - - If you want to just create an arbitrary string of QML, instead of - loading a qml file, consider the createQmlObject() function. -*/ QScriptValue QmlEnginePrivate::createComponent(QScriptContext *ctxt, QScriptEngine *engine) { @@ -670,31 +617,6 @@ QScriptValue QmlEnginePrivate::createComponent(QScriptContext *ctxt, return engine->newQObject(c); } -/*! - Creates a new object from the specified string of QML. It requires a - second argument, which is the id of an existing QML object to use as - the new object's parent. If a third argument is provided, this is used - as the filepath that the qml came from. - - Example (where targetItem is the id of an existing QML item): - \code - newObject = createQmlObject('import Qt 4.6; Rectangle {color: "red"; width: 20; height: 20}', - targetItem, "dynamicSnippet1"); - \endcode - - This function is intended for use inside QML only. It is intended to behave - similarly to eval, but for creating QML elements. - - Returns the created object, or null if there is an error. In the case of an - error, details of the error are output using qWarning(). - - Note that this function returns immediately, and therefore may not work if - the QML loads new components. If you are trying to load a new component, - for example from a QML file, consider the createComponent() function - instead. 'New components' refers to external QML files that have not yet - been loaded, and so it is safe to use createQmlObject to load built-in - components. -*/ QScriptValue QmlEnginePrivate::createQmlObject(QScriptContext *ctxt, QScriptEngine *engine) { QmlEnginePrivate *activeEnginePriv = -- cgit v0.12 From 6c3b7fb91fe512479f7321eb4d3f577cb8bbd1a7 Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Thu, 29 Oct 2009 15:01:10 +1000 Subject: Doc --- doc/src/declarative/globalobject.qdoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/src/declarative/globalobject.qdoc b/doc/src/declarative/globalobject.qdoc index fc5d988..e327047 100644 --- a/doc/src/declarative/globalobject.qdoc +++ b/doc/src/declarative/globalobject.qdoc @@ -82,10 +82,10 @@ when the property has one of the following types: There are also string based constructors for these types, see \l{basicqmltypes.html}{Qml Types}. \section3 Qt.rgba(int red, int green, int blue, int alpha) -This function returns a Color with the specified \c red, \c green, \c blue and \c alpha components. All components should be in the range 0-255 inclusive. +This function returns a Color with the specified \c red, \c green, \c blue and \c alpha components. All components should be in the range 0-1 inclusive. \section3 Qt.hsla(int hue, int saturation, int lightness, int alpha) -This function returns a Color with the specified \c hue, \c saturation, \c lightness and \c alpha components. All components should be in the range 0-255 inclusive. +This function returns a Color with the specified \c hue, \c saturation, \c lightness and \c alpha components. All components should be in the range 0-1 inclusive. \section3 Qt.rect(int x, int y, int width, int height) This function returns a Rect with the top-left corner at \c x,\c y and the specified \c width and \c height. -- cgit v0.12