annotate src/gui/pages/anime_list.cpp @ 12:cf6a73a5ba1c

cmake: fix for mingw
author Paper <mrpapersonic@gmail.com>
date Mon, 18 Sep 2023 23:40:11 -0400
parents fc1bf97c528b
children cde8f67a7c7d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
1 /**
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
2 * anime_list.cpp: defines the anime list page
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
3 * and widgets.
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
4 *
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
5 * much of this file is based around
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
6 * Qt's original QTabWidget implementation, because
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
7 * I needed a somewhat native way to create a tabbed
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
8 * widget with only one subwidget that worked exactly
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
9 * like a native tabbed widget.
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
10 **/
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
11 #include "gui/pages/anime_list.h"
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
12 #include "core/anime.h"
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
13 #include "core/anime_db.h"
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
14 #include "core/session.h"
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
15 #include "core/time.h"
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
16 #include "gui/dialog/information.h"
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
17 #include "gui/translate/anime.h"
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
18 #include "services/anilist.h"
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
19 #include <QHBoxLayout>
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
20 #include <QHeaderView>
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
21 #include <QMenu>
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
22 #include <QProgressBar>
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
23 #include <QDebug>
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
24 #include <QShortcut>
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
25 #include <QStylePainter>
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
26 #include <QStyledItemDelegate>
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
27 #include <QAbstractItemModelTester>
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
28 #include <cmath>
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
29
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
30 AnimeListWidgetDelegate::AnimeListWidgetDelegate(QObject* parent) : QStyledItemDelegate(parent) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
31 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
32
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
33 QWidget* AnimeListWidgetDelegate::createEditor(QWidget*, const QStyleOptionViewItem&, const QModelIndex&) const {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
34 // no edit 4 u
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
35 return nullptr;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
36 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
37
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
38 void AnimeListWidgetDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
39 const QModelIndex& index) const {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
40 switch (index.column()) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
41 /*
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
42 case AnimeListWidgetModel::AL_PROGRESS: {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
43 const int progress = static_cast<int>(index.data(Qt::UserRole).toReal());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
44 const int episodes =
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
45 static_cast<int>(index.siblingAtColumn(AnimeListWidgetModel::AL_EPISODES).data(Qt::UserRole).toReal());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
46
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
47 int text_width = 59;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
48 QRectF text_rect(option.rect.x() + text_width, option.rect.y(), text_width, option.decorationSize.height());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
49 painter->save();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
50 painter->drawText(text_rect, "/", QTextOption(Qt::AlignCenter | Qt::AlignVCenter));
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
51 // drawText(const QRectF &rectangle, const QString &text, const QTextOption &option = QTextOption())
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
52 painter->drawText(QRectF(text_rect.x(), text_rect.y(), text_width / 2 - 2, text_rect.height()),
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
53 QString::number(progress), QTextOption(Qt::AlignRight | Qt::AlignVCenter));
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
54 painter->drawText(
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
55 QRectF(text_rect.x() + text_width / 2 + 2, text_rect.y(), text_width / 2 - 2, text_rect.height()),
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
56 QString::number(episodes), QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
57 painter->restore();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
58 QStyledItemDelegate::paint(painter, option, index);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
59 break;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
60 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
61 */
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
62 default: QStyledItemDelegate::paint(painter, option, index); break;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
63 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
64 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
65
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
66 AnimeListWidgetSortFilter::AnimeListWidgetSortFilter(QObject* parent) : QSortFilterProxyModel(parent) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
67 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
68
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
69 bool AnimeListWidgetSortFilter::lessThan(const QModelIndex& l, const QModelIndex& r) const {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
70 QVariant left = sourceModel()->data(l, sortRole());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
71 QVariant right = sourceModel()->data(r, sortRole());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
72
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
73 switch (left.userType()) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
74 case QMetaType::Int:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
75 case QMetaType::UInt:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
76 case QMetaType::LongLong:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
77 case QMetaType::ULongLong: return left.toInt() < right.toInt();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
78 case QMetaType::QDate: return left.toDate() < right.toDate();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
79 case QMetaType::QString:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
80 default: return QString::compare(left.toString(), right.toString(), Qt::CaseInsensitive) < 0;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
81 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
82 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
83
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
84 AnimeListWidgetModel::AnimeListWidgetModel(QWidget* parent, Anime::ListStatus _status) : QAbstractListModel(parent) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
85 status = _status;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
86 return;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
87 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
88
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
89 int AnimeListWidgetModel::rowCount(const QModelIndex& parent) const {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
90 return list.size();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
91 (void)(parent);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
92 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
93
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
94 int AnimeListWidgetModel::columnCount(const QModelIndex& parent) const {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
95 return NB_COLUMNS;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
96 (void)(parent);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
97 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
98
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
99 QVariant AnimeListWidgetModel::headerData(const int section, const Qt::Orientation orientation, const int role) const {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
100 if (role == Qt::DisplayRole) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
101 switch (section) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
102 case AL_TITLE: return tr("Anime title");
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
103 case AL_PROGRESS: return tr("Progress");
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
104 case AL_EPISODES: return tr("Episodes");
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
105 case AL_TYPE: return tr("Type");
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
106 case AL_SCORE: return tr("Score");
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
107 case AL_SEASON: return tr("Season");
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
108 case AL_STARTED: return tr("Date started");
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
109 case AL_COMPLETED: return tr("Date completed");
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
110 case AL_NOTES: return tr("Notes");
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
111 case AL_AVG_SCORE: return tr("Average score");
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
112 case AL_UPDATED: return tr("Last updated");
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
113 default: return {};
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
114 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
115 } else if (role == Qt::TextAlignmentRole) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
116 switch (section) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
117 case AL_TITLE:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
118 case AL_NOTES: return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
119 case AL_PROGRESS:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
120 case AL_EPISODES:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
121 case AL_TYPE:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
122 case AL_SCORE:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
123 case AL_AVG_SCORE: return QVariant(Qt::AlignCenter | Qt::AlignVCenter);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
124 case AL_SEASON:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
125 case AL_STARTED:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
126 case AL_COMPLETED:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
127 case AL_UPDATED: return QVariant(Qt::AlignRight | Qt::AlignVCenter);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
128 default: return QAbstractListModel::headerData(section, orientation, role);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
129 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
130 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
131 return QAbstractListModel::headerData(section, orientation, role);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
132 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
133
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
134 QVariant AnimeListWidgetModel::data(const QModelIndex& index, int role) const {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
135 if (!index.isValid())
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
136 return QVariant();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
137 switch (role) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
138 case Qt::DisplayRole:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
139 switch (index.column()) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
140 case AL_TITLE: return QString::fromUtf8(list[index.row()].GetUserPreferredTitle().c_str());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
141 case AL_PROGRESS:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
142 return QString::number(list[index.row()].GetUserProgress()) + "/" + QString::number(list[index.row()].GetEpisodes());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
143 case AL_EPISODES: return list[index.row()].GetEpisodes();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
144 case AL_SCORE: return list[index.row()].GetUserScore();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
145 case AL_TYPE: return QString::fromStdString(Translate::TranslateSeriesFormat(list[index.row()].GetFormat()));
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
146 case AL_SEASON:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
147 return QString::fromStdString(Translate::TranslateSeriesSeason(list[index.row()].GetSeason())) + " " +
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
148 QString::number(list[index.row()].GetAirDate().GetYear());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
149 case AL_AVG_SCORE: return QString::number(list[index.row()].GetAudienceScore()) + "%";
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
150 case AL_STARTED: return list[index.row()].GetUserDateStarted().GetAsQDate();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
151 case AL_COMPLETED: return list[index.row()].GetUserDateCompleted().GetAsQDate();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
152 case AL_UPDATED: {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
153 if (list[index.row()].GetUserTimeUpdated() == 0)
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
154 return QString("-");
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
155 Time::Duration duration(Time::GetSystemTime() - list[index.row()].GetUserTimeUpdated());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
156 return QString::fromUtf8(duration.AsRelativeString().c_str());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
157 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
158 case AL_NOTES: return QString::fromUtf8(list[index.row()].GetUserNotes().c_str());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
159 default: return "";
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
160 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
161 break;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
162 case Qt::UserRole:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
163 switch (index.column()) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
164 case AL_ID: return list[index.row()].GetId();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
165 case AL_PROGRESS: return list[index.row()].GetUserProgress();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
166 case AL_TYPE: return static_cast<int>(list[index.row()].GetFormat());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
167 case AL_SEASON: return list[index.row()].GetAirDate().GetAsQDate();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
168 case AL_AVG_SCORE: return list[index.row()].GetAudienceScore();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
169 case AL_UPDATED: return list[index.row()].GetUserTimeUpdated();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
170 default: return data(index, Qt::DisplayRole);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
171 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
172 break;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
173 case Qt::TextAlignmentRole:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
174 switch (index.column()) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
175 case AL_TITLE:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
176 case AL_NOTES: return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
177 case AL_PROGRESS:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
178 case AL_EPISODES:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
179 case AL_TYPE:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
180 case AL_SCORE:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
181 case AL_AVG_SCORE: return QVariant(Qt::AlignCenter | Qt::AlignVCenter);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
182 case AL_SEASON:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
183 case AL_STARTED:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
184 case AL_COMPLETED:
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
185 case AL_UPDATED: return QVariant(Qt::AlignRight | Qt::AlignVCenter);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
186 default: break;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
187 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
188 break;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
189 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
190 return QVariant();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
191 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
192
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
193 void AnimeListWidgetModel::UpdateAnime(int id) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
194 /* meh... it might be better to just reinit the entire list */
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
195 int i = 0;
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
196 for (const auto& a : Anime::db.items) {
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
197 if (a.second.IsInUserList() && a.first == id && a.second.GetUserStatus() == status) {
10
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
198 emit dataChanged(index(i), index(i));
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
199 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
200 i++;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
201 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
202 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
203
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
204 Anime::Anime* AnimeListWidgetModel::GetAnimeFromIndex(QModelIndex index) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
205 return &list.at(index.row());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
206 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
207
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
208 void AnimeListWidgetModel::RefreshList() {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
209 bool has_children = !!rowCount(index(0));
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
210 if (has_children) beginResetModel();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
211 list.clear();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
212
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
213 for (const auto& a : Anime::db.items) {
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
214 if (a.second.IsInUserList() && a.second.GetUserStatus() == status) {
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
215 list.push_back(a.second);
10
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
216 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
217 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
218 if (has_children) endResetModel();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
219 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
220
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
221 int AnimeListWidget::VisibleColumnsCount() const {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
222 int count = 0;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
223
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
224 for (int i = 0, end = tree_view->header()->count(); i < end; i++) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
225 if (!tree_view->isColumnHidden(i))
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
226 count++;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
227 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
228
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
229 return count;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
230 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
231
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
232 void AnimeListWidget::SetColumnDefaults() {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
233 tree_view->setColumnHidden(AnimeListWidgetModel::AL_SEASON, false);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
234 tree_view->setColumnHidden(AnimeListWidgetModel::AL_TYPE, false);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
235 tree_view->setColumnHidden(AnimeListWidgetModel::AL_UPDATED, false);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
236 tree_view->setColumnHidden(AnimeListWidgetModel::AL_PROGRESS, false);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
237 tree_view->setColumnHidden(AnimeListWidgetModel::AL_SCORE, false);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
238 tree_view->setColumnHidden(AnimeListWidgetModel::AL_TITLE, false);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
239 tree_view->setColumnHidden(AnimeListWidgetModel::AL_EPISODES, true);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
240 tree_view->setColumnHidden(AnimeListWidgetModel::AL_AVG_SCORE, true);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
241 tree_view->setColumnHidden(AnimeListWidgetModel::AL_STARTED, true);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
242 tree_view->setColumnHidden(AnimeListWidgetModel::AL_COMPLETED, true);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
243 tree_view->setColumnHidden(AnimeListWidgetModel::AL_UPDATED, true);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
244 tree_view->setColumnHidden(AnimeListWidgetModel::AL_NOTES, true);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
245 tree_view->setColumnHidden(AnimeListWidgetModel::AL_ID, true);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
246 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
247
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
248 void AnimeListWidget::DisplayColumnHeaderMenu() {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
249 QMenu* menu = new QMenu(this);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
250 menu->setAttribute(Qt::WA_DeleteOnClose);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
251 menu->setTitle(tr("Column visibility"));
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
252 menu->setToolTipsVisible(true);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
253
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
254 for (int i = 0; i < AnimeListWidgetModel::NB_COLUMNS; i++) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
255 if (i == AnimeListWidgetModel::AL_TITLE || i == AnimeListWidgetModel::AL_ID)
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
256 continue;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
257 const auto column_name =
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
258 sort_models[tab_bar->currentIndex()]->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
259 QAction* action = menu->addAction(column_name, this, [this, i](const bool checked) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
260 if (!checked && (VisibleColumnsCount() <= 1))
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
261 return;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
262
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
263 tree_view->setColumnHidden(i, !checked);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
264
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
265 if (checked && (tree_view->columnWidth(i) <= 5))
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
266 tree_view->resizeColumnToContents(i);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
267
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
268 // SaveSettings();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
269 });
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
270 action->setCheckable(true);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
271 action->setChecked(!tree_view->isColumnHidden(i));
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
272 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
273
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
274 menu->addSeparator();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
275 QAction* resetAction = menu->addAction(tr("Reset to defaults"), this, [this]() {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
276 for (int i = 0, count = tree_view->header()->count(); i < count; ++i) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
277 SetColumnDefaults();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
278 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
279 // SaveSettings();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
280 });
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
281 menu->popup(QCursor::pos());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
282 (void)(resetAction);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
283 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
284
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
285 void AnimeListWidget::DisplayListMenu() {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
286 QMenu* menu = new QMenu(this);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
287 menu->setAttribute(Qt::WA_DeleteOnClose);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
288 menu->setTitle(tr("Column visibility"));
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
289 menu->setToolTipsVisible(true);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
290
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
291 const QItemSelection selection = sort_models[tab_bar->currentIndex()]->mapSelectionToSource(tree_view->selectionModel()->selection());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
292 if (!selection.indexes().first().isValid()) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
293 return;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
294 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
295
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
296 QAction* action = menu->addAction("Information", [this, selection] {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
297 const QModelIndex index = ((AnimeListWidgetModel*)sort_models[tab_bar->currentIndex()]->sourceModel())
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
298 ->index(selection.indexes().first().row());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
299 Anime::Anime* anime =
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
300 ((AnimeListWidgetModel*)sort_models[tab_bar->currentIndex()]->sourceModel())->GetAnimeFromIndex(index);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
301 if (!anime) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
302 return;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
303 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
304
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
305 InformationDialog* dialog = new InformationDialog(
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
306 *anime,
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
307 [this, anime] {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
308 ((AnimeListWidgetModel*)sort_models[tab_bar->currentIndex()]->sourceModel())->UpdateAnime(anime->GetId());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
309 },
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
310 this);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
311
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
312 dialog->show();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
313 dialog->raise();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
314 dialog->activateWindow();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
315 });
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
316 menu->popup(QCursor::pos());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
317 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
318
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
319 void AnimeListWidget::ItemDoubleClicked() {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
320 /* throw out any other garbage */
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
321 const QItemSelection selection =
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
322 sort_models[tab_bar->currentIndex()]->mapSelectionToSource(tree_view->selectionModel()->selection());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
323 if (!selection.indexes().first().isValid()) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
324 return;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
325 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
326
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
327 const QModelIndex index = ((AnimeListWidgetModel*)sort_models[tab_bar->currentIndex()]->sourceModel())
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
328 ->index(selection.indexes().first().row());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
329 Anime::Anime* anime =
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
330 ((AnimeListWidgetModel*)sort_models[tab_bar->currentIndex()]->sourceModel())->GetAnimeFromIndex(index);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
331
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
332 InformationDialog* dialog = new InformationDialog(
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
333 *anime,
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
334 [this, anime] {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
335 ((AnimeListWidgetModel*)sort_models[tab_bar->currentIndex()]->sourceModel())->UpdateAnime(anime->GetId());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
336 },
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
337 this);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
338
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
339 dialog->show();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
340 dialog->raise();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
341 dialog->activateWindow();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
342 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
343
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
344 void AnimeListWidget::paintEvent(QPaintEvent*) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
345 QStylePainter p(this);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
346
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
347 QStyleOptionTabWidgetFrame opt;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
348 InitStyle(&opt);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
349 opt.rect = panelRect;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
350 p.drawPrimitive(QStyle::PE_FrameTabWidget, opt);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
351 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
352
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
353 void AnimeListWidget::resizeEvent(QResizeEvent* e) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
354 QWidget::resizeEvent(e);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
355 SetupLayout();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
356 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
357
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
358 void AnimeListWidget::showEvent(QShowEvent*) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
359 SetupLayout();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
360 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
361
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
362 void AnimeListWidget::InitBasicStyle(QStyleOptionTabWidgetFrame* option) const {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
363 if (!option)
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
364 return;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
365
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
366 option->initFrom(this);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
367 option->lineWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, this);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
368 option->shape = QTabBar::RoundedNorth;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
369 option->tabBarRect = tab_bar->geometry();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
370 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
371
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
372 void AnimeListWidget::InitStyle(QStyleOptionTabWidgetFrame* option) const {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
373 if (!option)
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
374 return;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
375
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
376 InitBasicStyle(option);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
377
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
378 // int exth = style()->pixelMetric(QStyle::PM_TabBarBaseHeight, nullptr, this);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
379 QSize t(0, tree_view->frameWidth());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
380 if (tab_bar->isVisibleTo(this)) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
381 t = tab_bar->sizeHint();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
382 t.setWidth(width());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
383 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
384 option->tabBarSize = t;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
385
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
386 QRect selected_tab_rect = tab_bar->tabRect(tab_bar->currentIndex());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
387 selected_tab_rect.moveTopLeft(selected_tab_rect.topLeft() + option->tabBarRect.topLeft());
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
388 option->selectedTabRect = selected_tab_rect;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
389
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
390 option->lineWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, this);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
391 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
392
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
393 void AnimeListWidget::SetupLayout() {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
394 QStyleOptionTabWidgetFrame option;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
395 InitStyle(&option);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
396
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
397 QRect tabRect = style()->subElementRect(QStyle::SE_TabWidgetTabBar, &option, this);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
398 tabRect.setLeft(tabRect.left() + 1);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
399 panelRect = style()->subElementRect(QStyle::SE_TabWidgetTabPane, &option, this);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
400 QRect contentsRect = style()->subElementRect(QStyle::SE_TabWidgetTabContents, &option, this);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
401
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
402 tab_bar->setGeometry(tabRect);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
403 tree_view->parentWidget()->setGeometry(contentsRect);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
404 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
405
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
406 AnimeListWidget::AnimeListWidget(QWidget* parent) : QWidget(parent) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
407 /* Tab bar */
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
408 tab_bar = new QTabBar(this);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
409 tab_bar->setExpanding(false);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
410 tab_bar->setDrawBase(false);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
411
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
412 /* Tree view... */
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
413 QWidget* tree_widget = new QWidget(this);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
414 tree_view = new QTreeView(tree_widget);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
415 tree_view->setItemDelegate(new AnimeListWidgetDelegate(tree_view));
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
416 tree_view->setUniformRowHeights(true);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
417 tree_view->setAllColumnsShowFocus(false);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
418 tree_view->setAlternatingRowColors(true);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
419 tree_view->setSortingEnabled(true);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
420 tree_view->setSelectionMode(QAbstractItemView::ExtendedSelection);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
421 tree_view->setItemsExpandable(false);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
422 tree_view->setRootIsDecorated(false);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
423 tree_view->setContextMenuPolicy(Qt::CustomContextMenu);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
424 tree_view->setFrameShape(QFrame::NoFrame);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
425
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
426 for (unsigned int i = 0; i < ARRAYSIZE(sort_models); i++) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
427 tab_bar->addTab(QString::fromStdString(Translate::TranslateListStatus(Anime::ListStatuses[i])) + " (" + QString::number(Anime::db.GetListsAnimeAmount(Anime::ListStatuses[i])) + ")");
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
428 sort_models[i] = new AnimeListWidgetSortFilter(tree_view);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
429 AnimeListWidgetModel* model = new AnimeListWidgetModel(this, Anime::ListStatuses[i]);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
430 new QAbstractItemModelTester(model, QAbstractItemModelTester::FailureReportingMode::Fatal, this);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
431 sort_models[i]->setSourceModel(model);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
432 sort_models[i]->setSortRole(Qt::UserRole);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
433 sort_models[i]->setSortCaseSensitivity(Qt::CaseInsensitive);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
434 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
435
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
436 QHBoxLayout* layout = new QHBoxLayout;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
437 layout->addWidget(tree_view);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
438 layout->setMargin(0);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
439 tree_widget->setLayout(layout);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
440
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
441 /* Double click stuff */
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
442 connect(tree_view, &QAbstractItemView::doubleClicked, this, &AnimeListWidget::ItemDoubleClicked);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
443 connect(tree_view, &QWidget::customContextMenuRequested, this, &AnimeListWidget::DisplayListMenu);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
444
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
445 /* Enter & return keys */
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
446 connect(new QShortcut(Qt::Key_Return, tree_view, nullptr, nullptr, Qt::WidgetShortcut), &QShortcut::activated,
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
447 this, &AnimeListWidget::ItemDoubleClicked);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
448
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
449 connect(new QShortcut(Qt::Key_Enter, tree_view, nullptr, nullptr, Qt::WidgetShortcut), &QShortcut::activated,
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
450 this, &AnimeListWidget::ItemDoubleClicked);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
451
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
452 tree_view->header()->setStretchLastSection(false);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
453 tree_view->header()->setContextMenuPolicy(Qt::CustomContextMenu);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
454 connect(tree_view->header(), &QWidget::customContextMenuRequested, this,
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
455 &AnimeListWidget::DisplayColumnHeaderMenu);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
456
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
457 connect(tab_bar, &QTabBar::currentChanged, this, [this](int index) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
458 if (sort_models[index])
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
459 tree_view->setModel(sort_models[index]);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
460 });
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
461
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
462 setFocusPolicy(Qt::TabFocus);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
463 setFocusProxy(tab_bar);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
464 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
465
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
466 void AnimeListWidget::RefreshList() {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
467 for (unsigned int i = 0; i < ARRAYSIZE(sort_models); i++) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
468 ((AnimeListWidgetModel*)sort_models[i]->sourceModel())->RefreshList();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
469 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
470 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
471
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
472 void AnimeListWidget::Reset() {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
473 while (tab_bar->count())
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
474 tab_bar->removeTab(0);
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
475 for (unsigned int i = 0; i < ARRAYSIZE(sort_models); i++)
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
476 delete sort_models[i];
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
477 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
478
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
479 #include "gui/pages/moc_anime_list.cpp"