KiCad PCB EDA Suite
params_read_write.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) 2011 jean-pierre.charras
5  * Copyright (C) 2011 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 along
18  * with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <wx/app.h>
22 #include <wx/colour.h>
23 #include <wx/msgdlg.h>
24 
26 #include <pcb_calculator_frame.h>
27 #include <transline/transline.h>
28 
29 /*
30  * Return the value from a string,
31  * Unlike standard string to double conversion,
32  * both point and comma F.P. separator are accepted
33  * and values having units (like 4.7 K) are accepted
34  * but units are ignored.
35  * notation like 1e+3 is legal
36  */
37 double DoubleFromString( const wxString& TextValue )
38 {
39  double value = 0;
40 
41  /* Acquire the 'right' decimal point separator */
42  const struct lconv* lc = localeconv();
43  wxChar decimal_point = lc->decimal_point[0];
44  wxString buf( TextValue.Strip( wxString::both ) );
45 
46  /* Convert the period in decimal point */
47  buf.Replace( wxT( "." ), wxString( decimal_point, 1 ) );
48  // An ugly fix needed by WxWidgets 2.9.1 that sometimes
49  // back to a point as separator, although the separator is the comma
50  buf.Replace( wxT( "," ), wxString( decimal_point, 1 ) );
51 
52  /* Find the end of the numeric part
53  *(when units are append to the number, remove them)
54  */
55  unsigned brk_point = 0;
56  while( brk_point < buf.Len() )
57  {
58  wxChar ch = buf[brk_point];
59  if( !( ( ch >= '0' && ch <= '9' ) || ( ch == decimal_point ) || ( ch == '-' )
60  || ( ch == '+' ) || ( ch == 'e' ) || ( ch == 'E' ) ) )
61  {
62  break;
63  }
64  ++brk_point;
65  }
66 
67  // Check for strings that cannot qualify as a number
68  if( brk_point == 0 )
69  return std::nan( "" );
70 
71  /* Extract the numeric part */
72  if( !buf.Left( brk_point ).ToDouble( &value ) )
73  return std::nan( "" );
74 
75  return value;
76 }
77 
78 
79 // A helper function to get a reference to the PANEL_TRANSLINE
81 {
82  PCB_CALCULATOR_FRAME* frame = (PCB_CALCULATOR_FRAME*) wxTheApp->GetTopWindow();
83  PANEL_TRANSLINE* transline = frame->GetCalculator<PANEL_TRANSLINE>();
84 
85  wxASSERT( transline );
86  return transline;
87 }
88 
89 
90 // Functions to Read/Write parameters in pcb_calculator main frame:
91 // They are only wrapper to actual functions, so all transline functions do not
92 // depend on Graphic User Interface
93 void SetPropertyInDialog( enum PRMS_ID aPrmId, double value )
94 {
95  getTranslinePanel()->SetPrmValue( aPrmId, value );
96 }
97 
98 void SetPropertyBgColorInDialog( enum PRMS_ID aPrmId, const KIGFX::COLOR4D* aCol )
99 {
100  getTranslinePanel()->SetPrmBgColor( aPrmId, aCol );
101 }
102 
103 
104 /* Puts the text into the given result line.
105 */
106 void SetResultInDialog( int line, const wxString& aText )
107 {
108  getTranslinePanel()->SetResult( line, aText );
109 }
110 
111 /* print aValue into the given result line.
112 */
113 void SetResultInDialog( int aLineNumber, double aValue, const wxString& aText )
114 {
115  wxString fullmsg;
116  fullmsg.Printf( wxT( "%g " ), aValue );
117  fullmsg += aText;
118  getTranslinePanel()->SetResult( aLineNumber, fullmsg );
119 }
120 
121 /* Returns a named property value. */
122 double GetPropertyInDialog( enum PRMS_ID aPrmId )
123 {
124  return getTranslinePanel()->GetPrmValue( aPrmId );
125 }
126 
127 // Returns true if the param aPrmId is selected
128 // Has meaning only for params that have a radio button
129 bool IsSelectedInDialog( enum PRMS_ID aPrmId )
130 {
131  return getTranslinePanel()->IsPrmSelected( aPrmId );
132 }
133 
134 
141 double PANEL_TRANSLINE::GetPrmValue( enum PRMS_ID aPrmId ) const
142 {
144 
145  for( unsigned ii = 0; ii < tr_ident->GetPrmsCount(); ii++ )
146  {
147  TRANSLINE_PRM* prm = tr_ident->GetPrm( ii );
148 
149  if( aPrmId == prm->m_Id )
150  return prm->m_NormalizedValue;
151  }
152 
153  return 1.0;
154 }
155 
162 void PANEL_TRANSLINE::SetPrmValue( enum PRMS_ID aPrmId, double aValue )
163 {
165 
166  for( unsigned ii = 0; ii < tr_ident->GetPrmsCount(); ii++ )
167  {
168  TRANSLINE_PRM* prm = tr_ident->GetPrm( ii );
169 
170  if( aPrmId == prm->m_Id )
171  {
172  prm->m_NormalizedValue = aValue;
173  prm->m_Value = prm->m_NormalizedValue * prm->ToUserUnit();
174  wxString msg;
175  msg.Printf( wxT( "%g" ), prm->m_Value );
176  ( (wxTextCtrl*) prm->m_ValueCtrl )->SetValue( msg );
177  return;
178  }
179  }
180  wxLogMessage( wxT( "GetPrmValue: prm %d not found" ), (int) aPrmId );
181 }
182 
189 void PANEL_TRANSLINE::SetPrmBgColor( enum PRMS_ID aPrmId, const KIGFX::COLOR4D* aCol )
190 {
191  wxColour wxcol = wxColour( static_cast<unsigned char>( aCol->r * 255 ),
192  static_cast<unsigned char>( aCol->g * 255 ),
193  static_cast<unsigned char>( aCol->b * 255 ) );
194 
195  if( !wxcol.IsOk() )
196  return;
197 
199 
200  for( unsigned ii = 0; ii < tr_ident->GetPrmsCount(); ii++ )
201  {
202  TRANSLINE_PRM* prm = tr_ident->GetPrm( ii );
203  wxTextCtrl* ctl = static_cast<wxTextCtrl*>( prm->m_ValueCtrl );
204 
205  if( aPrmId == prm->m_Id )
206  {
207  ctl->SetBackgroundColour( wxcol );
208  ctl->SetStyle( 0, -1, ctl->GetDefaultStyle() );
209  return;
210  }
211 
212  }
213 }
214 
221 void PANEL_TRANSLINE::SetResult( int aLineNumber, const wxString& aText )
222 {
223  #define MSG_CNT_MAX 8
224  wxStaticText* messages[MSG_CNT_MAX] =
228  };
229 
230  wxASSERT( ( aLineNumber >= 0 ) && ( aLineNumber < MSG_CNT_MAX ) );
231 
232  if( aLineNumber < 0 )
233  aLineNumber = 0;
234 
235  if( aLineNumber >= MSG_CNT_MAX )
236  aLineNumber = MSG_CNT_MAX - 1;
237 
238  messages[aLineNumber]->SetLabel( aText );
239 }
240 
246 bool PANEL_TRANSLINE::IsPrmSelected( enum PRMS_ID aPrmId ) const
247 {
248  switch( aPrmId )
249  {
250  default:
251  wxMessageBox( wxT( "IsPrmSelected() error" ) );
252  break;
253 
254  case PHYS_WIDTH_PRM:
255  case PHYS_DIAM_IN_PRM:
256  return m_radioBtnPrm1->GetValue();
257  break;
258 
259  case PHYS_S_PRM:
260  case PHYS_DIAM_OUT_PRM:
261  return m_radioBtnPrm2->GetValue();
262  break;
263  }
264  return false;
265 }
PCB calculator the main frame.
void SetPropertyBgColorInDialog(enum PRMS_ID aPrmId, const KIGFX::COLOR4D *aCol)
Function SetPropertyBgColorInDialog Set the background color of a parameter.
double DoubleFromString(const wxString &TextValue)
double GetPropertyInDialog(enum PRMS_ID aPrmId)
double m_NormalizedValue
enum TRANSLINE_TYPE_ID m_currTransLineType
#define MSG_CNT_MAX
void SetPrmValue(enum PRMS_ID aPrmId, double aValue)
Read/write params values and results.
double GetPrmValue(enum PRMS_ID aPrmId) const
Return a param value.
A class to handle one parameter of transline.
A class to handle a list of parameters of a given transline.
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
wxRadioButton * m_radioBtnPrm1
PRMS_ID
Definition: transline.h:36
void SetResultInDialog(int line, const wxString &aText)
unsigned GetPrmsCount() const
void SetPrmBgColor(enum PRMS_ID aPrmId, const KIGFX::COLOR4D *aCol)
Set the background color of a parameter.
wxRadioButton * m_radioBtnPrm2
PANEL_TRANSLINE * getTranslinePanel()
TRANSLINE_PRM * GetPrm(unsigned aIdx) const
bool IsSelectedInDialog(enum PRMS_ID aPrmId)
void SetPropertyInDialog(enum PRMS_ID aPrmId, double value)
void SetResult(int aLineNumber, const wxString &aText)
Put the text into the given result line.
double r
Red component.
Definition: color4d.h:384
std::vector< TRANSLINE_IDENT * > m_transline_list
bool IsPrmSelected(enum PRMS_ID aPrmId) const
Function IsPrmSelected.
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103