From 1fb97e1891d3defcc7aa9d96caa10edf8f255373 Mon Sep 17 00:00:00 2001 From: birkeh Date: Tue, 6 Nov 2018 20:23:28 +0100 Subject: [PATCH] added serie discover --- cdiscover.cpp | 143 +++++++++++++++++++++++++++++++- cdiscover.h | 18 +++- cdiscover.ui | 201 ++++++++++++++++++++++++++++++++++++++++----- cmainwindow.cpp | 96 ++++++++++++++++++++-- cmoviediscover.cpp | 17 +++- cmoviediscover.h | 1 + cthemoviedbv3.cpp | 121 +++++++++++++++++++++++++++ cthemoviedbv3.h | 1 + 8 files changed, 565 insertions(+), 33 deletions(-) diff --git a/cdiscover.cpp b/cdiscover.cpp index f7e82a8..1d167fa 100644 --- a/cdiscover.cpp +++ b/cdiscover.cpp @@ -1,15 +1,154 @@ #include "cdiscover.h" #include "ui_cdiscover.h" +#include "ccheckboxitemdelegate.h" +#include "cthemoviedbv3.h" -cDiscover::cDiscover(QWidget *parent) : +#include +#include +#include + +#include + + +cDiscover::cDiscover(const cSerieList serieList, QWidget *parent) : QDialog(parent), - ui(new Ui::cDiscover) + ui(new Ui::cDiscover), + m_serieList(serieList) { ui->setupUi(this); + + m_lpGenresModel = new QStandardItemModel(0, 1); + QStringList headerLabels = QStringList() << tr("Name"); + m_lpGenresModel->setHorizontalHeaderLabels(headerLabels); + ui->m_lpGenres->setModel(m_lpGenresModel); + ui->m_lpGenres->setItemDelegate(new cCheckBoxItemDelegate()); + ui->m_lpGenres->setWrapping(true); + + m_lpSeriesModel = new QStandardItemModel(0, 1); + ui->m_lpSeries->setModel(m_lpSeriesModel); + + headerLabels = QStringList() << tr("Title") << tr("Year"); + m_lpSeriesModel->setHorizontalHeaderLabels(headerLabels); + + connect(ui->m_lpVoting, &QxtSpanSlider::spanChanged, this, &cDiscover::spanChanged); + + ui->m_lpVoting->setRange(0, 100); + ui->m_lpVoting->setLowerValue(0); + ui->m_lpVoting->setUpperValue(100); + + cTheMovieDBV3 movieDB3; + QMap genres; + + genres = movieDB3.genresSerie("de-DE"); + + QMapIterator iterator(genres); + while(iterator.hasNext()) + { + iterator.next(); + QStandardItem* lpItem = new QStandardItem(iterator.value()); + lpItem->setData(iterator.key()); + lpItem->setCheckable(true); + + m_lpGenresModel->appendRow(lpItem); + } } cDiscover::~cDiscover() { delete ui; + delete m_lpGenresModel; +} + +void cDiscover::spanChanged(int lower, int upper) +{ + ui->m_lpVotingFrom->setText(QString::number((qreal)lower/10)); + ui->m_lpVotingTo->setText(QString::number((qreal)upper/10)); +} + +void cDiscover::on_m_lpDiscover_clicked() +{ + QString szText = ui->m_lpText->text(); + qint16 iYear = ui->m_lpYear->value(); + qreal voteMin = (qreal)ui->m_lpVoting->lowerValue()/10; + qreal voteMax = (qreal)ui->m_lpVoting->upperValue()/10; + + QList genres; + + if(!ui->m_lpYearEnable->isChecked()) + iYear = -1; + + for(int x = 0;x < m_lpGenresModel->rowCount();x++) + { + QStandardItem* lpItem = m_lpGenresModel->item(x); + + if(lpItem->checkState() == Qt::Checked) + genres.append(lpItem->data().toInt()); + } + + setCursor(Qt::WaitCursor); + + cTheMovieDBV3 movieDB3; + QList serieList = movieDB3.discoverSerie(szText, iYear, genres, voteMin, voteMax, "de-DE"); + + m_lpSeriesModel->clear(); + + QStringList headerLabels = QStringList() << tr("Title") << tr("Year"); + m_lpSeriesModel->setHorizontalHeaderLabels(headerLabels); + + for(int x = 0; x < serieList.count();x++) + { + cSerie* lpSerie = serieList[x]; + + if(m_serieList.find(lpSerie->seriesID())) + continue; + QList items; + + items.append(new QStandardItem(lpSerie->seriesName())); + items.append(new QStandardItem(QString::number(lpSerie->firstAired().year()))); + items[0]->setData(QVariant::fromValue(lpSerie)); + items[1]->setData(QVariant::fromValue(lpSerie)); + items[0]->setCheckable(true); + + m_lpSeriesModel->appendRow(items); + } + + for(int x = 0;x < headerLabels.count();x++) + ui->m_lpSeries->resizeColumnToContents(x); + + setCursor(Qt::ArrowCursor); +} + +void cDiscover::on_m_lpYearEnable_clicked(bool checked) +{ + ui->m_lpYear->setEnabled(checked); +} + +void cDiscover::on_m_lpSeries_clicked(const QModelIndex &index) +{ + QStandardItem* lpItem = m_lpSeriesModel->itemFromIndex(index); + cSerie* lpSerie = lpItem->data().value(); + +// if(lpSerie->cast().isEmpty()) +// { +// cTheMovieDBV3 theMovieDB; +// theMovieDB.loadCastSerie(lpSerie); +// } +// ui->m_lpMovieDetails->setMovie(lpMovie); +} + +QList cDiscover::id() +{ + QList idList; + + for(int x = 0;x < m_lpSeriesModel->rowCount();x++) + { + if(m_lpSeriesModel->item(x, 0)->checkState() == Qt::Checked) + { + cSerie* lpSerie = m_lpSeriesModel->item(x, 0)->data().value(); + idList.append(lpSerie->seriesID()); + } + } + + return(idList); } diff --git a/cdiscover.h b/cdiscover.h index ea4a1fe..1feb4c3 100644 --- a/cdiscover.h +++ b/cdiscover.h @@ -2,7 +2,10 @@ #define CDISCOVER_H +#include + #include +#include namespace Ui { @@ -14,11 +17,22 @@ class cDiscover : public QDialog Q_OBJECT public: - explicit cDiscover(QWidget *parent = 0); + explicit cDiscover(const cSerieList serieList, QWidget *parent = 0); ~cDiscover(); + QList id(); + +private slots: + void spanChanged(int lower, int upper); + void on_m_lpDiscover_clicked(); + void on_m_lpYearEnable_clicked(bool checked); + void on_m_lpSeries_clicked(const QModelIndex &index); + private: - Ui::cDiscover* ui; + Ui::cDiscover* ui; + QStandardItemModel* m_lpGenresModel; + QStandardItemModel* m_lpSeriesModel; + cSerieList m_serieList; }; #endif // CDISCOVER_H diff --git a/cdiscover.ui b/cdiscover.ui index 1c3a687..1b1de44 100644 --- a/cdiscover.ui +++ b/cdiscover.ui @@ -1,38 +1,195 @@ + - - - cDiscover 0 0 - 400 - 300 + 900 + 500 Dialog - - - - 30 - 240 - 341 - 32 - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - + + + + + + + + + + + + + + + Text: + + + + + + + + + + + + + + Erscheinungsjahr: + + + + + + + false + + + 1800 + + + 2100 + + + + + + + + + + + Voting: + + + + + + + TextLabel + + + + + + + TextLabel + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + Qt::Horizontal + + + + + + + + + + + Genre + + + + + + QAbstractItemView::NoEditTriggers + + + true + + + QListView::TopToBottom + + + + + + + + + + + + Discover + + + + + + + + + + + QAbstractItemView::NoEditTriggers + + + + + + + true + + + + + 0 + 0 + 579 + 302 + + + + + + + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + - + + + QxtSpanSlider + QSlider +
qxtspanslider.h
+
+ + cMovieDetails + QWidget +
cmoviedetails.h
+ 1 +
+
diff --git a/cmainwindow.cpp b/cmainwindow.cpp index 6bcd619..dbc2eed 100644 --- a/cmainwindow.cpp +++ b/cmainwindow.cpp @@ -2468,18 +2468,102 @@ void cMainWindow::onActionExit() void cMainWindow::onActionDiscover() { - cTheMovieDBV3 movieDB3; - QMap genres; + cDiscover* lpDiscover = new cDiscover(m_serieList, this); - genres = movieDB3.genresSerie("de-DE"); + QSettings settings; + qint16 iX = settings.value("serieDiscover/x", QVariant::fromValue(-1)).toInt(); + qint16 iY = settings.value("serieDiscover/y", QVariant::fromValue(-1)).toInt(); + qint16 iWidth = settings.value("serieDiscover/width", QVariant::fromValue(-1)).toInt(); + qint16 iHeight = settings.value("serieDiscover/height", QVariant::fromValue(-1)).toInt(); + + if(iX != -1 && iY != -1) + lpDiscover->move(iX, iY); + if(iWidth != -1 && iHeight != -1) + lpDiscover->resize(iWidth, iHeight); + + if(lpDiscover->exec() == QDialog::Rejected) + { + delete lpDiscover; + return; + } + + settings.setValue("serieDiscover/width", QVariant::fromValue(lpDiscover->size().width())); + settings.setValue("serieDiscover/height", QVariant::fromValue(lpDiscover->size().height())); + settings.setValue("serieDiscover/x", QVariant::fromValue(lpDiscover->x())); + settings.setValue("serieDiscover/y", QVariant::fromValue(lpDiscover->y())); + + QList idList = lpDiscover->id(); + + delete lpDiscover; } void cMainWindow::onActionMovieDiscover() { - cMovieDiscover* lpMovieDiscover; + cMovieDiscover* lpMovieDiscover = new cMovieDiscover(m_movieList, this); - lpMovieDiscover = new cMovieDiscover(m_movieList, this); - lpMovieDiscover->exec(); + QSettings settings; + qint16 iX = settings.value("movieDiscover/x", QVariant::fromValue(-1)).toInt(); + qint16 iY = settings.value("movieDiscover/y", QVariant::fromValue(-1)).toInt(); + qint16 iWidth = settings.value("movieDiscover/width", QVariant::fromValue(-1)).toInt(); + qint16 iHeight = settings.value("movieDiscover/height", QVariant::fromValue(-1)).toInt(); + + if(iX != -1 && iY != -1) + lpMovieDiscover->move(iX, iY); + if(iWidth != -1 && iHeight != -1) + lpMovieDiscover->resize(iWidth, iHeight); + + if(lpMovieDiscover->exec() == QDialog::Rejected) + { + delete lpMovieDiscover; + return; + } + + settings.setValue("movieDiscover/width", QVariant::fromValue(lpMovieDiscover->size().width())); + settings.setValue("movieDiscover/height", QVariant::fromValue(lpMovieDiscover->size().height())); + settings.setValue("movieDiscover/x", QVariant::fromValue(lpMovieDiscover->x())); + settings.setValue("movieDiscover/y", QVariant::fromValue(lpMovieDiscover->y())); + + QList idList = lpMovieDiscover->id(); delete lpMovieDiscover; + + cMovie* lpMovie = 0; + + for(int x = 0;x < idList.count();x++) + { + qint32 id = idList.at(x); + if(id != -1) + { + cMessageAnimateDialog* lpDialog = new cMessageAnimateDialog(this); + lpDialog->setTitle("Refresh"); + lpDialog->setMessage("Loading"); + lpDialog->show(); + + cTheMovieDBV3 movieDB3; + + lpMovie = movieDB3.loadMovie(id, "de-DE"); + if(!lpMovie) + lpMovie = movieDB3.loadMovie(id, "en"); + + delete lpDialog; + + if(runMovieEdit(lpMovie)) + { + lpDialog = new cMessageAnimateDialog(this); + lpDialog->setTitle("Update"); + lpDialog->setMessage("Updating"); + lpDialog->show(); + + lpMovie->loadFanart(); + lpMovie->save(m_db); + + delete lpDialog; + } + delete lpMovie; + } + } + + loadMoviesDB(); + displayMovies(); + applyMoviesFilter(); } diff --git a/cmoviediscover.cpp b/cmoviediscover.cpp index b33c5fb..6fe4cf8 100644 --- a/cmoviediscover.cpp +++ b/cmoviediscover.cpp @@ -31,7 +31,6 @@ cMovieDiscover::cMovieDiscover(const cMovieList movieList, QWidget *parent) : headerLabels = QStringList() << tr("Title") << tr("Year"); m_lpMoviesModel->setHorizontalHeaderLabels(headerLabels); - connect(ui->m_lpVoting, &QxtSpanSlider::spanChanged, this, &cMovieDiscover::spanChanged); ui->m_lpVoting->setRange(0, 100); @@ -138,3 +137,19 @@ void cMovieDiscover::on_m_lpMovies_clicked(const QModelIndex &index) } ui->m_lpMovieDetails->setMovie(lpMovie); } + +QList cMovieDiscover::id() +{ + QList idList; + + for(int x = 0;x < m_lpMoviesModel->rowCount();x++) + { + if(m_lpMoviesModel->item(x, 0)->checkState() == Qt::Checked) + { + cMovie* lpMovie = m_lpMoviesModel->item(x, 0)->data().value(); + idList.append(lpMovie->movieID()); + } + } + + return(idList); +} diff --git a/cmoviediscover.h b/cmoviediscover.h index 25d7798..f30737e 100644 --- a/cmoviediscover.h +++ b/cmoviediscover.h @@ -20,6 +20,7 @@ public: explicit cMovieDiscover(const cMovieList movieList, QWidget *parent = 0); ~cMovieDiscover(); + QList id(); private slots: void spanChanged(int lower, int upper); void on_m_lpDiscover_clicked(); diff --git a/cthemoviedbv3.cpp b/cthemoviedbv3.cpp index 2d9f140..2117998 100644 --- a/cthemoviedbv3.cpp +++ b/cthemoviedbv3.cpp @@ -799,3 +799,124 @@ void cTheMovieDBV3::loadCastMovie(cMovie* lpMovie) lpMovie->setCrew(szCrew); } } + +QList cTheMovieDBV3::discoverSerie(const QString& szText, const qint16& iYear, const QList& genres, const qreal& voteMin, const qreal& voteMax, const QString& szLanguage) +{ + if(m_genres.isEmpty()) + m_genres = genresMovie("de-DE"); + + QList serieList; + QNetworkAccessManager networkManager; + QString szRequest = QString("https://api.themoviedb.org/3/discover/tv?api_key=%1").arg(m_szToken); + qint16 page = 1; + + if(!szLanguage.contains("all")) + szRequest.append(QString("&language=%1").arg(szLanguage)); + + if(!szText.isEmpty()) + szRequest.append(QString("&with_keywords=%1").arg(szText)); + if(iYear != -1) + szRequest.append(QString("&first_air_date_year=%1").arg(iYear)); + if(genres.count()) + { + QString tmp = QString::number(genres[0]); + + for(int x = 1;x < genres.count();x++) + tmp.append(QString(",%1").arg(genres[x])); + szRequest.append(QString("&with_genres=%1").arg(tmp)); + } + szRequest.append(QString("&vote_average.gte=%1").arg(voteMin)); + szRequest.append(QString("&vote_average.lte=%1").arg(voteMax)); + + for(;;) + { + QNetworkRequest request(QUrl(QString("%1&page=%2").arg(szRequest).arg(page))); + + QNetworkReply* reply = networkManager.get(request); + QEventLoop loop; + + QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); + loop.exec(); + + if (reply->error() == QNetworkReply::NoError) + { + QString strReply = (QString)reply->readAll(); + QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8()); + QJsonObject jsonObj = jsonResponse.object(); + QJsonArray jsonArray = jsonObj["results"].toArray(); + QJsonArray tmpArray; + QStringList tmpList; + + for(int z = 0;z < jsonArray.count();z++) + { + QJsonObject serie = jsonArray[z].toObject(); + cSerie* lpSerie = new cSerie; + + lpSerie->setSeriesID(serie["id"].toInt()); + lpSerie->setBackdropPath(serie["backdrop_path"].toString()); + tmpArray = serie["created_by"].toArray(); + for(int x = 0;x < tmpArray.count();x++) + tmpList.append(tmpArray.at(x).toObject()["name"].toString()); + lpSerie->setCreatedBy(tmpList); + tmpList.clear(); + lpSerie->setFirstAired(serie["first_air_date"].toString()); + tmpArray = serie["genres"].toArray(); + for(int x = 0;x < tmpArray.count();x++) + tmpList.append(tmpArray.at(x).toObject()["name"].toString()); + lpSerie->setGenre(tmpList); + tmpList.clear(); + lpSerie->setHomepage(serie["homepage"].toString()); + lpSerie->setLastAired(serie["last_air_date"].toString()); + tmpArray = serie["languages"].toArray(); + for(int x = 0;x < tmpArray.count();x++) + tmpList.append(tmpArray.at(x).toString()); + lpSerie->setLanguages(tmpList); + tmpList.clear(); + lpSerie->setSeriesName(serie["name"].toString()); + tmpArray = serie["networks"].toArray(); + for(int x = 0;x < tmpArray.count();x++) + tmpList.append(tmpArray.at(x).toObject()["name"].toString()); + lpSerie->setNetworks(tmpList); + tmpList.clear(); + lpSerie->setEpisodes(serie["number_of_episodes"].toInt()); + lpSerie->setSeasons(serie["number_of_seasons"].toInt()); + tmpArray = serie["origin_country"].toArray(); + for(int x = 0;x < tmpArray.count();x++) + tmpList.append(tmpArray.at(x).toString()); + lpSerie->setOriginCountries(tmpList); + tmpList.clear(); + lpSerie->setOriginalLanguage(serie["original_language"].toString()); + lpSerie->setOriginalName(serie["original_name"].toString()); + lpSerie->setOverview(serie["overview"].toString()); + lpSerie->setPopularity(serie["popularity"].toDouble()); + lpSerie->setPosterPath(serie["poster_path"].toString()); + tmpArray = serie["production_companies"].toArray(); + for(int x = 0;x < tmpArray.count();x++) + tmpList.append(tmpArray.at(x).toObject()["name"].toString()); + lpSerie->setProductionCompanies(tmpList); + tmpList.clear(); + lpSerie->setStatus(serie["status"].toString()); + lpSerie->setType(serie["type"].toString()); + lpSerie->setVoteAverage(serie["vote_average"].toDouble()); + lpSerie->setVoteCount(serie["vote_count"].toInt()); + + serieList.append(lpSerie); + } + if(jsonObj["total_pages"].toInt() == page) + break; + + page++; + if(page > 20) + break; + + delete reply; + } + else + { + qDebug() << reply->errorString(); + delete reply; + } + } + + return(serieList); +} diff --git a/cthemoviedbv3.h b/cthemoviedbv3.h index 59d3646..31f661e 100644 --- a/cthemoviedbv3.h +++ b/cthemoviedbv3.h @@ -27,6 +27,7 @@ public: cSerie* loadSerie(const qint32 &iID, const QString& szLanguage); cSerie* loadSerie(const QString& szIMDBID); QMap genresSerie(const QString& szLanguage = "all"); + QList discoverSerie(const QString& szText, const qint16& iYear, const QList& genres, const qreal& voteMin, const qreal& voteMax, const QString& szLanguage = "all"); private: QString m_szToken; QMap m_genres;