Example #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());
        }        
    }
void QJnextMainLoop::createObjectFromDocument(QVariantList& args,
		QByteArray* result) {
	QByteArray uri = args.takeFirst().toByteArray();

	Q_UNUSED(result);

	QDeclarativeComponent * component = new QDeclarativeComponent(
			declarativeEngine, uri);
	if (component->isLoading()) {
		*result = "Unimplemented: Network components";
		qWarning() << *result;
		return;

		// TODO: support loading qml documents from the network
		/*QObject::connect(component,
		 SIGNAL(statusChanged(QDeclarativeComponent::Status)), this,
		 SLOT(continueLoadingComponent()));*/
	}

	continueLoadingComponent(component, result);
}
QByteArray QDeclarativeDataTest::msgComponentError(const QDeclarativeComponent &c,
                                                   const QDeclarativeEngine *engine /* = 0 */)
{
    QString result;
    const QList<QDeclarativeError> errors = c.errors();
    QTextStream str(&result);
    str << "Component '" << c.url().toString() << "' has " << errors.size()
        << " errors: '";
    for (int i = 0; i < errors.size(); ++i) {
        if (i)
            str << ", '";
        str << errors.at(i).toString() << '\'';

    }
    if (!engine)
        if (QDeclarativeContext *context = c.creationContext())
            engine = context->engine();
    if (engine) {
        str << " Import paths: (" << engine->importPathList().join(QStringLiteral(", "))
            << ") Plugin paths: (" << engine->pluginPathList().join(QStringLiteral(", "))
            << ')';
    }
    return result.toLocal8Bit();
}
Example #4
0
void QmlProject::parseProject(RefreshOptions options)
{
    Core::MessageManager *messageManager = Core::ICore::messageManager();
    if (options & Files) {
        if (options & ProjectFile)
            delete m_projectItem.data();
        if (!m_projectItem) {
            Utils::FileReader reader;
            if (reader.fetch(m_fileName)) {
                QDeclarativeComponent *component = new QDeclarativeComponent(&m_engine, this);
                component->setData(reader.data(), QUrl::fromLocalFile(m_fileName));
                if (component->isReady()
                    && qobject_cast<QmlProjectItem*>(component->create())) {
                    m_projectItem = qobject_cast<QmlProjectItem*>(component->create());
                    connect(m_projectItem.data(), SIGNAL(qmlFilesChanged(QSet<QString>,QSet<QString>)),
                            this, SLOT(refreshFiles(QSet<QString>,QSet<QString>)));
                } else {
                    messageManager->printToOutputPane(tr("Error while loading project file %1.").arg(m_fileName), Core::MessageManager::NoModeSwitch);
                    messageManager->printToOutputPane(component->errorString(), Core::MessageManager::NoModeSwitch);
                }
            } else {
                messageManager->printToOutputPane(tr("QML project: %1").arg(reader.errorString()), Core::MessageManager::NoModeSwitch);
            }
        }
        if (m_projectItem) {
            m_projectItem.data()->setSourceDirectory(projectDir().path());
            m_modelManager->updateSourceFiles(m_projectItem.data()->files(), true);

            QString mainFilePath = m_projectItem.data()->mainFile();
            if (!mainFilePath.isEmpty()) {
                mainFilePath = projectDir().absoluteFilePath(mainFilePath);
                Utils::FileReader reader;
                QString errorMessage;
                if (!reader.fetch(mainFilePath, &errorMessage)) {
                    messageManager->printToOutputPane(
                                tr("Warning while loading project file %1.").arg(m_fileName),
                                Core::MessageManager::NoModeSwitch);
                    messageManager->printToOutputPane(errorMessage, Core::MessageManager::NoModeSwitch);
                } else {
                    m_defaultImport = detectImport(QString::fromUtf8(reader.data()));
                }
            }
        }
        m_rootNode->refresh();
    }

    if (options & Configuration) {
        // update configuration
    }

    if (options & Files)
        emit fileListChanged();
}
void QDeclarativeViewInspectorPrivate::_q_createQmlObject(const QString &qml, QObject *parent,
                                                         const QStringList &importList,
                                                         const QString &filename, int order)
{
    if (!parent)
        return;

    QString imports;
    foreach (const QString &s, importList) {
        imports += s;
        imports += QLatin1Char('\n');
    }

    QDeclarativeContext *parentContext = view->engine()->contextForObject(parent);
    QDeclarativeComponent component(view->engine(), q);
    QByteArray constructedQml = QString(imports + qml).toLatin1();

    component.setData(constructedQml, filename);
    QObject *newObject = component.create(parentContext);
    if (newObject) {
        newObject->setParent(parent);
        do {
            // add child item
            QDeclarativeItem *parentItem = qobject_cast<QDeclarativeItem*>(parent);
            QDeclarativeItem *newItem    = qobject_cast<QDeclarativeItem*>(newObject);
            if (parentItem && newItem) {
                newItem->setParentItem(parentItem);
                break;
            }
*/
void MainLoader::load()
{
#ifdef Q_OS_SYMBIAN
    // Lock orientation in Symbian for the splash screen
    CAknAppUi* appUi = dynamic_cast<CAknAppUi*>( CEikonEnv::Static()->AppUi() );

    TRAP_IGNORE(
        if ( appUi )
            {
            appUi->SetOrientationL( CAknAppUi::EAppUiOrientationPortrait );
            }
    );
#endif

    QDeclarativeComponent splashScreenComponent(mView.engine(), mSplashScreenUrl);
    mSplashScreenItem = qobject_cast<QDeclarativeItem *>(splashScreenComponent.create());

    qDebug() << mView.width() << mView.height();

    mSplashScreenItem->setWidth(mView.width());
    mSplashScreenItem->setHeight(mView.height());

    connect(mSplashScreenItem, SIGNAL(hidden()),
            this, SLOT(destroySplashScreen()),
            Qt::QueuedConnection);

    mView.scene()->addItem(mSplashScreenItem);
    mView.showFullScreen();
    QTimer::singleShot(100, this, SLOT(loadMainQml()));
}
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";
        }
    }

}