diff src/gui/pages/anime_list.cpp @ 77:6f7385bd334c

*: update formatted all source files, no more subclassing QThread... many other changes :)
author Paper <mrpapersonic@gmail.com>
date Fri, 06 Oct 2023 06:18:53 -0400
parents 3364fadc8a36
children
line wrap: on
line diff
--- a/src/gui/pages/anime_list.cpp	Wed Oct 04 01:46:33 2023 -0400
+++ b/src/gui/pages/anime_list.cpp	Fri Oct 06 06:18:53 2023 -0400
@@ -26,7 +26,8 @@
 #include <QShortcut>
 #include <QStylePainter>
 #include <QStyledItemDelegate>
-#include <cmath>
+#include <QThreadPool>
+#include <set>
 
 AnimeListPageDelegate::AnimeListPageDelegate(QObject* parent) : QStyledItemDelegate(parent) {
 }
@@ -191,33 +192,19 @@
 	return QVariant();
 }
 
-void AnimeListPageModel::UpdateAnime(int id) {
-	/* meh... it might be better to just reinit the entire list */
-	int i = 0;
-	for (const auto& a : Anime::db.items) {
-		if (a.second.IsInUserList() && a.first == id && a.second.GetUserStatus() == status) {
-			emit dataChanged(index(i), index(i));
-		}
-		i++;
-	}
-}
-
 Anime::Anime* AnimeListPageModel::GetAnimeFromIndex(QModelIndex index) {
 	return &list.at(index.row());
 }
 
 void AnimeListPageModel::RefreshList() {
 	bool has_children = !!rowCount(index(0));
-	if (has_children)
-		beginResetModel();
-	else {
-		int count = 0;
-		for (const auto& a : Anime::db.items)
-			if (a.second.IsInUserList() && a.second.GetUserStatus() == status)
-				count++;
-		beginInsertRows(index(0), 0, count - 1);
+	if (!has_children) {
+		beginInsertRows(QModelIndex(), 0, 0);
+		endInsertRows();
 	}
 
+	beginResetModel();
+
 	list.clear();
 
 	for (const auto& a : Anime::db.items) {
@@ -226,10 +213,7 @@
 		}
 	}
 
-	if (has_children)
-		endResetModel();
-	else
-		endInsertRows();
+	endResetModel();
 }
 
 int AnimeListPage::VisibleColumnsCount() const {
@@ -258,6 +242,19 @@
 	tree_view->setColumnHidden(AnimeListPageModel::AL_NOTES, true);
 }
 
+void AnimeListPage::UpdateAnime(int id) {
+	QThreadPool::globalInstance()->start([this, id] {
+		Services::UpdateAnimeEntry(id);
+		Refresh();
+	});
+}
+
+void AnimeListPage::RemoveAnime(int id) {
+	Anime::Anime& anime = Anime::db.items[id];
+	anime.RemoveFromUserList();
+	Refresh();
+}
+
 void AnimeListPage::DisplayColumnHeaderMenu() {
 	QMenu* menu = new QMenu(this);
 	menu->setAttribute(Qt::WA_DeleteOnClose);
@@ -301,32 +298,35 @@
 	menu->setTitle(tr("Column visibility"));
 	menu->setToolTipsVisible(true);
 
+	AnimeListPageModel* source_model =
+	    reinterpret_cast<AnimeListPageModel*>(sort_models[tab_bar->currentIndex()]->sourceModel());
 	const QItemSelection selection =
 	    sort_models[tab_bar->currentIndex()]->mapSelectionToSource(tree_view->selectionModel()->selection());
-	if (!selection.indexes().first().isValid()) {
-		return;
+
+	std::set<Anime::Anime*> animes;
+	for (const auto& index : selection.indexes()) {
+		if (!index.isValid())
+			continue;
+		Anime::Anime* anime = source_model->GetAnimeFromIndex(index);
+		if (anime)
+			animes.insert(anime);
 	}
 
-	QAction* action = menu->addAction(tr("Information"), [this, selection] {
-		AnimeListPageModel* source_model =
-		    reinterpret_cast<AnimeListPageModel*>(sort_models[tab_bar->currentIndex()]->sourceModel());
-		const QModelIndex index = source_model->index(selection.indexes().first().row());
-		Anime::Anime* anime = source_model->GetAnimeFromIndex(index);
-		if (!anime) {
-			return;
-		}
+	QAction* action = menu->addAction(tr("Information"), [this, animes] {
+		for (auto& anime : animes) {
+			InformationDialog* dialog = new InformationDialog(
+			    *anime, [this, anime] { UpdateAnime(anime->GetId()); }, this);
 
-		InformationDialog* dialog = new InformationDialog(
-		    *anime,
-		    [this, anime] {
-			    Services::UpdateAnimeEntry(anime->GetId());
-			    Refresh();
-		    },
-		    this);
-
-		dialog->show();
-		dialog->raise();
-		dialog->activateWindow();
+			dialog->show();
+			dialog->raise();
+			dialog->activateWindow();
+		}
+	});
+	menu->addSeparator();
+	action = menu->addAction(tr("Delete from list..."), [this, animes] {
+		for (auto& anime : animes) {
+			RemoveAnime(anime->GetId());
+		}
 	});
 	menu->popup(QCursor::pos());
 }
@@ -346,12 +346,7 @@
 	Anime::Anime* anime = source_model->GetAnimeFromIndex(index);
 
 	InformationDialog* dialog = new InformationDialog(
-	    *anime,
-	    [this, anime] {
-		    Services::UpdateAnimeEntry(anime->GetId());
-		    Refresh();
-	    },
-	    this);
+	    *anime, [this, anime] { UpdateAnime(anime->GetId()); }, this);
 
 	dialog->show();
 	dialog->raise();