From 30f8f1e6cb3e5d89c5d6c973939d3b4185dbe5f8 Mon Sep 17 00:00:00 2001 From: "Daniel Pettersson (dpettersson)" Date: Tue, 2 Oct 2018 21:10:50 +0200 Subject: [PATCH] Closes #158. Imported the patch from develop --- src/BtLineEdit.cpp | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/BtLineEdit.cpp b/src/BtLineEdit.cpp index eeb092cc0..324e2b793 100644 --- a/src/BtLineEdit.cpp +++ b/src/BtLineEdit.cpp @@ -41,7 +41,7 @@ BtLineEdit::BtLineEdit(QWidget *parent, Unit::UnitType type) : _forceUnit = Unit::noUnit; _forceScale = Unit::noScale; */ - + connect(this,SIGNAL(editingFinished()),this,SLOT(lineChanged())); } @@ -142,7 +142,7 @@ double BtLineEdit::toSI(Unit::unitDisplay oldUnit,Unit::unitScale oldScale,bool // get the qstringToSI() from the unit system, using the found unit. // Force the issue in qstringToSI() unless dspScale is Unit::noScale. - + return temp->qstringToSI(text(), works, dspScale != Unit::noScale, dspScale); } else if ( _type == Unit::String ) @@ -179,7 +179,7 @@ double BtLineEdit::toDouble(bool* ok) { QRegExp amtUnit; - if ( ok ) + if ( ok ) *ok = true; // Make sure we get the right decimal point (. or ,) and the right grouping // separator (, or .). Some locales write 1.000,10 and other write @@ -215,12 +215,19 @@ void BtLineEdit::setText( BeerXMLElement* element, int precision ) display = element->property(_editField.toLatin1().constData()).toString(); else if ( element->property(_editField.toLatin1().constData()).canConvert(QVariant::Double) ) { - // Get the amount bool ok = false; - QString tmp = element->property(_editField.toLatin1().constData()).toString(); - amount = Brewtarget::toDouble(tmp, &ok); - if ( !ok ) - Brewtarget::logW( QString("%1 could not convert %2 (%3:%4) to double").arg(Q_FUNC_INFO).arg(tmp).arg(_section).arg(_editField) ); + // Get the value from the element, and put it in a QVariant + QVariant tmp = element->property(_editField.toLatin1().constData()); + // It is important here to use QVariant::toDouble() instead of going + // through toString() and then Brewtarget::toDouble(). + amount = tmp.toDouble(&ok); + if ( !ok ) { + Brewtarget::logW( QString("%1 could not convert %2 (%3:%4) to double") + .arg(Q_FUNC_INFO) + .arg(tmp.toString()) + .arg(_section) + .arg(_editField) ); + } display = displayAmount(amount, precision); } @@ -256,7 +263,7 @@ void BtLineEdit::setText( QVariant amount, int precision) int BtLineEdit::type() const { return (int)_type; } QString BtLineEdit::editField() const { return _editField; } QString BtLineEdit::configSection() -{ +{ if ( _section.isEmpty() ) { setConfigSection(""); } @@ -266,8 +273,8 @@ QString BtLineEdit::configSection() // Once we require >qt5.5, we can replace this noise with // QMetaEnum::fromType() -QString BtLineEdit::forcedUnit() const -{ +QString BtLineEdit::forcedUnit() const +{ const QMetaObject &mo = Unit::staticMetaObject; int index = mo.indexOfEnumerator("unitDisplay"); QMetaEnum unitEnum = mo.enumerator(index); @@ -276,7 +283,7 @@ QString BtLineEdit::forcedUnit() const } QString BtLineEdit::forcedScale() const -{ +{ const QMetaObject &mo = Unit::staticMetaObject; int index = mo.indexOfEnumerator("unitScale"); QMetaEnum scaleEnum = mo.enumerator(index); @@ -288,20 +295,20 @@ void BtLineEdit::setType(int type) { _type = (Unit::UnitType)type;} void BtLineEdit::setEditField( QString editField) { _editField = editField; } // The cascade looks a little odd, but it is intentional. -void BtLineEdit::setConfigSection( QString configSection) +void BtLineEdit::setConfigSection( QString configSection) { - _section = configSection; + _section = configSection; if ( _section.isEmpty() ) _section = btParent->property("configSection").toString(); - if ( _section.isEmpty() ) + if ( _section.isEmpty() ) _section = btParent->objectName(); } // previous comment about qt5.5 applies -void BtLineEdit::setForcedUnit( QString forcedUnit ) -{ +void BtLineEdit::setForcedUnit( QString forcedUnit ) +{ const QMetaObject &mo = Unit::staticMetaObject; int index = mo.indexOfEnumerator("unitDisplay"); QMetaEnum unitEnum = mo.enumerator(index); @@ -309,8 +316,8 @@ void BtLineEdit::setForcedUnit( QString forcedUnit ) _forceUnit = (Unit::unitDisplay)unitEnum.keyToValue(forcedUnit.toStdString().c_str()); } -void BtLineEdit::setForcedScale( QString forcedScale ) -{ +void BtLineEdit::setForcedScale( QString forcedScale ) +{ const QMetaObject &mo = Unit::staticMetaObject; int index = mo.indexOfEnumerator("unitScale"); QMetaEnum unitEnum = mo.enumerator(index);