2022-03-09 01:40:59 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2022 Mikolaj Wielgus
|
2023-02-22 09:10:06 +00:00
|
|
|
* Copyright (C) 2022-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2022-03-09 01:40:59 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 3
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* https://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 3 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
2022-11-12 19:45:56 +00:00
|
|
|
|
2022-03-09 01:40:59 +00:00
|
|
|
#include <wx/combo.h>
|
|
|
|
|
2022-11-16 09:09:54 +00:00
|
|
|
// Include simulator headers after wxWidgets headers to avoid conflicts with Windows headers
|
|
|
|
// (especially on msys2 + wxWidgets 3.0.x)
|
|
|
|
#include <sim/sim_property.h>
|
|
|
|
#include <sim/sim_value.h>
|
|
|
|
#include <ki_exception.h>
|
|
|
|
|
2022-03-09 01:40:59 +00:00
|
|
|
|
2022-11-12 02:09:26 +00:00
|
|
|
SIM_PROPERTY::SIM_PROPERTY( SIM_MODEL& aModel, int aParamIndex ) :
|
|
|
|
m_model( aModel ),
|
|
|
|
m_paramIndex( aParamIndex ),
|
2022-12-05 02:07:45 +00:00
|
|
|
m_needsEval( false ),
|
|
|
|
m_eval( EDA_UNITS::UNSCALED ),
|
2022-11-12 02:09:26 +00:00
|
|
|
m_disabled( false )
|
2022-09-11 13:22:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-12 02:09:26 +00:00
|
|
|
void SIM_PROPERTY::Disable()
|
|
|
|
{
|
|
|
|
m_disabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-27 13:45:42 +00:00
|
|
|
SIM_BOOL_PROPERTY::SIM_BOOL_PROPERTY( const wxString& aLabel, const wxString& aName,
|
|
|
|
SIM_MODEL& aModel, int aParamIndex ) :
|
2022-12-05 02:07:45 +00:00
|
|
|
wxBoolProperty( aLabel, aName ),
|
|
|
|
SIM_PROPERTY( aModel, aParamIndex )
|
2022-06-21 02:22:52 +00:00
|
|
|
{
|
2023-02-22 09:10:06 +00:00
|
|
|
std::string value = m_model.GetParam( m_paramIndex ).value;
|
|
|
|
SetValue( value == "1" );
|
2022-06-21 02:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxValidator* SIM_BOOL_PROPERTY::DoGetValidator() const
|
|
|
|
{
|
2022-12-05 02:07:45 +00:00
|
|
|
return new SIM_VALIDATOR();
|
2022-06-21 02:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SIM_BOOL_PROPERTY::OnSetValue()
|
|
|
|
{
|
|
|
|
wxPGProperty::OnSetValue();
|
|
|
|
|
2022-11-12 02:09:26 +00:00
|
|
|
if( m_disabled )
|
|
|
|
return;
|
|
|
|
|
2023-02-22 09:10:06 +00:00
|
|
|
m_model.SetParamValue( m_paramIndex, m_value.GetBool() ? "1" : "0" );
|
2022-06-21 02:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-27 13:45:42 +00:00
|
|
|
SIM_STRING_PROPERTY::SIM_STRING_PROPERTY( const wxString& aLabel, const wxString& aName,
|
|
|
|
SIM_MODEL& aModel, int aParamIndex,
|
|
|
|
SIM_VALUE::TYPE aValueType,
|
2022-12-05 02:07:45 +00:00
|
|
|
SIM_VALUE_GRAMMAR::NOTATION aNotation ) :
|
|
|
|
wxStringProperty( aLabel, aName ),
|
|
|
|
SIM_PROPERTY( aModel, aParamIndex ),
|
2023-02-24 12:43:41 +00:00
|
|
|
m_valueType( aValueType )
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
2023-02-22 09:10:06 +00:00
|
|
|
SetValueFromString( GetParam().value );
|
2022-03-09 01:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-05 02:07:45 +00:00
|
|
|
bool SIM_STRING_PROPERTY::OnEvent( wxPropertyGrid* propgrid, wxWindow* wnd_primary, wxEvent& event )
|
|
|
|
{
|
2022-12-27 12:30:28 +00:00
|
|
|
if( event.GetEventType() == wxEVT_SET_FOCUS && allowEval() )
|
2022-12-05 02:07:45 +00:00
|
|
|
{
|
|
|
|
wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( wnd_primary );
|
|
|
|
|
|
|
|
if( textEntry )
|
|
|
|
{
|
|
|
|
wxString oldStr = m_eval.OriginalText();
|
|
|
|
|
|
|
|
if( oldStr.length() && oldStr != textEntry->GetValue() )
|
|
|
|
{
|
|
|
|
SetValueInEvent( oldStr );
|
|
|
|
textEntry->SetValue( oldStr );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_needsEval = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2022-12-27 12:30:28 +00:00
|
|
|
else if( event.GetEventType() == wxEVT_KILL_FOCUS && allowEval() )
|
2022-12-05 02:07:45 +00:00
|
|
|
{
|
|
|
|
wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( wnd_primary );
|
|
|
|
|
|
|
|
if( textEntry && m_eval.Process( textEntry->GetValue() ) )
|
|
|
|
{
|
2023-02-25 16:30:51 +00:00
|
|
|
double value = SIM_VALUE::ToDouble( m_eval.Result().ToStdString() );
|
|
|
|
|
|
|
|
if( isnan( value )
|
|
|
|
|| value == SIM_VALUE::ToDouble( textEntry->GetValue().ToStdString() ) )
|
|
|
|
{
|
|
|
|
// Don't mess up user formatting if eval'ing didn't actually change the value.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetValueInEvent( m_eval.Result() );
|
|
|
|
}
|
|
|
|
|
2022-12-05 02:07:45 +00:00
|
|
|
m_needsEval = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( event.GetEventType() == wxEVT_KEY_DOWN )
|
|
|
|
{
|
|
|
|
wxKeyEvent& keyEvent = dynamic_cast<wxKeyEvent&>( event );
|
|
|
|
wxPropertyGrid* propGrid = dynamic_cast<wxPropertyGrid*>( wnd_primary->GetParent() );
|
|
|
|
|
|
|
|
if( propGrid )
|
|
|
|
{
|
|
|
|
if( keyEvent.GetKeyCode() == WXK_TAB )
|
|
|
|
{
|
|
|
|
propGrid->CommitChangesFromEditor();
|
|
|
|
|
|
|
|
keyEvent.m_keyCode = keyEvent.ShiftDown() ? WXK_UP : WXK_DOWN;
|
|
|
|
keyEvent.m_shiftDown = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-24 14:19:01 +00:00
|
|
|
if( wxPropertyGrid* propGrid = dynamic_cast<wxPropertyGrid*>( wnd_primary->GetParent() ) )
|
|
|
|
{
|
|
|
|
// This doesn't seem like it should be required, as wxPGTextCtrlEditor::OnTextCtrlEvent()
|
|
|
|
// should be setting the value to modified. But it doesn't (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();
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2022-12-05 02:07:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-21 02:22:52 +00:00
|
|
|
wxValidator* SIM_STRING_PROPERTY::DoGetValidator() const
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
2022-12-05 02:07:45 +00:00
|
|
|
return new SIM_VALIDATOR();
|
2022-03-09 01:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-27 12:30:28 +00:00
|
|
|
bool SIM_STRING_PROPERTY::allowEval() const
|
|
|
|
{
|
|
|
|
return m_valueType == SIM_VALUE::TYPE_INT
|
|
|
|
|| m_valueType == SIM_VALUE::TYPE_FLOAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-27 13:45:42 +00:00
|
|
|
bool SIM_STRING_PROPERTY::StringToValue( wxVariant& aVariant, const wxString& aText,
|
|
|
|
int aArgFlags ) const
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
2022-11-12 02:09:26 +00:00
|
|
|
if( m_disabled )
|
|
|
|
return false;
|
|
|
|
|
2023-02-24 00:37:00 +00:00
|
|
|
wxString text = aText;
|
2022-04-12 14:37:06 +00:00
|
|
|
|
2022-12-27 12:30:28 +00:00
|
|
|
if( allowEval() && m_needsEval && m_eval.Process( aText ) )
|
2023-02-24 00:37:00 +00:00
|
|
|
{
|
|
|
|
double value = SIM_VALUE::ToDouble( m_eval.Result().ToStdString() );
|
2022-12-05 02:07:45 +00:00
|
|
|
|
2023-02-24 00:37:00 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
2022-03-09 01:40:59 +00:00
|
|
|
|
2023-02-24 00:37:00 +00:00
|
|
|
m_model.SetParamValue( m_paramIndex, text.ToStdString() );
|
|
|
|
aVariant = text.ToStdString();
|
2022-03-09 01:40:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-08-30 07:00:52 +00:00
|
|
|
|
|
|
|
|
2022-09-22 05:38:45 +00:00
|
|
|
static wxArrayString convertStringsToWx( const std::vector<std::string>& aStrings )
|
|
|
|
{
|
|
|
|
wxArrayString result;
|
|
|
|
|
|
|
|
for( const std::string& string : aStrings )
|
|
|
|
result.Add( string );
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-27 13:45:42 +00:00
|
|
|
SIM_ENUM_PROPERTY::SIM_ENUM_PROPERTY( const wxString& aLabel, const wxString& aName,
|
|
|
|
SIM_MODEL& aModel, int aParamIndex ) :
|
|
|
|
wxEnumProperty( aLabel, aName,
|
|
|
|
convertStringsToWx( aModel.GetParam( aParamIndex ).info.enumValues ) ),
|
2022-12-05 02:07:45 +00:00
|
|
|
SIM_PROPERTY( aModel, aParamIndex )
|
2022-08-30 07:00:52 +00:00
|
|
|
{
|
2022-12-27 13:45:42 +00:00
|
|
|
for( int ii = 0; ii < (int) GetParam().info.enumValues.size(); ++ii )
|
|
|
|
{
|
2023-02-22 09:10:06 +00:00
|
|
|
if( GetParam().info.enumValues[ii] == GetParam().value )
|
2022-12-27 13:45:42 +00:00
|
|
|
{
|
|
|
|
SetValue( ii );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 11:24:58 +00:00
|
|
|
|
2022-12-27 13:45:42 +00:00
|
|
|
SetValue( -1 );
|
2022-08-30 07:00:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SIM_ENUM_PROPERTY::IntToValue( wxVariant& aVariant, int aNumber, int aArgFlags ) const
|
|
|
|
{
|
2022-11-12 02:09:26 +00:00
|
|
|
if( m_disabled )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_model.SetParamValue( m_paramIndex, GetParam().info.enumValues.at( aNumber ) );
|
2022-08-30 07:00:52 +00:00
|
|
|
return wxEnumProperty::IntToValue( aVariant, aNumber, aArgFlags );
|
|
|
|
}
|