annotate src/anime.cpp @ 3:190ded9438c0

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