comparison src/gui/widgets/anime_info.cc @ 320:1b5c04268d6a

services/kitsu: ACTUALLY finish GetAnimeList there are some things the API just... doesn't provide. therefore we have to request the genres separately any time a new anime info box is opened...
author Paper <paper@paper.us.eu.org>
date Wed, 12 Jun 2024 19:49:19 -0400
parents b1f625b0227c
children 5d3c9b31aa6e
comparison
equal deleted inserted replaced
319:d928ec7b6a0d 320:1b5c04268d6a
1 #include "gui/widgets/anime_info.h" 1 #include "gui/widgets/anime_info.h"
2 #include "core/anime.h" 2 #include "core/anime.h"
3 #include "core/anime_db.h"
3 #include "core/strings.h" 4 #include "core/strings.h"
4 #include "gui/translate/anime.h" 5 #include "gui/translate/anime.h"
5 #include "gui/widgets/text.h" 6 #include "gui/widgets/text.h"
7 #include "services/services.h"
6 #include <QHBoxLayout> 8 #include <QHBoxLayout>
7 #include <QTextStream> 9 #include <QTextStream>
10
11 AnimeInfoWidgetGetMetadataThread::AnimeInfoWidgetGetMetadataThread(QObject* parent) : QThread(parent) {}
12
13 void AnimeInfoWidgetGetMetadataThread::AddToQueue(int id) {
14 const std::lock_guard<std::mutex> guard(queue_mutex_);
15 queue_.push(id);
16 }
17
18 /* processes the queue... */
19 void AnimeInfoWidgetGetMetadataThread::run() {
20 queue_mutex_.lock();
21 while (!queue_.empty() && !isInterruptionRequested()) {
22 int id = queue_.front();
23
24 queue_mutex_.unlock();
25
26 if (Services::RetrieveAnimeMetadata(id))
27 emit NeedRefresh(id);
28
29 queue_mutex_.lock();
30
31 queue_.pop();
32 }
33 queue_mutex_.unlock();
34 }
35
36 /* all widgets share this thread */
37 static AnimeInfoWidgetGetMetadataThread get_metadata_thread;
8 38
9 AnimeInfoWidget::AnimeInfoWidget(QWidget* parent) 39 AnimeInfoWidget::AnimeInfoWidget(QWidget* parent)
10 : QWidget(parent) 40 : QWidget(parent)
11 , _title(tr("Alternative titles"), "") 41 , _title(tr("Alternative titles"), "")
12 , _details(tr("Details"), tr("Type:\nEpisodes:\nStatus:\nSeason:\nGenres:\nScore:"), "") 42 , _details(tr("Details"), tr("Type:\nEpisodes:\nStatus:\nSeason:\nGenres:\nProducers:\nScore:"), "")
13 , _synopsis(tr("Synopsis"), "") { 43 , _synopsis(tr("Synopsis"), "") {
14 QVBoxLayout* layout = new QVBoxLayout(this); 44 QVBoxLayout* layout = new QVBoxLayout(this);
15 45
16 layout->addWidget(&_title); 46 layout->addWidget(&_title);
17 layout->addWidget(&_details); 47 layout->addWidget(&_details);
18 layout->addWidget(&_synopsis); 48 layout->addWidget(&_synopsis);
49
50 /* ... */
51 connect(&get_metadata_thread, &AnimeInfoWidgetGetMetadataThread::NeedRefresh, this, [this](int id) {
52 setUpdatesEnabled(false);
53
54 if (id == id_)
55 RefreshGenres(Anime::db.items[id]);
56
57 setUpdatesEnabled(true);
58 });
19 } 59 }
20 60
21 AnimeInfoWidget::AnimeInfoWidget(const Anime::Anime& anime, QWidget* parent) : AnimeInfoWidget(parent) { 61 AnimeInfoWidget::AnimeInfoWidget(const Anime::Anime& anime, QWidget* parent) : AnimeInfoWidget(parent) {
22 SetAnime(anime); 62 SetAnime(anime);
23 } 63 }
24 64
25 void AnimeInfoWidget::SetAnime(const Anime::Anime& anime) { 65 void AnimeInfoWidget::SetAnime(const Anime::Anime& anime) {
66 setUpdatesEnabled(false);
67
68 id_ = anime.GetId();
69
70 get_metadata_thread.AddToQueue(id_);
71 if (!get_metadata_thread.isRunning())
72 get_metadata_thread.start();
73
26 /* alt titles */ 74 /* alt titles */
27 _title.GetLine()->SetText(Strings::ToQString(Strings::Implode(anime.GetTitleSynonyms(), ", "))); 75 _title.GetLine()->SetText(Strings::ToQString(Strings::Implode(anime.GetTitleSynonyms(), ", ")));
28 76
77 RefreshGenres(anime);
78
79 _synopsis.GetParagraph()->SetText(Strings::ToQString(anime.GetSynopsis()));
80
81 setUpdatesEnabled(true);
82
83 updateGeometry();
84 }
85
86 void AnimeInfoWidget::RefreshGenres(const Anime::Anime& anime) {
29 /* details */ 87 /* details */
30 QString details_data; 88 QString details_data;
31 QTextStream details_data_s(&details_data); 89 QTextStream details_data_s(&details_data);
32 90
33 /* we have to convert ALL of these strings to 91 /* we have to convert ALL of these strings to
34 * QString because QTextStream sucks and assumes 92 * QString because QTextStream sucks and assumes
35 * Latin1 (on Windows?) */ 93 * Latin-1 (on Windows?) */
36 const auto genres = anime.GetGenres(); 94 const auto genres = anime.GetGenres();
95 const auto producers = anime.GetProducers();
96
37 details_data_s << Strings::ToQString(Translate::ToLocalString(anime.GetFormat())) << "\n" 97 details_data_s << Strings::ToQString(Translate::ToLocalString(anime.GetFormat())) << "\n"
38 << anime.GetEpisodes() << "\n" 98 << anime.GetEpisodes() << "\n"
39 << Strings::ToQString(Translate::ToLocalString(anime.GetAiringStatus())) << "\n" 99 << Strings::ToQString(Translate::ToLocalString(anime.GetAiringStatus())) << "\n"
40 << Strings::ToQString(Translate::ToLocalString(anime.GetSeason())) << " " 100 << Strings::ToQString(Translate::ToLocalString(anime.GetSeason())) << " "
41 << anime.GetAirDate().GetYear().value_or(2000) << "\n" 101 << anime.GetAirDate().GetYear().value_or(2000) << "\n"
42 << Strings::ToQString((genres.size() > 1) ? Strings::Implode(genres, ", ") : "-") << "\n" 102 << Strings::ToQString((genres.size() > 1) ? Strings::Implode(genres, ", ") : "-") << "\n"
103 << Strings::ToQString((producers.size() > 1) ? Strings::Implode(producers, ", ") : "-") << "\n"
43 << anime.GetAudienceScore() << "%"; 104 << anime.GetAudienceScore() << "%";
105
44 _details.GetData()->setText(details_data); 106 _details.GetData()->setText(details_data);
45
46 _synopsis.GetParagraph()->SetText(Strings::ToQString(anime.GetSynopsis()));
47
48 updateGeometry();
49 } 107 }