view src/gui/widgets/anime_info.cc @ 337:a7d4e5107531

dep/animone: REFACTOR ALL THE THINGS 1: animone now has its own syntax divergent from anisthesia, making different platforms actually have their own sections 2: process names in animone are now called `comm' (this will probably break things). this is what its called in bsd/linux so I'm just going to use it everywhere 3: the X11 code now checks for the existence of a UTF-8 window title and passes it if available 4: ANYTHING THATS NOT LINUX IS 100% UNTESTED AND CAN AND WILL BREAK! I still actually need to test the bsd code. to be honest I'm probably going to move all of the bsds into separate files because they're all essentially different operating systems at this point
author Paper <paper@paper.us.eu.org>
date Wed, 19 Jun 2024 12:51:15 -0400
parents b5d6c27c308f
children 6b0768158dcd
line wrap: on
line source

#include "gui/widgets/anime_info.h"
#include "core/anime.h"
#include "core/anime_db.h"
#include "core/strings.h"
#include "gui/translate/anime.h"
#include "gui/widgets/text.h"
#include "services/services.h"
#include <QHBoxLayout>
#include <QTextStream>

AnimeInfoWidgetGetMetadataThread::AnimeInfoWidgetGetMetadataThread(QObject* parent) : QThread(parent) {}

void AnimeInfoWidgetGetMetadataThread::AddToQueue(int id) {
	const std::lock_guard<std::mutex> guard(queue_mutex_);
	queue_.push(id);
}

/* processes the queue... */
void AnimeInfoWidgetGetMetadataThread::run() {
	queue_mutex_.lock();
	while (!queue_.empty() && !isInterruptionRequested()) {
		int id = queue_.front();

		queue_mutex_.unlock();

		if (Services::RetrieveAnimeMetadata(id))
			emit NeedRefresh(id);

		queue_mutex_.lock();

		queue_.pop();
	}
	queue_mutex_.unlock();
}

/* all widgets share this thread */
static AnimeInfoWidgetGetMetadataThread get_metadata_thread;

AnimeInfoWidget::AnimeInfoWidget(QWidget* parent)
	: QWidget(parent)
	, _title(tr("Alternative titles"), "")
	, _details(tr("Details"), tr("Type:\nEpisodes:\nStatus:\nSeason:\nGenres:\nProducers:\nScore:"), "")
	, _synopsis(tr("Synopsis"), "") {
	QVBoxLayout* layout = new QVBoxLayout(this);

	layout->addWidget(&_title);
	layout->addWidget(&_details);
	layout->addWidget(&_synopsis);

	/* ... */
	connect(&get_metadata_thread, &AnimeInfoWidgetGetMetadataThread::NeedRefresh, this, [this](int id) {
		setUpdatesEnabled(false);

		if (id == id_)
			RefreshGenres(Anime::db.items[id]);

		setUpdatesEnabled(true);
	});
}

AnimeInfoWidget::AnimeInfoWidget(const Anime::Anime& anime, QWidget* parent) : AnimeInfoWidget(parent) {
	SetAnime(anime);
}

void AnimeInfoWidget::SetAnime(const Anime::Anime& anime) {
	setUpdatesEnabled(false);

	id_ = anime.GetId();

	get_metadata_thread.AddToQueue(id_);
	if (!get_metadata_thread.isRunning())
		get_metadata_thread.start();

	/* alt titles */
	_title.GetLine()->SetText(Strings::ToQString(Strings::Implode(anime.GetTitleSynonyms(), ", ")));

	RefreshGenres(anime);

	_synopsis.GetParagraph()->SetText(Strings::ToQString(anime.GetSynopsis()));

	setUpdatesEnabled(true);

	updateGeometry();
}

void AnimeInfoWidget::RefreshGenres(const Anime::Anime& anime) {
	/* details */
	QString details_data;
	QTextStream details_data_s(&details_data);

	/* we have to convert ALL of these strings to
	 * QString because QTextStream sucks and assumes
	 * Latin-1 (on Windows?) */
	const auto genres = anime.GetGenres();
	const auto producers = anime.GetProducers();

	details_data_s << Strings::ToQString(Translate::ToLocalString(anime.GetFormat())) << "\n"
	               << anime.GetEpisodes() << "\n"
	               << Strings::ToQString(Translate::ToLocalString(anime.GetAiringStatus())) << "\n"
	               << Strings::ToQString(Translate::ToLocalString(anime.GetSeason())) << "\n"
	               << Strings::ToQString((genres.size() > 1)    ? Strings::Implode(genres, ", ")    : "-") << "\n"
	               << Strings::ToQString((producers.size() > 1) ? Strings::Implode(producers, ", ") : "-") << "\n"
	               << anime.GetAudienceScore() << "%";

	_details.GetData()->setText(details_data);
}