From ab9472cb58f23d938791fba08e34096f9031f9c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20=C3=81ngel=20San=20Mart=C3=ADn?= Date: Thu, 17 Aug 2023 19:41:13 +0200 Subject: [PATCH] Add a method for triggering the change of a comic cover from the model Sad day for nice code. --- YACReaderLibrary/db/comic_model.cpp | 19 +++++++++++++++++++ YACReaderLibrary/db/comic_model.h | 1 + 2 files changed, 20 insertions(+) diff --git a/YACReaderLibrary/db/comic_model.cpp b/YACReaderLibrary/db/comic_model.cpp index 7dff6a6b..ba4aeffa 100644 --- a/YACReaderLibrary/db/comic_model.cpp +++ b/YACReaderLibrary/db/comic_model.cpp @@ -1165,6 +1165,25 @@ void ComicModel::resetComicRating(const QModelIndex &mi) QSqlDatabase::removeDatabase(connectionName); } +void ComicModel::notifyCoverChange(const ComicDB &comic) +{ + auto it = std::find_if(_data.begin(), _data.end(), [comic](ComicItem *item) { return item->data(ComicModel::Id).toULongLong() == comic.id; }); + auto itemIndex = std::distance(_data.begin(), it); + auto item = _data[itemIndex]; + + // emiting a dataChage doesn't work in QML for some reason, CoverPathRole is requested but the view doesn't update the image + // removing and reading again works with the flow views without any additional code, but it's not the best solution + beginRemoveRows(QModelIndex(), itemIndex, itemIndex); + _data.removeAt(itemIndex); + endRemoveRows(); + + beginInsertRows(QModelIndex(), itemIndex, itemIndex); + _data.insert(itemIndex, item); + endInsertRows(); + + // this doesn't work in QML -> emit dataChanged(index(itemIndex, 0), index(itemIndex, 0), QVector() << CoverPathRole); +} + QUrl ComicModel::getCoverUrlPathForComicHash(const QString &hash) const { return QUrl("file:" + _databasePath + "/covers/" + hash + ".jpg"); diff --git a/YACReaderLibrary/db/comic_model.h b/YACReaderLibrary/db/comic_model.h index f9b8b5ca..fbb6416f 100644 --- a/YACReaderLibrary/db/comic_model.h +++ b/YACReaderLibrary/db/comic_model.h @@ -124,6 +124,7 @@ public: void reload(); void reload(const ComicDB &comic); void resetComicRating(const QModelIndex &mi); + void notifyCoverChange(const ComicDB &comic); Q_INVOKABLE QUrl getCoverUrlPathForComicHash(const QString &hash) const;