#include "brcompetition.h" #include "brleague.h" #include "brprovider.h" 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(); } 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 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 BRCompetition::getCategoriesQML() { return this->listToQmlList(this->categories); } QList 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(); } QList BRCompetition::getResultsQML() { if(this->currentCategory == nullptr) 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 {}; } BRWidget::BRWidgetStatusCode BRCompetition::load() { BRCompetitionData newData { this, this->name, this->startDate, this->endDate, this->eventWebsiteUrl, this->infosheetUrls, this->pinned, 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->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; }