Integrated Sport class into SportModel class.
This commit is contained in:
parent
884280076a
commit
b0063e9641
|
@ -29,8 +29,8 @@ qt_add_qml_module(itat_challange_olympics
|
||||||
src/model/EventInfo.h
|
src/model/EventInfo.h
|
||||||
src/model/MedalWinner.cpp
|
src/model/MedalWinner.cpp
|
||||||
src/model/MedalWinner.h
|
src/model/MedalWinner.h
|
||||||
src/model/Sport.cpp
|
src/model/SportModel.cpp
|
||||||
src/model/Sport.h
|
src/model/SportModel.h
|
||||||
|
|
||||||
RESOURCES
|
RESOURCES
|
||||||
res/pictograms/ARC_small.svg
|
res/pictograms/ARC_small.svg
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
// console output
|
// console output
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
// #include <iostream>
|
// #include <iostream>
|
||||||
#include "../model/Sport.h"
|
#include "../model/SportModel.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,124 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "MedalWinner.h"
|
|
||||||
#include "CompetitorWithResults.h"
|
|
||||||
#include <QAbstractListModel>
|
|
||||||
#include <QNetworkAccessManager>
|
|
||||||
#include <qcontainerfwd.h>
|
|
||||||
#include <set>
|
|
||||||
|
|
||||||
#include <QJsonObject>
|
|
||||||
#include <QJsonDocument>
|
|
||||||
#include <QString>
|
|
||||||
#include <QList>
|
|
||||||
#include "EventInfo.h"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
class SportModel : public QAbstractListModel
|
|
||||||
{
|
|
||||||
Q_OBJECT
|
|
||||||
|
|
||||||
Q_PROPERTY(QString discipline READ discipline WRITE setDiscipline);
|
|
||||||
|
|
||||||
public:
|
|
||||||
enum Role
|
|
||||||
{
|
|
||||||
EventName = Qt::UserRole + 1,
|
|
||||||
Competitors
|
|
||||||
};
|
|
||||||
|
|
||||||
explicit SportModel(QObject *parent = nullptr);
|
|
||||||
|
|
||||||
virtual int rowCount(const QModelIndex &parent) const override;
|
|
||||||
virtual QVariant data(const QModelIndex &index, int role) const override;
|
|
||||||
virtual QHash<int, QByteArray> roleNames() const override;
|
|
||||||
|
|
||||||
QString discipline() const;
|
|
||||||
void setDiscipline(const QString &discipline);
|
|
||||||
public slots:
|
|
||||||
void request(QString discipline);
|
|
||||||
void parseData();
|
|
||||||
|
|
||||||
private:
|
|
||||||
QList<EventInfo *> m_sportList;
|
|
||||||
QString m_discipline;
|
|
||||||
QNetworkAccessManager m_networkManager;
|
|
||||||
QNetworkReply *m_reply = nullptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Sport
|
|
||||||
{
|
|
||||||
|
|
||||||
public:
|
|
||||||
Sport(QJsonObject discipline)
|
|
||||||
{
|
|
||||||
this->discipline = discipline;
|
|
||||||
}
|
|
||||||
|
|
||||||
set<QString> getCategories();
|
|
||||||
QList<CompetitorWithResults> getCompetitorsByCategory(QString category);
|
|
||||||
QList<MedalWinner> getCompetitorsWithMedal();
|
|
||||||
|
|
||||||
// filter to change the current competitor array
|
|
||||||
void lastName(QList<Competitor> &competitors);
|
|
||||||
void filterByName(QList<Competitor> &competitors, QString name);
|
|
||||||
void filterByCountry(QList<Competitor> &competitors, QString nocShort);
|
|
||||||
|
|
||||||
// sort functions to change the order of the current competitor array
|
|
||||||
void sortByName(QList<Competitor> &competitors);
|
|
||||||
void sortByCountry(QList<Competitor> &competitors);
|
|
||||||
void sortByResult(QList<CompetitorWithResults> &competitors);
|
|
||||||
void sortByMedals(QList<MedalWinner> &competitors);
|
|
||||||
void reverseOrder(QList<Competitor> &competitors);
|
|
||||||
|
|
||||||
// statistic function(s)
|
|
||||||
void addRelativeToFirst(QList<CompetitorWithResults> &competitors);
|
|
||||||
|
|
||||||
void setDiscipline(QJsonObject discipline)
|
|
||||||
{
|
|
||||||
this->discipline = QJsonObject(discipline);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
/*
|
|
||||||
* Analysis of provided competitor objects:
|
|
||||||
*
|
|
||||||
* Attributes:
|
|
||||||
* - code
|
|
||||||
* - noc (national olympics comittee)
|
|
||||||
* - name (sometimes the country name? mostly the competitors name)
|
|
||||||
* - order
|
|
||||||
* [- results] (only if the results are out!)
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* Analysis of provided result objects:
|
|
||||||
*
|
|
||||||
* Attributes:
|
|
||||||
* - position
|
|
||||||
* - mark
|
|
||||||
* - medalType
|
|
||||||
* - irk
|
|
||||||
* [- winnerLoserTie] (only if provided in the discipline?)
|
|
||||||
*
|
|
||||||
* Analysis of where to find the medal winners:
|
|
||||||
*
|
|
||||||
* Search for ... in category name.
|
|
||||||
* - "Bronze"
|
|
||||||
* - "Gold"
|
|
||||||
* - "Final"
|
|
||||||
*
|
|
||||||
* ! ATTENTION !
|
|
||||||
* When searching for "Final" there might be "Final A", "Final B", etc.
|
|
||||||
* The results will only be in ONE of these categories!
|
|
||||||
* -> which is good... cause then we can count occurences.
|
|
||||||
*/
|
|
||||||
QJsonObject discipline;
|
|
||||||
|
|
||||||
void filterCompetitors(QList<Competitor> &competitors, QString filter);
|
|
||||||
|
|
||||||
bool validateDiscipline();
|
|
||||||
QJsonObject createCompetitorWithMedals(QJsonObject medalComp);
|
|
||||||
|
|
||||||
float calcRelativeStat(QString ref, QString val);
|
|
||||||
};
|
|
|
@ -1,4 +1,4 @@
|
||||||
#include "Sport.h"
|
#include "SportModel.h"
|
||||||
#include "Competitor.h"
|
#include "Competitor.h"
|
||||||
|
|
||||||
// categories
|
// categories
|
||||||
|
@ -127,13 +127,13 @@ QJsonArray filter(QJsonArray input, function<bool (QJsonObject)> eval) {
|
||||||
* @brief Sport::lastName Reduce the full name to the part that is marked in capital letters (probably last name).
|
* @brief Sport::lastName Reduce the full name to the part that is marked in capital letters (probably last name).
|
||||||
* @param competitors The competitors of one category.
|
* @param competitors The competitors of one category.
|
||||||
*/
|
*/
|
||||||
void Sport::lastName(QList<Competitor> &competitors) {
|
void SportModel::lastName(QList<Competitor*> &competitors) {
|
||||||
// validate competitors
|
// validate competitors
|
||||||
if (competitors.isEmpty()) return;
|
if (competitors.isEmpty()) return;
|
||||||
|
|
||||||
for (int i = 0; i < competitors.size(); i++) {
|
for (int i = 0; i < competitors.size(); i++) {
|
||||||
Competitor comp = competitors.value(i);
|
Competitor* comp = competitors.value(i);
|
||||||
string fullName = comp.getName().toUtf8().constData();
|
string fullName = comp->getName().toUtf8().constData();
|
||||||
|
|
||||||
// regex to identify names, written in CAPS
|
// regex to identify names, written in CAPS
|
||||||
regex r("[A-Z']{2,}");
|
regex r("[A-Z']{2,}");
|
||||||
|
@ -149,7 +149,7 @@ void Sport::lastName(QList<Competitor> &competitors) {
|
||||||
QString name = QString(lastName.substr(0, lastName.size() - 1).c_str());
|
QString name = QString(lastName.substr(0, lastName.size() - 1).c_str());
|
||||||
|
|
||||||
// replace competitor name in list
|
// replace competitor name in list
|
||||||
comp.setName(name);
|
comp->setName(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,21 +157,21 @@ void Sport::lastName(QList<Competitor> &competitors) {
|
||||||
* @brief Sport::validateDiscipline Validates the discipline object. Checks for the units attribute.
|
* @brief Sport::validateDiscipline Validates the discipline object. Checks for the units attribute.
|
||||||
* @return True, if discipline contains units.
|
* @return True, if discipline contains units.
|
||||||
*/
|
*/
|
||||||
bool Sport::validateDiscipline() {
|
bool SportModel::validateDiscipline() {
|
||||||
return this->discipline.contains("units");
|
return this->o_discipline.contains("units");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Sport::getCategories Reads all possible categories (also called units).
|
* @brief Sport::getCategories Reads all possible categories (also called units).
|
||||||
* @return A set of all category names.
|
* @return A set of all category names.
|
||||||
*/
|
*/
|
||||||
set<QString> Sport::getCategories() {
|
set<QString> SportModel::getCategories() {
|
||||||
set<QString> categoryNames;
|
set<QString> categoryNames;
|
||||||
|
|
||||||
if (!validateDiscipline()) return categoryNames;
|
if (!validateDiscipline()) return categoryNames;
|
||||||
|
|
||||||
// search in each unit for the category (named "eventUnitName")
|
// search in each unit for the category (named "eventUnitName")
|
||||||
for (const QJsonValueRef &unitRef : this->discipline["units"].toArray()) {
|
for (const QJsonValueRef &unitRef : this->o_discipline["units"].toArray()) {
|
||||||
QJsonObject unit = unitRef.toObject();
|
QJsonObject unit = unitRef.toObject();
|
||||||
|
|
||||||
// validate unit
|
// validate unit
|
||||||
|
@ -188,12 +188,12 @@ set<QString> Sport::getCategories() {
|
||||||
* @param category The category to search in.
|
* @param category The category to search in.
|
||||||
* @return An QJsonArray with all competitors as QJsonValueRef, which can be casted to QJsonObject.
|
* @return An QJsonArray with all competitors as QJsonValueRef, which can be casted to QJsonObject.
|
||||||
*/
|
*/
|
||||||
QList<CompetitorWithResults> Sport::getCompetitorsByCategory(QString category) {
|
QList<CompetitorWithResults*> SportModel::getCompetitorsByCategory(QString category) {
|
||||||
QList<CompetitorWithResults> competitors;
|
QList<CompetitorWithResults*> competitors;
|
||||||
|
|
||||||
if (!validateDiscipline()) return competitors;
|
if (!validateDiscipline()) return competitors;
|
||||||
|
|
||||||
for (const QJsonValueRef &unitRef : this->discipline["units"].toArray()) {
|
for (const QJsonValueRef &unitRef : this->o_discipline["units"].toArray()) {
|
||||||
QJsonObject unit = unitRef.toObject();
|
QJsonObject unit = unitRef.toObject();
|
||||||
|
|
||||||
// validate unit
|
// validate unit
|
||||||
|
@ -204,8 +204,8 @@ QList<CompetitorWithResults> Sport::getCompetitorsByCategory(QString category) {
|
||||||
|
|
||||||
// add all competitors from one unit
|
// add all competitors from one unit
|
||||||
for (const QJsonValueRef &compRef : unit["competitors"].toArray()) {
|
for (const QJsonValueRef &compRef : unit["competitors"].toArray()) {
|
||||||
CompetitorWithResults comp = new CompetitorWithResults(); // TODO declare comp
|
CompetitorWithResults *comp = new CompetitorWithResults(); // TODO declare comp
|
||||||
comp.setCompetitorWithResults(compRef.toObject());
|
comp->setCompetitorWithResults(compRef.toObject());
|
||||||
competitors.push_back(comp);
|
competitors.push_back(comp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -217,13 +217,13 @@ 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, m_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*> SportModel::getCompetitorsWithMedal() {
|
||||||
map<QString, QJsonObject> competitors;
|
map<QString, QJsonObject> competitors;
|
||||||
|
|
||||||
if (!validateDiscipline()) return QList<MedalWinner>();
|
if (!validateDiscipline()) return QList<MedalWinner*>();
|
||||||
|
|
||||||
// filter all units, which have medal events
|
// filter all units, which have medal events
|
||||||
QJsonArray units = filter(this->discipline["units"].toArray(), [](QJsonObject unit){
|
QJsonArray units = filter(this->o_discipline["units"].toArray(), [](QJsonObject unit){
|
||||||
// search all units with Final, Gold or Bronze in their name, because these are the categories with the medal winners
|
// search all units with Final, Gold or Bronze in their name, because these are the categories with the medal winners
|
||||||
QString unitName = unit["eventUnitName"].toString();
|
QString unitName = unit["eventUnitName"].toString();
|
||||||
return unitName.contains("Bronze", Qt::CaseSensitive)
|
return unitName.contains("Bronze", Qt::CaseSensitive)
|
||||||
|
@ -274,10 +274,10 @@ QList<MedalWinner> Sport::getCompetitorsWithMedal() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert map to QJsonArray
|
// convert map to QJsonArray
|
||||||
QList<MedalWinner> output;
|
QList<MedalWinner*> output;
|
||||||
for (const pair<QString, QJsonObject> &competitor : competitors) {
|
for (const pair<QString, QJsonObject> &competitor : competitors) {
|
||||||
MedalWinner comp = new MedalWinner(); // TODO declare comp
|
MedalWinner *comp = new MedalWinner(); // TODO declare comp
|
||||||
comp.setMedalWinner(competitor.second);
|
comp->setMedalWinner(competitor.second);
|
||||||
output.append(comp);
|
output.append(comp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -289,7 +289,7 @@ QList<MedalWinner> Sport::getCompetitorsWithMedal() {
|
||||||
* @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.
|
||||||
*/
|
*/
|
||||||
QJsonObject Sport::createCompetitorWithMedals(QJsonObject comp) {
|
QJsonObject SportModel::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", "");
|
||||||
|
@ -317,7 +317,7 @@ QJsonObject Sport::createCompetitorWithMedals(QJsonObject comp) {
|
||||||
* @param competitors The competitors of one category.
|
* @param competitors The competitors of one category.
|
||||||
* @param name The (part of the) name to search for.
|
* @param name The (part of the) name to search for.
|
||||||
*/
|
*/
|
||||||
void Sport::filterByName(QList<Competitor> &competitors, QString name) {
|
void SportModel::filterByName(QList<Competitor*> &competitors, QString name) {
|
||||||
filterCompetitors(competitors, name);
|
filterCompetitors(competitors, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -326,7 +326,7 @@ void Sport::filterByName(QList<Competitor> &competitors, QString name) {
|
||||||
* @param competitors The competitors of one category.
|
* @param competitors The competitors of one category.
|
||||||
* @param nocShort The (part of the) national olympics comittee short name to search for.
|
* @param nocShort The (part of the) national olympics comittee short name to search for.
|
||||||
*/
|
*/
|
||||||
void Sport::filterByCountry(QList<Competitor> &competitors, QString nocShort) {
|
void SportModel::filterByCountry(QList<Competitor*> &competitors, QString nocShort) {
|
||||||
filterCompetitors(competitors, nocShort);
|
filterCompetitors(competitors, nocShort);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -336,9 +336,9 @@ void Sport::filterByCountry(QList<Competitor> &competitors, QString nocShort) {
|
||||||
* @param attribute The attribute to filter by.
|
* @param attribute The attribute to filter by.
|
||||||
* @param filter The string, which should be contained.
|
* @param filter The string, which should be contained.
|
||||||
*/
|
*/
|
||||||
void Sport::filterCompetitors(QList<Competitor> &competitors, QString filter) {
|
void SportModel::filterCompetitors(QList<Competitor*> &competitors, QString filter) {
|
||||||
for (int i = 0; i < competitors.size(); i++) {
|
for (int i = 0; i < competitors.size(); i++) {
|
||||||
if (!competitors.value(i).getNOC().contains(filter)) {
|
if (!competitors.value(i)->getNOC().contains(filter)) {
|
||||||
competitors.remove(i);
|
competitors.remove(i);
|
||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
|
@ -349,48 +349,48 @@ void Sport::filterCompetitors(QList<Competitor> &competitors, QString filter) {
|
||||||
* @brief Sport::sortByName Sort the competitors by their name (alphabetical, ascending).
|
* @brief Sport::sortByName Sort the competitors by their name (alphabetical, ascending).
|
||||||
* @param competitors The competitors of one category.
|
* @param competitors The competitors of one category.
|
||||||
*/
|
*/
|
||||||
void Sport::sortByName(QList<Competitor> &competitors) {
|
void SportModel::sortByName(QList<Competitor*> &competitors) {
|
||||||
if (competitors.isEmpty()) return;
|
if (competitors.isEmpty()) return;
|
||||||
sort(competitors.begin(), competitors.end(), Competitor::compareName);
|
std::sort(competitors.begin(), competitors.end(), Competitor::compareName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Sport::sortByCountry Sort the competitors by their national olympic comittee short name (alphabetical, ascending).
|
* @brief Sport::sortByCountry Sort the competitors by their national olympic comittee short name (alphabetical, ascending).
|
||||||
* @param competitors The competitors of one category.
|
* @param competitors The competitors of one category.
|
||||||
*/
|
*/
|
||||||
void Sport::sortByCountry(QList<Competitor> &competitors) {
|
void SportModel::sortByCountry(QList<Competitor*> &competitors) {
|
||||||
if (competitors.isEmpty()) return;
|
if (competitors.isEmpty()) return;
|
||||||
sort(competitors.begin(), competitors.end(), Competitor::compareNOC);
|
std::sort(competitors.begin(), competitors.end(), Competitor::compareNOC);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Sport::sortByResult Sort the competitors by their results in one specific category (numerical, ascending).
|
* @brief Sport::sortByResult Sort the competitors by their results in one specific category (numerical, ascending).
|
||||||
* @param competitors The competitors of one category.
|
* @param competitors The competitors of one category.
|
||||||
*/
|
*/
|
||||||
void Sport::sortByResult(QList<CompetitorWithResults> &competitors) {
|
void SportModel::sortByResult(QList<CompetitorWithResults*> &competitors) {
|
||||||
if (competitors.isEmpty()) return;
|
if (competitors.isEmpty()) return;
|
||||||
sort(competitors.begin(), competitors.end(), CompetitorWithResults::compare);
|
std::sort(competitors.begin(), competitors.end(), CompetitorWithResults::compare);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Sport::sortByMedals Sort the competitors by their medal amounts in one specific category (numerical, ascending).
|
* @brief Sport::sortByMedals Sort the competitors by their medal amounts in one specific category (numerical, ascending).
|
||||||
* @param competitors The competitors of one category.
|
* @param competitors The competitors of one category.
|
||||||
*/
|
*/
|
||||||
void Sport::sortByMedals(QList<MedalWinner> &competitors) {
|
void SportModel::sortByMedals(QList<MedalWinner*> &competitors) {
|
||||||
if (competitors.isEmpty()) return;
|
if (competitors.isEmpty()) return;
|
||||||
sort(competitors.begin(), competitors.end(), MedalWinner::compare);
|
std::sort(competitors.begin(), competitors.end(), MedalWinner::compare);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Sport::reverseOrder Reverses the order of the competitors.
|
* @brief Sport::reverseOrder Reverses the order of the competitors.
|
||||||
* @param competitors The competitors of one category.
|
* @param competitors The competitors of one category.
|
||||||
*/
|
*/
|
||||||
void Sport::reverseOrder(QList<Competitor> &competitors) {
|
void SportModel::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 left = Competitor(competitors.value(i));
|
Competitor *left = competitors.value(i);
|
||||||
Competitor right = Competitor(competitors.value(competitors.size() - 1 - i));
|
Competitor *right = competitors.value(competitors.size() - 1 - i);
|
||||||
|
|
||||||
competitors.replace(i, right);
|
competitors.replace(i, right);
|
||||||
competitors.replace(competitors.size() - 1 - i, left);
|
competitors.replace(competitors.size() - 1 - i, left);
|
||||||
|
@ -402,13 +402,15 @@ void Sport::reverseOrder(QList<Competitor> &competitors) {
|
||||||
* Stores the m_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 SportModel::addRelativeToFirst(QList<CompetitorWithResults*> &competitors) {
|
||||||
if (competitors.isEmpty()) return;
|
if (competitors.isEmpty()) return;
|
||||||
|
|
||||||
QString reference = competitors.value(0).getMark();
|
QString reference = competitors.value(0)->getMark();
|
||||||
|
|
||||||
for (CompetitorWithResults comp : competitors) {
|
for (int i = 0; i < competitors.size(); i++) {
|
||||||
QString result = comp.getMark();
|
CompetitorWithResults *comp = competitors.value(i);
|
||||||
|
|
||||||
|
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;
|
||||||
|
@ -417,7 +419,7 @@ void Sport::addRelativeToFirst(QList<CompetitorWithResults> &competitors) {
|
||||||
stat.append("%");
|
stat.append("%");
|
||||||
if (stat.at(0).isNumber()) stat = QString("+").append(stat);
|
if (stat.at(0).isNumber()) stat = QString("+").append(stat);
|
||||||
|
|
||||||
comp.setStatistic(stat);
|
comp->setStatistic(stat);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -428,7 +430,7 @@ void Sport::addRelativeToFirst(QList<CompetitorWithResults> &competitors) {
|
||||||
* @param val The value to calculate the deviation from.
|
* @param val The value to calculate the deviation from.
|
||||||
* @return The deviation from ref to val in percent.
|
* @return The deviation from ref to val in percent.
|
||||||
*/
|
*/
|
||||||
float Sport::calcRelativeStat(QString ref, QString val) {
|
float SportModel::calcRelativeStat(QString ref, QString val) {
|
||||||
// check if the value is not a time
|
// check if the value is not a time
|
||||||
if (!ref.contains(":") && !val.contains(":")) {
|
if (!ref.contains(":") && !val.contains(":")) {
|
||||||
float fRef = ref.toFloat();
|
float fRef = ref.toFloat();
|
85
src/model/SportModel.h
Normal file
85
src/model/SportModel.h
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "MedalWinner.h"
|
||||||
|
#include "CompetitorWithResults.h"
|
||||||
|
#include <QAbstractListModel>
|
||||||
|
#include <QNetworkAccessManager>
|
||||||
|
#include <qcontainerfwd.h>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
#include <QString>
|
||||||
|
#include <QList>
|
||||||
|
#include "EventInfo.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class SportModel : public QAbstractListModel
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
Q_PROPERTY(QString discipline READ discipline WRITE setDiscipline);
|
||||||
|
|
||||||
|
public:
|
||||||
|
enum Role
|
||||||
|
{
|
||||||
|
EventName = Qt::UserRole + 1,
|
||||||
|
Competitors
|
||||||
|
};
|
||||||
|
|
||||||
|
explicit SportModel(QObject *parent = nullptr);
|
||||||
|
|
||||||
|
void setDiscipline(QJsonObject discipline)
|
||||||
|
{
|
||||||
|
this->o_discipline = QJsonObject(discipline);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual int rowCount(const QModelIndex &parent) const override;
|
||||||
|
virtual QVariant data(const QModelIndex &index, int role) const override;
|
||||||
|
virtual QHash<int, QByteArray> roleNames() const override;
|
||||||
|
|
||||||
|
set<QString> getCategories();
|
||||||
|
QList<CompetitorWithResults*> getCompetitorsByCategory(QString category); // TODO ref instead of obj
|
||||||
|
QList<MedalWinner*> getCompetitorsWithMedal(); // TODO ref instead of obj
|
||||||
|
|
||||||
|
// filter to change the current competitor list
|
||||||
|
void lastName(QList<Competitor*> &competitors); // TODO ref instead of obj
|
||||||
|
void filterByName(QList<Competitor*> &competitors, QString name); // TODO ref instead of obj
|
||||||
|
void filterByCountry(QList<Competitor*> &competitors, QString nocShort); // TODO ref instead of obj
|
||||||
|
|
||||||
|
// sort functions to change the order of the current competitor list
|
||||||
|
void sortByName(QList<Competitor*> &competitors); // TODO ref instead of obj
|
||||||
|
void sortByCountry(QList<Competitor*> &competitors); // TODO ref instead of obj
|
||||||
|
void sortByResult(QList<CompetitorWithResults*> &competitors); // TODO ref instead of obj
|
||||||
|
void sortByMedals(QList<MedalWinner*> &competitors); // TODO ref instead of obj
|
||||||
|
void reverseOrder(QList<Competitor*> &competitors); // TODO ref instead of obj
|
||||||
|
|
||||||
|
// statistic function
|
||||||
|
void addRelativeToFirst(QList<CompetitorWithResults*> &competitors); // TODO ref instead of obj
|
||||||
|
|
||||||
|
QString discipline() const;
|
||||||
|
void setDiscipline(const QString &discipline);
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void request(QString discipline);
|
||||||
|
void parseData();
|
||||||
|
|
||||||
|
private:
|
||||||
|
QList<EventInfo *> m_sportList;
|
||||||
|
QString m_discipline;
|
||||||
|
QNetworkAccessManager m_networkManager;
|
||||||
|
QNetworkReply *m_reply = nullptr;
|
||||||
|
|
||||||
|
// data from api
|
||||||
|
QJsonObject o_discipline;
|
||||||
|
bool validateDiscipline();
|
||||||
|
|
||||||
|
void filterCompetitors(QList<Competitor*> &competitors, QString filter); // TODO ref instead of obj
|
||||||
|
|
||||||
|
QJsonObject createCompetitorWithMedals(QJsonObject medalComp);
|
||||||
|
|
||||||
|
// function for statistic calculation
|
||||||
|
float calcRelativeStat(QString ref, QString val);
|
||||||
|
|
||||||
|
};
|
Loading…
Reference in a new issue