-
Notifications
You must be signed in to change notification settings - Fork 1
/
memorymodel.cpp
210 lines (180 loc) · 6.56 KB
/
memorymodel.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*!
* \file memorymodel.cpp
* \author Simon Coakley
* \date 2012
* \copyright Copyright (c) 2012 University of Sheffield
* \brief Implementation of model memory table
*/
#include <QtGui>
#include "./memorymodel.h"
/*MemoryModel::MemoryModel()
{
}*/
int MemoryModel::rowCount(const QModelIndex &/*parent*/) const {
// return stringListType.count();
return variables.count();
}
int MemoryModel::columnCount(const QModelIndex &/*parent*/) const {
if (myIsModel)
return 2;
else
return 3;
}
QVariant MemoryModel::data(const QModelIndex &index, int role) const {
if (!index.isValid())
return QVariant();
if (index.row() >= variables.size())
return QVariant();
if (role == Qt::DisplayRole) {
if (myIsModel) {
if (index.column() == 0)
return variables.at(index.row()).name;
if (index.column() == 1)
return variables.at(index.row()).description;
} else {
if (index.column() == 0)
return variables.at(index.row()).type;
// stringListType.at(index.row());
if (index.column() == 1)
return variables.at(index.row()).name;
// stringListName.at(index.row());
if (index.column() == 2)
return variables.at(index.row()).description;
/*if(index.column() == 2)
{
//return variables[index.row()].getValue();
if(variables.at(index.row()).type == "int")
return variables.at(index.row()).ivalue;
else
return variables.at(index.row()).dvalue;
}*/
}
return QVariant();
} else {
return QVariant();
}
}
QVariant MemoryModel::headerData(int section, Qt::Orientation orientation,
int role) const {
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
if (myIsModel) {
if (section == 0) return QString("Type");
else if (section == 1) return QString("Value");
} else {
if (section == 0) return QString("Type");
else if (section == 1) return QString("Name");
else if (section == 2) return QString("Description");
}
return QString("");
} else {
return QString("Row %1").arg(section);
}
}
Qt::ItemFlags MemoryModel::flags(const QModelIndex &index) const {
if (!index.isValid())
return Qt::ItemIsEnabled;
if (myIsModel && index.column() == 0)
return QAbstractItemModel::flags(index);
else
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
bool MemoryModel::setData(const QModelIndex &index,
const QVariant &value, int role) {
if (index.isValid() && role == Qt::EditRole) {
if (isModel()) {
if (index.column() == 0)
variables[index.row()].name = value.toString();
if (index.column() == 1)
variables[index.row()].description = value.toString();
} else {
if (index.column() == 0)
variables[index.row()].type = value.toString();
// stringListType.replace(index.row(), value.toString());
if (index.column() == 1)
variables[index.row()].name = value.toString();
// stringListName.replace(index.row(), value.toString());
if (index.column() == 2)
variables[index.row()].description = value.toString();
/*if(index.column() == 2)
//variables[index.row()].setValue(value.toDouble());
{
variables[index.row()].ivalue = value.toInt();
variables[index.row()].dvalue = value.toDouble();
}*/
}
emit dataChanged(index, index);
return true;
}
return false;
}
bool MemoryModel::insertRows(int position, int rows,
const QModelIndex &/*parent*/) {
beginInsertRows(QModelIndex(), position, position+rows-1);
for (int row = 0; row < rows; ++row) {
QString s = "name_";
s.append(QString("%1").arg(this->rowCount()));
variables.insert(position, Variable(s, "int"));
/*stringListType.insert(position, "int");
stringListName.insert(position, s);
intListValue.insert(position, 0);
doubleListValue.insert(position, 0);*/
}
endInsertRows();
return true;
}
bool MemoryModel::removeRows(int position, int rows,
const QModelIndex &/*parent*/) {
beginRemoveRows(QModelIndex(), position, position+rows-1);
for (int row = 0; row < rows; ++row) {
variables.removeAt(position);
/*stringListType.removeAt(position);
stringListName.removeAt(position);
intListValue.removeAt(position);
doubleListValue.removeAt(position);*/
}
endRemoveRows();
return true;
}
/*void MemoryModel::replaceIntValue(int i, int value)
{
intListValue.replace(i, value);
QModelIndex myIndex = this->index(i, 2, QModelIndex());
emit( this->dataChanged(myIndex, myIndex) );
}*/
void MemoryModel::replaceValue(int i, double value) {
variables[i].ivalue = static_cast<int>(value);
variables[i].dvalue = value;
QModelIndex myIndex = this->index(i, 2, QModelIndex());
emit(this->dataChanged(myIndex, myIndex));
}
void MemoryModel::replaceValue(QString type, QString value) {
for (int i = 0; i < variables.size(); i++) {
if (variables[i].name == type) variables[i].description = value;
QModelIndex myIndex = this->index(i, 1, QModelIndex());
emit(this->dataChanged(myIndex, myIndex));
}
}
void MemoryModel::addVariable(QString t, QString n, QString desc,
bool constant, double i) {
// if(myIsModel) qDebug() << n << desc;
insertRow(rowCount());
variables[(rowCount()-1)].type = t;
variables[(rowCount()-1)].name = n;
variables[(rowCount()-1)].description = desc;
variables[(rowCount()-1)].constant = constant;
variables[(rowCount()-1)].ivalue = static_cast<int>(i);
variables[(rowCount()-1)].dvalue = i;
/*stringListType.replace(rowCount()-1, t);
stringListName.replace(rowCount()-1, n);
intListValue.replace(rowCount()-1, (int)i);
doubleListValue.replace(rowCount()-1, i);*/
}
QStringList MemoryModel::getNames() {
QStringList names;
for (int i = 0; i < variables.count(); i++) {
names.append(variables[i].name);
}
return names;
}