main.cpp 13.7 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
#include <QtDeclarative/QDeclarativeView>
17
18
19
#ifdef QT_SIMULATOR
#include <QtGui/private/qsimulatorconnection_p.h>
#endif
20
21
22
#ifdef Q_OS_UNIX
#include <signal.h>
#endif
23

24
25
static QHash<QByteArray, QList<const QDeclarativeType *> > qmlTypesByCppName;
static QHash<QByteArray, QByteArray> cppToId;
26
QString currentProperty;
27

28
QByteArray convertToId(const QByteArray &cppName)
29
{
30
31
32
33
    QByteArray idName = cppToId.value(cppName, cppName);
    idName.replace("::", ".");
    idName.replace("/", ".");
    return idName;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
}

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);
    }

51
    *typeName = convertToId(*typeName);
52
53
54
55
56
57
58
}

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

59
60
61
62
63
    // dynamic meta objects break things badly
    const QMetaObjectPrivate *mop = reinterpret_cast<const QMetaObjectPrivate *>(meta->d.data);
    if (!(mop->flags & DynamicMetaObject))
        metas->insert(meta);

64
65
66
67
68
69
70
71
72
    processMetaObject(meta->superClass(), metas);
}

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

    const QMetaObject *meta = object->metaObject();
73
    qDebug() << "Processing object" << meta->className();
74
75
76
77
78
    processMetaObject(meta, metas);

    for (int index = 0; index < meta->propertyCount(); ++index) {
        QMetaProperty prop = meta->property(index);
        if (QDeclarativeMetaType::isQObject(prop.userType())) {
79
            qDebug() << "  Processing property" << prop.name();
80
            currentProperty = QString("%1::%2").arg(meta->className(), prop.name());
81
            QObject *oo = QDeclarativeMetaType::toQObject(prop.read(object));
82
83
            if (oo && !metas->contains(oo->metaObject()))
                processObject(oo, metas);
84
            currentProperty.clear();
85
86
87
88
89
90
91
92
93
        }
    }
}

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

94
void writeType(QXmlStreamAttributes *attrs, QByteArray typeName, bool isWritable = false)
95
96
97
98
99
100
{
    bool isList = false, isPointer = false;
    erasure(&typeName, &isList, &isPointer);
    attrs->append(QXmlStreamAttribute("type", typeName));
    if (isList)
        attrs->append(QXmlStreamAttribute("isList", "true"));
101
102
103
104
    if (isWritable)
        attrs->append(QXmlStreamAttribute("isWritable", "true"));
    if (isPointer)
        attrs->append(QXmlStreamAttribute("isPointer", "true"));
105
106
107
108
109
110
111
112
113
}

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

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

114
    writeType(&attributes, prop.typeName(), prop.isWritable());
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

    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));

148
    const QString typeName = convertToId(meth.typeName());
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
194
195
196
197
198
199
200
201
202
    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)
{
203
    QByteArray id = convertToId(meta->className());
204

205
206
207
    xml->writeStartElement("type");

    QXmlStreamAttributes attributes;
208
    attributes.append(QXmlStreamAttribute("name", id));
209

210
211
212
213
214
215
216
217
    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;
        }
    }

218
    if (meta->superClass())
219
        attributes.append(QXmlStreamAttribute("extends", convertToId(meta->superClass()->className())));
220
221
222

    xml->writeAttributes(attributes);

223
    QList<const QDeclarativeType *> qmlTypes = qmlTypesByCppName.value(meta->className());
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
    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();
    }

243
244
245
246
247
248
249
250
251
252
253
254
    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();
}

255
void writeEasingCurve(QXmlStreamWriter *xml)
256
{
257
    xml->writeStartElement("type");
258
259
    {
        QXmlStreamAttributes attributes;
260
        attributes.append(QXmlStreamAttribute("name", "QEasingCurve"));
261
        attributes.append(QXmlStreamAttribute("extends", "QDeclarativeEasingValueType"));
262
263
264
265
266
267
        xml->writeAttributes(attributes);
    }

    xml->writeEndElement();
}

268
269
270
271
272
273
274
275
276
#ifdef Q_OS_UNIX
void sigSegvHandler(int) {
    fprintf(stderr, "Error: qmldump SEGV\n");
    if (!currentProperty.isEmpty())
        fprintf(stderr, "While processing the property '%s', which probably has uninitialized data.\n", currentProperty.toLatin1().constData());
    exit(EXIT_FAILURE);
}
#endif

277
278
int main(int argc, char *argv[])
{
279
280
281
282
283
284
285
286
287
288
289
290
#ifdef Q_OS_UNIX
    // qmldump may crash, but we don't want any crash handlers to pop up
    // therefore we intercept the segfault and just exit() ourselves
    struct sigaction action;

    sigemptyset(&action.sa_mask);
    action.sa_handler = &sigSegvHandler;
    action.sa_flags   = 0;

    sigaction(SIGSEGV, &action, 0);
#endif

291
292
293
#ifdef QT_SIMULATOR
    QtSimulatorPrivate::SimulatorConnection::createStubInstance();
#endif
294
295
    QApplication app(argc, argv);

296
297
    if (argc != 1 && argc != 3) {
        qWarning() << "Usage: qmldump [plugin/import/path plugin.uri]";
298
299
300
301
302
        return 1;
    }

    QString pluginImportName;
    QString pluginImportPath;
303
304
305
    if (argc == 3) {
        pluginImportPath = QString(argv[1]);
        pluginImportName = QString(argv[2]);
306
307
    }

308
309
    QDeclarativeView view;
    QDeclarativeEngine *engine = view.engine();
310
311
312
    if (!pluginImportPath.isEmpty())
        engine->addImportPath(pluginImportPath);

313
314
315
316
317
318
319
320
321
322
323
    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;
        }
    }

324
325
    QByteArray importCode;
    importCode += "import Qt 4.7;\n";
326
327
328
    if (hasQtQuickModule) {
        importCode += "import QtQuick 1.0;\n";
    }
329
330
331
332
333
334
    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 {
335
        importCode += QString("import %0 1.0;\n").arg(pluginImportName).toAscii();
336
    }
337

338
    {
339
        QByteArray code = importCode;
340
341
        code += "Item {}";
        QDeclarativeComponent c(engine);
342

343
344
        c.setData(code, QUrl("xxx"));
        c.create();
345
346
        if (!c.errors().isEmpty())
            qDebug() << c.errorString();
347
348
    }

349
350
    cppToId.insert("QString", "string");
    cppToId.insert("QDeclarativeEasingValueType::Type", "Type");
351
352
353
354
355

    QSet<const QMetaObject *> metas;

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

356
    QHash<QByteArray, QSet<QByteArray> > extensions;
357
    foreach (const QDeclarativeType *ty, QDeclarativeMetaType::qmlTypes()) {
358
        qmlTypesByCppName[ty->metaObject()->className()].append(ty);
359
        if (ty->isExtendedType()) {
360
            extensions[ty->typeName()].insert(ty->metaObject()->className());
361
        } else {
362
            cppToId.insert(ty->metaObject()->className(), ty->metaObject()->className());
363
        }
364
365
366
        processDeclarativeType(ty, &metas);
    }

367
    // Adjust ids of extended objects.
368
369
370
371
372
373
374
    // 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()) {
375
376
377
378
379
380
381
382
383
384
385
386
        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;
387
388
389
        }
    }

390
    foreach (const QDeclarativeType *ty, QDeclarativeMetaType::qmlTypes()) {
391
392
393
        if (ty->isExtendedType())
            continue;

394
395
396
        QByteArray tyName = ty->qmlTypeName();
        tyName = tyName.mid(tyName.lastIndexOf('/') + 1);

397
        QByteArray code = importCode;
398
399
400
401
402
        code += tyName;
        code += " {}\n";

        QDeclarativeComponent c(engine);
        c.setData(code, QUrl("xxx"));
403
404
405
406
407
408

        QObject *object = c.create();
        if (object)
            processObject(object, &metas);
        else
            qDebug() << "Could not create" << tyName << ":" << c.errorString();
409
410
411
412
413
414
415
416
417
    }

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

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

418
    QMap<QString, const QMetaObject *> nameToMeta;
419
    foreach (const QMetaObject *meta, metas) {
420
        nameToMeta.insert(convertToId(meta->className()), meta);
421
422
    }
    foreach (const QMetaObject *meta, nameToMeta) {
423
424
        dump(meta, &xml);
    }
425

426
    // define QEasingCurve as an extension of QDeclarativeEasingValueType
427
    writeEasingCurve(&xml);
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442

    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();
}