old render cleanup part 1
This commit is contained in:
38 files changed
+166
-1366
No files matched your search
@@ -11,7 +11,6 @@
|
||||
|
||||
QImage EntryImageProvider::loadImageFromSource(IByteSource &source,
|
||||
const QString &entryName,
|
||||
int displayWidth,
|
||||
const QSize &requestedSize)
|
||||
{
|
||||
if (entryName.isEmpty())
|
||||
@@ -25,7 +24,7 @@ QImage EntryImageProvider::loadImageFromSource(IByteSource &source,
|
||||
if (!image.loadFromData(data))
|
||||
return {};
|
||||
|
||||
return scaleToFit(image, displayWidth, requestedSize);
|
||||
return scaleToFit(image, requestedSize);
|
||||
}
|
||||
|
||||
EntryImageProvider::EntryImageProvider(SourceFactory sourceFactory, QString scheme)
|
||||
@@ -38,11 +37,9 @@ QQuickImageResponse *EntryImageProvider::requestImageResponse(const QString &id,
|
||||
const QSize &requestedSize)
|
||||
{
|
||||
QString sourcePath;
|
||||
int displayWidth;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
sourcePath = m_sourcePath;
|
||||
displayWidth = m_displayWidth;
|
||||
sourcePath = m_sourcePath;
|
||||
}
|
||||
|
||||
// id may be "entry/name.jpg?src=<encoded-path>"
|
||||
@@ -52,50 +49,16 @@ QQuickImageResponse *EntryImageProvider::requestImageResponse(const QString &id,
|
||||
sourcePath = parsed.sourcePath;
|
||||
|
||||
auto *response = new AsyncImageResponse(
|
||||
[factory = m_sourceFactory, sourcePath, entryName, displayWidth, requestedSize]() -> QImage {
|
||||
[factory = m_sourceFactory, sourcePath, entryName, requestedSize]() -> QImage {
|
||||
auto source = factory();
|
||||
if (!source->open(sourcePath))
|
||||
return {};
|
||||
return loadImageFromSource(*source, entryName, displayWidth, requestedSize);
|
||||
return loadImageFromSource(*source, entryName, requestedSize);
|
||||
});
|
||||
QThreadPool::globalInstance()->start(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
QImage EntryImageProvider::load(const QUrl &url, int maxWidth) const
|
||||
{
|
||||
if (url.scheme() != QLatin1String("image"))
|
||||
return {};
|
||||
|
||||
const ImageUrl::Parsed parsed = ImageUrl::parseImageUrl(url);
|
||||
const QString entryName = parsed.entry;
|
||||
if (entryName.isEmpty())
|
||||
return {};
|
||||
|
||||
QString sourcePath = parsed.sourcePath;
|
||||
if (sourcePath.isEmpty()) {
|
||||
QMutexLocker lock(&m_mutex);
|
||||
sourcePath = m_sourcePath;
|
||||
}
|
||||
|
||||
auto source = m_sourceFactory();
|
||||
if (!source->open(sourcePath))
|
||||
return {};
|
||||
return loadImageFromSource(*source, entryName, maxWidth, {});
|
||||
}
|
||||
|
||||
std::unique_ptr<IImageLoader> EntryImageProvider::clone() const
|
||||
{
|
||||
QString sourcePath;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
sourcePath = m_sourcePath;
|
||||
}
|
||||
auto c = std::make_unique<EntryImageProvider>(m_sourceFactory, m_scheme);
|
||||
c->setFilePath(sourcePath);
|
||||
return c;
|
||||
}
|
||||
|
||||
void EntryImageProvider::setFilePath(const QString &filePath)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
@@ -114,15 +77,8 @@ QString EntryImageProvider::scheme() const
|
||||
return m_scheme;
|
||||
}
|
||||
|
||||
void EntryImageProvider::setDisplayWidth(int width)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_displayWidth = width;
|
||||
}
|
||||
|
||||
void EntryImageProvider::clear()
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_sourcePath.clear();
|
||||
m_displayWidth = 0;
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "IByteSource.h"
|
||||
#include "backend/IImageLoader.h"
|
||||
#include "backend/IImageProvider.h"
|
||||
|
||||
#include <functional>
|
||||
@@ -9,7 +8,7 @@
|
||||
#include <QMutex>
|
||||
#include <QString>
|
||||
|
||||
class EntryImageProvider : public IImageProvider, public IImageLoader {
|
||||
class EntryImageProvider : public IImageProvider {
|
||||
public:
|
||||
using SourceFactory = std::function<std::unique_ptr<IByteSource>()>;
|
||||
|
||||
@@ -19,17 +18,12 @@ public:
|
||||
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 loadImageFromSource(IByteSource &source,
|
||||
const QString &entryName,
|
||||
int displayWidth,
|
||||
const QSize &requestedSize);
|
||||
|
||||
private:
|
||||
@@ -37,5 +31,4 @@ private:
|
||||
QString m_scheme;
|
||||
mutable QMutex m_mutex;
|
||||
QString m_sourcePath;
|
||||
int m_displayWidth = 0;
|
||||
};
|
||||
@@ -7,36 +7,6 @@
|
||||
#include <QFileInfo>
|
||||
#include <QQmlEngine>
|
||||
|
||||
// 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;
|
||||
};
|
||||
|
||||
FormatRegistry::FormatRegistry(QVector<FormatSpec> specs)
|
||||
: m_specs(std::move(specs))
|
||||
{
|
||||
@@ -69,24 +39,6 @@ IImageProvider *FormatRegistry::createImageProvider(const QString &filePath) con
|
||||
return new EntryImageProvider(spec->makeSource, spec->scheme);
|
||||
}
|
||||
|
||||
std::unique_ptr<IImageLoader> FormatRegistry::createImageLoader(const QString &filePath) const
|
||||
{
|
||||
const FormatSpec *spec = specFor(filePath);
|
||||
if (!spec || spec->scheme.isEmpty() || !spec->makeSource)
|
||||
return nullptr;
|
||||
|
||||
auto loader = std::make_unique<EntryImageProvider>(spec->makeSource, spec->scheme);
|
||||
loader->setFilePath(filePath);
|
||||
|
||||
if (!spec->loaderIncludesWeb)
|
||||
return loader;
|
||||
|
||||
auto composite = std::make_unique<CompositeImageLoader>();
|
||||
composite->add(spec->scheme, std::move(loader));
|
||||
composite->add(QStringLiteral("web"), std::make_unique<WebImageProvider>());
|
||||
return composite;
|
||||
}
|
||||
|
||||
QStringList FormatRegistry::supportedExtensions() const
|
||||
{
|
||||
QStringList out;
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
|
||||
class FormatReader;
|
||||
class IByteSource;
|
||||
class IImageLoader;
|
||||
class IImageProvider;
|
||||
class QQmlEngine;
|
||||
|
||||
@@ -21,7 +20,6 @@ struct FormatSpec {
|
||||
QString scheme; // image:// scheme; empty = format has no images
|
||||
std::function<std::unique_ptr<FormatReader>()> makeReader;
|
||||
std::function<std::unique_ptr<IByteSource>()> makeSource; // required when scheme is set
|
||||
bool loaderIncludesWeb = false; // formats whose content may reference web images (epub)
|
||||
};
|
||||
|
||||
class FormatRegistry {
|
||||
@@ -34,9 +32,6 @@ public:
|
||||
// Caller owns the pointer (the QML engine takes ownership on registration).
|
||||
IImageProvider *createImageProvider(const QString &filePath) const;
|
||||
|
||||
// Synchronous loader for measurement, or nullptr for formats without images.
|
||||
std::unique_ptr<IImageLoader> createImageLoader(const QString &filePath) const;
|
||||
|
||||
QStringList supportedExtensions() const;
|
||||
|
||||
// Register one provider per scheme with the QML engine (engine takes ownership).
|
||||
|
||||
@@ -3,17 +3,13 @@
|
||||
#include <QImage>
|
||||
#include <QSize>
|
||||
|
||||
// Honor an explicit requested size (clamped to the display width), otherwise
|
||||
// scale down to fit the display width — never enlarge.
|
||||
inline QImage scaleToFit(QImage image, int displayWidth, const QSize &requestedSize)
|
||||
// Honor an explicit requested size, otherwise keep original image dimensions — never enlarge.
|
||||
inline QImage scaleToFit(QImage image, 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);
|
||||
@@ -22,9 +18,6 @@ inline QImage scaleToFit(QImage image, int displayWidth, const QSize &requestedS
|
||||
} else {
|
||||
image = image.scaledToHeight(targetH, Qt::SmoothTransformation);
|
||||
}
|
||||
} else {
|
||||
if (displayWidth > 0 && image.width() > displayWidth)
|
||||
image = image.scaledToWidth(displayWidth, Qt::SmoothTransformation);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
@@ -39,7 +39,7 @@ QString WebImageProvider::cacheFilePath(const QString &urlString)
|
||||
// WebImageProvider
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
QImage WebImageProvider::loadWebImage(const QString &urlString, int displayWidth,
|
||||
QImage WebImageProvider::loadWebImage(const QString &urlString,
|
||||
const QSize &requestedSize)
|
||||
{
|
||||
if (urlString.isEmpty())
|
||||
@@ -50,7 +50,7 @@ QImage WebImageProvider::loadWebImage(const QString &urlString, int displayWidth
|
||||
if (QFile::exists(cachePath)) {
|
||||
QImage image;
|
||||
if (image.load(cachePath))
|
||||
return scaleToFit(image, displayWidth, requestedSize);
|
||||
return scaleToFit(image, requestedSize);
|
||||
}
|
||||
|
||||
// Download — blocking, safe to call from any thread via a local event loop
|
||||
@@ -89,7 +89,7 @@ QImage WebImageProvider::loadWebImage(const QString &urlString, int displayWidth
|
||||
if (!image.loadFromData(data))
|
||||
return {};
|
||||
|
||||
return scaleToFit(image, displayWidth, requestedSize);
|
||||
return scaleToFit(image, requestedSize);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -109,13 +109,8 @@ QQuickImageResponse *WebImageProvider::requestImageResponse(const QString &id,
|
||||
return response;
|
||||
}
|
||||
|
||||
int displayWidth;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
displayWidth = m_displayWidth;
|
||||
}
|
||||
auto *response = new AsyncImageResponse([id, displayWidth, requestedSize] {
|
||||
return loadWebImage(id, displayWidth, requestedSize);
|
||||
auto *response = new AsyncImageResponse([id, requestedSize] {
|
||||
return loadWebImage(id, requestedSize);
|
||||
});
|
||||
QThreadPool::globalInstance()->start(response);
|
||||
return response;
|
||||
@@ -125,42 +120,3 @@ 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;
|
||||
}
|
||||
@@ -1,34 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <QMutex>
|
||||
#include <QSize>
|
||||
#include <QString>
|
||||
|
||||
#include "backend/IImageLoader.h"
|
||||
#include "backend/IImageProvider.h"
|
||||
|
||||
class WebImageProvider : public IImageProvider, public IImageLoader {
|
||||
class WebImageProvider : public IImageProvider {
|
||||
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,
|
||||
static QImage loadWebImage(const QString &urlString,
|
||||
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