From 7edd8ee9a6885e57715c50be1dd402d9bd5118c8 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Wed, 27 Jul 2011 13:13:50 +1000 Subject: [PATCH] Support QT_TRANSLATE_NOOP in ListElement. Task-number: QTBUG-16289 Change-Id: I13e6859de185478e2c6c9486d8deeda103dd7b90 Reviewed-on: http://codereview.qt.nokia.com/2238 Reviewed-by: Qt Sanity Bot Reviewed-by: Aaron Kennedy --- src/declarative/util/qdeclarativelistmodel.cpp | 29 ++++++++++- .../tst_qdeclarativelistmodel.cpp | 57 +++++++++++++++++++--- 2 files changed, 77 insertions(+), 9 deletions(-) diff --git a/src/declarative/util/qdeclarativelistmodel.cpp b/src/declarative/util/qdeclarativelistmodel.cpp index e35d64b..d1496fc 100644 --- a/src/declarative/util/qdeclarativelistmodel.cpp +++ b/src/declarative/util/qdeclarativelistmodel.cpp @@ -42,6 +42,8 @@ #include "private/qdeclarativelistmodel_p_p.h" #include "private/qdeclarativelistmodelworkeragent_p.h" #include "private/qdeclarativeopenmetaobject_p.h" +#include "parser/qdeclarativejsast_p.h" +#include "parser/qdeclarativejsengine_p.h" #include #include @@ -840,9 +842,32 @@ bool QDeclarativeListModelParser::compileProperty(const QDeclarativeCustomParser QByteArray script = variant.asScript().toUtf8(); int v = evaluateEnum(script); if (v<0) { - if (script.startsWith("QT_TR_NOOP(\"") && script.endsWith("\")")) { + using namespace QDeclarativeJS; + AST::Node *node = variant.asAST(); + AST::StringLiteral *literal = 0; + if (AST::CallExpression *callExpr = AST::cast(node)) { + if (AST::IdentifierExpression *idExpr = AST::cast(callExpr->base)) { + if (idExpr->name->asString() == QLatin1String("QT_TR_NOOP")) { + if (callExpr->arguments && !callExpr->arguments->next) + literal = AST::cast(callExpr->arguments->expression); + if (!literal) { + error(prop, QDeclarativeListModel::tr("ListElement: improperly specified QT_TR_NOOP")); + return false; + } + } else if (idExpr->name->asString() == QLatin1String("QT_TRANSLATE_NOOP")) { + if (callExpr->arguments && callExpr->arguments->next && !callExpr->arguments->next->next) + literal = AST::cast(callExpr->arguments->next->expression); + if (!literal) { + error(prop, QDeclarativeListModel::tr("ListElement: improperly specified QT_TRANSLATE_NOOP")); + return false; + } + } + } + } + + if (literal) { d[0] = char(QDeclarativeParser::Variant::String); - d += script.mid(12,script.length()-14); + d += literal->value->asString().toUtf8(); } else { error(prop, QDeclarativeListModel::tr("ListElement: cannot use script for property value")); return false; diff --git a/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp b/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp index 3132239..1016e5d 100644 --- a/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp +++ b/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp @@ -76,6 +76,7 @@ private slots: void static_types(); void static_types_data(); void static_i18n(); + void static_i18n_data(); void static_nestedElements(); void static_nestedElements_data(); void dynamic_data(); @@ -197,20 +198,62 @@ void tst_qdeclarativelistmodel::static_types() delete obj; } +void tst_qdeclarativelistmodel::static_i18n_data() +{ + QTest::addColumn("qml"); + QTest::addColumn("value"); + QTest::addColumn("error"); + + QTest::newRow("QT_TR_NOOP") + << QString::fromUtf8("ListElement { foo: QT_TR_NOOP(\"na\303\257ve\") }") + << QVariant(QString::fromUtf8("na\303\257ve")) + << QString(); + + QTest::newRow("QT_TRANSLATE_NOOP") + << "ListElement { foo: QT_TRANSLATE_NOOP(\"MyListModel\", \"hello\") }" + << QVariant(QString("hello")) + << QString(); + + QTest::newRow("QT_TR_NOOP extra param") + << QString::fromUtf8("ListElement { foo: QT_TR_NOOP(\"hello\",\"world\") }") + << QVariant(QString()) + << QString("ListElement: improperly specified QT_TR_NOOP"); + + QTest::newRow("QT_TRANSLATE_NOOP missing params") + << "ListElement { foo: QT_TRANSLATE_NOOP() }" + << QVariant(QString()) + << QString("ListElement: improperly specified QT_TRANSLATE_NOOP"); +} + void tst_qdeclarativelistmodel::static_i18n() { - QString expect = QString::fromUtf8("na\303\257ve"); + QFETCH(QString, qml); + QFETCH(QVariant, value); + QFETCH(QString, error); + + qml = "import QtQuick 2.0\nItem { property variant test: model.get(0).foo; ListModel { id: model; " + qml + " } }"; - QString componentStr = "import QtQuick 2.0\nItem { property string prop1: model.get(0).prop1; property string prop2: model.get(0).prop2; ListModel { id: model; ListElement { prop1: \""+expect+"\"; prop2: QT_TR_NOOP(\""+expect+"\") } } }"; QDeclarativeEngine engine; QDeclarativeComponent component(&engine); - component.setData(componentStr.toUtf8(), QUrl::fromLocalFile("")); + component.setData(qml.toUtf8(), + QUrl::fromLocalFile(QString("dummy.qml"))); + + if (!error.isEmpty()) { + QVERIFY(component.isError()); + QCOMPARE(component.errors().at(0).description(), error); + return; + } + + QVERIFY(!component.isError()); + QObject *obj = component.create(); QVERIFY(obj != 0); - QString prop1 = obj->property("prop1").toString(); - QCOMPARE(prop1,expect); - QString prop2 = obj->property("prop2").toString(); - QCOMPARE(prop2,expect); // (no, not translated, QT_TR_NOOP is a no-op) + + QVariant actual = obj->property("test"); + + QCOMPARE(actual, value); + QCOMPARE(actual.toString(), value.toString()); + delete obj; } -- 2.7.4