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) {
|
3
|
121 for (unsigned long long i = 0; i < l.Size(); i++) {
|
2
|
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 {
|
3
|
141 for (unsigned long long i = 0; i < Size(); i++) {
|
2
|
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();
|
3
|
169 (void)(parent);
|
2
|
170 }
|
|
171
|
|
172 int AnimeListWidgetModel::columnCount(const QModelIndex& parent) const {
|
|
173 return NB_COLUMNS;
|
3
|
174 (void)(parent);
|
2
|
175 }
|
|
176
|
|
177 QVariant AnimeListWidgetModel::headerData(const int section, const Qt::Orientation orientation, const int role) const {
|
|
178 if (role == Qt::DisplayRole) {
|
|
179 switch (section) {
|
|
180 case AL_TITLE:
|
|
181 return tr("Anime title");
|
|
182 case AL_PROGRESS:
|
|
183 return tr("Progress");
|
|
184 case AL_TYPE:
|
|
185 return tr("Type");
|
|
186 case AL_SCORE:
|
|
187 return tr("Score");
|
|
188 case AL_SEASON:
|
|
189 return tr("Season");
|
|
190 case AL_STARTED:
|
|
191 return tr("Date started");
|
|
192 case AL_COMPLETED:
|
|
193 return tr("Date completed");
|
|
194 case AL_NOTES:
|
|
195 return tr("Notes");
|
|
196 case AL_AVG_SCORE:
|
|
197 return tr("Average score");
|
|
198 case AL_UPDATED:
|
|
199 return tr("Last updated");
|
|
200 default:
|
|
201 return {};
|
|
202 }
|
|
203 } else if (role == Qt::TextAlignmentRole) {
|
|
204 switch (section) {
|
|
205 case AL_TITLE:
|
|
206 case AL_NOTES:
|
|
207 return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
|
|
208 case AL_PROGRESS:
|
|
209 case AL_TYPE:
|
|
210 case AL_SCORE:
|
|
211 case AL_AVG_SCORE:
|
|
212 return QVariant(Qt::AlignCenter | Qt::AlignVCenter);
|
|
213 case AL_SEASON:
|
|
214 case AL_STARTED:
|
|
215 case AL_COMPLETED:
|
|
216 case AL_UPDATED:
|
|
217 return QVariant(Qt::AlignRight | Qt::AlignVCenter);
|
|
218 default:
|
|
219 return QAbstractListModel::headerData(section, orientation, role);
|
|
220 }
|
|
221 }
|
|
222 return QAbstractListModel::headerData(section, orientation, role);
|
|
223 }
|
|
224
|
|
225 Anime* AnimeListWidgetModel::GetAnimeFromIndex(const QModelIndex& index) {
|
|
226 return (index.isValid()) ? &(list[index.row()]) : nullptr;
|
|
227 }
|
|
228
|
|
229 QVariant AnimeListWidgetModel::data(const QModelIndex& index, int role) const {
|
|
230 if (!index.isValid())
|
|
231 return QVariant();
|
|
232 if (role == Qt::DisplayRole) {
|
|
233 switch (index.column()) {
|
|
234 case AL_TITLE:
|
|
235 return QString::fromWCharArray(list[index.row()].title.english.c_str());
|
|
236 case AL_PROGRESS:
|
|
237 return list[index.row()].progress;
|
|
238 case AL_SCORE:
|
|
239 return list[index.row()].score;
|
|
240 case AL_TYPE:
|
|
241 return QString::fromStdString(AnimeFormatToStringMap[list[index.row()].type]);
|
|
242 case AL_SEASON:
|
|
243 return QString::fromStdString(AnimeSeasonToStringMap[list[index.row()].season]) + " " + QString::number(list[index.row()].air_date.GetYear());
|
|
244 case AL_AVG_SCORE:
|
|
245 return list[index.row()].audience_score;
|
|
246 case AL_STARTED:
|
|
247 return list[index.row()].started.GetAsQDate();
|
|
248 case AL_COMPLETED:
|
|
249 return list[index.row()].completed.GetAsQDate();
|
|
250 case AL_UPDATED: {
|
|
251 if (list[index.row()].updated == 0)
|
|
252 return QString("-");
|
|
253 Time::Duration duration(Time::GetSystemTime() - list[index.row()].updated);
|
|
254 return QString::fromStdString(duration.AsRelativeString());
|
|
255 }
|
|
256 case AL_NOTES:
|
|
257 return QString::fromWCharArray(list[index.row()].notes.c_str());
|
|
258 default:
|
|
259 return "";
|
|
260 }
|
|
261 } else if (role == Qt::TextAlignmentRole) {
|
|
262 switch (index.column()) {
|
|
263 case AL_TITLE:
|
|
264 case AL_NOTES:
|
|
265 return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
|
|
266 case AL_PROGRESS:
|
|
267 case AL_TYPE:
|
|
268 case AL_SCORE:
|
|
269 case AL_AVG_SCORE:
|
|
270 return QVariant(Qt::AlignCenter | Qt::AlignVCenter);
|
|
271 case AL_SEASON:
|
|
272 case AL_STARTED:
|
|
273 case AL_COMPLETED:
|
|
274 case AL_UPDATED:
|
|
275 return QVariant(Qt::AlignRight | Qt::AlignVCenter);
|
|
276 default:
|
|
277 break;
|
|
278 }
|
|
279 }
|
|
280 return QVariant();
|
|
281 }
|
|
282
|
|
283 void AnimeListWidgetModel::UpdateAnime(Anime& anime) {
|
|
284 int i = list.GetAnimeIndex(anime);
|
|
285 emit dataChanged(index(i), index(i));
|
|
286 }
|
|
287
|
|
288 /* Most of this stuff is const and/or should be edited in the Information dialog
|
|
289
|
|
290 bool AnimeListWidgetModel::setData(const QModelIndex &index, const QVariant &value, int role) {
|
|
291 if (!index.isValid() || role != Qt::DisplayRole)
|
|
292 return false;
|
|
293
|
|
294 Anime* const anime = &list[index.row()];
|
|
295
|
|
296 switch (index.column()) {
|
|
297 case AL_TITLE:
|
|
298 break;
|
|
299 case AL_CATEGORY:
|
|
300 break;
|
|
301 default:
|
|
302 return false;
|
|
303 }
|
|
304
|
|
305 return true;
|
|
306 }
|
|
307 */
|
|
308
|
|
309 int AnimeListWidget::VisibleColumnsCount() const {
|
|
310 int count = 0;
|
|
311
|
|
312 for (int i = 0, end = header()->count(); i < end; i++)
|
|
313 {
|
|
314 if (!isColumnHidden(i))
|
|
315 count++;
|
|
316 }
|
|
317
|
|
318 return count;
|
|
319 }
|
|
320
|
|
321 void AnimeListWidget::SetColumnDefaults() {
|
|
322 setColumnHidden(AnimeListWidgetModel::AL_SEASON, false);
|
|
323 setColumnHidden(AnimeListWidgetModel::AL_TYPE, false);
|
|
324 setColumnHidden(AnimeListWidgetModel::AL_UPDATED, false);
|
|
325 setColumnHidden(AnimeListWidgetModel::AL_PROGRESS, false);
|
|
326 setColumnHidden(AnimeListWidgetModel::AL_SCORE, false);
|
|
327 setColumnHidden(AnimeListWidgetModel::AL_TITLE, false);
|
|
328 setColumnHidden(AnimeListWidgetModel::AL_AVG_SCORE, true);
|
|
329 setColumnHidden(AnimeListWidgetModel::AL_STARTED, true);
|
|
330 setColumnHidden(AnimeListWidgetModel::AL_COMPLETED, true);
|
|
331 setColumnHidden(AnimeListWidgetModel::AL_UPDATED, true);
|
|
332 setColumnHidden(AnimeListWidgetModel::AL_NOTES, true);
|
|
333 }
|
|
334
|
|
335 void AnimeListWidget::DisplayColumnHeaderMenu() {
|
|
336 QMenu *menu = new QMenu(this);
|
|
337 menu->setAttribute(Qt::WA_DeleteOnClose);
|
|
338 menu->setTitle(tr("Column visibility"));
|
|
339 menu->setToolTipsVisible(true);
|
|
340
|
3
|
341 for (int i = 0; i < AnimeListWidgetModel::NB_COLUMNS; i++) {
|
2
|
342 if (i == AnimeListWidgetModel::AL_TITLE)
|
|
343 continue;
|
|
344 const auto column_name = model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString();
|
|
345 QAction *action = menu->addAction(column_name, this, [this, i](const bool checked) {
|
|
346 if (!checked && (VisibleColumnsCount() <= 1))
|
|
347 return;
|
|
348
|
|
349 setColumnHidden(i, !checked);
|
|
350
|
|
351 if (checked && (columnWidth(i) <= 5))
|
|
352 resizeColumnToContents(i);
|
|
353
|
|
354 // SaveSettings();
|
|
355 });
|
|
356 action->setCheckable(true);
|
|
357 action->setChecked(!isColumnHidden(i));
|
|
358 }
|
|
359
|
|
360 menu->addSeparator();
|
3
|
361 QAction *resetAction = menu->addAction(tr("Reset to defaults"), this, [this]() {
|
2
|
362 for (int i = 0, count = header()->count(); i < count; ++i)
|
|
363 {
|
|
364 SetColumnDefaults();
|
|
365 }
|
|
366 // SaveSettings();
|
|
367 });
|
|
368 menu->popup(QCursor::pos());
|
3
|
369 (void)(resetAction);
|
2
|
370 }
|
|
371
|
|
372 void AnimeListWidget::DisplayListMenu() {
|
|
373 /* throw out any other garbage */
|
|
374 const QModelIndexList selected_items = selectionModel()->selectedRows();
|
3
|
375 if (selected_items.size() != 1 || !selected_items.first().isValid()) {
|
2
|
376 return;
|
3
|
377 }
|
2
|
378
|
|
379 const QModelIndex index = model->index(selected_items.first().row());
|
|
380 Anime* anime = model->GetAnimeFromIndex(index);
|
3
|
381 if (!anime) {
|
2
|
382 return;
|
3
|
383 }
|
2
|
384
|
|
385 }
|
|
386
|
|
387 void AnimeListWidget::ItemDoubleClicked() {
|
|
388 /* throw out any other garbage */
|
|
389 const QModelIndexList selected_items = selectionModel()->selectedRows();
|
3
|
390 if (selected_items.size() != 1 || !selected_items.first().isValid()) {
|
2
|
391 return;
|
3
|
392 }
|
2
|
393
|
|
394 /* TODO: after we implement our sort model, we have to use mapToSource here... */
|
|
395 const QModelIndex index = model->index(selected_items.first().row());
|
|
396 Anime* anime = model->GetAnimeFromIndex(index);
|
3
|
397 if (!anime) {
|
2
|
398 return;
|
3
|
399 }
|
2
|
400
|
|
401 InformationDialog* dialog = new InformationDialog(*anime, model, this);
|
|
402
|
|
403 dialog->show();
|
|
404 dialog->raise();
|
|
405 dialog->activateWindow();
|
|
406 }
|
|
407
|
|
408 AnimeListWidget::AnimeListWidget(QWidget* parent, AnimeList* alist)
|
|
409 : QTreeView(parent) {
|
|
410 model = new AnimeListWidgetModel(parent, alist);
|
|
411 setModel(model);
|
|
412 setObjectName("listwidget");
|
|
413 setStyleSheet("QTreeView#listwidget{border-top:0px;}");
|
|
414 setUniformRowHeights(true);
|
|
415 setAllColumnsShowFocus(false);
|
|
416 setSortingEnabled(true);
|
|
417 setSelectionMode(QAbstractItemView::ExtendedSelection);
|
|
418 setItemsExpandable(false);
|
|
419 setRootIsDecorated(false);
|
|
420 setContextMenuPolicy(Qt::CustomContextMenu);
|
3
|
421 connect(this, &QAbstractItemView::doubleClicked, this, &AnimeListWidget::ItemDoubleClicked);
|
|
422 connect(this, &QWidget::customContextMenuRequested, this, &AnimeListWidget::DisplayListMenu);
|
2
|
423
|
|
424 /* Enter & return keys */
|
|
425 connect(new QShortcut(Qt::Key_Return, this, nullptr, nullptr, Qt::WidgetShortcut),
|
3
|
426 &QShortcut::activated, this, &AnimeListWidget::ItemDoubleClicked);
|
2
|
427
|
|
428 connect(new QShortcut(Qt::Key_Enter, this, nullptr, nullptr, Qt::WidgetShortcut),
|
3
|
429 &QShortcut::activated, this, &AnimeListWidget::ItemDoubleClicked);
|
2
|
430
|
|
431 header()->setStretchLastSection(false);
|
|
432 header()->setContextMenuPolicy(Qt::CustomContextMenu);
|
3
|
433 connect(header(), &QWidget::customContextMenuRequested, this, &AnimeListWidget::DisplayColumnHeaderMenu);
|
2
|
434 // if(!session.config.anime_list.columns) {
|
|
435 SetColumnDefaults();
|
|
436 // }
|
|
437 }
|
|
438
|
|
439 AnimeListPage::AnimeListPage(QWidget* parent) : QTabWidget (parent) {
|
|
440 setDocumentMode(false);
|
|
441 SyncAnimeList();
|
|
442 for (AnimeList& list : anime_lists) {
|
|
443 addTab(new AnimeListWidget(this, &list), QString::fromWCharArray(list.name.c_str()));
|
|
444 }
|
|
445 }
|
|
446
|
|
447 void AnimeListPage::SyncAnimeList() {
|
|
448 switch (session.config.service) {
|
|
449 case ANILIST: {
|
|
450 AniList anilist = AniList();
|
|
451 anilist.Authorize();
|
|
452 session.config.anilist.user_id = anilist.GetUserId(session.config.anilist.username);
|
|
453 FreeAnimeList();
|
|
454 anilist.UpdateAnimeList(&anime_lists, session.config.anilist.user_id);
|
|
455 break;
|
|
456 }
|
3
|
457 default:
|
|
458 break;
|
2
|
459 }
|
|
460 }
|
|
461
|
|
462 void AnimeListPage::FreeAnimeList() {
|
|
463 if (anime_lists.size() > 0) {
|
|
464 /* FIXME: we may not need this, but to prevent memleaks
|
|
465 we should keep it until we're sure we don't */
|
|
466 for (auto& list : anime_lists) {
|
|
467 list.Clear();
|
|
468 }
|
|
469 anime_lists.clear();
|
|
470 }
|
|
471 }
|
|
472
|
|
473 int AnimeListPage::GetTotalAnimeAmount() {
|
|
474 int total = 0;
|
|
475 for (auto& list : anime_lists) {
|
|
476 total += list.Size();
|
|
477 }
|
|
478 return total;
|
|
479 }
|
|
480
|
|
481 int AnimeListPage::GetTotalEpisodeAmount() {
|
|
482 /* FIXME: this also needs to take into account rewatches... */
|
|
483 int total = 0;
|
|
484 for (auto& list : anime_lists) {
|
|
485 for (auto& anime : list) {
|
|
486 total += anime.progress;
|
|
487 }
|
|
488 }
|
|
489 return total;
|
|
490 }
|
|
491
|
|
492 /* Returns the total watched amount in minutes. */
|
|
493 int AnimeListPage::GetTotalWatchedAmount() {
|
|
494 int total = 0;
|
|
495 for (auto& list : anime_lists) {
|
|
496 for (auto& anime : list) {
|
|
497 total += anime.duration*anime.progress;
|
|
498 }
|
|
499 }
|
|
500 return total;
|
|
501 }
|
|
502
|
|
503 /* Returns the total planned amount in minutes.
|
|
504 Note that we should probably limit progress to the
|
|
505 amount of episodes, as AniList will let you
|
|
506 set episode counts up to 32768. But that should
|
|
507 rather be handled elsewhere. */
|
|
508 int AnimeListPage::GetTotalPlannedAmount() {
|
|
509 int total = 0;
|
|
510 for (auto& list : anime_lists) {
|
|
511 for (auto& anime : list) {
|
|
512 total += anime.duration*(anime.episodes-anime.progress);
|
|
513 }
|
|
514 }
|
|
515 return total;
|
|
516 }
|
|
517
|
|
518 double AnimeListPage::GetAverageScore() {
|
|
519 double avg = 0;
|
|
520 int amt = 0;
|
|
521 for (auto& list : anime_lists) {
|
|
522 for (auto& anime : list) {
|
|
523 avg += anime.score;
|
|
524 if (anime.score != 0)
|
|
525 amt++;
|
|
526 }
|
|
527 }
|
|
528 return avg/amt;
|
|
529 }
|
|
530
|
|
531 double AnimeListPage::GetScoreDeviation() {
|
|
532 double squares_sum = 0, avg = GetAverageScore();
|
|
533 int amt = 0;
|
|
534 for (auto& list : anime_lists) {
|
|
535 for (auto& anime : list) {
|
|
536 if (anime.score != 0) {
|
|
537 squares_sum += std::pow((double)anime.score - avg, 2);
|
|
538 amt++;
|
|
539 }
|
|
540 }
|
|
541 }
|
|
542 return (amt > 0) ? std::sqrt(squares_sum / amt) : 0;
|
|
543 }
|
|
544
|
|
545 #include "moc_anime.cpp"
|