Add back history view cover photos
This commit is contained in:
11 files changed
+476
-28
No files matched your search
@@ -1,7 +1,10 @@
|
||||
#include "HistoryBookModel.h"
|
||||
#include "FormatReader.h"
|
||||
#include "IImageProvider.h"
|
||||
#include "formats/ImageProviderFactory.h"
|
||||
#include "formats/ReaderFactory.h"
|
||||
#include <QtConcurrent/QtConcurrent>
|
||||
#include <QUrl>
|
||||
|
||||
HistoryBookModel::HistoryBookModel(ReaderFactory &factory, QObject *parent)
|
||||
: QObject(parent)
|
||||
@@ -93,11 +96,24 @@ void HistoryBookModel::loadMetadata()
|
||||
success = true;
|
||||
}
|
||||
|
||||
QMetaObject::invokeMethod(self.data(), [self, title, author, success]() {
|
||||
QString coverImageUrl;
|
||||
if (success) {
|
||||
QString coverPath = reader->coverImagePath();
|
||||
if (!coverPath.isEmpty()) {
|
||||
std::unique_ptr<IImageProvider> provider(ImageProviderFactory::create(filePath));
|
||||
if (provider) {
|
||||
provider->setFilePath(filePath);
|
||||
coverImageUrl = provider->coverImageUrl(coverPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QMetaObject::invokeMethod(self.data(), [self, title, author, coverImageUrl, success]() {
|
||||
if (!self || !success)
|
||||
return;
|
||||
self->m_title = title;
|
||||
self->m_author = author;
|
||||
self->m_coverImageUrl = coverImageUrl;
|
||||
self->m_metadataLoaded = true;
|
||||
emit self->metadataLoaded();
|
||||
}, Qt::QueuedConnection);
|
||||
@@ -106,20 +122,4 @@ void HistoryBookModel::loadMetadata()
|
||||
|
||||
void HistoryBookModel::onMetadataLoaded()
|
||||
{
|
||||
// This slot can be used for any post-processing after metadata is loaded
|
||||
}
|
||||
|
||||
void HistoryBookModel::loadCoverImage(const QString &epubBasePath)
|
||||
{
|
||||
if (!m_coverImageUrl.isEmpty() || m_filePath.isEmpty())
|
||||
return;
|
||||
|
||||
auto reader = m_factory.create(m_filePath);
|
||||
if (reader && reader->loadMetadata(m_filePath)) {
|
||||
QString coverPath = reader->coverImagePath();
|
||||
if (!coverPath.isEmpty()) {
|
||||
m_coverImageUrl = QStringLiteral("image://epub/") + coverPath;
|
||||
emit coverImageLoaded();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,6 @@ public:
|
||||
QString coverImageUrl() const;
|
||||
|
||||
void loadMetadata();
|
||||
void loadCoverImage(const QString &epubBasePath);
|
||||
|
||||
public slots:
|
||||
void onMetadataLoaded();
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <QMutexLocker>
|
||||
#include <QQuickTextureFactory>
|
||||
#include <QThreadPool>
|
||||
#include <QUrl>
|
||||
|
||||
#include <minizip/unzip.h>
|
||||
|
||||
@@ -64,7 +65,20 @@ QQuickImageResponse *EpubImageProvider::requestImageResponse(const QString &id,
|
||||
displayWidth = m_displayWidth;
|
||||
}
|
||||
|
||||
auto *response = new EpubImageResponse(epubPath, id, displayWidth, requestedSize);
|
||||
// id may be "internal/path.jpg" (old format) or
|
||||
// "internal/path.jpg?epub=<percent-encoded-epub-path>" (new embedded format).
|
||||
QString internalPath = id;
|
||||
const int qmark = id.indexOf(QLatin1Char('?'));
|
||||
if (qmark >= 0) {
|
||||
internalPath = id.left(qmark);
|
||||
const QString query = id.mid(qmark + 1);
|
||||
// Parse epub=<value> from query string
|
||||
const QLatin1String key("epub=");
|
||||
if (query.startsWith(key))
|
||||
epubPath = QUrl::fromPercentEncoding(query.mid(key.size()).toUtf8());
|
||||
}
|
||||
|
||||
auto *response = new EpubImageResponse(epubPath, internalPath, displayWidth, requestedSize);
|
||||
QThreadPool::globalInstance()->start(response);
|
||||
return response;
|
||||
}
|
||||
@@ -119,16 +133,26 @@ 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;
|
||||
{
|
||||
if (url.hasQuery()) {
|
||||
// Embedded format: epub path is in the query string as epub=<encoded>
|
||||
const QString q = url.query();
|
||||
const QLatin1String key("epub=");
|
||||
if (q.startsWith(key))
|
||||
epubPath = QUrl::fromPercentEncoding(q.mid(key.size()).toUtf8());
|
||||
}
|
||||
if (epubPath.isEmpty()) {
|
||||
QMutexLocker lock(&m_mutex);
|
||||
epubPath = m_epubPath;
|
||||
}
|
||||
|
||||
return loadImageFromEpub(epubPath, zipPath, maxWidth, {});
|
||||
}
|
||||
|
||||
@@ -152,7 +176,11 @@ void EpubImageProvider::setFilePath(const QString &filePath)
|
||||
|
||||
QString EpubImageProvider::coverImageUrl(const QString &coverPath) const
|
||||
{
|
||||
return QStringLiteral("image://epub/") + coverPath;
|
||||
QString epubPath;
|
||||
{ QMutexLocker lock(&m_mutex); epubPath = m_epubPath; }
|
||||
return QStringLiteral("image://epub/") + coverPath
|
||||
+ QStringLiteral("?epub=")
|
||||
+ QString::fromUtf8(QUrl::toPercentEncoding(epubPath));
|
||||
}
|
||||
|
||||
QString EpubImageProvider::scheme() const
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#include "FileIconProvider.h"
|
||||
#include <QFileIconProvider>
|
||||
#include <QFileInfo>
|
||||
#include <QIcon>
|
||||
|
||||
FileIconProvider::FileIconProvider()
|
||||
: QQuickImageProvider(QQuickImageProvider::Image)
|
||||
{}
|
||||
|
||||
QImage FileIconProvider::requestImage(const QString &id, QSize *size,
|
||||
const QSize &requestedSize)
|
||||
{
|
||||
// QML strips the leading slash from the URL path component, so restore it on Unix.
|
||||
QString filePath = id.startsWith(QLatin1Char('/')) ? id : QLatin1Char('/') + id;
|
||||
|
||||
QFileIconProvider iconProvider;
|
||||
QIcon icon = iconProvider.icon(QFileInfo(filePath));
|
||||
if (icon.isNull())
|
||||
icon = iconProvider.icon(QFileIconProvider::File);
|
||||
|
||||
QSize target = (requestedSize.width() > 0 && requestedSize.height() > 0)
|
||||
? requestedSize : QSize(60, 80);
|
||||
|
||||
QImage image = icon.pixmap(target).toImage();
|
||||
if (size)
|
||||
*size = image.size();
|
||||
return image;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include <QQuickImageProvider>
|
||||
|
||||
// Serves OS file-type icons under image://fileicon/<absolute-file-path>
|
||||
class FileIconProvider : public QQuickImageProvider {
|
||||
public:
|
||||
FileIconProvider();
|
||||
QImage requestImage(const QString &id, QSize *size,
|
||||
const QSize &requestedSize) override;
|
||||
};
|
||||
@@ -13,6 +13,8 @@
|
||||
#include "backend/IImageProvider.h"
|
||||
#include "backend/formats/ImageLoaderFactory.h"
|
||||
#include "backend/formats/ImageProviderFactory.h"
|
||||
#include "backend/formats/EpubImageProvider.h"
|
||||
#include "backend/formats/FileIconProvider.h"
|
||||
#include "backend/formats/WebImageProvider.h"
|
||||
#include "backend/HistoryModel.h"
|
||||
#include "backend/ReadingModel.h"
|
||||
@@ -137,6 +139,8 @@ int main(int argc, char *argv[])
|
||||
// --- QML engine ---
|
||||
QQmlApplicationEngine engine;
|
||||
engine.addImageProvider(QStringLiteral("web"), new WebImageProvider());
|
||||
engine.addImageProvider(QStringLiteral("epub"), new EpubImageProvider());
|
||||
engine.addImageProvider(QStringLiteral("fileicon"), new FileIconProvider());
|
||||
engine.rootContext()->setContextProperty(QStringLiteral("isDebugBuild"),
|
||||
#ifdef QT_DEBUG
|
||||
true
|
||||
|
||||
@@ -38,19 +38,32 @@ Rectangle {
|
||||
anchors.bottomMargin: 12
|
||||
spacing: 8
|
||||
|
||||
Image {
|
||||
id: coverImage
|
||||
Item {
|
||||
id: coverSlot
|
||||
visible: filePath !== ""
|
||||
width: 60
|
||||
height: 80
|
||||
source: coverImageUrl
|
||||
fillMode: Image.PreserveAspectFit
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
|
||||
Image {
|
||||
anchors.fill: parent
|
||||
source: filePath !== "" ? ("image://fileicon/" + filePath) : ""
|
||||
fillMode: Image.PreserveAspectFit
|
||||
visible: coverImage.status !== Image.Ready
|
||||
}
|
||||
|
||||
Image {
|
||||
id: coverImage
|
||||
anchors.fill: parent
|
||||
source: coverImageUrl
|
||||
fillMode: Image.PreserveAspectFit
|
||||
visible: status === Image.Ready
|
||||
}
|
||||
}
|
||||
|
||||
Column {
|
||||
id: bookInfoCol
|
||||
width: parent.width - removeBtn.width - parent.spacing - (coverImage.visible ? coverImage.width + 8 : 0)
|
||||
width: parent.width - removeBtn.width - parent.spacing - (coverSlot.visible ? coverSlot.width + parent.spacing : 0)
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
spacing: 3
|
||||
|
||||
|
||||
Reference in new issue
Block a user