forked from askubis/RESpecTa
/
stateWidget.cpp
222 lines (194 loc) · 6.75 KB
/
stateWidget.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
#include "stateWidget.h"
#include <QtGui>
#include <QObject>
StateWidget::StateWidget(QWidget *parent, Model * newmod )
: QWidget(parent)
{
mod = newmod;
this->setMaximumWidth(230);
//creating Base of the widget (background)
QHBoxLayout *bottomLayout = new QHBoxLayout;
mainLayout = new QVBoxLayout;
setLayout(mainLayout);
OKButton = new QPushButton(tr("&Apply"));
OKButton->setChecked(false);
connect(OKButton, SIGNAL(clicked()), this, SLOT(AcceptState()));
OKButton->setDisabled(true);
bottomLayout->addWidget(OKButton);
/*InsertButton = new QPushButton(tr("&Insert"));
InsertButton->setChecked(false);
connect(InsertButton, SIGNAL(clicked()), this, SLOT(InsertState()));
InsertButton->setDisabled(true);
bottomLayout->addWidget(InsertButton);*/
StateLayout = new QVBoxLayout;
QLabel * nameLabel = new QLabel(tr("State Name:"));
stateNameEdit = new QLineEdit;
connect(stateNameEdit, SIGNAL(textChanged(QString)), this, SLOT(lengthChanged(QString)));
StateLayout->addWidget(nameLabel);
nameLabel->setBuddy(stateNameEdit);
StateLayout->addWidget(stateNameEdit);
stateTypeCombo = new QComboBox(this);
stateTypeCombo->addItems(getStateTypeTable());
connect(stateTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(setStateSubclass(int)));
StateLayout->addWidget(stateTypeCombo);
QLabel * paramLabel = new QLabel("Parameters:");
paramEdit = new QLineEdit;
StateLayout->addWidget(paramLabel);
StateLayout->addWidget(paramEdit);
mainLayout->addLayout(StateLayout);
mainLayout->addStretch();
sysIniWidget* sysIni = new sysIniWidget(this, mod );
mainLayout->addWidget(sysIni);
tmpWidget = 0;
StateWidgets[0]=sysIni;
connect(sysIni, SIGNAL(reportError(QString)), this, SLOT(forwardError(QString)));
runGenWidget* runGen = new runGenWidget(this, mod );
mainLayout->addWidget(runGen);
runGen->setVisible(false);
StateWidgets[1]=runGen;
connect(runGen, SIGNAL(reportError(QString)), this, SLOT(forwardError(QString)));
emptyGenForSetWidget* emptyForSet = new emptyGenForSetWidget(this, mod );
mainLayout->addWidget(emptyForSet);
emptyForSet->setVisible(false);
StateWidgets[2]=emptyForSet;
connect(emptyForSet, SIGNAL(reportError(QString)), this, SLOT(forwardError(QString)));
emptyGenWidget* emptyGen = new emptyGenWidget(this, mod );
mainLayout->addWidget(emptyGen);
emptyGen->setVisible(false);
StateWidgets[3]=emptyGen;
waitStateWidget* waitGen = new waitStateWidget(this, mod );
mainLayout->addWidget(waitGen);
waitGen->setVisible(false);
StateWidgets[4]=waitGen;
stopGenWidget* stopGen = new stopGenWidget(this, mod );
mainLayout->addWidget(stopGen);
stopGen->setVisible(false);
StateWidgets[5]=stopGen;
connect(stopGen, SIGNAL(reportError(QString)), this, SLOT(forwardError(QString)));
iniSensorWidget* initSensor = new iniSensorWidget(this, mod );
mainLayout->addWidget(initSensor);
initSensor->setVisible(false);
StateWidgets[6]=initSensor;
getSensorWidget* getSensorReading = new getSensorWidget(this, mod );
mainLayout->addWidget(getSensorReading);
getSensorReading->setVisible(false);
StateWidgets[7]=getSensorReading;
if(tmpWidget==SYSTEM_INITIALIZATION)
{
stateNameEdit->setText("INIT");
stateNameEdit->setDisabled(true);
}
mainLayout->addLayout(bottomLayout);
}
void StateWidget::lengthChanged(QString newString)
{
if(newString.size()>0)
{
//InsertButton->setDisabled(false);
}
else
{
//InsertButton->setDisabled(true);
}
}
void StateWidget::AcceptState()
{
if(edited==NULL)
{
emit reportError(QString("Please, select a state to edit, edit it and then press OK."));
return;
}
if ( !StateNameOK())
{
return;
}
BaseState * toInsertState = StateWidgets[tmpWidget]->getStateObject();
if(toInsertState==NULL)
return;
toInsertState->setName(this->stateNameEdit->text());
toInsertState->setType(StateType(stateTypeCombo->currentIndex()));
toInsertState->setParameters(paramEdit->text());
OKButton->setDisabled(true);
BaseState * tmp = edited;
edited = NULL;
emit ReplaceState(tmp, toInsertState);
}
void StateWidget::setStateSubclass(int chosen)
{
if(tmpWidget==SYSTEM_INITIALIZATION)
{
stateNameEdit->setEnabled(true);
}
if(chosen==SYSTEM_INITIALIZATION)
{
stateNameEdit->setText("INIT");
stateNameEdit->setDisabled(true);
}
StateWidgets[tmpWidget]->setVisible(false);
tmpWidget = chosen;
StateWidgets[tmpWidget]->setVisible(true);
}
/*void StateWidget::InsertState()
{
edited = NULL;
OKButton->setDisabled(true);
if ( !StateNameOK())
{
return;
}
BaseState * toInsertState = StateWidgets[tmpWidget]->getStateObject();
if(toInsertState==NULL)
return;
toInsertState->setName(this->stateNameEdit->text());
toInsertState->setType(StateType(stateTypeCombo->currentIndex()));
toInsertState->setParameters(paramEdit->text());
emit InsertState(toInsertState);
}*/
void StateWidget::refreshData()
{
}
void StateWidget::StateSelected(BaseState * state)
{
if(state->getName().toLower()==QString("_end_")||state->getName().toLower()==QString("_stop_"))return;
edited = state;
OKButton->setDisabled(false);
stateNameEdit->setText(state->getName());
paramEdit->setText(state->getParameters());
stateTypeCombo->setCurrentIndex(state->getType());
StateWidgets[tmpWidget]->setVisible(false);
if(state->getType()<STATE_TYPES_NUMBER)
{
tmpWidget = state->getType();
StateWidgets[tmpWidget]->setVisible(true);
StateWidgets[tmpWidget]->setState(state);
}
}
bool StateWidget::StateNameOK()
{
if (stateNameEdit->text().toLower()=="init" && stateTypeCombo->currentIndex()!=SYSTEM_INITIALIZATION)
{
emit reportError(QString("INIT name is dedicated only to SYSTEM_INITIALIZATION state"));
return false;
}
if(stateNameEdit->text().toLower()=="_end_"||stateNameEdit->text().toLower()=="_stop_")
{
emit reportError(QString("_STOP_ and _END_ names are restricted(can be only used as ending states from the upper panel"));
return false;
}
if(stateNameEdit->text().toLower().contains("<<"))
{
emit reportError(QString("The StateName cannot contain \"<<\""));
return false;
}
if(stateNameEdit->text().toLower().contains("/"))
{
emit reportError(QString("The StateName cannot contain \"/\""));
return false;
}
if(mod->getState(stateNameEdit->text()) && edited->getName()!=stateNameEdit->text())
{
emit reportError(QString("The StateName is already in use"));
return false;
}
return true;
}