annotate src/gui/widgets/poster.cc @ 367:8d45d892be88 default tip

*: instead of pugixml, use Qt XML features this means we have one extra Qt dependency though...
author Paper <paper@tflc.us>
date Sun, 17 Nov 2024 22:55:47 -0500
parents 6b0768158dcd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
1 #include "gui/widgets/poster.h"
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
2 #include "core/anime_db.h"
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
3 #include "core/http.h"
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
4 #include "core/session.h"
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
5 #include "core/strings.h"
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
6 #include "gui/widgets/clickable_label.h"
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
7
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
8 #include <QByteArray>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
9 #include <QDebug>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
10 #include <QDesktopServices>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
11 #include <QFrame>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
12 #include <QHBoxLayout>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
13 #include <QLabel>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
14 #include <QMessageBox>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
15 #include <QPixmap>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
16 #include <QThread>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
17 #include <QUrl>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
18
348
6b0768158dcd text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
19 #include <iostream>
6b0768158dcd text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
20
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
21 Poster::Poster(QWidget* parent) : QFrame(parent) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
22 QHBoxLayout* layout = new QHBoxLayout(this);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
23 layout->setContentsMargins(1, 1, 1, 1);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
24
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
25 setCursor(Qt::PointingHandCursor);
348
6b0768158dcd text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
26 setFixedSize(150, 225); // FIXME need to kill this
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
27 setFrameShape(QFrame::Box);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
28 setFrameShadow(QFrame::Plain);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
29
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
30 label_.setAlignment(Qt::AlignCenter);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
31 layout->addWidget(&label_);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
32
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
33 get_thread_ = new HTTP::RequestThread(HTTP::Type::Get);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
34 connect(get_thread_, &HTTP::RequestThread::ReceivedData, this, &Poster::ImageDownloadFinished);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
35 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
36
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
37 Poster::Poster(const Anime::Anime& anime, QWidget* parent) : Poster(parent) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
38 SetAnime(anime);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
39 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
40
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
41 Poster::~Poster() {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
42 /* schedule deletion of the thread */
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
43 get_thread_->deleteLater();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
44 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
45
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
46 void Poster::DownloadPoster() {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
47 if (get_thread_->isRunning())
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
48 get_thread_->Stop();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
49 get_thread_->wait();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
50
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
51 get_thread_->SetUrl(poster_url_);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
52 get_thread_->start();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
53 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
54
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
55 void Poster::SetAnime(const Anime::Anime& anime) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
56 label_.clear();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
57
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
58 poster_url_ = anime.GetPosterUrl();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
59 if (isVisible())
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
60 DownloadPoster();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
61 else
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
62 need_refresh_ = true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
63
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
64 std::optional<std::string> url = anime.GetServiceUrl(session.config.service);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
65 if (url)
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
66 service_url_ = Strings::ToQString(url.value());
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
67
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
68 if (clickable_) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
69 label_.disconnect();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
70 connect(&label_, &ClickableLabel::clicked, this, [this] { QDesktopServices::openUrl(service_url_); });
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
71 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
72 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
73
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
74 void Poster::showEvent(QShowEvent* event) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
75 if (need_refresh_) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
76 DownloadPoster();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
77 need_refresh_ = false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
78 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
79 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
80
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
81 void Poster::SetClickable(bool enabled) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
82 clickable_ = enabled;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
83
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
84 if (clickable_ && !service_url_.isEmpty()) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
85 setCursor(Qt::PointingHandCursor);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
86 label_.disconnect();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
87 connect(&label_, &ClickableLabel::clicked, this, [this] { QDesktopServices::openUrl(service_url_); });
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
88 } else {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
89 setCursor(Qt::ArrowCursor);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
90 label_.disconnect();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
91 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
92 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
93
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
94 void Poster::ImageDownloadFinished(const QByteArray& arr) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
95 img_.loadFromData(arr);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
96 RenderToLabel();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
97 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
98
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
99 void Poster::RenderToLabel() {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
100 const QPixmap pixmap = QPixmap::fromImage(img_);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
101 if (pixmap.isNull())
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
102 return;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
103 label_.setPixmap(pixmap.scaled(label_.size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
104 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
105
348
6b0768158dcd text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
106 bool Poster::hasHeightForWidth(void) const {
6b0768158dcd text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
107 return true;
6b0768158dcd text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
108 }
6b0768158dcd text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
109
6b0768158dcd text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
110 int Poster::heightForWidth(int w) const {
6b0768158dcd text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
111 return static_cast<int>(static_cast<double>(w) * 225 / 150);
6b0768158dcd text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
112 }
6b0768158dcd text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
113
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
114 void Poster::resizeEvent(QResizeEvent*) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
115 RenderToLabel();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 295
diff changeset
116 }
348
6b0768158dcd text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
117
6b0768158dcd text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
118 QSize Poster::minimumSizeHint() const {
6b0768158dcd text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
119 return QSize(120, heightForWidth(120));
6b0768158dcd text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
120 }
6b0768158dcd text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
121
6b0768158dcd text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
122 QSize Poster::sizeHint() const {
6b0768158dcd text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
123 return QSize(150, heightForWidth(150));
6b0768158dcd text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
124 }