diff options
-rw-r--r-- | doc/src/declarative/qmlintro.qdoc | 57 | ||||
-rw-r--r-- | src/declarative/qml/qmlscriptparser.cpp | 41 | ||||
-rw-r--r-- | src/declarative/util/qmlconnection.cpp | 23 | ||||
-rw-r--r-- | src/declarative/util/qmlconnection.h | 7 |
4 files changed, 75 insertions, 53 deletions
diff --git a/doc/src/declarative/qmlintro.qdoc b/doc/src/declarative/qmlintro.qdoc index 767cdd0..f84d614 100644 --- a/doc/src/declarative/qmlintro.qdoc +++ b/doc/src/declarative/qmlintro.qdoc @@ -265,9 +265,66 @@ because \c changes is the default property of the \c State type. \section2 Dot Properties + + \section2 Attached Properties \target attached-properties +Some objects attach properties to another object. Attached Properties +are of the form \e {Type.property} where \e Type is the type of the +element that attaches \e property. + +For example: +\code +Component { + id: myDelegate + Text { + text: "Hello" + color: ListView.isCurrentItem ? "red" : "blue" + } +} +ListView { + delegate: myDelegate +} +\endcode + +The \l ListView element attaches the \e ListView.isCurrentItem property +to each delegate it creates. + +Another example of attached properties is the \l Keys element which +attaches properties for handling key presses to +any visual Item, for example: + +\code +Item { + focus: true + Keys.onSelectPressed: print("Selected") +} +\endcode + \section2 Signal Handlers +Signal handlers allow actions to be taken in reponse to an event. For instance, +the \l MouseRegion element has signal handlers to handle mouse press, release +and click: + +\code +MouseRegion { + onPressed: print("mouse button pressed") +} +\endcode + +All signal handlers begin with \e "on". + +Some signal handlers include an optional parameter, for example +the MouseRegion onPressed signal handler has a \e mouse parameter: + +\code +MouseRegion { + acceptedButtons: Qt.LeftButton | Qt.RightButton + onPressed: if (mouse.button == Qt.RightButton) print("Right mouse button pressed") +} +\endcode + + */ diff --git a/src/declarative/qml/qmlscriptparser.cpp b/src/declarative/qml/qmlscriptparser.cpp index 6e5f315..fb84651 100644 --- a/src/declarative/qml/qmlscriptparser.cpp +++ b/src/declarative/qml/qmlscriptparser.cpp @@ -365,46 +365,7 @@ Object *ProcessAST::defineObjectBinding(AST::UiQualifiedId *qualifiedId, const QString objectType = asString(objectTypeName); const AST::SourceLocation typeLocation = objectTypeName->identifierToken; - if (objectType == QLatin1String("Connection")) { - - Object *obj = defineObjectBinding_helper(/*propertyName = */0, objectType, typeLocation, location); - - _stateStack.pushObject(obj); - - AST::UiObjectMemberList *it = initializer->members; - for (; it; it = it->next) { - AST::UiScriptBinding *scriptBinding = AST::cast<AST::UiScriptBinding *>(it->member); - if (! scriptBinding) - continue; - - QString propertyName = asString(scriptBinding->qualifiedId); - if (propertyName == QLatin1String("script")) { - QString script; - - if (AST::ExpressionStatement *stmt = AST::cast<AST::ExpressionStatement *>(scriptBinding->statement)) { - script = getVariant(stmt->expression).asScript(); - } else { - script = asString(scriptBinding->statement); - } - - LocationSpan l = this->location(scriptBinding->statement->firstSourceLocation(), - scriptBinding->statement->lastSourceLocation()); - - _stateStack.pushProperty(QLatin1String("script"), l); - Value *value = new Value; - value->value = QmlParser::Variant(script); - value->location = l; - currentProperty()->addValue(value); - _stateStack.pop(); - } else { - accept(it->member); - } - } - - _stateStack.pop(); // object - - return obj; - } else if (objectType == QLatin1String("Script")) { + if (objectType == QLatin1String("Script")) { AST::UiObjectMemberList *it = initializer->members; for (; it; it = it->next) { diff --git a/src/declarative/util/qmlconnection.cpp b/src/declarative/util/qmlconnection.cpp index 2a7cf6c..5475fb2 100644 --- a/src/declarative/util/qmlconnection.cpp +++ b/src/declarative/util/qmlconnection.cpp @@ -51,11 +51,12 @@ QT_BEGIN_NAMESPACE class QmlConnectionPrivate : public QObjectPrivate { public: - QmlConnectionPrivate() : boundsignal(0), signalSender(0), componentcomplete(false) {} + QmlConnectionPrivate() : boundsignal(0), signalSender(0), scriptset(false), componentcomplete(false) {} QmlBoundSignal *boundsignal; QObject *signalSender; - QString script; + QmlScriptString script; + bool scriptset; QString signal; bool componentcomplete; }; @@ -154,7 +155,7 @@ void QmlConnection::connectIfValid() if (!d->componentcomplete) return; // boundsignal must not exist - if ((d->signalSender || parent()) && !d->signal.isEmpty() && !d->script.isEmpty()) { + if ((d->signalSender || parent()) && !d->signal.isEmpty() && d->scriptset) { // create // XXX scope? int sigIdx = -1; @@ -187,7 +188,7 @@ void QmlConnection::connectIfValid() return; } - d->boundsignal = new QmlBoundSignal(qmlContext(this), d->script, sender, mo->method(sigIdx), this); + d->boundsignal = new QmlBoundSignal(qmlContext(this), d->script.script(), sender, mo->method(sigIdx), this); } } @@ -196,7 +197,7 @@ void QmlConnection::disconnectIfValid() Q_D(QmlConnection); if (!d->componentcomplete) return; - if ((d->signalSender || parent()) && !d->signal.isEmpty() && !d->script.isEmpty()) { + if ((d->signalSender || parent()) && !d->signal.isEmpty() && d->scriptset) { // boundsignal must exist // destroy delete d->boundsignal; @@ -213,31 +214,33 @@ void QmlConnection::componentComplete() /*! - \qmlproperty string Connection::script + \qmlproperty script Connection::script This property holds the JavaScript executed whenever the signal is sent. This is the default attribute of Connection. */ -QString QmlConnection::script() const +QmlScriptString QmlConnection::script() const { Q_D(const QmlConnection); return d->script; } -void QmlConnection::setScript(const QString& script) +void QmlConnection::setScript(const QmlScriptString& script) { Q_D(QmlConnection); if ((d->signalSender || parent()) && !d->signal.isEmpty()) { - if (d->script.isEmpty()) { + if (!d->scriptset) { // mustn't exist - create + d->scriptset = true; d->script = script; connectIfValid(); } else { // must exist - update d->script = script; - d->boundsignal->expression()->setExpression(script); + d->boundsignal->expression()->setExpression(script.script()); } } else { + d->scriptset = true; d->script = script; } } diff --git a/src/declarative/util/qmlconnection.h b/src/declarative/util/qmlconnection.h index dcaac34..3d69c6f 100644 --- a/src/declarative/util/qmlconnection.h +++ b/src/declarative/util/qmlconnection.h @@ -45,6 +45,7 @@ #include <QtCore/qobject.h> #include <QtCore/qstring.h> #include <QtDeclarative/qml.h> +#include <QtDeclarative/qmlscriptstring.h> QT_BEGIN_HEADER @@ -62,7 +63,7 @@ class Q_DECLARATIVE_EXPORT QmlConnection : public QObject, public QmlParserStatu Q_INTERFACES(QmlParserStatus) Q_PROPERTY(QObject *sender READ signalSender WRITE setSignalSender) - Q_PROPERTY(QString script READ script WRITE setScript) + Q_PROPERTY(QmlScriptString script READ script WRITE setScript) Q_PROPERTY(QString signal READ signal WRITE setSignal) public: @@ -71,8 +72,8 @@ public: QObject *signalSender() const; void setSignalSender(QObject *); - QString script() const; - void setScript(const QString&); + QmlScriptString script() const; + void setScript(const QmlScriptString&); QString signal() const; void setSignal(const QString&); |