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