diff src/gui/widgets/poster.cc @ 81:9b2b41f83a5e

boring: mass rename to cc because this is a very unix-y project, it makes more sense to use the 'cc' extension
author Paper <mrpapersonic@gmail.com>
date Mon, 23 Oct 2023 12:07:27 -0400
parents src/gui/widgets/poster.cpp@6f7385bd334c
children d02fdf1d6708
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/gui/widgets/poster.cc	Mon Oct 23 12:07:27 2023 -0400
@@ -0,0 +1,60 @@
+#include "gui/widgets/poster.h"
+#include "core/anime_db.h"
+#include "core/http.h"
+#include "core/session.h"
+#include "core/strings.h"
+#include "gui/widgets/clickable_label.h"
+#include <QByteArray>
+#include <QDebug>
+#include <QDesktopServices>
+#include <QFrame>
+#include <QHBoxLayout>
+#include <QLabel>
+#include <QMessageBox>
+#include <QPixmap>
+#include <QThreadPool>
+#include <QUrl>
+#include <curl/curl.h>
+
+Poster::Poster(int id, QWidget* parent) : QFrame(parent) {
+	QHBoxLayout* layout = new QHBoxLayout(this);
+	layout->setContentsMargins(1, 1, 1, 1);
+
+	setCursor(Qt::PointingHandCursor);
+	setFixedSize(150, 225);
+	setFrameShape(QFrame::Box);
+	setFrameShadow(QFrame::Plain);
+
+	const Anime::Anime& anime = Anime::db.items[id];
+
+	QThreadPool::globalInstance()->start([this, anime] {
+		QByteArray ba = HTTP::Get(anime.GetPosterUrl(), {});
+		ImageDownloadFinished(ba);
+	});
+
+	QPixmap pixmap = QPixmap::fromImage(img);
+
+	label = new ClickableLabel(this);
+	label->setAlignment(Qt::AlignCenter);
+	connect(label, &ClickableLabel::clicked, this,
+	        [anime] { QDesktopServices::openUrl(Strings::ToQString(anime.GetServiceUrl())); });
+	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*) {
+	RenderToLabel();
+}
+
+#include "gui/widgets/moc_poster.cpp"