Mercurial > minori
view src/anime.cpp @ 6:1d82f6e04d7d
Update: add first parts to the settings dialog
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 16 Aug 2023 00:49:17 -0400 |
parents | 190ded9438c0 |
children | 07a9095eaeed |
line wrap: on
line source
#include <chrono> #include <string> #include <vector> #include <cmath> #include "window.h" #include "anilist.h" #include "config.h" #include "anime.h" #include "date.h" #include "time_utils.h" #include "information.h" #include "ui_utils.h" #include <iostream> std::map<enum AnimeWatchingStatus, std::string> AnimeWatchingToStringMap = { {CURRENT, "Watching"}, {PLANNING, "Planning"}, {COMPLETED, "Completed"}, {DROPPED, "Dropped"}, {PAUSED, "On hold"}, {REPEATING, "Rewatching"} }; std::map<enum AnimeAiringStatus, std::string> AnimeAiringToStringMap = { {FINISHED, "Finished"}, {RELEASING, "Airing"}, {NOT_YET_RELEASED, "Not aired yet"}, {CANCELLED, "Cancelled"}, {HIATUS, "On hiatus"} }; std::map<enum AnimeSeason, std::string> AnimeSeasonToStringMap = { {UNKNOWN, "Unknown"}, {WINTER, "Winter"}, {SPRING, "Spring"}, {SUMMER, "Summer"}, {FALL, "Fall"} }; std::map<enum AnimeFormat, std::string> AnimeFormatToStringMap = { {TV, "TV"}, {TV_SHORT, "TV short"}, {MOVIE, "Movie"}, {SPECIAL, "Special"}, {OVA, "OVA"}, {ONA, "ONA"}, {MUSIC, "Music video"}, /* these should NEVER be in the list. naybe we should remove them? */ {MANGA, "Manga"}, {NOVEL, "Novel"}, {ONE_SHOT, "One-shot"} }; Anime::Anime() {} Anime::Anime(const Anime& a) { status = a.status; progress = a.progress; score = a.score; started = a.started; completed = a.completed; updated = a.updated; notes = a.notes; id = a.id; title = a.title; episodes = a.episodes; airing = a.airing; air_date = a.air_date; genres = a.genres; producers = a.producers; type = a.type; season = a.season; audience_score = a.audience_score; synopsis = a.synopsis; duration = a.duration; } std::string Anime::GetUserPreferredTitle() { if (title.english.empty()) return title.romaji; return title.english; } void AnimeList::Add(Anime& anime) { if (anime_id_to_anime.contains(anime.id)) return; anime_list.push_back(anime); anime_id_to_anime.emplace(anime.id, &anime); } void AnimeList::Insert(size_t pos, Anime& anime) { if (anime_id_to_anime.contains(anime.id)) return; anime_list.insert(anime_list.begin()+pos, anime); anime_id_to_anime.emplace(anime.id, &anime); } void AnimeList::Delete(size_t index) { anime_list.erase(anime_list.begin()+index); } void AnimeList::Clear() { anime_list.clear(); } size_t AnimeList::Size() const { return anime_list.size(); } std::vector<Anime>::iterator AnimeList::begin() noexcept { return anime_list.begin(); } std::vector<Anime>::iterator AnimeList::end() noexcept { return anime_list.end(); } std::vector<Anime>::const_iterator AnimeList::cbegin() noexcept { return anime_list.cbegin(); } std::vector<Anime>::const_iterator AnimeList::cend() noexcept { return anime_list.cend(); } AnimeList::AnimeList() {} AnimeList::AnimeList(const AnimeList& l) { for (unsigned long long i = 0; i < l.Size(); i++) { anime_list.push_back(Anime(l[i])); } name = l.name; } AnimeList::~AnimeList() { anime_list.clear(); anime_list.shrink_to_fit(); } Anime* AnimeList::AnimeById(int id) { return anime_id_to_anime.contains(id) ? anime_id_to_anime[id] : nullptr; } bool AnimeList::AnimeInList(int id) { return anime_id_to_anime.contains(id); } int AnimeList::GetAnimeIndex(Anime& anime) const { for (unsigned long long i = 0; i < Size(); i++) { if (&anime_list.at(i) == &anime) { // lazy return i; } } return -1; } Anime& AnimeList::operator[](std::size_t index) { return anime_list.at(index); } const Anime& AnimeList::operator[](std::size_t index) const { return anime_list.at(index); } /* ------------------------------------------------------------------------- */ AnimeListWidgetDelegate::AnimeListWidgetDelegate(QObject* parent) : QStyledItemDelegate (parent) { } QWidget *AnimeListWidgetDelegate::createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const { // LOL return nullptr; } void AnimeListWidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { switch (index.column()) { case AnimeListWidgetModel::AL_PROGRESS: { const int progress = static_cast<int>(index.data(Qt::UserRole).toReal()); const int episodes = static_cast<int>(index.siblingAtColumn(AnimeListWidgetModel::AL_EPISODES).data(Qt::UserRole).toReal()); QStyleOptionViewItem customOption (option); customOption.state.setFlag(QStyle::State_Enabled, true); progress_bar.paint(painter, customOption, index.data().toString(), progress, episodes); break; } default: QStyledItemDelegate::paint(painter, option, index); break; } } AnimeListWidgetSortFilter::AnimeListWidgetSortFilter(QObject *parent) : QSortFilterProxyModel(parent) { } bool AnimeListWidgetSortFilter::lessThan(const QModelIndex &l, const QModelIndex &r) const { QVariant left = sourceModel()->data(l, sortRole()); QVariant right = sourceModel()->data(r, sortRole()); switch (left.userType()) { case QMetaType::Int: case QMetaType::UInt: case QMetaType::LongLong: case QMetaType::ULongLong: return left.toInt() < right.toInt(); case QMetaType::QDate: return left.toDate() < right.toDate(); case QMetaType::QString: default: return left.toString() < right.toString(); } } /* Thank you qBittorrent for having a great example of a widget model. */ AnimeListWidgetModel::AnimeListWidgetModel (QWidget* parent, AnimeList* alist) : QAbstractListModel(parent) , list(*alist) { return; } int AnimeListWidgetModel::rowCount(const QModelIndex& parent) const { return list.Size(); (void)(parent); } int AnimeListWidgetModel::columnCount(const QModelIndex& parent) const { return NB_COLUMNS; (void)(parent); } QVariant AnimeListWidgetModel::headerData(const int section, const Qt::Orientation orientation, const int role) const { if (role == Qt::DisplayRole) { switch (section) { case AL_TITLE: return tr("Anime title"); case AL_PROGRESS: return tr("Progress"); case AL_EPISODES: return tr("Episodes"); case AL_TYPE: return tr("Type"); case AL_SCORE: return tr("Score"); case AL_SEASON: return tr("Season"); case AL_STARTED: return tr("Date started"); case AL_COMPLETED: return tr("Date completed"); case AL_NOTES: return tr("Notes"); case AL_AVG_SCORE: return tr("Average score"); case AL_UPDATED: return tr("Last updated"); default: return {}; } } else if (role == Qt::TextAlignmentRole) { switch (section) { case AL_TITLE: case AL_NOTES: return QVariant(Qt::AlignLeft | Qt::AlignVCenter); case AL_PROGRESS: case AL_EPISODES: case AL_TYPE: case AL_SCORE: case AL_AVG_SCORE: return QVariant(Qt::AlignCenter | Qt::AlignVCenter); case AL_SEASON: case AL_STARTED: case AL_COMPLETED: case AL_UPDATED: return QVariant(Qt::AlignRight | Qt::AlignVCenter); default: return QAbstractListModel::headerData(section, orientation, role); } } return QAbstractListModel::headerData(section, orientation, role); } Anime* AnimeListWidgetModel::GetAnimeFromIndex(const QModelIndex& index) { return (index.isValid()) ? &(list[index.row()]) : nullptr; } QVariant AnimeListWidgetModel::data(const QModelIndex& index, int role) const { if (!index.isValid()) return QVariant(); switch (role) { case Qt::DisplayRole: switch (index.column()) { case AL_TITLE: return QString::fromUtf8(list[index.row()].GetUserPreferredTitle().c_str()); case AL_PROGRESS: return QString::number(list[index.row()].progress) + "/" + QString::number(list[index.row()].episodes); case AL_EPISODES: return list[index.row()].episodes; case AL_SCORE: return list[index.row()].score; case AL_TYPE: return QString::fromStdString(AnimeFormatToStringMap[list[index.row()].type]); case AL_SEASON: return QString::fromStdString(AnimeSeasonToStringMap[list[index.row()].season]) + " " + QString::number(list[index.row()].air_date.GetYear()); case AL_AVG_SCORE: return QString::number(list[index.row()].audience_score) + "%"; case AL_STARTED: return list[index.row()].started.GetAsQDate(); case AL_COMPLETED: return list[index.row()].completed.GetAsQDate(); case AL_UPDATED: { if (list[index.row()].updated == 0) return QString("-"); Time::Duration duration(Time::GetSystemTime() - list[index.row()].updated); return QString::fromUtf8(duration.AsRelativeString().c_str()); } case AL_NOTES: return QString::fromUtf8(list[index.row()].notes.c_str()); default: return ""; } break; case Qt::UserRole: switch (index.column()) { case AL_PROGRESS: return list[index.row()].progress; case AL_TYPE: return list[index.row()].type; case AL_SEASON: return list[index.row()].air_date.GetAsQDate(); case AL_AVG_SCORE: return list[index.row()].audience_score; case AL_UPDATED: return list[index.row()].updated; default: return data(index, Qt::DisplayRole); } break; case Qt::TextAlignmentRole: switch (index.column()) { case AL_TITLE: case AL_NOTES: return QVariant(Qt::AlignLeft | Qt::AlignVCenter); case AL_PROGRESS: case AL_EPISODES: case AL_TYPE: case AL_SCORE: case AL_AVG_SCORE: return QVariant(Qt::AlignCenter | Qt::AlignVCenter); case AL_SEASON: case AL_STARTED: case AL_COMPLETED: case AL_UPDATED: return QVariant(Qt::AlignRight | Qt::AlignVCenter); default: break; } break; } return QVariant(); } void AnimeListWidgetModel::UpdateAnime(Anime& anime) { int i = list.GetAnimeIndex(anime); emit dataChanged(index(i), index(i)); } int AnimeListWidget::VisibleColumnsCount() const { int count = 0; for (int i = 0, end = header()->count(); i < end; i++) { if (!isColumnHidden(i)) count++; } return count; } void AnimeListWidget::SetColumnDefaults() { setColumnHidden(AnimeListWidgetModel::AL_SEASON, false); setColumnHidden(AnimeListWidgetModel::AL_TYPE, false); setColumnHidden(AnimeListWidgetModel::AL_UPDATED, false); setColumnHidden(AnimeListWidgetModel::AL_PROGRESS, false); setColumnHidden(AnimeListWidgetModel::AL_SCORE, false); setColumnHidden(AnimeListWidgetModel::AL_TITLE, false); setColumnHidden(AnimeListWidgetModel::AL_EPISODES, true); setColumnHidden(AnimeListWidgetModel::AL_AVG_SCORE, true); setColumnHidden(AnimeListWidgetModel::AL_STARTED, true); setColumnHidden(AnimeListWidgetModel::AL_COMPLETED, true); setColumnHidden(AnimeListWidgetModel::AL_UPDATED, true); setColumnHidden(AnimeListWidgetModel::AL_NOTES, true); } void AnimeListWidget::DisplayColumnHeaderMenu() { QMenu *menu = new QMenu(this); menu->setAttribute(Qt::WA_DeleteOnClose); menu->setTitle(tr("Column visibility")); menu->setToolTipsVisible(true); for (int i = 0; i < AnimeListWidgetModel::NB_COLUMNS; i++) { if (i == AnimeListWidgetModel::AL_TITLE) continue; const auto column_name = model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString(); QAction *action = menu->addAction(column_name, this, [this, i](const bool checked) { if (!checked && (VisibleColumnsCount() <= 1)) return; setColumnHidden(i, !checked); if (checked && (columnWidth(i) <= 5)) resizeColumnToContents(i); // SaveSettings(); }); action->setCheckable(true); action->setChecked(!isColumnHidden(i)); } menu->addSeparator(); QAction *resetAction = menu->addAction(tr("Reset to defaults"), this, [this]() { for (int i = 0, count = header()->count(); i < count; ++i) { SetColumnDefaults(); } // SaveSettings(); }); menu->popup(QCursor::pos()); (void)(resetAction); } void AnimeListWidget::DisplayListMenu() { QMenu *menu = new QMenu(this); menu->setAttribute(Qt::WA_DeleteOnClose); menu->setTitle(tr("Column visibility")); menu->setToolTipsVisible(true); const QItemSelection selection = sort_model->mapSelectionToSource(selectionModel()->selection()); if (!selection.indexes().first().isValid()) { return; } QAction* action = menu->addAction("Information", [this, selection]{ const QModelIndex index = model->index(selection.indexes().first().row()); Anime* anime = model->GetAnimeFromIndex(index); if (!anime) { return; } InformationDialog* dialog = new InformationDialog(*anime, model, this); dialog->show(); dialog->raise(); dialog->activateWindow(); }); menu->popup(QCursor::pos()); } void AnimeListWidget::ItemDoubleClicked() { /* throw out any other garbage */ const QItemSelection selection = sort_model->mapSelectionToSource(selectionModel()->selection()); if (!selection.indexes().first().isValid()) { return; } const QModelIndex index = model->index(selection.indexes().first().row()); const QString title = index.siblingAtColumn(AnimeListWidgetModel::AL_TITLE).data(Qt::UserRole).toString(); QMessageBox box; box.setText(QString::number(title.size())); box.exec(); Anime* anime = model->GetAnimeFromIndex(index); if (!anime) { return; } InformationDialog* dialog = new InformationDialog(*anime, model, this); dialog->show(); dialog->raise(); dialog->activateWindow(); } AnimeListWidget::AnimeListWidget(QWidget* parent, AnimeList* alist) : QTreeView(parent) { setItemDelegate(new AnimeListWidgetDelegate(this)); model = new AnimeListWidgetModel(parent, alist); sort_model = new AnimeListWidgetSortFilter(this); sort_model->setSourceModel(model); sort_model->setSortRole(Qt::UserRole); setModel(sort_model); setObjectName("listwidget"); setStyleSheet("QTreeView#listwidget{border:0px;}"); setUniformRowHeights(true); setAllColumnsShowFocus(false); setSortingEnabled(true); setSelectionMode(QAbstractItemView::ExtendedSelection); setItemsExpandable(false); setRootIsDecorated(false); setContextMenuPolicy(Qt::CustomContextMenu); connect(this, &QAbstractItemView::doubleClicked, this, &AnimeListWidget::ItemDoubleClicked); connect(this, &QWidget::customContextMenuRequested, this, &AnimeListWidget::DisplayListMenu); /* Enter & return keys */ connect(new QShortcut(Qt::Key_Return, this, nullptr, nullptr, Qt::WidgetShortcut), &QShortcut::activated, this, &AnimeListWidget::ItemDoubleClicked); connect(new QShortcut(Qt::Key_Enter, this, nullptr, nullptr, Qt::WidgetShortcut), &QShortcut::activated, this, &AnimeListWidget::ItemDoubleClicked); header()->setStretchLastSection(false); header()->setContextMenuPolicy(Qt::CustomContextMenu); connect(header(), &QWidget::customContextMenuRequested, this, &AnimeListWidget::DisplayColumnHeaderMenu); // if(!session.config.anime_list.columns) { SetColumnDefaults(); // } } AnimeListPage::AnimeListPage(QWidget* parent) : QTabWidget (parent) { setDocumentMode(false); setObjectName("animepage"); //setStyleSheet("QTabWidget#animepage{border-bottom:0px;border-left:0px;border-right:0px;}"); SyncAnimeList(); for (AnimeList& list : anime_lists) { addTab(new AnimeListWidget(this, &list), QString::fromUtf8(list.name.c_str())); } } void AnimeListPage::SyncAnimeList() { switch (session.config.service) { case ANILIST: { AniList anilist = AniList(); session.config.anilist.user_id = anilist.GetUserId(session.config.anilist.username); FreeAnimeList(); anilist.UpdateAnimeList(&anime_lists, session.config.anilist.user_id); break; } default: break; } } void AnimeListPage::FreeAnimeList() { for (auto& list : anime_lists) { list.Clear(); } anime_lists.clear(); } int AnimeListPage::GetTotalAnimeAmount() { int total = 0; for (auto& list : anime_lists) { total += list.Size(); } return total; } int AnimeListPage::GetTotalEpisodeAmount() { /* FIXME: this also needs to take into account rewatches... */ int total = 0; for (auto& list : anime_lists) { for (auto& anime : list) { total += anime.progress; } } return total; } /* Returns the total watched amount in minutes. */ int AnimeListPage::GetTotalWatchedAmount() { int total = 0; for (auto& list : anime_lists) { for (auto& anime : list) { total += anime.duration*anime.progress; } } return total; } /* Returns the total planned amount in minutes. Note that we should probably limit progress to the amount of episodes, as AniList will let you set episode counts up to 32768. But that should rather be handled elsewhere. */ int AnimeListPage::GetTotalPlannedAmount() { int total = 0; for (auto& list : anime_lists) { for (auto& anime : list) { total += anime.duration*(anime.episodes-anime.progress); } } return total; } double AnimeListPage::GetAverageScore() { double avg = 0; int amt = 0; for (auto& list : anime_lists) { for (auto& anime : list) { avg += anime.score; if (anime.score != 0) amt++; } } return avg/amt; } double AnimeListPage::GetScoreDeviation() { double squares_sum = 0, avg = GetAverageScore(); int amt = 0; for (auto& list : anime_lists) { for (auto& anime : list) { if (anime.score != 0) { squares_sum += std::pow((double)anime.score - avg, 2); amt++; } } } return (amt > 0) ? std::sqrt(squares_sum / amt) : 0; } #include "moc_anime.cpp"