QVariant AnimeModel::data(const QModelIndex &index, int role) const { if (index.row() < 0 || index.row() >= m_list.count() || role != Qt::DisplayRole) { return QVariant(); } AnimePtr anime = m_list.at(index.row()); if (index.column() == ListRoles::ID) return anime->id(); if (index.column() == ListRoles::Title) return anime->title(); if (index.column() == ListRoles::RomajiTitle) return anime->titleRomaji(); if (index.column() == ListRoles::JapaneseTitle) return anime->titleJapanese(); if (index.column() == ListRoles::EnglishTitle) return anime->titleEnglish(); if (index.column() == ListRoles::Progress) return QString::number(anime->episodesWatched()) + " / " + QString::number(anime->totalEpisodes()); if (index.column() == ListRoles::Score) return anime->score(); if (index.column() == ListRoles::Status) return User::sharedUser()->trList(anime->listStatus()); if (index.column() == ListRoles::AiringStatus) return User::sharedUser()->trStatus(anime->airingStatus()); if (index.column() == ListRoles::Type) return User::sharedUser()->trType(anime->type()); if (index.column() == ListRoles::Notes) return anime->notes(); return QVariant(); }
void AnimeListWidget::updateStatus(AnimePtr t_anime, const QString &t_status) { if (t_status == t_anime->listStatus()) return; t_anime->setListStatus(t_status); emit animeChanged(t_anime); User::sharedUser()->removeFromList(m_list, t_anime); User::sharedUser()->addToList(t_status, t_anime); m_proxy_model->invalidate(); }
void AnimeListWidget::customContextMenuRequested(const QPoint &t_pos) { auto model = m_ui->table->indexAt(t_pos); auto row = model.row(); if (row < 0) return; QPoint position = t_pos; position.setY(t_pos.y() + 20); QAction *pAnimePanel = new QAction(tr("Open Anime Panel"), m_ui->table); QAction *pEpisodeIncrement = new QAction(tr("Increment Progress by 1"), m_ui->table); QMenu *pStatusUpdate = new QMenu(tr("Status"), m_ui->table); QAction *pDeleteEntry = new QAction(tr("Delete Entry"), m_ui->table); QMenu *pCustomLists = new QMenu(tr("Custom Lists"), m_ui->table); QAction *pWatching = new QAction(tr("Watching"), pStatusUpdate); QAction *pOnHold = new QAction(tr("On Hold"), pStatusUpdate); QAction *pPlanToWatch = new QAction(tr("Plan to Watch"), pStatusUpdate); QAction *pCompleted = new QAction(tr("Completed"), pStatusUpdate); QAction *pDropped = new QAction(tr("Dropped"), pStatusUpdate); QAction *pHideDefault = new QAction(tr("Hide Default"), pCustomLists); QAction *pRuleAction = new QAction(tr("Create rule"), m_ui->table); QAction *pSearch = new QAction(tr("Search For Torrents"), m_ui->table); auto index = m_proxy_model->index(model.row(), ListRoles::Title); auto title = m_proxy_model->data(index, Qt::DisplayRole).toString(); AnimePtr anime = User::sharedUser()->getAnimeByTitle(title); for (int i = 0; i < User::sharedUser()->customListNames().count(); i++) { QString list = User::sharedUser()->customListNames().at(i); if (list.isEmpty()) continue; QAction *temp = new QAction(list, m_ui->table); temp->setCheckable(true); if (anime->customLists().at(i) == 1) { temp->setChecked(true); } else { temp->setChecked(false); } connect(temp, &QAction::toggled, [this, anime, i](bool selected) { QList<int> custom = anime->customLists(); custom.replace(i, selected ? 1 : 0); anime->setCustomLists(custom); QMap<QString, QString> data; data.insert("id", anime->id()); QString d; for (int i = 0; i < custom.length(); i++) { d += QString::number(custom.at(i)) + ((i == custom.length() - 1) ? "" : ","); } data.insert("custom_lists", d); auto api = API::sharedAPI()->sharedAniListAPI(); api->put(api->API_EDIT_LIST, data); emit refreshLists(); }); pCustomLists->addAction(temp); } pHideDefault->setCheckable(true); pHideDefault->setChecked(anime->hiddenDefault()); pCustomLists->addAction(pHideDefault); connect(pHideDefault, &QAction::toggled, [this, anime](bool selected) { QMap<QString, QString> data; data.insert("id", anime->id()); data.insert("hidden_default", selected ? "1" : "0"); if (selected) { User::sharedUser()->removeFromList(anime->listStatus(), anime); } emit animePayloadChanged(anime, data); }); pStatusUpdate->addAction(pWatching); pStatusUpdate->addAction(pOnHold); pStatusUpdate->addAction(pPlanToWatch); pStatusUpdate->addAction(pCompleted); pStatusUpdate->addAction(pDropped); connect(pAnimePanel, &QAction::triggered, [this, row, anime]() { emit animePanelRequested(anime); }); connect(pEpisodeIncrement, &QAction::triggered, [&anime, this]() { if (anime->episodesWatched() >= anime->totalEpisodes() && anime->totalEpisodes() != 0) { return; } anime->setEpisodesWatched(anime->episodesWatched() + 1); if (anime->episodesWatched() == anime->totalEpisodes() && anime->totalEpisodes() != 0) { anime->setListStatus("completed"); } m_proxy_model->invalidate(); emit animeChanged(anime); }); connect(pWatching, &QAction::triggered, [this, anime]() { updateStatus(anime, "watching"); }); connect(pPlanToWatch, &QAction::triggered, [this, anime]() { updateStatus(anime, "plan to watch"); }); connect(pOnHold, &QAction::triggered, [this, anime]() { updateStatus(anime, "on-hold"); }); connect(pDropped, &QAction::triggered, [this, anime]() { updateStatus(anime, "dropped"); }); connect(pCompleted, &QAction::triggered, [this, anime]() { updateStatus(anime, "completed"); }); connect(pDeleteEntry, &QAction::triggered, [this, anime]() { auto api = API::sharedAPI()->sharedAniListAPI(); auto url = api->API_DELETE_ANIME(anime->id()); api->deleteResource(url); User::sharedUser()->removeAll(anime); emit refreshLists(); }); connect(pRuleAction, &QAction::triggered, [this, anime]() { emit createAndShowRule(anime, ""); }); connect(pSearch, &QAction::triggered, [this, anime]() { emit searchTorrents(anime); }); QMenu *pContextMenu = new QMenu(this); pContextMenu->addAction(pAnimePanel); pContextMenu->addAction(pEpisodeIncrement); pContextMenu->addMenu(pStatusUpdate); if (User::sharedUser()->customListNames().length() > 0) { pContextMenu->addMenu(pCustomLists); } else { delete pCustomLists; } pContextMenu->addAction(pDeleteEntry); pContextMenu->addAction(pRuleAction); pContextMenu->addAction(pSearch); pContextMenu->exec(mapToGlobal(position)); delete pContextMenu; pContextMenu = nullptr; }