summaryrefslogtreecommitdiff
path: root/src/model/SportModel.h
blob: 808a1cc7681dc82bc8031cc593d06a4794b2d323 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#pragma once

#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();
  map<QString, map<QString, int>> getMedalsOfCompetitors();

  // filter to change the current competitor list
  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 list
//  void sortByName(QList<Competitor*> &competitors);
//  void sortByCountry(QList<Competitor*> &competitors);
//  void sortByResult(QList<Competitor*> &competitors);
//  void sortByMedals(QList<Competitor*> &competitors);
  void reverseOrder(QList<Competitor*> &competitors);

  // statistic function
  void addRelativeToFirst(QList<Competitor*> &competitors);

  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

  // function for statistic calculation
  float calcRelativeStat(QString ref, QString val);

};