-
Notifications
You must be signed in to change notification settings - Fork 6
/
robotvaredit.cpp
453 lines (382 loc) · 15.6 KB
/
robotvaredit.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/***************************************************************************
* Copyright (C) 2007 by Massimiliano Fago *
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/*! \file
* \class RobotVarEdit
* \author Massimiliano Fago
* \version 1.0
* \date 2007
* \brief Edit variabile
*
*/
#include "robotvaredit.h"
RobotVarEdit::RobotVarEdit(const QByteArray& variabile, const QByteArray& varname, const QHostAddress varip, QWidget *parent)
: QDialog(parent)
{
this->varip = varip;
setWindowTitle(tr("Variable value edit"));
robotvar = new KukaVar(varname, variabile);
QVBoxLayout *topLayout = new QVBoxLayout();
QGridLayout *leftLayout = new QGridLayout();
QVBoxLayout *bottomLayout = new QVBoxLayout();
switch(robotvar->getVarType()){
case KukaVar::STRUCTURE:
{
//top labels
QLabel *titleLabel = new QLabel(tr("Variable edit: %1").arg(varname.data()));
titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
QLabel *structureLabel = new QLabel(tr("Variable type: %1").arg(robotvar->getStructureName().data()));
structureLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
topLayout->addWidget(titleLabel);
topLayout->addWidget(structureLabel);
for (int i=0;i<robotvar->getElementsNumber();i++)
{
int tipodato;
robotvar->getStructureValue(i,tipodato);
switch(tipodato){
case KukaVar::INT:
addInt(i,robotvar->getStructureMember(i), robotvar->getStructureValue(i,tipodato).toInt());
addMap(i,i,tipodato);
break;
case KukaVar::REAL:
addReal(i,robotvar->getStructureMember(i), robotvar->getStructureValue(i,tipodato).toDouble());
addMap(i,i,tipodato);
break;
case KukaVar::BOOL:
addBool(i,robotvar->getStructureMember(i), robotvar->getStructureValue(i,tipodato));
addMap(i,i,tipodato);
break;
case KukaVar::CHAR:
addChar(i,robotvar->getStructureMember(i), robotvar->getStructureValue(i,tipodato));
addMap(i,i,tipodato);
break;
default:
widget[i] = new QLineEdit(robotvar->getStructureValue(i,tipodato));
break;
}
leftLayout->addWidget(label[i],i,0);
leftLayout->addWidget(widget[i],i,1);
}
break;
}
case KukaVar::INT:
{
//INT
//top labels
QLabel *titleLabel = new QLabel(tr("Variable edit: %1").arg(varname.data()));
titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
QLabel *structureLabel = new QLabel(tr("Variable type: %1").arg(robotvar->getVarTypeName().data()));
structureLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
topLayout->addWidget(titleLabel);
topLayout->addWidget(structureLabel);
//topLayout->addStretch();
addInt(0,robotvar->getVarName(),robotvar->getValue().toInt());
leftLayout->addWidget(label[0],0,0);
leftLayout->addWidget(widget[0],0,1);
addMap(0,0,robotvar->getVarType());
break;
}
case KukaVar::REAL:
{
//REAL
//top labels
QLabel *titleLabel = new QLabel(tr("Variable edit: %1").arg(varname.data()));
titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
QLabel *structureLabel = new QLabel(tr("Variable type: %1").arg(robotvar->getVarTypeName().data()));
structureLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
topLayout->addWidget(titleLabel);
topLayout->addWidget(structureLabel);
//topLayout->addStretch();
addReal(0,robotvar->getVarName(),robotvar->getValue().toDouble());
leftLayout->addWidget(label[0],0,0);
leftLayout->addWidget(widget[0],0,1);
addMap(0,0,robotvar->getVarType());
break;
}
case KukaVar::BOOL:
{
//BOOL
//top labels
QLabel *titleLabel = new QLabel(tr("Variable edit: %1").arg(varname.data()));
titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
QLabel *structureLabel = new QLabel(tr("Variable type: %1").arg(robotvar->getVarTypeName().data()));
structureLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
topLayout->addWidget(titleLabel);
topLayout->addWidget(structureLabel);
//topLayout->addStretch();
addBool(0,robotvar->getVarName(),robotvar->getValue());
leftLayout->addWidget(label[0],0,0);
leftLayout->addWidget(widget[0],0,1);
addMap(0,0,robotvar->getVarType());
break;
}
case KukaVar::CHAR:
{
//CHAR
//top labels
QLabel *titleLabel = new QLabel(tr("Variable edit: %1").arg(varname.data()));
titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
QLabel *structureLabel = new QLabel(tr("Variable type: %1").arg(robotvar->getVarTypeName().data()));
structureLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
topLayout->addWidget(titleLabel);
topLayout->addWidget(structureLabel);
//topLayout->addStretch();
addChar(0,robotvar->getVarName(),robotvar->getValue());
leftLayout->addWidget(label[0],0,0);
leftLayout->addWidget(widget[0],0,1);
addMap(0,0,robotvar->getVarType());
break;
}
default:
{
delete robotvar;
return;
}
}
//bottom commands
QPushButton *okButton = new QPushButton(tr("OK"),this);
//QPushButton *abortButton = new QPushButton(tr("Abort"));
bottomLayout->addWidget(okButton);
//bottomLayout->addWidget(abortButton);
bottomLayout->addStretch();
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addLayout(topLayout);
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(bottomLayout);
setLayout(mainLayout);
//connection slots and signals
connect(okButton, SIGNAL(clicked()), this, SLOT(on_okButton()));
//connect(abortButton, SIGNAL(clicked()), this, SLOT(on_abortButton()));
}
RobotVarEdit::~RobotVarEdit()
{
qDebug() << "Chiamato distruttore";
}
void RobotVarEdit::on_okButton()
{
switch(robotvar->getVarType()){
case KukaVar::STRUCTURE:
{
//set new value for robotvar
for (int i=0;i<robotvar->getElementsNumber();i++)
{
if (QSpinBox* spinBox = dynamic_cast<QSpinBox*>(widget[i]))
{
robotvar->setFieldValue(((QSpinBox*)widget[i])->text().toAscii(),i);
}
else if (QDoubleSpinBox* doublespinBox = dynamic_cast<QDoubleSpinBox*>(widget[i]))
{
robotvar->setFieldValue(((QDoubleSpinBox*)widget[i])->text().replace(",",".").toAscii(),i);
}
else if (QLineEdit* lineEdit = dynamic_cast<QLineEdit*>(widget[i]))
{
robotvar->setFieldValue(((QLineEdit*)widget[i])->text().toAscii(),i);
}
else if (QComboBox* comboBox = dynamic_cast<QComboBox*>(widget[i]))
{
robotvar->setFieldValue(((QComboBox*)widget[i])->currentText().toAscii(),i);
}
else
qDebug() << "Nessuna delle precedenti";
}
//send new value to robot
emit writevalue(robotvar->getVarName(),QByteArray(robotvar->createStructure()), varip);
break;
}
case KukaVar::INT:
{
//INT
robotvar->setValue(((QSpinBox*)widget[0])->text().toAscii());
emit writevalue(robotvar->getVarName(),robotvar->getNewValue(), varip);
break;
}
case KukaVar::REAL:
{
//REAL
robotvar->setValue(((QDoubleSpinBox*)widget[0])->text().replace(",",".").toAscii());
emit writevalue(robotvar->getVarName(),robotvar->getNewValue(), varip);
break;
}
case KukaVar::BOOL:
{
//BOOL
robotvar->setValue(((QComboBox*)widget[0])->currentText().toAscii());
emit writevalue(robotvar->getVarName(),robotvar->getNewValue(), varip);
break;
}
case KukaVar::CHAR:
{
//CHAR
robotvar->setValue(((QLineEdit*)widget[0])->text().toAscii());
emit writevalue(robotvar->getVarName(),robotvar->getNewValue(), varip);
break;
}
default:
{
qDebug() << "ANOMALIA!!";
break;
}
}
on_Accept();
}
void RobotVarEdit::on_abortButton()
{
reject();
}
void RobotVarEdit::on_namedLineEdit_textChanged(const QString &text)
{
//okButton->setEnabled(lineEdit->hasAcceptableInput());
}
/*! \brief Modifica label descrizione
*
* Modifica il contenuto e il colore della label di descrizione
* Nella label dopo la modifica, viene messo tra parentesi il
* vecchio valore
*
* \param index Indice della label da modificare
*/
void RobotVarEdit::on_Changed(int index)
{
int datatype;
QPalette p = label[index]->palette();
p.setColor(QPalette::WindowText, Qt::red);
label[index]->setPalette(p);
label[index]->setAutoFillBackground(true);
if (robotvar->getVarType()==KukaVar::STRUCTURE)
(label[index])->setText(robotvar->getStructureMember(index) + " (" + robotvar->getStructureValue(index,datatype) + ")");
else
(label[index])->setText(robotvar->getVarName() + " (" + robotvar->getValue() + ")");
}
/*! \brief Ripristino label
*
* Ripristina il contenuto e il colore della label di descrizione
*
*/
void RobotVarEdit::on_Accept()
{
for(int i=0;i<robotvar->getElementsNumber();i++){
QPalette p = label[i]->palette();
p.setColor(QPalette::WindowText, Qt::black);
label[i]->setPalette(p);
label[i]->setAutoFillBackground(true);
if (robotvar->getVarType()==KukaVar::STRUCTURE)
(label[i])->setText(robotvar->getStructureMember(i));
else
(label[i])->setText(robotvar->getVarName());
}
}
/*! \brief Inserimento elemento di tipo intero
*
* Inserisce un elemento di tipo intero alla finestra
*
*/
void RobotVarEdit::addInt(int widgetindex, QByteArray varName, int value){
//qDebug() << "Indice widget intero: " << widgetindex << " valore: " << value;
label[widgetindex] = new QLabel(varName,this);
widget[widgetindex] = new QSpinBox(this);
((QSpinBox*)widget[widgetindex])->setRange(-(9999999),(9999999));
((QSpinBox*)widget[widgetindex])->setValue(value);
}
/*! \brief Inserimento elemento di tipo reale
*
* Inserisce un elemento di tipo real alla finestra
*
*/
void RobotVarEdit::addReal(int widgetindex, QByteArray varName, double value){
qDebug() << "Indice widget reale: " << widgetindex << " valore: " << value;
label[widgetindex] = new QLabel(varName,this);
widget[widgetindex] = new QDoubleSpinBox(this);
((QDoubleSpinBox*)widget[widgetindex])->setDecimals(5);
((QDoubleSpinBox*)widget[widgetindex])->setRange(-99999,99999);
((QDoubleSpinBox*)widget[widgetindex])->setValue(value);
}
/*! \brief Inserimento elemento di tipo char
*
* Inserisce un elemento di tipo char alla finestra
*
*/
void RobotVarEdit::addChar(int widgetindex, QByteArray varName, QByteArray value){
label[widgetindex] = new QLabel(varName,this);
widget[widgetindex] = new QLineEdit(value,this);
//QRegExp regExp("[A-Za-z0-9]{1-30}");
//((QLineEdit*)widget[widgetindex])->setValidator(new QRegExpValidator(regExp, this));
connect(widget[widgetindex], SIGNAL(textChanged(const QString &)), this, SLOT(on_namedLineEdit_textChanged(const QString &)));
}
/*! \brief Inserimento elemento di tipo intero
*
* Inserisce un elemento di tipo intero alla finestra
*
*/
void RobotVarEdit::addBool(int widgetindex, QByteArray varName, QByteArray value){
//qDebug() << "Indice widget: " << widgetindex << " valore: " << value;
label[widgetindex] = new QLabel(varName,this);
widget[widgetindex] = new QComboBox(this);
((QComboBox*)widget[widgetindex])->addItem("TRUE");
((QComboBox*)widget[widgetindex])->addItem("FALSE");
((QComboBox*)widget[widgetindex])->setCurrentIndex(((QComboBox*)widget[widgetindex])->findText(value));
}
/*! \brief Inserimento elemento di tipo struttura
*
* Inserisce un elemento di tipo struttura alla finestra
*
*/
void RobotVarEdit::addStructure(int widgetindex, int value){
//qDebug() << "Indice widget: " << widgetindex << " valore: " << value;
widget[widgetindex] = new QSpinBox(this);
((QSpinBox*)widget[widgetindex])->setRange(-(9999999),(9999999));
((QSpinBox*)widget[widgetindex])->setValue(value);
}
/*! \brief Inserimento elemento di tipo struttura
*
* Inserisce un elemento di tipo struttura alla finestra
*
*/
void RobotVarEdit::addMap(int widgetIndex, int mapIndex, int tipodato){
//qDebug() << "Indice widget: " << widgetIndex << " mapIndex: " << mapIndex << " tipo dato: " << tipodato;
mapper[mapIndex] = new QSignalMapper(this);
switch(tipodato){
case KukaVar::BOOL:
{
connect(widget[widgetIndex], SIGNAL(currentIndexChanged(const QString&)),mapper[mapIndex], SLOT(map()));
break;
}
case KukaVar::CHAR:
{
connect(widget[widgetIndex], SIGNAL(textChanged(const QString&)),mapper[mapIndex], SLOT(map()));
break;
}
default:
{
connect(widget[widgetIndex], SIGNAL(valueChanged(const QString&)),mapper[mapIndex], SLOT(map()));
break;
}
}
mapper[mapIndex]->setMapping(widget[widgetIndex], mapIndex);
connect(mapper[mapIndex], SIGNAL(mapped(int)), this, SLOT(on_Changed(int)));
}
void RobotVarEdit::closeEvent (QCloseEvent* event)
{
for (int i=0;i<robotvar->getElementsNumber();i++)
{
delete widget[i];
delete label[i];
delete mapper[i];
}
delete robotvar;
}