Web image support
This commit is contained in:
22 files changed
+367
-45
No files matched your search
@@ -19,7 +19,13 @@
|
|||||||
"Bash(dpkg -l)",
|
"Bash(dpkg -l)",
|
||||||
"Bash(pkg-config:*)",
|
"Bash(pkg-config:*)",
|
||||||
"Bash(git checkout:*)",
|
"Bash(git checkout:*)",
|
||||||
"Bash(git:*)"
|
"Bash(git:*)",
|
||||||
|
"Bash(ls -lh /opt/qt/6.10.3/gcc_64/lib/libQt6WebView*.so* /opt/qt/6.10.3/gcc_64/lib/libQt6WebEngine*.so*)",
|
||||||
|
"Bash(xargs ls:*)",
|
||||||
|
"Bash(ls -lh /opt/qt/6.10.3/gcc_64/lib/*WebChannel*)",
|
||||||
|
"Bash(ls -1 /opt/qt/6.10.3/gcc_64/lib/libQt6*.so.6.10.3)",
|
||||||
|
"Bash(ls -lh /opt/qt/6.10.3/gcc_64/lib/libQt6*.so.6.10.3)",
|
||||||
|
"Bash(du:*)"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+23
-17
@@ -15,6 +15,7 @@ find_package(Qt6 REQUIRED COMPONENTS
|
|||||||
Quick
|
Quick
|
||||||
Widgets
|
Widgets
|
||||||
Concurrent
|
Concurrent
|
||||||
|
Network
|
||||||
Svg
|
Svg
|
||||||
QuickControls2
|
QuickControls2
|
||||||
Test
|
Test
|
||||||
@@ -31,8 +32,6 @@ pkg_check_modules(MINIZIP REQUIRED IMPORTED_TARGET minizip)
|
|||||||
# Source files
|
# Source files
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
src/backend/ReaderFactory.h
|
|
||||||
src/backend/ReaderFactory.cpp
|
|
||||||
src/backend/FormatReader.h
|
src/backend/FormatReader.h
|
||||||
src/backend/AppState.h
|
src/backend/AppState.h
|
||||||
src/backend/AppState.cpp
|
src/backend/AppState.cpp
|
||||||
@@ -51,14 +50,8 @@ set(SOURCES
|
|||||||
src/backend/book/Book.h
|
src/backend/book/Book.h
|
||||||
src/backend/book/Book.cpp
|
src/backend/book/Book.cpp
|
||||||
src/backend/IImageProvider.h
|
src/backend/IImageProvider.h
|
||||||
src/backend/EpubImageProvider.h
|
|
||||||
src/backend/EpubImageProvider.cpp
|
|
||||||
src/backend/ImageProviderFactory.h
|
|
||||||
src/backend/ImageProviderFactory.cpp
|
|
||||||
src/backend/ZipUtils.h
|
|
||||||
src/backend/IImageLoader.h
|
src/backend/IImageLoader.h
|
||||||
src/backend/ImageLoaderFactory.h
|
src/backend/ZipUtils.h
|
||||||
src/backend/ImageLoaderFactory.cpp
|
|
||||||
src/backend/ChunkMeasurer.h
|
src/backend/ChunkMeasurer.h
|
||||||
src/backend/ChunkMeasurer.cpp
|
src/backend/ChunkMeasurer.cpp
|
||||||
src/backend/IFileDialogService.h
|
src/backend/IFileDialogService.h
|
||||||
@@ -69,6 +62,16 @@ set(SOURCES
|
|||||||
src/backend/formats/TxtReader.cpp
|
src/backend/formats/TxtReader.cpp
|
||||||
src/backend/formats/EpubReader.h
|
src/backend/formats/EpubReader.h
|
||||||
src/backend/formats/EpubReader.cpp
|
src/backend/formats/EpubReader.cpp
|
||||||
|
src/backend/formats/EpubImageProvider.h
|
||||||
|
src/backend/formats/EpubImageProvider.cpp
|
||||||
|
src/backend/formats/WebImageProvider.h
|
||||||
|
src/backend/formats/WebImageProvider.cpp
|
||||||
|
src/backend/formats/ImageProviderFactory.h
|
||||||
|
src/backend/formats/ImageProviderFactory.cpp
|
||||||
|
src/backend/formats/ImageLoaderFactory.h
|
||||||
|
src/backend/formats/ImageLoaderFactory.cpp
|
||||||
|
src/backend/formats/ReaderFactory.h
|
||||||
|
src/backend/formats/ReaderFactory.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -96,6 +99,7 @@ target_link_libraries(asteria-qt PRIVATE
|
|||||||
Qt6::Quick
|
Qt6::Quick
|
||||||
Qt6::Widgets
|
Qt6::Widgets
|
||||||
Qt6::Concurrent
|
Qt6::Concurrent
|
||||||
|
Qt6::Network
|
||||||
Qt6::Svg
|
Qt6::Svg
|
||||||
Qt6::QuickControls2
|
Qt6::QuickControls2
|
||||||
)
|
)
|
||||||
@@ -204,29 +208,31 @@ add_asteria_test(EpubReaderTest tests/formats/EpubReaderTest.cpp
|
|||||||
target_link_libraries(EpubReaderTest PRIVATE PkgConfig::MINIZIP Qt6::Xml)
|
target_link_libraries(EpubReaderTest PRIVATE PkgConfig::MINIZIP Qt6::Xml)
|
||||||
|
|
||||||
add_asteria_test(ReaderFactoryTest tests/ReaderFactoryTest.cpp
|
add_asteria_test(ReaderFactoryTest tests/ReaderFactoryTest.cpp
|
||||||
src/backend/ReaderFactory.cpp
|
src/backend/formats/ReaderFactory.cpp
|
||||||
src/backend/formats/TxtReader.cpp
|
src/backend/formats/TxtReader.cpp
|
||||||
src/backend/formats/EpubReader.cpp)
|
src/backend/formats/EpubReader.cpp)
|
||||||
target_link_libraries(ReaderFactoryTest PRIVATE PkgConfig::MINIZIP Qt6::Xml)
|
target_link_libraries(ReaderFactoryTest PRIVATE PkgConfig::MINIZIP Qt6::Xml)
|
||||||
|
|
||||||
add_asteria_test(ChunkMeasurerTest tests/ChunkMeasurerTest.cpp
|
add_asteria_test(ChunkMeasurerTest tests/ChunkMeasurerTest.cpp
|
||||||
src/backend/ChunkMeasurer.cpp
|
src/backend/ChunkMeasurer.cpp
|
||||||
src/backend/EpubImageProvider.cpp)
|
src/backend/formats/EpubImageProvider.cpp
|
||||||
target_link_libraries(ChunkMeasurerTest PRIVATE Qt6::Gui Qt6::Concurrent Qt6::Quick PkgConfig::MINIZIP)
|
src/backend/formats/WebImageProvider.cpp)
|
||||||
|
target_link_libraries(ChunkMeasurerTest PRIVATE Qt6::Gui Qt6::Concurrent Qt6::Quick Qt6::Network PkgConfig::MINIZIP)
|
||||||
set_tests_properties(ChunkMeasurerTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
set_tests_properties(ChunkMeasurerTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
||||||
|
|
||||||
add_asteria_test(ReadingModelTest tests/ReadingModelTest.cpp
|
add_asteria_test(ReadingModelTest tests/ReadingModelTest.cpp
|
||||||
src/backend/ReadingModel.cpp
|
src/backend/ReadingModel.cpp
|
||||||
src/backend/ChunkMeasurer.cpp
|
src/backend/ChunkMeasurer.cpp
|
||||||
src/backend/EpubImageProvider.cpp
|
src/backend/formats/EpubImageProvider.cpp
|
||||||
|
src/backend/formats/WebImageProvider.cpp
|
||||||
src/backend/AppState.cpp
|
src/backend/AppState.cpp
|
||||||
src/backend/state/BookPositionStore.cpp
|
src/backend/state/BookPositionStore.cpp
|
||||||
src/backend/LibraryBookModel.cpp
|
src/backend/LibraryBookModel.cpp
|
||||||
src/backend/LibraryModel.cpp
|
src/backend/LibraryModel.cpp
|
||||||
src/backend/ReaderFactory.cpp
|
src/backend/formats/ReaderFactory.cpp
|
||||||
src/backend/formats/TxtReader.cpp
|
src/backend/formats/TxtReader.cpp
|
||||||
src/backend/formats/EpubReader.cpp)
|
src/backend/formats/EpubReader.cpp)
|
||||||
target_link_libraries(ReadingModelTest PRIVATE Qt6::Gui Qt6::Concurrent Qt6::Quick PkgConfig::MINIZIP Qt6::Xml)
|
target_link_libraries(ReadingModelTest PRIVATE Qt6::Gui Qt6::Concurrent Qt6::Quick Qt6::Network PkgConfig::MINIZIP Qt6::Xml)
|
||||||
set_tests_properties(ReadingModelTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
set_tests_properties(ReadingModelTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
||||||
|
|
||||||
add_asteria_test(AppStateTest tests/AppStateTest.cpp
|
add_asteria_test(AppStateTest tests/AppStateTest.cpp
|
||||||
@@ -250,7 +256,7 @@ add_asteria_test(BookTest tests/book/BookTest.cpp
|
|||||||
src/backend/book/Book.cpp
|
src/backend/book/Book.cpp
|
||||||
src/backend/AppState.cpp
|
src/backend/AppState.cpp
|
||||||
src/backend/state/BookPositionStore.cpp
|
src/backend/state/BookPositionStore.cpp
|
||||||
src/backend/ReaderFactory.cpp
|
src/backend/formats/ReaderFactory.cpp
|
||||||
src/backend/IFileDialogService.cpp
|
src/backend/IFileDialogService.cpp
|
||||||
src/backend/NativeFileDialogService.cpp
|
src/backend/NativeFileDialogService.cpp
|
||||||
src/backend/formats/TxtReader.cpp
|
src/backend/formats/TxtReader.cpp
|
||||||
@@ -259,7 +265,7 @@ target_link_libraries(BookTest PRIVATE Qt6::Concurrent Qt6::Widgets PkgConfig::M
|
|||||||
set_tests_properties(BookTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
set_tests_properties(BookTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
||||||
|
|
||||||
add_asteria_test(EpubImageProviderTest tests/EpubImageProviderTest.cpp
|
add_asteria_test(EpubImageProviderTest tests/EpubImageProviderTest.cpp
|
||||||
src/backend/EpubImageProvider.cpp)
|
src/backend/formats/EpubImageProvider.cpp)
|
||||||
target_link_libraries(EpubImageProviderTest PRIVATE PkgConfig::MINIZIP Qt6::Quick Qt6::Gui)
|
target_link_libraries(EpubImageProviderTest PRIVATE PkgConfig::MINIZIP Qt6::Quick Qt6::Gui)
|
||||||
set_tests_properties(EpubImageProviderTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
set_tests_properties(EpubImageProviderTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
||||||
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
#include "ImageLoaderFactory.h"
|
|
||||||
#include "EpubImageProvider.h"
|
|
||||||
#include <QFileInfo>
|
|
||||||
|
|
||||||
std::unique_ptr<IImageLoader> ImageLoaderFactory::create(const QString &filePath)
|
|
||||||
{
|
|
||||||
const QString ext = QFileInfo(filePath).suffix().toLower();
|
|
||||||
if (ext == QLatin1String("epub")) {
|
|
||||||
auto provider = std::make_unique<EpubImageProvider>();
|
|
||||||
provider->setFilePath(filePath);
|
|
||||||
return provider;
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#include "LibraryBookModel.h"
|
#include "LibraryBookModel.h"
|
||||||
#include "FormatReader.h"
|
#include "FormatReader.h"
|
||||||
#include "ReaderFactory.h"
|
#include "formats/ReaderFactory.h"
|
||||||
#include <QtConcurrent/QtConcurrent>
|
#include <QtConcurrent/QtConcurrent>
|
||||||
|
|
||||||
LibraryBookModel::LibraryBookModel(ReaderFactory &factory, QObject *parent)
|
LibraryBookModel::LibraryBookModel(ReaderFactory &factory, QObject *parent)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
#include <QPointer>
|
#include <QPointer>
|
||||||
#include <QFuture>
|
#include <QFuture>
|
||||||
|
|
||||||
#include "ReaderFactory.h"
|
#include "formats/ReaderFactory.h"
|
||||||
|
|
||||||
class FormatReader;
|
class FormatReader;
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include <QList>
|
#include <QList>
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
|
|
||||||
#include "ReaderFactory.h"
|
#include "formats/ReaderFactory.h"
|
||||||
|
|
||||||
class LibraryBookModel;
|
class LibraryBookModel;
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
#include "backend/FormatReader.h"
|
#include "backend/FormatReader.h"
|
||||||
#include "backend/IFileDialogService.h"
|
#include "backend/IFileDialogService.h"
|
||||||
#include "backend/ReaderFactory.h"
|
#include "backend/formats/ReaderFactory.h"
|
||||||
#include "backend/state/BookPositionStore.h"
|
#include "backend/state/BookPositionStore.h"
|
||||||
|
|
||||||
class Book : public QObject {
|
class Book : public QObject {
|
||||||
|
|||||||
File renamed without changes.
@@ -4,8 +4,8 @@
|
|||||||
#include <QRunnable>
|
#include <QRunnable>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
#include "IImageLoader.h"
|
#include "backend/IImageLoader.h"
|
||||||
#include "IImageProvider.h"
|
#include "backend/IImageProvider.h"
|
||||||
|
|
||||||
class EpubImageResponse : public QQuickImageResponse, public QRunnable {
|
class EpubImageResponse : public QQuickImageResponse, public QRunnable {
|
||||||
// NO Q_OBJECT — already inherited from QQuickImageResponse
|
// NO Q_OBJECT — already inherited from QQuickImageResponse
|
||||||
@@ -285,10 +285,14 @@ QStringList EpubReader::chunkXhtml(const QByteArray &data, const QString &xhtmlH
|
|||||||
if (src.isEmpty() || src.startsWith(QStringLiteral("image://")))
|
if (src.isEmpty() || src.startsWith(QStringLiteral("image://")))
|
||||||
continue;
|
continue;
|
||||||
src = QUrl::fromPercentEncoding(src.toUtf8());
|
src = QUrl::fromPercentEncoding(src.toUtf8());
|
||||||
|
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);
|
const QString resolved = QDir::cleanPath(xhtmlDir + src);
|
||||||
img.setAttribute(QStringLiteral("src"),
|
img.setAttribute(QStringLiteral("src"),
|
||||||
QStringLiteral("image://epub/") + resolved);
|
QStringLiteral("image://epub/") + resolved);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Find <body> element (may have namespace prefix in some EPUBs)
|
// Find <body> element (may have namespace prefix in some EPUBs)
|
||||||
QDomElement body;
|
QDomElement body;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,5 +1,5 @@
|
|||||||
#include "ReaderFactory.h"
|
#include "ReaderFactory.h"
|
||||||
#include "FormatReader.h"
|
#include "backend/FormatReader.h"
|
||||||
|
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
|
|
||||||
File renamed without changes.
@@ -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;
|
||||||
|
};
|
||||||
+5
-3
@@ -11,12 +11,13 @@
|
|||||||
|
|
||||||
#include "backend/AppState.h"
|
#include "backend/AppState.h"
|
||||||
#include "backend/IImageProvider.h"
|
#include "backend/IImageProvider.h"
|
||||||
#include "backend/ImageLoaderFactory.h"
|
#include "backend/formats/ImageLoaderFactory.h"
|
||||||
#include "backend/ImageProviderFactory.h"
|
#include "backend/formats/ImageProviderFactory.h"
|
||||||
|
#include "backend/formats/WebImageProvider.h"
|
||||||
#include "backend/LibraryModel.h"
|
#include "backend/LibraryModel.h"
|
||||||
#include "backend/ReadingModel.h"
|
#include "backend/ReadingModel.h"
|
||||||
#include "backend/NativeFileDialogService.h"
|
#include "backend/NativeFileDialogService.h"
|
||||||
#include "backend/ReaderFactory.h"
|
#include "backend/formats/ReaderFactory.h"
|
||||||
#include "backend/book/Book.h"
|
#include "backend/book/Book.h"
|
||||||
#include "backend/formats/TxtReader.h"
|
#include "backend/formats/TxtReader.h"
|
||||||
#include "backend/formats/EpubReader.h"
|
#include "backend/formats/EpubReader.h"
|
||||||
@@ -135,6 +136,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
// --- QML engine ---
|
// --- QML engine ---
|
||||||
QQmlApplicationEngine engine;
|
QQmlApplicationEngine engine;
|
||||||
|
engine.addImageProvider(QStringLiteral("web"), new WebImageProvider());
|
||||||
engine.rootContext()->setContextProperty(QStringLiteral("isDebugBuild"),
|
engine.rootContext()->setContextProperty(QStringLiteral("isDebugBuild"),
|
||||||
#ifdef QT_DEBUG
|
#ifdef QT_DEBUG
|
||||||
true
|
true
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
#include <minizip/zip.h>
|
#include <minizip/zip.h>
|
||||||
|
|
||||||
#include "backend/EpubImageProvider.h"
|
#include "backend/formats/EpubImageProvider.h"
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Helpers
|
// Helpers
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#include <QtTest>
|
#include <QtTest>
|
||||||
#include "backend/ReaderFactory.h"
|
#include "backend/formats/ReaderFactory.h"
|
||||||
#include "backend/FormatReader.h"
|
#include "backend/FormatReader.h"
|
||||||
#include "backend/formats/TxtReader.h"
|
#include "backend/formats/TxtReader.h"
|
||||||
#include "backend/formats/EpubReader.h"
|
#include "backend/formats/EpubReader.h"
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
#include "backend/AppState.h"
|
#include "backend/AppState.h"
|
||||||
#include "backend/IFileDialogService.h"
|
#include "backend/IFileDialogService.h"
|
||||||
#include "backend/ReaderFactory.h"
|
#include "backend/formats/ReaderFactory.h"
|
||||||
#include "backend/formats/TxtReader.h"
|
#include "backend/formats/TxtReader.h"
|
||||||
#include "backend/formats/EpubReader.h"
|
#include "backend/formats/EpubReader.h"
|
||||||
#include "backend/state/BookPositionStore.h"
|
#include "backend/state/BookPositionStore.h"
|
||||||
|
|||||||
Reference in new issue
Block a user