app/sources/competition/brcompetition.cpp

164 lines
4.6 KiB
C++
Raw Normal View History

#include "brcompetition.h"
#include "brleague.h"
#include "brprovider.h"
2020-10-31 15:16:06 +01:00
BRCompetition::BRCompetition(BRProvider* provider, BRWidget::BRFederation federation, int id, BRCompetitionData initialData) : BRWidget(provider, federation, id) {
this->currentCategory = nullptr;
this->eventWebsiteUrl = "";
this->pinned = false;
connect(this, &BRCompetition::categoriesChanged, this, &BRCompetition::resultsChanged);
this->setData(initialData);
}
void BRCompetition::setLeague(BRLeague* league) {
if(league == nullptr)
return;
this->league = league;
emit this->metadataChanged();
2020-10-31 15:16:06 +01:00
}
QString BRCompetition::getName() {
return this->name;
}
QDate BRCompetition::getStartDate() {
return this->startDate;
}
QDate BRCompetition::getEndDate() {
return this->endDate;
}
QString BRCompetition::getDateSpan() {
if(this->startDate == this->endDate)
return this->startDate.toString("d MMMM yyyy");
else
return this->startDate.toString("d MMMM yyyy") + " - " + this->endDate.toString("d MMMM yyyy");
}
BRLeague* BRCompetition::getLeague() {
return this->league;
}
QUrl BRCompetition::getEventWebsiteUrl() {
return this->eventWebsiteUrl;
}
QList<QUrl> BRCompetition::getInfosheetUrls() {
return this->infosheetUrls;
}
bool BRCompetition::getPinned() {
return this->pinned;
}
void BRCompetition::setPinned(bool pinned) {
if(this->pinned == pinned)
return;
this->pinned= pinned;
emit this->pinnedChanged();
}
QList<QObject*> BRCompetition::getCategoriesQML() {
return this->listToQmlList(this->categories);
}
2020-11-04 17:25:28 +01:00
QList<BRCategory*> BRCompetition::getCategories() {
return this->categories;
}
BRCategory* BRCompetition::getCurrentCategory() const {
return this->currentCategory;
}
void BRCompetition::setCurrentCategory(BRCategory* category) {
if(!this->categories.contains(category))
return;
// try to find the same round in the new category
if(this->currentCategory != nullptr && this->currentCategory->getCurrentRound() != nullptr) {
QString currentRoundName = this->currentCategory->getCurrentRound()->getName();
// search round name in new category
for(BRRound* round : category->getRounds()) {
qDebug() << "Trying to restore round: " << currentRoundName << " with round: " << round->getName();
if(round->getName() == currentRoundName)
category->setCurrentRound(round);
}
qDebug() << "Restored round with ID: " << category->currentRound->getId();
}
this->currentCategory = category;
emit this->currentCategoryChanged();
}
2020-11-04 17:25:28 +01:00
QList<QObject*> BRCompetition::getResultsQML() {
if(this->currentCategory == nullptr)
2020-11-04 17:25:28 +01:00
return {};
qDebug() << "getting results";
if( this->currentCategory->getCurrentRound() != nullptr)
return this->currentCategory->getCurrentRound()->getResultsQML();
else if(this->currentCategory->getGeneralResult() != nullptr)
return this->currentCategory->getGeneralResult()->getResultsQML();
else
return {};
2020-11-04 17:25:28 +01:00
}
2020-10-31 15:16:06 +01:00
BRWidget::BRWidgetStatusCode BRCompetition::load() {
BRCompetitionData newData {
this,
this->name,
this->startDate,
this->endDate,
this->eventWebsiteUrl,
this->infosheetUrls,
this->pinned,
2020-11-04 17:25:28 +01:00
this->currentCategory,
this->categories
};
BRWidget::BRWidgetStatusCode statusCode = this->getProvider()->getWidgetData(&newData);
if(statusCode != BRWidget::Success)
return statusCode;
this->setData(newData);
2020-10-31 15:16:06 +01:00
return BRWidget::Success;
2020-10-31 15:16:06 +01:00
}
void BRCompetition::setData(BRCompetition::BRCompetitionData data) {
this->name = data.name;
this->startDate = data.startDate;
this->endDate = data.endDate;
this->eventWebsiteUrl = data.eventWebsiteUrl;
this->infosheetUrls = data.infosheetUrls;
emit this->metadataChanged();
2020-11-04 17:25:28 +01:00
if(this->currentCategory != data.currentCategory) {
this->currentCategory = data.currentCategory;
emit this->currentCategoryChanged();
}
if(this->categories != data.categories) {
this->categories.clear();
this->categories = data.categories;
2020-11-04 17:25:28 +01:00
for(BRCategory* category : this->categories) {
category->competition = this;
2020-11-04 17:25:28 +01:00
connect(category, &BRCategory::resultsChanged, this, &BRCompetition::resultsChanged);
}
emit this->categoriesChanged();
}
}
bool BRCompetition::lessThan(BRCompetition* competition1, BRCompetition* competition2) {
return competition1->startDate < competition2->startDate;
}