Skip to content

Commit

Permalink
Closes Brewtarget#158. Imported the patch from develop
Browse files Browse the repository at this point in the history
  • Loading branch information
dpettersson committed Oct 2, 2018
1 parent 6001561 commit 30f8f1e
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/BtLineEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ BtLineEdit::BtLineEdit(QWidget *parent, Unit::UnitType type) :
_forceUnit = Unit::noUnit;
_forceScale = Unit::noScale;
*/

connect(this,SIGNAL(editingFinished()),this,SLOT(lineChanged()));
}

Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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("");
}
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -288,29 +295,29 @@ 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);

_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);
Expand Down

0 comments on commit 30f8f1e

Please sign in to comment.