177 lines
4.7 KiB
C++
177 lines
4.7 KiB
C++
#include "LibraryModel.h"
|
|
#include "LibraryBookModel.h"
|
|
|
|
LibraryModel::LibraryModel(QObject *parent)
|
|
: QAbstractListModel(parent)
|
|
{
|
|
// 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";
|
|
}
|
|
|
|
LibraryModel::~LibraryModel()
|
|
{
|
|
clear();
|
|
}
|
|
|
|
int LibraryModel::rowCount(const QModelIndex &parent) const
|
|
{
|
|
if (parent.isValid())
|
|
return 0;
|
|
return m_books.count();
|
|
}
|
|
|
|
QVariant LibraryModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
if (!index.isValid() || index.row() < 0 || index.row() >= m_books.count())
|
|
return QVariant();
|
|
|
|
LibraryBookModel* 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 LibraryModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
{
|
|
if (!index.isValid() || index.row() < 0 || index.row() >= m_books.count())
|
|
return false;
|
|
|
|
LibraryBookModel* 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> LibraryModel::roleNames() const
|
|
{
|
|
return m_roleNames;
|
|
}
|
|
|
|
void LibraryModel::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) {
|
|
LibraryBookModel* 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 — LibraryModel takes ownership via parent
|
|
auto* book = new LibraryBookModel(filePath, progress, lastOpened, this);
|
|
connect(book, &LibraryBookModel::metadataLoaded, this, &LibraryModel::onBookMetadataLoaded);
|
|
|
|
beginInsertRows(QModelIndex(), 0, 0);
|
|
m_books.prepend(book);
|
|
endInsertRows();
|
|
|
|
emit countChanged();
|
|
book->loadMetadata();
|
|
}
|
|
|
|
void LibraryModel::removeBook(const QString &filePath)
|
|
{
|
|
for (int i = 0; i < m_books.size(); i++) {
|
|
if (m_books[i]->filePath() == filePath) {
|
|
LibraryBookModel* book = m_books[i];
|
|
|
|
// Disconnect signals
|
|
disconnect(book, &LibraryBookModel::metadataLoaded, this, &LibraryModel::onBookMetadataLoaded);
|
|
|
|
beginRemoveRows(QModelIndex(), i, i);
|
|
m_books.removeAt(i);
|
|
endRemoveRows();
|
|
|
|
book->deleteLater();
|
|
emit countChanged();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
LibraryBookModel* LibraryModel::findBook(const QString &filePath) const
|
|
{
|
|
for (LibraryBookModel* book : m_books) {
|
|
if (book->filePath() == filePath) {
|
|
return book;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void LibraryModel::clear()
|
|
{
|
|
if (m_books.isEmpty())
|
|
return;
|
|
|
|
beginRemoveRows(QModelIndex(), 0, m_books.size() - 1);
|
|
|
|
// Disconnect and delete all books
|
|
for (LibraryBookModel* book : m_books) {
|
|
disconnect(book, &LibraryBookModel::metadataLoaded, this, &LibraryModel::onBookMetadataLoaded);
|
|
book->deleteLater();
|
|
}
|
|
|
|
m_books.clear();
|
|
endRemoveRows();
|
|
|
|
emit countChanged();
|
|
}
|
|
|
|
QList<LibraryBookModel*> LibraryModel::books() const
|
|
{
|
|
return m_books;
|
|
}
|
|
|
|
void LibraryModel::onBookMetadataLoaded()
|
|
{
|
|
LibraryBookModel* book = qobject_cast<LibraryBookModel*>(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});
|
|
}
|
|
} |