Skip to content

Commit

Permalink
Refs #6616. Clear error only when the parameter was really changed
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbekasov committed Mar 12, 2014
1 parent 20e48a9 commit 78e2469
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
30 changes: 25 additions & 5 deletions Code/Mantid/QtPropertyBrowser/src/DoubleEditorFactory.cpp
Expand Up @@ -38,9 +38,7 @@ DoubleEditor::~DoubleEditor()

void DoubleEditor::setValue(const double& d)
{
double absVal = fabs(d);
char format = absVal > 1e5 || (absVal != 0 && absVal < 1e-5) ? 'e' : 'f';
setText(QString::number(d,format , m_decimals));
setText(formatValue(d));
}

void DoubleEditor::updateProperty()
Expand All @@ -52,13 +50,35 @@ void DoubleEditor::updateProperty()
}
}

/**
* @param d :: The value to format
* @return Formatted string representation of the value
*/
QString DoubleEditor::formatValue(const double& d) const
{
// XXX: this is taken from QtDoublePropertyManager. Ideally, QtDoublePropertyManager should be
// refactored to haves such a method, but it doesn't seem to be a good idea to edit it heavily.
double absVal = fabs(d);
char format = absVal > 1e5 || (absVal != 0 && absVal < 1e-5) ? 'e' : 'f';
return QString::number(d, format, m_decimals);
}

void ParameterEditor::updateProperty()
{
auto mgr = dynamic_cast<ParameterPropertyManager*>(m_property->propertyManager());
if (mgr)
{
// As the property get supdated, the error becomes invalid, so clear it
mgr->clearError(m_property);
// To find out whether the value was really changed, we format it and compare string values. This
// ensures that we don't suffer from double precision loss and consider only real changes in value

QString prevVal = formatValue(mgr->value(m_property));
QString newVal = formatValue(text().toDouble());

if ( prevVal != newVal )
{
// As the value was changed, the error becomes invalid, so clear it
mgr->clearError(m_property);
}
}

DoubleEditor::updateProperty();
Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/QtPropertyBrowser/src/DoubleEditorFactory.h
Expand Up @@ -53,6 +53,9 @@ class QT_QTPROPERTYBROWSER_EXPORT DoubleEditor: public QLineEdit
protected slots:
virtual void updateProperty();
protected:
/// Returns string representation of the value, using the format of the property
QString formatValue(const double& d) const;

QtProperty* m_property;
int m_decimals;
};
Expand Down

0 comments on commit 78e2469

Please sign in to comment.