Web image support
This commit is contained in:
22 files changed
+370
-48
No files matched your search
@@ -0,0 +1,174 @@
|
||||
#include "EpubImageProvider.h"
|
||||
|
||||
#include <QImage>
|
||||
#include <QMutexLocker>
|
||||
#include <QQuickTextureFactory>
|
||||
#include <QThreadPool>
|
||||
|
||||
#include <minizip/unzip.h>
|
||||
|
||||
#include "backend/ZipUtils.h"
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// EpubImageResponse
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
EpubImageResponse::EpubImageResponse(const QString &epubPath, const QString &id,
|
||||
int displayWidth, const QSize &requestedSize)
|
||||
: m_epubPath(epubPath)
|
||||
, m_id(id)
|
||||
, m_displayWidth(displayWidth)
|
||||
, m_requestedSize(requestedSize)
|
||||
{
|
||||
setAutoDelete(false);
|
||||
}
|
||||
|
||||
QQuickTextureFactory *EpubImageResponse::textureFactory() const
|
||||
{
|
||||
if (m_image.isNull())
|
||||
return nullptr;
|
||||
return QQuickTextureFactory::textureFactoryForImage(m_image);
|
||||
}
|
||||
|
||||
void EpubImageResponse::cancel()
|
||||
{
|
||||
m_cancelled.storeRelaxed(1);
|
||||
}
|
||||
|
||||
void EpubImageResponse::run()
|
||||
{
|
||||
if (m_cancelled.loadRelaxed())
|
||||
return;
|
||||
|
||||
m_image = EpubImageProvider::loadImageFromEpub(
|
||||
m_epubPath, m_id, m_displayWidth, m_requestedSize);
|
||||
|
||||
if (m_cancelled.loadRelaxed())
|
||||
return;
|
||||
|
||||
emit finished();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// EpubImageProvider
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
QQuickImageResponse *EpubImageProvider::requestImageResponse(const QString &id,
|
||||
const QSize &requestedSize)
|
||||
{
|
||||
QString epubPath;
|
||||
int displayWidth;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
epubPath = m_epubPath;
|
||||
displayWidth = m_displayWidth;
|
||||
}
|
||||
|
||||
auto *response = new EpubImageResponse(epubPath, id, displayWidth, requestedSize);
|
||||
QThreadPool::globalInstance()->start(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
QImage EpubImageProvider::loadImageFromEpub(const QString &epubPath, const QString &id,
|
||||
int displayWidth, const QSize &requestedSize)
|
||||
{
|
||||
if (epubPath.isEmpty())
|
||||
return {};
|
||||
|
||||
unzFile zf = unzOpen(epubPath.toUtf8().constData());
|
||||
if (!zf)
|
||||
return {};
|
||||
|
||||
const QByteArray data = ZipUtils::readZipEntry(zf, id.toUtf8().constData());
|
||||
unzClose(zf);
|
||||
|
||||
if (data.isEmpty())
|
||||
return {};
|
||||
|
||||
QImage image;
|
||||
if (!image.loadFromData(data))
|
||||
return {};
|
||||
|
||||
// Sizing logic: honor EPUB explicit dimensions, scale down to fit display width otherwise
|
||||
if (requestedSize.width() > 0 || requestedSize.height() > 0) {
|
||||
int targetW = requestedSize.width() > 0 ? requestedSize.width() : -1;
|
||||
int targetH = requestedSize.height() > 0 ? requestedSize.height() : -1;
|
||||
|
||||
// Clamp to display width
|
||||
if (displayWidth > 0 && targetW > 0 && targetW > displayWidth)
|
||||
targetW = displayWidth;
|
||||
|
||||
if (targetW > 0 && targetH > 0) {
|
||||
image = image.scaled(QSize(targetW, targetH), Qt::KeepAspectRatio,
|
||||
Qt::SmoothTransformation);
|
||||
} else if (targetW > 0) {
|
||||
image = image.scaledToWidth(targetW, Qt::SmoothTransformation);
|
||||
} else {
|
||||
image = image.scaledToHeight(targetH, Qt::SmoothTransformation);
|
||||
}
|
||||
} else {
|
||||
// No EPUB hints: scale down if wider than display, never enlarge
|
||||
if (displayWidth > 0 && image.width() > displayWidth)
|
||||
image = image.scaledToWidth(displayWidth, Qt::SmoothTransformation);
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
QImage EpubImageProvider::load(const QUrl &url, int maxWidth) const
|
||||
{
|
||||
if (url.scheme() != QLatin1String("image"))
|
||||
return {};
|
||||
QString zipPath = url.path();
|
||||
if (zipPath.startsWith(QLatin1Char('/')))
|
||||
zipPath = zipPath.mid(1);
|
||||
if (zipPath.isEmpty())
|
||||
return {};
|
||||
QString epubPath;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
epubPath = m_epubPath;
|
||||
}
|
||||
return loadImageFromEpub(epubPath, zipPath, maxWidth, {});
|
||||
}
|
||||
|
||||
std::unique_ptr<IImageLoader> EpubImageProvider::clone() const
|
||||
{
|
||||
QString epubPath;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
epubPath = m_epubPath;
|
||||
}
|
||||
auto c = std::make_unique<EpubImageProvider>();
|
||||
c->setFilePath(epubPath);
|
||||
return c;
|
||||
}
|
||||
|
||||
void EpubImageProvider::setFilePath(const QString &filePath)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_epubPath = filePath;
|
||||
}
|
||||
|
||||
QString EpubImageProvider::coverImageUrl(const QString &coverPath) const
|
||||
{
|
||||
return QStringLiteral("image://epub/") + coverPath;
|
||||
}
|
||||
|
||||
QString EpubImageProvider::scheme() const
|
||||
{
|
||||
return QStringLiteral("epub");
|
||||
}
|
||||
|
||||
void EpubImageProvider::setDisplayWidth(int width)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_displayWidth = width;
|
||||
}
|
||||
|
||||
void EpubImageProvider::clear()
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_epubPath.clear();
|
||||
m_displayWidth = 0;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <QMutex>
|
||||
#include <QRunnable>
|
||||
#include <QString>
|
||||
|
||||
#include "backend/IImageLoader.h"
|
||||
#include "backend/IImageProvider.h"
|
||||
|
||||
class EpubImageResponse : public QQuickImageResponse, public QRunnable {
|
||||
// NO Q_OBJECT — already inherited from QQuickImageResponse
|
||||
public:
|
||||
EpubImageResponse(const QString &epubPath, const QString &id,
|
||||
int displayWidth, const QSize &requestedSize);
|
||||
|
||||
QQuickTextureFactory *textureFactory() const override;
|
||||
void cancel() override;
|
||||
void run() override;
|
||||
|
||||
private:
|
||||
const QString m_epubPath;
|
||||
const QString m_id;
|
||||
const int m_displayWidth;
|
||||
const QSize m_requestedSize;
|
||||
QAtomicInt m_cancelled{0};
|
||||
QImage m_image;
|
||||
};
|
||||
|
||||
class EpubImageProvider : public IImageProvider, public IImageLoader {
|
||||
public:
|
||||
QQuickImageResponse *requestImageResponse(const QString &id,
|
||||
const QSize &requestedSize) override;
|
||||
|
||||
void setFilePath(const QString &filePath) override;
|
||||
void setDisplayWidth(int width) override;
|
||||
void clear() override;
|
||||
QString coverImageUrl(const QString &coverPath) const override;
|
||||
QString scheme() const override;
|
||||
|
||||
QImage load(const QUrl &url, int maxWidth) const override;
|
||||
std::unique_ptr<IImageLoader> clone() const override;
|
||||
|
||||
static QImage loadImageFromEpub(const QString &epubPath, const QString &id,
|
||||
int displayWidth, const QSize &requestedSize);
|
||||
|
||||
private:
|
||||
mutable QMutex m_mutex;
|
||||
QString m_epubPath;
|
||||
int m_displayWidth = 0;
|
||||
};
|
||||
@@ -285,9 +285,13 @@ QStringList EpubReader::chunkXhtml(const QByteArray &data, const QString &xhtmlH
|
||||
if (src.isEmpty() || src.startsWith(QStringLiteral("image://")))
|
||||
continue;
|
||||
src = QUrl::fromPercentEncoding(src.toUtf8());
|
||||
const QString resolved = QDir::cleanPath(xhtmlDir + src);
|
||||
img.setAttribute(QStringLiteral("src"),
|
||||
QStringLiteral("image://epub/") + resolved);
|
||||
if (src.startsWith(QLatin1String("http://")) || src.startsWith(QLatin1String("https://"))) {
|
||||
img.setAttribute(QStringLiteral("src"), QStringLiteral("image://web/") + src);
|
||||
} else {
|
||||
const QString resolved = QDir::cleanPath(xhtmlDir + src);
|
||||
img.setAttribute(QStringLiteral("src"),
|
||||
QStringLiteral("image://epub/") + resolved);
|
||||
}
|
||||
}
|
||||
|
||||
// Find <body> element (may have namespace prefix in some EPUBs)
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
#include "ImageLoaderFactory.h"
|
||||
#include "EpubImageProvider.h"
|
||||
#include "WebImageProvider.h"
|
||||
#include <QFileInfo>
|
||||
|
||||
// Dispatches load() calls to the right child loader based on the image:// host (scheme name).
|
||||
class CompositeImageLoader : public IImageLoader {
|
||||
public:
|
||||
void add(const QString &host, std::unique_ptr<IImageLoader> loader)
|
||||
{
|
||||
m_loaders.emplace_back(host, std::move(loader));
|
||||
}
|
||||
|
||||
QImage load(const QUrl &url, int maxWidth) const override
|
||||
{
|
||||
const QString host = url.host();
|
||||
for (const auto &[h, loader] : m_loaders) {
|
||||
if (h == host)
|
||||
return loader->load(url, maxWidth);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::unique_ptr<IImageLoader> clone() const override
|
||||
{
|
||||
auto c = std::make_unique<CompositeImageLoader>();
|
||||
for (const auto &[host, loader] : m_loaders)
|
||||
c->add(host, loader->clone());
|
||||
return c;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::pair<QString, std::unique_ptr<IImageLoader>>> m_loaders;
|
||||
};
|
||||
|
||||
std::unique_ptr<IImageLoader> ImageLoaderFactory::create(const QString &filePath)
|
||||
{
|
||||
const QString ext = QFileInfo(filePath).suffix().toLower();
|
||||
if (ext == QLatin1String("epub")) {
|
||||
auto composite = std::make_unique<CompositeImageLoader>();
|
||||
|
||||
auto epubLoader = std::make_unique<EpubImageProvider>();
|
||||
epubLoader->setFilePath(filePath);
|
||||
composite->add(QStringLiteral("epub"), std::move(epubLoader));
|
||||
|
||||
composite->add(QStringLiteral("web"), std::make_unique<WebImageProvider>());
|
||||
|
||||
return composite;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <QString>
|
||||
|
||||
class IImageLoader;
|
||||
|
||||
class ImageLoaderFactory {
|
||||
public:
|
||||
// Returns a new loader for the given file format, or nullptr for formats without images.
|
||||
static std::unique_ptr<IImageLoader> create(const QString &filePath);
|
||||
};
|
||||
@@ -0,0 +1,11 @@
|
||||
#include "ImageProviderFactory.h"
|
||||
#include "EpubImageProvider.h"
|
||||
#include <QFileInfo>
|
||||
|
||||
IImageProvider *ImageProviderFactory::create(const QString &filePath)
|
||||
{
|
||||
const QString ext = QFileInfo(filePath).suffix().toLower();
|
||||
if (ext == QLatin1String("epub"))
|
||||
return new EpubImageProvider();
|
||||
return nullptr;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include <QString>
|
||||
|
||||
class IImageProvider;
|
||||
|
||||
class ImageProviderFactory {
|
||||
public:
|
||||
// Returns a new provider for the given file format, or nullptr for formats without images.
|
||||
// Caller is responsible for the returned pointer's lifetime.
|
||||
static IImageProvider *create(const QString &filePath);
|
||||
};
|
||||
@@ -0,0 +1,26 @@
|
||||
#include "ReaderFactory.h"
|
||||
#include "backend/FormatReader.h"
|
||||
|
||||
#include <QFileInfo>
|
||||
|
||||
ReaderFactory::ReaderFactory(QList<CreatorFn> creators)
|
||||
{
|
||||
for (const auto &creator : creators) {
|
||||
auto probe = creator();
|
||||
m_registry[probe->fileType()] = creator;
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<FormatReader> ReaderFactory::create(const QString &filePath) const
|
||||
{
|
||||
const QString ext = QFileInfo(filePath).suffix().toLower();
|
||||
auto it = m_registry.find(ext);
|
||||
if (it == m_registry.end())
|
||||
return nullptr;
|
||||
return it.value()();
|
||||
}
|
||||
|
||||
QStringList ReaderFactory::supportedExtensions() const
|
||||
{
|
||||
return m_registry.keys();
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef READERFACTORY_H
|
||||
#define READERFACTORY_H
|
||||
|
||||
#include <QMap>
|
||||
#include <QStringList>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
class FormatReader;
|
||||
|
||||
class ReaderFactory {
|
||||
public:
|
||||
using CreatorFn = std::function<std::unique_ptr<FormatReader>()>;
|
||||
|
||||
// Constructs the factory by probing each creator via fileType() to build the registry.
|
||||
explicit ReaderFactory(QList<CreatorFn> creators);
|
||||
|
||||
std::unique_ptr<FormatReader> create(const QString &filePath) const;
|
||||
QStringList supportedExtensions() const;
|
||||
|
||||
private:
|
||||
QMap<QString, CreatorFn> m_registry; // extension -> creator
|
||||
};
|
||||
|
||||
#endif // READERFACTORY_H
|
||||
@@ -0,0 +1,214 @@
|
||||
#include "WebImageProvider.h"
|
||||
|
||||
#include <QCryptographicHash>
|
||||
#include <QDir>
|
||||
#include <QEventLoop>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QImage>
|
||||
#include <QMutexLocker>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
#include <QQuickTextureFactory>
|
||||
#include <QStandardPaths>
|
||||
#include <QThreadPool>
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
QString WebImageProvider::cacheDir()
|
||||
{
|
||||
return QStandardPaths::writableLocation(QStandardPaths::CacheLocation)
|
||||
+ QStringLiteral("/web-images");
|
||||
}
|
||||
|
||||
QString WebImageProvider::cacheFilePath(const QString &urlString)
|
||||
{
|
||||
const QByteArray hash =
|
||||
QCryptographicHash::hash(urlString.toUtf8(), QCryptographicHash::Sha256).toHex();
|
||||
const QString ext = QFileInfo(QUrl(urlString).path()).suffix();
|
||||
return cacheDir() + QLatin1Char('/') + QString::fromLatin1(hash)
|
||||
+ (ext.isEmpty() ? QString() : QLatin1Char('.') + ext);
|
||||
}
|
||||
|
||||
static QImage scaleToFit(QImage image, int displayWidth, const QSize &requestedSize)
|
||||
{
|
||||
if (requestedSize.width() > 0 || requestedSize.height() > 0) {
|
||||
int targetW = requestedSize.width() > 0 ? requestedSize.width() : -1;
|
||||
int targetH = requestedSize.height() > 0 ? requestedSize.height() : -1;
|
||||
|
||||
if (displayWidth > 0 && targetW > 0 && targetW > displayWidth)
|
||||
targetW = displayWidth;
|
||||
|
||||
if (targetW > 0 && targetH > 0) {
|
||||
image = image.scaled(QSize(targetW, targetH), Qt::KeepAspectRatio,
|
||||
Qt::SmoothTransformation);
|
||||
} else if (targetW > 0) {
|
||||
image = image.scaledToWidth(targetW, Qt::SmoothTransformation);
|
||||
} else {
|
||||
image = image.scaledToHeight(targetH, Qt::SmoothTransformation);
|
||||
}
|
||||
} else {
|
||||
if (displayWidth > 0 && image.width() > displayWidth)
|
||||
image = image.scaledToWidth(displayWidth, Qt::SmoothTransformation);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// WebImageProvider
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
QImage WebImageProvider::loadWebImage(const QString &urlString, int displayWidth,
|
||||
const QSize &requestedSize)
|
||||
{
|
||||
if (urlString.isEmpty())
|
||||
return {};
|
||||
|
||||
// Check cache first
|
||||
const QString cachePath = cacheFilePath(urlString);
|
||||
if (QFile::exists(cachePath)) {
|
||||
QImage image;
|
||||
if (image.load(cachePath))
|
||||
return scaleToFit(image, displayWidth, requestedSize);
|
||||
}
|
||||
|
||||
// Download — blocking, safe to call from any thread via a local event loop
|
||||
QNetworkAccessManager nam;
|
||||
QNetworkRequest request{QUrl(urlString)};
|
||||
request.setHeader(QNetworkRequest::UserAgentHeader, QStringLiteral("Asteria/1.0"));
|
||||
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute,
|
||||
QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||
|
||||
QNetworkReply *reply = nam.get(request);
|
||||
|
||||
QEventLoop loop;
|
||||
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
||||
loop.exec();
|
||||
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
reply->deleteLater();
|
||||
return {};
|
||||
}
|
||||
|
||||
const QByteArray data = reply->readAll();
|
||||
reply->deleteLater();
|
||||
|
||||
if (data.isEmpty())
|
||||
return {};
|
||||
|
||||
// Persist to cache
|
||||
QDir().mkpath(cacheDir());
|
||||
QFile f(cachePath);
|
||||
if (f.open(QIODevice::WriteOnly)) {
|
||||
f.write(data);
|
||||
f.close();
|
||||
}
|
||||
|
||||
QImage image;
|
||||
if (!image.loadFromData(data))
|
||||
return {};
|
||||
|
||||
return scaleToFit(image, displayWidth, requestedSize);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// WebImageResponse
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
WebImageResponse::WebImageResponse(const QString &url, int displayWidth,
|
||||
const QSize &requestedSize)
|
||||
: m_url(url)
|
||||
, m_displayWidth(displayWidth)
|
||||
, m_requestedSize(requestedSize)
|
||||
{
|
||||
setAutoDelete(false);
|
||||
}
|
||||
|
||||
QQuickTextureFactory *WebImageResponse::textureFactory() const
|
||||
{
|
||||
if (m_image.isNull())
|
||||
return nullptr;
|
||||
return QQuickTextureFactory::textureFactoryForImage(m_image);
|
||||
}
|
||||
|
||||
void WebImageResponse::cancel()
|
||||
{
|
||||
m_cancelled.storeRelaxed(1);
|
||||
}
|
||||
|
||||
void WebImageResponse::run()
|
||||
{
|
||||
if (m_cancelled.loadRelaxed())
|
||||
return;
|
||||
|
||||
m_image = WebImageProvider::loadWebImage(m_url, m_displayWidth, m_requestedSize);
|
||||
|
||||
if (m_cancelled.loadRelaxed())
|
||||
return;
|
||||
|
||||
emit finished();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// WebImageProvider — IImageProvider
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
QQuickImageResponse *WebImageProvider::requestImageResponse(const QString &id,
|
||||
const QSize &requestedSize)
|
||||
{
|
||||
int displayWidth;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
displayWidth = m_displayWidth;
|
||||
}
|
||||
auto *response = new WebImageResponse(id, displayWidth, requestedSize);
|
||||
QThreadPool::globalInstance()->start(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
QString WebImageProvider::scheme() const
|
||||
{
|
||||
return QStringLiteral("web");
|
||||
}
|
||||
|
||||
void WebImageProvider::setDisplayWidth(int width)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_displayWidth = width;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// WebImageProvider — IImageLoader
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
QImage WebImageProvider::load(const QUrl &url, int maxWidth) const
|
||||
{
|
||||
// url is image://web/<original-web-url>
|
||||
// url.path() yields "/<original-web-url>"; strip leading '/'
|
||||
QString webUrl = url.path();
|
||||
if (webUrl.startsWith(QLatin1Char('/')))
|
||||
webUrl = webUrl.mid(1);
|
||||
if (webUrl.isEmpty())
|
||||
return {};
|
||||
|
||||
int displayWidth;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
displayWidth = m_displayWidth;
|
||||
}
|
||||
const int effective = (displayWidth > 0 && maxWidth > 0)
|
||||
? qMin(displayWidth, maxWidth)
|
||||
: qMax(displayWidth, maxWidth);
|
||||
return loadWebImage(webUrl, effective, {});
|
||||
}
|
||||
|
||||
std::unique_ptr<IImageLoader> WebImageProvider::clone() const
|
||||
{
|
||||
auto c = std::make_unique<WebImageProvider>();
|
||||
QMutexLocker lock(&m_mutex);
|
||||
c->m_displayWidth = m_displayWidth;
|
||||
return c;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include <QAtomicInt>
|
||||
#include <QMutex>
|
||||
#include <QRunnable>
|
||||
#include <QSize>
|
||||
#include <QString>
|
||||
|
||||
#include "backend/IImageLoader.h"
|
||||
#include "backend/IImageProvider.h"
|
||||
|
||||
class WebImageResponse : public QQuickImageResponse, public QRunnable {
|
||||
// NO Q_OBJECT — already inherited from QQuickImageResponse
|
||||
public:
|
||||
WebImageResponse(const QString &url, int displayWidth, const QSize &requestedSize);
|
||||
|
||||
QQuickTextureFactory *textureFactory() const override;
|
||||
void cancel() override;
|
||||
void run() override;
|
||||
|
||||
private:
|
||||
const QString m_url;
|
||||
const int m_displayWidth;
|
||||
const QSize m_requestedSize;
|
||||
QAtomicInt m_cancelled{0};
|
||||
QImage m_image;
|
||||
};
|
||||
|
||||
class WebImageProvider : public IImageProvider, public IImageLoader {
|
||||
public:
|
||||
QQuickImageResponse *requestImageResponse(const QString &id,
|
||||
const QSize &requestedSize) override;
|
||||
|
||||
void setFilePath(const QString &) override {}
|
||||
void setDisplayWidth(int width) override;
|
||||
void clear() override {}
|
||||
QString coverImageUrl(const QString &) const override { return {}; }
|
||||
QString scheme() const override;
|
||||
|
||||
QImage load(const QUrl &url, int maxWidth) const override;
|
||||
std::unique_ptr<IImageLoader> clone() const override;
|
||||
|
||||
// Download (or load from disk cache) a web image. Thread-safe, may block.
|
||||
static QImage loadWebImage(const QString &urlString, int displayWidth,
|
||||
const QSize &requestedSize);
|
||||
|
||||
static QString cacheDir();
|
||||
static QString cacheFilePath(const QString &urlString);
|
||||
|
||||
private:
|
||||
mutable QMutex m_mutex;
|
||||
int m_displayWidth = 0;
|
||||
};
|
||||
Reference in new issue
Block a user