Skip to content

Commit 269c7b9

Browse files
committed
Validate math formula immediately and show error message
1 parent 0966466 commit 269c7b9

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

Software/PC_Application/LibreVNA-GUI/Traces/trace.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,10 @@ void Trace::setMathFormula(const QString &newMathFormula)
408408
scheduleMathCalculation(0, data.size());
409409
}
410410

411-
bool Trace::mathFormularValid() const
411+
QString Trace::getMathFormulaError() const
412412
{
413413
if(mathFormula.isEmpty()) {
414-
return false;
414+
return "Math formula must not be empty";
415415
}
416416
try {
417417
ParserX parser(pckCOMMON | pckUNIT | pckCOMPLEX);
@@ -428,15 +428,15 @@ bool Trace::mathFormularValid() const
428428
}
429429
}
430430
if(!found) {
431-
return false;
431+
return "Unknown variable: "+varName;
432432
}
433433
}
434434
} catch (const ParserError &e) {
435435
// parser error occurred
436-
return false;
436+
return "Parsing failed: " + QString::fromStdString(e.GetMsg());
437437
}
438438
// all variables used in the expression are set as math sources
439-
return true;
439+
return "";
440440
}
441441

442442
bool Trace::resolveMathSourceHashes()

Software/PC_Application/LibreVNA-GUI/Traces/trace.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class Trace : public TraceMath
166166

167167
const QString &getMathFormula() const;
168168
void setMathFormula(const QString &newMathFormula);
169-
bool mathFormularValid() const;
169+
QString getMathFormulaError() const;
170170

171171
// When loading setups, some traces may be used as a math source before they are loaded.
172172
// If that happens, their hashes are added to a list. Call this function for every new trace

Software/PC_Application/LibreVNA-GUI/Traces/traceeditdialog.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ TraceEditDialog::TraceEditDialog(Trace &t, QWidget *parent) :
5656
connect(ui->bMath, &QPushButton::clicked, [&](bool math){
5757
if(math) {
5858
ui->stack->setCurrentIndex(3);
59-
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(t.mathFormularValid());
59+
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(updateMathFormulaStatus());
6060
ui->impedance->setEnabled(true);
6161
}
6262
});
@@ -146,10 +146,11 @@ TraceEditDialog::TraceEditDialog(Trace &t, QWidget *parent) :
146146
connect(ui->csvImport, &CSVImport::filenameChanged, updateCSVFileStatus);
147147

148148
// Math source configuration
149+
ui->lMathFormula->setText(t.getMathFormula());
149150
if(t.getModel()) {
150151
connect(ui->lMathFormula, &QLineEdit::textChanged, [&](){
151152
t.setMathFormula(ui->lMathFormula->text());
152-
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(t.mathFormularValid());
153+
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(updateMathFormulaStatus());
153154
});
154155

155156
ui->mathTraceTable->setColumnCount(2);
@@ -212,7 +213,7 @@ TraceEditDialog::TraceEditDialog(Trace &t, QWidget *parent) :
212213
t.addMathSource(trace, item->text());
213214
}
214215
ui->mathTraceTable->blockSignals(false);
215-
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(t.mathFormularValid());
216+
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(updateMathFormulaStatus());
216217
});
217218
}
218219

@@ -381,6 +382,21 @@ void TraceEditDialog::okClicked()
381382
delete this;
382383
}
383384

385+
bool TraceEditDialog::updateMathFormulaStatus()
386+
{
387+
auto error = trace.getMathFormulaError();
388+
if(error.isEmpty()) {
389+
// all good
390+
ui->lMathFormulaStatus->setText("Math formula valid");
391+
ui->lMathFormulaStatus->setStyleSheet("");
392+
return true;
393+
} else {
394+
ui->lMathFormulaStatus->setText(error);
395+
ui->lMathFormulaStatus->setStyleSheet("QLabel { color : red; }");
396+
return false;
397+
}
398+
}
399+
384400
MathModel::MathModel(Trace &t, QObject *parent)
385401
: QAbstractTableModel(parent),
386402
t(t)

Software/PC_Application/LibreVNA-GUI/Traces/traceeditdialog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ private slots:
4949
void okClicked();
5050

5151
private:
52+
bool updateMathFormulaStatus();
5253
Ui::TraceEditDialog *ui;
5354
Trace &trace;
5455
bool VNAtrace;

Software/PC_Application/LibreVNA-GUI/Traces/traceeditdialog.ui

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
<item>
144144
<widget class="QStackedWidget" name="stack">
145145
<property name="currentIndex">
146-
<number>0</number>
146+
<number>3</number>
147147
</property>
148148
<widget class="QWidget" name="LivePage">
149149
<layout class="QFormLayout" name="formLayout_2">
@@ -239,6 +239,16 @@
239239
</item>
240240
</layout>
241241
</item>
242+
<item>
243+
<widget class="QLabel" name="lMathFormulaStatus">
244+
<property name="text">
245+
<string/>
246+
</property>
247+
<property name="wordWrap">
248+
<bool>true</bool>
249+
</property>
250+
</widget>
251+
</item>
242252
</layout>
243253
</widget>
244254
</widget>

0 commit comments

Comments
 (0)