Don't commit changes on paint events or updateUI events.

Also use std::numeric_limits::epsilon for comparing doubles.

Fixes https://gitlab.com/kicad/code/kicad/issues/13851
This commit is contained in:
Jeff Young 2023-03-04 00:08:41 +00:00
parent d1b7fa6b0f
commit d714ff8549
3 changed files with 24 additions and 19 deletions

View File

@ -121,8 +121,7 @@ bool SIM_STRING_PROPERTY::OnEvent( wxPropertyGrid* propgrid, wxWindow* wnd_prima
{
double value = SIM_VALUE::ToDouble( m_eval.Result().ToStdString() );
if( isnan( value )
|| value == SIM_VALUE::ToDouble( textEntry->GetValue().ToStdString() ) )
if( isnan( value ) || SIM_VALUE::Equal( value, textEntry->GetValue().ToStdString() ) )
{
// Don't mess up user formatting if eval'ing didn't actually change the value.
}
@ -149,25 +148,23 @@ bool SIM_STRING_PROPERTY::OnEvent( wxPropertyGrid* propgrid, wxWindow* wnd_prima
keyEvent.m_keyCode = keyEvent.ShiftDown() ? WXK_UP : WXK_DOWN;
keyEvent.m_shiftDown = false;
}
}
}
#ifdef __WXMAC__
if( wxPropertyGrid* propGrid = dynamic_cast<wxPropertyGrid*>( wnd_primary->GetParent() ) )
{
// This shouldn't be required, as wxPGTextCtrlEditor::OnTextCtrlEvent() should be setting
// the value to modified. But it doesn't on Mac (or the modified flag is cleared at some
// point later), and even if it is set, the changes don't get committed.
// (We used to have code in DIALOG_SIM_MODEL to commit things on *some* actions, but it
// wasn't complete and this appears to have at least a better hit rate.)
propGrid->CallAfter(
[propGrid]()
{
propGrid->EditorsValueWasModified();
propGrid->CommitChangesFromEditor();
} );
}
// This shouldn't be required, as wxPGTextCtrlEditor::OnTextCtrlEvent() should be
// setting the value to modified. But it doesn't on Mac (or the modified flag is
// cleared at some point later), and even if it is set, the changes don't get
// committed.
// (We used to have code in DIALOG_SIM_MODEL to commit things on *some* actions, but
// it wasn't complete and this appears to have at least a better hit rate.)
propGrid->CallAfter(
[propGrid]()
{
propGrid->EditorsValueWasModified();
propGrid->CommitChangesFromEditor();
} );
#endif
}
}
return false;
}
@ -198,7 +195,7 @@ bool SIM_STRING_PROPERTY::StringToValue( wxVariant& aVariant, const wxString& aT
{
double value = SIM_VALUE::ToDouble( m_eval.Result().ToStdString() );
if( isnan( value ) || value == SIM_VALUE::ToDouble( aText.ToStdString() ) )
if( isnan( value ) || SIM_VALUE::Equal( value, aText.ToStdString() ) )
{
// Don't mess up user formatting if eval'ing didn't actually change the value.
}

View File

@ -451,3 +451,9 @@ int SIM_VALUE::ToInt( const std::string& aString, int aDefault )
return aDefault;
}
bool SIM_VALUE::Equal( double aLH, const std::string& aRH )
{
return std::abs( aLH - ToDouble( aRH ) ) <= std::numeric_limits<double>::epsilon();
}

View File

@ -89,6 +89,8 @@ public:
static double ToDouble( const std::string& aString, double aDefault = NAN );
static int ToInt( const std::string& aString, int aDefault = -1 );
static bool Equal( double aLH, const std::string& aRH );
};