void MainWindow::loadBrowserData() {
  QUrl url = API::sharedAPI()->sharedAniListAPI()->API_BROWSE;

  QString season = ui->comboSeason->currentText();
  QString year   = ui->comboYear->currentText();
  QString type   = ui->comboType->currentText();
  QString status = ui->comboStatus->currentText();

  // Clear the browser view
  QLayoutItem *item;

  while ((item = layout2->takeAt(0))) {
    delete item->widget();
    delete item;
  }

  if (!season.isEmpty()) {
    url = addQuery(url, "season", season);
  }

  if (!year.isEmpty()) {
    url = addQuery(url, "year", year);
  }

  if (!type.isEmpty()) {
    url = addQuery(url, "type", type);
  }

  if (!status.isEmpty()) {
    url = addQuery(url, "status", status);
  }

  QStringList genres;
  QStringList exclude;

  for (int i = 0; i < ui->genreList->count(); i++) {
    QCheckBox *w = static_cast<QCheckBox *>
        (dynamic_cast<QWidgetItem *>(ui->genreList->itemAt(i))->widget());

    if (w->checkState() == Qt::PartiallyChecked) {
      exclude.append(w->text());
    } else if (w->checkState() == Qt::Checked) {
      genres.append(w->text());
    }
  }

  if (!genres.isEmpty()) {
    url = addQuery(url, "genres", genres.join(","));
  }

  if (!exclude.isEmpty()) {
    url = addQuery(url, "genres_exclude", exclude.join(","));
  }

  // Load the results for the requested type
  QJsonArray browse_results =
      API::sharedAPI()->sharedAniListAPI()->get(url).array();

  for (int i = 0; i <= browse_results.size(); i++) {
    QJsonObject anime = browse_results.at(i).toObject();

    Anime *a = User::sharedUser()->getAnimeByTitle(
          anime.value("title_romaji").toString());

    if (a == 0) {
      a = new Anime();
      a->setID(QString::number(anime.value("id").toInt()));
      a->setMyProgress(0);
      a->setMyNotes("");
      a->setMyRewatch(0);
      a->setMyStatus("");

      if (a->getID() == "0") {
        delete a;
        continue;
      }
    }

    BrowseAnime *s = new BrowseAnime(this, User::sharedUser()->scoreType());

    if (a->needsLoad() || a->needsCover()) {
      User::sharedUser()->loadAnimeData(a, true);

      QEventLoop evt;
      connect(a, SIGNAL(finishedReloading()), &evt, SLOT(quit()));
      evt.exec();
    }

    s->setAnime(a);

    layout2->addWidget(s);

    // Do we need to keep loading?
    if (season != ui->comboSeason->currentText() ||
       year != ui->comboYear->currentText() ||
       type != ui->comboType->currentText() ||
       status != ui->comboStatus->currentText()) {
      return;
    }

    int width = layout2->geometry().width();
    int cwidth = layout2->contentsWidth();

    if (cwidth < 0) {
      width = this->width() - 2;
      cwidth = this->width() - (this->width() % 200);
    }

    layout2->setContentsMargins((width-cwidth)/2, 0, 0, 0);
  }
}
Exemple #2
0
void BrowseWidget::parseBrowserData(const QString &t_id,
                                    QNetworkReply *t_reply) {
  if (t_id != m_request_id) return;

  auto title_language = User::sharedUser()->titleLanguage();
  QByteArray data = t_reply->readAll();
  QJsonArray browse_results = QJsonDocument::fromJson(data).array();

  for (int i = 0; i <= browse_results.size(); i++) {
    QJsonObject anime = browse_results.at(i).toObject();
    auto title = anime.value(title_language).toString();

    if (title.isEmpty()) {
      qDebug() << "Unknown title for: " << QJsonDocument(anime).toJson();
      continue;
    }

    AnimePtr a = User::sharedUser()->getAnimeByTitle(title);

    if (a == AnimePtr(nullptr)) {
      a = Anime::makeAnime();
      a->setId(QString::number(anime.value("id").toInt()));
      a->setTitle(title);
      a->setImageUrl(anime.value("image_url_lge").toString());
      a->setTitleRomaji(anime.value("title_romaji").toString());
      a->setTitleJapanese(anime.value("title_japanese").toString());
      a->setTitleEnglish(anime.value("title_english").toString());
      a->setType(anime.value("type").toString());
      a->setAiringStatus(anime.value("airing_status").toString());
      a->setAverageScore(anime.value("average_score").toString().toDouble());
      a->setTotalEpisodes(anime.value("total_episodes").toInt());
      a->setAdult(anime.value("adult").toBool());

      if (a->id() == "0") {
        continue;
      }

      User::sharedUser()->addKnownAnime(a);
    }

    if (a->adult() && !User::sharedUser()->adultContent()) {
      continue;
    }

    BrowseAnime *s = new BrowseAnime(this, User::sharedUser()->scoreType());

    if (!a->hasLoaded()) {
      connect(a.get(), SIGNAL(imageLoaded()), s, SLOT(repaint()));
      emit loadAnime(a);
    }

    s->setAnime(a);

    m_browse_layout->addWidget(s);
    m_browse_layout->setGeometry(m_ui->result_area->geometry());

    int width = m_browse_layout->geometry().width();
    int cwidth = m_browse_layout->contentsWidth();

    if (m_browse_layout->heightForWidth(width) > this->geometry().height()) {
      width -= qApp->style()->pixelMetric(QStyle::PM_ScrollBarExtent);
    }

    m_browse_layout->setContentsMargins((width - cwidth) / 2, 0, 0, 0);
  }

  m_ui->browse_button->setEnabled(true);
}