aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/qqmlcompiler.cpp
diff options
context:
space:
mode:
authorAntti Piira <apiira@blackberry.com>2013-08-22 12:08:37 -0700
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-09-21 01:20:55 +0200
commit200a869441562d62e7fc0867599097e0599f0411 (patch)
tree982dc3c5a9c22bdea24a21054dd2b434fcea1147 /src/qml/qml/qqmlcompiler.cpp
parentb365471f0abc79f08bf0d852aea3be0a601c6901 (diff)
Add Singleton support for QML
This introduces Singleton support for QML (Composite Singleton). For now, the Singleton support is only availabe for QML types in modules or (remote and local) directories with qmldir file. However, in the future this support may be expanded to arbitrary QML file imports without by leaving out the qmldir requirement. You define a QML type as a Singleton with the following two steps: 1. By adding a pragma Singleton to a type's QML file: pragma Singleton The pragma and import statements can be mixed and their order does not matter. Singleton is the only supported pragma for now. Others will generate errors. 2. By specifying a qmldir file for the directory of your imported type and prepending the type with "singleton" keyword as follows: singleton TestTypeSingleton TestTypeSingleton.qml Alternatively you may specify a qmldir file for a module and specify your type as a singleton as follows: singleton TestTypeSingleton 1.0 TestTypeSingleton.qml Composite Singletons may be included in a module and may be used with a local namespace qualifier when imported with: "import xxx as NameSpace" A singleton instance is created at first use and stored into the QmlEngine (one instance per engine) and eventually released by the engine's destructor. CompositeSingletonType has a dual nature and will return true to both isComposite() and isSingleton() calls. In most cases its enough to check for just isComposite() or isSingleton(). However, there is a isCompositeSingleton() available as well. I used "qlalr --no-debug --no-lines --qt qqmljs.g" to generate the qqmljsparser and qqmljsgrammar files from qqmljs.g. Unit tests are included. Change-Id: I91b303612c5e132143b325b9a8f982e9355bc90e Reviewed-by: Alan Alpert (Personal) <416365416c@gmail.com>
Diffstat (limited to 'src/qml/qml/qqmlcompiler.cpp')
-rw-r--r--src/qml/qml/qqmlcompiler.cpp14
1 files changed, 13 insertions, 1 deletions
diff --git a/src/qml/qml/qqmlcompiler.cpp b/src/qml/qml/qqmlcompiler.cpp
index 8a0e015827..08ae37e747 100644
--- a/src/qml/qml/qqmlcompiler.cpp
+++ b/src/qml/qml/qqmlcompiler.cpp
@@ -821,6 +821,10 @@ bool QQmlCompiler::compile(QQmlEngine *engine,
QQmlScript::TypeReference *parserRef = referencedTypes.at(ii);
if (tref.typeData) { //QML-based type
+ if (tref.type->isCompositeSingleton()) {
+ QString err = tr( "Composite Singleton Type %1 is not creatable.").arg(tref.type->qmlTypeName());
+ COMPILE_EXCEPTION(parserRef->firstUse, err);
+ }
ref.component = tref.typeData->compiledData();
ref.component->addref();
} else if (tref.type) {//C++-based type
@@ -886,6 +890,12 @@ void QQmlCompiler::compileTree(QQmlScript::Object *tree)
output->importCache->add(ns);
}
+ // Add any Composite Singletons that were used to the import cache
+ for (int i = 0; i < unit->compositeSingletons().count(); ++i) {
+ output->importCache->add(unit->compositeSingletons().at(i).type->qmlTypeName(),
+ unit->compositeSingletons().at(i).type->sourceUrl(), unit->compositeSingletons().at(i).prefix);
+ }
+
int scriptIndex = 0;
foreach (const QQmlTypeData::ScriptReference &script, unit->resolvedScripts()) {
QString qualifier = script.qualifier;
@@ -2546,7 +2556,7 @@ bool QQmlCompiler::testQualifiedEnumAssignment(QQmlScript::Property *prop,
if (!type && typeName != QLatin1String("Qt"))
return true;
- if (type && type->isComposite()) //No enums on composite types
+ if (type && type->isComposite()) //No enums on composite (or composite singleton) types
return true;
int value = 0;
@@ -2984,6 +2994,8 @@ bool QQmlCompiler::buildDynamicMeta(QQmlScript::Object *obj, DynamicMetaMode mod
if (!unit->imports().resolveType(s->parameterTypeNames.at(i).toString(), &qmltype, 0, 0, 0))
COMPILE_EXCEPTION(s, tr("Invalid signal parameter type: %1").arg(s->parameterTypeNames.at(i).toString()));
+ // We dont mind even if the composite type ends up being composite singleton, here
+ // we just acquire the metaTypeId.
if (qmltype->isComposite()) {
QQmlTypeData *tdata = enginePrivate->typeLoader.getType(qmltype->sourceUrl());
Q_ASSERT(tdata);