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  * @author Maciej Suminski <maciej.suminski@cern.ch>
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 "tuner_slider.h"
26 
27 #include <sim/sim_plot_frame.h>
28 #include <sch_component.h>
29 #include <template_fieldnames.h>
31 
32 TUNER_SLIDER::TUNER_SLIDER( SIM_PLOT_FRAME* aFrame, wxWindow* aParent, SCH_COMPONENT* aComponent )
33  : TUNER_SLIDER_BASE( aParent ), m_component( aComponent ),
34  m_min( 0.0 ), m_max( 0.0 ), m_value( 0.0 ), m_frame ( aFrame )
35 {
36  const wxString compName = aComponent->GetField( REFERENCE )->GetText();
37  m_name->SetLabel( compName );
38  m_value = SPICE_VALUE( aComponent->GetField( VALUE )->GetText() );
39 
40  m_changed = false;
41  m_spiceName = aFrame->GetExporter()->GetSpiceDevice( compName ).Lower();
42 
43  // Call Set*() methods to update fields and slider
44  m_max = SPICE_VALUE( 2.0 ) * m_value;
45  m_min = SPICE_VALUE( 0.5 ) * m_value;
46 
47  m_minText->SetValue( m_min.ToOrigString() );
48  m_maxText->SetValue( m_max.ToOrigString() );
49 
51  updateSlider();
52 
53  m_simTimer.SetOwner( this );
54  Connect( wxEVT_TIMER, wxTimerEventHandler( TUNER_SLIDER::onSimTimer ), NULL, this );
55 }
56 
57 
59 {
60  // Get the value into the current range boundaries
61  if( aVal > m_max )
62  m_value = m_max;
63  else if( aVal < m_min )
64  m_value = m_min;
65  else
66  m_value = aVal;
67 
69  updateSlider();
71 
72  return true;
73 }
74 
75 
76 bool TUNER_SLIDER::SetMin( const SPICE_VALUE& aVal )
77 {
78  if( aVal >= m_max )
79  return false;
80 
81  m_min = aVal;
82 
83  if( m_value < aVal ) // Limit the curent value
84  SetValue( aVal );
85 
86  m_minText->SetValue( aVal.ToOrigString() );
87  updateSlider();
88 
89  return true;
90 }
91 
92 
93 bool TUNER_SLIDER::SetMax( const SPICE_VALUE& aVal )
94 {
95  if( aVal <= m_min )
96  return false;
97 
98  m_max = aVal;
99 
100  if( m_value > aVal ) // Limit the current value
101  SetValue( aVal );
102 
103  m_maxText->SetValue( aVal.ToOrigString() );
104  updateSlider();
105 
106  return true;
107 }
108 
109 
111 {
112  // Start simulation in 100 ms, if the value does not change meanwhile
113  m_simTimer.StartOnce( 100 );
114 }
115 
116 
118 {
119  assert( m_max >= m_value && m_value >= m_min );
120 
121  m_slider->SetValue( ( ( m_value - m_min ) / ( m_max - m_min ) ).ToDouble() * 100.0 );
122 }
123 
124 
126 {
127  bool spiceString = m_min.IsSpiceString() || m_max.IsSpiceString();
128  m_valueText->SetValue( spiceString ? m_value.ToSpiceString() : m_value.ToString() );
129 }
130 
131 
132 void TUNER_SLIDER::onClose( wxCommandEvent& event )
133 {
134  m_frame->RemoveTuner( this );
135 }
136 
137 
138 void TUNER_SLIDER::onSave( wxCommandEvent& event )
139 {
142 }
143 
144 
145 void TUNER_SLIDER::onSliderChanged( wxScrollEvent& event )
146 {
147  m_value = m_min + ( m_max - m_min ) * SPICE_VALUE( m_slider->GetValue() / 100.0 );
148  updateValueText();
150  m_changed = true;
151 }
152 
153 
154 void TUNER_SLIDER::onMaxTextEnter( wxCommandEvent& event )
155 {
156  try
157  {
158  SPICE_VALUE newMax( m_maxText->GetValue() );
159  SetMax( newMax );
160  }
161  catch( const KI_PARAM_ERROR& )
162  {
163  // Restore the previous value
164  m_maxText->SetValue( m_max.ToOrigString() );
165  }
166 }
167 
168 
169 void TUNER_SLIDER::onValueTextEnter( wxCommandEvent& event )
170 {
171  try
172  {
173  SPICE_VALUE newCur( m_valueText->GetValue() );
174  SetValue( newCur );
175  m_changed = true;
176  }
177  catch( const KI_PARAM_ERROR& )
178  {
179  // Restore the previous value
180  m_valueText->SetValue( m_value.ToOrigString() );
181  }
182 }
183 
184 
185 void TUNER_SLIDER::onMinTextEnter( wxCommandEvent& event )
186 {
187  try
188  {
189  SPICE_VALUE newMin( m_minText->GetValue() );
190  SetMin( newMin );
191  }
192  catch( const KI_PARAM_ERROR& )
193  {
194  // Restore the previous value
195  m_minText->SetValue( m_min.ToOrigString() );
196  }
197 }
198 
199 
200 void TUNER_SLIDER::onSimTimer( wxTimerEvent& event )
201 {
202  if(m_changed)
203  {
204  wxQueueEvent( m_frame, new wxCommandEvent( EVT_SIM_UPDATE ) );
205  m_changed = false;
206  }
207 }
void onSimTimer(wxTimerEvent &event)
void updateSlider()
void RemoveTuner(TUNER_SLIDER *aTuner, bool aErase=true)
Removes an existing tuner.
wxString ToSpiceString() const
Returns string value in Spice format (e.g.
void onValueTextEnter(wxCommandEvent &event) override
wxTextCtrl * m_valueText
wxTimer m_simTimer
Timer that restarts the simulation after the slider value has changed
Definition: tuner_slider.h:92
bool IsSpiceString() const
Returns true if the object was initiated with a Spice formatted string value.
Definition: spice_value.h:98
wxStaticText * m_name
Field Reference of part, i.e. "IC21".
TUNER_SLIDER(SIM_PLOT_FRAME *aFrame, wxWindow *aParent, SCH_COMPONENT *aComponent)
const NETLIST_EXPORTER_PSPICE_SIM * GetExporter() const
Returns the netlist exporter object used for simulations.
SPICE_VALUE m_min
Definition: tuner_slider.h:96
wxString m_spiceName
Definition: tuner_slider.h:89
SCH_FIELD * GetField(int aFieldNdx) const
Returns a field in this symbol.
Helper class to handle Spice way of expressing values (e.g. 10.5 Meg)
Definition: spice_value.h:32
bool SetValue(const SPICE_VALUE &aVal)
Class TUNER_SLIDER_BASE.
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:143
SPICE_VALUE m_value
Definition: tuner_slider.h:96
wxString ToString() const
Returns string value as when converting double to string (e.g.
void onSliderChanged(wxScrollEvent &event) override
SPICE_VALUE m_max
Definition: tuner_slider.h:96
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
Definition the SCH_COMPONENT class for Eeschema.
void onMaxTextEnter(wxCommandEvent &event) override
wxTextCtrl * m_minText
bool SetMin(const SPICE_VALUE &aVal)
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:69
wxString GetSpiceDevice(const wxString &aComponent) const
Returns name of Spice device corresponding to a schematic component.
bool SetMax(const SPICE_VALUE &aVal)
void onMinTextEnter(wxCommandEvent &event) override
void updateValueText()
SCH_COMPONENT * m_component
Definition: tuner_slider.h:94
#define VALUE
SIM_PLOT_FRAME * m_frame
Definition: tuner_slider.h:99
void updateComponentValue()
class KI_PARAM_ERROR is a class used to hold a translatable error message and may be used when throwi...
Definition: ki_exception.h:45
virtual void SetText(const wxString &aText)
Definition: eda_text.h:154
wxString ToOrigString() const
Returns either a normal string or Spice format string, depending on the original value format...
Definition: spice_value.h:90