From 100e73ec28dbc1f307c540cbd4b5a04dfe5922f8 Mon Sep 17 00:00:00 2001 From: Steru Date: Fri, 16 Aug 2024 22:19:42 +0200 Subject: Compacted competitors into one object, deleted API class (now in sportmodel). --- src/model/Competitor.cpp | 114 +++++++++++++++++++++++-- src/model/Competitor.h | 71 ++++++++++++---- src/model/CompetitorWithResults.cpp | 61 -------------- src/model/CompetitorWithResults.h | 40 --------- src/model/EventInfo.cpp | 9 +- src/model/EventInfo.h | 23 +++--- src/model/MedalWinner.cpp | 57 ------------- src/model/MedalWinner.h | 39 --------- src/model/SportModel.cpp | 161 +++++++++++------------------------- src/model/SportModel.h | 25 +++--- 10 files changed, 239 insertions(+), 361 deletions(-) delete mode 100644 src/model/CompetitorWithResults.cpp delete mode 100644 src/model/CompetitorWithResults.h delete mode 100644 src/model/MedalWinner.cpp delete mode 100644 src/model/MedalWinner.h (limited to 'src/model') diff --git a/src/model/Competitor.cpp b/src/model/Competitor.cpp index a584689..c0ee01d 100644 --- a/src/model/Competitor.cpp +++ b/src/model/Competitor.cpp @@ -1,22 +1,126 @@ #include "Competitor.h" +/** + * Reads certain properties from a competitor json object. + * These are: code, name, noc, results + * + * For further information on 'results' see [Competitor::setResult]. + * + * Does not set the amounts of medals. For this, call [Competitor::setMedals]. + * + * @param competitor The competitor as a QJsonObject. + * @return True if successful. + */ bool Competitor::setCompetitor(const QJsonObject &competitor) { if (!competitor.contains("code") || !competitor.contains("name") - || !competitor.contains("m_noc")) { - throw invalid_argument("Not a competitor object."); + || !competitor.contains("noc")) { + return false; } setCode(competitor["code"].toInt()); setName(competitor["name"].toString()); - setNOC(competitor["m_noc"].toString()); - return true; + setNOC(competitor["noc"].toString()); + + if (!competitor.contains("results")) return false; + QJsonObject results = competitor["results"].toObject(); + return setResults(results); } -bool Competitor::setCompetitor(const Competitor &competitor) { +/** + * Copies all values of a given competitor. + * + * @param competitor The competitor to copy. + */ +void Competitor::setCompetitor(const Competitor &competitor) { setCode(competitor.m_code); setName(competitor.m_name); setNOC(competitor.m_noc); + setMark(competitor.m_mark); + setMedalType(competitor.m_medalType); + setStatistic(competitor.m_statistic); + setGold(competitor.m_gold); + setSilver(competitor.m_silver); + setBronze(competitor.m_bronze); +} + +/** + * Replaces/sets the results of a competitor. + * + * @param results The results of the competitor. + * @return True, if successful. + */ +bool Competitor::setResults(const QJsonObject &results) { + if (!results.contains("mark") + || !results.contains("medalType")) { + return false; + } + + setMark(results["mark"].toString()); + setMedalType(results["medalType"].toString()); + + return true; +} + +/** + * Replaces/sets the won medals of a competitor. + * + * @param medals The won medals with their amount. + * @return True, if successful. + */ +bool Competitor::setMedals(const map &medals) { + if (medals.find("ME_GOLD") == medals.end() + || medals.find("ME_SILVER") == medals.end() + || medals.find("ME_BRONZE") == medals.end()) return false; + + setGold(medals.find("ME_GOLD")->second); + setSilver(medals.find("ME_SILVER")->second); + setBronze(medals.find("ME_BRONZE")->second); + return true; } + +/** + * Static compare method, which can compare the result times or points of two competitors. + * Returns true, if the left competitor (lComp) got a real lesser score than the right competitor (rComp). + * + * @param lComp First competitor to compare. + * @param rComp Second competitor to compare. + * @return True, if the second competitor got a higher score. + */ +bool Competitor::compareMark(Competitor lComp, Competitor rComp) { + QString l = lComp.mark(); + QString r = rComp.mark(); + + // check if values are numerical (-> not time values) + if (!l.contains(":") || !r.contains(":")) { + return l.toFloat() < r.toFloat(); + } + + // compare time values if not numerical + QString lTime(""), rTime(""); + + for (QChar c : l) if (c.isDigit()) lTime.append(c); + for (QChar c : r) if (c.isDigit()) rTime.append(c); + + return lTime.compare(rTime) < 0; +} + +/** + * Static compare method, which can compare the amount of medals of two competitors. + * Gold has the highest priority, then m_silver and finally m_bronze. + * + * @param lComp First competitor to compare. + * @param rComp Second competitor to compare. + * @return True, if the second competitor got more or higher medals. + */ +bool Competitor::compareMedals(Competitor lComp, Competitor rComp) { + // create difference between medal amounts + int gold = lComp.gold() - rComp.gold(); + int silver = lComp.silver() - rComp.silver(); + int bronze = lComp.bronze() - rComp.bronze(); + + // compare medal differences + return gold < 0 || (gold == 0 && (silver < 0 || (silver == 0 && bronze < 0))); +} diff --git a/src/model/Competitor.h b/src/model/Competitor.h index 1913325..e27b537 100644 --- a/src/model/Competitor.h +++ b/src/model/Competitor.h @@ -14,39 +14,82 @@ class Competitor : public QObject { Q_OBJECT - Q_PROPERTY(int code READ code NOTIFY nCode) - Q_PROPERTY(QString name READ name NOTIFY nName) - Q_PROPERTY(QString noc READ noc NOTIFY nNoc) + Q_PROPERTY(int code READ code NOTIFY codeChanged) + Q_PROPERTY(QString name READ name NOTIFY nameChanged) + Q_PROPERTY(QString noc READ noc NOTIFY nocChanged) + + // results in a certain event/category + Q_PROPERTY(QString mark READ mark NOTIFY markChanged) + Q_PROPERTY(QString medalType READ medalType NOTIFY medalTypeChanged) + Q_PROPERTY(QString statistic READ statistic NOTIFY statisticChanged) + + // medal amounts in the whole discipline + Q_PROPERTY(int gold READ gold NOTIFY goldChanged) + Q_PROPERTY(int silver READ silver NOTIFY silverChanged) + Q_PROPERTY(int bronze READ bronze NOTIFY bronzeChanged) public: explicit Competitor(QObject *parent) : QObject(parent) {} - int getCode() { return this->m_code; } - QString getName() { return this->m_name; } - QString getNOC() { return this->m_noc; } + // getter + int code() const { return this->m_code; } + QString name() const { return this->m_name; } + QString noc() const { return this->m_noc; } + QString mark() const { return this->m_mark; } + QString medalType() const { return this->m_medalType; } + QString statistic() const { return this->m_statistic; } + int gold() const { return this->m_gold; } + int silver() const { return this->m_silver; } + int bronze() const { return this->m_bronze; } + // setter void setCode(int code) { this->m_code = code; } void setName(QString name) { this->m_name = name; } void setNOC(QString noc) { this->m_noc = noc; } + void setMark(QString mark) { this->m_mark = mark; } + void setMedalType(QString medalType) { this->m_medalType = medalType; } + void setStatistic(QString stat) { this->m_statistic = stat; } + void setGold(int gold) { this->m_gold = gold; } + void setSilver(int silver) { this->m_silver = silver; } + void setBronze(int bronze) { this->m_bronze = bronze; } + + bool setResults(const QJsonObject &results); + bool setMedals(const map &medals); bool setCompetitor(const QJsonObject &competitor); - bool setCompetitor(const Competitor &competitor); + void setCompetitor(const Competitor &competitor); - static bool compareName(const Competitor &left, const Competitor &right) { - return left.m_name.compare(right.m_name) < 0; + static bool compareName(Competitor lComp, Competitor rComp) { + return lComp.m_name.compare(rComp.m_name) < 0; } - static bool compareNOC(const Competitor &left, const Competitor &right) { - return left.m_noc.compare(right.m_noc) < 0; + static bool compareNOC(Competitor lComp, Competitor rComp) { + return lComp.m_noc.compare(rComp.m_noc) < 0; } + static bool compareMark(Competitor lComp, Competitor rComp); + static bool compareMedals(Competitor lComp, Competitor rComp); + signals: - void nCode(); - void nName(); - void nNoc(); + void codeChanged(); + void nameChanged(); + void nocChanged(); + void markChanged(); + void medalTypeChanged(); + void statisticChanged(); + void goldChanged(); + void silverChanged(); + void bronzeChanged(); private: int m_code; QString m_name; QString m_noc; + QString m_mark; + QString m_medalType; + QString m_statistic; + + int m_gold; + int m_silver; + int m_bronze; }; diff --git a/src/model/CompetitorWithResults.cpp b/src/model/CompetitorWithResults.cpp deleted file mode 100644 index 70d9473..0000000 --- a/src/model/CompetitorWithResults.cpp +++ /dev/null @@ -1,61 +0,0 @@ - -#include "CompetitorWithResults.h" - -/** - * Replaces/sets the results of a competitor. - * - * @param results The results of the competitor. - * @return True, if successful. - */ -bool CompetitorWithResults::setResults(const QJsonObject &results) { - if (!results.contains("m_mark") - || !results.contains("m_medalType")) { - return false; - } - - this->m_mark = results["m_mark"].toString(); - this->m_medalType = results["m_medalType"].toString(); - - return true; -} - -/** - * Static compare method, which can compare the result times or points of two CompetitorsWithResult. - * Returns true, if the left competitor (lComp) got a real lesser score than the right competitor (rComp). - * - * @param lComp First competitor to compare. - * @param rComp Second competitor to compare. - * @return True, if the second competitor got a higher score. - */ -bool CompetitorWithResults::compare(CompetitorWithResults lComp, CompetitorWithResults rComp) { - QString l = lComp.getMark(); - QString r = rComp.getMark(); - - // check if values are numerical (-> not time values) - if (!l.contains(":") || !r.contains(":")) { - return l.toFloat() < r.toFloat(); - } - - // compare time values if not numerical - QString lTime(""), rTime(""); - - for (QChar c : l) if (c.isDigit()) lTime.append(c); - for (QChar c : r) if (c.isDigit()) rTime.append(c); - - return lTime.compare(rTime) < 0; -} - -bool CompetitorWithResults::setCompetitorWithResults(const QJsonObject &competitor) { - setCompetitor(competitor); - - if (!competitor.contains("results")) return false; - QJsonObject results = competitor["results"].toObject(); - return setResults(results); -} - -void CompetitorWithResults::setCompetitorWithResults(const CompetitorWithResults &competitor) { - setCompetitor(competitor); - setMark(competitor.m_mark); - setMedalType(competitor.m_medalType); - setStatistic(competitor.m_statistic); -} diff --git a/src/model/CompetitorWithResults.h b/src/model/CompetitorWithResults.h deleted file mode 100644 index a98a1e8..0000000 --- a/src/model/CompetitorWithResults.h +++ /dev/null @@ -1,40 +0,0 @@ - -#pragma once - -#include "Competitor.h" -#include -#include -#include -#include - -class CompetitorWithResults : public Competitor { - - Q_OBJECT - - Q_PROPERTY(QString mark READ mark NOTIFY nMark) - Q_PROPERTY(QString medalType READ medalType NOTIFY nMedalType) - Q_PROPERTY(QString statistic READ statistic NOTIFY nStatistic) - -public: - explicit CompetitorWithResults(Competitor *parent) : Competitor(parent) {} - - bool setResults(const QJsonObject &results); - void setMark(QString mark) { this->m_mark = mark; } - void setMedalType(QString medalType) { this->m_medalType = medalType; } - void setStatistic(QString stat) { this->m_statistic = stat; } - - bool setCompetitorWithResults(const QJsonObject &competitor); - void setCompetitorWithResults(const CompetitorWithResults &competitor); - - QString getMark() { return this->m_mark; } - QString getMedalType() { return this->m_medalType; } - QString getStatistic() { return this->m_statistic; } - - static bool compare(CompetitorWithResults lComp, CompetitorWithResults rComp); - -private: - QString m_mark; - QString m_medalType; - QString m_statistic; - -}; diff --git a/src/model/EventInfo.cpp b/src/model/EventInfo.cpp index e9ecd46..fef0fba 100644 --- a/src/model/EventInfo.cpp +++ b/src/model/EventInfo.cpp @@ -1,22 +1,21 @@ -#include #include "EventInfo.h" EventInfo::EventInfo(QObject *parent) : QObject(parent) { } QString EventInfo::eventName() const { - return m_eventName; + return this->m_eventName; } void EventInfo::setEventName(const QString &newEventName) { m_eventName = newEventName; } -QList EventInfo::competitors() const { +QList EventInfo::competitors() const { return m_competitors; } -void EventInfo::setCompetitors(const QList &newCompetitors) { - m_competitors = newCompetitors; +void EventInfo::setCompetitors(const QList &newCompetitors) { + this->m_competitors = newCompetitors; } diff --git a/src/model/EventInfo.h b/src/model/EventInfo.h index 7f937b2..def3885 100644 --- a/src/model/EventInfo.h +++ b/src/model/EventInfo.h @@ -1,28 +1,27 @@ -#ifndef ITAT_CHALLANGE_OLYMPICS_EVENT_H -#define ITAT_CHALLANGE_OLYMPICS_EVENT_H +#pragma once +#include "Competitor.h" #include +#include #include -class EventInfo : QObject { +class EventInfo : public QObject { Q_OBJECT // QML_ELEMENT - Q_PROPERTY(QString eventName READ eventName WRITE setEventName); - Q_PROPERTY(QList competitors READ competitors WRITE setCompetitors); + Q_PROPERTY(QString eventName READ eventName CONSTANT) + Q_PROPERTY(QList competitors READ competitors CONSTANT) - public: +public: explicit EventInfo(QObject *parent = nullptr); QString eventName() const; void setEventName(const QString &newEventName); - QList competitors() const; - void setCompetitors(const QList &newCompetitors); + QList competitors() const; + void setCompetitors(const QList &newCompetitors); - private: +private: QString m_eventName; - QList m_competitors; + QList m_competitors; }; - -#endif diff --git a/src/model/MedalWinner.cpp b/src/model/MedalWinner.cpp deleted file mode 100644 index 56cfb22..0000000 --- a/src/model/MedalWinner.cpp +++ /dev/null @@ -1,57 +0,0 @@ - -#include "MedalWinner.h" - -/** - * Replaces/sets the won medals of a competitor. - * - * @param medals The won medals with their amount. - * @return True, if successful. - */ -bool MedalWinner::setMedals(const QJsonObject &medals) { - if (!medals.contains("ME_GOLD") - || !medals.contains("ME_SILVER") - || !medals.contains("ME_BRONZE")) { - return false; - } - - setGold(medals["ME_GOLD"].toInt()); - setSilver(medals["ME_SILVER"].toInt()); - setBronze(medals["ME_BRONZE"].toInt()); - - return true; -} - -/** - * Static compare method, which can compare the amount of medals of two MedalWinners. - * Gold has the highest priority, then m_silver and finally m_bronze. - * - * @param lComp First competitor to compare. - * @param rComp Second competitor to compare. - * @return True, if the second competitor got more or higher medals. - */ -bool MedalWinner::compare(MedalWinner lComp, MedalWinner rComp) { - // create difference between medal amounts - int gold = lComp.getGold() - rComp.getGold(); - int silver = lComp.getSilver() - rComp.getSilver(); - int bronze = lComp.getBronze() - rComp.getBronze(); - - // compare medal differences - return gold < 0 || (gold == 0 && (silver < 0 || (silver == 0 && bronze < 0))); -} - -bool MedalWinner::setMedalWinner(const QJsonObject &competitor) { - setCompetitor(competitor); - - if (!competitor.contains("medals")) return false; - QJsonObject medals = competitor["medals"].toObject(); - setMedals(medals); - return true; -} - -void MedalWinner::setMedalWinner(const MedalWinner &competitor) { - setCompetitor(competitor); - - setGold(competitor.m_gold); - setSilver(competitor.m_silver); - setBronze(competitor.m_bronze); -} diff --git a/src/model/MedalWinner.h b/src/model/MedalWinner.h deleted file mode 100644 index 10d08bd..0000000 --- a/src/model/MedalWinner.h +++ /dev/null @@ -1,39 +0,0 @@ - -#pragma once - -#include "Competitor.h" -#include -#include - -#include - -class MedalWinner : public Competitor { - - Q_OBJECT - - Q_PROPERTY(int gold READ m_gold NOTIFY nGold) - Q_PROPERTY(int silver READ m_silver NOTIFY nSilver) - Q_PROPERTY(int bronze READ m_bronze NOTIFY nBronze) - -public: - explicit MedalWinner(Competitor *parent) : Competitor(parent) {} - - bool setMedalWinner(const QJsonObject &competitor); - void setMedalWinner(const MedalWinner &competitor); - - bool setMedals(const QJsonObject &medals); - - void setGold(int gold) { this->m_gold = gold; } - void setSilver(int silver) { this->m_silver = silver; } - void setBronze(int bronze) { this->m_bronze = bronze; } - - int getGold() { return m_gold; } - int getSilver() { return m_silver; } - int getBronze() { return m_bronze; } - - static bool compare(MedalWinner lComp, MedalWinner rComp); - -private: - int m_gold, m_silver, m_bronze; - -}; diff --git a/src/model/SportModel.cpp b/src/model/SportModel.cpp index 2e847b3..b393498 100644 --- a/src/model/SportModel.cpp +++ b/src/model/SportModel.cpp @@ -8,8 +8,7 @@ #include // sorting and filtering -#include -#include +//#include #include // float to string formatting @@ -42,7 +41,7 @@ QVariant SportModel::data(const QModelIndex &index, int role) const { return event->eventName(); case Competitors: - return event->competitors(); + return QVariant::fromValue(event->competitors()); } } @@ -81,14 +80,14 @@ void SportModel::parseData() { qDeleteAll(m_sportList); m_sportList.clear(); - - QByteArray strReply = m_reply->readAll(); //parse json // qDebug() << "Response:" << strReply; QJsonDocument jsonDocument = QJsonDocument::fromJson(strReply); + map> medals = getMedalsOfCompetitors(); + QJsonArray sports = jsonDocument["units"].toArray(); for (const auto &sport : sports) { QJsonObject entry = sport.toObject(); @@ -96,15 +95,20 @@ void SportModel::parseData() { EventInfo *event = new EventInfo(this); event->setEventName(entry["eventUnitName"].toString()); - QList competitors; + QList competitors; for (const auto &competitor : entry["competitors"].toArray()) { - competitors << competitor.toObject()["name"].toString(); + Competitor *comp = new Competitor(this); + comp->setCompetitor(competitor.toObject()); + if (medals.find(comp->name()) != medals.end()) comp->setMedals(medals.find(comp->name())->second); + if (!competitors.empty()) comp->setStatistic(competitors.first()->mark()); + competitors << comp; } event->setCompetitors(competitors); qDebug() << entry["eventUnitName"].toString(); m_sportList << event; } + endResetModel(); } } @@ -133,7 +137,7 @@ void SportModel::lastName(QList &competitors) { for (int i = 0; i < competitors.size(); i++) { Competitor* comp = competitors.value(i); - string fullName = comp->getName().toUtf8().constData(); + string fullName = comp->name().toUtf8().constData(); // regex to identify names, written in CAPS regex r("[A-Z']{2,}"); @@ -184,43 +188,13 @@ set SportModel::getCategories() { } /** - * @brief Sport::getCompetitorsByCategory Searches for all competitors, who took part in the given category. - * @param category The category to search in. - * @return An QJsonArray with all competitors as QJsonValueRef, which can be casted to QJsonObject. - */ -QList SportModel::getCompetitorsByCategory(QString category) { - QList competitors; - - if (!validateDiscipline()) return competitors; - - for (const QJsonValueRef &unitRef : this->o_discipline["units"].toArray()) { - QJsonObject unit = unitRef.toObject(); - - // validate unit - if (!unit.contains("eventUnitName") || !unit.contains("competitors")) continue; - - // search all units with the same category - if (unit["eventUnitName"].toString().compare(category, Qt::CaseSensitive) != 0) continue; - - // add all competitors from one unit - for (const QJsonValueRef &compRef : unit["competitors"].toArray()) { - CompetitorWithResults *comp = new CompetitorWithResults(); // TODO declare comp - comp->setCompetitorWithResults(compRef.toObject()); - competitors.push_back(comp); - } - } - - return competitors; -} - -/** - * @brief Sport::getCompetitorsWithMedal Filters all competitors, who have at least one medal. These objects are different from getCompetitorsByCategory !!! + * @brief Sport::getMedalsOfCompetitor Filters all competitors, who have at least one medal. These objects are different from getCompetitorsByCategory !!! * @return All competitors, who won at least one medal. Structure of one competitor: {code, name, m_noc, medals{ME_GOLD, ME_SILVER, ME_BRONZE}} */ -QList SportModel::getCompetitorsWithMedal() { - map competitors; +map> SportModel::getMedalsOfCompetitors() { + map> competitors; - if (!validateDiscipline()) return QList(); + if (!validateDiscipline()) return competitors; // filter all units, which have medal events QJsonArray units = filter(this->o_discipline["units"].toArray(), [](QJsonObject unit){ @@ -241,7 +215,7 @@ QList SportModel::getCompetitorsWithMedal() { QJsonArray medalComps = filter(unit["competitors"].toArray(), [](QJsonObject comp) { if (!comp.contains("results")) return false; - QString medalType = comp["results"].toObject()["m_medalType"].toString(); + QString medalType = comp["results"].toObject()["medalType"].toString(); return !medalType.isEmpty(); }); @@ -251,65 +225,26 @@ QList SportModel::getCompetitorsWithMedal() { // validate competitor (with medal) if (!medalComp.contains("name") || !medalComp.contains("results") - || !medalComp["results"].toObject().contains("m_medalType")) continue; + || !medalComp["results"].toObject().contains("medalType")) continue; QString name = medalComp["name"].toString(); - QString medalType = medalComp["results"].toObject()["m_medalType"].toString(); + QString medalType = medalComp["results"].toObject()["medalType"].toString(); // check if competitor has other medal(s) if (competitors.find(name) == competitors.end()) { - competitors.insert({name, createCompetitorWithMedals(medalComp)}); + map emptyMedalObject = { + {"ME_GOLD", 0}, + {"ME_SILVER", 0}, + {"ME_BRONZE", 0} + }; + competitors.insert({name, emptyMedalObject}); } // update the medal count - QJsonObject updatedMedalCount = QJsonObject(competitors.find(name)->second["medals"].toObject()); - - int amount = updatedMedalCount[medalType].toInt() + 1; - updatedMedalCount.remove(medalType); - updatedMedalCount.insert(medalType, amount); - - // create new medals QJsonObject and set it in the map - competitors.find(name)->second["medals"] = updatedMedalCount; + competitors.find(name)->second.find(medalType)->second++; } } - - // convert map to QJsonArray - QList output; - for (const pair &competitor : competitors) { - MedalWinner *comp = new MedalWinner(); // TODO declare comp - comp->setMedalWinner(competitor.second); - output.append(comp); - } - - return output; -} - -/** - * @brief Sport::createCompetitorWithMedals Creates a competitor QJsonObject with the following attributes: code, name, m_noc, medals{ME_GOLD, ME_SILVER, ME_BRONZE} - * @param comp The original competitor object. - * @return A competitor object with medal counts. - */ -QJsonObject SportModel::createCompetitorWithMedals(QJsonObject comp) { - // repair competitor if something is missing - if (!comp.contains("code")) comp.insert("code", "0"); - if (!comp.contains("name")) comp.insert("code", ""); - if (!comp.contains("m_noc")) comp.insert("code", ""); - - // create new competitor QJsonObject and add it to the competitor map - QJsonObject medals { - {"ME_GOLD", 0}, - {"ME_SILVER", 0}, - {"ME_BRONZE", 0} - }; - - QJsonObject medalComp { - {"code", comp["code"].toString()}, - {"name", comp["name"].toString()}, - {"m_noc", comp["m_noc"].toString()}, - {"medals", medals} - }; - - return medalComp; + return competitors; } /** @@ -338,7 +273,7 @@ void SportModel::filterByCountry(QList &competitors, QString nocSho */ void SportModel::filterCompetitors(QList &competitors, QString filter) { for (int i = 0; i < competitors.size(); i++) { - if (!competitors.value(i)->getNOC().contains(filter)) { + if (!competitors.value(i)->noc().contains(filter)) { competitors.remove(i); i--; } @@ -349,37 +284,37 @@ void SportModel::filterCompetitors(QList &competitors, QString filt * @brief Sport::sortByName Sort the competitors by their name (alphabetical, ascending). * @param competitors The competitors of one category. */ -void SportModel::sortByName(QList &competitors) { - if (competitors.isEmpty()) return; - std::sort(competitors.begin(), competitors.end(), Competitor::compareName); -} +//void SportModel::sortByName(QList &competitors) { +// if (competitors.isEmpty()) return; +// std::sort(competitors.begin(), competitors.end(), Competitor::compareName); +//} /** * @brief Sport::sortByCountry Sort the competitors by their national olympic comittee short name (alphabetical, ascending). * @param competitors The competitors of one category. */ -void SportModel::sortByCountry(QList &competitors) { - if (competitors.isEmpty()) return; - std::sort(competitors.begin(), competitors.end(), Competitor::compareNOC); -} +//void SportModel::sortByCountry(QList &competitors) { +// if (competitors.isEmpty()) return; +// std::sort(competitors.begin(), competitors.end(), Competitor::compareNOC); +//} /** * @brief Sport::sortByResult Sort the competitors by their results in one specific category (numerical, ascending). * @param competitors The competitors of one category. */ -void SportModel::sortByResult(QList &competitors) { - if (competitors.isEmpty()) return; - std::sort(competitors.begin(), competitors.end(), CompetitorWithResults::compare); -} +//void SportModel::sortByResult(QList &competitors) { +// if (competitors.isEmpty()) return; +// std::sort(competitors.begin(), competitors.end(), Competitor::compareMark); +//} /** * @brief Sport::sortByMedals Sort the competitors by their medal amounts in one specific category (numerical, ascending). * @param competitors The competitors of one category. */ -void SportModel::sortByMedals(QList &competitors) { - if (competitors.isEmpty()) return; - std::sort(competitors.begin(), competitors.end(), MedalWinner::compare); -} +//void SportModel::sortByMedals(QList &competitors) { +// if (competitors.isEmpty()) return; +// std::sort(competitors.begin(), competitors.end(), Competitor::compareMedals); +//} /** * @brief Sport::reverseOrder Reverses the order of the competitors. @@ -402,15 +337,15 @@ void SportModel::reverseOrder(QList &competitors) { * Stores the m_statistic in obj->results->stat for each competitor. * @param competitors The competitors of one category. */ -void SportModel::addRelativeToFirst(QList &competitors) { +void SportModel::addRelativeToFirst(QList &competitors) { if (competitors.isEmpty()) return; - QString reference = competitors.value(0)->getMark(); + QString reference = competitors.value(0)->mark(); for (int i = 0; i < competitors.size(); i++) { - CompetitorWithResults *comp = competitors.value(i); + Competitor *comp = competitors.value(i); - QString result = comp->getMark(); + QString result = comp->mark(); // format relative float value to string with 2 digits after decimal point and sign stringstream sstream; diff --git a/src/model/SportModel.h b/src/model/SportModel.h index 5c3bad1..808a1cc 100644 --- a/src/model/SportModel.h +++ b/src/model/SportModel.h @@ -1,7 +1,5 @@ #pragma once -#include "MedalWinner.h" -#include "CompetitorWithResults.h" #include #include #include @@ -40,23 +38,22 @@ public: virtual QHash roleNames() const override; set getCategories(); - QList getCompetitorsByCategory(QString category); // TODO ref instead of obj - QList getCompetitorsWithMedal(); // TODO ref instead of obj + map> getMedalsOfCompetitors(); // filter to change the current competitor list - void lastName(QList &competitors); // TODO ref instead of obj - void filterByName(QList &competitors, QString name); // TODO ref instead of obj - void filterByCountry(QList &competitors, QString nocShort); // TODO ref instead of obj + void lastName(QList &competitors); + void filterByName(QList &competitors, QString name); + void filterByCountry(QList &competitors, QString nocShort); // sort functions to change the order of the current competitor list - void sortByName(QList &competitors); // TODO ref instead of obj - void sortByCountry(QList &competitors); // TODO ref instead of obj - void sortByResult(QList &competitors); // TODO ref instead of obj - void sortByMedals(QList &competitors); // TODO ref instead of obj - void reverseOrder(QList &competitors); // TODO ref instead of obj +// void sortByName(QList &competitors); +// void sortByCountry(QList &competitors); +// void sortByResult(QList &competitors); +// void sortByMedals(QList &competitors); + void reverseOrder(QList &competitors); // statistic function - void addRelativeToFirst(QList &competitors); // TODO ref instead of obj + void addRelativeToFirst(QList &competitors); QString discipline() const; void setDiscipline(const QString &discipline); @@ -77,8 +74,6 @@ private: void filterCompetitors(QList &competitors, QString filter); // TODO ref instead of obj - QJsonObject createCompetitorWithMedals(QJsonObject medalComp); - // function for statistic calculation float calcRelativeStat(QString ref, QString val); -- cgit v1.2.3