QVariant QtPixmapInstance::variantFromObject(JSObject* object, QMetaType::Type hint)
{
    if (!object)
        goto returnEmptyVariant;

    if (object->inherits(&JSHTMLImageElement::s_info)) {
        JSHTMLImageElement* elementJSWrapper = static_cast<JSHTMLImageElement*>(object);
        HTMLImageElement* imageElement = static_cast<HTMLImageElement*>(elementJSWrapper->impl());

        if (!imageElement)
            goto returnEmptyVariant;

        CachedImage* cachedImage = imageElement->cachedImage();
        if (!cachedImage)
            goto returnEmptyVariant;

        Image* image = cachedImage->image();
        if (!image)
            goto returnEmptyVariant;

        QPixmap* pixmap = image->nativeImageForCurrentFrame();
        if (!pixmap)
            goto returnEmptyVariant;

        return (hint == static_cast<QMetaType::Type>(qMetaTypeId<QPixmap>()))
                  ? QVariant::fromValue<QPixmap>(*pixmap)
                  : QVariant::fromValue<QImage>(pixmap->toImage());
    }

    if (object->inherits(&QtPixmapRuntimeObject::s_info)) {
        QtPixmapRuntimeObject* runtimeObject = static_cast<QtPixmapRuntimeObject*>(object);
        QtPixmapInstance* instance = static_cast<QtPixmapInstance*>(runtimeObject->getInternalInstance());
        if (!instance)
            goto returnEmptyVariant;

        if (hint == qMetaTypeId<QPixmap>())
            return QVariant::fromValue<QPixmap>(instance->toPixmap());

        if (hint == qMetaTypeId<QImage>())
            return QVariant::fromValue<QImage>(instance->toImage());
    }

returnEmptyVariant:
    if (hint == qMetaTypeId<QPixmap>())
        return QVariant::fromValue<QPixmap>(QPixmap());
    if (hint == qMetaTypeId<QImage>())
        return QVariant::fromValue<QImage>(QImage());
    return QVariant();
}
Exemple #2
0
QVariant QtPixmapRuntime::toQt(JSContextRef context, JSObjectRef obj, QMetaType::Type hint, JSValueRef* exception)
{
    if (!obj)
        return emptyVariantForHint(hint);

    if (JSValueIsObjectOfClass(context, obj, QtPixmapRuntime::getClassRef())) {
        QVariant* originalVariant = static_cast<QVariant*>(JSObjectGetPrivate(obj));
        if (hint == qMetaTypeId<QPixmap>())
            return QVariant::fromValue<QPixmap>(toPixmap(*originalVariant));

        if (hint == qMetaTypeId<QImage>())
            return QVariant::fromValue<QImage>(toImage(*originalVariant));
    }

    JSObject* jsObject = ::toJS(obj);
    if (!jsObject->inherits(&JSHTMLImageElement::s_info))
        return emptyVariantForHint(hint);

    JSHTMLImageElement* elementJSWrapper = static_cast<JSHTMLImageElement*>(jsObject);
    HTMLImageElement* imageElement = static_cast<HTMLImageElement*>(elementJSWrapper->impl());

    if (!imageElement)
        return emptyVariantForHint(hint);

    CachedImage* cachedImage = imageElement->cachedImage();
    if (!cachedImage)
        return emptyVariantForHint(hint);

    Image* image = cachedImage->imageForRenderer(imageElement->renderer());
    if (!image)
        return emptyVariantForHint(hint);

    QImage* nativeImage = image->nativeImageForCurrentFrame();
    if (!nativeImage)
        return emptyVariantForHint(hint);

    return (hint == static_cast<QMetaType::Type>(qMetaTypeId<QPixmap>()))
              ? QVariant::fromValue<QPixmap>(QPixmap::fromImage(*nativeImage))
              : QVariant::fromValue<QImage>(*nativeImage);
}