From eb61690873e234ea28a3f437fb7211c773a449fe Mon Sep 17 00:00:00 2001 From: Orangerot Date: Fri, 16 Aug 2024 07:06:05 +0200 Subject: feat(EventInfo): display EventInfo with List of Competitors --- src/model/EventInfo.cpp | 22 ++++++++++++++++++++++ src/model/EventInfo.h | 28 ++++++++++++++++++++++++++++ src/model/Sport.cpp | 29 ++++++++++++++++++++++------- src/model/Sport.h | 8 +++++--- 4 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 src/model/EventInfo.cpp create mode 100644 src/model/EventInfo.h (limited to 'src') diff --git a/src/model/EventInfo.cpp b/src/model/EventInfo.cpp new file mode 100644 index 0000000..e9ecd46 --- /dev/null +++ b/src/model/EventInfo.cpp @@ -0,0 +1,22 @@ +#include +#include "EventInfo.h" + +EventInfo::EventInfo(QObject *parent) : QObject(parent) { +} + +QString EventInfo::eventName() const { + return m_eventName; +} + +void EventInfo::setEventName(const QString &newEventName) { + m_eventName = newEventName; +} + +QList EventInfo::competitors() const { + return m_competitors; +} + +void EventInfo::setCompetitors(const QList &newCompetitors) { + m_competitors = newCompetitors; +} + diff --git a/src/model/EventInfo.h b/src/model/EventInfo.h new file mode 100644 index 0000000..7f937b2 --- /dev/null +++ b/src/model/EventInfo.h @@ -0,0 +1,28 @@ +#ifndef ITAT_CHALLANGE_OLYMPICS_EVENT_H +#define ITAT_CHALLANGE_OLYMPICS_EVENT_H + +#include +#include + +class EventInfo : QObject { + Q_OBJECT + // QML_ELEMENT + + Q_PROPERTY(QString eventName READ eventName WRITE setEventName); + Q_PROPERTY(QList competitors READ competitors WRITE setCompetitors); + + public: + explicit EventInfo(QObject *parent = nullptr); + + QString eventName() const; + void setEventName(const QString &newEventName); + + QList competitors() const; + void setCompetitors(const QList &newCompetitors); + + private: + QString m_eventName; + QList m_competitors; +}; + +#endif diff --git a/src/model/Sport.cpp b/src/model/Sport.cpp index 38173ce..8c4902b 100644 --- a/src/model/Sport.cpp +++ b/src/model/Sport.cpp @@ -1,4 +1,5 @@ #include "Sport.h" +#include "Competitor.h" // categories #include @@ -34,12 +35,15 @@ int SportModel::rowCount(const QModelIndex &parent) const { QVariant SportModel::data(const QModelIndex &index, int role) const { if (index.isValid() && index.row() >= 0 && index.row() < m_sportList.size()) { - QString sportName = m_sportList[index.row()]; + EventInfo *event = m_sportList[index.row()]; - return sportName; - // switch ((Role) role) { - // case SportName: - // } + switch ((Role) role) { + case EventName: + return event->eventName(); + + case Competitors: + return event->competitors(); + } } return {}; @@ -48,7 +52,8 @@ QVariant SportModel::data(const QModelIndex &index, int role) const { QHash SportModel::roleNames() const { QHash names; - names[SportName] = "sportName"; + names[EventName] = "eventName"; + names[Competitors] = "competitors"; return names; } @@ -77,8 +82,18 @@ void SportModel::parseData() { QJsonArray sports = jsonDocument["units"].toArray(); for (const auto &sport : sports) { QJsonObject entry = sport.toObject(); + + EventInfo *event = new EventInfo(this); + event->setEventName(entry["eventUnitName"].toString()); + + QList competitors; + for (const auto &competitor : entry["competitors"].toArray()) { + competitors << competitor.toObject()["name"].toString(); + } + event->setCompetitors(competitors); + qDebug() << entry["eventUnitName"].toString(); - m_sportList << entry["eventUnitName"].toString(); + m_sportList << event; } endResetModel(); } diff --git a/src/model/Sport.h b/src/model/Sport.h index 0f183b7..be74a51 100644 --- a/src/model/Sport.h +++ b/src/model/Sport.h @@ -8,6 +8,7 @@ #include #include #include +#include "EventInfo.h" using namespace std; @@ -16,7 +17,8 @@ class SportModel : public QAbstractListModel { public: enum Role { - SportName = Qt::UserRole + 1 + EventName = Qt::UserRole + 1, + Competitors }; explicit SportModel(QObject *parent = nullptr); @@ -29,7 +31,7 @@ class SportModel : public QAbstractListModel { void parseData(); private: - QList m_sportList; + QList m_sportList; QNetworkAccessManager m_networkManager; QNetworkReply *m_reply = nullptr; }; @@ -111,4 +113,4 @@ private: }; -#endif //ITAT_CHALLANGE_OLYMPICS_SPORT_H +#endif -- cgit v1.2.3