summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteru <jerrydream111@gmail.com>2024-08-15 19:21:02 +0200
committerSteru <jerrydream111@gmail.com>2024-08-16 23:50:34 +0200
commit70d5f014e50b9255dcb5c36122a109cc049c8936 (patch)
treef14ab291bec1a27457166ab1c4e0e70920b2ffa9
parent173ce00cf29822294137d3ebacc5b33ad01a3d25 (diff)
Added Q Object macros.
-rw-r--r--src/model/Competitor.cpp2
-rw-r--r--src/model/Competitor.h17
-rw-r--r--src/model/CompetitorWithResults.cpp39
-rw-r--r--src/model/CompetitorWithResults.h24
-rw-r--r--src/model/MedalWinner.cpp32
-rw-r--r--src/model/MedalWinner.h30
6 files changed, 124 insertions, 20 deletions
diff --git a/src/model/Competitor.cpp b/src/model/Competitor.cpp
index a5d2e69..d39d134 100644
--- a/src/model/Competitor.cpp
+++ b/src/model/Competitor.cpp
@@ -8,7 +8,7 @@ bool Competitor::setCompetitor(const QJsonObject &competitor) {
throw invalid_argument("Not a competitor object.");
}
- this->code = competitor["code"].toString();
+ this->code = competitor["code"].toInt();
this->name = competitor["name"].toString();
this->noc = competitor["noc"].toString();
return true;
diff --git a/src/model/Competitor.h b/src/model/Competitor.h
index 919c2fa..1121a5b 100644
--- a/src/model/Competitor.h
+++ b/src/model/Competitor.h
@@ -5,13 +5,26 @@
#include <QString>
#include <QMap>
#include <QJsonObject>
+#include <QAbstractListModel>
#include <stdexcept>
using namespace std;
class Competitor {
+ Q_OBJECT
+
+ Q_PROPERTY(int code READ code)
+ Q_PROPERTY(QString name READ name)
+ Q_PROPERTY(QString noc READ noc)
+
public:
+ Competitor() {
+ this->code = 0;
+ this->name = "na";
+ this->noc = "---";
+ }
+
Competitor(const Competitor &competitor) {
this->code = competitor.code;
this->name = competitor.name;
@@ -22,14 +35,14 @@ public:
setCompetitor(competitor);
}
- QString getCode() { return this->code; }
+ int getCode() { return this->code; }
QString getName() { return this->name; }
QString getNOC() { return this->noc; }
bool setCompetitor(const QJsonObject &competitor);
private:
- QString code;
+ int code;
QString name;
QString noc;
diff --git a/src/model/CompetitorWithResults.cpp b/src/model/CompetitorWithResults.cpp
index d29441d..1601a4a 100644
--- a/src/model/CompetitorWithResults.cpp
+++ b/src/model/CompetitorWithResults.cpp
@@ -1,15 +1,46 @@
#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("mark")
|| !results.contains("medalType")) {
throw invalid_argument("Results object of competitor is incomplete.");
}
- this->results = {
- {QString("mark"), results["mark"].toString()},
- {QString("medalType"), results["medalType"].toString()}
- };
+ this->mark = results["mark"].toString();
+ this->medalType = results["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;
+}
diff --git a/src/model/CompetitorWithResults.h b/src/model/CompetitorWithResults.h
index cb952f2..a313db2 100644
--- a/src/model/CompetitorWithResults.h
+++ b/src/model/CompetitorWithResults.h
@@ -6,11 +6,27 @@
#include <QString>
#include <QMap>
#include <QJsonObject>
+#include <QAbstractListModel>
#include <stdexcept>
class CompetitorWithResults : public Competitor {
+ Q_OBJECT
+
+ Q_PROPERTY(QString mark READ mark)
+ Q_PROPERTY(QString medalType READ medalType)
+
public:
+ CompetitorWithResults() : Competitor() {
+ this->mark = "-";
+ this->medalType = "-";
+ }
+
+ CompetitorWithResults(const CompetitorWithResults &competitor) : Competitor(competitor) {
+ this->mark = competitor.mark;
+ this->medalType = competitor.medalType;
+ }
+
CompetitorWithResults(const QJsonObject &competitor) : Competitor(competitor) {
if (!competitor.contains("results")) throw invalid_argument("Competitor does not contain results.");
QJsonObject results = competitor["results"].toObject();
@@ -19,8 +35,14 @@ public:
bool setResults(const QJsonObject &results);
+ QString getMark() { return this->mark; }
+ QString getMedalType() { return this->medalType; }
+
+ static bool compare(CompetitorWithResults lComp, CompetitorWithResults rComp);
+
private:
- QMap<QString, QString> results;
+ QString mark;
+ QString medalType;
};
diff --git a/src/model/MedalWinner.cpp b/src/model/MedalWinner.cpp
index c4cb663..4eca811 100644
--- a/src/model/MedalWinner.cpp
+++ b/src/model/MedalWinner.cpp
@@ -1,6 +1,12 @@
#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")
@@ -8,11 +14,27 @@ bool MedalWinner::setMedals(const QJsonObject &medals) {
throw invalid_argument("Medal object of competitor is incomplete.");
}
- this->wonMedals = {
- {QString("ME_GOLD"), medals["ME_GOLD"].toString()},
- {QString("ME_SILVER"), medals["ME_SILVER"].toString()},
- {QString("ME_BRONZE"), medals["ME_BRONZE"].toString()}
- };
+ this->gold = medals["ME_GOLD"].toInt();
+ this->silver = medals["ME_SILVER"].toInt();
+ this->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.
+ *
+ * @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)));
+}
diff --git a/src/model/MedalWinner.h b/src/model/MedalWinner.h
index e771185..99ca1a3 100644
--- a/src/model/MedalWinner.h
+++ b/src/model/MedalWinner.h
@@ -7,27 +7,43 @@
#include <QJsonObject>
#include <stdexcept>
+#include <QAbstractListModel>
+
class MedalWinner : public Competitor {
+ Q_OBJECT
+
+ Q_PROPERTY(int gold READ gold)
+ Q_PROPERTY(int silver READ silver)
+ Q_PROPERTY(int bronze READ bronze)
public:
+ MedalWinner() : Competitor() {
+ this->gold = 0;
+ this->silver = 0;
+ this->bronze = 0;
+ }
+
MedalWinner(const MedalWinner &medalWinner) : Competitor(medalWinner) {
- this->wonMedals = {
- {QString("ME_GOLD"), medalWinner.wonMedals.value("ME_GOLD")},
- {QString("ME_SILVER"), medalWinner.wonMedals.value("ME_SILVER")},
- {QString("ME_BRONZE"), medalWinner.wonMedals.value("ME_BRONZE")}
- };
+ this->gold = medalWinner.gold;
+ this->silver = medalWinner.silver;
+ this->bronze = medalWinner.bronze;
}
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();
setMedals(medals);
}
bool setMedals(const QJsonObject &medals);
+ int getGold() { return gold; }
+ int getSilver() { return silver; }
+ int getBronze() { return bronze; }
+
+ static bool compare(MedalWinner lComp, MedalWinner rComp);
private:
- QMap<QString, QString> wonMedals;
+ int gold, silver, bronze;
};