Mercurial > minori
annotate src/gui/widgets/poster.cpp @ 68:2417121d894e
*: normalize usage of layouts
before, I used them two ways, once was by setting the layout later
by using setLayout(QWidget), and the other was just using the constructor.
I find the constructor to be easier to read, so I chose that one.
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Mon, 02 Oct 2023 21:33:25 -0400 |
parents | 442065432549 |
children | d3e9310598b1 |
rev | line source |
---|---|
66 | 1 #include "gui/widgets/poster.h" |
67
442065432549
poster: make posters link to AniList
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
2 #include "gui/widgets/clickable_label.h" |
66 | 3 #include "core/anime_db.h" |
67
442065432549
poster: make posters link to AniList
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
4 #include "core/strings.h" |
66 | 5 #include "core/session.h" |
6 #include <QFrame> | |
7 #include <QMessageBox> | |
8 #include <QLabel> | |
9 #include <QHBoxLayout> | |
10 #include <QByteArray> | |
67
442065432549
poster: make posters link to AniList
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
11 #include <QDesktopServices> |
442065432549
poster: make posters link to AniList
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
12 #include <QUrl> |
442065432549
poster: make posters link to AniList
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
13 #include <QDebug> |
66 | 14 #include <QPixmap> |
15 #include <curl/curl.h> | |
16 | |
17 static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userdata) { | |
18 reinterpret_cast<QByteArray*>(userdata)->append(reinterpret_cast<char*>(contents), size * nmemb); | |
19 return size * nmemb; | |
20 } | |
21 | |
22 static QByteArray SendRequest(std::string url) { | |
23 QByteArray userdata; | |
24 CURL* curl = curl_easy_init(); | |
25 if (curl) { | |
26 curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); | |
27 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userdata); | |
28 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); | |
29 /* Use system certs... useful on Windows. */ | |
30 curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA); | |
31 CURLcode res = curl_easy_perform(curl); | |
32 session.IncrementRequests(); | |
33 curl_easy_cleanup(curl); | |
34 if (res != CURLE_OK) { | |
35 QMessageBox box(QMessageBox::Icon::Critical, "", | |
36 QString("curl_easy_perform(curl) failed!: ") + QString(curl_easy_strerror(res))); | |
37 box.exec(); | |
38 } | |
39 } | |
40 return userdata; | |
41 } | |
42 | |
43 Poster::Poster(int id, QWidget* parent) : QFrame(parent) { | |
44 QHBoxLayout* layout = new QHBoxLayout(this); | |
45 layout->setContentsMargins(1, 1, 1, 1); | |
46 | |
67
442065432549
poster: make posters link to AniList
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
47 setCursor(Qt::PointingHandCursor); |
66 | 48 setFixedSize(150, 225); |
49 setFrameShape(QFrame::Box); | |
50 setFrameShadow(QFrame::Plain); | |
51 | |
52 const Anime::Anime& anime = Anime::db.items[id]; | |
53 QByteArray ret = SendRequest(anime.GetPosterUrl()); | |
54 | |
55 img.loadFromData(ret); | |
56 QPixmap pixmap = QPixmap::fromImage(img); | |
57 | |
67
442065432549
poster: make posters link to AniList
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
58 label = new ClickableLabel(this); |
68
2417121d894e
*: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents:
67
diff
changeset
|
59 label->setAlignment(Qt::AlignCenter); |
67
442065432549
poster: make posters link to AniList
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
60 connect(label, &ClickableLabel::clicked, this, [anime]{ |
442065432549
poster: make posters link to AniList
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
61 QDesktopServices::openUrl(Strings::ToQString(anime.GetServiceUrl())); |
442065432549
poster: make posters link to AniList
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
62 }); |
66 | 63 layout->addWidget(label); |
64 } | |
65 | |
67
442065432549
poster: make posters link to AniList
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
66 void Poster::resizeEvent(QResizeEvent*) { |
68
2417121d894e
*: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents:
67
diff
changeset
|
67 QPixmap pixmap = QPixmap::fromImage(img).scaled(label->size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); |
67
442065432549
poster: make posters link to AniList
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
68 label->setPixmap(pixmap); |
66 | 69 } |
70 | |
71 #include "gui/widgets/moc_poster.cpp" |