changeset 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 26721c28bf22
children 442065432549
files CMakeLists.txt include/core/anime.h include/gui/widgets/poster.h src/core/anime.cpp src/gui/dialog/information.cpp src/gui/pages/now_playing.cpp src/gui/widgets/poster.cpp src/services/anilist.cpp src/sys/win32/dark_theme.cpp
diffstat 9 files changed, 120 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Sun Oct 01 23:26:35 2023 -0400
+++ b/CMakeLists.txt	Mon Oct 02 05:56:32 2023 -0400
@@ -76,6 +76,7 @@
 
 	# Custom widgets
 	src/gui/widgets/anime_info.cpp
+	src/gui/widgets/poster.cpp
 	src/gui/widgets/sidebar.cpp
 	src/gui/widgets/text.cpp
 	src/gui/widgets/optional_date.cpp
--- a/include/core/anime.h	Sun Oct 01 23:26:35 2023 -0400
+++ b/include/core/anime.h	Mon Oct 02 05:56:32 2023 -0400
@@ -94,6 +94,7 @@
 		int audience_score = 0;
 		std::string synopsis;
 		int duration = 0;
+		std::string poster_url;
 };
 
 class Anime {
@@ -137,6 +138,7 @@
 		int GetAudienceScore() const; /* should be double once MAL and Kitsu are implemented */
 		std::string GetSynopsis() const;
 		int GetDuration() const;
+		std::string GetPosterUrl() const;
 
 		void SetId(int id);
 		void SetRomajiTitle(std::string const& title);
@@ -154,6 +156,7 @@
 		void SetAudienceScore(int audience_score);
 		void SetSynopsis(std::string synopsis);
 		void SetDuration(int duration);
+		void SetPosterUrl(std::string poster);
 
 		std::string GetUserPreferredTitle() const;
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/gui/widgets/poster.h	Mon Oct 02 05:56:32 2023 -0400
@@ -0,0 +1,23 @@
+#ifndef __gui__widgets__poster_h
+#define __gui__widgets__poster_h
+#include <QFrame>
+#include <QImage>
+
+class QWidget;
+class QLabel;
+
+class Poster : public QFrame {
+		Q_OBJECT
+
+	public:
+		Poster(int id, QWidget* parent = nullptr);
+
+	protected:
+		void resizeEvent(QResizeEvent* event) override;
+
+	private:
+		QImage img;
+		QLabel* label;
+};
+
+#endif // __gui__widgets__poster_h
\ No newline at end of file
--- a/src/core/anime.cpp	Sun Oct 01 23:26:35 2023 -0400
+++ b/src/core/anime.cpp	Mon Oct 02 05:56:32 2023 -0400
@@ -202,6 +202,10 @@
 	return info_.duration;
 }
 
+std::string Anime::GetPosterUrl() const {
+	return info_.poster_url;
+}
+
 void Anime::SetId(int id) {
 	info_.id = id;
 }
@@ -266,6 +270,10 @@
 	info_.duration = duration;
 }
 
+void Anime::SetPosterUrl(std::string url) {
+	info_.poster_url = url;
+}
+
 std::string Anime::GetUserPreferredTitle() const {
 	switch (session.config.anime_list.language) {
 		case TitleLanguage::NATIVE: return (GetNativeTitle().empty()) ? GetRomajiTitle() : GetNativeTitle();
--- a/src/gui/dialog/information.cpp	Sun Oct 01 23:26:35 2023 -0400
+++ b/src/gui/dialog/information.cpp	Mon Oct 02 05:56:32 2023 -0400
@@ -6,6 +6,7 @@
 #include "gui/pages/anime_list.h"
 #include "gui/translate/anime.h"
 #include "gui/widgets/anime_info.h"
+#include "gui/widgets/poster.h"
 #include "gui/widgets/optional_date.h"
 #include "gui/widgets/text.h"
 #include "gui/window.h"
@@ -51,6 +52,11 @@
 
 	/* "sidebar", includes... just the anime image :) */
 	QWidget* sidebar = new QWidget(widget);
+	QVBoxLayout* sidebar_layout = new QVBoxLayout(sidebar);
+	Poster* poster = new Poster(anime.GetId(), sidebar);
+	sidebar_layout->addWidget(poster);
+	sidebar_layout->setContentsMargins(0, 0, 0, 0);
+	sidebar_layout->addStretch();
 	sidebar->setFixedWidth(175);
 
 	/* main widget */
--- a/src/gui/pages/now_playing.cpp	Sun Oct 01 23:26:35 2023 -0400
+++ b/src/gui/pages/now_playing.cpp	Mon Oct 02 05:56:32 2023 -0400
@@ -21,13 +21,7 @@
 
 	public:
 		Playing(QWidget* parent = nullptr);
-
-		void SetPlayingAnime(int id) {
-			if (info.get())
-				layout()->removeWidget(info.get());
-			info.reset(new AnimeInfoWidget(Anime::db.items[id]));
-			layout()->addWidget(info.get());
-		}
+		void SetPlayingAnime(int id);
 
 	private:
 		std::unique_ptr<AnimeInfoWidget> info = nullptr;
@@ -41,10 +35,18 @@
 }
 
 Playing::Playing(QWidget* parent) : QWidget(parent) {
-	QVBoxLayout* layout = new QVBoxLayout(this);
+	QHBoxLayout* layout = new QHBoxLayout(this);
+	
 	layout->setContentsMargins(0, 0, 0, 0);
 }
 
+void Playing::SetPlayingAnime(int id) {
+	if (info.get())
+		layout()->removeWidget(info.get());
+	info.reset(new AnimeInfoWidget(Anime::db.items[id]));
+	layout()->addWidget(info.get());
+}
+
 } // namespace NowPlayingPages
 
 NowPlayingPage::NowPlayingPage(QWidget* parent) : QFrame(parent) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/gui/widgets/poster.cpp	Mon Oct 02 05:56:32 2023 -0400
@@ -0,0 +1,62 @@
+#include "gui/widgets/poster.h"
+#include "core/anime_db.h"
+#include "core/session.h"
+#include <QFrame>
+#include <QMessageBox>
+#include <QLabel>
+#include <QHBoxLayout>
+#include <QByteArray>
+#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);
+
+	setFixedSize(150, 225);
+	setFrameShape(QFrame::Box);
+	setFrameShadow(QFrame::Plain);
+	
+	const Anime::Anime& anime = Anime::db.items[id];
+	QByteArray ret = SendRequest(anime.GetPosterUrl());
+
+	img.loadFromData(ret);
+	QPixmap pixmap = QPixmap::fromImage(img);
+
+	label = new QLabel(this);
+	label->setAlignment(Qt::AlignCenter);
+	layout->addWidget(label);
+}
+
+void Poster::resizeEvent(QResizeEvent* event) {
+	QPixmap pixmap = QPixmap::fromImage(img);
+	label->setPixmap(pixmap.scaled(label->width(), label->height(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
+}
+
+#include "gui/widgets/moc_poster.cpp"
--- a/src/services/anilist.cpp	Sun Oct 01 23:26:35 2023 -0400
+++ b/src/services/anilist.cpp	Mon Oct 02 05:56:32 2023 -0400
@@ -157,6 +157,8 @@
 
 	anime.SetAirDate(ParseDate(json["/startDate"_json_pointer]));
 
+	anime.SetPosterUrl(JSON::GetString(json, "/coverImage/large"_json_pointer));
+
 	anime.SetAudienceScore(JSON::GetInt(json, "/averageScore"_json_pointer));
 	anime.SetSeason(Translate::AniList::ToSeriesSeason(JSON::GetString(json, "/season"_json_pointer)));
 	anime.SetDuration(JSON::GetInt(json, "/duration"_json_pointer));
@@ -219,6 +221,9 @@
 	                          "        }\n"
 	                          "        updatedAt\n"
 	                          "        media {\n"
+	                          "          coverImage {\n"
+	                          "            large\n"
+	                          "          }\n"
 	                          "          id\n"
 	                          "          title {\n"
 	                          "            romaji\n"
--- a/src/sys/win32/dark_theme.cpp	Sun Oct 01 23:26:35 2023 -0400
+++ b/src/sys/win32/dark_theme.cpp	Mon Oct 02 05:56:32 2023 -0400
@@ -7,13 +7,8 @@
 bool DarkThemeAvailable() {
 	// dark mode supported Windows 10 1809 10.0.17763 onward
 	// https://stackoverflow.com/questions/53501268/win10-dark-theme-how-to-use-in-winapi
-	if (QOperatingSystemVersion::current().majorVersion() == 10) {
-		return QOperatingSystemVersion::current().microVersion() >= 17763;
-	} else if (QOperatingSystemVersion::current().majorVersion() > 10) {
-		return true;
-	} else {
-		return false;
-	}
+	const auto& ver = QOperatingSystemVersion::current();
+	return (ver.majorVersion() > 10) ? true : (ver.majorVersion() == 10 && ver.microVersion() >= 17763);
 }
 
 bool IsInDarkTheme() {