Made Competitor a QObject and tidied up some code.

This commit is contained in:
Steru 2024-08-15 23:27:54 +02:00 committed by Orangerot
parent db2d0c21c4
commit b6c86f02ff
7 changed files with 76 additions and 94 deletions

View file

@ -4,12 +4,12 @@
bool Competitor::setCompetitor(const QJsonObject &competitor) { bool Competitor::setCompetitor(const QJsonObject &competitor) {
if (!competitor.contains("code") if (!competitor.contains("code")
|| !competitor.contains("name") || !competitor.contains("name")
|| !competitor.contains("noc")) { || !competitor.contains("m_noc")) {
throw invalid_argument("Not a competitor object."); throw invalid_argument("Not a competitor object.");
} }
this->code = competitor["code"].toInt(); this->m_code = competitor["code"].toInt();
this->name = competitor["name"].toString(); this->m_name = competitor["name"].toString();
this->noc = competitor["noc"].toString(); this->m_noc = competitor["m_noc"].toString();
return true; return true;
} }

View file

@ -4,11 +4,12 @@
#include <QString> #include <QString>
#include <QMap> #include <QMap>
#include <QJsonObject> #include <QJsonObject>
#include <QAbstractListModel> #include <QObject>
using namespace std; using namespace std;
class Competitor { class Competitor : public QObject {
Q_OBJECT Q_OBJECT
@ -17,42 +18,35 @@ class Competitor {
Q_PROPERTY(QString noc READ noc NOTIFY nNoc) Q_PROPERTY(QString noc READ noc NOTIFY nNoc)
public: public:
Competitor() { Competitor() : m_code(0), m_name(""), m_noc("") {}
this->code = 0; Competitor(const Competitor &competitor) : m_code(competitor.m_code), m_name(competitor.m_name), m_noc(competitor.m_noc) {}
this->name = ""; Competitor(const QJsonObject &competitor) { setCompetitor(competitor); }
this->noc = "";
}
Competitor(const Competitor &competitor) { int getCode() { return this->m_code; }
this->code = competitor.code; QString getName() { return this->m_name; }
this->name = competitor.name; QString getNOC() { return this->m_noc; }
this->noc = competitor.noc;
}
Competitor(const QJsonObject &competitor) { void setCode(int code) { this->m_code = code; }
setCompetitor(competitor); void setName(QString name) { this->m_name = name; }
} void setNOC(QString noc) { this->m_noc = noc; }
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; }
bool setCompetitor(const QJsonObject &competitor); bool setCompetitor(const QJsonObject &competitor);
static bool compareName(const Competitor &left, const Competitor &right) { 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) { 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: private:
int code; int m_code;
QString name; QString m_name;
QString noc; QString m_noc;
}; };

View file

@ -8,13 +8,13 @@
* @return True, if successful. * @return True, if successful.
*/ */
bool CompetitorWithResults::setResults(const QJsonObject &results) { bool CompetitorWithResults::setResults(const QJsonObject &results) {
if (!results.contains("mark") if (!results.contains("m_mark")
|| !results.contains("medalType")) { || !results.contains("m_medalType")) {
throw invalid_argument("Results object of competitor is incomplete."); throw invalid_argument("Results object of competitor is incomplete.");
} }
this->mark = results["mark"].toString(); this->m_mark = results["m_mark"].toString();
this->medalType = results["medalType"].toString(); this->m_medalType = results["m_medalType"].toString();
return true; return true;
} }

View file

@ -5,7 +5,7 @@
#include <QString> #include <QString>
#include <QMap> #include <QMap>
#include <QJsonObject> #include <QJsonObject>
#include <QAbstractListModel> #include <QObject>
#include <stdexcept> #include <stdexcept>
class CompetitorWithResults : public Competitor { class CompetitorWithResults : public Competitor {
@ -17,15 +17,9 @@ class CompetitorWithResults : public Competitor {
Q_PROPERTY(QString statistic READ statistic NOTIFY nStatistic) Q_PROPERTY(QString statistic READ statistic NOTIFY nStatistic)
public: public:
CompetitorWithResults() : Competitor() { CompetitorWithResults() : Competitor(), m_mark(""), m_medalType("") {}
this->mark = ""; CompetitorWithResults(const CompetitorWithResults &competitor) : Competitor(competitor),
this->medalType = ""; m_mark(competitor.m_mark), m_medalType(competitor.m_medalType) {}
}
CompetitorWithResults(const CompetitorWithResults &competitor) : Competitor(competitor) {
this->mark = competitor.mark;
this->medalType = competitor.medalType;
}
CompetitorWithResults(const QJsonObject &competitor) : Competitor(competitor) { CompetitorWithResults(const QJsonObject &competitor) : Competitor(competitor) {
if (!competitor.contains("results")) throw invalid_argument("Competitor does not contain results."); if (!competitor.contains("results")) throw invalid_argument("Competitor does not contain results.");
@ -34,17 +28,17 @@ public:
} }
bool setResults(const QJsonObject &results); 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 getMark() { return this->m_mark; }
QString getMedalType() { return this->medalType; } QString getMedalType() { return this->m_medalType; }
QString getStatistic() { return this->statistic; } QString getStatistic() { return this->m_statistic; }
static bool compare(CompetitorWithResults lComp, CompetitorWithResults rComp); static bool compare(CompetitorWithResults lComp, CompetitorWithResults rComp);
private: private:
QString mark; QString m_mark;
QString medalType; QString m_medalType;
QString statistic; QString m_statistic;
}; };

View file

@ -14,16 +14,16 @@ bool MedalWinner::setMedals(const QJsonObject &medals) {
throw invalid_argument("Medal object of competitor is incomplete."); throw invalid_argument("Medal object of competitor is incomplete.");
} }
this->gold = medals["ME_GOLD"].toInt(); this->m_gold = medals["ME_GOLD"].toInt();
this->silver = medals["ME_SILVER"].toInt(); this->m_silver = medals["ME_SILVER"].toInt();
this->bronze = medals["ME_BRONZE"].toInt(); this->m_bronze = medals["ME_BRONZE"].toInt();
return true; return true;
} }
/** /**
* Static compare method, which can compare the amount of medals of two MedalWinners. * 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 lComp First competitor to compare.
* @param rComp Second competitor to compare. * @param rComp Second competitor to compare.

View file

@ -9,25 +9,17 @@
#include <QAbstractListModel> #include <QAbstractListModel>
class MedalWinner : public Competitor { class MedalWinner : public Competitor {
Q_OBJECT Q_OBJECT
Q_PROPERTY(int gold READ gold NOTIFY nGold) Q_PROPERTY(int gold READ m_gold NOTIFY nGold)
Q_PROPERTY(int silver READ silver NOTIFY nSilver) Q_PROPERTY(int silver READ m_silver NOTIFY nSilver)
Q_PROPERTY(int bronze READ bronze NOTIFY nBronze) Q_PROPERTY(int bronze READ m_bronze NOTIFY nBronze)
public: public:
MedalWinner() : Competitor() { MedalWinner() : Competitor(), m_gold(0), m_silver(0), m_bronze(0) {}
this->gold = 0; MedalWinner(const MedalWinner &medalWinner) : Competitor(medalWinner),
this->silver = 0; m_gold(medalWinner.m_gold), m_silver(medalWinner.m_silver), m_bronze(medalWinner.m_bronze) {}
this->bronze = 0;
}
MedalWinner(const MedalWinner &medalWinner) : Competitor(medalWinner) {
this->gold = medalWinner.gold;
this->silver = medalWinner.silver;
this->bronze = medalWinner.bronze;
}
MedalWinner(const QJsonObject &competitor) : Competitor(competitor) { MedalWinner(const QJsonObject &competitor) : Competitor(competitor) {
if (!competitor.contains("medals")) throw invalid_argument("Competitor has no medals."); if (!competitor.contains("medals")) throw invalid_argument("Competitor has no medals.");
QJsonObject medals = competitor["medals"].toObject(); QJsonObject medals = competitor["medals"].toObject();
@ -35,13 +27,13 @@ public:
} }
bool setMedals(const QJsonObject &medals); bool setMedals(const QJsonObject &medals);
int getGold() { return gold; } int getGold() { return m_gold; }
int getSilver() { return silver; } int getSilver() { return m_silver; }
int getBronze() { return bronze; } int getBronze() { return m_bronze; }
static bool compare(MedalWinner lComp, MedalWinner rComp); static bool compare(MedalWinner lComp, MedalWinner rComp);
private: private:
int gold, silver, bronze; int m_gold, m_silver, m_bronze;
}; };

View file

@ -213,7 +213,7 @@ QList<CompetitorWithResults> Sport::getCompetitorsByCategory(QString category) {
/** /**
* @brief Sport::getCompetitorsWithMedal Filters all competitors, who have at least one medal. These objects are different from getCompetitorsByCategory !!! * @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<MedalWinner> Sport::getCompetitorsWithMedal() { QList<MedalWinner> Sport::getCompetitorsWithMedal() {
map<QString, QJsonObject> competitors; map<QString, QJsonObject> competitors;
@ -239,7 +239,7 @@ QList<MedalWinner> Sport::getCompetitorsWithMedal() {
QJsonArray medalComps = filter(unit["competitors"].toArray(), [](QJsonObject comp) { QJsonArray medalComps = filter(unit["competitors"].toArray(), [](QJsonObject comp) {
if (!comp.contains("results")) return false; if (!comp.contains("results")) return false;
QString medalType = comp["results"].toObject()["medalType"].toString(); QString medalType = comp["results"].toObject()["m_medalType"].toString();
return !medalType.isEmpty(); return !medalType.isEmpty();
}); });
@ -249,10 +249,10 @@ QList<MedalWinner> Sport::getCompetitorsWithMedal() {
// validate competitor (with medal) // validate competitor (with medal)
if (!medalComp.contains("name") if (!medalComp.contains("name")
|| !medalComp.contains("results") || !medalComp.contains("results")
|| !medalComp["results"].toObject().contains("medalType")) continue; || !medalComp["results"].toObject().contains("m_medalType")) continue;
QString name = medalComp["name"].toString(); 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) // check if competitor has other medal(s)
if (competitors.find(name) == competitors.end()) { if (competitors.find(name) == competitors.end()) {
@ -281,7 +281,7 @@ QList<MedalWinner> 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. * @param comp The original competitor object.
* @return A competitor object with medal counts. * @return A competitor object with medal counts.
*/ */
@ -289,7 +289,7 @@ QJsonObject Sport::createCompetitorWithMedals(QJsonObject comp) {
// repair competitor if something is missing // repair competitor if something is missing
if (!comp.contains("code")) comp.insert("code", "0"); if (!comp.contains("code")) comp.insert("code", "0");
if (!comp.contains("name")) comp.insert("code", ""); 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 // create new competitor QJsonObject and add it to the competitor map
QJsonObject medals { QJsonObject medals {
@ -301,7 +301,7 @@ QJsonObject Sport::createCompetitorWithMedals(QJsonObject comp) {
QJsonObject medalComp { QJsonObject medalComp {
{"code", comp["code"].toString()}, {"code", comp["code"].toString()},
{"name", comp["name"].toString()}, {"name", comp["name"].toString()},
{"noc", comp["noc"].toString()}, {"m_noc", comp["m_noc"].toString()},
{"medals", medals} {"medals", medals}
}; };
@ -385,15 +385,17 @@ void Sport::reverseOrder(QList<Competitor> &competitors) {
int iterations = competitors.size() / 2; // automatically rounds down int iterations = competitors.size() / 2; // automatically rounds down
for (int i = 0; i < iterations; i++) { for (int i = 0; i < iterations; i++) {
Competitor temp = competitors.value(i); Competitor left = Competitor(competitors.value(i));
competitors.replace(i, competitors.value(competitors.size() - 1 - i)); Competitor right = Competitor(competitors.value(competitors.size() - 1 - i));
competitors.replace(competitors.size() - 1 - i, temp);
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. * @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. * @param competitors The competitors of one category.
*/ */
void Sport::addRelativeToFirst(QList<CompetitorWithResults> &competitors) { void Sport::addRelativeToFirst(QList<CompetitorWithResults> &competitors) {
@ -402,11 +404,11 @@ void Sport::addRelativeToFirst(QList<CompetitorWithResults> &competitors) {
QString reference = competitors.value(0).getMark(); QString reference = competitors.value(0).getMark();
for (CompetitorWithResults comp : competitors) { 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 // format relative float value to string with 2 digits after decimal point and sign
stringstream sstream; stringstream sstream;
sstream << fixed << setprecision(2) << calcRelativeStat(reference, results["mark"].toString()); sstream << fixed << setprecision(2) << calcRelativeStat(reference, result);
QString stat(sstream.str().c_str()); QString stat(sstream.str().c_str());
stat.append("%"); stat.append("%");
if (stat.at(0).isNumber()) stat = QString("+").append(stat); if (stat.at(0).isNumber()) stat = QString("+").append(stat);