old render cleanup part 3
This commit is contained in:
45 files changed
+1057
-1135
No files matched your search
+163
-122
@@ -1,100 +1,133 @@
|
||||
#include "HistoryModel.h"
|
||||
#include "HistoryBookModel.h"
|
||||
#include "AppState.h"
|
||||
#include "FormatReader.h"
|
||||
#include "formats/FormatRegistry.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QFileInfo>
|
||||
#include <QPointer>
|
||||
#include <QtConcurrent>
|
||||
|
||||
namespace {
|
||||
QString formatForPath(const QString &filePath)
|
||||
{
|
||||
if (QFileInfo(filePath).isDir())
|
||||
return QStringLiteral("Folder");
|
||||
return filePath.section('.', -1).toUpper();
|
||||
}
|
||||
}
|
||||
|
||||
HistoryModel::HistoryModel(FormatRegistry ®istry, QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
, m_registry(registry)
|
||||
{
|
||||
// Initialize role names
|
||||
m_roleNames[FilePathRole] = "filePath";
|
||||
m_roleNames[TitleRole] = "title";
|
||||
m_roleNames[AuthorRole] = "author";
|
||||
m_roleNames[FormatRole] = "format";
|
||||
m_roleNames[ProgressRole] = "progress";
|
||||
m_roleNames[LastOpenedRole] = "lastOpened";
|
||||
m_roleNames[CoverImageUrlRole] = "coverImageUrl";
|
||||
}
|
||||
|
||||
HistoryModel::~HistoryModel()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
HistoryModel::~HistoryModel() = default;
|
||||
|
||||
int HistoryModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid())
|
||||
return 0;
|
||||
return m_books.count();
|
||||
return m_entries.size();
|
||||
}
|
||||
|
||||
QVariant HistoryModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_books.count())
|
||||
return QVariant();
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_entries.size())
|
||||
return {};
|
||||
|
||||
HistoryBookModel* book = m_books.at(index.row());
|
||||
if (!book)
|
||||
return QVariant();
|
||||
|
||||
const auto &entry = m_entries.at(index.row());
|
||||
switch (role) {
|
||||
case FilePathRole:
|
||||
return book->filePath();
|
||||
case TitleRole:
|
||||
return book->title();
|
||||
case AuthorRole:
|
||||
return book->author();
|
||||
case FormatRole:
|
||||
return book->format();
|
||||
case ProgressRole:
|
||||
return book->progress();
|
||||
case LastOpenedRole:
|
||||
return book->lastOpened();
|
||||
case CoverImageUrlRole:
|
||||
return book->coverImageUrl();
|
||||
default:
|
||||
return QVariant();
|
||||
case FilePathRole:
|
||||
return entry.filePath;
|
||||
case TitleRole:
|
||||
return entry.title;
|
||||
case AuthorRole:
|
||||
return entry.author;
|
||||
case FormatRole:
|
||||
return entry.format;
|
||||
case ProgressRole:
|
||||
return entry.progress;
|
||||
case LastOpenedRole:
|
||||
return entry.lastOpened;
|
||||
case CoverImageUrlRole:
|
||||
return entry.coverImageUrl;
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
bool HistoryModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_books.count())
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_entries.size())
|
||||
return false;
|
||||
|
||||
HistoryBookModel* book = m_books.at(index.row());
|
||||
|
||||
auto &entry = m_entries[index.row()];
|
||||
switch (role) {
|
||||
case ProgressRole:
|
||||
book->setProgress(value.toDouble());
|
||||
emit dataChanged(index, index, {ProgressRole});
|
||||
return true;
|
||||
case LastOpenedRole:
|
||||
book->setLastOpened(value.toString());
|
||||
emit dataChanged(index, index, {LastOpenedRole});
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
case ProgressRole:
|
||||
entry.progress = value.toDouble();
|
||||
emit dataChanged(index, index, {ProgressRole});
|
||||
return true;
|
||||
case LastOpenedRole:
|
||||
entry.lastOpened = value.toString();
|
||||
emit dataChanged(index, index, {LastOpenedRole});
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> HistoryModel::roleNames() const
|
||||
{
|
||||
return m_roleNames;
|
||||
return {
|
||||
{FilePathRole, "filePath"},
|
||||
{TitleRole, "title"},
|
||||
{AuthorRole, "author"},
|
||||
{FormatRole, "format"},
|
||||
{ProgressRole, "progress"},
|
||||
{LastOpenedRole, "lastOpened"},
|
||||
{CoverImageUrlRole, "coverImageUrl"}
|
||||
};
|
||||
}
|
||||
|
||||
void HistoryModel::load(AppState &state)
|
||||
{
|
||||
clear();
|
||||
for (const QVariantMap &item : state.loadArray(QStringLiteral("history"))) {
|
||||
const QString fp = item.value(QStringLiteral("filePath")).toString();
|
||||
if (fp.isEmpty()) continue;
|
||||
double progress = item.value(QStringLiteral("progress"), 0.0).toDouble();
|
||||
progress = qBound(0.0, progress, 1.0);
|
||||
const QString lo = item.value(QStringLiteral("lastOpened")).toString();
|
||||
addOrUpdateBook(fp, progress, lo);
|
||||
}
|
||||
}
|
||||
|
||||
void HistoryModel::save(AppState &state) const
|
||||
{
|
||||
QList<QVariantMap> items;
|
||||
items.reserve(m_entries.size());
|
||||
for (const auto &entry : m_entries) {
|
||||
items.append({
|
||||
{QStringLiteral("filePath"), entry.filePath},
|
||||
{QStringLiteral("progress"), entry.progress},
|
||||
{QStringLiteral("lastOpened"), entry.lastOpened},
|
||||
});
|
||||
}
|
||||
state.saveArray(QStringLiteral("history"), items);
|
||||
}
|
||||
|
||||
void HistoryModel::addOrUpdateBook(const QString &filePath, double progress, const QString &lastOpened)
|
||||
{
|
||||
// Check if book already exists
|
||||
for (int i = 0; i < m_books.size(); i++) {
|
||||
if (m_books[i]->filePath() == filePath) {
|
||||
HistoryBookModel* existingBook = m_books[i];
|
||||
existingBook->setProgress(progress);
|
||||
existingBook->setLastOpened(lastOpened);
|
||||
for (int i = 0; i < m_entries.size(); ++i) {
|
||||
if (m_entries[i].filePath == filePath) {
|
||||
m_entries[i].progress = progress;
|
||||
m_entries[i].lastOpened = lastOpened;
|
||||
|
||||
if (i != 0) {
|
||||
beginMoveRows(QModelIndex(), i, i, QModelIndex(), 0);
|
||||
m_books.move(i, 0);
|
||||
m_entries.move(i, 0);
|
||||
endMoveRows();
|
||||
}
|
||||
emit dataChanged(createIndex(0, 0), createIndex(0, 0), {ProgressRole, LastOpenedRole});
|
||||
@@ -102,106 +135,114 @@ void HistoryModel::addOrUpdateBook(const QString &filePath, double progress, con
|
||||
}
|
||||
}
|
||||
|
||||
// New book — HistoryModel takes ownership via parent
|
||||
auto* book = new HistoryBookModel(filePath, progress, lastOpened, m_registry, this);
|
||||
connect(book, &HistoryBookModel::metadataLoaded, this, &HistoryModel::onBookMetadataLoaded);
|
||||
HistoryEntry entry;
|
||||
entry.filePath = filePath;
|
||||
entry.progress = progress;
|
||||
entry.lastOpened = lastOpened;
|
||||
entry.format = formatForPath(filePath);
|
||||
|
||||
beginInsertRows(QModelIndex(), 0, 0);
|
||||
m_books.prepend(book);
|
||||
m_entries.prepend(entry);
|
||||
endInsertRows();
|
||||
|
||||
emit countChanged();
|
||||
book->loadMetadata();
|
||||
|
||||
requestMetadata(filePath);
|
||||
}
|
||||
|
||||
void HistoryModel::addOrUpdateBook(const QString &filePath, double progress)
|
||||
{
|
||||
double resolvedProgress = progress;
|
||||
if (progress < 0.0) {
|
||||
for (const auto &entry : m_entries) {
|
||||
if (entry.filePath == filePath) {
|
||||
resolvedProgress = entry.progress;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (resolvedProgress < 0.0)
|
||||
resolvedProgress = 0.0;
|
||||
}
|
||||
addOrUpdateBook(filePath, resolvedProgress,
|
||||
QDateTime::currentDateTime().toString(Qt::ISODate));
|
||||
}
|
||||
|
||||
void HistoryModel::removeBook(const QString &filePath)
|
||||
{
|
||||
for (int i = 0; i < m_books.size(); i++) {
|
||||
if (m_books[i]->filePath() == filePath) {
|
||||
HistoryBookModel* book = m_books[i];
|
||||
|
||||
for (int i = 0; i < m_entries.size(); ++i) {
|
||||
if (m_entries[i].filePath == filePath) {
|
||||
beginRemoveRows(QModelIndex(), i, i);
|
||||
m_books.removeAt(i);
|
||||
m_entries.removeAt(i);
|
||||
endRemoveRows();
|
||||
|
||||
book->deleteLater();
|
||||
emit countChanged();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
HistoryBookModel* HistoryModel::findBook(const QString &filePath) const
|
||||
{
|
||||
for (HistoryBookModel* book : m_books) {
|
||||
if (book->filePath() == filePath) {
|
||||
return book;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void HistoryModel::clear()
|
||||
{
|
||||
if (m_books.isEmpty())
|
||||
if (m_entries.isEmpty())
|
||||
return;
|
||||
|
||||
beginRemoveRows(QModelIndex(), 0, m_books.size() - 1);
|
||||
|
||||
for (HistoryBookModel* book : m_books)
|
||||
book->deleteLater();
|
||||
|
||||
m_books.clear();
|
||||
beginRemoveRows(QModelIndex(), 0, m_entries.size() - 1);
|
||||
m_entries.clear();
|
||||
endRemoveRows();
|
||||
|
||||
emit countChanged();
|
||||
}
|
||||
|
||||
void HistoryModel::addOrUpdateBook(const QString &filePath, double progress)
|
||||
{
|
||||
// Sentinel: progress < 0 means "preserve the existing progress value"
|
||||
double resolvedProgress = progress;
|
||||
if (progress < 0.0) {
|
||||
HistoryBookModel *existing = findBook(filePath);
|
||||
resolvedProgress = existing ? existing->progress() : 0.0;
|
||||
}
|
||||
addOrUpdateBook(filePath, resolvedProgress,
|
||||
QDateTime::currentDateTime().toString(Qt::ISODate));
|
||||
}
|
||||
|
||||
QString HistoryModel::filePath(int index) const
|
||||
{
|
||||
if (index < 0 || index >= m_books.size()) return {};
|
||||
return m_books.at(index)->filePath();
|
||||
if (index < 0 || index >= m_entries.size()) return {};
|
||||
return m_entries.at(index).filePath;
|
||||
}
|
||||
|
||||
double HistoryModel::progress(int index) const
|
||||
{
|
||||
if (index < 0 || index >= m_books.size()) return 0.0;
|
||||
return m_books.at(index)->progress();
|
||||
if (index < 0 || index >= m_entries.size()) return 0.0;
|
||||
return m_entries.at(index).progress;
|
||||
}
|
||||
|
||||
QString HistoryModel::lastOpened(int index) const
|
||||
{
|
||||
if (index < 0 || index >= m_books.size()) return {};
|
||||
return m_books.at(index)->lastOpened();
|
||||
if (index < 0 || index >= m_entries.size()) return {};
|
||||
return m_entries.at(index).lastOpened;
|
||||
}
|
||||
|
||||
QList<HistoryBookModel*> HistoryModel::books() const
|
||||
void HistoryModel::requestMetadata(const QString &filePath)
|
||||
{
|
||||
return m_books;
|
||||
FormatRegistry *registry = &m_registry;
|
||||
QPointer<HistoryModel> self(this);
|
||||
|
||||
(void)QtConcurrent::run([self, filePath, registry]() {
|
||||
auto reader = registry->createReader(filePath);
|
||||
QString title;
|
||||
QString author;
|
||||
QString coverUrl;
|
||||
bool ok = false;
|
||||
|
||||
if (reader && reader->loadMetadata(filePath)) {
|
||||
title = reader->title();
|
||||
author = reader->author();
|
||||
coverUrl = reader->coverImageUrl();
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if (!self)
|
||||
return;
|
||||
|
||||
QMetaObject::invokeMethod(self.data(), [self, filePath, title, author, coverUrl, ok]() {
|
||||
if (!self || !ok) return;
|
||||
for (int i = 0; i < self->m_entries.size(); ++i) {
|
||||
if (self->m_entries[i].filePath == filePath) {
|
||||
self->m_entries[i].title = title;
|
||||
self->m_entries[i].author = author;
|
||||
self->m_entries[i].coverImageUrl = coverUrl;
|
||||
self->m_entries[i].metadataLoaded = true;
|
||||
const QModelIndex idx = self->createIndex(i, 0);
|
||||
emit self->dataChanged(idx, idx, {TitleRole, AuthorRole, CoverImageUrlRole});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}, Qt::QueuedConnection);
|
||||
});
|
||||
}
|
||||
|
||||
void HistoryModel::onBookMetadataLoaded()
|
||||
{
|
||||
HistoryBookModel* book = qobject_cast<HistoryBookModel*>(sender());
|
||||
if (!book)
|
||||
return;
|
||||
|
||||
int row = m_books.indexOf(book);
|
||||
if (row >= 0) {
|
||||
QModelIndex index = this->index(row);
|
||||
// Update all metadata-related roles when metadata is loaded
|
||||
emit dataChanged(index, index, {TitleRole, AuthorRole, CoverImageUrlRole});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user