diff options
author | Michael Brasser <michael.brasser@nokia.com> | 2012-06-22 09:23:26 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-06-22 06:54:59 +0200 |
commit | 55faad4873a9409bb1b33a10da7329d13a95aff9 (patch) | |
tree | b08f5b801079e49ddc9d6a5839eeb14a1e238a0f /src | |
parent | 033bf75d99c281d4a133fada297b7b141a0af555 (diff) |
Handle enum values of -1 correctly.
This was already handled correctly most places; now the
remaining cases (using an enum in ListModel, and assigning
an enum to an integer property) should also work correctly.
Task-number: QTBUG-21679
Change-Id: Ibff13f0b94da94b18e2e3bae4aa6ba44e0fa944b
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/qml/qml/qqmlcompiler.cpp | 14 | ||||
-rw-r--r-- | src/qml/qml/qqmlcompiler_p.h | 2 | ||||
-rw-r--r-- | src/qml/qml/qqmlcustomparser.cpp | 14 | ||||
-rw-r--r-- | src/qml/qml/qqmlcustomparser_p.h | 2 | ||||
-rw-r--r-- | src/qml/qml/qquicklistmodel.cpp | 5 |
5 files changed, 22 insertions, 15 deletions
diff --git a/src/qml/qml/qqmlcompiler.cpp b/src/qml/qml/qqmlcompiler.cpp index 5df02d0bc1..2bb99ddbab 100644 --- a/src/qml/qml/qqmlcompiler.cpp +++ b/src/qml/qml/qqmlcompiler.cpp @@ -2517,8 +2517,9 @@ bool QQmlCompiler::testQualifiedEnumAssignment(QQmlScript::Property *prop, if (isIntProp) { // Allow enum assignment to ints. - int enumval = evaluateEnum(typeName, enumValue.toUtf8()); - if (enumval != -1) { + bool ok; + int enumval = evaluateEnum(typeName, enumValue.toUtf8(), &ok); + if (ok) { v->type = Value::Literal; v->value = QQmlScript::Variant((double)enumval); *isAssignment = true; @@ -2565,8 +2566,10 @@ bool QQmlCompiler::testQualifiedEnumAssignment(QQmlScript::Property *prop, } // Similar logic to above, but not knowing target property. -int QQmlCompiler::evaluateEnum(const QHashedStringRef &scope, const QByteArray& enumValue) const +int QQmlCompiler::evaluateEnum(const QHashedStringRef &scope, const QByteArray& enumValue, bool *ok) const { + Q_ASSERT_X(ok, "QQmlCompiler::evaluateEnum", "ok must not be a null pointer"); + *ok = false; QQmlType *type = 0; if (scope != QLatin1String("Qt")) { unit->imports().resolveType(scope, &type, 0, 0, 0, 0); @@ -2577,9 +2580,8 @@ int QQmlCompiler::evaluateEnum(const QHashedStringRef &scope, const QByteArray& const QMetaObject *mo = type ? type->metaObject() : StaticQtMetaObject::get(); int i = mo->enumeratorCount(); while (i--) { - bool ok; - int v = mo->enumerator(i).keyToValue(enumValue.constData(), &ok); - if (ok) + int v = mo->enumerator(i).keyToValue(enumValue.constData(), ok); + if (*ok) return v; } return -1; diff --git a/src/qml/qml/qqmlcompiler_p.h b/src/qml/qml/qqmlcompiler_p.h index 4b730c2e20..b919b53207 100644 --- a/src/qml/qml/qqmlcompiler_p.h +++ b/src/qml/qml/qqmlcompiler_p.h @@ -295,7 +295,7 @@ public: static bool isAttachedPropertyName(const QHashedStringRef &); static bool isSignalPropertyName(const QHashedStringRef &); - int evaluateEnum(const QHashedStringRef &scope, const QByteArray& enumValue) const; // for QQmlCustomParser::evaluateEnum + int evaluateEnum(const QHashedStringRef &scope, const QByteArray& enumValue, bool *ok) const; // for QQmlCustomParser::evaluateEnum const QMetaObject *resolveType(const QString& name) const; // for QQmlCustomParser::resolveType int rewriteBinding(const QQmlScript::Variant& value, const QString& name); // for QQmlCustomParser::rewriteBinding QString rewriteSignalHandler(const QQmlScript::Variant& value, const QString &name); // for QQmlCustomParser::rewriteSignalHandler diff --git a/src/qml/qml/qqmlcustomparser.cpp b/src/qml/qml/qqmlcustomparser.cpp index f020376360..7c7a2f5611 100644 --- a/src/qml/qml/qqmlcustomparser.cpp +++ b/src/qml/qml/qqmlcustomparser.cpp @@ -279,18 +279,22 @@ void QQmlCustomParser::error(const QQmlCustomParserNode& node, const QString& de } /*! - If \a script is a simply enum expression (eg. Text.AlignLeft), - returns the integer equivalent (eg. 1). + If \a script is a simple enum expression (eg. Text.AlignLeft), + returns the integer equivalent (eg. 1), and sets \a ok to true. - Otherwise, returns -1. + Otherwise sets \a ok to false. + + A valid \a ok must be provided, or the function will assert. */ -int QQmlCustomParser::evaluateEnum(const QByteArray& script) const +int QQmlCustomParser::evaluateEnum(const QByteArray& script, bool *ok) const { + Q_ASSERT_X(ok, "QQmlCustomParser::evaluateEnum", "ok must not be a null pointer"); + *ok = false; int dot = script.indexOf('.'); if (dot == -1) return -1; - return compiler->evaluateEnum(QString::fromUtf8(script.left(dot)), script.mid(dot+1)); + return compiler->evaluateEnum(QString::fromUtf8(script.left(dot)), script.mid(dot+1), ok); } /*! diff --git a/src/qml/qml/qqmlcustomparser_p.h b/src/qml/qml/qqmlcustomparser_p.h index 0207797ce9..b8cc9f7644 100644 --- a/src/qml/qml/qqmlcustomparser_p.h +++ b/src/qml/qml/qqmlcustomparser_p.h @@ -137,7 +137,7 @@ protected: void error(const QQmlCustomParserProperty&, const QString& description); void error(const QQmlCustomParserNode&, const QString& description); - int evaluateEnum(const QByteArray&) const; + int evaluateEnum(const QByteArray&, bool *ok) const; const QMetaObject *resolveType(const QString&) const; diff --git a/src/qml/qml/qquicklistmodel.cpp b/src/qml/qml/qquicklistmodel.cpp index 461d4d8610..52ef2ab61f 100644 --- a/src/qml/qml/qquicklistmodel.cpp +++ b/src/qml/qml/qquicklistmodel.cpp @@ -2229,8 +2229,9 @@ bool QQuickListModelParser::compileProperty(const QQmlCustomParserProperty &prop d[0] = char(QQmlScript::Variant::Invalid); // marks empty list } else { QByteArray script = variant.asScript().toUtf8(); - int v = evaluateEnum(script); - if (v<0) { + bool ok; + int v = evaluateEnum(script, &ok); + if (!ok) { using namespace QQmlJS; AST::Node *node = variant.asAST(); AST::StringLiteral *literal = 0; |