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
50 // An ugly fix needed by WxWidgets 2.9.1 that sometimes
51 // back to a point as separator, although the separator is the comma
52 buf.Replace( wxT( "," ), wxString( decimal_point, 1 ) );
53
54 /* Find the end of the numeric part
55 *(when units are append to the number, remove them)
56 */
57 unsigned brk_point = 0;
58
59 while( brk_point < buf.Len() )
60 {
61 wxChar ch = buf[brk_point];
62 if( !( ( ch >= '0' && ch <= '9' ) || ( ch == decimal_point ) || ( ch == '-' )
63 || ( ch == '+' ) || ( ch == 'e' ) || ( ch == 'E' ) ) )
64 {
65 break;
66 }
67 ++brk_point;
68 }
69
70 // Check for strings that cannot qualify as a number
71 if( brk_point == 0 )
72 return std::nan( "" );
73
74 /* Extract the numeric part */
75 if( !buf.Left( brk_point ).ToDouble( &value ) )
76 return std::nan( "" );
77
78 return value;
79}
80
81
82// A helper function to get a reference to the PANEL_TRANSLINE
84{
85 PCB_CALCULATOR_FRAME* frame = (PCB_CALCULATOR_FRAME*) wxTheApp->GetTopWindow();
86 PANEL_TRANSLINE* transline = frame->GetCalculator<PANEL_TRANSLINE>();
87
88 wxASSERT( transline );
89 return transline;
90}
91
92
93// Functions to Read/Write parameters in pcb_calculator main frame:
94// They are only wrapper to actual functions, so all transline functions do not
95// depend on Graphic User Interface
96void SetPropertyInDialog( enum PRMS_ID aPrmId, double value )
97{
98 getTranslinePanel()->SetPrmValue( aPrmId, value );
99}
100
101
102void SetPropertyBgColorInDialog( enum PRMS_ID aPrmId, const KIGFX::COLOR4D* aCol )
103{
104 getTranslinePanel()->SetPrmBgColor( aPrmId, aCol );
105}
106
110void SetResultInDialog( int line, const char* aText )
111{
112 wxString msg = wxString::FromUTF8( aText );
113 getTranslinePanel()->SetResult( line, msg );
114}
115
116
120void SetResultInDialog( int aLineNumber, double aValue, const char* aText )
121{
122 wxString msg = wxString::FromUTF8( aText );
123 wxString fullmsg;
124 fullmsg.Printf( wxT( "%g " ), aValue );
125 fullmsg += msg;
126 getTranslinePanel()->SetResult( aLineNumber, fullmsg );
127}
128
129
130/* Returns a named property value. */
131double GetPropertyInDialog( enum PRMS_ID aPrmId )
132{
133 return getTranslinePanel()->GetPrmValue( aPrmId );
134}
135
136
137// Returns true if the param aPrmId is selected
138// Has meaning only for params that have a radio button
139bool IsSelectedInDialog( enum PRMS_ID aPrmId )
140{
141 return getTranslinePanel()->IsPrmSelected( aPrmId );
142}
143
144
145double PANEL_TRANSLINE::GetPrmValue( enum PRMS_ID aPrmId ) const
146{
148
149 for( unsigned ii = 0; ii < tr_ident->GetPrmsCount(); ii++ )
150 {
151 TRANSLINE_PRM* prm = tr_ident->GetPrm( ii );
152
153 if( aPrmId == prm->m_Id )
154 return prm->m_NormalizedValue;
155 }
156
157 return 1.0;
158}
159
160
161void PANEL_TRANSLINE::SetPrmValue( enum PRMS_ID aPrmId, double aValue )
162{
164
165 for( unsigned ii = 0; ii < tr_ident->GetPrmsCount(); ii++ )
166 {
167 TRANSLINE_PRM* prm = tr_ident->GetPrm( ii );
168
169 if( aPrmId == prm->m_Id )
170 {
171 prm->m_NormalizedValue = aValue;
172 prm->m_Value = prm->m_NormalizedValue * prm->ToUserUnit();
173 wxString msg;
174 msg.Printf( wxT( "%g" ), prm->m_Value );
175 ( (wxTextCtrl*) prm->m_ValueCtrl )->SetValue( msg );
176 return;
177 }
178 }
179
180 wxLogMessage( wxT( "GetPrmValue: prm %d not found" ), (int) aPrmId );
181}
182
183
185{
186 wxColour wxcol = wxColour( static_cast<unsigned char>( aCol->r * 255 ),
187 static_cast<unsigned char>( aCol->g * 255 ),
188 static_cast<unsigned char>( aCol->b * 255 ) );
189
190 if( !wxcol.IsOk() )
191 return;
192
194
195 for( unsigned ii = 0; ii < tr_ident->GetPrmsCount(); ii++ )
196 {
197 TRANSLINE_PRM* prm = tr_ident->GetPrm( ii );
198 wxTextCtrl* ctl = static_cast<wxTextCtrl*>( prm->m_ValueCtrl );
199
200 if( aPrmId == prm->m_Id )
201 {
202 ctl->SetBackgroundColour( wxcol );
203 ctl->SetStyle( 0, -1, ctl->GetDefaultStyle() );
204 return;
205 }
206
207 }
208}
209
210
211void PANEL_TRANSLINE::SetResult( int aLineNumber, const wxString& aText )
212{
213#define MSG_CNT_MAX 10
214 wxStaticText* messages[MSG_CNT_MAX] = { m_Message1, m_Message2, m_Message3, m_Message4, m_Message5,
216
217 wxASSERT( ( aLineNumber >= 0 ) && ( aLineNumber < MSG_CNT_MAX ) );
218
219 if( aLineNumber < 0 )
220 aLineNumber = 0;
221
222 if( aLineNumber >= MSG_CNT_MAX )
223 aLineNumber = MSG_CNT_MAX - 1;
224
225 messages[aLineNumber]->SetLabel( aText );
226}
227
228
230{
231 switch( aPrmId )
232 {
233 default:
234 wxMessageBox( wxT( "IsPrmSelected() error" ) );
235 break;
236
237 case PHYS_WIDTH_PRM:
238 case PHYS_DIAM_IN_PRM:
239 return m_radioBtnPrm1->GetValue();
240 break;
241
242 case PHYS_S_PRM:
244 return m_radioBtnPrm2->GetValue();
245 break;
246 }
247 return false;
248}
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
double r
Red component.
Definition color4d.h:391
double g
Green component.
Definition color4d.h:392
double b
Blue component.
Definition color4d.h:393
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
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)
Set the background color of a parameter.
double GetPropertyInDialog(enum PRMS_ID aPrmId)
Return a named property value.
void SetResultInDialog(int line, const char *aText)
Puts the text into the given result line.
bool IsSelectedInDialog(enum PRMS_ID aPrmId)
Return true if the param aPrmId is selected.
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