From 5e7cc30564ec261260bddcfd68b82fce7d5827a9 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Fri, 24 Feb 2023 00:37:00 +0000 Subject: [PATCH] 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 8059edff0eb36a7d4263255a66b8f2cb45b8ebad) --- eeschema/sim/sim_model.cpp | 5 ++++- eeschema/sim/sim_property.cpp | 20 ++++++++++++++------ eeschema/sim/sim_value.cpp | 13 +++++++++++++ eeschema/sim/sim_value.h | 6 +----- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/eeschema/sim/sim_model.cpp b/eeschema/sim/sim_model.cpp index 74686286ac..9332d71a17 100644 --- a/eeschema/sim/sim_model.cpp +++ b/eeschema/sim/sim_model.cpp @@ -859,7 +859,10 @@ void SIM_MODEL::doSetParamValue( int aParamIndex, const std::string& aValue ) void SIM_MODEL::SetParamValue( int aParamIndex, const std::string& aValue, 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 ); } diff --git a/eeschema/sim/sim_property.cpp b/eeschema/sim/sim_property.cpp index eba8d7e953..01788333dd 100644 --- a/eeschema/sim/sim_property.cpp +++ b/eeschema/sim/sim_property.cpp @@ -158,16 +158,24 @@ bool SIM_STRING_PROPERTY::StringToValue( wxVariant& aVariant, const wxString& aT if( m_disabled ) return false; - wxString text; + wxString text = aText; if( allowEval() && m_needsEval && m_eval.Process( aText ) ) - text = m_eval.Result(); - else - text = aText; + { + double value = SIM_VALUE::ToDouble( m_eval.Result().ToStdString() ); - m_model.SetParamValue( m_paramIndex, std::string( text.ToUTF8() ) ); - aVariant = GetParam().value; + 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 + { + text = SIM_VALUE::Normalize( value ); + } + } + m_model.SetParamValue( m_paramIndex, text.ToStdString() ); + aVariant = text.ToStdString(); return true; } diff --git a/eeschema/sim/sim_value.cpp b/eeschema/sim/sim_value.cpp index 2e30b52fdd..7007ba26ae 100644 --- a/eeschema/sim/sim_value.cpp +++ b/eeschema/sim/sim_value.cpp @@ -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 ) { SIM_VALUE_PARSER::PARSE_RESULT parseResult = SIM_VALUE_PARSER::Parse( aString, NOTATION::SI ); diff --git a/eeschema/sim/sim_value.h b/eeschema/sim/sim_value.h index 521c841a8f..2f9f23618c 100644 --- a/eeschema/sim/sim_value.h +++ b/eeschema/sim/sim_value.h @@ -79,11 +79,7 @@ public: static std::string ConvertNotation( const std::string& aString, NOTATION aFromNotation, NOTATION aToNotation ); - static std::string Normalize( const std::string& aString, NOTATION aNotation ) - { - // Note: also converts decimal separators to '.' - return ConvertNotation( aString, aNotation, NOTATION::SI ); - } + static std::string Normalize( double aValue ); static std::string ToSpice( const std::string& aString ) {