Made Competitor a QObject and tidied up some code.
This commit is contained in:
parent
b49c066bd5
commit
66d5d8685f
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -4,11 +4,12 @@
|
|||
#include <QString>
|
||||
#include <QMap>
|
||||
#include <QJsonObject>
|
||||
#include <QAbstractListModel>
|
||||
#include <QObject>
|
||||
|
||||
|
||||
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;
|
||||
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#include <QString>
|
||||
#include <QMap>
|
||||
#include <QJsonObject>
|
||||
#include <QAbstractListModel>
|
||||
#include <QObject>
|
||||
#include <stdexcept>
|
||||
|
||||
class CompetitorWithResults : public Competitor {
|
||||
|
@ -17,15 +17,9 @@ class CompetitorWithResults : public Competitor {
|
|||
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;
|
||||
|
||||
};
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -9,25 +9,17 @@
|
|||
#include <QAbstractListModel>
|
||||
|
||||
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_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() {
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
};
|
||||
|
|
|
@ -212,7 +212,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 !!!
|
||||
* @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() {
|
||||
map<QString, QJsonObject> competitors;
|
||||
|
@ -238,7 +238,7 @@ QList<MedalWinner> 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();
|
||||
});
|
||||
|
||||
|
@ -248,10 +248,10 @@ QList<MedalWinner> 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()) {
|
||||
|
@ -280,7 +280,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.
|
||||
* @return A competitor object with medal counts.
|
||||
*/
|
||||
|
@ -288,7 +288,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 {
|
||||
|
@ -300,7 +300,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}
|
||||
};
|
||||
|
||||
|
@ -384,15 +384,17 @@ void Sport::reverseOrder(QList<Competitor> &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<CompetitorWithResults> &competitors) {
|
||||
|
@ -401,11 +403,11 @@ void Sport::addRelativeToFirst(QList<CompetitorWithResults> &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);
|
||||
|
|
Loading…
Reference in a new issue