-
Notifications
You must be signed in to change notification settings - Fork 0
/
teamlistdatamodel.cpp
191 lines (172 loc) · 4.9 KB
/
teamlistdatamodel.cpp
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <algorithm>
#include <teamlistdatamodel.h>
TeamListDataModel::TeamListDataModel(EntryDataStore& store, const QFont& font, QObject* parent) : QAbstractTableModel(parent),
team_selections(),
data_store(store),
font_metrics(font) {
}
TeamListDataModel::~TeamListDataModel() {}
int TeamListDataModel::columnCount(const QModelIndex& parent) const {
return 5;
}
int TeamListDataModel::rowCount(const QModelIndex& parent) const {
return data_store.teamCount();
}
QVariant TeamListDataModel::data(const QModelIndex& index, int role) const {
int team = data_store.sortedTeam(index.row());
if (team == -1) {
return QVariant(QVariant::Invalid);
}
if (index.column() < 0 || index.column() >= columnCount()) {
return QVariant(QVariant::Invalid);
}
if (role == Qt::TextAlignmentRole) {
return QVariant(columnAlignment(index.column()));
}
Entry::Range low;
QString text;
switch (columnType(index.column())) {
case INVALID:
return QVariant(QVariant::Invalid);
case TEAM_NUMBER:
text = QString::number(team);
break;
case GEAR_HIGH:
text = QString::number(data_store.gearHigh(team));
break;
case CLIMB_LOW:
low = data_store.climbLow(team);
text = QString::number(low.low) + " - " + QString::number(low.high);
break;
case GEAR_AVG:
text = QString::number(data_store.gearAverage(team));
break;
case CLIMB_AVG:
text = QString::number(data_store.climbAverage(team));
break;
case CLIMB_RATE:
text = QString::number(data_store.climbRate(team));
break;
case SELECTABLE:
if (role == Qt::CheckStateRole) {
return QVariant(team_selections.value(team, true) ? Qt::Checked : Qt::Unchecked);
} else {
return QVariant(QVariant::Invalid);
}
}
switch (role) {
case Qt::DisplayRole:
return QVariant(text);
case Qt::SizeHintRole:
return QVariant(font_metrics.boundingRect(text).size());
default:
return QVariant(QVariant::Invalid);
}
}
bool TeamListDataModel::setData(const QModelIndex& index, const QVariant& value, int role) {
if (index.row() < 0 || index.row() >= data_store.teamCount()) {
return false;
}
if (columnType(index.column()) == SELECTABLE && role == Qt::CheckStateRole) {
if (value.type() == QVariant::Int) {
int team = data_store.sortedTeam(index.row());
if (value.toInt() == Qt::Checked) {
team_selections[team] = true;
} else if (value.toInt() == Qt::Unchecked) {
team_selections[team] = false;
} else {
return false;
}
}
return true;
}
return false;
}
QVariant TeamListDataModel::headerData(int section, Qt::Orientation orientation, int role) const {
if (orientation == Qt::Vertical || role == Qt::DecorationRole || role == Qt::EditRole) {
return QVariant(QVariant::Invalid);
}
if (role == Qt::TextAlignmentRole) {
return QVariant(columnAlignment(section));
}
QString col_name;
switch (columnType(section)) {
case TeamListDataModel::TEAM_NUMBER:
col_name = "Team";
break;
case TeamListDataModel::GEAR_HIGH:
col_name = "Gear High";
break;
case TeamListDataModel::CLIMB_LOW:
col_name = "Climb Low";
break;
case TeamListDataModel::GEAR_AVG:
col_name = "Gear Average";
break;
case TeamListDataModel::CLIMB_AVG:
col_name = "Climb Average";
break;
case TeamListDataModel::CLIMB_RATE:
col_name = "Climb Rate";
break;
case TeamListDataModel::SELECTABLE:
col_name = "Selectable";
break;
case TeamListDataModel::INVALID:
return QVariant(QVariant::Invalid);
}
if (role == Qt::SizeHintRole) {
return QVariant(font_metrics.boundingRect(col_name).size());
}
return QVariant(col_name);
}
Qt::ItemFlags TeamListDataModel::flags(const QModelIndex& index) const {
if (index.row() < 0 || index.row() >= data_store.teamCount() ||
index.column() < 0 || index.column() >= columnCount()) {
return Qt::NoItemFlags;
}
Qt::ItemFlags f = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
if (columnType(index.column()) == TeamListDataModel::SELECTABLE) {
f |= Qt::ItemIsUserCheckable;
}
return f;
}
TeamListDataModel::ColumnType TeamListDataModel::columnType(int col) const {
switch (col) {
case 0:
return TEAM_NUMBER;
case 1:
switch (data_store.sortOrder()) {
case EntryDataStore::TEAM_NUMBER:
case EntryDataStore::GEAR_AVG:
case EntryDataStore::CLIMB_AVG:
case EntryDataStore::CLIMB_RATE:
return GEAR_AVG;
case EntryDataStore::GEAR_HIGH:
case EntryDataStore::CLIMB_LOW:
return GEAR_HIGH;
}
case 2:
switch (data_store.sortOrder()) {
case EntryDataStore::TEAM_NUMBER:
case EntryDataStore::GEAR_AVG:
case EntryDataStore::CLIMB_AVG:
case EntryDataStore::CLIMB_RATE:
return CLIMB_AVG;
case EntryDataStore::GEAR_HIGH:
case EntryDataStore::CLIMB_LOW:
return CLIMB_LOW;
}
case 3:
return CLIMB_RATE;
case 4:
return SELECTABLE;
}
return INVALID;
}
QFlags<Qt::AlignmentFlag> TeamListDataModel::columnAlignment(int col) {
return col == 0 ? Qt::AlignLeft | Qt::AlignVCenter : Qt::AlignCenter;
}
void TeamListDataModel::setSortOrder(EntryDataStore::SortOrder order) {
data_store.setSortOrder(order);
}