app/sources/brcompetition.cpp

143 lines
4 KiB
C++

#include "headers/brcompetition.h"
#include "headers/brleague.h"
#include "headers/brprovider.h"
BRCompetition::BRCompetition(BRProvider* provider, BRWidget::BRFederation federation, int id, BRCompetitionData initialData) : BRWidget(provider, federation, id) {
this->currentCategory = nullptr;
this->setData(initialData);
}
void BRCompetition::setLeague(BRLeague* league) {
if(league == nullptr)
return;
this->league = league;
emit this->metadataChanged();
}
QString BRCompetition::getName() {
return this->name;
}
BRCompetition::BRDiscipline BRCompetition::getDiscipline() {
return this->discipline;
}
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;
}
QList<QObject*> BRCompetition::getCategoriesQML() {
return this->listToQmlList(this->categories);
}
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())
if(round->getName() == currentRoundName)
category->setCurrentRound(round);
}
this->currentCategory = category;
emit this->currentCategoryChanged();
}
QList<QObject*> BRCompetition::getResultsQML() {
if(this->currentCategory == nullptr || this->currentCategory->getCurrentRound() == nullptr)
return {};
return this->currentCategory->getCurrentRound()->getResultsQML();
}
BRWidget::BRWidgetStatusCode BRCompetition::load() {
BRCompetitionData newData {
this,
this->name,
this->discipline,
this->startDate,
this->endDate,
this->eventWebsiteUrl,
this->infosheetUrls,
this->currentCategory,
this->categories
};
BRWidget::BRWidgetStatusCode statusCode = this->getProvider()->getWidgetData(&newData);
if(statusCode != BRWidget::Success)
return statusCode;
this->setData(newData);
return BRWidget::Success;
}
void BRCompetition::setData(BRCompetition::BRCompetitionData data) {
this->name = data.name;
this->discipline = data.discipline;
this->startDate = data.startDate;
this->endDate = data.endDate;
this->eventWebsiteUrl = data.eventWebsiteUrl;
this->infosheetUrls = data.infosheetUrls;
emit this->metadataChanged();
if(this->currentCategory != data.currentCategory) {
this->currentCategory = data.currentCategory;
emit this->currentCategoryChanged();
}
if(this->categories != data.categories) {
this->categories.clear();
this->categories = data.categories;
for(BRCategory* category : this->categories) {
category->competition = this;
connect(category, &BRCategory::resultsChanged, this, &BRCompetition::resultsChanged);
}
emit this->categoriesChanged();
}
}
bool BRCompetition::lessThan(BRCompetition* competition1, BRCompetition* competition2) {
return competition1->startDate < competition2->startDate;
}