-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathparametereditor.cpp
585 lines (485 loc) · 16.6 KB
/
parametereditor.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/*
Original copyright 2018 Benjamin Vedder [email protected] and the VESC Tool project ( https://github.com/vedderb/vesc_tool )
Now forked to:
Danny Bokma [email protected]
This file is part of BMS Tool.
DieBieMS Tool 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.
DieBieMS Tool 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, see <http://www.gnu.org/licenses/>.
*/
#include "parametereditor.h"
#include "ui_parametereditor.h"
#include <QStringList>
#include <QMessageBox>
#include <QFileDialog>
#include <QInputDialog>
#include <QDebug>
#include <cmath>
ParameterEditor::ParameterEditor(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ParameterEditor)
{
ui->setupUi(this);
setEditorValues("new_parameter", ConfigParam());
mStatusInfoTime = 0;
mStatusLabel = new QLabel(this);
ui->statusBar->addPermanentWidget(mStatusLabel);
mTimer = new QTimer(this);
mTimer->start(20);
connect(mTimer, SIGNAL(timeout()),
this, SLOT(timerSlot()));
}
ParameterEditor::~ParameterEditor()
{
delete ui;
}
void ParameterEditor::setParams(const ConfigParams *params)
{
QStringList paramList = params->getParamOrder();
mParams.clearParams();
mParams.setSerializeOrder(params->getSerializeOrder());
for (int i = 0;i < paramList.size();i++) {
QString name = paramList.at(i);
mParams.addParam(name, params->getParamCopy(name));
}
mParams.setUpdatesEnabled(false);
showStatusInfo(tr("Parameters Loaded"), true);
updateUi();
}
void ParameterEditor::timerSlot()
{
// Update status label
if (mStatusInfoTime) {
mStatusInfoTime--;
if (!mStatusInfoTime) {
mStatusLabel->setStyleSheet(qApp->styleSheet());
}
} else {
if (!mStatusLabel->text().isEmpty()) {
mStatusLabel->clear();
}
}
}
void ParameterEditor::on_paramRemoveButton_clicked()
{
int row = ui->paramList->currentRow();
if (row >= 0) {
mParams.deleteParam(ui->paramList->item(row)->text());
delete ui->paramList->takeItem(row);
}
}
void ParameterEditor::on_paramDownButton_clicked()
{
int row = ui->paramList->currentRow();
QStringList order = mParams.getParamOrder();
if (row >= 0 && row < (order.size() - 1)) {
QString name = order.at(row);
order.removeAt(row);
order.insert(row + 1, name);
mParams.setParamOrder(order);
updateUi();
ui->paramList->setCurrentRow(row + 1);
}
}
void ParameterEditor::on_paramUpButton_clicked()
{
int row = ui->paramList->currentRow();
QStringList order = mParams.getParamOrder();
if (row >= 1) {
QString name = order.at(row);
order.removeAt(row);
order.insert(row - 1, name);
mParams.setParamOrder(order);
updateUi();
ui->paramList->setCurrentRow(row - 1);
}
}
void ParameterEditor::on_paramOpenButton_clicked()
{
QListWidgetItem *item = ui->paramList->currentItem();
QString selected;
if (item) {
selected = item->text();
}
ConfigParam *p = mParams.getParam(selected);
if (p) {
setEditorValues(selected, *p);
if (ui->previewTable->rowCount() > 0) {
ui->previewTable->removeRow(0);
}
if (p->type != CFG_T_UNDEFINED) {
ui->previewTable->addParamRow(&mParams, selected);
}
}
}
void ParameterEditor::on_paramSaveButton_clicked()
{
if (ui->nameEdit->text().contains(" ")) {
QMessageBox::warning(this,
tr("Save Parameter"),
tr("Spaces are not allowed in the name."));
return;
}
if (ui->nameEdit->text().isEmpty()) {
QMessageBox::warning(this,
tr("Save Parameter"),
tr("Name is empty."));
return;
}
QString name;
ConfigParam p;
name = getEditorValues(&p);
if (mParams.hasParam(name)) {
*mParams.getParam(name) = p;
showStatusInfo(tr("Parameter updated: %1").arg(name), true);
} else {
mParams.addParam(name, p);
int row = ui->paramList->currentRow();
QStringList order = mParams.getParamOrder();
order.removeLast();
if (row < 0) {
row = order.size();
} else {
row += 1;
}
order.insert(row, name);
mParams.setParamOrder(order);
updateUi();
ui->paramList->setCurrentRow(row);
showStatusInfo(tr("New parameter added: %1").arg(name), true);
}
if (ui->previewTable->rowCount() > 0) {
ui->previewTable->removeRow(0);
}
if (p.type != CFG_T_QSTRING && p.type != CFG_T_UNDEFINED) {
ui->previewTable->addParamRow(&mParams, name);
}
}
void ParameterEditor::on_paramResetButton_clicked()
{
setEditorValues("new_parameter", ConfigParam());
}
void ParameterEditor::on_serRemoveButton_clicked()
{
int row = ui->paramSerialOrderList->currentRow();
if (row >= 0) {
QStringList order = mParams.getSerializeOrder();
order.removeAt(row);
mParams.setSerializeOrder(order);
delete ui->paramSerialOrderList->takeItem(row);
}
}
void ParameterEditor::on_serDownButton_clicked()
{
int row = ui->paramSerialOrderList->currentRow();
QStringList order = mParams.getSerializeOrder();
if (row >= 0 && row < (order.size() - 1)) {
QString name = order.at(row);
order.removeAt(row);
order.insert(row + 1, name);
mParams.setSerializeOrder(order);
updateUi();
ui->paramSerialOrderList->setCurrentRow(row + 1);
}
}
void ParameterEditor::on_serUpButton_clicked()
{
int row = ui->paramSerialOrderList->currentRow();
QStringList order = mParams.getSerializeOrder();
if (row >= 1) {
QString name = order.at(row);
order.removeAt(row);
order.insert(row - 1, name);
mParams.setSerializeOrder(order);
updateUi();
ui->paramSerialOrderList->setCurrentRow(row - 1);
}
}
void ParameterEditor::on_serAddButton_clicked()
{
QListWidgetItem *item = ui->paramList->currentItem();
QString selected = "";
if (item) {
selected = item->text();
}
bool ok;
QString name = QInputDialog::getText(this,
tr("Add to Serialization Order"),
tr("Parameter Name"),
QLineEdit::Normal,
selected,
&ok);
if (ok) {
if (name.contains(" ")) {
QMessageBox::warning(this,
tr("Add"),
tr("Spaces are not allowed in the name."));
return;
}
if (name.isEmpty()) {
QMessageBox::warning(this,
tr("Add"),
tr("Name is empty."));
return;
}
int row = ui->paramSerialOrderList->currentRow();
QStringList order = mParams.getSerializeOrder();
if (row < 0 || row >= order.size()) {
row = order.size() - 1;
}
order.insert(row + 1, name);
mParams.setSerializeOrder(order);
updateUi();
}
}
void ParameterEditor::on_paramList_doubleClicked(const QModelIndex &index)
{
(void)index;
on_paramOpenButton_clicked();
}
void ParameterEditor::on_enumAddButton_clicked()
{
addEnum(tr("<double-click to edit>"));
}
void ParameterEditor::on_enumRemoveButton_clicked()
{
int row = ui->enumList->currentRow();
if (row >= 0) {
delete ui->enumList->takeItem(row);
}
}
void ParameterEditor::on_enumMoveUpButton_clicked()
{
int row = ui->enumList->currentRow();
if (row >= 1) {
QListWidgetItem *item = ui->enumList->takeItem(row);
ui->enumList->insertItem(row - 1, item);
ui->enumList->setCurrentRow(row - 1);
}
}
void ParameterEditor::on_enumMoveDownButton_clicked()
{
int row = ui->enumList->currentRow();
if (row >= 0 && row < (ui->enumList->count() - 1)) {
QListWidgetItem *item = ui->enumList->takeItem(row);
ui->enumList->insertItem(row + 1, item);
ui->enumList->setCurrentRow(row + 1);
}
}
void ParameterEditor::on_actionLoad_XML_triggered()
{
QString path;
path = QFileDialog::getOpenFileName(this,
tr("Choose parameter file to load"),
".",
tr("Xml files (*.xml)"));
if (path.isNull()) {
return;
}
bool res = mParams.loadParamsXml(path);
if (!res) {
QMessageBox::information(this,
tr("Load Parameters"),
tr("Could not load parameters:<BR>"
"%1").arg(mParams.xmlStatus()));
} else {
showStatusInfo(tr("Configuration Loaded"), true);
}
updateUi();
}
void ParameterEditor::on_actionSave_XML_as_triggered()
{
QString path;
path = QFileDialog::getSaveFileName(this,
tr("Choose where to save the parameter XML file"),
".",
tr("Xml files (*.xml)"));
if (path.isNull()) {
return;
}
if (!path.toLower().endsWith(".xml")) {
path += ".xml";
}
bool res = mParams.saveParamsXml(path);
if (!res) {
QMessageBox::information(this,
tr("Save Parameters"),
tr("Could not save parameters:<BR>"
"%1").arg(mParams.xmlStatus()));
} else {
showStatusInfo(tr("Configuration Saved"), true);
}
}
void ParameterEditor::on_actionDeleteAll_triggered()
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this,
tr("Delete All"),
tr("Are you sure that you want to delete all data?"),
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes) {
mParams.clearAll();
updateUi();
showStatusInfo(tr("Configuration Deleted"), true);
}
}
void ParameterEditor::updateUi()
{
ui->paramList->clear();
ui->paramList->addItems(mParams.getParamOrder());
ui->paramSerialOrderList->clear();
ui->paramSerialOrderList->addItems(mParams.getSerializeOrder());
}
void ParameterEditor::setEditorValues(QString name, ConfigParam p)
{
ui->nameEdit->setText(name);
ui->longNameEdit->setText(p.longName);
ui->typeBox->setCurrentIndex(p.type);
ui->transmittableBox->setCurrentIndex(p.transmittable ? 0 : 1);
ui->descriptionEdit->document()->setHtml(p.description);
ui->cDefineEdit->setText(p.cDefine);
// Double
ui->doubleEditorScaleBox->setValue(p.editorScale);
ui->doubleEditPercentageBox->setChecked(p.editAsPercentage);
ui->doubleDecimalsBox->setValue(p.editorDecimalsDouble);
ui->doubleMaxBox->setValue(p.maxDouble);
ui->doubleMinBox->setValue(p.minDouble);
ui->doubleShowDisplayBox->setChecked(p.showDisplay);
ui->doubleStep->setValue(p.stepDouble);
ui->doubleSuffixEdit->setText(p.suffix);
double val = p.vTxDoubleScale;
double power = floor(log10(val));
val /= pow(10, power);
ui->doubleTxScaleBaseBox->setValue(val);
ui->doubleTxScaleExpBox->setValue(power);
if (p.vTx == VESC_TX_DOUBLE16) {
ui->doubleTxTypeBox->setCurrentIndex(0);
} else if (p.vTx == VESC_TX_DOUBLE32) {
ui->doubleTxTypeBox->setCurrentIndex(1);
} else {
ui->doubleTxTypeBox->setCurrentIndex(2);
}
ui->doubleValBox->setValue(p.valDouble);
// Int
ui->intEditorScaleBox->setValue(p.editorScale);
ui->intEditPercentageBox->setChecked(p.editAsPercentage);
ui->intMaxBox->setValue(p.maxInt);
ui->intMinBox->setValue(p.minInt);
ui->intShowDisplayBox->setChecked(p.showDisplay);
ui->intStepBox->setValue(p.stepInt);
ui->intSuffixEdit->setText(p.suffix);
if (p.vTx <= 6 && p.vTx > 0) {
ui->intTxTypeBox->setCurrentIndex(p.vTx - 1);
}
ui->intValBox->setValue(p.valInt);
// String
ui->stringValEdit->setText(p.valString);
// Enum
ui->enumList->clear();
for (int i = 0;i < p.enumNames.size();i++) {
addEnum(p.enumNames.at(i));
}
if (p.valInt >= 0 && p.valInt < p.enumNames.size()) {
ui->enumList->setCurrentRow(p.valInt);
}
// Bool
ui->boolBox->setCurrentIndex(p.valInt > 0 ? 1 : 0);
}
QString ParameterEditor::getEditorValues(ConfigParam *p)
{
if (p) {
p->longName = ui->longNameEdit->text();
p->type = (CFG_T)ui->typeBox->currentIndex();
p->transmittable = ui->transmittableBox->currentIndex() == 0;
p->description = ui->descriptionEdit->toHtmlNoFontSize();
p->cDefine = ui->cDefineEdit->text();
switch (p->type) {
case CFG_T_DOUBLE:
p->editorScale = ui->doubleEditorScaleBox->value();
p->editAsPercentage = ui->doubleEditPercentageBox->isChecked();
p->editorDecimalsDouble = ui->doubleDecimalsBox->value();
p->maxDouble = ui->doubleMaxBox->value();
p->minDouble = ui->doubleMinBox->value();
p->showDisplay = ui->doubleShowDisplayBox->isChecked();
p->stepDouble = ui->doubleStep->value();
p->suffix = ui->doubleSuffixEdit->text();
p->vTxDoubleScale = ui->doubleTxScaleBaseBox->value() *
pow(10, ui->doubleTxScaleExpBox->value());
p->vTx = (VESC_TX_T)(ui->doubleTxTypeBox->currentIndex() + 7);
p->valDouble = ui->doubleValBox->value();
break;
case CFG_T_INT:
p->editorScale = ui->intEditorScaleBox->value();
p->editAsPercentage = ui->intEditPercentageBox->isChecked();
p->maxInt = ui->intMaxBox->value();
p->minInt = ui->intMinBox->value();
p->showDisplay = ui->intShowDisplayBox->isChecked();
p->stepInt = ui->intStepBox->value();
p->suffix = ui->intSuffixEdit->text();
p->vTx = (VESC_TX_T)(ui->intTxTypeBox->currentIndex() + 1);
p->valInt = ui->intValBox->value();
break;
case CFG_T_QSTRING:
p->valString = ui->stringValEdit->text();
break;
case CFG_T_ENUM:
p->enumNames.clear();
for (int i = 0;i < ui->enumList->count();i++) {
p->enumNames.append(ui->enumList->item(i)->text());
}
p->valInt = ui->enumList->currentRow();
break;
case CFG_T_BOOL:
p->valInt = ui->boolBox->currentIndex();
break;
default:
break;
}
}
return ui->nameEdit->text();
}
void ParameterEditor::addEnum(QString name)
{
QListWidgetItem *item = new QListWidgetItem(name);
item->setFlags (item->flags() | Qt :: ItemIsEditable);
ui->enumList->addItem(item);
}
void ParameterEditor::showStatusInfo(QString info, bool isGood)
{
if (isGood) {
mStatusLabel->setStyleSheet("QLabel { background-color : lightgreen; color : black; }");
} else {
mStatusLabel->setStyleSheet("QLabel { background-color : red; color : black; }");
}
mStatusInfoTime = 80;
mStatusLabel->setText(info);
}
void ParameterEditor::on_doubleTxTypeBox_currentIndexChanged(int index)
{
if (index == 2) {
ui->doubleTxScaleBaseBox->setEnabled(false);
ui->doubleTxScaleExpBox->setEnabled(false);
ui->doubleTxScaleExpLabel->setEnabled(false);
ui->doubleTxScaleLabel->setEnabled(false);
} else {
ui->doubleTxScaleBaseBox->setEnabled(true);
ui->doubleTxScaleExpBox->setEnabled(true);
ui->doubleTxScaleExpLabel->setEnabled(true);
ui->doubleTxScaleLabel->setEnabled(true);
}
}
void ParameterEditor::on_actionCalculatePacketSize_triggered()
{
VByteArray bytes;
mParams.serialize(bytes);
QMessageBox::information(this,
tr("Packet Size"),
tr("%1 Bytes").arg(bytes.size()));
}