KiCad PCB EDA Suite
tuner_slider.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) 2016 CERN
5  * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Maciej Suminski <maciej.suminski@cern.ch>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 3
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * https://www.gnu.org/licenses/gpl-3.0.html
22  * or you may search the http://www.gnu.org website for the version 3 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #include "tuner_slider.h"
28 
29 #include <sim/sim_plot_frame.h>
30 #include <sch_symbol.h>
31 #include <template_fieldnames.h>
33 
34 TUNER_SLIDER::TUNER_SLIDER( SIM_PLOT_FRAME* aFrame, wxWindow* aParent, SCH_SYMBOL* aSymbol )
35  : TUNER_SLIDER_BASE( aParent ), m_symbol( aSymbol ),
36  m_min( 0.0 ), m_max( 0.0 ), m_value( 0.0 ), m_frame ( aFrame )
37 {
38  const wxString compName = aSymbol->GetField( REFERENCE_FIELD )->GetText();
39  m_name->SetLabel( compName );
40  m_value = SPICE_VALUE( aSymbol->GetField( VALUE_FIELD )->GetText() );
41 
42  m_changed = false;
43  m_spiceName = aFrame->GetExporter()->GetSpiceDevice( compName ).Lower();
44 
45  // Call Set*() methods to update fields and slider
46  m_max = SPICE_VALUE( 2.0 ) * m_value;
47  m_min = SPICE_VALUE( 0.5 ) * m_value;
48 
49  m_minText->SetValue( m_min.ToOrigString() );
50  m_maxText->SetValue( m_max.ToOrigString() );
51 
53  updateSlider();
54 
55  m_simTimer.SetOwner( this );
56  Connect( wxEVT_TIMER, wxTimerEventHandler( TUNER_SLIDER::onSimTimer ), nullptr, this );
57 }
58 
59 
61 {
62  // Get the value into the current range boundaries
63  if( aVal > m_max )
64  m_value = m_max;
65  else if( aVal < m_min )
66  m_value = m_min;
67  else
68  m_value = aVal;
69 
71  updateSlider();
73 
74  return true;
75 }
76 
77 
78 bool TUNER_SLIDER::SetMin( const SPICE_VALUE& aVal )
79 {
80  if( aVal >= m_max )
81  return false;
82 
83  m_min = aVal;
84 
85  if( m_value < aVal ) // Limit the current value
86  SetValue( aVal );
87 
88  m_minText->SetValue( aVal.ToOrigString() );
89  updateSlider();
90 
91  return true;
92 }
93 
94 
95 bool TUNER_SLIDER::SetMax( const SPICE_VALUE& aVal )
96 {
97  if( aVal <= m_min )
98  return false;
99 
100  m_max = aVal;
101 
102  if( m_value > aVal ) // Limit the current value
103  SetValue( aVal );
104 
105  m_maxText->SetValue( aVal.ToOrigString() );
106  updateSlider();
107 
108  return true;
109 }
110 
111 
113 {
114  // Start simulation in 100 ms, if the value does not change meanwhile
115  m_simTimer.StartOnce( 100 );
116 }
117 
118 
120 {
121  assert( m_max >= m_value && m_value >= m_min );
122 
123  m_slider->SetValue( ( ( m_value - m_min ) / ( m_max - m_min ) ).ToDouble() * 100.0 );
124 }
125 
126 
128 {
129  bool spiceString = m_min.IsSpiceString() || m_max.IsSpiceString();
130  m_valueText->SetValue( spiceString ? m_value.ToSpiceString() : m_value.ToString() );
131 }
132 
133 
135 {
136  try
137  {
138  SPICE_VALUE newMax( m_maxText->GetValue() );
139  SetMax( newMax );
140  }
141  catch( const KI_PARAM_ERROR& )
142  {
143  // Restore the previous value
144  m_maxText->SetValue( m_max.ToOrigString() );
145  }
146 }
147 
148 
150 {
151  try
152  {
153  SPICE_VALUE newCur( m_valueText->GetValue() );
154  SetValue( newCur );
155  m_changed = true;
156  }
157  catch( const KI_PARAM_ERROR& )
158  {
159  // Restore the previous value
160  m_valueText->SetValue( m_value.ToOrigString() );
161  }
162 }
163 
164 
166 {
167  try
168  {
169  SPICE_VALUE newMin( m_minText->GetValue() );
170  SetMin( newMin );
171  }
172  catch( const KI_PARAM_ERROR& )
173  {
174  // Restore the previous value
175  m_minText->SetValue( m_min.ToOrigString() );
176  }
177 }
178 
179 
180 void TUNER_SLIDER::onClose( wxCommandEvent& event )
181 {
182  m_frame->RemoveTuner( this );
183 }
184 
185 
186 void TUNER_SLIDER::onSave( wxCommandEvent& event )
187 {
190 }
191 
192 
193 void TUNER_SLIDER::onSliderChanged( wxScrollEvent& event )
194 {
195  m_value = m_min + ( m_max - m_min ) * SPICE_VALUE( m_slider->GetValue() / 100.0 );
196  updateValueText();
198  m_changed = true;
199 }
200 
201 
202 void TUNER_SLIDER::onMaxKillFocus( wxFocusEvent& event )
203 {
204  updateMax();
205 }
206 
207 
208 void TUNER_SLIDER::onValueKillFocus( wxFocusEvent& event )
209 {
210  updateValue();
211 }
212 
213 
214 void TUNER_SLIDER::onMinKillFocus( wxFocusEvent& event )
215 {
216  updateMin();
217 }
218 
219 
220 void TUNER_SLIDER::onMaxTextEnter( wxCommandEvent& event )
221 {
222  updateMax();
223 }
224 
225 
226 void TUNER_SLIDER::onValueTextEnter( wxCommandEvent& event )
227 {
228  updateValue();
229 }
230 
231 
232 void TUNER_SLIDER::onMinTextEnter( wxCommandEvent& event )
233 {
234  updateMin();
235 }
236 
237 
238 void TUNER_SLIDER::onSimTimer( wxTimerEvent& event )
239 {
240  if( m_changed )
241  {
242  wxQueueEvent( m_frame, new wxCommandEvent( EVT_SIM_UPDATE ) );
243  m_changed = false;
244  }
245 }
Field Reference of part, i.e. "IC21".
void onSimTimer(wxTimerEvent &event)
void updateSlider()
wxString ToSpiceString() const
Return string value in Spice format (e.g.
void RemoveTuner(TUNER_SLIDER *aTuner, bool aErase=true)
Remove an existing tuner.
SCH_FIELD * GetField(MANDATORY_FIELD_T aFieldType)
Return a mandatory field in this symbol.
Definition: sch_symbol.cpp:675
void onValueTextEnter(wxCommandEvent &event) override
wxTextCtrl * m_valueText
wxTimer m_simTimer
Definition: tuner_slider.h:102
wxStaticText * m_name
wxString ToOrigString() const
Return either a normal string or Spice format string, depending on the original value format.
Definition: spice_value.h:92
bool IsSpiceString() const
Return true if the object was initiated with a Spice formatted string value.
Definition: spice_value.h:100
SPICE_VALUE m_min
Definition: tuner_slider.h:106
wxString ToString() const
Return string value as when converting double to string (e.g.
wxString m_spiceName
Timer that restarts the simulation after the slider value has changed.
Definition: tuner_slider.h:99
Field Value of part, i.e. "3.3K".
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:114
< Helper class to handle Spice way of expressing values (e.g. 10.5 Meg) Helper class to recognize Spi...
Definition: spice_value.h:34
bool SetValue(const SPICE_VALUE &aVal)
Class TUNER_SLIDER_BASE.
SPICE_VALUE m_value
Definition: tuner_slider.h:106
wxString GetSpiceDevice(const wxString &aSymbol) const
Return name of Spice device corresponding to a schematic symbol.
void onSliderChanged(wxScrollEvent &event) override
SPICE_VALUE m_max
Definition: tuner_slider.h:106
Subclass of SIM_PLOT_FRAME_BASE, which is generated by wxFormBuilder.
void onMaxKillFocus(wxFocusEvent &event) override
wxTextCtrl * m_maxText
Implementing SIM_PLOT_FRAME_BASE.
SCH_SYMBOL * m_symbol
Definition: tuner_slider.h:104
void onClose(wxCommandEvent &event) override
void updateValue()
void onValueKillFocus(wxFocusEvent &event) override
void onSave(wxCommandEvent &event) override
void onMaxTextEnter(wxCommandEvent &event) override
Schematic symbol object.
Definition: sch_symbol.h:78
wxTextCtrl * m_minText
bool SetMin(const SPICE_VALUE &aVal)
void onMinKillFocus(wxFocusEvent &event) override
bool SetMax(const SPICE_VALUE &aVal)
TUNER_SLIDER(SIM_PLOT_FRAME *aFrame, wxWindow *aParent, SCH_SYMBOL *aSymbol)
const NETLIST_EXPORTER_PSPICE_SIM * GetExporter() const
Return the netlist exporter object used for simulations.
void onMinTextEnter(wxCommandEvent &event) override
void updateValueText()
SIM_PLOT_FRAME * m_frame
Definition: tuner_slider.h:109
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:133
void updateComponentValue()
Hold a translatable error message and may be used when throwing exceptions containing a translated er...
Definition: ki_exception.h:44