Don't mess up user formatting if eval'ing didn't actually change value.

Fixes https://gitlab.com/kicad/code/kicad/issues/13989

(cherry picked from commit 8059edff0e)
This commit is contained in:
Jeff Young 2023-02-24 00:37:00 +00:00
parent f942b4c657
commit 5e7cc30564
4 changed files with 32 additions and 12 deletions

View File

@ -859,7 +859,10 @@ void SIM_MODEL::doSetParamValue( int aParamIndex, const std::string& aValue )
void SIM_MODEL::SetParamValue( int aParamIndex, const std::string& aValue, void SIM_MODEL::SetParamValue( int aParamIndex, const std::string& aValue,
SIM_VALUE::NOTATION aNotation ) SIM_VALUE::NOTATION aNotation )
{ {
doSetParamValue( aParamIndex, SIM_VALUE::Normalize( aValue, aNotation ) ); // Note: also converts decimal separators to '.'
std::string value = SIM_VALUE::ConvertNotation( aValue, aNotation, SIM_VALUE::NOTATION::SI );
doSetParamValue( aParamIndex, value );
} }

View File

@ -158,16 +158,24 @@ bool SIM_STRING_PROPERTY::StringToValue( wxVariant& aVariant, const wxString& aT
if( m_disabled ) if( m_disabled )
return false; return false;
wxString text; wxString text = aText;
if( allowEval() && m_needsEval && m_eval.Process( aText ) ) if( allowEval() && m_needsEval && m_eval.Process( aText ) )
text = m_eval.Result(); {
double value = SIM_VALUE::ToDouble( m_eval.Result().ToStdString() );
if( isnan( value ) || value == SIM_VALUE::ToDouble( aText.ToStdString() ) )
{
// Don't mess up user formatting if eval'ing didn't actually change the value.
}
else else
text = aText; {
text = SIM_VALUE::Normalize( value );
m_model.SetParamValue( m_paramIndex, std::string( text.ToUTF8() ) ); }
aVariant = GetParam().value; }
m_model.SetParamValue( m_paramIndex, text.ToStdString() );
aVariant = text.ToStdString();
return true; return true;
} }

View File

@ -395,6 +395,19 @@ std::string SIM_VALUE::ConvertNotation( const std::string& aString, NOTATION aFr
} }
std::string SIM_VALUE::Normalize( double aValue )
{
double exponent = std::log10( std::abs( aValue ) );
int expReduction = 0;
std::string prefix = SIM_VALUE_PARSER::ExponentToUnitPrefix( exponent, expReduction,
NOTATION::SI );
double reducedValue = aValue / std::pow( 10, expReduction );
return fmt::format( "{:g}{}", reducedValue, prefix );
}
double SIM_VALUE::ToDouble( const std::string& aString, double aDefault ) double SIM_VALUE::ToDouble( const std::string& aString, double aDefault )
{ {
SIM_VALUE_PARSER::PARSE_RESULT parseResult = SIM_VALUE_PARSER::Parse( aString, NOTATION::SI ); SIM_VALUE_PARSER::PARSE_RESULT parseResult = SIM_VALUE_PARSER::Parse( aString, NOTATION::SI );

View File

@ -79,11 +79,7 @@ public:
static std::string ConvertNotation( const std::string& aString, NOTATION aFromNotation, static std::string ConvertNotation( const std::string& aString, NOTATION aFromNotation,
NOTATION aToNotation ); NOTATION aToNotation );
static std::string Normalize( const std::string& aString, NOTATION aNotation ) static std::string Normalize( double aValue );
{
// Note: also converts decimal separators to '.'
return ConvertNotation( aString, aNotation, NOTATION::SI );
}
static std::string ToSpice( const std::string& aString ) static std::string ToSpice( const std::string& aString )
{ {