void contextSetObject(QQmlContext_ *context, QObject_ *value) { QQmlContext *qcontext = reinterpret_cast<QQmlContext *>(context); QObject *qvalue = reinterpret_cast<QObject *>(value); // Give qvalue an engine reference if it doesn't yet have one. if (!qmlEngine(qvalue)) { QQmlEngine::setContextForObject(qvalue, qcontext->engine()->rootContext()); } qcontext->setContextObject(qvalue); }
QQmlContext* OffscreenSurface::contextForUrl(const QUrl& qmlSource, QQuickItem* parent, bool forceNewContext) { QQmlContext* targetContext = parent ? QQmlEngine::contextForObject(parent) : getSurfaceContext(); if (!targetContext) { targetContext = getSurfaceContext(); } if (getRootItem() && forceNewContext) { targetContext = new QQmlContext(targetContext, targetContext->engine()); } return targetContext; }
QJSValue QDeclarativeGeoRoute::path() const { QQmlContext *context = QQmlEngine::contextForObject(parent()); QQmlEngine *engine = context->engine(); QV4::ExecutionEngine *v4 = QQmlEnginePrivate::getV4Engine(engine); QV4::Scope scope(v4); QV4::Scoped<QV4::ArrayObject> pathArray(scope, v4->newArrayObject(route_.path().length())); for (int i = 0; i < route_.path().length(); ++i) { const QGeoCoordinate &c = route_.path().at(i); QV4::ScopedValue cv(scope, v4->fromVariant(QVariant::fromValue(c))); pathArray->putIndexed(i, cv); } return QJSValue(v4, pathArray.asReturnedValue()); }
bool LunaServiceCall::handleResponse(LSHandle *handle, LSMessage *message) { LunaServiceMessage msg(message); mResponseCount++; if (mCallback.isCallable()) { QQmlContext *context = QQmlEngine::contextForObject(parent()); mCallback.call(QJSValueList() << context->engine()->newQObject(&msg)); } const char* category = LSMessageGetCategory(message); bool messageInErrorCategory = (category && strcmp(LUNABUS_ERROR_CATEGORY, category) == 0); if (messageInErrorCategory || (mResponseLimit != -1 && mResponseCount >= mResponseLimit)) cancel(); return true; }
QJSValue QDeclarativePolylineMapItem::path() const { QQmlContext *context = QQmlEngine::contextForObject(parent()); QQmlEngine *engine = context->engine(); QV8Engine *v8Engine = QQmlEnginePrivate::getV8Engine(engine); QV8ValueTypeWrapper *valueTypeWrapper = v8Engine->valueTypeWrapper(); v8::Local<v8::Array> pathArray = v8::Array::New(path_.length()); for (int i = 0; i < path_.length(); ++i) { const QGeoCoordinate &c = path_.at(i); QQmlValueType *vt = QQmlValueTypeFactory::valueType(qMetaTypeId<QGeoCoordinate>()); v8::Local<v8::Object> cv = valueTypeWrapper->newValueType(QVariant::fromValue(c), vt); pathArray->Set(i, cv); } return v8Engine->scriptValueFromInternal(pathArray); }
QJSValue QDeclarativePolylineMapItem::path() const { QQmlContext *context = QQmlEngine::contextForObject(parent()); QQmlEngine *engine = context->engine(); QV8Engine *v8Engine = QQmlEnginePrivate::getV8Engine(engine); QV4::ExecutionEngine *v4 = QV8Engine::getV4(v8Engine); QV4::Scope scope(v4); QV4::Scoped<QV4::ArrayObject> pathArray(scope, v4->newArrayObject(path_.length())); for (int i = 0; i < path_.length(); ++i) { const QGeoCoordinate &c = path_.at(i); QQmlValueType *vt = QQmlValueTypeFactory::valueType(qMetaTypeId<QGeoCoordinate>()); QV4::ScopedValue cv(scope, QV4::QmlValueTypeWrapper::create(v8Engine, QVariant::fromValue(c), vt)); pathArray->putIndexed(i, cv); } return new QJSValuePrivate(v4, QV4::ValueRef(pathArray)); }
void tst_qqmltranslation::idTranslation() { QTranslator translator; translator.load(QLatin1String("qmlid_fr"), dataDirectory()); QCoreApplication::installTranslator(&translator); QQmlEngine engine; QQmlComponent component(&engine, testFileUrl("idtranslation.qml")); QObject *object = component.create(); QVERIFY(object != 0); { QQmlContext *context = qmlContext(object); QQmlEnginePrivate *engine = QQmlEnginePrivate::get(context->engine()); QQmlTypeData *typeData = engine->typeLoader.getType(context->baseUrl()); QQmlCompiledData *cdata = typeData->compiledData(); QVERIFY(cdata); const QV4::CompiledData::Unit *unit = cdata->compilationUnit->data; const QV4::CompiledData::Object *rootObject = unit->objectAt(unit->indexOfRootObject); const QV4::CompiledData::Binding *binding = rootObject->bindingTable(); for (quint32 i = 0; i < rootObject->nBindings; ++i, ++binding) { const QString propertyName = unit->stringAt(binding->propertyNameIndex); if (propertyName == "idTranslation") { if (binding->type != QV4::CompiledData::Binding::Type_TranslationById) qDebug() << "binding for property" << propertyName << "is not a compiled translation"; QCOMPARE(binding->type, quint32(QV4::CompiledData::Binding::Type_TranslationById)); } else { QVERIFY(binding->type != QV4::CompiledData::Binding::Type_Translation); } } } QCOMPARE(object->property("idTranslation").toString(), QLatin1String("bonjour tout le monde")); QCOMPARE(object->property("idTranslation2").toString(), QLatin1String("bonjour tout le monde")); QCOMPARE(object->property("idTranslation3").toString(), QLatin1String("bonjour tout le monde")); QCoreApplication::removeTranslator(&translator); delete object; }
void tst_qqmltranslation::translation() { QFETCH(QString, translation); QFETCH(QUrl, testFile); QFETCH(bool, verifyCompiledData); QTranslator translator; translator.load(translation, dataDirectory()); QCoreApplication::installTranslator(&translator); QQmlEngine engine; QQmlComponent component(&engine, testFile); QObject *object = component.create(); QVERIFY(object != 0); if (verifyCompiledData) { QQmlContext *context = qmlContext(object); QQmlEnginePrivate *engine = QQmlEnginePrivate::get(context->engine()); QQmlTypeData *typeData = engine->typeLoader.getType(context->baseUrl()); QQmlCompiledData *cdata = typeData->compiledData(); QVERIFY(cdata); QSet<QString> compiledTranslations; compiledTranslations << QStringLiteral("basic") << QStringLiteral("disambiguation") << QStringLiteral("singular") << QStringLiteral("plural"); const QV4::CompiledData::Unit *unit = cdata->compilationUnit->data; const QV4::CompiledData::Object *rootObject = unit->objectAt(unit->indexOfRootObject); const QV4::CompiledData::Binding *binding = rootObject->bindingTable(); for (quint32 i = 0; i < rootObject->nBindings; ++i, ++binding) { const QString propertyName = unit->stringAt(binding->propertyNameIndex); const bool expectCompiledTranslation = compiledTranslations.contains(propertyName); if (expectCompiledTranslation) { if (binding->type != QV4::CompiledData::Binding::Type_Translation) qDebug() << "binding for property" << propertyName << "is not a compiled translation"; QCOMPARE(binding->type, quint32(QV4::CompiledData::Binding::Type_Translation)); } else { if (binding->type == QV4::CompiledData::Binding::Type_Translation) qDebug() << "binding for property" << propertyName << "is not supposed to be a compiled translation"; QVERIFY(binding->type != QV4::CompiledData::Binding::Type_Translation); } } } QCOMPARE(object->property("basic").toString(), QLatin1String("bonjour")); QCOMPARE(object->property("basic2").toString(), QLatin1String("au revoir")); QCOMPARE(object->property("basic3").toString(), QLatin1String("bonjour")); QCOMPARE(object->property("disambiguation").toString(), QLatin1String("salut")); QCOMPARE(object->property("disambiguation2").toString(), QString::fromUtf8("\xc3\xa0 plus tard")); QCOMPARE(object->property("disambiguation3").toString(), QLatin1String("salut")); QCOMPARE(object->property("noop").toString(), QLatin1String("bonjour")); QCOMPARE(object->property("noop2").toString(), QLatin1String("au revoir")); QCOMPARE(object->property("singular").toString(), QLatin1String("1 canard")); QCOMPARE(object->property("singular2").toString(), QLatin1String("1 canard")); QCOMPARE(object->property("plural").toString(), QLatin1String("2 canards")); QCOMPARE(object->property("plural2").toString(), QLatin1String("2 canards")); QCoreApplication::removeTranslator(&translator); delete object; }
void QQuickStyledTextPrivate::parseImageAttributes(const QChar *&ch, const QString &textIn, QString &textOut) { qreal imgWidth = 0.0; if (!updateImagePositions) { QQuickStyledTextImgTag *image = new QQuickStyledTextImgTag; image->position = textOut.length() + 1; QPair<QStringRef,QStringRef> attr; do { attr = parseAttribute(ch, textIn); if (attr.first == QLatin1String("src")) { image->url = QUrl(attr.second.toString()); } else if (attr.first == QLatin1String("width")) { image->size.setWidth(attr.second.toString().toInt()); } else if (attr.first == QLatin1String("height")) { image->size.setHeight(attr.second.toString().toInt()); } else if (attr.first == QLatin1String("align")) { if (attr.second.toString() == QLatin1String("top")) { image->align = QQuickStyledTextImgTag::Top; } else if (attr.second.toString() == QLatin1String("middle")) { image->align = QQuickStyledTextImgTag::Middle; } } } while (!ch->isNull() && !attr.first.isEmpty()); if (preloadImages && !image->size.isValid()) { // if we don't know its size but the image is a local image, // we load it in the pixmap cache and save its implicit size // to avoid a relayout later on. QUrl url = baseUrl.resolved(image->url); if (url.isLocalFile()) { image->pix = new QQuickPixmap(context->engine(), url, image->size); if (image->pix && image->pix->isReady()) { image->size = image->pix->implicitSize(); } else { delete image->pix; image->pix = 0; } } } imgWidth = image->size.width(); imgTags->append(image); } else { // if we already have a list of img tags for this text // we only want to update the positions of these tags. QQuickStyledTextImgTag *image = imgTags->value(nbImages); image->position = textOut.length() + 1; imgWidth = image->size.width(); QPair<QStringRef,QStringRef> attr; do { attr = parseAttribute(ch, textIn); } while (!ch->isNull() && !attr.first.isEmpty()); nbImages++; } QFontMetricsF fm(layout.font()); QString padding(qFloor(imgWidth / fm.width(QChar::Nbsp)), QChar::Nbsp); textOut += QLatin1Char(' '); textOut += padding; textOut += QLatin1Char(' '); }