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();
}
Exemple #2
0
bool AnimeModel::setData(const QModelIndex &index, const QVariant &value,
                         int role) {
  if (index.row() < 0 || index.row() >= m_list.count() ||
      role != Qt::EditRole) {
    return false;
  }

  AnimePtr anime = m_list.at(index.row());

  if (index.column() == ListRoles::Progress) {
    anime->setEpisodesWatched(value.toInt());
    emit animeChanged(anime);
    return true;
  } else if (index.column() == ListRoles::Status) {
    anime->setListStatus(value.toString());
    emit animeChanged(anime);
    return true;
  } else if (index.column() == ListRoles::Score) {
    auto score_type = User::sharedUser()->scoreType();

    if ((score_type == ScoreType::TenPoint) ||
        (score_type == ScoreType::HundredPoint)) {
      anime->setScore(QString::number(value.toInt()));
    } else if (score_type == ScoreType::FiveStar) {
      anime->setScore(value.toString());
    } else if (score_type == ScoreType::Smiles) {
      anime->setScore(value.toString());
    } else {
      anime->setScore(QString::number(value.toDouble()));
    }

    emit animeChanged(anime);
    return true;
  }

  return false;
}
AnimeListWidget::AnimeListWidget(QWidget *parent, QString list)
    : QWidget(parent), m_ui(new Ui::AnimeListWidget), m_list(list) {
    m_ui->setupUi(this);
    m_ui->retranslateUi(this);

    m_proxy_model = new AnimeProxyModel;
    m_model = new AnimeModel;
    m_progress_delegate = new ProgressDelegate;

    m_proxy_model->setDynamicSortFilter(true);
    m_proxy_model->setFilterCaseSensitivity(Qt::CaseInsensitive);
    m_proxy_model->setSortCaseSensitivity(Qt::CaseInsensitive);

    if (User::sharedUser()->listOrder() == ListOrder::Alphabet) {
        m_proxy_model->sort(ListRoles::Title);
    } else if (User::sharedUser()->listOrder() == ListOrder::Score) {
        m_proxy_model->sort(ListRoles::Score);
    }

    m_proxy_model->setSourceModel(m_model);
    m_ui->table->setModel(m_proxy_model);
    m_ui->table->setItemDelegateForColumn(ListRoles::Progress,
                                          m_progress_delegate);

    connect(m_ui->table, SIGNAL(doubleClicked(QModelIndex)), this,
            SLOT(doubleClicked(QModelIndex)));

    connect(m_ui->table, SIGNAL(customContextMenuRequested(QPoint)), this,
            SLOT(customContextMenuRequested(QPoint)));

    m_ui->table->horizontalHeader()->setContextMenuPolicy(Qt::CustomContextMenu);

    connect(m_ui->table->horizontalHeader(),
            SIGNAL(customContextMenuRequested(QPoint)), this,
            SLOT(headerContextMenuRequested(QPoint)));

    connect(m_model, &AnimeModel::animeChanged, this,
    [this](AnimePtr anime) {
        emit animeChanged(anime);
    });

    auto font = m_ui->table->fontMetrics();
    m_ui->table->verticalHeader()->setDefaultSectionSize(font.height() + 4);
}
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;
}