Validate math formula immediately and show error message

This commit is contained in:
Jan Käberich 2025-02-24 13:10:52 +01:00
parent 0966466686
commit 269c7b9859
5 changed files with 37 additions and 10 deletions

View file

@ -408,10 +408,10 @@ void Trace::setMathFormula(const QString &newMathFormula)
scheduleMathCalculation(0, data.size());
}
bool Trace::mathFormularValid() const
QString Trace::getMathFormulaError() const
{
if(mathFormula.isEmpty()) {
return false;
return "Math formula must not be empty";
}
try {
ParserX parser(pckCOMMON | pckUNIT | pckCOMPLEX);
@ -428,15 +428,15 @@ bool Trace::mathFormularValid() const
}
}
if(!found) {
return false;
return "Unknown variable: "+varName;
}
}
} catch (const ParserError &e) {
// parser error occurred
return false;
return "Parsing failed: " + QString::fromStdString(e.GetMsg());
}
// all variables used in the expression are set as math sources
return true;
return "";
}
bool Trace::resolveMathSourceHashes()

View file

@ -166,7 +166,7 @@ public:
const QString &getMathFormula() const;
void setMathFormula(const QString &newMathFormula);
bool mathFormularValid() const;
QString getMathFormulaError() const;
// When loading setups, some traces may be used as a math source before they are loaded.
// If that happens, their hashes are added to a list. Call this function for every new trace

View file

@ -56,7 +56,7 @@ TraceEditDialog::TraceEditDialog(Trace &t, QWidget *parent) :
connect(ui->bMath, &QPushButton::clicked, [&](bool math){
if(math) {
ui->stack->setCurrentIndex(3);
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(t.mathFormularValid());
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(updateMathFormulaStatus());
ui->impedance->setEnabled(true);
}
});
@ -146,10 +146,11 @@ TraceEditDialog::TraceEditDialog(Trace &t, QWidget *parent) :
connect(ui->csvImport, &CSVImport::filenameChanged, updateCSVFileStatus);
// Math source configuration
ui->lMathFormula->setText(t.getMathFormula());
if(t.getModel()) {
connect(ui->lMathFormula, &QLineEdit::textChanged, [&](){
t.setMathFormula(ui->lMathFormula->text());
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(t.mathFormularValid());
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(updateMathFormulaStatus());
});
ui->mathTraceTable->setColumnCount(2);
@ -212,7 +213,7 @@ TraceEditDialog::TraceEditDialog(Trace &t, QWidget *parent) :
t.addMathSource(trace, item->text());
}
ui->mathTraceTable->blockSignals(false);
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(t.mathFormularValid());
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(updateMathFormulaStatus());
});
}
@ -381,6 +382,21 @@ void TraceEditDialog::okClicked()
delete this;
}
bool TraceEditDialog::updateMathFormulaStatus()
{
auto error = trace.getMathFormulaError();
if(error.isEmpty()) {
// all good
ui->lMathFormulaStatus->setText("Math formula valid");
ui->lMathFormulaStatus->setStyleSheet("");
return true;
} else {
ui->lMathFormulaStatus->setText(error);
ui->lMathFormulaStatus->setStyleSheet("QLabel { color : red; }");
return false;
}
}
MathModel::MathModel(Trace &t, QObject *parent)
: QAbstractTableModel(parent),
t(t)

View file

@ -49,6 +49,7 @@ private slots:
void okClicked();
private:
bool updateMathFormulaStatus();
Ui::TraceEditDialog *ui;
Trace &trace;
bool VNAtrace;

View file

@ -143,7 +143,7 @@
<item>
<widget class="QStackedWidget" name="stack">
<property name="currentIndex">
<number>0</number>
<number>3</number>
</property>
<widget class="QWidget" name="LivePage">
<layout class="QFormLayout" name="formLayout_2">
@ -239,6 +239,16 @@
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="lMathFormulaStatus">
<property name="text">
<string/>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</widget>