-
Notifications
You must be signed in to change notification settings - Fork 1
/
machinemodel.cpp
312 lines (272 loc) · 10.3 KB
/
machinemodel.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*!
* \file machinemodel.cpp
* \author Simon Coakley
* \date 2012
* \copyright Copyright (c) 2012 University of Sheffield
* \brief Implementation of machine transition function table
*/
#include <QtGui>
#include <QXmlStreamWriter>
#include "./machinemodel.h"
State * MachineModel::addState(QString name) {
/*states.append(s);
if(s->getStartState()) startState = s;*/
// qDebug() << "machine addState " << s->getName();
State * s = new State(name);
// if(start) startState = s;
states.append(s);
return s;
}
Transition * MachineModel::addTransition(QString name, State *current,
State *next) {
// qDebug() << cs << " " << name << " " << ns;
// State * pcs = 0;
// State * pns = 0;
this->insertRows(0, 1);
// Search for and add states
/* for(int i = 0; i < states.size(); i ++)
{
State * s = states.at(i);
if(s->name() == t->preState) pcs = s;
if(s->name() == t->postState) pns = s;
}
if(pcs == 0)
{
State * s = new State(t->preState);
states.append(s);
pcs = s;
}
if(pns == 0)
{
State * s = new State(t->postState);
states.append(s);
pns = s;
}
*/
Transition * transition = new Transition(current, name, next);
transitions.append(transition);
// t->transition = transition;
return transition;
// connect(a, SIGNAL(updateMpre(Arrow*)), this,
// SLOT(handleUpdatedMpre(Arrow*)));
/*transitionTable->insertRow(0);
transitionTable->setItem(0,0,new StateTableItem(a->startItem()));//new QTableWidgetItem(a->startItem()->getName()));
transitionTable->setItem(0,3,new QTableWidgetItem(a->getName()));
transitionTable->setItem(0,6,new StateTableItem(a->endItem()));//new QTableWidgetItem(a->endItem()->getName()));*/
// qDebug() << "machine addTransition " << t->name() <<
// " " << pcs->name() << " -> " << pns->name();
}
Transition * MachineModel::addTransitionString(QString n, QString cs,
QString ns, Condition c, Mpost mpost,
Communication input, Communication output, QString desc) {
State * pcs = 0;
State * pns = 0;
this->insertRows(0, 1);
// Search for and add states
for (int i = 0; i < states.size(); i ++) {
State * s = states.at(i);
if (s->name() == cs) pcs = s;
if (s->name() == ns) pns = s;
}
if (pcs == 0) {
State * s = new State(cs);
states.append(s);
pcs = s;
}
if (pns == 0) {
State * s = new State(ns);
states.append(s);
pns = s;
}
Transition * t = new Transition(pcs, n, pns);
// t->setMpre(mpre);
t->setCondition(c);
t->setMpost(mpost);
t->setInput(input);
t->setOutput(output);
t->setDescription(desc);
transitions.append(t);
return t;
}
int MachineModel::rowCount(const QModelIndex &/*parent*/) const {
return transitions.count();
}
int MachineModel::columnCount(const QModelIndex &/*parent*/) const {
return 8;
}
QVariant MachineModel::data(const QModelIndex &index, int role) const {
if (!index.isValid())
return QVariant();
if (index.row() >= transitions.size())
return QVariant();
if (role == Qt::DisplayRole) {
if (index.column() == 0) return transitions.at(
index.row())->currentState()->name();
if (index.column() == 1) return qVariantFromValue(
transitions.at(index.row())->input());
if (index.column() == 2) return qVariantFromValue(
transitions.at(index.row())->condition());
if (index.column() == 3) return transitions.at(
index.row())->name();
if (index.column() == 4) return qVariantFromValue(
transitions.at(index.row())->mpost());
if (index.column() == 5) return qVariantFromValue(
transitions.at(index.row())->output());
if (index.column() == 6) return transitions.at(
index.row())->nextState()->name();
if (index.column() == 7) return transitions.at(
index.row())->description();
return QVariant();
}
return QVariant();
}
QVariant MachineModel::headerData(int section, Qt::Orientation orientation,
int role) const {
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
if (section == 0) return QString("Current State");
else if (section == 1) return QString("Input");
else if (section == 2) return QString("Condition");
else if (section == 3) return QString("Function Name");
else if (section == 4) return QString("Mpost");
else if (section == 5) return QString("Output");
else if (section == 6) return QString("Next State");
else if (section == 7) return QString("Description");
else
return QString("Row %1").arg(section);
} else {
return QString("Row %1").arg(section);
}
}
Qt::ItemFlags MachineModel::flags(const QModelIndex &index) const {
if (!index.isValid())
return Qt::ItemIsEnabled;
if (index.column() == 4) return QAbstractItemModel::flags(index);
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
bool MachineModel::setData(const QModelIndex &index,
const QVariant &value, int role) {
if (index.isValid() && role == Qt::EditRole) {
if (index.column() == 0) {
transitions.at(index.row())->currentState()->
setName(value.toString());
emit(updateStateName(transitions.at(index.row())->currentState()));
}
if (index.column() == 1) {
transitions.at(index.row())->
setInput(qVariantValue<Communication>(value));
emit(updateInput(transitions.at(index.row())));
}
if (index.column() == 2) transitions.at(index.row())->
setCondition(qVariantValue<Condition>(value));
if (index.column() == 3) {
transitions.at(index.row())->setName(value.toString());
emit(updateTransitionName(transitions.at(index.row())));
}
if (index.column() == 4) transitions.at(index.row())->
setMpost(qVariantValue<Mpost>(value));
if (index.column() == 5) {
transitions.at(index.row())->
setOutput(qVariantValue<Communication>(value));
emit(updateOutput(transitions.at(index.row())));
}
if (index.column() == 6) {
transitions.at(index.row())->nextState()->
setName(value.toString());
emit(updateStateName(transitions.at(index.row())->nextState()));
}
if (index.column() == 7) transitions.at(index.row())->
setDescription(value.toString());
emit dataChanged(index, index);
return true;
}
return false;
}
void MachineModel::addMessageToTransition(Transition * t, bool isInput,
QString messageType) {
if (isInput) {
t->input().messageModel->addMessage(messageType);
emit dataChanged(index(transitions.indexOf(t), 1, QModelIndex()),
index(transitions.indexOf(t), 1, QModelIndex()));
} else { // output
t->output().messageModel->addMessage(messageType);
emit dataChanged(index(transitions.indexOf(t), 5, QModelIndex()),
index(transitions.indexOf(t), 5, QModelIndex()));
}
emit(communicationChanged());
}
bool MachineModel::insertRows(int position, int rows,
const QModelIndex &/*parent*/) {
beginInsertRows(QModelIndex(), position, position+rows-1);
for (int row = 0; row < rows; ++row) {
/*stringListCurrentState.insert(position, "");
stringListInput.insert(position, "");
stringListMpre.insert(position, "");
stringListFunctionName.insert(position, "");
stringListMpost.insert(position, "");
stringListOutput.insert(position, "");
stringListNextState.insert(position, "");*/
}
endInsertRows();
return true;
}
bool MachineModel::removeRows(int position, int rows,
const QModelIndex &/*parent*/) {
beginRemoveRows(QModelIndex(), position, position+rows-1);
for (int row = position; row < position+rows; row++) {
transitions.removeAt(row);
}
endRemoveRows();
return true;
}
/*void MachineModel::setStartStateString(QString n)
{
// Check to see if state exists
startState = 0;
// Check if states exist
for(int i = 0; i < states.size(); i++)
{
states.at(i)->setStartState(false);
if(states.at(i)->name() == n)
{
startState = states.at(i);
states.at(i)->setStartState(true);
}
}
if(startState == 0)
{
this->addState(n, true);
}
}*/
void MachineModel::transitionUpdated(QModelIndex topLeft,
QModelIndex /*bottomRight*/) {
// ui->label->setText(QString(
// "transitionChanged r: %1 c: %2").arg(topLeft.row(), topLeft.column()));
// emit( updateScene() );
// If mpre was updated
if (topLeft.column() == 2) {
// If another branch from the same state exists
Transition * t = transitions.at(topLeft.row());
for (int i = 0; i < transitions.size(); i++) {
Transition * t2 = transitions.at(i);
if (t->currentState() == t2->currentState() &&
i != topLeft.row()) {
// qDebug() << "Found other branch: " << t2->name();
t2->getMprePointer()->setNot(false);
t2->getMprePointer()->setEnabled(t->mpre().enabled());
t2->getMprePointer()->setName(t->mpre().name());
if (t->mpre().op() == "==") t2->getMprePointer()->setOp("!=");
if (t->mpre().op() == "!=") t2->getMprePointer()->setOp("==");
if (t->mpre().op() == ">") t2->getMprePointer()->setOp("<=");
if (t->mpre().op() == "<") t2->getMprePointer()->setOp(">=");
if (t->mpre().op() == ">=") t2->getMprePointer()->setOp("<");
if (t->mpre().op() == "<=") t2->getMprePointer()->setOp(">");
t2->getMprePointer()->setValue(t->mpre().value());
}
}
}
}
void MachineModel::deleteTransition(Transition *t) {
removeRows(transitions.indexOf(t), 1);
}