2
|
1 #include <chrono>
|
|
2 #include <string>
|
|
3 #include <vector>
|
|
4 #include <cmath>
|
|
5 #include "window.h"
|
|
6 #include "anilist.h"
|
|
7 #include "config.h"
|
|
8 #include "anime.h"
|
|
9 #include "date.h"
|
|
10 #include "time_utils.h"
|
|
11 #include "information.h"
|
|
12 #include "ui_utils.h"
|
|
13
|
|
14 std::map<enum AnimeWatchingStatus, std::string> AnimeWatchingToStringMap = {
|
|
15 {CURRENT, "Watching"},
|
|
16 {PLANNING, "Planning"},
|
|
17 {COMPLETED, "Completed"},
|
|
18 {DROPPED, "Dropped"},
|
|
19 {PAUSED, "On hold"},
|
|
20 {REPEATING, "Rewatching"}
|
|
21 };
|
|
22
|
|
23 std::map<enum AnimeAiringStatus, std::string> AnimeAiringToStringMap = {
|
|
24 {FINISHED, "Finished"},
|
|
25 {RELEASING, "Airing"},
|
|
26 {NOT_YET_RELEASED, "Not aired yet"},
|
|
27 {CANCELLED, "Cancelled"},
|
|
28 {HIATUS, "On hiatus"}
|
|
29 };
|
|
30
|
|
31 std::map<enum AnimeSeason, std::string> AnimeSeasonToStringMap = {
|
|
32 {UNKNOWN, "Unknown"},
|
|
33 {WINTER, "Winter"},
|
|
34 {SPRING, "Spring"},
|
|
35 {SUMMER, "Summer"},
|
|
36 {FALL, "Fall"}
|
|
37 };
|
|
38
|
|
39 std::map<enum AnimeFormat, std::string> AnimeFormatToStringMap = {
|
|
40 {TV, "TV"},
|
|
41 {TV_SHORT, "TV short"},
|
|
42 {MOVIE, "Movie"},
|
|
43 {SPECIAL, "Special"},
|
|
44 {OVA, "OVA"},
|
|
45 {ONA, "ONA"},
|
|
46 {MUSIC, "Music video"},
|
|
47 /* these should NEVER be in the list. naybe we should
|
|
48 remove them? */
|
|
49 {MANGA, "Manga"},
|
|
50 {NOVEL, "Novel"},
|
|
51 {ONE_SHOT, "One-shot"}
|
|
52 };
|
|
53
|
|
54 Anime::Anime() {}
|
|
55 Anime::Anime(const Anime& a) {
|
|
56 status = a.status;
|
|
57 progress = a.progress;
|
|
58 score = a.score;
|
|
59 started = a.started;
|
|
60 completed = a.completed;
|
|
61 updated = a.updated;
|
|
62 notes = a.notes;
|
|
63 id = a.id;
|
|
64 title = a.title;
|
|
65 episodes = a.episodes;
|
|
66 airing = a.airing;
|
|
67 air_date = a.air_date;
|
|
68 genres = a.genres;
|
|
69 producers = a.producers;
|
|
70 type = a.type;
|
|
71 season = a.season;
|
|
72 audience_score = a.audience_score;
|
|
73 synopsis = a.synopsis;
|
|
74 duration = a.duration;
|
|
75 }
|
|
76
|
|
77 void AnimeList::Add(Anime& anime) {
|
|
78 if (anime_id_to_anime.contains(anime.id))
|
|
79 return;
|
|
80 anime_list.push_back(anime);
|
|
81 anime_id_to_anime.emplace(anime.id, &anime);
|
|
82 }
|
|
83
|
|
84 void AnimeList::Insert(size_t pos, Anime& anime) {
|
|
85 if (anime_id_to_anime.contains(anime.id))
|
|
86 return;
|
|
87 anime_list.insert(anime_list.begin()+pos, anime);
|
|
88 anime_id_to_anime.emplace(anime.id, &anime);
|
|
89 }
|
|
90
|
|
91 void AnimeList::Delete(size_t index) {
|
|
92 anime_list.erase(anime_list.begin()+index);
|
|
93 }
|
|
94
|
|
95 void AnimeList::Clear() {
|
|
96 anime_list.clear();
|
|
97 }
|
|
98
|
|
99 size_t AnimeList::Size() const {
|
|
100 return anime_list.size();
|
|
101 }
|
|
102
|
|
103 std::vector<Anime>::iterator AnimeList::begin() noexcept {
|
|
104 return anime_list.begin();
|
|
105 }
|
|
106
|
|
107 std::vector<Anime>::iterator AnimeList::end() noexcept {
|
|
108 return anime_list.end();
|
|
109 }
|
|
110
|
|
111 std::vector<Anime>::const_iterator AnimeList::cbegin() noexcept {
|
|
112 return anime_list.cbegin();
|
|
113 }
|
|
114
|
|
115 std::vector<Anime>::const_iterator AnimeList::cend() noexcept {
|
|
116 return anime_list.cend();
|
|
117 }
|
|
118
|
|
119 AnimeList::AnimeList() {}
|
|
120 AnimeList::AnimeList(const AnimeList& l) {
|
|
121 for (int i = 0; i < l.Size(); i++) {
|
|
122 anime_list.push_back(Anime(l[i]));
|
|
123 }
|
|
124 name = l.name;
|
|
125 }
|
|
126
|
|
127 AnimeList::~AnimeList() {
|
|
128 anime_list.clear();
|
|
129 anime_list.shrink_to_fit();
|
|
130 }
|
|
131
|
|
132 Anime* AnimeList::AnimeById(int id) {
|
|
133 return anime_id_to_anime.contains(id) ? anime_id_to_anime[id] : nullptr;
|
|
134 }
|
|
135
|
|
136 bool AnimeList::AnimeInList(int id) {
|
|
137 return anime_id_to_anime.contains(id);
|
|
138 }
|
|
139
|
|
140 int AnimeList::GetAnimeIndex(Anime& anime) const {
|
|
141 for (int i = 0; i < Size(); i++) {
|
|
142 if (&anime_list.at(i) == &anime) { // lazy
|
|
143 return i;
|
|
144 }
|
|
145 }
|
|
146 return -1;
|
|
147 }
|
|
148
|
|
149 Anime& AnimeList::operator[](std::size_t index) {
|
|
150 return anime_list.at(index);
|
|
151 }
|
|
152
|
|
153 const Anime& AnimeList::operator[](std::size_t index) const {
|
|
154 return anime_list.at(index);
|
|
155 }
|
|
156
|
|
157 /* ------------------------------------------------------------------------- */
|
|
158
|
|
159 /* Thank you qBittorrent for having a great example of a
|
|
160 widget model. */
|
|
161 AnimeListWidgetModel::AnimeListWidgetModel (QWidget* parent, AnimeList* alist)
|
|
162 : QAbstractListModel(parent)
|
|
163 , list(*alist) {
|
|
164 return;
|
|
165 }
|
|
166
|
|
167 int AnimeListWidgetModel::rowCount(const QModelIndex& parent) const {
|
|
168 return list.Size();
|
|
169 }
|
|
170
|
|
171 int AnimeListWidgetModel::columnCount(const QModelIndex& parent) const {
|
|
172 return NB_COLUMNS;
|
|
173 }
|
|
174
|
|
175 QVariant AnimeListWidgetModel::headerData(const int section, const Qt::Orientation orientation, const int role) const {
|
|
176 if (role == Qt::DisplayRole) {
|
|
177 switch (section) {
|
|
178 case AL_TITLE:
|
|
179 return tr("Anime title");
|
|
180 case AL_PROGRESS:
|
|
181 return tr("Progress");
|
|
182 case AL_TYPE:
|
|
183 return tr("Type");
|
|
184 case AL_SCORE:
|
|
185 return tr("Score");
|
|
186 case AL_SEASON:
|
|
187 return tr("Season");
|
|
188 case AL_STARTED:
|
|
189 return tr("Date started");
|
|
190 case AL_COMPLETED:
|
|
191 return tr("Date completed");
|
|
192 case AL_NOTES:
|
|
193 return tr("Notes");
|
|
194 case AL_AVG_SCORE:
|
|
195 return tr("Average score");
|
|
196 case AL_UPDATED:
|
|
197 return tr("Last updated");
|
|
198 default:
|
|
199 return {};
|
|
200 }
|
|
201 } else if (role == Qt::TextAlignmentRole) {
|
|
202 switch (section) {
|
|
203 case AL_TITLE:
|
|
204 case AL_NOTES:
|
|
205 return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
|
|
206 case AL_PROGRESS:
|
|
207 case AL_TYPE:
|
|
208 case AL_SCORE:
|
|
209 case AL_AVG_SCORE:
|
|
210 return QVariant(Qt::AlignCenter | Qt::AlignVCenter);
|
|
211 case AL_SEASON:
|
|
212 case AL_STARTED:
|
|
213 case AL_COMPLETED:
|
|
214 case AL_UPDATED:
|
|
215 return QVariant(Qt::AlignRight | Qt::AlignVCenter);
|
|
216 default:
|
|
217 return QAbstractListModel::headerData(section, orientation, role);
|
|
218 }
|
|
219 }
|
|
220 return QAbstractListModel::headerData(section, orientation, role);
|
|
221 }
|
|
222
|
|
223 Anime* AnimeListWidgetModel::GetAnimeFromIndex(const QModelIndex& index) {
|
|
224 return (index.isValid()) ? &(list[index.row()]) : nullptr;
|
|
225 }
|
|
226
|
|
227 QVariant AnimeListWidgetModel::data(const QModelIndex& index, int role) const {
|
|
228 if (!index.isValid())
|
|
229 return QVariant();
|
|
230 if (role == Qt::DisplayRole) {
|
|
231 switch (index.column()) {
|
|
232 case AL_TITLE:
|
|
233 return QString::fromWCharArray(list[index.row()].title.english.c_str());
|
|
234 case AL_PROGRESS:
|
|
235 return list[index.row()].progress;
|
|
236 case AL_SCORE:
|
|
237 return list[index.row()].score;
|
|
238 case AL_TYPE:
|
|
239 return QString::fromStdString(AnimeFormatToStringMap[list[index.row()].type]);
|
|
240 case AL_SEASON:
|
|
241 return QString::fromStdString(AnimeSeasonToStringMap[list[index.row()].season]) + " " + QString::number(list[index.row()].air_date.GetYear());
|
|
242 case AL_AVG_SCORE:
|
|
243 return list[index.row()].audience_score;
|
|
244 case AL_STARTED:
|
|
245 return list[index.row()].started.GetAsQDate();
|
|
246 case AL_COMPLETED:
|
|
247 return list[index.row()].completed.GetAsQDate();
|
|
248 case AL_UPDATED: {
|
|
249 if (list[index.row()].updated == 0)
|
|
250 return QString("-");
|
|
251 Time::Duration duration(Time::GetSystemTime() - list[index.row()].updated);
|
|
252 return QString::fromStdString(duration.AsRelativeString());
|
|
253 }
|
|
254 case AL_NOTES:
|
|
255 return QString::fromWCharArray(list[index.row()].notes.c_str());
|
|
256 default:
|
|
257 return "";
|
|
258 }
|
|
259 } else if (role == Qt::TextAlignmentRole) {
|
|
260 switch (index.column()) {
|
|
261 case AL_TITLE:
|
|
262 case AL_NOTES:
|
|
263 return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
|
|
264 case AL_PROGRESS:
|
|
265 case AL_TYPE:
|
|
266 case AL_SCORE:
|
|
267 case AL_AVG_SCORE:
|
|
268 return QVariant(Qt::AlignCenter | Qt::AlignVCenter);
|
|
269 case AL_SEASON:
|
|
270 case AL_STARTED:
|
|
271 case AL_COMPLETED:
|
|
272 case AL_UPDATED:
|
|
273 return QVariant(Qt::AlignRight | Qt::AlignVCenter);
|
|
274 default:
|
|
275 break;
|
|
276 }
|
|
277 }
|
|
278 return QVariant();
|
|
279 }
|
|
280
|
|
281 void AnimeListWidgetModel::UpdateAnime(Anime& anime) {
|
|
282 int i = list.GetAnimeIndex(anime);
|
|
283 emit dataChanged(index(i), index(i));
|
|
284 }
|
|
285
|
|
286 /* Most of this stuff is const and/or should be edited in the Information dialog
|
|
287
|
|
288 bool AnimeListWidgetModel::setData(const QModelIndex &index, const QVariant &value, int role) {
|
|
289 if (!index.isValid() || role != Qt::DisplayRole)
|
|
290 return false;
|
|
291
|
|
292 Anime* const anime = &list[index.row()];
|
|
293
|
|
294 switch (index.column()) {
|
|
295 case AL_TITLE:
|
|
296 break;
|
|
297 case AL_CATEGORY:
|
|
298 break;
|
|
299 default:
|
|
300 return false;
|
|
301 }
|
|
302
|
|
303 return true;
|
|
304 }
|
|
305 */
|
|
306
|
|
307 int AnimeListWidget::VisibleColumnsCount() const {
|
|
308 int count = 0;
|
|
309
|
|
310 for (int i = 0, end = header()->count(); i < end; i++)
|
|
311 {
|
|
312 if (!isColumnHidden(i))
|
|
313 count++;
|
|
314 }
|
|
315
|
|
316 return count;
|
|
317 }
|
|
318
|
|
319 void AnimeListWidget::SetColumnDefaults() {
|
|
320 setColumnHidden(AnimeListWidgetModel::AL_SEASON, false);
|
|
321 setColumnHidden(AnimeListWidgetModel::AL_TYPE, false);
|
|
322 setColumnHidden(AnimeListWidgetModel::AL_UPDATED, false);
|
|
323 setColumnHidden(AnimeListWidgetModel::AL_PROGRESS, false);
|
|
324 setColumnHidden(AnimeListWidgetModel::AL_SCORE, false);
|
|
325 setColumnHidden(AnimeListWidgetModel::AL_TITLE, false);
|
|
326 setColumnHidden(AnimeListWidgetModel::AL_AVG_SCORE, true);
|
|
327 setColumnHidden(AnimeListWidgetModel::AL_STARTED, true);
|
|
328 setColumnHidden(AnimeListWidgetModel::AL_COMPLETED, true);
|
|
329 setColumnHidden(AnimeListWidgetModel::AL_UPDATED, true);
|
|
330 setColumnHidden(AnimeListWidgetModel::AL_NOTES, true);
|
|
331 }
|
|
332
|
|
333 void AnimeListWidget::DisplayColumnHeaderMenu() {
|
|
334 QMenu *menu = new QMenu(this);
|
|
335 menu->setAttribute(Qt::WA_DeleteOnClose);
|
|
336 menu->setTitle(tr("Column visibility"));
|
|
337 menu->setToolTipsVisible(true);
|
|
338
|
|
339 for (int i = 0; i < AnimeListWidgetModel::NB_COLUMNS; i++)
|
|
340 {
|
|
341 if (i == AnimeListWidgetModel::AL_TITLE)
|
|
342 continue;
|
|
343 const auto column_name = model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString();
|
|
344 QAction *action = menu->addAction(column_name, this, [this, i](const bool checked) {
|
|
345 if (!checked && (VisibleColumnsCount() <= 1))
|
|
346 return;
|
|
347
|
|
348 setColumnHidden(i, !checked);
|
|
349
|
|
350 if (checked && (columnWidth(i) <= 5))
|
|
351 resizeColumnToContents(i);
|
|
352
|
|
353 // SaveSettings();
|
|
354 });
|
|
355 action->setCheckable(true);
|
|
356 action->setChecked(!isColumnHidden(i));
|
|
357 }
|
|
358
|
|
359 menu->addSeparator();
|
|
360 QAction *resetAction = menu->addAction(tr("Reset to defaults"), this, [this]()
|
|
361 {
|
|
362 for (int i = 0, count = header()->count(); i < count; ++i)
|
|
363 {
|
|
364 SetColumnDefaults();
|
|
365 }
|
|
366 // SaveSettings();
|
|
367 });
|
|
368
|
|
369 menu->popup(QCursor::pos());
|
|
370 }
|
|
371
|
|
372 void AnimeListWidget::DisplayListMenu() {
|
|
373 /* throw out any other garbage */
|
|
374 const QModelIndexList selected_items = selectionModel()->selectedRows();
|
|
375 if (selected_items.size() != 1 || !selected_items.first().isValid())
|
|
376 return;
|
|
377
|
|
378 const QModelIndex index = model->index(selected_items.first().row());
|
|
379 Anime* anime = model->GetAnimeFromIndex(index);
|
|
380 if (!anime)
|
|
381 return;
|
|
382
|
|
383 }
|
|
384
|
|
385 void AnimeListWidget::ItemDoubleClicked() {
|
|
386 /* throw out any other garbage */
|
|
387 const QModelIndexList selected_items = selectionModel()->selectedRows();
|
|
388 if (selected_items.size() != 1 || !selected_items.first().isValid())
|
|
389 return;
|
|
390
|
|
391 /* TODO: after we implement our sort model, we have to use mapToSource here... */
|
|
392 const QModelIndex index = model->index(selected_items.first().row());
|
|
393 Anime* anime = model->GetAnimeFromIndex(index);
|
|
394 if (!anime)
|
|
395 return;
|
|
396
|
|
397 InformationDialog* dialog = new InformationDialog(*anime, model, this);
|
|
398
|
|
399 dialog->show();
|
|
400 dialog->raise();
|
|
401 dialog->activateWindow();
|
|
402 }
|
|
403
|
|
404 AnimeListWidget::AnimeListWidget(QWidget* parent, AnimeList* alist)
|
|
405 : QTreeView(parent) {
|
|
406 model = new AnimeListWidgetModel(parent, alist);
|
|
407 setModel(model);
|
|
408 setObjectName("listwidget");
|
|
409 setStyleSheet("QTreeView#listwidget{border-top:0px;}");
|
|
410 setUniformRowHeights(true);
|
|
411 setAllColumnsShowFocus(false);
|
|
412 setSortingEnabled(true);
|
|
413 setSelectionMode(QAbstractItemView::ExtendedSelection);
|
|
414 setItemsExpandable(false);
|
|
415 setRootIsDecorated(false);
|
|
416 setContextMenuPolicy(Qt::CustomContextMenu);
|
|
417 connect(this, &QAbstractItemView::doubleClicked, this, &ItemDoubleClicked);
|
|
418 connect(this, &QWidget::customContextMenuRequested, this, &DisplayListMenu);
|
|
419
|
|
420 /* Enter & return keys */
|
|
421 connect(new QShortcut(Qt::Key_Return, this, nullptr, nullptr, Qt::WidgetShortcut),
|
|
422 &QShortcut::activated, this, &ItemDoubleClicked);
|
|
423
|
|
424 connect(new QShortcut(Qt::Key_Enter, this, nullptr, nullptr, Qt::WidgetShortcut),
|
|
425 &QShortcut::activated, this, &ItemDoubleClicked);
|
|
426
|
|
427 header()->setStretchLastSection(false);
|
|
428 header()->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
429 connect(header(), &QWidget::customContextMenuRequested, this, &DisplayColumnHeaderMenu);
|
|
430 // if(!session.config.anime_list.columns) {
|
|
431 SetColumnDefaults();
|
|
432 // }
|
|
433 }
|
|
434
|
|
435 AnimeListPage::AnimeListPage(QWidget* parent) : QTabWidget (parent) {
|
|
436 setDocumentMode(false);
|
|
437 SyncAnimeList();
|
|
438 for (AnimeList& list : anime_lists) {
|
|
439 addTab(new AnimeListWidget(this, &list), QString::fromWCharArray(list.name.c_str()));
|
|
440 }
|
|
441 }
|
|
442
|
|
443 void AnimeListPage::SyncAnimeList() {
|
|
444 switch (session.config.service) {
|
|
445 case ANILIST: {
|
|
446 AniList anilist = AniList();
|
|
447 anilist.Authorize();
|
|
448 session.config.anilist.user_id = anilist.GetUserId(session.config.anilist.username);
|
|
449 FreeAnimeList();
|
|
450 anilist.UpdateAnimeList(&anime_lists, session.config.anilist.user_id);
|
|
451 break;
|
|
452 }
|
|
453 }
|
|
454 }
|
|
455
|
|
456 void AnimeListPage::FreeAnimeList() {
|
|
457 if (anime_lists.size() > 0) {
|
|
458 /* FIXME: we may not need this, but to prevent memleaks
|
|
459 we should keep it until we're sure we don't */
|
|
460 for (auto& list : anime_lists) {
|
|
461 list.Clear();
|
|
462 }
|
|
463 anime_lists.clear();
|
|
464 }
|
|
465 }
|
|
466
|
|
467 int AnimeListPage::GetTotalAnimeAmount() {
|
|
468 int total = 0;
|
|
469 for (auto& list : anime_lists) {
|
|
470 total += list.Size();
|
|
471 }
|
|
472 return total;
|
|
473 }
|
|
474
|
|
475 int AnimeListPage::GetTotalEpisodeAmount() {
|
|
476 /* FIXME: this also needs to take into account rewatches... */
|
|
477 int total = 0;
|
|
478 for (auto& list : anime_lists) {
|
|
479 for (auto& anime : list) {
|
|
480 total += anime.progress;
|
|
481 }
|
|
482 }
|
|
483 return total;
|
|
484 }
|
|
485
|
|
486 /* Returns the total watched amount in minutes. */
|
|
487 int AnimeListPage::GetTotalWatchedAmount() {
|
|
488 int total = 0;
|
|
489 for (auto& list : anime_lists) {
|
|
490 for (auto& anime : list) {
|
|
491 total += anime.duration*anime.progress;
|
|
492 }
|
|
493 }
|
|
494 return total;
|
|
495 }
|
|
496
|
|
497 /* Returns the total planned amount in minutes.
|
|
498 Note that we should probably limit progress to the
|
|
499 amount of episodes, as AniList will let you
|
|
500 set episode counts up to 32768. But that should
|
|
501 rather be handled elsewhere. */
|
|
502 int AnimeListPage::GetTotalPlannedAmount() {
|
|
503 int total = 0;
|
|
504 for (auto& list : anime_lists) {
|
|
505 for (auto& anime : list) {
|
|
506 total += anime.duration*(anime.episodes-anime.progress);
|
|
507 }
|
|
508 }
|
|
509 return total;
|
|
510 }
|
|
511
|
|
512 double AnimeListPage::GetAverageScore() {
|
|
513 double avg = 0;
|
|
514 int amt = 0;
|
|
515 for (auto& list : anime_lists) {
|
|
516 for (auto& anime : list) {
|
|
517 avg += anime.score;
|
|
518 if (anime.score != 0)
|
|
519 amt++;
|
|
520 }
|
|
521 }
|
|
522 return avg/amt;
|
|
523 }
|
|
524
|
|
525 double AnimeListPage::GetScoreDeviation() {
|
|
526 double squares_sum = 0, avg = GetAverageScore();
|
|
527 int amt = 0;
|
|
528 for (auto& list : anime_lists) {
|
|
529 for (auto& anime : list) {
|
|
530 if (anime.score != 0) {
|
|
531 squares_sum += std::pow((double)anime.score - avg, 2);
|
|
532 amt++;
|
|
533 }
|
|
534 }
|
|
535 }
|
|
536 return (amt > 0) ? std::sqrt(squares_sum / amt) : 0;
|
|
537 }
|
|
538
|
|
539 #include "moc_anime.cpp"
|