annotate src/pages/anime_list.cpp @ 7:07a9095eaeed

Update Refactored some code, moved some around
author Paper <mrpapersonic@gmail.com>
date Thu, 24 Aug 2023 23:11:38 -0400
parents src/anime.cpp@1d82f6e04d7d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
1 /**
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
2 * anime_list.cpp: defines the anime list page
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
3 * and widgets.
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
4 *
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
5 * much of this file is based around
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
6 * Qt's original QTabWidget implementation, because
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
7 * I needed a somewhat native way to create a tabbed
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
8 * widget with only one subwidget that worked exactly
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
9 * like a native tabbed widget.
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
10 **/
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
11 #include <cmath>
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
12 #include <QStyledItemDelegate>
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
13 #include <QProgressBar>
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
14 #include <QShortcut>
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
15 #include <QHBoxLayout>
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
16 #include <QStylePainter>
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
17 #include <QMenu>
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
18 #include <QHeaderView>
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
19 #include "anilist.h"
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
20 #include "anime.h"
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
21 #include "anime_list.h"
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
22 #include "information.h"
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
23 #include "session.h"
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
24 #include "time_utils.h"
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
25
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
26 AnimeListWidgetDelegate::AnimeListWidgetDelegate(QObject* parent)
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
27 : QStyledItemDelegate (parent) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
28 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
29
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
30 QWidget* AnimeListWidgetDelegate::createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
31 // no edit 4 u
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
32 return nullptr;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
33 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
34
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
35 void AnimeListWidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
36 {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
37 switch (index.column()) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
38 case AnimeListWidgetModel::AL_PROGRESS: {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
39 const int progress = static_cast<int>(index.data(Qt::UserRole).toReal());
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
40 const int episodes = static_cast<int>(index.siblingAtColumn(AnimeListWidgetModel::AL_EPISODES).data(Qt::UserRole).toReal());
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
41
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
42 QStyleOptionViewItem customOption (option);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
43 customOption.state.setFlag(QStyle::State_Enabled, true);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
44
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
45 progress_bar.paint(painter, customOption, index.data().toString(), progress, episodes);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
46 break;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
47 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
48 default:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
49 QStyledItemDelegate::paint(painter, option, index);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
50 break;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
51 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
52 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
53
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
54 AnimeListWidgetSortFilter::AnimeListWidgetSortFilter(QObject *parent)
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
55 : QSortFilterProxyModel(parent) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
56 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
57
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
58 bool AnimeListWidgetSortFilter::lessThan(const QModelIndex &l,
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
59 const QModelIndex &r) const {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
60 QVariant left = sourceModel()->data(l, sortRole());
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
61 QVariant right = sourceModel()->data(r, sortRole());
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
62
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
63 switch (left.userType()) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
64 case QMetaType::Int:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
65 case QMetaType::UInt:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
66 case QMetaType::LongLong:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
67 case QMetaType::ULongLong:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
68 return left.toInt() < right.toInt();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
69 case QMetaType::QDate:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
70 return left.toDate() < right.toDate();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
71 case QMetaType::QString:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
72 default:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
73 return QString::compare(left.toString(), right.toString(), Qt::CaseInsensitive) < 0;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
74 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
75 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
76
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
77 AnimeListWidgetModel::AnimeListWidgetModel (QWidget* parent, AnimeList* alist)
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
78 : QAbstractListModel(parent)
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
79 , list(*alist) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
80 return;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
81 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
82
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
83 int AnimeListWidgetModel::rowCount(const QModelIndex& parent) const {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
84 return list.Size();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
85 (void)(parent);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
86 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
87
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
88 int AnimeListWidgetModel::columnCount(const QModelIndex& parent) const {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
89 return NB_COLUMNS;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
90 (void)(parent);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
91 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
92
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
93 QVariant AnimeListWidgetModel::headerData(const int section, const Qt::Orientation orientation, const int role) const {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
94 if (role == Qt::DisplayRole) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
95 switch (section) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
96 case AL_TITLE:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
97 return tr("Anime title");
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
98 case AL_PROGRESS:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
99 return tr("Progress");
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
100 case AL_EPISODES:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
101 return tr("Episodes");
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
102 case AL_TYPE:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
103 return tr("Type");
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
104 case AL_SCORE:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
105 return tr("Score");
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
106 case AL_SEASON:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
107 return tr("Season");
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
108 case AL_STARTED:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
109 return tr("Date started");
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
110 case AL_COMPLETED:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
111 return tr("Date completed");
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
112 case AL_NOTES:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
113 return tr("Notes");
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
114 case AL_AVG_SCORE:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
115 return tr("Average score");
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
116 case AL_UPDATED:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
117 return tr("Last updated");
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
118 default:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
119 return {};
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
120 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
121 } else if (role == Qt::TextAlignmentRole) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
122 switch (section) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
123 case AL_TITLE:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
124 case AL_NOTES:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
125 return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
126 case AL_PROGRESS:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
127 case AL_EPISODES:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
128 case AL_TYPE:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
129 case AL_SCORE:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
130 case AL_AVG_SCORE:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
131 return QVariant(Qt::AlignCenter | Qt::AlignVCenter);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
132 case AL_SEASON:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
133 case AL_STARTED:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
134 case AL_COMPLETED:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
135 case AL_UPDATED:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
136 return QVariant(Qt::AlignRight | Qt::AlignVCenter);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
137 default:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
138 return QAbstractListModel::headerData(section, orientation, role);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
139 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
140 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
141 return QAbstractListModel::headerData(section, orientation, role);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
142 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
143
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
144 Anime* AnimeListWidgetModel::GetAnimeFromIndex(const QModelIndex& index) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
145 return (index.isValid()) ? &(list[index.row()]) : nullptr;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
146 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
147
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
148 QVariant AnimeListWidgetModel::data(const QModelIndex& index, int role) const {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
149 if (!index.isValid())
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
150 return QVariant();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
151 switch (role) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
152 case Qt::DisplayRole:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
153 switch (index.column()) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
154 case AL_TITLE:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
155 return QString::fromUtf8(list[index.row()].GetUserPreferredTitle().c_str());
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
156 case AL_PROGRESS:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
157 return QString::number(list[index.row()].progress) + "/" + QString::number(list[index.row()].episodes);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
158 case AL_EPISODES:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
159 return list[index.row()].episodes;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
160 case AL_SCORE:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
161 return list[index.row()].score;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
162 case AL_TYPE:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
163 return QString::fromStdString(AnimeFormatToStringMap[list[index.row()].type]);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
164 case AL_SEASON:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
165 return QString::fromStdString(AnimeSeasonToStringMap[list[index.row()].season]) + " " + QString::number(list[index.row()].air_date.GetYear());
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
166 case AL_AVG_SCORE:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
167 return QString::number(list[index.row()].audience_score) + "%";
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
168 case AL_STARTED:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
169 return list[index.row()].started.GetAsQDate();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
170 case AL_COMPLETED:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
171 return list[index.row()].completed.GetAsQDate();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
172 case AL_UPDATED: {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
173 if (list[index.row()].updated == 0)
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
174 return QString("-");
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
175 Time::Duration duration(Time::GetSystemTime() - list[index.row()].updated);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
176 return QString::fromUtf8(duration.AsRelativeString().c_str());
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
177 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
178 case AL_NOTES:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
179 return QString::fromUtf8(list[index.row()].notes.c_str());
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
180 default:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
181 return "";
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
182 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
183 break;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
184 case Qt::UserRole:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
185 switch (index.column()) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
186 case AL_PROGRESS:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
187 return list[index.row()].progress;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
188 case AL_TYPE:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
189 return list[index.row()].type;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
190 case AL_SEASON:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
191 return list[index.row()].air_date.GetAsQDate();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
192 case AL_AVG_SCORE:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
193 return list[index.row()].audience_score;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
194 case AL_UPDATED:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
195 return list[index.row()].updated;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
196 default:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
197 return data(index, Qt::DisplayRole);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
198 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
199 break;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
200 case Qt::TextAlignmentRole:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
201 switch (index.column()) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
202 case AL_TITLE:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
203 case AL_NOTES:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
204 return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
205 case AL_PROGRESS:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
206 case AL_EPISODES:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
207 case AL_TYPE:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
208 case AL_SCORE:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
209 case AL_AVG_SCORE:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
210 return QVariant(Qt::AlignCenter | Qt::AlignVCenter);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
211 case AL_SEASON:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
212 case AL_STARTED:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
213 case AL_COMPLETED:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
214 case AL_UPDATED:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
215 return QVariant(Qt::AlignRight | Qt::AlignVCenter);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
216 default:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
217 break;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
218 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
219 break;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
220 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
221 return QVariant();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
222 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
223
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
224 void AnimeListWidgetModel::UpdateAnime(Anime& anime) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
225 int i = list.GetAnimeIndex(anime);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
226 emit dataChanged(index(i), index(i));
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
227 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
228
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
229 void AnimeListWidgetModel::Update(AnimeList const& new_list) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
230 list = AnimeList(new_list);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
231 emit dataChanged(index(0), index(rowCount()));
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
232 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
233
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
234 int AnimeListWidget::VisibleColumnsCount() const {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
235 int count = 0;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
236
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
237 for (int i = 0, end = tree_view->header()->count(); i < end; i++)
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
238 {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
239 if (!tree_view->isColumnHidden(i))
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
240 count++;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
241 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
242
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
243 return count;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
244 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
245
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
246 void AnimeListWidget::SetColumnDefaults() {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
247 tree_view->setColumnHidden(AnimeListWidgetModel::AL_SEASON, false);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
248 tree_view->setColumnHidden(AnimeListWidgetModel::AL_TYPE, false);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
249 tree_view->setColumnHidden(AnimeListWidgetModel::AL_UPDATED, false);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
250 tree_view->setColumnHidden(AnimeListWidgetModel::AL_PROGRESS, false);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
251 tree_view->setColumnHidden(AnimeListWidgetModel::AL_SCORE, false);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
252 tree_view->setColumnHidden(AnimeListWidgetModel::AL_TITLE, false);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
253 tree_view->setColumnHidden(AnimeListWidgetModel::AL_EPISODES, true);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
254 tree_view->setColumnHidden(AnimeListWidgetModel::AL_AVG_SCORE, true);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
255 tree_view->setColumnHidden(AnimeListWidgetModel::AL_STARTED, true);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
256 tree_view->setColumnHidden(AnimeListWidgetModel::AL_COMPLETED, true);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
257 tree_view->setColumnHidden(AnimeListWidgetModel::AL_UPDATED, true);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
258 tree_view->setColumnHidden(AnimeListWidgetModel::AL_NOTES, true);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
259 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
260
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
261 void AnimeListWidget::DisplayColumnHeaderMenu() {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
262 QMenu *menu = new QMenu(this);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
263 menu->setAttribute(Qt::WA_DeleteOnClose);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
264 menu->setTitle(tr("Column visibility"));
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
265 menu->setToolTipsVisible(true);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
266
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
267 for (int i = 0; i < AnimeListWidgetModel::NB_COLUMNS; i++) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
268 if (i == AnimeListWidgetModel::AL_TITLE)
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
269 continue;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
270 const auto column_name = sort_models[tab_bar->currentIndex()]->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
271 QAction *action = menu->addAction(column_name, this, [this, i](const bool checked) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
272 if (!checked && (VisibleColumnsCount() <= 1))
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
273 return;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
274
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
275 tree_view->setColumnHidden(i, !checked);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
276
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
277 if (checked && (tree_view->columnWidth(i) <= 5))
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
278 tree_view->resizeColumnToContents(i);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
279
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
280 // SaveSettings();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
281 });
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
282 action->setCheckable(true);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
283 action->setChecked(!tree_view->isColumnHidden(i));
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
284 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
285
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
286 menu->addSeparator();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
287 QAction *resetAction = menu->addAction(tr("Reset to defaults"), this, [this]() {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
288 for (int i = 0, count = tree_view->header()->count(); i < count; ++i)
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
289 {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
290 SetColumnDefaults();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
291 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
292 // SaveSettings();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
293 });
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
294 menu->popup(QCursor::pos());
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
295 (void)(resetAction);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
296 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
297
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
298 void AnimeListWidget::DisplayListMenu() {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
299 QMenu *menu = new QMenu(this);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
300 menu->setAttribute(Qt::WA_DeleteOnClose);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
301 menu->setTitle(tr("Column visibility"));
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
302 menu->setToolTipsVisible(true);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
303
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
304 const QItemSelection selection = sort_models[tab_bar->currentIndex()]->mapSelectionToSource(tree_view->selectionModel()->selection());
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
305 if (!selection.indexes().first().isValid()) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
306 return;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
307 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
308
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
309 QAction* action = menu->addAction("Information", [this, selection]{
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
310 const QModelIndex index = ((AnimeListWidgetModel*)sort_models[tab_bar->currentIndex()]->sourceModel())->index(selection.indexes().first().row());
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
311 Anime* anime = ((AnimeListWidgetModel*)sort_models[tab_bar->currentIndex()]->sourceModel())->GetAnimeFromIndex(index);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
312 if (!anime) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
313 return;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
314 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
315
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
316 InformationDialog* dialog = new InformationDialog(*anime, ((AnimeListWidgetModel*)sort_models[tab_bar->currentIndex()]->sourceModel()), this);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
317
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
318 dialog->show();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
319 dialog->raise();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
320 dialog->activateWindow();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
321 });
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
322 menu->popup(QCursor::pos());
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
323 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
324
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
325 void AnimeListWidget::ItemDoubleClicked() {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
326 /* throw out any other garbage */
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
327 const QItemSelection selection = sort_models[tab_bar->currentIndex()]->mapSelectionToSource(tree_view->selectionModel()->selection());
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
328 if (!selection.indexes().first().isValid()) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
329 return;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
330 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
331
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
332 const QModelIndex index = ((AnimeListWidgetModel*)sort_models[tab_bar->currentIndex()]->sourceModel())->index(selection.indexes().first().row());
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
333 Anime* anime = ((AnimeListWidgetModel*)sort_models[tab_bar->currentIndex()]->sourceModel())->GetAnimeFromIndex(index);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
334 if (!anime) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
335 return;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
336 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
337
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
338 InformationDialog* dialog = new InformationDialog(*anime, ((AnimeListWidgetModel*)sort_models[tab_bar->currentIndex()]->sourceModel()), this);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
339
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
340 dialog->show();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
341 dialog->raise();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
342 dialog->activateWindow();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
343 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
344
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
345 void AnimeListWidget::paintEvent(QPaintEvent*) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
346 QStylePainter p(this);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
347
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
348 QStyleOptionTabWidgetFrame opt;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
349 InitStyle(&opt);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
350 opt.rect = panelRect;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
351 p.drawPrimitive(QStyle::PE_FrameTabWidget, opt);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
352 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
353
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
354 void AnimeListWidget::resizeEvent(QResizeEvent* e) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
355 QWidget::resizeEvent(e);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
356 SetupLayout();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
357 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
358
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
359 void AnimeListWidget::showEvent(QShowEvent*) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
360 SetupLayout();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
361 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
362
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
363 void AnimeListWidget::InitBasicStyle(QStyleOptionTabWidgetFrame *option) const
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
364 {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
365 if (!option)
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
366 return;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
367
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
368 option->initFrom(this);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
369 option->lineWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, this);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
370 option->shape = QTabBar::RoundedNorth;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
371 option->tabBarRect = tab_bar->geometry();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
372 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
373
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
374 void AnimeListWidget::InitStyle(QStyleOptionTabWidgetFrame *option) const
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
375 {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
376 if (!option)
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
377 return;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
378
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
379 InitBasicStyle(option);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
380
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
381 //int exth = style()->pixelMetric(QStyle::PM_TabBarBaseHeight, nullptr, this);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
382 QSize t(0, tree_view->frameWidth());
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
383 if (tab_bar->isVisibleTo(this)) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
384 t = tab_bar->sizeHint();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
385 t.setWidth(width());
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
386 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
387 option->tabBarSize = t;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
388
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
389 QRect selected_tab_rect = tab_bar->tabRect(tab_bar->currentIndex());
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
390 selected_tab_rect.moveTopLeft(selected_tab_rect.topLeft() + option->tabBarRect.topLeft());
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
391 option->selectedTabRect = selected_tab_rect;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
392
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
393 option->lineWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, this);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
394 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
395
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
396 void AnimeListWidget::SetupLayout() {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
397 QStyleOptionTabWidgetFrame option;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
398 InitStyle(&option);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
399
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
400 QRect tabRect = style()->subElementRect(QStyle::SE_TabWidgetTabBar, &option, this);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
401 tabRect.setLeft(tabRect.left()+1);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
402 panelRect = style()->subElementRect(QStyle::SE_TabWidgetTabPane, &option, this);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
403 QRect contentsRect = style()->subElementRect(QStyle::SE_TabWidgetTabContents, &option, this);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
404
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
405 tab_bar->setGeometry(tabRect);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
406 tree_view->parentWidget()->setGeometry(contentsRect);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
407 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
408
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
409 AnimeListWidget::AnimeListWidget(QWidget* parent)
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
410 : QWidget(parent) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
411 /* Tab bar */
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
412 tab_bar = new QTabBar(this);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
413 tab_bar->setExpanding(false);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
414 tab_bar->setDrawBase(false);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
415
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
416 /* Tree view... */
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
417 QWidget* tree_widget = new QWidget(this);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
418 tree_view = new QTreeView(tree_widget);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
419 tree_view->setItemDelegate(new AnimeListWidgetDelegate(tree_view));
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
420 tree_view->setUniformRowHeights(true);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
421 tree_view->setAllColumnsShowFocus(false);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
422 tree_view->setSortingEnabled(true);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
423 tree_view->setSelectionMode(QAbstractItemView::ExtendedSelection);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
424 tree_view->setItemsExpandable(false);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
425 tree_view->setRootIsDecorated(false);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
426 tree_view->setContextMenuPolicy(Qt::CustomContextMenu);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
427 tree_view->setFrameShape(QFrame::NoFrame);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
428 QHBoxLayout* layout = new QHBoxLayout;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
429 layout->addWidget(tree_view);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
430 layout->setMargin(0);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
431 tree_widget->setLayout(layout);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
432 connect(tree_view, &QAbstractItemView::doubleClicked, this, &AnimeListWidget::ItemDoubleClicked);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
433 connect(tree_view, &QWidget::customContextMenuRequested, this, &AnimeListWidget::DisplayListMenu);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
434
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
435 /* Enter & return keys */
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
436 connect(new QShortcut(Qt::Key_Return, tree_view, nullptr, nullptr, Qt::WidgetShortcut),
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
437 &QShortcut::activated, this, &AnimeListWidget::ItemDoubleClicked);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
438
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
439 connect(new QShortcut(Qt::Key_Enter, tree_view, nullptr, nullptr, Qt::WidgetShortcut),
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
440 &QShortcut::activated, this, &AnimeListWidget::ItemDoubleClicked);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
441
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
442 tree_view->header()->setStretchLastSection(false);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
443 tree_view->header()->setContextMenuPolicy(Qt::CustomContextMenu);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
444 connect(tree_view->header(), &QWidget::customContextMenuRequested, this, &AnimeListWidget::DisplayColumnHeaderMenu);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
445
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
446 connect(tab_bar, &QTabBar::currentChanged, this, [this](int index){
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
447 if (index < sort_models.size())
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
448 tree_view->setModel(sort_models[index]);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
449 });
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
450
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
451 setFocusPolicy(Qt::TabFocus);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
452 setFocusProxy(tab_bar);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
453 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
454
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
455 void AnimeListWidget::SyncAnimeList() {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
456 switch (session.config.service) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
457 case ANILIST: {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
458 session.config.anilist.user_id = AniList::GetUserId(session.config.anilist.username);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
459 FreeAnimeList();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
460 AniList::UpdateAnimeList(&anime_lists, session.config.anilist.user_id);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
461 break;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
462 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
463 default:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
464 break;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
465 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
466 for (unsigned int i = 0; i < anime_lists.size(); i++) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
467 tab_bar->addTab(QString::fromStdString(anime_lists[i].name));
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
468 AnimeListWidgetSortFilter* sort_model = new AnimeListWidgetSortFilter(tree_view);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
469 sort_model->setSourceModel(new AnimeListWidgetModel(this, &anime_lists[i]));
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
470 sort_model->setSortRole(Qt::UserRole);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
471 sort_model->setSortCaseSensitivity(Qt::CaseInsensitive);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
472 sort_models.push_back(sort_model);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
473 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
474 if (anime_lists.size() > 0)
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
475 tree_view->setModel(sort_models.at(0));
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
476 SetColumnDefaults();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
477 SetupLayout();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
478 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
479
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
480 void AnimeListWidget::FreeAnimeList() {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
481 while (tab_bar->count())
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
482 tab_bar->removeTab(0);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
483 while (sort_models.size()) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
484 delete sort_models[sort_models.size()-1];
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
485 sort_models.pop_back();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
486 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
487 for (auto& list : anime_lists) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
488 list.Clear();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
489 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
490 anime_lists.clear();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
491 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
492
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
493 int AnimeListWidget::GetTotalAnimeAmount() {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
494 int total = 0;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
495 for (auto& list : anime_lists) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
496 total += list.Size();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
497 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
498 return total;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
499 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
500
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
501 int AnimeListWidget::GetTotalEpisodeAmount() {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
502 /* FIXME: this also needs to take into account rewatches... */
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
503 int total = 0;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
504 for (auto& list : anime_lists) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
505 for (auto& anime : list) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
506 total += anime.progress;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
507 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
508 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
509 return total;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
510 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
511
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
512 /* Returns the total watched amount in minutes. */
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
513 int AnimeListWidget::GetTotalWatchedAmount() {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
514 int total = 0;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
515 for (auto& list : anime_lists) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
516 for (auto& anime : list) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
517 total += anime.duration*anime.progress;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
518 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
519 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
520 return total;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
521 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
522
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
523 /* Returns the total planned amount in minutes.
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
524 Note that we should probably limit progress to the
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
525 amount of episodes, as AniList will let you
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
526 set episode counts up to 32768. But that should
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
527 rather be handled elsewhere. */
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
528 int AnimeListWidget::GetTotalPlannedAmount() {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
529 int total = 0;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
530 for (auto& list : anime_lists) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
531 for (auto& anime : list) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
532 total += anime.duration*(anime.episodes-anime.progress);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
533 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
534 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
535 return total;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
536 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
537
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
538 double AnimeListWidget::GetAverageScore() {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
539 double avg = 0;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
540 int amt = 0;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
541 for (auto& list : anime_lists) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
542 for (auto& anime : list) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
543 avg += anime.score;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
544 if (anime.score != 0)
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
545 amt++;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
546 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
547 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
548 return avg/amt;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
549 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
550
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
551 double AnimeListWidget::GetScoreDeviation() {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
552 double squares_sum = 0, avg = GetAverageScore();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
553 int amt = 0;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
554 for (auto& list : anime_lists) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
555 for (auto& anime : list) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
556 if (anime.score != 0) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
557 squares_sum += std::pow((double)anime.score - avg, 2);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
558 amt++;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
559 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
560 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
561 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
562 return (amt > 0) ? std::sqrt(squares_sum / amt) : 0;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
563 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
564
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
565 #include "moc_anime_list.cpp"