This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
maptablemodel.cpp
163 lines (135 loc) · 4.06 KB
/
maptablemodel.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
/*
Copyright 2013 Jared Wiltshire
This file is part of PPD Maps.
PPD Maps is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PPD Maps is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PPD Maps. If not, see <http://www.gnu.org/licenses/>.
*/
#include "maptablemodel.h"
mapTableModel::mapTableModel(QList<map*>* data, QObject *parent) :
QAbstractTableModel(parent), modelData(data)
{
}
QVariant mapTableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= modelData->size())
return QVariant();
if (role == Qt::DisplayRole) {
switch(index.column()) {
case 0:
return modelData->at(index.row())->getAddressStr();
case 1:
return modelData->at(index.row())->dimensionsStr();
case 2:
return modelData->at(index.row())->getLabel();
}
}
if (role == Qt::BackgroundRole) {
if (highlighted.contains(index.row())) {
return QBrush(Qt::yellow);
}
}
// UserRole is used for sorting
if (role == Qt::UserRole) {
int elements = 0;
switch(index.column()) {
case 0:
return modelData->at(index.row())->getAddressStr();
case 1:
elements = modelData->at(index.row())->getXDim() * modelData->at(index.row())->getYDim();
return QString("%1").arg(elements, 3, 10, QChar('0')) + modelData->at(index.row())->getAddressStr();
case 2:
return modelData->at(index.row())->getLabel() + modelData->at(index.row())->getAddressStr();
}
}
// UserRole+1 is used for filtering
if (role == Qt::UserRole+1) {
return modelData->at(index.row())->getAddressStr() + " " +
modelData->at(index.row())->dimensionsStr() + " " +
modelData->at(index.row())->getLabel();
}
return QVariant();
}
map* mapTableModel::getMap(const QModelIndex &index)
{
return modelData->at(index.row());
}
QModelIndex mapTableModel::getIndex(map* search) {
for (int i = 0; i < modelData->length(); i++) {
if (modelData->at(i) == search) {
return createIndex(i , 0);
}
}
return QModelIndex();
}
void mapTableModel::setHighlightedRow(int i)
{
highlighted.append(i);
updateRow(i);
}
QVariant mapTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole)
{
if (orientation == Qt::Horizontal) {
switch (section)
{
case 0:
return "Addr";
case 1:
return "Dim";
case 2:
return "Label";
}
}
}
return QVariant();
}
int mapTableModel::rowCount(const QModelIndex &parent) const
{
return modelData->size();
}
int mapTableModel::columnCount(const QModelIndex &parent) const
{
return 3;
}
void mapTableModel::refresh()
{
highlighted.clear();
reset();
emit refreshed();
}
void mapTableModel::clearHighlightedRows()
{
if (highlighted.empty()) {
return;
}
int max = 0, min = 0;
for (int i = 0; i < highlighted.count(); i++) {
if (highlighted.at(i) > max) {
max = highlighted.at(i);
}
if (highlighted.at(i) <= min) {
min = highlighted.at(i);
}
}
highlighted.clear();
QModelIndex first = createIndex(min, 0);
QModelIndex last = createIndex(max, 2);
emit dataChanged(first, last);
}
void mapTableModel::updateRow(int i)
{
QModelIndex first = createIndex(i, 0);
QModelIndex last = createIndex(i, 2);
emit dataChanged(first, last);
}