示例#1
0
QObject* CuteNews::createQmlObject(const QString &fileName) {
    initEngine();
    QDeclarativeContext *context = new QDeclarativeContext(m_engine->rootContext());
    QDeclarativeComponent *component = new QDeclarativeComponent(m_engine, fileName, this);
    
    if (QObject *obj = component->create(context)) {
        context->setParent(obj);
        return obj;
    }

    if (component->isError()) {
        foreach (const QDeclarativeError &error, component->errors()) {
            Logger::log("CuteNews::createQmlObject(). Error: " + error.toString());
        }        
    }
int main(int argc, char *argv[])
{
    // Get options from the command line
    QApplication a(argc, argv);
    QTextStream qout(stdout);
    if (argc < 2) {
        qout << "Usage: " << argv[0] << " <qmlfile> [outputfolder]";
        return(1);
    }
    // source qml file
    QFileInfo qmlfile(argv[1]);
    if (!qmlfile.exists()) {
        qout << "File not found: " << qmlfile.filePath();
        return (1);
    }
    // output folder
    QFileInfo outputfolder("");
    if (argc == 3) {
        outputfolder.setFile(argv[2]);
        if (!outputfolder.isDir()) {
            qout << "Output folder doesn't exist: " << outputfolder.filePath();
            return (1);
        }
    }

    // Parse the qml file
    QDeclarativeEngine* qmlEngine = new QDeclarativeEngine();
    qmlEngine->addPluginPath("../../imports/com/nokia/symbian"); // component's own imports specified fully so point directly to import folder where dll built to
    //qmlEngine->addImportPath("../../imports");
    QDeclarativeComponent* qmlComponent = new QDeclarativeComponent(qmlEngine, QUrl::fromLocalFile(qmlfile.filePath()));
    if (qmlComponent->isError()) {
        qDebug() << qmlComponent->errors();
        return (1);
        }
    QObject* qmlObject = qmlComponent->create();
    if ( !qmlObject ) {
        qout << "Failed to load: " << qmlfile.path();
        qDebug() << qmlComponent->errors();
        return (1);
    }

    //explicit mapping of Qt types to QML types
    //it's too complicated to scan through the basic types using private headers so hard-coding this
    QMap<QString,QString> qmlBasicType;
    qmlBasicType.insert("QAction",                      "action");
    qmlBasicType.insert("bool",                         "bool");
    qmlBasicType.insert("QColor",                       "color");
    qmlBasicType.insert("QDateTime",                    "date");
    qmlBasicType.insert("double",                       "double");
    qmlBasicType.insert("enumeration",                  "enumeration");
    qmlBasicType.insert("QFont",                        "font");
    qmlBasicType.insert("int",                          "int");
    qmlBasicType.insert("ListProperty",                 "list");
    qmlBasicType.insert("QPointF",                      "point");
    qmlBasicType.insert("qreal",                        "real");
    qmlBasicType.insert("QRectF",                       "rect");
    qmlBasicType.insert("QSize",                        "size");
    qmlBasicType.insert("QString",                      "string");
    qmlBasicType.insert("QDateTime",                    "time"); //Duplicate...
    qmlBasicType.insert("QUrl",                         "url");
    qmlBasicType.insert("QVariant",                     "variant");
    qmlBasicType.insert("TransformOrigin",              "enumeration");
    qmlBasicType.insert("QVector3d",                    "vector3d");
    qmlBasicType.insert("QGraphicsObject",              "Item");
    qmlBasicType.insert("SStyleWrapper",                "style");
    qmlBasicType.insert("QObject",                      "QtObject");
    qmlBasicType.insert("QValidator",                   "validator");
    qmlBasicType.insert("SDeclarativeImplicitSizeItem", "Item");
    qmlBasicType.insert("ListProperty<QObject>",        "list<QtObject>");
    qmlBasicType.insert("ListProperty<QGraphicsObject>","list<Item>");


    // Get the component name and remove the _QMLTYPE part
    QString componentname(qmlObject->metaObject()->className());
    componentname =  componentname.left(componentname.indexOf("_QML"));

    QStringList memberlist;

    // Collect all the properties (excluding inherited)
    for(int i = qmlObject->metaObject()->propertyOffset() ; i < qmlObject->metaObject()->propertyCount(); ++i) {
        memberlist.append(parseProperty(&qmlObject->metaObject()->property(i),qmlBasicType,componentname));
    }

    // Collect all the methods (excluding inherited)
    for(int i = qmlObject->metaObject()->methodOffset() ; i < qmlObject->metaObject()->methodCount(); ++i) {
        memberlist.append(parseMethod(&qmlObject->metaObject()->method(i),qmlBasicType,componentname));
    }

    // Output the results
    memberlist.sort();
    if (outputfolder.exists()) { // output to file
        QFile outputfile(outputfolder.filePath() + "/" + componentname.toLower() + ".txt");
        if (outputfile.open(QFile::WriteOnly)) {
            QTextStream fout(&outputfile);
            for (int i=0; i<memberlist.size();i++) {
                if (memberlist.at(i) != QString::null)
                    fout << memberlist.at(i) << "\n";
            }
        }
        else {
            qout << "Failed to open: " << outputfile.fileName();
            return(1);
        }
        outputfile.close();
    }
    else { // output to console
        for (int i=0; i<memberlist.size();i++) {
            if (memberlist.at(i) != QString::null)
                qout << memberlist.at(i) << "\n";
        }
    }

}