-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathticketmodel.cpp
More file actions
100 lines (88 loc) · 2.7 KB
/
ticketmodel.cpp
File metadata and controls
100 lines (88 loc) · 2.7 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "ticketmodel.h"
#include <QJsonDocument>
#include <QDebug>
TicketModel::TicketModel(QObject *parent)
: QAbstractListModel(parent)
{
}
int TicketModel::rowCount(const QModelIndex &parent) const
{
// For list models only the root node (an invalid parent) should return the list's size. For all
// other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
if (parent.isValid())
return 0;
return m_tickets.size();
}
QHash<int, QByteArray> TicketModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[IdRole] = "idRole";
roles[StatusRole] = "statusRole";
roles[FirstNameRole] = "firstNameRole";
roles[LastNameRole] = "lastNameRole";
roles[EmailRole] = "emailRole";
roles[TimeIdRole] = "timeIdRole";
roles[TimeRole] = "timeRole";
roles[TimeOfReservationRole] = "timeOfReservationRole";
roles[YearRole] = "yearRole";
return roles;
}
QVariant TicketModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() < 0 || index.row() >= rowCount()) {
return {};
}
const Ticket &ticket = m_tickets[index.row()];
switch (static_cast<TicketRole>(role)) {
case TicketModel::IdRole:
return ticket.id;
case TicketModel::StatusRole:
return ticket.status;
case TicketModel::FirstNameRole:
return ticket.firstName;
case TicketModel::LastNameRole:
return ticket.lastName;
case TicketModel::EmailRole:
return ticket.email;
case TicketModel::TimeIdRole:
return ticket.timeId;
case TicketModel::TimeRole:
return ticket.time;
case TicketModel::TimeOfReservationRole:
return ticket.timeOfReservation;
case TicketModel::YearRole:
return ticket.year;
}
return {};
}
void TicketModel::loadFromArray(QJsonArray array) {
clear();
for (QJsonValue value : array) {
if (!value.isObject()) {
continue;
}
QJsonObject object = value.toObject();
QJsonValue yearValue = object.value("year");
if (yearValue.isObject()) {
QJsonObject yearObject = yearValue.toObject();
QString year = yearObject.value("name").toString();
if (year != "2023") {
continue;
}
}
beginInsertRows({}, rowCount(), rowCount());
m_tickets << Ticket(object);
endInsertRows();
}
}
void TicketModel::loadFromJson(QString json) {
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
if (!doc.isArray()) {
return;
}
loadFromArray(doc.array());
}
void TicketModel::clear() {
beginResetModel();
m_tickets.clear();
endResetModel();
}