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