From 46d7136718292aa7ff4e6ac9e32018c13e493d22 Mon Sep 17 00:00:00 2001 From: "a.yankovich" Date: Tue, 29 Sep 2020 18:44:22 +0300 Subject: [PATCH] change AsyncImageResponse --- .../Front/src/correnthostimageprovider.cpp | 90 +++++++++++++++---- .../Front/src/correnthostimageprovider.h | 25 ++++-- 2 files changed, 93 insertions(+), 22 deletions(-) diff --git a/Site/BaseFront/Front/src/correnthostimageprovider.cpp b/Site/BaseFront/Front/src/correnthostimageprovider.cpp index ff5bab6..c605b0f 100644 --- a/Site/BaseFront/Front/src/correnthostimageprovider.cpp +++ b/Site/BaseFront/Front/src/correnthostimageprovider.cpp @@ -1,9 +1,22 @@ -#include "correnthostimageprovider.h" -#include -#include +//# +//# 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 + +#ifdef Q_OS_WASM #include +#include +#else +#include +#include +#include #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(tmpPTR); - resp->m_image = QImage::fromData(reinterpret_cast(fetch->data), fetch->numBytes); + + +#ifdef Q_OS_WASM + response = this; + auto downloadSucceeded = [](emscripten_fetch_t *fetch) { + auto resp = static_cast(response); + resp->m_image = QImage::fromData(reinterpret_cast(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(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 - - } } diff --git a/Site/BaseFront/Front/src/correnthostimageprovider.h b/Site/BaseFront/Front/src/correnthostimageprovider.h index 1b7cab3..f943b80 100644 --- a/Site/BaseFront/Front/src/correnthostimageprovider.h +++ b/Site/BaseFront/Front/src/correnthostimageprovider.h @@ -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 #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;