diff 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
line wrap: on
line diff
--- a/src/gui/widgets/poster.cpp	Tue Oct 03 06:12:43 2023 -0400
+++ b/src/gui/widgets/poster.cpp	Wed Oct 04 01:42:30 2023 -0400
@@ -1,6 +1,7 @@
 #include "gui/widgets/poster.h"
 #include "gui/widgets/clickable_label.h"
 #include "core/anime_db.h"
+#include "core/http.h"
 #include "core/strings.h"
 #include "core/session.h"
 #include <QFrame>
@@ -14,32 +15,6 @@
 #include <QPixmap>
 #include <curl/curl.h>
 
-static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userdata) {
-	reinterpret_cast<QByteArray*>(userdata)->append(reinterpret_cast<char*>(contents), size * nmemb);
-	return size * nmemb;
-}
-
-static QByteArray SendRequest(std::string url) {
-	QByteArray userdata;
-	CURL* curl = curl_easy_init();
-	if (curl) {
-		curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
-		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userdata);
-		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
-		/* Use system certs... useful on Windows. */
-		curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA);
-		CURLcode res = curl_easy_perform(curl);
-		session.IncrementRequests();
-		curl_easy_cleanup(curl);
-		if (res != CURLE_OK) {
-			QMessageBox box(QMessageBox::Icon::Critical, "",
-			                QString("curl_easy_perform(curl) failed!: ") + QString(curl_easy_strerror(res)));
-			box.exec();
-		}
-	}
-	return userdata;
-}
-
 Poster::Poster(int id, QWidget* parent) : QFrame(parent) {
 	QHBoxLayout* layout = new QHBoxLayout(this);
 	layout->setContentsMargins(1, 1, 1, 1);
@@ -50,9 +25,12 @@
 	setFrameShadow(QFrame::Plain);
 	
 	const Anime::Anime& anime = Anime::db.items[id];
-	QByteArray ret = SendRequest(anime.GetPosterUrl());
 
-	img.loadFromData(ret);
+	HTTP::HttpGetThread *image_thread = new HTTP::HttpGetThread(anime.GetPosterUrl(), {}, this);
+	connect(image_thread, &HTTP::HttpGetThread::resultReady, this, &Poster::ImageDownloadFinished);
+	connect(image_thread, &HTTP::HttpGetThread::finished, image_thread, &QObject::deleteLater);
+	image_thread->start();
+
 	QPixmap pixmap = QPixmap::fromImage(img);
 
 	label = new ClickableLabel(this);
@@ -63,9 +41,19 @@
 	layout->addWidget(label);
 }
 
+void Poster::ImageDownloadFinished(QByteArray arr) {
+	img.loadFromData(arr);
+	RenderToLabel();
+}
+
+void Poster::RenderToLabel() {
+	QPixmap pixmap = QPixmap::fromImage(img);
+	if (pixmap.isNull()) return;
+	label->setPixmap(pixmap.scaled(label->size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
+}
+
 void Poster::resizeEvent(QResizeEvent*) {
-	QPixmap pixmap = QPixmap::fromImage(img).scaled(label->size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
-	label->setPixmap(pixmap);
+	RenderToLabel();
 }
 
 #include "gui/widgets/moc_poster.cpp"