From bcc51241b3ff6c342eb9d9f1b402b134d49476f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20=C3=81ngel=20San=20Mart=C3=ADn?= Date: Sun, 17 Apr 2016 11:20:14 +0200 Subject: [PATCH] Added support for knowing if a comic is in favorites or not. --- YACReaderLibrary/comics_view.cpp | 2 ++ YACReaderLibrary/db/comic_model.cpp | 13 +++++++++++++ YACReaderLibrary/db/comic_model.h | 2 ++ YACReaderLibrary/db_helper.cpp | 15 +++++++++++++++ YACReaderLibrary/db_helper.h | 2 ++ YACReaderLibrary/qml/ComicInfo.qml | 4 ++++ 6 files changed, 38 insertions(+) diff --git a/YACReaderLibrary/comics_view.cpp b/YACReaderLibrary/comics_view.cpp index f743fd8f..fdc8c16f 100644 --- a/YACReaderLibrary/comics_view.cpp +++ b/YACReaderLibrary/comics_view.cpp @@ -28,6 +28,8 @@ void ComicsView::updateInfoForIndex(int index) ComicInfo *comicInfo = &(comicDB->info); ctxt->setContextProperty("comic", comicDB); ctxt->setContextProperty("comicInfo", comicInfo); + + ctxt->setContextProperty("is_favorite", model->isFavorite(model->index(index,0))); } void ComicsView::dragEnterEvent(QDragEnterEvent *event) diff --git a/YACReaderLibrary/db/comic_model.cpp b/YACReaderLibrary/db/comic_model.cpp index 80118626..896fdf7a 100644 --- a/YACReaderLibrary/db/comic_model.cpp +++ b/YACReaderLibrary/db/comic_model.cpp @@ -1155,6 +1155,19 @@ void ComicModel::deleteComicsFromModel(const QList &comicsList) emit isEmpty(); } +bool ComicModel::isFavorite(const QModelIndex &index) +{ + bool isFavorite; + + QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath); + + isFavorite = DBHelper::isFavoriteComic(_data[index.row()]->data(Id).toLongLong(),db); + + db.close(); + QSqlDatabase::removeDatabase(_databasePath); + + return isFavorite; +} void ComicModel::updateRating(int rating, QModelIndex mi) { diff --git a/YACReaderLibrary/db/comic_model.h b/YACReaderLibrary/db/comic_model.h index ce0de290..bf517298 100644 --- a/YACReaderLibrary/db/comic_model.h +++ b/YACReaderLibrary/db/comic_model.h @@ -81,6 +81,8 @@ public: void deleteComicsFromModel(const QList &comicsList); + bool isFavorite(const QModelIndex &index); + QHash roleNames() const; enum Columns { diff --git a/YACReaderLibrary/db_helper.cpp b/YACReaderLibrary/db_helper.cpp index 9f784f91..6b1d995d 100644 --- a/YACReaderLibrary/db_helper.cpp +++ b/YACReaderLibrary/db_helper.cpp @@ -1062,3 +1062,18 @@ QList DBHelper::loadSubfoldersNames(qulonglong folderId, QSqlDatabase & } return result; } + +bool DBHelper::isFavoriteComic(qulonglong id, QSqlDatabase &db) +{ + QSqlQuery selectQuery(db); + selectQuery.prepare("SELECT * FROM comic_default_reading_list cl WHERE cl.comic_id = :comic_id AND cl.default_reading_list_id = 1"); + selectQuery.bindValue(":comic_id", id); + selectQuery.exec(); + + if(selectQuery.next()) + { + return true; + } + + return false; +} diff --git a/YACReaderLibrary/db_helper.h b/YACReaderLibrary/db_helper.h index 5eb2eae3..5dee01d6 100644 --- a/YACReaderLibrary/db_helper.h +++ b/YACReaderLibrary/db_helper.h @@ -76,6 +76,8 @@ public: static ComicDB loadComic(QString cname, QString cpath, QString chash, QSqlDatabase & database); static ComicInfo loadComicInfo(QString hash, QSqlDatabase & db); static QList loadSubfoldersNames(qulonglong folderId, QSqlDatabase & db); + //queries + static bool isFavoriteComic(qulonglong id, QSqlDatabase & db); }; #endif diff --git a/YACReaderLibrary/qml/ComicInfo.qml b/YACReaderLibrary/qml/ComicInfo.qml index d67855c9..48291cef 100644 --- a/YACReaderLibrary/qml/ComicInfo.qml +++ b/YACReaderLibrary/qml/ComicInfo.qml @@ -98,6 +98,8 @@ Rectangle { Layout.topMargin: 1 Layout.rightMargin: 17 Layout.alignment: Qt.AlignTop + + active: is_favorite } InfoRating { @@ -137,6 +139,8 @@ Rectangle { Layout.topMargin: 1 Layout.rightMargin: 17 Layout.alignment: Qt.AlignTop + + active: is_favorite } InfoRating {