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 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( wnd_primary );
101
102 wxCHECK( textEntry, false );
103
104 wxString oldStr = m_eval.OriginalText();
105
106 if( oldStr.length() && oldStr != textEntry->GetValue() )
107 {
108 SetValueInEvent( oldStr );
109 textEntry->SetValue( oldStr );
110 }
111
112 m_needsEval = true;
113 return true;
114 }
115 else if( event.GetEventType() == wxEVT_KILL_FOCUS && allowEval() )
116 {
117 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( wnd_primary );
118
119 wxCHECK( textEntry, false );
120
121 wxString strValue = textEntry->GetValue();
122
123 if( !strValue.IsEmpty() && m_eval.Process( strValue ) )
124 {
125 double value = SIM_VALUE::ToDouble( m_eval.Result().ToStdString() );
126
127 if( std::isnan( value ) || SIM_VALUE::Equal( value, strValue.ToStdString() ) )
128 {
129 // Don't mess up user formatting if eval'ing didn't actually change the value.
130 }
131 else
132 {
133 SetValueInEvent( m_eval.Result() );
134 }
135 }
136
137 m_needsEval = false;
138 return true;
139 }
140 else if( event.GetEventType() == wxEVT_KEY_DOWN )
141 {
142 wxKeyEvent& keyEvent = dynamic_cast<wxKeyEvent&>( event );
143 wxPropertyGrid* propGrid = dynamic_cast<wxPropertyGrid*>( wnd_primary->GetParent() );
144
145 if( propGrid )
146 {
147 if( keyEvent.GetKeyCode() == WXK_TAB )
148 {
149 propGrid->CommitChangesFromEditor();
150
151 keyEvent.m_keyCode = keyEvent.ShiftDown() ? WXK_UP : WXK_DOWN;
152 keyEvent.m_shiftDown = false;
153 }
154
155#ifdef __WXMAC__
156 // This shouldn't be required, as wxPGTextCtrlEditor::OnTextCtrlEvent() should be
157 // setting the value to modified. But it doesn't on Mac (or the modified flag is
158 // cleared at some point later), and even if it is set, the changes don't get
159 // committed.
160 // (We used to have code in DIALOG_SIM_MODEL to commit things on *some* actions, but
161 // it wasn't complete and this appears to have at least a better hit rate.)
162 propGrid->CallAfter(
163 [propGrid]()
164 {
165 propGrid->EditorsValueWasModified();
166 propGrid->CommitChangesFromEditor();
167 } );
168#endif
169 }
170 }
171
172 return false;
173}
174
175
177{
178 return new SIM_VALIDATOR();
179}
180
181
183{
186}
187
188
189bool SIM_STRING_PROPERTY::StringToValue( wxVariant& aVariant, const wxString& aText,
190 int aArgFlags ) const
191{
192 if( m_disabled )
193 return false;
194
195 wxString text = aText;
196
197 if( allowEval() && m_needsEval && m_eval.Process( aText ) )
198 {
199 if( !aText.IsEmpty() )
200 {
201 double value = SIM_VALUE::ToDouble( m_eval.Result().ToStdString() );
202
203 if( std::isnan( value ) || SIM_VALUE::Equal( value, aText.ToStdString() ) )
204 {
205 // Don't mess up user formatting if eval'ing didn't actually change the value.
206 }
207 else
208 {
209 text = SIM_VALUE::Normalize( value );
210 }
211 }
212 }
213
214 m_model.SetParamValue( m_paramIndex, text.ToStdString() );
215 aVariant = text.ToStdString();
216 return true;
217}
218
219
220SIM_ENUM_PROPERTY::SIM_ENUM_PROPERTY( const wxString& aLabel, const wxString& aName,
221 SIM_MODEL& aModel, int aParamIndex,
222 const wxArrayString& aValues ) :
223 wxEnumProperty( aLabel, aName, aValues ),
224 SIM_PROPERTY( aModel, aParamIndex )
225{
226 for( int ii = 0; ii < (int) GetParam().info.enumValues.size(); ++ii )
227 {
228 if( GetParam().info.enumValues[ii] == GetParam().value )
229 {
230 SetValue( ii );
231 return;
232 }
233 }
234
235 SetValue( -1 );
236}
237
238
239#if wxCHECK_VERSION( 3, 3, 0 )
240bool SIM_ENUM_PROPERTY::IntToValue( wxVariant& aVariant, int aNumber,
241 wxPGPropValFormatFlags aArgFlags ) const
242#else
243bool SIM_ENUM_PROPERTY::IntToValue( wxVariant& aVariant, int aNumber, int aArgFlags ) const
244#endif
245{
246 if( m_disabled )
247 return false;
248
249 m_model.SetParamValue( m_paramIndex, m_choices.GetLabel( aNumber ).ToStdString() );
250 return wxEnumProperty::IntToValue( aVariant, aNumber, aArgFlags );
251}
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:779
void SetParamValue(int aParamIndex, const std::string &aValue, SIM_VALUE::NOTATION aNotation=SIM_VALUE::NOTATION::SI)
Definition: sim_model.cpp:848
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:405
static bool Equal(double aLH, const std::string &aRH)
Definition: sim_value.cpp:463
@ 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:418
EDA_UNITS
Definition: eda_units.h:46
static std::string strValue(double aValue)
std::vector< std::string > enumValues
Definition: sim_model.h:389
std::string value
Definition: sim_model.h:401
const INFO & info
Definition: sim_model.h:402