From bf17bb569c470e678d3d55cbea11d0cf30361123 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20=C3=81ngel=20San=20Mart=C3=ADn?= Date: Sat, 29 Nov 2014 21:05:22 +0100 Subject: [PATCH] 'Add to...' submenu completed --- YACReaderLibrary/db/comic_model.cpp | 18 ++++++++++++++ YACReaderLibrary/db/comic_model.h | 1 + YACReaderLibrary/db/reading_list_model.cpp | 5 ++++ YACReaderLibrary/db/reading_list_model.h | 1 + YACReaderLibrary/db_helper.cpp | 14 +++++++++++ YACReaderLibrary/db_helper.h | 1 + YACReaderLibrary/library_window.cpp | 29 ++++++++++++++++++++++ YACReaderLibrary/library_window.h | 1 + 8 files changed, 70 insertions(+) diff --git a/YACReaderLibrary/db/comic_model.cpp b/YACReaderLibrary/db/comic_model.cpp index 548f0056..61cb7214 100644 --- a/YACReaderLibrary/db/comic_model.cpp +++ b/YACReaderLibrary/db/comic_model.cpp @@ -815,6 +815,24 @@ void ComicModel::addComicsToFavorites(const QList & comicsList) QSqlDatabase::removeDatabase(_databasePath); } +void ComicModel::addComicsToLabel(const QList &comicsList, qulonglong labelId) +{ + QList comics = getComics(comicsList); + + DBHelper::insertComicsInFavorites(comics, QSqlDatabase()); + + QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath); + + db.transaction(); + + DBHelper::insertComicsInLabel(comics,labelId,db); + + db.commit(); + + db.close(); + QSqlDatabase::removeDatabase(_databasePath); +} + void ComicModel::updateRating(int rating, QModelIndex mi) { diff --git a/YACReaderLibrary/db/comic_model.h b/YACReaderLibrary/db/comic_model.h index 1c13775a..a05eb024 100644 --- a/YACReaderLibrary/db/comic_model.h +++ b/YACReaderLibrary/db/comic_model.h @@ -63,6 +63,7 @@ public: void reload(const ComicDB & comic); void resetComicRating(const QModelIndex & mi); void addComicsToFavorites(const QList &comicsList); + void addComicsToLabel(const QList &comicsList, qulonglong labelId); QHash roleNames() const; diff --git a/YACReaderLibrary/db/reading_list_model.cpp b/YACReaderLibrary/db/reading_list_model.cpp index ec7235a4..dbf542b5 100644 --- a/YACReaderLibrary/db/reading_list_model.cpp +++ b/YACReaderLibrary/db/reading_list_model.cpp @@ -339,6 +339,11 @@ void ReadingListModel::deleteItem(const QModelIndex &mi) } } +const QList ReadingListModel::getLabels() +{ + return labels; +} + void ReadingListModel::cleanAll() { if(rootItem != 0) diff --git a/YACReaderLibrary/db/reading_list_model.h b/YACReaderLibrary/db/reading_list_model.h index 40048a2f..c1411a29 100644 --- a/YACReaderLibrary/db/reading_list_model.h +++ b/YACReaderLibrary/db/reading_list_model.h @@ -49,6 +49,7 @@ public: QString name(const QModelIndex & mi); void rename(const QModelIndex & mi, const QString & name); void deleteItem(const QModelIndex & mi); + const QList getLabels(); enum Roles { TypeListsRole = Qt::UserRole + 1, diff --git a/YACReaderLibrary/db_helper.cpp b/YACReaderLibrary/db_helper.cpp index c631d607..0e1bb1bb 100644 --- a/YACReaderLibrary/db_helper.cpp +++ b/YACReaderLibrary/db_helper.cpp @@ -434,6 +434,20 @@ void DBHelper::insertComicsInFavorites(const QList &comicsList, QSqlDat query.exec(); } } + +void DBHelper::insertComicsInLabel(const QList &comicsList, qulonglong labelId, QSqlDatabase &db) +{ + QSqlQuery query(db); + query.prepare("INSERT INTO comic_label (label_id, comic_id) " + "VALUES (:label_id, :comic_id)"); + + foreach(ComicDB comic, comicsList) + { + query.bindValue(":label_id", labelId); + query.bindValue(":comic_id", comic.id); + query.exec(); + } +} //queries QList DBHelper::getFoldersFromParent(qulonglong parentId, QSqlDatabase & db, bool sort) { diff --git a/YACReaderLibrary/db_helper.h b/YACReaderLibrary/db_helper.h index c6be4d2e..306a305f 100644 --- a/YACReaderLibrary/db_helper.h +++ b/YACReaderLibrary/db_helper.h @@ -42,6 +42,7 @@ public: static qulonglong insertLabel(const QString & name, YACReader::LabelColors color , QSqlDatabase & db); static qulonglong insertReadingList(const QString & name, QSqlDatabase & db); static void insertComicsInFavorites(const QList & comicsList, QSqlDatabase & db); + static void insertComicsInLabel(const QList & comicsList, qulonglong labelId, QSqlDatabase & db); //updates static void update(qulonglong libraryId, ComicInfo & comicInfo); static void update(ComicDB * comics, QSqlDatabase & db); diff --git a/YACReaderLibrary/library_window.cpp b/YACReaderLibrary/library_window.cpp index b40ab31d..4588ecb8 100644 --- a/YACReaderLibrary/library_window.cpp +++ b/YACReaderLibrary/library_window.cpp @@ -83,6 +83,8 @@ #include "yacreader_history_controller.h" #include "db_helper.h" +#include "reading_list_item.h" + #include "QsLog.h" #ifdef Q_OS_WIN @@ -1698,6 +1700,33 @@ void LibraryWindow::setupAddToSubmenu(QMenu &menu) { menu.addAction(addToFavoritesAction); addToMenuAction->setMenu(&menu); + + const QList labels = listsModel->getLabels(); + if(labels.count() > 0) + menu.addSeparator(); + foreach(LabelItem * label, labels) + { + QAction * action = new QAction(this); + action->setIcon(label->getIcon()); + action->setText(label->name()); + + action->setData(label->getId()); + + menu.addAction(action); + + connect(action,SIGNAL(triggered()),this,SLOT(onAddComicsToLabel())); + } +} + +void LibraryWindow::onAddComicsToLabel() +{ + QAction * action = static_cast(sender()); + + qulonglong labelId = action->data().toULongLong(); + + QModelIndexList comics = getSelectedComics(); + + comicsModel->addComicsToLabel(comics,labelId); } void LibraryWindow::selectSubfolder(const QModelIndex &mi, int child) diff --git a/YACReaderLibrary/library_window.h b/YACReaderLibrary/library_window.h index 43db505e..c72b4180 100644 --- a/YACReaderLibrary/library_window.h +++ b/YACReaderLibrary/library_window.h @@ -391,6 +391,7 @@ public slots: void showComicsViewContextMenu(const QPoint & point); void showComicsItemContextMenu(const QPoint & point); void setupAddToSubmenu(QMenu & menu); + void onAddComicsToLabel(); };