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 if( textEntry )
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 }
116 else if( event.GetEventType() == wxEVT_KILL_FOCUS && allowEval() )
117 {
118 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( wnd_primary );
119
120 if( textEntry && m_eval.Process( textEntry->GetValue() ) )
121 {
122 double value = SIM_VALUE::ToDouble( m_eval.Result().ToStdString() );
123
124 if( std::isnan( value ) || SIM_VALUE::Equal( value, textEntry->GetValue().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 m_needsEval = false;
134 return true;
135 }
136 }
137 else if( event.GetEventType() == wxEVT_KEY_DOWN )
138 {
139 wxKeyEvent& keyEvent = dynamic_cast<wxKeyEvent&>( event );
140 wxPropertyGrid* propGrid = dynamic_cast<wxPropertyGrid*>( wnd_primary->GetParent() );
141
142 if( propGrid )
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( !aText.IsEmpty() && allowEval() && m_needsEval && m_eval.Process( aText ) )
195 {
196 double value = SIM_VALUE::ToDouble( m_eval.Result().ToStdString() );
197
198 if( std::isnan( value ) || SIM_VALUE::Equal( value, aText.ToStdString() ) )
199 {
200 // Don't mess up user formatting if eval'ing didn't actually change the value.
201 }
202 else
203 {
204 text = SIM_VALUE::Normalize( value );
205 }
206 }
207
208 m_model.SetParamValue( m_paramIndex, text.ToStdString() );
209 aVariant = text.ToStdString();
210 return true;
211}
212
213
214static wxArrayString convertStringsToWx( const std::vector<std::string>& aStrings )
215{
216 wxArrayString result;
217
218 for( const std::string& string : aStrings )
219 result.Add( string );
220
221 return result;
222}
223
224
225SIM_ENUM_PROPERTY::SIM_ENUM_PROPERTY( const wxString& aLabel, const wxString& aName,
226 SIM_MODEL& aModel, int aParamIndex ) :
227 wxEnumProperty( aLabel, aName,
228 convertStringsToWx( aModel.GetParam( aParamIndex ).info.enumValues ) ),
229 SIM_PROPERTY( aModel, aParamIndex )
230{
231 for( int ii = 0; ii < (int) GetParam().info.enumValues.size(); ++ii )
232 {
233 if( GetParam().info.enumValues[ii] == GetParam().value )
234 {
235 SetValue( ii );
236 return;
237 }
238 }
239
240 SetValue( -1 );
241}
242
243
244bool SIM_ENUM_PROPERTY::IntToValue( wxVariant& aVariant, int aNumber, int aArgFlags ) const
245{
246 if( m_disabled )
247 return false;
248
249 m_model.SetParamValue( m_paramIndex, GetParam().info.enumValues.at( aNumber ) );
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)
bool IntToValue(wxVariant &aVariant, int aNumber, int aArgFlags=0) const override
virtual const PARAM & GetParam(unsigned aParamIndex) const
Definition: sim_model.cpp:785
void SetParamValue(int aParamIndex, const std::string &aValue, SIM_VALUE::NOTATION aNotation=SIM_VALUE::NOTATION::SI)
Definition: sim_model.cpp:854
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:44
static wxArrayString convertStringsToWx(const std::vector< std::string > &aStrings)
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