From b869b8983656b12f9dee59439acfb6b3db647b72 Mon Sep 17 00:00:00 2001 From: Steru Date: Thu, 15 Aug 2024 23:27:54 +0200 Subject: Made Competitor a QObject and tidied up some code. --- src/model/Competitor.cpp | 8 +++--- src/model/Competitor.h | 50 ++++++++++++++++--------------------- src/model/CompetitorWithResults.cpp | 8 +++--- src/model/CompetitorWithResults.h | 36 +++++++++++--------------- src/model/MedalWinner.cpp | 8 +++--- src/model/MedalWinner.h | 32 +++++++++--------------- src/model/Sport.cpp | 28 +++++++++++---------- 7 files changed, 76 insertions(+), 94 deletions(-) (limited to 'src/model') diff --git a/src/model/Competitor.cpp b/src/model/Competitor.cpp index d39d134..080dd4d 100644 --- a/src/model/Competitor.cpp +++ b/src/model/Competitor.cpp @@ -4,12 +4,12 @@ bool Competitor::setCompetitor(const QJsonObject &competitor) { if (!competitor.contains("code") || !competitor.contains("name") - || !competitor.contains("noc")) { + || !competitor.contains("m_noc")) { throw invalid_argument("Not a competitor object."); } - this->code = competitor["code"].toInt(); - this->name = competitor["name"].toString(); - this->noc = competitor["noc"].toString(); + this->m_code = competitor["code"].toInt(); + this->m_name = competitor["name"].toString(); + this->m_noc = competitor["m_noc"].toString(); return true; } diff --git a/src/model/Competitor.h b/src/model/Competitor.h index 51fc3b1..79552a5 100644 --- a/src/model/Competitor.h +++ b/src/model/Competitor.h @@ -4,11 +4,12 @@ #include #include #include -#include +#include + using namespace std; -class Competitor { +class Competitor : public QObject { Q_OBJECT @@ -17,42 +18,35 @@ class Competitor { Q_PROPERTY(QString noc READ noc NOTIFY nNoc) public: - Competitor() { - this->code = 0; - this->name = ""; - this->noc = ""; - } + Competitor() : m_code(0), m_name(""), m_noc("") {} + Competitor(const Competitor &competitor) : m_code(competitor.m_code), m_name(competitor.m_name), m_noc(competitor.m_noc) {} + Competitor(const QJsonObject &competitor) { setCompetitor(competitor); } - Competitor(const Competitor &competitor) { - this->code = competitor.code; - this->name = competitor.name; - this->noc = competitor.noc; - } + int getCode() { return this->m_code; } + QString getName() { return this->m_name; } + QString getNOC() { return this->m_noc; } - Competitor(const QJsonObject &competitor) { - setCompetitor(competitor); - } - - int getCode() { return this->code; } - QString getName() { return this->name; } - QString getNOC() { return this->noc; } - - void setCode(int code) { this->code = code; } - void setName(QString name) { this->name = name; } - void setNOC(QString noc) { this->noc = noc; } + void setCode(int code) { this->m_code = code; } + void setName(QString name) { this->m_name = name; } + void setNOC(QString noc) { this->m_noc = noc; } bool setCompetitor(const QJsonObject &competitor); static bool compareName(const Competitor &left, const Competitor &right) { - return left.name.compare(right.name) < 0; + return left.m_name.compare(right.m_name) < 0; } static bool compareNOC(const Competitor &left, const Competitor &right) { - return left.noc.compare(right.noc) < 0; + return left.m_noc.compare(right.m_noc) < 0; } +signals: + void nCode(); + void nName(); + void nNoc(); + private: - int code; - QString name; - QString noc; + int m_code; + QString m_name; + QString m_noc; }; diff --git a/src/model/CompetitorWithResults.cpp b/src/model/CompetitorWithResults.cpp index 1601a4a..9dbd615 100644 --- a/src/model/CompetitorWithResults.cpp +++ b/src/model/CompetitorWithResults.cpp @@ -8,13 +8,13 @@ * @return True, if successful. */ bool CompetitorWithResults::setResults(const QJsonObject &results) { - if (!results.contains("mark") - || !results.contains("medalType")) { + if (!results.contains("m_mark") + || !results.contains("m_medalType")) { throw invalid_argument("Results object of competitor is incomplete."); } - this->mark = results["mark"].toString(); - this->medalType = results["medalType"].toString(); + this->m_mark = results["m_mark"].toString(); + this->m_medalType = results["m_medalType"].toString(); return true; } diff --git a/src/model/CompetitorWithResults.h b/src/model/CompetitorWithResults.h index a58023f..2203064 100644 --- a/src/model/CompetitorWithResults.h +++ b/src/model/CompetitorWithResults.h @@ -5,27 +5,21 @@ #include #include #include -#include +#include #include class CompetitorWithResults : public Competitor { - Q_OBJECT + 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) + 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: - CompetitorWithResults() : Competitor() { - this->mark = ""; - this->medalType = ""; - } - - CompetitorWithResults(const CompetitorWithResults &competitor) : Competitor(competitor) { - this->mark = competitor.mark; - this->medalType = competitor.medalType; - } + CompetitorWithResults() : Competitor(), m_mark(""), m_medalType("") {} + CompetitorWithResults(const CompetitorWithResults &competitor) : Competitor(competitor), + m_mark(competitor.m_mark), m_medalType(competitor.m_medalType) {} CompetitorWithResults(const QJsonObject &competitor) : Competitor(competitor) { if (!competitor.contains("results")) throw invalid_argument("Competitor does not contain results."); @@ -34,17 +28,17 @@ public: } bool setResults(const QJsonObject &results); - void setStatistic(QString stat) { this->statistic = stat; } + void setStatistic(QString stat) { this->m_statistic = stat; } - QString getMark() { return this->mark; } - QString getMedalType() { return this->medalType; } - QString getStatistic() { return this->statistic; } + 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 mark; - QString medalType; - QString statistic; + QString m_mark; + QString m_medalType; + QString m_statistic; }; diff --git a/src/model/MedalWinner.cpp b/src/model/MedalWinner.cpp index 4eca811..bdf6ee3 100644 --- a/src/model/MedalWinner.cpp +++ b/src/model/MedalWinner.cpp @@ -14,16 +14,16 @@ bool MedalWinner::setMedals(const QJsonObject &medals) { throw invalid_argument("Medal object of competitor is incomplete."); } - this->gold = medals["ME_GOLD"].toInt(); - this->silver = medals["ME_SILVER"].toInt(); - this->bronze = medals["ME_BRONZE"].toInt(); + this->m_gold = medals["ME_GOLD"].toInt(); + this->m_silver = medals["ME_SILVER"].toInt(); + this->m_bronze = 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 silver and finally bronze. + * Gold has the highest priority, then m_silver and finally m_bronze. * * @param lComp First competitor to compare. * @param rComp Second competitor to compare. diff --git a/src/model/MedalWinner.h b/src/model/MedalWinner.h index 7f17449..45d1ee8 100644 --- a/src/model/MedalWinner.h +++ b/src/model/MedalWinner.h @@ -9,25 +9,17 @@ #include class MedalWinner : public Competitor { - Q_OBJECT - Q_PROPERTY(int gold READ gold NOTIFY nGold) - Q_PROPERTY(int silver READ silver NOTIFY nSilver) - Q_PROPERTY(int bronze READ bronze NOTIFY nBronze) + Q_OBJECT -public: - MedalWinner() : Competitor() { - this->gold = 0; - this->silver = 0; - this->bronze = 0; - } - - MedalWinner(const MedalWinner &medalWinner) : Competitor(medalWinner) { - this->gold = medalWinner.gold; - this->silver = medalWinner.silver; - this->bronze = medalWinner.bronze; - } + 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: + MedalWinner() : Competitor(), m_gold(0), m_silver(0), m_bronze(0) {} + MedalWinner(const MedalWinner &medalWinner) : Competitor(medalWinner), + m_gold(medalWinner.m_gold), m_silver(medalWinner.m_silver), m_bronze(medalWinner.m_bronze) {} MedalWinner(const QJsonObject &competitor) : Competitor(competitor) { if (!competitor.contains("medals")) throw invalid_argument("Competitor has no medals."); QJsonObject medals = competitor["medals"].toObject(); @@ -35,13 +27,13 @@ public: } bool setMedals(const QJsonObject &medals); - int getGold() { return gold; } - int getSilver() { return silver; } - int getBronze() { return 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 gold, silver, bronze; + int m_gold, m_silver, m_bronze; }; diff --git a/src/model/Sport.cpp b/src/model/Sport.cpp index 4b822fa..4b341ed 100644 --- a/src/model/Sport.cpp +++ b/src/model/Sport.cpp @@ -213,7 +213,7 @@ QList Sport::getCompetitorsByCategory(QString category) { /** * @brief Sport::getCompetitorsWithMedal 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, noc, medals{ME_GOLD, ME_SILVER, ME_BRONZE}} + * @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 Sport::getCompetitorsWithMedal() { map competitors; @@ -239,7 +239,7 @@ QList Sport::getCompetitorsWithMedal() { QJsonArray medalComps = filter(unit["competitors"].toArray(), [](QJsonObject comp) { if (!comp.contains("results")) return false; - QString medalType = comp["results"].toObject()["medalType"].toString(); + QString medalType = comp["results"].toObject()["m_medalType"].toString(); return !medalType.isEmpty(); }); @@ -249,10 +249,10 @@ QList Sport::getCompetitorsWithMedal() { // validate competitor (with medal) if (!medalComp.contains("name") || !medalComp.contains("results") - || !medalComp["results"].toObject().contains("medalType")) continue; + || !medalComp["results"].toObject().contains("m_medalType")) continue; QString name = medalComp["name"].toString(); - QString medalType = medalComp["results"].toObject()["medalType"].toString(); + QString medalType = medalComp["results"].toObject()["m_medalType"].toString(); // check if competitor has other medal(s) if (competitors.find(name) == competitors.end()) { @@ -281,7 +281,7 @@ QList Sport::getCompetitorsWithMedal() { } /** - * @brief Sport::createCompetitorWithMedals Creates a competitor QJsonObject with the following attributes: code, name, noc, medals{ME_GOLD, ME_SILVER, ME_BRONZE} + * @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. */ @@ -289,7 +289,7 @@ QJsonObject Sport::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("noc")) comp.insert("code", ""); + if (!comp.contains("m_noc")) comp.insert("code", ""); // create new competitor QJsonObject and add it to the competitor map QJsonObject medals { @@ -301,7 +301,7 @@ QJsonObject Sport::createCompetitorWithMedals(QJsonObject comp) { QJsonObject medalComp { {"code", comp["code"].toString()}, {"name", comp["name"].toString()}, - {"noc", comp["noc"].toString()}, + {"m_noc", comp["m_noc"].toString()}, {"medals", medals} }; @@ -385,15 +385,17 @@ void Sport::reverseOrder(QList &competitors) { int iterations = competitors.size() / 2; // automatically rounds down for (int i = 0; i < iterations; i++) { - Competitor temp = competitors.value(i); - competitors.replace(i, competitors.value(competitors.size() - 1 - i)); - competitors.replace(competitors.size() - 1 - i, temp); + Competitor left = Competitor(competitors.value(i)); + Competitor right = Competitor(competitors.value(competitors.size() - 1 - i)); + + competitors.replace(i, right); + competitors.replace(competitors.size() - 1 - i, left); } } /** * @brief Sport::addRelativeToFirst Adds a relative value to the result of all competitors. Relative to the first competitor in the QJsonArray. - * Stores the statistic in obj->results->stat for each competitor. + * Stores the m_statistic in obj->results->stat for each competitor. * @param competitors The competitors of one category. */ void Sport::addRelativeToFirst(QList &competitors) { @@ -402,11 +404,11 @@ void Sport::addRelativeToFirst(QList &competitors) { QString reference = competitors.value(0).getMark(); for (CompetitorWithResults comp : competitors) { - QString results = comp.getMark(); + QString result = comp.getMark(); // format relative float value to string with 2 digits after decimal point and sign stringstream sstream; - sstream << fixed << setprecision(2) << calcRelativeStat(reference, results["mark"].toString()); + sstream << fixed << setprecision(2) << calcRelativeStat(reference, result); QString stat(sstream.str().c_str()); stat.append("%"); if (stat.at(0).isNumber()) stat = QString("+").append(stat); -- cgit v1.2.3