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