1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the tools applications of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtDeclarative/QtDeclarative>
43 #include <QtDeclarative/private/qdeclarativemetatype_p.h>
44 #include <QtDeclarative/private/qdeclarativeopenmetaobject_p.h>
45 #include <QtDeclarative/private/qquickevents_p_p.h>
46 #include <QtDeclarative/private/qquickpincharea_p.h>
48 #include <QtWidgets/QApplication>
50 #include <QtCore/QSet>
51 #include <QtCore/QMetaObject>
52 #include <QtCore/QMetaProperty>
53 #include <QtCore/QDebug>
54 #include <QtCore/private/qobject_p.h>
55 #include <QtCore/private/qmetaobject_p.h>
59 #include "qmlstreamwriter.h"
62 #include <QtGui/private/qsimulatorconnection_p.h>
69 QString pluginImportPath;
72 QString currentProperty;
73 QString inObjectInstantiation;
75 void collectReachableMetaObjects(const QMetaObject *meta, QSet<const QMetaObject *> *metas)
77 if (! meta || metas->contains(meta))
80 // dynamic meta objects break things badly, so just ignore them
81 const QMetaObjectPrivate *mop = reinterpret_cast<const QMetaObjectPrivate *>(meta->d.data);
82 if (!(mop->flags & DynamicMetaObject))
85 collectReachableMetaObjects(meta->superClass(), metas);
88 void collectReachableMetaObjects(QObject *object, QSet<const QMetaObject *> *metas)
93 const QMetaObject *meta = object->metaObject();
95 qDebug() << "Processing object" << meta->className();
96 collectReachableMetaObjects(meta, metas);
98 for (int index = 0; index < meta->propertyCount(); ++index) {
99 QMetaProperty prop = meta->property(index);
100 if (QDeclarativeMetaType::isQObject(prop.userType())) {
102 qDebug() << " Processing property" << prop.name();
103 currentProperty = QString("%1::%2").arg(meta->className(), prop.name());
105 // if the property was not initialized during construction,
106 // accessing a member of oo is going to cause a segmentation fault
107 QObject *oo = QDeclarativeMetaType::toQObject(prop.read(object));
108 if (oo && !metas->contains(oo->metaObject()))
109 collectReachableMetaObjects(oo, metas);
110 currentProperty.clear();
115 void collectReachableMetaObjects(const QDeclarativeType *ty, QSet<const QMetaObject *> *metas)
117 collectReachableMetaObjects(ty->metaObject(), metas);
118 if (ty->attachedPropertiesType())
119 collectReachableMetaObjects(ty->attachedPropertiesType(), metas);
122 /* We want to add the MetaObject for 'Qt' to the list, this is a
123 simple way to access it.
125 class FriendlyQObject: public QObject
128 static const QMetaObject *qtMeta() { return &staticQtMetaObject; }
131 /* When we dump a QMetaObject, we want to list all the types it is exported as.
132 To do this, we need to find the QDeclarativeTypes associated with this
135 static QHash<QByteArray, QSet<const QDeclarativeType *> > qmlTypesByCppName;
137 static QHash<QByteArray, QByteArray> cppToId;
139 /* Takes a C++ type name, such as Qt::LayoutDirection or QString and
140 maps it to how it should appear in the description file.
142 These names need to be unique globally, so we don't change the C++ symbol's
143 name much. It is mostly used to for explicit translations such as
144 QString->string and translations for extended QML objects.
146 QByteArray convertToId(const QByteArray &cppName)
148 return cppToId.value(cppName, cppName);
151 QByteArray convertToId(const QMetaObject *mo)
153 QByteArray className(mo->className());
154 if (!className.isEmpty())
155 return convertToId(className);
157 // likely a metaobject generated for an extended qml object
158 if (mo->superClass()) {
159 className = convertToId(mo->superClass());
160 className.append("_extended");
164 static QHash<const QMetaObject *, QByteArray> generatedNames;
165 className = generatedNames.value(mo);
166 if (!className.isEmpty())
169 qWarning() << "Found a QMetaObject without a className, generating a random name";
170 className = QByteArray("error-unknown-name-");
171 className.append(QByteArray::number(generatedNames.size()));
172 generatedNames.insert(mo, className);
176 /* All exported module APIs are collected into this list */
184 QList<ModuleApi> moduleApis;
186 QSet<const QMetaObject *> collectReachableMetaObjects(QDeclarativeEngine *engine, const QList<QDeclarativeType *> &skip = QList<QDeclarativeType *>())
188 QSet<const QMetaObject *> metas;
189 metas.insert(FriendlyQObject::qtMeta());
191 QHash<QByteArray, QSet<QByteArray> > extensions;
192 foreach (const QDeclarativeType *ty, QDeclarativeMetaType::qmlTypes()) {
193 qmlTypesByCppName[ty->metaObject()->className()].insert(ty);
194 if (ty->isExtendedType()) {
195 extensions[ty->typeName()].insert(ty->metaObject()->className());
197 collectReachableMetaObjects(ty, &metas);
200 // Adjust exports of the base object if there are extensions.
201 // For each export of a base object there can be a single extension object overriding it.
202 // Example: QDeclarativeGraphicsWidget overrides the QtQuick/QGraphicsWidget export
203 // of QGraphicsWidget.
204 foreach (const QByteArray &baseCpp, extensions.keys()) {
205 QSet<const QDeclarativeType *> baseExports = qmlTypesByCppName.value(baseCpp);
207 const QSet<QByteArray> extensionCppNames = extensions.value(baseCpp);
208 foreach (const QByteArray &extensionCppName, extensionCppNames) {
209 const QSet<const QDeclarativeType *> extensionExports = qmlTypesByCppName.value(extensionCppName);
211 // remove extension exports from base imports
212 // unfortunately the QDeclarativeType pointers don't match, so can't use QSet::substract
213 QSet<const QDeclarativeType *> newBaseExports;
214 foreach (const QDeclarativeType *baseExport, baseExports) {
216 foreach (const QDeclarativeType *extensionExport, extensionExports) {
217 if (baseExport->qmlTypeName() == extensionExport->qmlTypeName()
218 && baseExport->majorVersion() == extensionExport->majorVersion()
219 && baseExport->minorVersion() == extensionExport->minorVersion()) {
225 newBaseExports.insert(baseExport);
227 baseExports = newBaseExports;
229 qmlTypesByCppName[baseCpp] = baseExports;
232 // find even more QMetaObjects by instantiating QML types and running
233 // over the instances
234 foreach (QDeclarativeType *ty, QDeclarativeMetaType::qmlTypes()) {
235 if (skip.contains(ty))
237 if (ty->isExtendedType())
239 if (!ty->isCreatable())
241 if (ty->typeName() == "QDeclarativeComponent")
244 QString tyName = ty->qmlTypeName();
245 tyName = tyName.mid(tyName.lastIndexOf(QLatin1Char('/')) + 1);
246 if (tyName.isEmpty())
249 inObjectInstantiation = tyName;
250 QObject *object = ty->create();
251 inObjectInstantiation.clear();
254 collectReachableMetaObjects(object, &metas);
256 qWarning() << "Could not create" << tyName;
259 // extract exported module api
260 QHashIterator<QString, QList<QDeclarativeMetaType::ModuleApi> > moduleApiIt(QDeclarativeMetaType::moduleApis());
261 while (moduleApiIt.hasNext()) {
263 foreach (const QDeclarativeMetaType::ModuleApi &api, moduleApiIt.value()) {
265 moduleApi.uri = moduleApiIt.key();
266 moduleApi.majorVersion = api.major;
267 moduleApi.minorVersion = api.minor;
270 if (QObject *object = (*api.qobject)(engine, engine)) {
271 collectReachableMetaObjects(object, &metas);
272 moduleApi.objectId = convertToId(object->metaObject()->className());
275 } else if (api.script) {
276 qWarning() << "Can't dump the module api in " << moduleApi.uri << ". QJSValue based module API is not supported.";
277 // QJSValue value = (*api.script)(engine, engine);
278 // IdToObjectHash jsObjects;
279 // collectReachableJSObjects(value, &jsObjects, &metas);
282 moduleApis += moduleApi;
292 QmlStreamWriter *qml;
293 QString relocatableModuleUri;
296 Dumper(QmlStreamWriter *qml) : qml(qml) {}
298 void setRelocatableModuleUri(const QString &uri)
300 relocatableModuleUri = uri;
303 void dump(const QMetaObject *meta)
305 qml->writeStartObject("Component");
307 QByteArray id = convertToId(meta);
308 qml->writeScriptBinding(QLatin1String("name"), enquote(id));
310 for (int index = meta->classInfoCount() - 1 ; index >= 0 ; --index) {
311 QMetaClassInfo classInfo = meta->classInfo(index);
312 if (QLatin1String(classInfo.name()) == QLatin1String("DefaultProperty")) {
313 qml->writeScriptBinding(QLatin1String("defaultProperty"), enquote(QLatin1String(classInfo.value())));
318 if (meta->superClass())
319 qml->writeScriptBinding(QLatin1String("prototype"), enquote(convertToId(meta->superClass())));
321 QSet<const QDeclarativeType *> qmlTypes = qmlTypesByCppName.value(meta->className());
322 if (!qmlTypes.isEmpty()) {
323 QHash<QString, const QDeclarativeType *> exports;
325 foreach (const QDeclarativeType *qmlTy, qmlTypes) {
326 QString qmlTyName = qmlTy->qmlTypeName();
327 if (qmlTyName.startsWith(relocatableModuleUri + QLatin1Char('/'))) {
328 qmlTyName.remove(0, relocatableModuleUri.size() + 1);
330 if (qmlTyName.startsWith("./")) {
331 qmlTyName.remove(0, 2);
333 if (qmlTyName.startsWith("/")) {
334 qmlTyName.remove(0, 1);
336 const QString exportString = enquote(
337 QString("%1 %2.%3").arg(
339 QString::number(qmlTy->majorVersion()),
340 QString::number(qmlTy->minorVersion())));
341 exports.insert(exportString, qmlTy);
344 // ensure exports are sorted and don't change order when the plugin is dumped again
345 QStringList exportStrings = exports.keys();
346 qSort(exportStrings);
347 qml->writeArrayBinding(QLatin1String("exports"), exportStrings);
349 // write meta object revisions unless they're all zero
350 QStringList metaObjectRevisions;
351 bool shouldWriteMetaObjectRevisions = false;
352 foreach (const QString &exportString, exportStrings) {
353 int metaObjectRevision = exports[exportString]->metaObjectRevision();
354 if (metaObjectRevision != 0)
355 shouldWriteMetaObjectRevisions = true;
356 metaObjectRevisions += QString::number(metaObjectRevision);
358 if (shouldWriteMetaObjectRevisions)
359 qml->writeArrayBinding(QLatin1String("exportMetaObjectRevisions"), metaObjectRevisions);
361 if (const QMetaObject *attachedType = (*qmlTypes.begin())->attachedPropertiesType()) {
362 // Can happen when a type is registered that returns itself as attachedPropertiesType()
363 // because there is no creatable type to attach to.
364 if (attachedType != meta) {
365 qml->writeScriptBinding(QLatin1String("attachedType"), enquote(
366 convertToId(attachedType)));
371 for (int index = meta->enumeratorOffset(); index < meta->enumeratorCount(); ++index)
372 dump(meta->enumerator(index));
374 QSet<QString> implicitSignals;
375 for (int index = meta->propertyOffset(); index < meta->propertyCount(); ++index) {
376 const QMetaProperty &property = meta->property(index);
378 implicitSignals.insert(QString("%1Changed").arg(QString::fromUtf8(property.name())));
381 if (meta == &QObject::staticMetaObject) {
382 // for QObject, hide deleteLater() and onDestroyed
383 for (int index = meta->methodOffset(); index < meta->methodCount(); ++index) {
384 QMetaMethod method = meta->method(index);
385 const char *signature(method.signature());
386 if (signature == QLatin1String("destroyed(QObject*)")
387 || signature == QLatin1String("destroyed()")
388 || signature == QLatin1String("deleteLater()"))
390 dump(method, implicitSignals);
393 // and add toString(), destroy() and destroy(int)
394 qml->writeStartObject(QLatin1String("Method"));
395 qml->writeScriptBinding(QLatin1String("name"), enquote(QLatin1String("toString")));
396 qml->writeEndObject();
397 qml->writeStartObject(QLatin1String("Method"));
398 qml->writeScriptBinding(QLatin1String("name"), enquote(QLatin1String("destroy")));
399 qml->writeEndObject();
400 qml->writeStartObject(QLatin1String("Method"));
401 qml->writeScriptBinding(QLatin1String("name"), enquote(QLatin1String("destroy")));
402 qml->writeStartObject(QLatin1String("Parameter"));
403 qml->writeScriptBinding(QLatin1String("name"), enquote(QLatin1String("delay")));
404 qml->writeScriptBinding(QLatin1String("type"), enquote(QLatin1String("int")));
405 qml->writeEndObject();
406 qml->writeEndObject();
408 for (int index = meta->methodOffset(); index < meta->methodCount(); ++index)
409 dump(meta->method(index), implicitSignals);
412 qml->writeEndObject();
415 void dump(const ModuleApi &api)
417 qml->writeStartObject(QLatin1String("ModuleApi"));
418 if (api.uri != relocatableModuleUri)
419 qml->writeScriptBinding(QLatin1String("uri"), enquote(api.uri));
420 qml->writeScriptBinding(QLatin1String("version"), QString("%1.%2").arg(
421 QString::number(api.majorVersion),
422 QString::number(api.minorVersion)));
423 qml->writeScriptBinding(QLatin1String("name"), enquote(api.objectId));
424 qml->writeEndObject();
427 void writeEasingCurve()
429 qml->writeStartObject(QLatin1String("Component"));
430 qml->writeScriptBinding(QLatin1String("name"), enquote(QLatin1String("QEasingCurve")));
431 qml->writeScriptBinding(QLatin1String("prototype"), enquote(QLatin1String("QDeclarativeEasingValueType")));
432 qml->writeEndObject();
436 static QString enquote(const QString &string)
438 return QString("\"%1\"").arg(string);
441 /* Removes pointer and list annotations from a type name, returning
442 what was removed in isList and isPointer
444 static void removePointerAndList(QByteArray *typeName, bool *isList, bool *isPointer)
446 static QByteArray declListPrefix = "QDeclarativeListProperty<";
448 if (typeName->endsWith('*')) {
450 typeName->truncate(typeName->length() - 1);
451 removePointerAndList(typeName, isList, isPointer);
452 } else if (typeName->startsWith(declListPrefix)) {
454 typeName->truncate(typeName->length() - 1); // get rid of the suffix '>'
455 *typeName = typeName->mid(declListPrefix.size());
456 removePointerAndList(typeName, isList, isPointer);
459 *typeName = convertToId(*typeName);
462 void writeTypeProperties(QByteArray typeName, bool isWritable)
464 bool isList = false, isPointer = false;
465 removePointerAndList(&typeName, &isList, &isPointer);
467 qml->writeScriptBinding(QLatin1String("type"), enquote(typeName));
469 qml->writeScriptBinding(QLatin1String("isList"), QLatin1String("true"));
471 qml->writeScriptBinding(QLatin1String("isReadonly"), QLatin1String("true"));
473 qml->writeScriptBinding(QLatin1String("isPointer"), QLatin1String("true"));
476 void dump(const QMetaProperty &prop)
478 qml->writeStartObject("Property");
480 qml->writeScriptBinding(QLatin1String("name"), enquote(QString::fromUtf8(prop.name())));
481 #if (QT_VERSION >= QT_VERSION_CHECK(4, 7, 4))
482 if (int revision = prop.revision())
483 qml->writeScriptBinding(QLatin1String("revision"), QString::number(revision));
485 writeTypeProperties(prop.typeName(), prop.isWritable());
487 qml->writeEndObject();
490 void dump(const QMetaMethod &meth, const QSet<QString> &implicitSignals)
492 if (meth.methodType() == QMetaMethod::Signal) {
493 if (meth.access() != QMetaMethod::Protected)
494 return; // nothing to do.
495 } else if (meth.access() != QMetaMethod::Public) {
496 return; // nothing to do.
499 QByteArray name = meth.signature();
500 int lparenIndex = name.indexOf('(');
501 if (lparenIndex == -1) {
502 return; // invalid signature
504 name = name.left(lparenIndex);
505 const QString typeName = convertToId(meth.typeName());
507 if (implicitSignals.contains(name)
509 && meth.methodType() == QMetaMethod::Signal
510 && meth.parameterNames().isEmpty()
511 && typeName.isEmpty()) {
512 // don't mention implicit signals
516 if (meth.methodType() == QMetaMethod::Signal)
517 qml->writeStartObject(QLatin1String("Signal"));
519 qml->writeStartObject(QLatin1String("Method"));
521 qml->writeScriptBinding(QLatin1String("name"), enquote(name));
523 #if (QT_VERSION >= QT_VERSION_CHECK(4, 7, 4))
524 if (int revision = meth.revision())
525 qml->writeScriptBinding(QLatin1String("revision"), QString::number(revision));
528 if (! typeName.isEmpty())
529 qml->writeScriptBinding(QLatin1String("type"), enquote(typeName));
531 for (int i = 0; i < meth.parameterTypes().size(); ++i) {
532 QByteArray argName = meth.parameterNames().at(i);
534 qml->writeStartObject(QLatin1String("Parameter"));
535 if (! argName.isEmpty())
536 qml->writeScriptBinding(QLatin1String("name"), enquote(argName));
537 writeTypeProperties(meth.parameterTypes().at(i), true);
538 qml->writeEndObject();
541 qml->writeEndObject();
544 void dump(const QMetaEnum &e)
546 qml->writeStartObject(QLatin1String("Enum"));
547 qml->writeScriptBinding(QLatin1String("name"), enquote(QString::fromUtf8(e.name())));
549 QList<QPair<QString, QString> > namesValues;
550 for (int index = 0; index < e.keyCount(); ++index) {
551 namesValues.append(qMakePair(enquote(QString::fromUtf8(e.key(index))), QString::number(e.value(index))));
554 qml->writeScriptObjectLiteralBinding(QLatin1String("values"), namesValues);
555 qml->writeEndObject();
561 EXIT_INVALIDARGUMENTS = 1,
567 void sigSegvHandler(int) {
568 fprintf(stderr, "Error: SEGV\n");
569 if (!currentProperty.isEmpty())
570 fprintf(stderr, "While processing the property '%s', which probably has uninitialized data.\n", currentProperty.toLatin1().constData());
571 if (!inObjectInstantiation.isEmpty())
572 fprintf(stderr, "While instantiating the object '%s'.\n", inObjectInstantiation.toLatin1().constData());
577 void printUsage(const QString &appName)
579 qWarning() << qPrintable(QString(
580 "Usage: %1 [-v] [-notrelocatable] module.uri version [module/import/path]\n"
581 " %1 [-v] -path path/to/qmldir/directory [version]\n"
582 " %1 [-v] -builtins\n"
583 "Example: %1 Qt.labs.particles 4.7 /home/user/dev/qt-install/imports").arg(
587 int main(int argc, char *argv[])
590 // qmldump may crash, but we don't want any crash handlers to pop up
591 // therefore we intercept the segfault and just exit() ourselves
592 struct sigaction sigAction;
594 sigemptyset(&sigAction.sa_mask);
595 sigAction.sa_handler = &sigSegvHandler;
596 sigAction.sa_flags = 0;
598 sigaction(SIGSEGV, &sigAction, 0);
602 // Running this application would bring up the Qt Simulator (since it links QtGui), avoid that!
603 QtSimulatorPrivate::SimulatorConnection::createStubInstance();
605 QApplication app(argc, argv);
606 const QStringList args = app.arguments();
607 const QString appName = QFileInfo(app.applicationFilePath()).baseName();
608 if (args.size() < 2) {
610 return EXIT_INVALIDARGUMENTS;
613 QString pluginImportUri;
614 QString pluginImportVersion;
615 bool relocatable = true;
616 enum Action { Uri, Path, Builtins };
619 QStringList positionalArgs;
620 foreach (const QString &arg, args) {
621 if (!arg.startsWith(QLatin1Char('-'))) {
622 positionalArgs.append(arg);
626 if (arg == QLatin1String("--notrelocatable")
627 || arg == QLatin1String("-notrelocatable")) {
629 } else if (arg == QLatin1String("--path")
630 || arg == QLatin1String("-path")) {
632 } else if (arg == QLatin1String("--builtins")
633 || arg == QLatin1String("-builtins")) {
635 } else if (arg == QLatin1String("-v")) {
638 qWarning() << "Invalid argument: " << arg;
639 return EXIT_INVALIDARGUMENTS;
644 if (positionalArgs.size() != 3 && positionalArgs.size() != 4) {
645 qWarning() << "Incorrect number of positional arguments";
646 return EXIT_INVALIDARGUMENTS;
648 pluginImportUri = positionalArgs[1];
649 pluginImportVersion = positionalArgs[2];
650 if (positionalArgs.size() >= 4)
651 pluginImportPath = positionalArgs[3];
652 } else if (action == Path) {
653 if (positionalArgs.size() != 2 && positionalArgs.size() != 3) {
654 qWarning() << "Incorrect number of positional arguments";
655 return EXIT_INVALIDARGUMENTS;
657 pluginImportPath = QDir::fromNativeSeparators(positionalArgs[1]);
658 if (positionalArgs.size() == 3)
659 pluginImportVersion = positionalArgs[2];
660 } else if (action == Builtins) {
661 if (positionalArgs.size() != 1) {
662 qWarning() << "Incorrect number of positional arguments";
663 return EXIT_INVALIDARGUMENTS;
668 QDeclarativeEngine engine;
669 if (!pluginImportPath.isEmpty()) {
670 QDir cur = QDir::current();
671 cur.cd(pluginImportPath);
672 pluginImportPath = cur.absolutePath();
673 QDir::setCurrent(pluginImportPath);
674 engine.addImportPath(pluginImportPath);
677 // load the QtQuick 1 plugin
679 QByteArray code("import QtQuick 1.0\nQtObject {}");
680 QDeclarativeComponent c(&engine);
681 c.setData(code, QUrl::fromLocalFile(pluginImportPath + "/loadqtquick1.qml"));
683 if (!c.errors().isEmpty()) {
684 foreach (const QDeclarativeError &error, c.errors())
685 qWarning() << error.toString();
686 return EXIT_IMPORTERROR;
690 // find all QMetaObjects reachable from the builtin module
691 QSet<const QMetaObject *> defaultReachable = collectReachableMetaObjects(&engine);
692 QList<QDeclarativeType *> defaultTypes = QDeclarativeMetaType::qmlTypes();
694 // add some otherwise unreachable QMetaObjects
695 defaultReachable.insert(&QQuickMouseEvent::staticMetaObject);
696 // QQuickKeyEvent, QQuickPinchEvent, QQuickDropEvent are not exported
698 // this will hold the meta objects we want to dump information of
699 QSet<const QMetaObject *> metas;
701 if (action == Builtins) {
702 metas = defaultReachable;
704 // find a valid QtQuick import
705 QByteArray importCode;
706 QDeclarativeType *qtObjectType = QDeclarativeMetaType::qmlType(&QObject::staticMetaObject);
708 qWarning() << "Could not find QtObject type";
709 importCode = QByteArray("import QtQuick 2.0\n");
711 QString module = qtObjectType->qmlTypeName();
712 module = module.mid(0, module.lastIndexOf(QLatin1Char('/')));
713 importCode = QString("import %1 %2.%3\n").arg(module,
714 QString::number(qtObjectType->majorVersion()),
715 QString::number(qtObjectType->minorVersion())).toUtf8();
718 // find all QMetaObjects reachable when the specified module is imported
719 if (action != Path) {
720 importCode += QString("import %0 %1\n").arg(pluginImportUri, pluginImportVersion).toAscii();
722 // pluginImportVersion can be empty
723 importCode += QString("import \".\" %2\n").arg(pluginImportVersion).toAscii();
726 // create a component with these imports to make sure the imports are valid
727 // and to populate the declarative meta type system
729 QByteArray code = importCode;
730 code += "QtObject {}";
731 QDeclarativeComponent c(&engine);
733 c.setData(code, QUrl::fromLocalFile(pluginImportPath + "/typelist.qml"));
735 if (!c.errors().isEmpty()) {
736 foreach (const QDeclarativeError &error, c.errors())
737 qWarning() << error.toString();
738 return EXIT_IMPORTERROR;
742 QSet<const QMetaObject *> candidates = collectReachableMetaObjects(&engine, defaultTypes);
743 candidates.subtract(defaultReachable);
745 // Also eliminate meta objects with the same classname.
746 // This is required because extended objects seem not to share
747 // a single meta object instance.
748 QSet<QByteArray> defaultReachableNames;
749 foreach (const QMetaObject *mo, defaultReachable)
750 defaultReachableNames.insert(QByteArray(mo->className()));
751 foreach (const QMetaObject *mo, candidates) {
752 if (!defaultReachableNames.contains(mo->className()))
757 // setup static rewrites of type names
758 cppToId.insert("QString", "string");
759 cppToId.insert("QDeclarativeEasingValueType::Type", "Type");
761 // start dumping data
763 QmlStreamWriter qml(&bytes);
765 qml.writeStartDocument();
766 qml.writeLibraryImport(QLatin1String("QtQuick.tooling"), 1, 1);
768 "// This file describes the plugin-supplied types contained in the library.\n"
769 "// It is used for QML tooling purposes only.\n"
771 qml.writeStartObject("Module");
773 // put the metaobjects into a map so they are always dumped in the same order
774 QMap<QString, const QMetaObject *> nameToMeta;
775 foreach (const QMetaObject *meta, metas)
776 nameToMeta.insert(convertToId(meta), meta);
780 dumper.setRelocatableModuleUri(pluginImportUri);
781 foreach (const QMetaObject *meta, nameToMeta) {
785 // define QEasingCurve as an extension of QDeclarativeEasingValueType, this way
786 // properties using the QEasingCurve type get useful type information.
787 if (pluginImportUri.isEmpty())
788 dumper.writeEasingCurve();
790 // write out module api elements
791 foreach (const ModuleApi &api, moduleApis) {
795 qml.writeEndObject();
796 qml.writeEndDocument();
798 std::cout << bytes.constData() << std::flush;
800 // workaround to avoid crashes on exit
802 timer.setSingleShot(true);
803 timer.setInterval(0);
804 QObject::connect(&timer, SIGNAL(timeout()), &app, SLOT(quit()));