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 
25 #include "pcb_calculator_frame.h"
26 #include "transline/transline.h"
27 
28 /*
29  * Return the value from a string,
30  * Unlike standard string to double convertion,
31  * both point and comma F.P. separator are accepted
32  * and values having units (like 4.7 K) are accepted
33  * but units are ignored.
34  * notation like 1e+3 is legal
35  */
36 double DoubleFromString( const wxString& TextValue )
37 {
38  double value = 0;
39 
40  /* Acquire the 'right' decimal point separator */
41  const struct lconv* lc = localeconv();
42  wxChar decimal_point = lc->decimal_point[0];
43  wxString buf( TextValue.Strip( wxString::both ) );
44 
45  /* Convert the period in decimal point */
46  buf.Replace( wxT( "." ), wxString( decimal_point, 1 ) );
47  // An ugly fix needed by WxWidgets 2.9.1 that sometimes
48  // back to a point as separator, although the separator is the comma
49  buf.Replace( wxT( "," ), wxString( decimal_point, 1 ) );
50 
51  /* Find the end of the numeric part
52  *(when units are append to the number, remove them)
53  */
54  unsigned brk_point = 0;
55  while( brk_point < buf.Len() )
56  {
57  wxChar ch = buf[brk_point];
58  if( !( ( ch >= '0' && ch <= '9' ) || ( ch == decimal_point ) || ( ch == '-' )
59  || ( ch == '+' ) || ( ch == 'e' ) || ( ch == 'E' ) ) )
60  {
61  break;
62  }
63  ++brk_point;
64  }
65 
66  // Check for strings that cannot qualify as a number
67  if( brk_point == 0 )
68  {
69  return std::nan( "" );
70  }
71 
72  /* Extract the numeric part */
73  if( !buf.Left( brk_point ).ToDouble( &value ) )
74  {
75  return std::nan( "" );
76  }
77  return value;
78 }
79 
80 
81 // Functions to Read/Write parameters in pcb_calculator main frame:
82 // They are only wrapper to actual functions, so all transline functions do not
83 // depend on Graphic User Interface
84 void SetPropertyInDialog( enum PRMS_ID aPrmId, double value )
85 {
86  PCB_CALCULATOR_FRAME* frame = (PCB_CALCULATOR_FRAME*) wxTheApp->GetTopWindow();
87  frame->SetPrmValue( aPrmId, value );
88 }
89 
90 void SetPropertyBgColorInDialog( enum PRMS_ID aPrmId, const KIGFX::COLOR4D* aCol )
91 {
92  PCB_CALCULATOR_FRAME* frame = (PCB_CALCULATOR_FRAME*) wxTheApp->GetTopWindow();
93  frame->SetPrmBgColor( aPrmId, aCol );
94 }
95 
96 /* Puts the text into the given result line.
97 */
98 void SetResultInDialog( int line, const char* aText )
99 {
100  PCB_CALCULATOR_FRAME* frame = (PCB_CALCULATOR_FRAME*) wxTheApp->GetTopWindow();
101  wxString msg = wxString::FromUTF8( aText );
102  frame->SetResult( line, msg );
103 }
104 
105 /* print aValue into the given result line.
106 */
107 void SetResultInDialog( int aLineNumber, double aValue, const char* aText )
108 {
109  PCB_CALCULATOR_FRAME* frame = (PCB_CALCULATOR_FRAME*) wxTheApp->GetTopWindow();
110  wxString msg = wxString::FromUTF8( aText );
111  wxString fullmsg;
112  fullmsg.Printf( wxT( "%g " ), aValue );
113  fullmsg += msg;
114  frame->SetResult( aLineNumber, fullmsg );
115 }
116 
117 /* Returns a named property value. */
118 double GetPropertyInDialog( enum PRMS_ID aPrmId )
119 {
120  PCB_CALCULATOR_FRAME* frame = (PCB_CALCULATOR_FRAME*) wxTheApp->GetTopWindow();
121  return frame->GetPrmValue( aPrmId );
122 }
123 
124 // Returns true if the param aPrmId is selected
125 // Has meaning only for params that have a radio button
126 bool IsSelectedInDialog( enum PRMS_ID aPrmId )
127 {
128  PCB_CALCULATOR_FRAME* frame = (PCB_CALCULATOR_FRAME*) wxTheApp->GetTopWindow();
129  return frame->IsPrmSelected( aPrmId );
130 }
131 
132 
139 double PCB_CALCULATOR_FRAME::GetPrmValue( enum PRMS_ID aPrmId ) const
140 {
142  for( unsigned ii = 0; ii < tr_ident->GetPrmsCount(); ii++ )
143  {
144  TRANSLINE_PRM* prm = tr_ident->GetPrm( ii );
145  if( aPrmId == prm->m_Id )
146  return prm->m_NormalizedValue;
147  }
148  return 1.0;
149 }
150 
157 void PCB_CALCULATOR_FRAME::SetPrmValue( enum PRMS_ID aPrmId, double aValue )
158 {
160  for( unsigned ii = 0; ii < tr_ident->GetPrmsCount(); ii++ )
161  {
162  TRANSLINE_PRM* prm = tr_ident->GetPrm( ii );
163 
164  if( aPrmId == prm->m_Id )
165  {
166  prm->m_NormalizedValue = aValue;
167  prm->m_Value = prm->m_NormalizedValue * prm->ToUserUnit();
168  wxString msg;
169  msg.Printf( wxT( "%g" ), prm->m_Value );
170  ( (wxTextCtrl*) prm->m_ValueCtrl )->SetValue( msg );
171  return;
172  }
173  }
174  wxLogMessage( wxT( "GetPrmValue: prm %d not found" ), (int) aPrmId );
175 }
176 
184 {
185  wxColour wxcol = wxColour( static_cast<unsigned char>( aCol->r * 255 ),
186  static_cast<unsigned char>( aCol->g * 255 ),
187  static_cast<unsigned char>( aCol->b * 255 ) );
188 
189  if( !wxcol.IsOk() )
190  {
191  return;
192  }
193 
195 
196  for( unsigned ii = 0; ii < tr_ident->GetPrmsCount(); ii++ )
197  {
198  TRANSLINE_PRM* prm = tr_ident->GetPrm( ii );
199  wxTextCtrl* ctl = static_cast<wxTextCtrl*>( prm->m_ValueCtrl );
200 
201  if( aPrmId == prm->m_Id )
202  {
203  ctl->SetBackgroundColour( wxcol );
204  ctl->SetStyle( 0, -1, ctl->GetDefaultStyle() );
205  return;
206  }
207 
208  }
209 }
210 
217 void PCB_CALCULATOR_FRAME::SetResult( int aLineNumber, const wxString& aText )
218 {
219  #define MSG_CNT_MAX 7
220  wxStaticText* messages[MSG_CNT_MAX] =
223  m_Message7
224  };
225 
226  wxASSERT( ( aLineNumber >= 0 ) && ( aLineNumber < MSG_CNT_MAX ) );
227 
228  if( aLineNumber < 0 )
229  aLineNumber = 0;
230  if( aLineNumber >= MSG_CNT_MAX )
231  aLineNumber = MSG_CNT_MAX - 1;
232 
233  messages[aLineNumber]->SetLabel( aText );
234 }
235 
242 {
243  switch( aPrmId )
244  {
245  default:
246  wxMessageBox( wxT( "IsPrmSelected() error" ) );
247  break;
248 
249  case PHYS_WIDTH_PRM:
250  case PHYS_DIAM_IN_PRM:
251  return m_radioBtnPrm1->GetValue();
252  break;
253 
254  case PHYS_S_PRM:
255  case PHYS_DIAM_OUT_PRM:
256  return m_radioBtnPrm2->GetValue();
257  break;
258  }
259  return false;
260 }
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
#define MSG_CNT_MAX
double g
Green component.
Definition: color4d.h:359
void SetResult(int aLineNumber, const wxString &aText)
Function SetResult Puts the text into the given result line.
double b
Blue component.
Definition: color4d.h:360
bool IsPrmSelected(enum PRMS_ID aPrmId) const
Function IsPrmSelected.
void SetPrmBgColor(enum PRMS_ID aPrmId, const KIGFX::COLOR4D *aCol)
Function SetPrgmBgColor Set the background color of a parameter.
PRMS_ID
Definition: transline.h:37
unsigned GetPrmsCount() const
enum TRANSLINE_TYPE_ID m_currTransLineType
double GetPrmValue(enum PRMS_ID aPrmId) const
Function GetPrmValue Returns a param value.
TRANSLINE_PRM * GetPrm(unsigned aIdx) const
bool IsSelectedInDialog(enum PRMS_ID aPrmId)
void SetPropertyInDialog(enum PRMS_ID aPrmId, double value)
std::vector< TRANSLINE_IDENT * > m_transline_list
double r
Red component.
Definition: color4d.h:358
void SetPrmValue(enum PRMS_ID aPrmId, double aValue)
Function SetPrmValue Read/write params values and results.
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:98