Bugfix: prevent crash on empty input

This commit is contained in:
Jan Käberich 2020-11-15 00:21:09 +01:00
parent 3055564a27
commit 4deaddf5d3
4 changed files with 27 additions and 27 deletions

View File

@ -81,12 +81,8 @@ bool SIUnitEdit::eventFilter(QObject *, QEvent *event)
} }
} }
} else if(event->type() == QEvent::FocusOut) { } else if(event->type() == QEvent::FocusOut) {
if(!text().isEmpty()) { parseNewValue(1.0);
parseNewValue(1.0); emit focusLost();
} else {
setValueQuiet(_value);
emit editingAborted();
}
} }
return false; return false;
} }
@ -101,25 +97,29 @@ void SIUnitEdit::setValueQuiet(double value)
void SIUnitEdit::parseNewValue(double factor) void SIUnitEdit::parseNewValue(double factor)
{ {
QString input = text(); QString input = text();
// remove optional unit if(input.isEmpty()) {
if(input.endsWith(unit)) { setValueQuiet(_value);
input.chop(unit.size()); emit editingAborted();
}
auto lastChar = input.at(input.size()-1).toLatin1();
if(prefixes.indexOf(lastChar) >= 0) {
factor = Unit::SIPrefixToFactor(lastChar);
input.chop(1);
}
// remaining input should only contain numbers
bool conversion_ok;
auto v = input.toDouble(&conversion_ok);
if(conversion_ok) {
qDebug() << v;
setValue(v * factor);
} else { } else {
qWarning() << "SIUnit conversion failure:" << input; // remove optional unit
if(input.endsWith(unit)) {
input.chop(unit.size());
}
auto lastChar = input.at(input.size()-1).toLatin1();
if(prefixes.indexOf(lastChar) >= 0) {
factor = Unit::SIPrefixToFactor(lastChar);
input.chop(1);
}
// remaining input should only contain numbers
bool conversion_ok;
auto v = input.toDouble(&conversion_ok);
if(conversion_ok) {
setValue(v * factor);
} else {
qWarning() << "SIUnit conversion failure:" << input;
}
clear();
} }
clear();
} }
void SIUnitEdit::continueEditing() void SIUnitEdit::continueEditing()

View File

@ -21,6 +21,7 @@ signals:
void valueChanged(double newvalue); void valueChanged(double newvalue);
void valueUpdated(QWidget *w); void valueUpdated(QWidget *w);
void editingAborted(); void editingAborted();
void focusLost();
protected: protected:
bool eventFilter(QObject *obj, QEvent *event) override; bool eventFilter(QObject *obj, QEvent *event) override;
private: private:

View File

@ -499,11 +499,11 @@ SIUnitEdit *TraceMarker::getSettingsEditor()
case Type::Noise: case Type::Noise:
case Type::PhaseNoise: case Type::PhaseNoise:
default: default:
return new SIUnitEdit("Hz", " kMG"); return new SIUnitEdit("Hz", " kMG", 6);
case Type::Lowpass: case Type::Lowpass:
case Type::Highpass: case Type::Highpass:
case Type::PeakTable: case Type::PeakTable:
return new SIUnitEdit("db", " "); return new SIUnitEdit("db", " ", 3);
case Type::TOI: case Type::TOI:
return nullptr; return nullptr;
} }

View File

@ -316,7 +316,7 @@ QWidget *MarkerSettingsDelegate::createEditor(QWidget *parent, const QStyleOptio
e->setMaximumHeight(rowHeight); e->setMaximumHeight(rowHeight);
e->setParent(parent); e->setParent(parent);
connect(e, &SIUnitEdit::valueUpdated, this, &MarkerSettingsDelegate::commitData); connect(e, &SIUnitEdit::valueUpdated, this, &MarkerSettingsDelegate::commitData);
connect(e, &SIUnitEdit::editingAborted, [=](){ connect(e, &SIUnitEdit::focusLost, [=](){
marker->editingFrequeny = false; marker->editingFrequeny = false;
}); });
} }
@ -327,7 +327,6 @@ void MarkerSettingsDelegate::setModelData(QWidget *editor, QAbstractItemModel *m
{ {
auto markerModel = (TraceMarkerModel*) model; auto markerModel = (TraceMarkerModel*) model;
auto marker = markerModel->markerFromIndex(index); auto marker = markerModel->markerFromIndex(index);
marker->editingFrequeny = false;
auto si = (SIUnitEdit*) editor; auto si = (SIUnitEdit*) editor;
markerModel->setData(index, si->value()); markerModel->setData(index, si->value());
} }