main.cpp 12.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#include <QApplication>
#include <QSet>
#include <QXmlStreamWriter>
#include <QXmlStreamReader>

#include <QMetaObject>
#include <QMetaProperty>
#include <QPushButton>
#include <QDebug>
#include <iostream>
#include <QtDeclarative>
12
13
#include <QtCore/private/qobject_p.h>
#include <QtCore/private/qmetaobject_p.h>
14
#include <QtDeclarative/private/qdeclarativemetatype_p.h>
15
#include <QtDeclarative/private/qdeclarativeopenmetaobject_p.h>
16
17
#include <QtDeclarative/QDeclarativeView>

18
19
static QHash<QByteArray, QList<const QDeclarativeType *> > qmlTypesByCppName;
static QHash<QByteArray, QByteArray> cppToId;
20

21
QByteArray convertToId(const QByteArray &cppName)
22
{
23
24
25
26
    QByteArray idName = cppToId.value(cppName, cppName);
    idName.replace("::", ".");
    idName.replace("/", ".");
    return idName;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
}

void erasure(QByteArray *typeName, bool *isList, bool *isPointer)
{
    static QByteArray declListPrefix = "QDeclarativeListProperty<";

    if (typeName->endsWith('*')) {
        *isPointer = true;
        typeName->truncate(typeName->length() - 1);
        erasure(typeName, isList, isPointer);
    } else if (typeName->startsWith(declListPrefix)) {
        *isList = true;
        typeName->truncate(typeName->length() - 1); // get rid of the suffix '>'
        *typeName = typeName->mid(declListPrefix.size());
        erasure(typeName, isList, isPointer);
    }

44
    *typeName = convertToId(*typeName);
45
46
47
48
49
50
51
}

void processMetaObject(const QMetaObject *meta, QSet<const QMetaObject *> *metas)
{
    if (! meta || metas->contains(meta))
        return;

52
53
54
55
56
    // dynamic meta objects break things badly
    const QMetaObjectPrivate *mop = reinterpret_cast<const QMetaObjectPrivate *>(meta->d.data);
    if (!(mop->flags & DynamicMetaObject))
        metas->insert(meta);

57
58
59
60
61
62
63
64
65
    processMetaObject(meta->superClass(), metas);
}

void processObject(QObject *object, QSet<const QMetaObject *> *metas)
{
    if (! object)
        return;

    const QMetaObject *meta = object->metaObject();
66
    qDebug() << "Processing object" << meta->className();
67
68
69
70
71
    processMetaObject(meta, metas);

    for (int index = 0; index < meta->propertyCount(); ++index) {
        QMetaProperty prop = meta->property(index);
        if (QDeclarativeMetaType::isQObject(prop.userType())) {
72
            qDebug() << "  Processing property" << prop.name();
73
            QObject *oo = QDeclarativeMetaType::toQObject(prop.read(object));
74
75
            if (oo && !metas->contains(oo->metaObject()))
                processObject(oo, metas);
76
77
78
79
80
81
82
83
84
        }
    }
}

void processDeclarativeType(const QDeclarativeType *ty, QSet<const QMetaObject *> *metas)
{
    processMetaObject(ty->metaObject(), metas);
}

85
void writeType(QXmlStreamAttributes *attrs, QByteArray typeName, bool isWritable = false)
86
87
88
89
90
91
{
    bool isList = false, isPointer = false;
    erasure(&typeName, &isList, &isPointer);
    attrs->append(QXmlStreamAttribute("type", typeName));
    if (isList)
        attrs->append(QXmlStreamAttribute("isList", "true"));
92
93
94
95
    if (isWritable)
        attrs->append(QXmlStreamAttribute("isWritable", "true"));
    if (isPointer)
        attrs->append(QXmlStreamAttribute("isPointer", "true"));
96
97
98
99
100
101
102
103
104
}

void dump(const QMetaProperty &prop, QXmlStreamWriter *xml)
{
    xml->writeStartElement("property");

    QXmlStreamAttributes attributes;
    attributes.append(QXmlStreamAttribute("name", QString::fromUtf8(prop.name())));

105
    writeType(&attributes, prop.typeName(), prop.isWritable());
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

    xml->writeAttributes(attributes);
    xml->writeEndElement();
}

void dump(const QMetaMethod &meth, QXmlStreamWriter *xml)
{
    if (meth.methodType() == QMetaMethod::Signal) {
        if (meth.access() != QMetaMethod::Protected)
            return; // nothing to do.
    } else if (meth.access() != QMetaMethod::Public) {
        return; // nothing to do.
    }

    QByteArray name = meth.signature();
    int lparenIndex = name.indexOf('(');
    if (lparenIndex == -1) {
        return; // invalid signature
    }

    name = name.left(lparenIndex);


    QString elementName = QLatin1String("method");

    QXmlStreamAttributes attributes;
    if (meth.methodType() == QMetaMethod::Signal)
        elementName = QLatin1String("signal");

    xml->writeStartElement(elementName);

    attributes.append(QXmlStreamAttribute("name", name));

139
    const QString typeName = convertToId(meth.typeName());
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
    if (! typeName.isEmpty())
        attributes.append(QXmlStreamAttribute("type", typeName));

    xml->writeAttributes(attributes);

    for (int i = 0; i < meth.parameterTypes().size(); ++i) {
        QByteArray argName = meth.parameterNames().at(i);

        xml->writeStartElement("param");

        QXmlStreamAttributes attrs;

        if (! argName.isEmpty())
            attrs.append(QXmlStreamAttribute("name", argName));

        writeType(&attrs, meth.parameterTypes().at(i));
        xml->writeAttributes(attrs);

        xml->writeEndElement();
    }

    xml->writeEndElement();
}

void dump(const QMetaEnum &e, QXmlStreamWriter *xml)
{
    xml->writeStartElement("enum");

    QXmlStreamAttributes attributes;
    attributes.append(QXmlStreamAttribute("name", QString::fromUtf8(e.name()))); // ### FIXME
    xml->writeAttributes(attributes);

    for (int index = 0; index < e.keyCount(); ++index) {
        xml->writeStartElement("enumerator");

        QXmlStreamAttributes attributes;
        attributes.append(QXmlStreamAttribute("name", QString::fromUtf8(e.key(index))));
        attributes.append(QXmlStreamAttribute("value", QString::number(e.value(index))));
        xml->writeAttributes(attributes);

        xml->writeEndElement();
    }

    xml->writeEndElement();
}

class FriendlyQObject: public QObject
{
public:
    static const QMetaObject *qtMeta() { return &staticQtMetaObject; }
};

void dump(const QMetaObject *meta, QXmlStreamWriter *xml)
{
194
    QByteArray id = convertToId(meta->className());
195

196
197
198
    xml->writeStartElement("type");

    QXmlStreamAttributes attributes;
199
    attributes.append(QXmlStreamAttribute("name", id));
200

201
202
203
204
205
206
207
208
    for (int index = meta->classInfoCount() - 1 ; index >= 0 ; --index) {
        QMetaClassInfo classInfo = meta->classInfo(index);
        if (QLatin1String(classInfo.name()) == QLatin1String("DefaultProperty")) {
            attributes.append(QXmlStreamAttribute("defaultProperty", QLatin1String(classInfo.value())));
            break;
        }
    }

209
    if (meta->superClass())
210
        attributes.append(QXmlStreamAttribute("extends", convertToId(meta->superClass()->className())));
211
212
213

    xml->writeAttributes(attributes);

214
    QList<const QDeclarativeType *> qmlTypes = qmlTypesByCppName.value(meta->className());
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
    if (!qmlTypes.isEmpty()) {
        xml->writeStartElement("exports");
        foreach (const QDeclarativeType *qmlTy, qmlTypes) {
            QXmlStreamAttributes moduleAttributes;
            const QString qmlTyName = qmlTy->qmlTypeName();
            int slashIdx = qmlTyName.lastIndexOf(QLatin1Char('/'));
            if (slashIdx == -1)
                continue;
            const QString moduleName = qmlTyName.left(slashIdx);
            const QString typeName = qmlTyName.mid(slashIdx + 1);
            moduleAttributes.append(QXmlStreamAttribute("module", moduleName));
            moduleAttributes.append(QXmlStreamAttribute("version", QString("%1.%2").arg(qmlTy->majorVersion()).arg(qmlTy->minorVersion())));
            moduleAttributes.append(QXmlStreamAttribute("type", typeName));
            xml->writeEmptyElement("export");
            xml->writeAttributes(moduleAttributes);
        }
        xml->writeEndElement();
    }

234
235
236
237
238
239
240
241
242
243
244
245
    for (int index = meta->enumeratorOffset(); index < meta->enumeratorCount(); ++index)
        dump(meta->enumerator(index), xml);

    for (int index = meta->propertyOffset(); index < meta->propertyCount(); ++index)
        dump(meta->property(index), xml);

    for (int index = meta->methodOffset(); index < meta->methodCount(); ++index)
        dump(meta->method(index), xml);

    xml->writeEndElement();
}

246
void writeEasingCurve(QXmlStreamWriter *xml)
247
{
248
    xml->writeStartElement("type");
249
250
    {
        QXmlStreamAttributes attributes;
251
        attributes.append(QXmlStreamAttribute("name", "QEasingCurve"));
252
        attributes.append(QXmlStreamAttribute("extends", "QDeclarativeEasingValueType"));
253
254
255
256
257
258
        xml->writeAttributes(attributes);
    }

    xml->writeEndElement();
}

259
260
261
262
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

263
264
    if (argc != 1 && argc != 3) {
        qWarning() << "Usage: qmldump [plugin/import/path plugin.uri]";
265
266
267
268
269
        return 1;
    }

    QString pluginImportName;
    QString pluginImportPath;
270
271
272
    if (argc == 3) {
        pluginImportPath = QString(argv[1]);
        pluginImportName = QString(argv[2]);
273
274
    }

275
276
    QDeclarativeView view;
    QDeclarativeEngine *engine = view.engine();
277
278
279
    if (!pluginImportPath.isEmpty())
        engine->addImportPath(pluginImportPath);

280
281
282
283
284
285
286
287
288
289
290
    bool hasQtQuickModule = false;
    {
        QByteArray code = "import QtQuick 1.0; Item {}";
        QDeclarativeComponent c(engine);
        c.setData(code, QUrl("xxx"));
        c.create();
        if (c.errors().isEmpty()) {
            hasQtQuickModule = true;
        }
    }

291
292
    QByteArray importCode;
    importCode += "import Qt 4.7;\n";
293
294
295
    if (hasQtQuickModule) {
        importCode += "import QtQuick 1.0;\n";
    }
296
297
298
299
300
301
    if (pluginImportName.isEmpty()) {
        importCode += "import Qt.labs.particles 4.7;\n";
        importCode += "import Qt.labs.gestures 4.7;\n";
        importCode += "import Qt.labs.folderlistmodel 4.7;\n";
        importCode += "import QtWebKit 1.0;\n";
    } else {
302
        importCode += QString("import %0 1.0;\n").arg(pluginImportName).toAscii();
303
    }
304

305
    {
306
        QByteArray code = importCode;
307
308
        code += "Item {}";
        QDeclarativeComponent c(engine);
309

310
311
        c.setData(code, QUrl("xxx"));
        c.create();
312
313
        if (!c.errors().isEmpty())
            qDebug() << c.errorString();
314
315
    }

316
317
    cppToId.insert("QString", "string");
    cppToId.insert("QDeclarativeEasingValueType::Type", "Type");
318
319
320
321
322

    QSet<const QMetaObject *> metas;

    metas.insert(FriendlyQObject::qtMeta());

323
    QHash<QByteArray, QSet<QByteArray> > extensions;
324
    foreach (const QDeclarativeType *ty, QDeclarativeMetaType::qmlTypes()) {
325
        qmlTypesByCppName[ty->metaObject()->className()].append(ty);
326
        if (ty->isExtendedType()) {
327
            extensions[ty->typeName()].insert(ty->metaObject()->className());
328
        } else {
329
            cppToId.insert(ty->metaObject()->className(), ty->metaObject()->className());
330
        }
331
332
333
        processDeclarativeType(ty, &metas);
    }

334
    // Adjust ids of extended objects.
335
336
337
338
339
340
341
    // The chain ends up being:
    // __extended__.originalname - the base object
    // __extension_0_.originalname - first extension
    // ..
    // __extension_n-2_.originalname - second to last extension
    // originalname - last extension
    foreach (const QByteArray &extendedCpp, extensions.keys()) {
342
343
344
345
346
347
348
349
350
351
352
353
        const QByteArray extendedId = cppToId.value(extendedCpp);
        cppToId.insert(extendedCpp, "__extended__." + extendedId);
        QSet<QByteArray> extensionCppNames = extensions.value(extendedCpp);
        int c = 0;
        foreach (const QByteArray &extensionCppName, extensionCppNames) {
            if (c != extensionCppNames.size() - 1) {
                QByteArray adjustedName = QString("__extension__%1.%2").arg(QString::number(c), QString(extendedId)).toAscii();
                cppToId.insert(extensionCppName, adjustedName);
            } else {
                cppToId.insert(extensionCppName, extendedId);
            }
            ++c;
354
355
356
        }
    }

357
    foreach (const QDeclarativeType *ty, QDeclarativeMetaType::qmlTypes()) {
358
359
360
        if (ty->isExtendedType())
            continue;

361
362
363
        QByteArray tyName = ty->qmlTypeName();
        tyName = tyName.mid(tyName.lastIndexOf('/') + 1);

364
        QByteArray code = importCode;
365
366
367
368
369
        code += tyName;
        code += " {}\n";

        QDeclarativeComponent c(engine);
        c.setData(code, QUrl("xxx"));
370
371
372
373
374
375

        QObject *object = c.create();
        if (object)
            processObject(object, &metas);
        else
            qDebug() << "Could not create" << tyName << ":" << c.errorString();
376
377
378
379
380
381
382
383
384
    }

    QByteArray bytes;
    QXmlStreamWriter xml(&bytes);
    xml.setAutoFormatting(true);

    xml.writeStartDocument("1.0");
    xml.writeStartElement("module");

385
    QMap<QString, const QMetaObject *> nameToMeta;
386
    foreach (const QMetaObject *meta, metas) {
387
        nameToMeta.insert(convertToId(meta->className()), meta);
388
389
    }
    foreach (const QMetaObject *meta, nameToMeta) {
390
391
        dump(meta, &xml);
    }
392

393
    // define QEasingCurve as an extension of QDeclarativeEasingValueType
394
    writeEasingCurve(&xml);
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409

    xml.writeEndElement();
    xml.writeEndDocument();

    std::cout << bytes.constData();

    QTimer timer;
    timer.setSingleShot(true);
    timer.setInterval(0);
    QObject::connect(&timer, SIGNAL(timeout()), &app, SLOT(quit()));

    timer.start();

    return app.exec();
}