annotate src/gui/widgets/poster.cpp @ 66:6481c5aed3e1

posters: add poster widget... why does AniList call these cover images? they're posters...
author Paper <mrpapersonic@gmail.com>
date Mon, 02 Oct 2023 05:56:32 -0400
parents
children 442065432549
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
66
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
1 #include "gui/widgets/poster.h"
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
2 #include "core/anime_db.h"
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3 #include "core/session.h"
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4 #include <QFrame>
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 #include <QMessageBox>
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6 #include <QLabel>
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 #include <QHBoxLayout>
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 #include <QByteArray>
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9 #include <QPixmap>
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 #include <curl/curl.h>
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12 static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userdata) {
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13 reinterpret_cast<QByteArray*>(userdata)->append(reinterpret_cast<char*>(contents), size * nmemb);
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14 return size * nmemb;
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15 }
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17 static QByteArray SendRequest(std::string url) {
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18 QByteArray userdata;
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19 CURL* curl = curl_easy_init();
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20 if (curl) {
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userdata);
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24 /* Use system certs... useful on Windows. */
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25 curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA);
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
26 CURLcode res = curl_easy_perform(curl);
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27 session.IncrementRequests();
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
28 curl_easy_cleanup(curl);
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29 if (res != CURLE_OK) {
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30 QMessageBox box(QMessageBox::Icon::Critical, "",
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31 QString("curl_easy_perform(curl) failed!: ") + QString(curl_easy_strerror(res)));
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32 box.exec();
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
33 }
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
34 }
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35 return userdata;
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 }
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38 Poster::Poster(int id, QWidget* parent) : QFrame(parent) {
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 QHBoxLayout* layout = new QHBoxLayout(this);
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40 layout->setContentsMargins(1, 1, 1, 1);
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 setFixedSize(150, 225);
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 setFrameShape(QFrame::Box);
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44 setFrameShadow(QFrame::Plain);
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46 const Anime::Anime& anime = Anime::db.items[id];
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 QByteArray ret = SendRequest(anime.GetPosterUrl());
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49 img.loadFromData(ret);
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50 QPixmap pixmap = QPixmap::fromImage(img);
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 label = new QLabel(this);
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53 label->setAlignment(Qt::AlignCenter);
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54 layout->addWidget(label);
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 }
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57 void Poster::resizeEvent(QResizeEvent* event) {
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58 QPixmap pixmap = QPixmap::fromImage(img);
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
59 label->setPixmap(pixmap.scaled(label->width(), label->height(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 }
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62 #include "gui/widgets/moc_poster.cpp"