Mercurial > minori
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 76:3364fadc8a36 | 77:6f7385bd334c |
|---|---|
| 24 #include <QMenu> | 24 #include <QMenu> |
| 25 #include <QProgressBar> | 25 #include <QProgressBar> |
| 26 #include <QShortcut> | 26 #include <QShortcut> |
| 27 #include <QStylePainter> | 27 #include <QStylePainter> |
| 28 #include <QStyledItemDelegate> | 28 #include <QStyledItemDelegate> |
| 29 #include <cmath> | 29 #include <QThreadPool> |
| 30 #include <set> | |
| 30 | 31 |
| 31 AnimeListPageDelegate::AnimeListPageDelegate(QObject* parent) : QStyledItemDelegate(parent) { | 32 AnimeListPageDelegate::AnimeListPageDelegate(QObject* parent) : QStyledItemDelegate(parent) { |
| 32 } | 33 } |
| 33 | 34 |
| 34 QWidget* AnimeListPageDelegate::createEditor(QWidget*, const QStyleOptionViewItem&, const QModelIndex&) const { | 35 QWidget* AnimeListPageDelegate::createEditor(QWidget*, const QStyleOptionViewItem&, const QModelIndex&) const { |
| 189 break; | 190 break; |
| 190 } | 191 } |
| 191 return QVariant(); | 192 return QVariant(); |
| 192 } | 193 } |
| 193 | 194 |
| 194 void AnimeListPageModel::UpdateAnime(int id) { | |
| 195 /* meh... it might be better to just reinit the entire list */ | |
| 196 int i = 0; | |
| 197 for (const auto& a : Anime::db.items) { | |
| 198 if (a.second.IsInUserList() && a.first == id && a.second.GetUserStatus() == status) { | |
| 199 emit dataChanged(index(i), index(i)); | |
| 200 } | |
| 201 i++; | |
| 202 } | |
| 203 } | |
| 204 | |
| 205 Anime::Anime* AnimeListPageModel::GetAnimeFromIndex(QModelIndex index) { | 195 Anime::Anime* AnimeListPageModel::GetAnimeFromIndex(QModelIndex index) { |
| 206 return &list.at(index.row()); | 196 return &list.at(index.row()); |
| 207 } | 197 } |
| 208 | 198 |
| 209 void AnimeListPageModel::RefreshList() { | 199 void AnimeListPageModel::RefreshList() { |
| 210 bool has_children = !!rowCount(index(0)); | 200 bool has_children = !!rowCount(index(0)); |
| 211 if (has_children) | 201 if (!has_children) { |
| 212 beginResetModel(); | 202 beginInsertRows(QModelIndex(), 0, 0); |
| 213 else { | 203 endInsertRows(); |
| 214 int count = 0; | 204 } |
| 215 for (const auto& a : Anime::db.items) | 205 |
| 216 if (a.second.IsInUserList() && a.second.GetUserStatus() == status) | 206 beginResetModel(); |
| 217 count++; | |
| 218 beginInsertRows(index(0), 0, count - 1); | |
| 219 } | |
| 220 | 207 |
| 221 list.clear(); | 208 list.clear(); |
| 222 | 209 |
| 223 for (const auto& a : Anime::db.items) { | 210 for (const auto& a : Anime::db.items) { |
| 224 if (a.second.IsInUserList() && a.second.GetUserStatus() == status) { | 211 if (a.second.IsInUserList() && a.second.GetUserStatus() == status) { |
| 225 list.push_back(a.second); | 212 list.push_back(a.second); |
| 226 } | 213 } |
| 227 } | 214 } |
| 228 | 215 |
| 229 if (has_children) | 216 endResetModel(); |
| 230 endResetModel(); | |
| 231 else | |
| 232 endInsertRows(); | |
| 233 } | 217 } |
| 234 | 218 |
| 235 int AnimeListPage::VisibleColumnsCount() const { | 219 int AnimeListPage::VisibleColumnsCount() const { |
| 236 int count = 0; | 220 int count = 0; |
| 237 | 221 |
| 256 tree_view->setColumnHidden(AnimeListPageModel::AL_COMPLETED, true); | 240 tree_view->setColumnHidden(AnimeListPageModel::AL_COMPLETED, true); |
| 257 tree_view->setColumnHidden(AnimeListPageModel::AL_UPDATED, true); | 241 tree_view->setColumnHidden(AnimeListPageModel::AL_UPDATED, true); |
| 258 tree_view->setColumnHidden(AnimeListPageModel::AL_NOTES, true); | 242 tree_view->setColumnHidden(AnimeListPageModel::AL_NOTES, true); |
| 259 } | 243 } |
| 260 | 244 |
| 245 void AnimeListPage::UpdateAnime(int id) { | |
| 246 QThreadPool::globalInstance()->start([this, id] { | |
| 247 Services::UpdateAnimeEntry(id); | |
| 248 Refresh(); | |
| 249 }); | |
| 250 } | |
| 251 | |
| 252 void AnimeListPage::RemoveAnime(int id) { | |
| 253 Anime::Anime& anime = Anime::db.items[id]; | |
| 254 anime.RemoveFromUserList(); | |
| 255 Refresh(); | |
| 256 } | |
| 257 | |
| 261 void AnimeListPage::DisplayColumnHeaderMenu() { | 258 void AnimeListPage::DisplayColumnHeaderMenu() { |
| 262 QMenu* menu = new QMenu(this); | 259 QMenu* menu = new QMenu(this); |
| 263 menu->setAttribute(Qt::WA_DeleteOnClose); | 260 menu->setAttribute(Qt::WA_DeleteOnClose); |
| 264 menu->setTitle(tr("Column visibility")); | 261 menu->setTitle(tr("Column visibility")); |
| 265 menu->setToolTipsVisible(true); | 262 menu->setToolTipsVisible(true); |
| 299 QMenu* menu = new QMenu(this); | 296 QMenu* menu = new QMenu(this); |
| 300 menu->setAttribute(Qt::WA_DeleteOnClose); | 297 menu->setAttribute(Qt::WA_DeleteOnClose); |
| 301 menu->setTitle(tr("Column visibility")); | 298 menu->setTitle(tr("Column visibility")); |
| 302 menu->setToolTipsVisible(true); | 299 menu->setToolTipsVisible(true); |
| 303 | 300 |
| 301 AnimeListPageModel* source_model = | |
| 302 reinterpret_cast<AnimeListPageModel*>(sort_models[tab_bar->currentIndex()]->sourceModel()); | |
| 304 const QItemSelection selection = | 303 const QItemSelection selection = |
| 305 sort_models[tab_bar->currentIndex()]->mapSelectionToSource(tree_view->selectionModel()->selection()); | 304 sort_models[tab_bar->currentIndex()]->mapSelectionToSource(tree_view->selectionModel()->selection()); |
| 306 if (!selection.indexes().first().isValid()) { | 305 |
| 307 return; | 306 std::set<Anime::Anime*> animes; |
| 308 } | 307 for (const auto& index : selection.indexes()) { |
| 309 | 308 if (!index.isValid()) |
| 310 QAction* action = menu->addAction(tr("Information"), [this, selection] { | 309 continue; |
| 311 AnimeListPageModel* source_model = | |
| 312 reinterpret_cast<AnimeListPageModel*>(sort_models[tab_bar->currentIndex()]->sourceModel()); | |
| 313 const QModelIndex index = source_model->index(selection.indexes().first().row()); | |
| 314 Anime::Anime* anime = source_model->GetAnimeFromIndex(index); | 310 Anime::Anime* anime = source_model->GetAnimeFromIndex(index); |
| 315 if (!anime) { | 311 if (anime) |
| 316 return; | 312 animes.insert(anime); |
| 317 } | 313 } |
| 318 | 314 |
| 319 InformationDialog* dialog = new InformationDialog( | 315 QAction* action = menu->addAction(tr("Information"), [this, animes] { |
| 320 *anime, | 316 for (auto& anime : animes) { |
| 321 [this, anime] { | 317 InformationDialog* dialog = new InformationDialog( |
| 322 Services::UpdateAnimeEntry(anime->GetId()); | 318 *anime, [this, anime] { UpdateAnime(anime->GetId()); }, this); |
| 323 Refresh(); | 319 |
| 324 }, | 320 dialog->show(); |
| 325 this); | 321 dialog->raise(); |
| 326 | 322 dialog->activateWindow(); |
| 327 dialog->show(); | 323 } |
| 328 dialog->raise(); | 324 }); |
| 329 dialog->activateWindow(); | 325 menu->addSeparator(); |
| 326 action = menu->addAction(tr("Delete from list..."), [this, animes] { | |
| 327 for (auto& anime : animes) { | |
| 328 RemoveAnime(anime->GetId()); | |
| 329 } | |
| 330 }); | 330 }); |
| 331 menu->popup(QCursor::pos()); | 331 menu->popup(QCursor::pos()); |
| 332 } | 332 } |
| 333 | 333 |
| 334 void AnimeListPage::ItemDoubleClicked() { | 334 void AnimeListPage::ItemDoubleClicked() { |
| 344 | 344 |
| 345 const QModelIndex index = source_model->index(selection.indexes().first().row()); | 345 const QModelIndex index = source_model->index(selection.indexes().first().row()); |
| 346 Anime::Anime* anime = source_model->GetAnimeFromIndex(index); | 346 Anime::Anime* anime = source_model->GetAnimeFromIndex(index); |
| 347 | 347 |
| 348 InformationDialog* dialog = new InformationDialog( | 348 InformationDialog* dialog = new InformationDialog( |
| 349 *anime, | 349 *anime, [this, anime] { UpdateAnime(anime->GetId()); }, this); |
| 350 [this, anime] { | |
| 351 Services::UpdateAnimeEntry(anime->GetId()); | |
| 352 Refresh(); | |
| 353 }, | |
| 354 this); | |
| 355 | 350 |
| 356 dialog->show(); | 351 dialog->show(); |
| 357 dialog->raise(); | 352 dialog->raise(); |
| 358 dialog->activateWindow(); | 353 dialog->activateWindow(); |
| 359 } | 354 } |
