Mercurial > minori
comparison src/gui/widgets/poster.cpp @ 75:d3e9310598b1
*: refactor some stuff
text: "TextParagraph"s are now called sections, because that's the
actual word for it :P
text: new classes: Line and OneLineSection, solves many problems with
paragraphs that are only one line long (ex. going out of bounds)
http: reworked http stuff to allow threaded get requests, also moved it
to its own file to (hopefully) remove clutter
eventually I'll make a threaded post request method and use that in
the "basic" function
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 04 Oct 2023 01:42:30 -0400 |
parents | 2417121d894e |
children | 3364fadc8a36 |
comparison
equal
deleted
inserted
replaced
74:5ccb99bfa605 | 75:d3e9310598b1 |
---|---|
1 #include "gui/widgets/poster.h" | 1 #include "gui/widgets/poster.h" |
2 #include "gui/widgets/clickable_label.h" | 2 #include "gui/widgets/clickable_label.h" |
3 #include "core/anime_db.h" | 3 #include "core/anime_db.h" |
4 #include "core/http.h" | |
4 #include "core/strings.h" | 5 #include "core/strings.h" |
5 #include "core/session.h" | 6 #include "core/session.h" |
6 #include <QFrame> | 7 #include <QFrame> |
7 #include <QMessageBox> | 8 #include <QMessageBox> |
8 #include <QLabel> | 9 #include <QLabel> |
12 #include <QUrl> | 13 #include <QUrl> |
13 #include <QDebug> | 14 #include <QDebug> |
14 #include <QPixmap> | 15 #include <QPixmap> |
15 #include <curl/curl.h> | 16 #include <curl/curl.h> |
16 | 17 |
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) { | 18 Poster::Poster(int id, QWidget* parent) : QFrame(parent) { |
44 QHBoxLayout* layout = new QHBoxLayout(this); | 19 QHBoxLayout* layout = new QHBoxLayout(this); |
45 layout->setContentsMargins(1, 1, 1, 1); | 20 layout->setContentsMargins(1, 1, 1, 1); |
46 | 21 |
47 setCursor(Qt::PointingHandCursor); | 22 setCursor(Qt::PointingHandCursor); |
48 setFixedSize(150, 225); | 23 setFixedSize(150, 225); |
49 setFrameShape(QFrame::Box); | 24 setFrameShape(QFrame::Box); |
50 setFrameShadow(QFrame::Plain); | 25 setFrameShadow(QFrame::Plain); |
51 | 26 |
52 const Anime::Anime& anime = Anime::db.items[id]; | 27 const Anime::Anime& anime = Anime::db.items[id]; |
53 QByteArray ret = SendRequest(anime.GetPosterUrl()); | |
54 | 28 |
55 img.loadFromData(ret); | 29 HTTP::HttpGetThread *image_thread = new HTTP::HttpGetThread(anime.GetPosterUrl(), {}, this); |
30 connect(image_thread, &HTTP::HttpGetThread::resultReady, this, &Poster::ImageDownloadFinished); | |
31 connect(image_thread, &HTTP::HttpGetThread::finished, image_thread, &QObject::deleteLater); | |
32 image_thread->start(); | |
33 | |
56 QPixmap pixmap = QPixmap::fromImage(img); | 34 QPixmap pixmap = QPixmap::fromImage(img); |
57 | 35 |
58 label = new ClickableLabel(this); | 36 label = new ClickableLabel(this); |
59 label->setAlignment(Qt::AlignCenter); | 37 label->setAlignment(Qt::AlignCenter); |
60 connect(label, &ClickableLabel::clicked, this, [anime]{ | 38 connect(label, &ClickableLabel::clicked, this, [anime]{ |
61 QDesktopServices::openUrl(Strings::ToQString(anime.GetServiceUrl())); | 39 QDesktopServices::openUrl(Strings::ToQString(anime.GetServiceUrl())); |
62 }); | 40 }); |
63 layout->addWidget(label); | 41 layout->addWidget(label); |
64 } | 42 } |
65 | 43 |
44 void Poster::ImageDownloadFinished(QByteArray arr) { | |
45 img.loadFromData(arr); | |
46 RenderToLabel(); | |
47 } | |
48 | |
49 void Poster::RenderToLabel() { | |
50 QPixmap pixmap = QPixmap::fromImage(img); | |
51 if (pixmap.isNull()) return; | |
52 label->setPixmap(pixmap.scaled(label->size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation)); | |
53 } | |
54 | |
66 void Poster::resizeEvent(QResizeEvent*) { | 55 void Poster::resizeEvent(QResizeEvent*) { |
67 QPixmap pixmap = QPixmap::fromImage(img).scaled(label->size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); | 56 RenderToLabel(); |
68 label->setPixmap(pixmap); | |
69 } | 57 } |
70 | 58 |
71 #include "gui/widgets/moc_poster.cpp" | 59 #include "gui/widgets/moc_poster.cpp" |