change AsyncImageResponse

This commit is contained in:
Andrei Yankovich 2020-09-29 18:44:22 +03:00
parent adc41cc058
commit 46d7136718
2 changed files with 93 additions and 22 deletions

View File

@ -1,9 +1,22 @@
#include "correnthostimageprovider.h"
#include <string>
#include <functional>
//#
//# Copyright (C) 2020-2020 QuasarApp.
//# Distributed under the lgplv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#ifdef WASM32_BUILD
#include "correnthostimageprovider.h"
#include <quasarapp.h>
#ifdef Q_OS_WASM
#include <emscripten/fetch.h>
#include <string>
#else
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#endif
namespace BaseFront {
@ -25,24 +38,38 @@ QQuickImageResponse *CorrentHostImageProvider::requestImageResponse(
}
AsyncImageResponse::AsyncImageResponse(const QString &id, const QSize &requestedSize)
: m_id(id), m_requestedSize(requestedSize) {
:
#ifndef Q_OS_WASM
m_manager(new QNetworkAccessManager),
#endif
m_id(id),
m_requestedSize(requestedSize) {
setAutoDelete(false);
}
AsyncImageResponse::~AsyncImageResponse() {
#ifndef Q_OS_WASM
delete m_manager;
#endif
}
QQuickTextureFactory *AsyncImageResponse::textureFactory() const {
return QQuickTextureFactory::textureFactoryForImage(m_image);
}
#ifdef WASM32_BUILD
static void * tmpPTR = nullptr;
#ifdef Q_OS_WASM
static void *response = nullptr;
#endif
void AsyncImageResponse::run() {
#ifdef WASM32_BUILD
tmpPTR = this;
auto downloadSucceeded = [](emscripten_fetch_t *fetch){
auto resp = reinterpret_cast<AsyncImageResponse*>(tmpPTR);
resp->m_image = QImage::fromData(reinterpret_cast<const unsigned char *>(fetch->data), fetch->numBytes);
#ifdef Q_OS_WASM
response = this;
auto downloadSucceeded = [](emscripten_fetch_t *fetch) {
auto resp = static_cast<AsyncImageResponse*>(response);
resp->m_image = QImage::fromData(reinterpret_cast<const unsigned char*>(fetch->data),
fetch->numBytes);
if (resp->m_requestedSize.isValid())
resp->m_image = resp->m_image.scaled(resp->m_requestedSize);
@ -52,8 +79,16 @@ void AsyncImageResponse::run() {
emscripten_fetch_close(fetch); // Free data associated with the fetch.
};
auto downloadFailed = [](emscripten_fetch_t *fetch){
auto downloadFailed = [](emscripten_fetch_t *fetch) {
QuasarAppUtils::Params::log(QString("Downloading %0 failed, HTTP failure status code: %1.\n").
arg(fetch->url).arg(fetch->status),
QuasarAppUtils::Error);
emscripten_fetch_close(fetch); // Also free data on failure.
auto resp = static_cast<AsyncImageResponse*>(response);
resp->cancel();
};
emscripten_fetch_attr_t attr;
@ -62,14 +97,37 @@ void AsyncImageResponse::run() {
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
attr.onsuccess = downloadSucceeded;
attr.onerror = downloadFailed;
auto stdString = m_id.toStdString();
emscripten_fetch(&attr, stdString.c_str());
#else
cancel();
QNetworkRequest request;
m_manager->get(request);
auto handleRequest= [this](QNetworkReply *reply) {
if (reply->error() != QNetworkReply::NoError) {
QuasarAppUtils::Params::log(reply->errorString(),
QuasarAppUtils::Error);
cancel();
}
auto data = reply->readAll();
m_image = QImage::fromData(data);
if (m_requestedSize.isValid())
m_image = m_image.scaled(m_requestedSize);
emit finished();
};
connect(m_manager, &QNetworkAccessManager::finished, handleRequest);
#endif
}
}

View File

@ -1,3 +1,11 @@
//#
//# Copyright (C) 2020-2020 QuasarApp.
//# Distributed under the lgplv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#ifndef CORRENTHOSTIMAGEPROVIDER_H
#define CORRENTHOSTIMAGEPROVIDER_H
@ -5,6 +13,7 @@
#include <QRunnable>
#include "BaseFront_global.h"
class QNetworkAccessManager;
namespace BaseFront {
@ -12,21 +21,25 @@ class BASEFRONT_LIBRARYSHARED_EXPORT AsyncImageResponse : public QQuickImageResp
{
public:
AsyncImageResponse(const QString &id, const QSize &requestedSize);
~AsyncImageResponse() override;
QQuickTextureFactory *textureFactory() const override;
QQuickTextureFactory *textureFactory() const;
void run();
void run() override;
#ifndef Q_OS_WASM
private:
QNetworkAccessManager *m_manager = nullptr;
#endif
QString m_id;
QSize m_requestedSize;
QImage m_image;
QSize m_requestedSize;
};
class BASEFRONT_LIBRARYSHARED_EXPORT CorrentHostImageProvider: public QQuickAsyncImageProvider
{
public:
CorrentHostImageProvider();
~CorrentHostImageProvider();
~CorrentHostImageProvider() override;
QQuickImageResponse *requestImageResponse(const QString &id, const QSize &requestedSize) override;