66
|
1 #include "gui/widgets/poster.h"
|
|
2 #include "core/anime_db.h"
|
|
3 #include "core/session.h"
|
|
4 #include <QFrame>
|
|
5 #include <QMessageBox>
|
|
6 #include <QLabel>
|
|
7 #include <QHBoxLayout>
|
|
8 #include <QByteArray>
|
|
9 #include <QPixmap>
|
|
10 #include <curl/curl.h>
|
|
11
|
|
12 static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userdata) {
|
|
13 reinterpret_cast<QByteArray*>(userdata)->append(reinterpret_cast<char*>(contents), size * nmemb);
|
|
14 return size * nmemb;
|
|
15 }
|
|
16
|
|
17 static QByteArray SendRequest(std::string url) {
|
|
18 QByteArray userdata;
|
|
19 CURL* curl = curl_easy_init();
|
|
20 if (curl) {
|
|
21 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
|
22 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userdata);
|
|
23 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
|
|
24 /* Use system certs... useful on Windows. */
|
|
25 curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA);
|
|
26 CURLcode res = curl_easy_perform(curl);
|
|
27 session.IncrementRequests();
|
|
28 curl_easy_cleanup(curl);
|
|
29 if (res != CURLE_OK) {
|
|
30 QMessageBox box(QMessageBox::Icon::Critical, "",
|
|
31 QString("curl_easy_perform(curl) failed!: ") + QString(curl_easy_strerror(res)));
|
|
32 box.exec();
|
|
33 }
|
|
34 }
|
|
35 return userdata;
|
|
36 }
|
|
37
|
|
38 Poster::Poster(int id, QWidget* parent) : QFrame(parent) {
|
|
39 QHBoxLayout* layout = new QHBoxLayout(this);
|
|
40 layout->setContentsMargins(1, 1, 1, 1);
|
|
41
|
|
42 setFixedSize(150, 225);
|
|
43 setFrameShape(QFrame::Box);
|
|
44 setFrameShadow(QFrame::Plain);
|
|
45
|
|
46 const Anime::Anime& anime = Anime::db.items[id];
|
|
47 QByteArray ret = SendRequest(anime.GetPosterUrl());
|
|
48
|
|
49 img.loadFromData(ret);
|
|
50 QPixmap pixmap = QPixmap::fromImage(img);
|
|
51
|
|
52 label = new QLabel(this);
|
|
53 label->setAlignment(Qt::AlignCenter);
|
|
54 layout->addWidget(label);
|
|
55 }
|
|
56
|
|
57 void Poster::resizeEvent(QResizeEvent* event) {
|
|
58 QPixmap pixmap = QPixmap::fromImage(img);
|
|
59 label->setPixmap(pixmap.scaled(label->width(), label->height(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
|
|
60 }
|
|
61
|
|
62 #include "gui/widgets/moc_poster.cpp"
|