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_COMPONENT* aComponent )
35  : TUNER_SLIDER_BASE( aParent ), m_component( aComponent ),
36  m_min( 0.0 ), m_max( 0.0 ), m_value( 0.0 ), m_frame ( aFrame )
37 {
38  const wxString compName = aComponent->GetField( REFERENCE_FIELD )->GetText();
39  m_name->SetLabel( compName );
40  m_value = SPICE_VALUE( aComponent->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 ), NULL, 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 curent 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 
134 void TUNER_SLIDER::onClose( wxCommandEvent& event )
135 {
136  m_frame->RemoveTuner( this );
137 }
138 
139 
140 void TUNER_SLIDER::onSave( wxCommandEvent& event )
141 {
144 }
145 
146 
147 void TUNER_SLIDER::onSliderChanged( wxScrollEvent& event )
148 {
149  m_value = m_min + ( m_max - m_min ) * SPICE_VALUE( m_slider->GetValue() / 100.0 );
150  updateValueText();
152  m_changed = true;
153 }
154 
155 
156 void TUNER_SLIDER::onMaxTextEnter( wxCommandEvent& event )
157 {
158  try
159  {
160  SPICE_VALUE newMax( m_maxText->GetValue() );
161  SetMax( newMax );
162  }
163  catch( const KI_PARAM_ERROR& )
164  {
165  // Restore the previous value
166  m_maxText->SetValue( m_max.ToOrigString() );
167  }
168 }
169 
170 
171 void TUNER_SLIDER::onValueTextEnter( wxCommandEvent& event )
172 {
173  try
174  {
175  SPICE_VALUE newCur( m_valueText->GetValue() );
176  SetValue( newCur );
177  m_changed = true;
178  }
179  catch( const KI_PARAM_ERROR& )
180  {
181  // Restore the previous value
182  m_valueText->SetValue( m_value.ToOrigString() );
183  }
184 }
185 
186 
187 void TUNER_SLIDER::onMinTextEnter( wxCommandEvent& event )
188 {
189  try
190  {
191  SPICE_VALUE newMin( m_minText->GetValue() );
192  SetMin( newMin );
193  }
194  catch( const KI_PARAM_ERROR& )
195  {
196  // Restore the previous value
197  m_minText->SetValue( m_min.ToOrigString() );
198  }
199 }
200 
201 
202 void TUNER_SLIDER::onSimTimer( wxTimerEvent& event )
203 {
204  if(m_changed)
205  {
206  wxQueueEvent( m_frame, new wxCommandEvent( EVT_SIM_UPDATE ) );
207  m_changed = false;
208  }
209 }
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:674
void onValueTextEnter(wxCommandEvent &event) override
wxTextCtrl * m_valueText
wxTimer m_simTimer
Definition: tuner_slider.h:94
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
TUNER_SLIDER(SIM_PLOT_FRAME *aFrame, wxWindow *aParent, SCH_COMPONENT *aComponent)
SPICE_VALUE m_min
Definition: tuner_slider.h:98
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:91
Field Value of part, i.e. "3.3K".
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:121
#define NULL
< 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:98
void onSliderChanged(wxScrollEvent &event) override
SPICE_VALUE m_max
Definition: tuner_slider.h:98
Subclass of SIM_PLOT_FRAME_BASE, which is generated by wxFormBuilder.
wxTextCtrl * m_maxText
Implementing SIM_PLOT_FRAME_BASE.
void onClose(wxCommandEvent &event) override
void onSave(wxCommandEvent &event) override
void onMaxTextEnter(wxCommandEvent &event) override
wxTextCtrl * m_minText
bool SetMin(const SPICE_VALUE &aVal)
Schematic symbol object.
Definition: sch_symbol.h:78
wxString GetSpiceDevice(const wxString &aComponent) const
Return name of Spice device corresponding to a schematic component.
bool SetMax(const SPICE_VALUE &aVal)
const NETLIST_EXPORTER_PSPICE_SIM * GetExporter() const
Return the netlist exporter object used for simulations.
void onMinTextEnter(wxCommandEvent &event) override
void updateValueText()
SCH_COMPONENT * m_component
Definition: tuner_slider.h:96
SIM_PLOT_FRAME * m_frame
Definition: tuner_slider.h:101
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