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  m_changed = false;
40 
41  // Generate Spice component name
42  char prim = NETLIST_EXPORTER_PSPICE::GetSpiceField( SF_PRIMITIVE, aComponent, 0 )[0];
43  m_spiceName = wxString( prim + 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( std::exception& e )
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( std::exception& e )
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( std::exception& e )
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 }
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)
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:130
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
Field Value of part, i.e. "3.3K".
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)
static wxString GetSpiceField(SPICE_FIELD aField, SCH_COMPONENT *aComponent, unsigned aCtl)
Retrieves either the requested field value or the default value.
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:69
bool SetMax(const SPICE_VALUE &aVal)
void onMinTextEnter(wxCommandEvent &event) override
void updateValueText()
SCH_COMPONENT * m_component
Definition: tuner_slider.h:94
SIM_PLOT_FRAME * m_frame
Definition: tuner_slider.h:99
void updateComponentValue()
virtual void SetText(const wxString &aText)
Definition: eda_text.h:141
wxString ToOrigString() const
Returns either a normal string or Spice format string, depending on the original value format...
Definition: spice_value.h:90