KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sim_property.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2022 Mikolaj Wielgus
5 * Copyright (C) 2022-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * https://www.gnu.org/licenses/gpl-3.0.html
20 * or you may search the http://www.gnu.org website for the version 3 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <wx/combo.h>
26
27// Include simulator headers after wxWidgets headers to avoid conflicts with Windows headers
28// (especially on msys2 + wxWidgets 3.0.x)
29#include <sim/sim_property.h>
30#include <sim/sim_value.h>
31#include <ki_exception.h>
32
33
41SIM_PROPERTY::SIM_PROPERTY( SIM_MODEL& aModel, int aParamIndex ) :
42 m_model( aModel ),
43 m_paramIndex( aParamIndex ),
44 m_needsEval( false ),
45 m_eval( EDA_UNITS::UNSCALED ),
46 m_disabled( false )
47{
48}
49
50
52{
53 m_disabled = true;
54}
55
56
57SIM_BOOL_PROPERTY::SIM_BOOL_PROPERTY( const wxString& aLabel, const wxString& aName,
58 SIM_MODEL& aModel, int aParamIndex ) :
59 wxBoolProperty( aLabel, aName ),
60 SIM_PROPERTY( aModel, aParamIndex )
61{
62 std::string value = m_model.GetParam( m_paramIndex ).value;
63 SetValue( value == "1" );
64}
65
66
68{
69 return new SIM_VALIDATOR();
70}
71
72
74{
75 wxPGProperty::OnSetValue();
76
77 if( m_disabled )
78 return;
79
80 m_model.SetParamValue( m_paramIndex, m_value.GetBool() ? "1" : "0" );
81}
82
83
84SIM_STRING_PROPERTY::SIM_STRING_PROPERTY( const wxString& aLabel, const wxString& aName,
85 SIM_MODEL& aModel, int aParamIndex,
86 SIM_VALUE::TYPE aValueType,
87 SIM_VALUE_GRAMMAR::NOTATION aNotation ) :
88 wxStringProperty( aLabel, aName ),
89 SIM_PROPERTY( aModel, aParamIndex ),
90 m_valueType( aValueType )
91{
92 SetValueFromString( GetParam().value );
93}
94
95
96bool SIM_STRING_PROPERTY::OnEvent( wxPropertyGrid* propgrid, wxWindow* wnd_primary, wxEvent& event )
97{
98 if( event.GetEventType() == wxEVT_SET_FOCUS && allowEval() )
99 {
100 if( wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( wnd_primary ) )
101 {
102 wxString oldStr = m_eval.OriginalText();
103
104 if( oldStr.length() && oldStr != textEntry->GetValue() )
105 {
106 SetValueInEvent( oldStr );
107 textEntry->SetValue( oldStr );
108 }
109
110 m_needsEval = true;
111 return true;
112 }
113 }
114 else if( event.GetEventType() == wxEVT_KILL_FOCUS && allowEval() )
115 {
116 if( wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( wnd_primary ) )
117 {
118 wxString strValue = textEntry->GetValue();
119
120 if( !strValue.IsEmpty() && m_eval.Process( strValue ) )
121 {
122 double value = SIM_VALUE::ToDouble( m_eval.Result().ToStdString() );
123
124 if( std::isnan( value ) || SIM_VALUE::Equal( value, strValue.ToStdString() ) )
125 {
126 // Don't mess up user formatting if eval'ing didn't actually change the value.
127 }
128 else
129 {
130 SetValueInEvent( m_eval.Result() );
131 }
132 }
133
134 m_needsEval = false;
135 return true;
136 }
137 }
138 else if( event.GetEventType() == wxEVT_KEY_DOWN )
139 {
140 wxKeyEvent& keyEvent = dynamic_cast<wxKeyEvent&>( event );
141
142 if( wxPropertyGrid* propGrid = dynamic_cast<wxPropertyGrid*>( wnd_primary->GetParent() ) )
143 {
144 if( keyEvent.GetKeyCode() == WXK_TAB )
145 {
146 propGrid->CommitChangesFromEditor();
147
148 keyEvent.m_keyCode = keyEvent.ShiftDown() ? WXK_UP : WXK_DOWN;
149 keyEvent.m_shiftDown = false;
150 }
151
152#ifdef __WXMAC__
153 // This shouldn't be required, as wxPGTextCtrlEditor::OnTextCtrlEvent() should be
154 // setting the value to modified. But it doesn't on Mac (or the modified flag is
155 // cleared at some point later), and even if it is set, the changes don't get
156 // committed.
157 // (We used to have code in DIALOG_SIM_MODEL to commit things on *some* actions, but
158 // it wasn't complete and this appears to have at least a better hit rate.)
159 propGrid->CallAfter(
160 [propGrid]()
161 {
162 propGrid->EditorsValueWasModified();
163 propGrid->CommitChangesFromEditor();
164 } );
165#endif
166 }
167 }
168
169 return false;
170}
171
172
174{
175 return new SIM_VALIDATOR();
176}
177
178
180{
183}
184
185
186bool SIM_STRING_PROPERTY::StringToValue( wxVariant& aVariant, const wxString& aText,
187 int aArgFlags ) const
188{
189 if( m_disabled )
190 return false;
191
192 wxString text = aText;
193
194 if( allowEval() && m_needsEval && m_eval.Process( aText ) )
195 {
196 if( !aText.IsEmpty() )
197 {
198 double value = SIM_VALUE::ToDouble( m_eval.Result().ToStdString() );
199
200 if( std::isnan( value ) || SIM_VALUE::Equal( value, aText.ToStdString() ) )
201 {
202 // Don't mess up user formatting if eval'ing didn't actually change the value.
203 }
204 else
205 {
206 text = SIM_VALUE::Normalize( value );
207 }
208 }
209 }
210
211 m_model.SetParamValue( m_paramIndex, text.ToStdString() );
212 aVariant = text.ToStdString();
213 return true;
214}
215
216
217SIM_ENUM_PROPERTY::SIM_ENUM_PROPERTY( const wxString& aLabel, const wxString& aName,
218 SIM_MODEL& aModel, int aParamIndex,
219 const wxArrayString& aValues ) :
220 wxEnumProperty( aLabel, aName, aValues ),
221 SIM_PROPERTY( aModel, aParamIndex )
222{
223 for( int ii = 0; ii < (int) GetParam().info.enumValues.size(); ++ii )
224 {
225 if( GetParam().info.enumValues[ii] == GetParam().value )
226 {
227 SetValue( ii );
228 return;
229 }
230 }
231
232 SetValue( -1 );
233}
234
235
236#if wxCHECK_VERSION( 3, 3, 0 )
237bool SIM_ENUM_PROPERTY::IntToValue( wxVariant& aVariant, int aNumber,
238 wxPGPropValFormatFlags aArgFlags ) const
239#else
240bool SIM_ENUM_PROPERTY::IntToValue( wxVariant& aVariant, int aNumber, int aArgFlags ) const
241#endif
242{
243 if( m_disabled )
244 return false;
245
246 m_model.SetParamValue( m_paramIndex, m_choices.GetLabel( aNumber ).ToStdString() );
247 return wxEnumProperty::IntToValue( aVariant, aNumber, aArgFlags );
248}
wxString OriginalText() const
wxString Result() const
bool Process(const wxString &aString)
SIM_BOOL_PROPERTY(const wxString &aLabel, const wxString &aName, SIM_MODEL &aModel, int aParamIndex)
void OnSetValue() override
wxValidator * DoGetValidator() const override
SIM_ENUM_PROPERTY(const wxString &aLabel, const wxString &aName, SIM_MODEL &aModel, int aParamIndex, const wxArrayString &aValues)
bool IntToValue(wxVariant &aVariant, int aNumber, int aArgFlags=0) const override
virtual const PARAM & GetParam(unsigned aParamIndex) const
Definition: sim_model.cpp:789
void SetParamValue(int aParamIndex, const std::string &aValue, SIM_VALUE::NOTATION aNotation=SIM_VALUE::NOTATION::SI)
Definition: sim_model.cpp:845
const SIM_MODEL::PARAM & GetParam() const
Definition: sim_property.h:60
SIM_PROPERTY(SIM_MODEL &aModel, int aParamIndex)
wxPropertyGrid property specializations for simulator.
SIM_MODEL & m_model
Definition: sim_property.h:63
bool m_disabled
If true, never access the models.
Definition: sim_property.h:69
bool m_needsEval
Definition: sim_property.h:66
NUMERIC_EVALUATOR m_eval
Definition: sim_property.h:67
wxValidator * DoGetValidator() const override
SIM_STRING_PROPERTY(const wxString &aLabel, const wxString &aName, SIM_MODEL &aModel, int aParamIndex, SIM_VALUE::TYPE aValueType=SIM_VALUE::TYPE_FLOAT, SIM_VALUE_GRAMMAR::NOTATION aNotation=SIM_VALUE_GRAMMAR::NOTATION::SI)
bool allowEval() const
SIM_VALUE::TYPE m_valueType
Definition: sim_property.h:108
bool OnEvent(wxPropertyGrid *propgrid, wxWindow *wnd_primary, wxEvent &event) override
bool StringToValue(wxVariant &aVariant, const wxString &aText, int aArgFlags=0) const override
wxPropertyGrid property specializations for simulator.
Definition: sim_property.h:45
static std::string Normalize(double aValue)
Definition: sim_value.cpp:406
static bool Equal(double aLH, const std::string &aRH)
Definition: sim_value.cpp:487
@ TYPE_INT
Definition: sim_value.h:68
@ TYPE_FLOAT
Definition: sim_value.h:69
static double ToDouble(const std::string &aString, double aDefault=NAN)
Definition: sim_value.cpp:442
EDA_UNITS
Definition: eda_units.h:46
static std::string strValue(double aValue)
std::vector< std::string > enumValues
Definition: sim_model.h:388
std::string value
Definition: sim_model.h:400
const INFO & info
Definition: sim_model.h:401