Rename Library to History
This commit is contained in:
11 files changed
+102
-102
No files matched your search
@@ -0,0 +1,209 @@
|
||||
#include "HistoryModel.h"
|
||||
#include "HistoryBookModel.h"
|
||||
#include <QDateTime>
|
||||
|
||||
HistoryModel::HistoryModel(ReaderFactory &factory, QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
, m_factory(factory)
|
||||
{
|
||||
// Initialize role names
|
||||
m_roleNames[FilePathRole] = "filePath";
|
||||
m_roleNames[TitleRole] = "title";
|
||||
m_roleNames[AuthorRole] = "author";
|
||||
m_roleNames[ProgressRole] = "progress";
|
||||
m_roleNames[LastOpenedRole] = "lastOpened";
|
||||
m_roleNames[CoverImageUrlRole] = "coverImageUrl";
|
||||
}
|
||||
|
||||
HistoryModel::~HistoryModel()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
int HistoryModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid())
|
||||
return 0;
|
||||
return m_books.count();
|
||||
}
|
||||
|
||||
QVariant HistoryModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_books.count())
|
||||
return QVariant();
|
||||
|
||||
HistoryBookModel* book = m_books.at(index.row());
|
||||
if (!book)
|
||||
return QVariant();
|
||||
|
||||
switch (role) {
|
||||
case FilePathRole:
|
||||
return book->filePath();
|
||||
case TitleRole:
|
||||
return book->title();
|
||||
case AuthorRole:
|
||||
return book->author();
|
||||
case ProgressRole:
|
||||
return book->progress();
|
||||
case LastOpenedRole:
|
||||
return book->lastOpened();
|
||||
case CoverImageUrlRole:
|
||||
return book->coverImageUrl();
|
||||
default:
|
||||
return QVariant();
|
||||
}
|
||||
}
|
||||
|
||||
bool HistoryModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_books.count())
|
||||
return false;
|
||||
|
||||
HistoryBookModel* book = m_books.at(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;
|
||||
}
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> HistoryModel::roleNames() const
|
||||
{
|
||||
return m_roleNames;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (i != 0) {
|
||||
beginMoveRows(QModelIndex(), i, i, QModelIndex(), 0);
|
||||
m_books.move(i, 0);
|
||||
endMoveRows();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// New book — HistoryModel takes ownership via parent
|
||||
auto* book = new HistoryBookModel(filePath, progress, lastOpened, m_factory, this);
|
||||
connect(book, &HistoryBookModel::metadataLoaded, this, &HistoryModel::onBookMetadataLoaded);
|
||||
|
||||
beginInsertRows(QModelIndex(), 0, 0);
|
||||
m_books.prepend(book);
|
||||
endInsertRows();
|
||||
|
||||
emit countChanged();
|
||||
book->loadMetadata();
|
||||
}
|
||||
|
||||
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];
|
||||
|
||||
// Disconnect signals
|
||||
disconnect(book, &HistoryBookModel::metadataLoaded, this, &HistoryModel::onBookMetadataLoaded);
|
||||
|
||||
beginRemoveRows(QModelIndex(), i, i);
|
||||
m_books.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())
|
||||
return;
|
||||
|
||||
beginRemoveRows(QModelIndex(), 0, m_books.size() - 1);
|
||||
|
||||
// Disconnect and delete all books
|
||||
for (HistoryBookModel* book : m_books) {
|
||||
disconnect(book, &HistoryBookModel::metadataLoaded, this, &HistoryModel::onBookMetadataLoaded);
|
||||
book->deleteLater();
|
||||
}
|
||||
|
||||
m_books.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();
|
||||
}
|
||||
|
||||
double HistoryModel::progress(int index) const
|
||||
{
|
||||
if (index < 0 || index >= m_books.size()) return 0.0;
|
||||
return m_books.at(index)->progress();
|
||||
}
|
||||
|
||||
QString HistoryModel::lastOpened(int index) const
|
||||
{
|
||||
if (index < 0 || index >= m_books.size()) return {};
|
||||
return m_books.at(index)->lastOpened();
|
||||
}
|
||||
|
||||
QList<HistoryBookModel*> HistoryModel::books() const
|
||||
{
|
||||
return m_books;
|
||||
}
|
||||
|
||||
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