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 /* Puts the text into the given result line.
104 */
105 void SetResultInDialog( int line, const char* aText )
106 {
107  wxString msg = wxString::FromUTF8( aText );
108  getTranslinePanel()->SetResult( line, msg );
109 }
110 
111 /* print aValue into the given result line.
112 */
113 void SetResultInDialog( int aLineNumber, double aValue, const char* aText )
114 {
115  wxString msg = wxString::FromUTF8( aText );
116  wxString fullmsg;
117  fullmsg.Printf( wxT( "%g " ), aValue );
118  fullmsg += msg;
119  getTranslinePanel()->SetResult( aLineNumber, fullmsg );
120 }
121 
122 /* Returns a named property value. */
123 double GetPropertyInDialog( enum PRMS_ID aPrmId )
124 {
125  return getTranslinePanel()->GetPrmValue( aPrmId );
126 }
127 
128 // Returns true if the param aPrmId is selected
129 // Has meaning only for params that have a radio button
130 bool IsSelectedInDialog( enum PRMS_ID aPrmId )
131 {
132  return getTranslinePanel()->IsPrmSelected( aPrmId );
133 }
134 
135 
142 double PANEL_TRANSLINE::GetPrmValue( enum PRMS_ID aPrmId ) const
143 {
145 
146  for( unsigned ii = 0; ii < tr_ident->GetPrmsCount(); ii++ )
147  {
148  TRANSLINE_PRM* prm = tr_ident->GetPrm( ii );
149 
150  if( aPrmId == prm->m_Id )
151  return prm->m_NormalizedValue;
152  }
153 
154  return 1.0;
155 }
156 
163 void PANEL_TRANSLINE::SetPrmValue( enum PRMS_ID aPrmId, double aValue )
164 {
166 
167  for( unsigned ii = 0; ii < tr_ident->GetPrmsCount(); ii++ )
168  {
169  TRANSLINE_PRM* prm = tr_ident->GetPrm( ii );
170 
171  if( aPrmId == prm->m_Id )
172  {
173  prm->m_NormalizedValue = aValue;
174  prm->m_Value = prm->m_NormalizedValue * prm->ToUserUnit();
175  wxString msg;
176  msg.Printf( wxT( "%g" ), prm->m_Value );
177  ( (wxTextCtrl*) prm->m_ValueCtrl )->SetValue( msg );
178  return;
179  }
180  }
181  wxLogMessage( wxT( "GetPrmValue: prm %d not found" ), (int) aPrmId );
182 }
183 
190 void PANEL_TRANSLINE::SetPrmBgColor( enum PRMS_ID aPrmId, const KIGFX::COLOR4D* aCol )
191 {
192  wxColour wxcol = wxColour( static_cast<unsigned char>( aCol->r * 255 ),
193  static_cast<unsigned char>( aCol->g * 255 ),
194  static_cast<unsigned char>( aCol->b * 255 ) );
195 
196  if( !wxcol.IsOk() )
197  return;
198 
200 
201  for( unsigned ii = 0; ii < tr_ident->GetPrmsCount(); ii++ )
202  {
203  TRANSLINE_PRM* prm = tr_ident->GetPrm( ii );
204  wxTextCtrl* ctl = static_cast<wxTextCtrl*>( prm->m_ValueCtrl );
205 
206  if( aPrmId == prm->m_Id )
207  {
208  ctl->SetBackgroundColour( wxcol );
209  ctl->SetStyle( 0, -1, ctl->GetDefaultStyle() );
210  return;
211  }
212 
213  }
214 }
215 
222 void PANEL_TRANSLINE::SetResult( int aLineNumber, const wxString& aText )
223 {
224  #define MSG_CNT_MAX 8
225  wxStaticText* messages[MSG_CNT_MAX] =
229  };
230 
231  wxASSERT( ( aLineNumber >= 0 ) && ( aLineNumber < MSG_CNT_MAX ) );
232 
233  if( aLineNumber < 0 )
234  aLineNumber = 0;
235 
236  if( aLineNumber >= MSG_CNT_MAX )
237  aLineNumber = MSG_CNT_MAX - 1;
238 
239  messages[aLineNumber]->SetLabel( aText );
240 }
241 
247 bool PANEL_TRANSLINE::IsPrmSelected( enum PRMS_ID aPrmId ) const
248 {
249  switch( aPrmId )
250  {
251  default:
252  wxMessageBox( wxT( "IsPrmSelected() error" ) );
253  break;
254 
255  case PHYS_WIDTH_PRM:
256  case PHYS_DIAM_IN_PRM:
257  return m_radioBtnPrm1->GetValue();
258  break;
259 
260  case PHYS_S_PRM:
261  case PHYS_DIAM_OUT_PRM:
262  return m_radioBtnPrm2->GetValue();
263  break;
264  }
265  return false;
266 }
PCB calculator the main frame.
void SetPropertyBgColorInDialog(enum PRMS_ID aPrmId, const KIGFX::COLOR4D *aCol)
Function SetPropertyBgColorInDialog Set the background color of a parameter.
void SetResultInDialog(int line, const char *aText)
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:378
double b
Blue component.
Definition: color4d.h:379
wxRadioButton * m_radioBtnPrm1
PRMS_ID
Definition: transline.h:36
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:377
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