KiCad PCB EDA Suite
config_params.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) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
5  * Copyright (C) 2008-2011 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <fctsys.h>
27 #include <gr_basic.h>
28 #include <pgm_base.h>
29 #include <common.h>
30 #include <kicad_string.h>
31 #include <gestfich.h>
32 #include <wxstruct.h>
33 #include <config_params.h>
34 
35 #include <wx/apptrait.h>
36 #include <wx/stdpaths.h>
37 
39 
40 
41 void wxConfigLoadParams( wxConfigBase* aCfg,
42  const PARAM_CFG_ARRAY& aList, const wxString& aGroup )
43 {
44  wxASSERT( aCfg );
45 
46  for( const PARAM_CFG_BASE& param : aList )
47  {
48  if( !!param.m_Group )
49  aCfg->SetPath( param.m_Group );
50  else
51  aCfg->SetPath( aGroup );
52 
53  if( param.m_Setup )
54  continue;
55 
56  param.ReadParam( aCfg );
57  }
58 }
59 
60 
61 void wxConfigLoadSetups( wxConfigBase* aCfg, const PARAM_CFG_ARRAY& aList )
62 {
63  wxASSERT( aCfg );
64 
65  for( const PARAM_CFG_BASE& param : aList )
66  {
67  if( !param.m_Setup )
68  continue;
69 
70  param.ReadParam( aCfg );
71  }
72 }
73 
74 
75 void wxConfigSaveParams( wxConfigBase* aCfg,
76  const PARAM_CFG_ARRAY& aList, const wxString& aGroup )
77 {
78  wxASSERT( aCfg );
79 
80  for( const PARAM_CFG_BASE& param : aList )
81  {
82  if( !!param.m_Group )
83  aCfg->SetPath( param.m_Group );
84  else
85  aCfg->SetPath( aGroup );
86 
87  if( param.m_Setup )
88  continue;
89 
90  if( param.m_Type == PARAM_COMMAND_ERASE ) // Erase all data
91  {
92  if( !!param.m_Ident )
93  aCfg->DeleteGroup( param.m_Ident );
94  }
95  else
96  {
97  param.SaveParam( aCfg );
98  }
99  }
100 }
101 
102 
103 void wxConfigSaveSetups( wxConfigBase* aCfg, const PARAM_CFG_ARRAY& aList )
104 {
105  wxASSERT( aCfg );
106 
107  for( const PARAM_CFG_BASE& param : aList )
108  {
109  if( !param.m_Setup )
110  continue;
111 
112  if( param.m_Type == PARAM_COMMAND_ERASE ) // Erase all data
113  {
114  if( !!param.m_Ident )
115  aCfg->DeleteGroup( param.m_Ident );
116  }
117  else
118  {
119  param.SaveParam( aCfg );
120  }
121  }
122 }
123 
124 
125 void ConfigBaseWriteDouble( wxConfigBase* aConfig, const wxString& aKey, double aValue )
126 {
127  // Use a single strategy, regardless of wx version.
128  // Want C locale float string.
129 
130  LOCALE_IO toggle;
131  wxString tnumber = wxString::Format( wxT( "%.16g" ), aValue );
132 
133  aConfig->Write( aKey, tnumber );
134 }
135 
136 
137 PARAM_CFG_BASE::PARAM_CFG_BASE( const wxString& ident, const paramcfg_id type,
138  const wxChar* group )
139 {
140  m_Ident = ident;
141  m_Type = type;
142  m_Group = group;
143  m_Setup = false;
144 }
145 
146 
147 PARAM_CFG_INT::PARAM_CFG_INT( const wxString& ident, int* ptparam,
148  int default_val, int min, int max,
149  const wxChar* group ) :
150  PARAM_CFG_BASE( ident, PARAM_INT, group )
151 {
152  m_Pt_param = ptparam;
153  m_Default = default_val;
154  m_Min = min;
155  m_Max = max;
156 }
157 
158 
159 PARAM_CFG_INT::PARAM_CFG_INT( bool Insetup, const wxString& ident, int* ptparam,
160  int default_val, int min, int max,
161  const wxChar* group ) :
162  PARAM_CFG_BASE( ident, PARAM_INT, group )
163 {
164  m_Pt_param = ptparam;
165  m_Default = default_val;
166  m_Min = min;
167  m_Max = max;
168  m_Setup = Insetup;
169 }
170 
171 
172 void PARAM_CFG_INT::ReadParam( wxConfigBase* aConfig ) const
173 {
174  if( !m_Pt_param || !aConfig )
175  return;
176 
177  int itmp = aConfig->Read( m_Ident, m_Default );
178 
179  if( (itmp < m_Min) || (itmp > m_Max) )
180  itmp = m_Default;
181 
182  *m_Pt_param = itmp;
183 }
184 
185 
186 void PARAM_CFG_INT::SaveParam( wxConfigBase* aConfig ) const
187 {
188  if( !m_Pt_param || !aConfig )
189  return;
190 
191  aConfig->Write( m_Ident, *m_Pt_param );
192 }
193 
194 
195 PARAM_CFG_INT_WITH_SCALE::PARAM_CFG_INT_WITH_SCALE( const wxString& ident, int* ptparam,
196  int default_val, int min, int max,
197  const wxChar* group, double aBiu2cfgunit ) :
198  PARAM_CFG_INT( ident, ptparam, default_val, min, max, group )
199 {
201  m_BIU_to_cfgunit = aBiu2cfgunit;
202 }
203 
204 
206  const wxString& ident, int* ptparam,
207  int default_val, int min, int max,
208  const wxChar* group, double aBiu2cfgunit ) :
209  PARAM_CFG_INT( Insetup, ident, ptparam, default_val, min, max, group )
210 {
212  m_BIU_to_cfgunit = aBiu2cfgunit;
213 }
214 
215 
216 void PARAM_CFG_INT_WITH_SCALE::ReadParam( wxConfigBase* aConfig ) const
217 {
218  if( !m_Pt_param || !aConfig )
219  return;
220 
221  double dtmp = (double) m_Default * m_BIU_to_cfgunit;
222  aConfig->Read( m_Ident, &dtmp );
223 
224  int itmp = KiROUND( dtmp / m_BIU_to_cfgunit );
225 
226  if( (itmp < m_Min) || (itmp > m_Max) )
227  itmp = m_Default;
228 
229  *m_Pt_param = itmp;
230 }
231 
232 
233 void PARAM_CFG_INT_WITH_SCALE::SaveParam( wxConfigBase* aConfig ) const
234 {
235  if( !m_Pt_param || !aConfig )
236  return;
237 
238  // We cannot use aConfig->Write for a double, because
239  // this function uses a format with very few digits in mantissa,
240  // and truncature issues are frequent.
241  // We uses our function.
243 }
244 
245 
246 PARAM_CFG_SETCOLOR::PARAM_CFG_SETCOLOR( const wxString& ident, COLOR4D* ptparam,
247  COLOR4D default_val,
248  const wxChar* group ) :
249  PARAM_CFG_BASE( ident, PARAM_SETCOLOR, group )
250 {
251  m_Pt_param = ptparam;
252  m_Default = default_val;
253 }
254 
255 
257  const wxString& ident,
258  COLOR4D* ptparam,
259  COLOR4D default_val,
260  const wxChar* group ) :
261  PARAM_CFG_BASE( ident, PARAM_SETCOLOR, group )
262 {
263  m_Pt_param = ptparam;
264  m_Default = default_val;
265  m_Setup = Insetup;
266 }
267 
268 
269 void PARAM_CFG_SETCOLOR::ReadParam( wxConfigBase* aConfig ) const
270 {
271  if( !m_Pt_param || !aConfig )
272  return;
273 
274  // First try reading old format
275  EDA_COLOR_T itmp = ColorByName( aConfig->Read( m_Ident, wxT( "NONE" ) ) );
276  COLOR4D wtmp = COLOR4D::UNSPECIFIED;
277 
278  if( itmp == UNSPECIFIED_COLOR )
279  {
280  // Next try reading new format
281  if( !wtmp.SetFromWxString( aConfig->Read( m_Ident, wxT( "NONE" ) ) ) )
282  wtmp = m_Default;
283  }
284  else
285  {
286  wtmp = COLOR4D( itmp );
287  }
288 
289  *m_Pt_param = wtmp;
290 }
291 
292 
293 void PARAM_CFG_SETCOLOR::SaveParam( wxConfigBase* aConfig ) const
294 {
295  if( !m_Pt_param || !aConfig )
296  return;
297 
298  aConfig->Write( m_Ident, m_Pt_param->ToColour().GetAsString( wxC2S_CSS_SYNTAX ) );
299 }
300 
301 
302 PARAM_CFG_DOUBLE::PARAM_CFG_DOUBLE( const wxString& ident, double* ptparam,
303  double default_val, double min, double max,
304  const wxChar* group ) :
305  PARAM_CFG_BASE( ident, PARAM_DOUBLE, group )
306 {
307  m_Pt_param = ptparam;
308  m_Default = default_val;
309  m_Min = min;
310  m_Max = max;
311 }
312 
313 
315  const wxString& ident,
316  double* ptparam,
317  double default_val,
318  double min,
319  double max,
320  const wxChar* group ) :
321  PARAM_CFG_BASE( ident, PARAM_DOUBLE, group )
322 {
323  m_Pt_param = ptparam;
324  m_Default = default_val;
325  m_Min = min;
326  m_Max = max;
327  m_Setup = Insetup;
328 }
329 
330 
331 void PARAM_CFG_DOUBLE::ReadParam( wxConfigBase* aConfig ) const
332 {
333  if( !m_Pt_param || !aConfig )
334  return;
335 
336  double dtmp = m_Default;
337  aConfig->Read( m_Ident, &dtmp );
338 
339  if( (dtmp < m_Min) || (dtmp > m_Max) )
340  dtmp = m_Default;
341 
342  *m_Pt_param = dtmp;
343 }
344 
345 
346 void PARAM_CFG_DOUBLE::SaveParam( wxConfigBase* aConfig ) const
347 {
348  if( !m_Pt_param || !aConfig )
349  return;
350 
351  // We cannot use aConfig->Write for a double, because
352  // this function uses a format with very few digits in mantissa,
353  // and truncature issues are frequent.
354  // We uses our function.
356 }
357 
358 
359 PARAM_CFG_BOOL::PARAM_CFG_BOOL( const wxString& ident, bool* ptparam,
360  int default_val, const wxChar* group ) :
361  PARAM_CFG_BASE( ident, PARAM_BOOL, group )
362 {
363  m_Pt_param = ptparam;
364  m_Default = default_val ? true : false;
365 }
366 
367 
369  const wxString& ident,
370  bool* ptparam,
371  int default_val,
372  const wxChar* group ) :
373  PARAM_CFG_BASE( ident, PARAM_BOOL, group )
374 {
375  m_Pt_param = ptparam;
376  m_Default = default_val ? true : false;
377  m_Setup = Insetup;
378 }
379 
380 
381 void PARAM_CFG_BOOL::ReadParam( wxConfigBase* aConfig ) const
382 {
383  if( !m_Pt_param || !aConfig )
384  return;
385 
386  int itmp = aConfig->Read( m_Ident, (int) m_Default );
387 
388  *m_Pt_param = itmp ? true : false;
389 }
390 
391 
392 void PARAM_CFG_BOOL::SaveParam( wxConfigBase* aConfig ) const
393 {
394  if( !m_Pt_param || !aConfig )
395  return;
396 
397  aConfig->Write( m_Ident, *m_Pt_param );
398 }
399 
400 
402  wxString* ptparam,
403  const wxChar* group ) :
404  PARAM_CFG_BASE( ident, PARAM_WXSTRING, group )
405 {
406  m_Pt_param = ptparam;
407 }
408 
409 
410 PARAM_CFG_WXSTRING::PARAM_CFG_WXSTRING( bool Insetup, const wxString& ident,
411  wxString* ptparam,
412  const wxString& default_val,
413  const wxChar* group ) :
414  PARAM_CFG_BASE( ident, PARAM_WXSTRING, group )
415 {
416  m_Pt_param = ptparam;
417  m_Setup = Insetup;
418  m_default = default_val;
419 }
420 
421 
422 void PARAM_CFG_WXSTRING::ReadParam( wxConfigBase* aConfig ) const
423 {
424  if( !m_Pt_param || !aConfig )
425  return;
426 
427  *m_Pt_param = aConfig->Read( m_Ident, m_default );
428 }
429 
430 
431 void PARAM_CFG_WXSTRING::SaveParam( wxConfigBase* aConfig ) const
432 {
433  if( !m_Pt_param || !aConfig )
434  return;
435 
436  aConfig->Write( m_Ident, *m_Pt_param );
437 }
438 
439 
441  wxString* ptparam,
442  const wxChar* group ) :
443  PARAM_CFG_BASE( ident, PARAM_FILENAME, group )
444 {
445  m_Pt_param = ptparam;
446 }
447 
448 
449 void PARAM_CFG_FILENAME::ReadParam( wxConfigBase* aConfig ) const
450 {
451  if( !m_Pt_param || !aConfig )
452  return;
453 
454  wxString prm = aConfig->Read( m_Ident );
455  // file names are stored using Unix notation
456  // under Window we must use \ instead of /
457  // mainly if there is a server name in path (something like \\server\kicad)
458 #ifdef __WINDOWS__
459  prm.Replace(wxT("/"), wxT("\\"));
460 #endif
461  *m_Pt_param = prm;
462 }
463 
464 
465 void PARAM_CFG_FILENAME::SaveParam( wxConfigBase* aConfig ) const
466 {
467  if( !m_Pt_param || !aConfig )
468  return;
469 
470  wxString prm = *m_Pt_param;
471  // filenames are stored using Unix notation
472  prm.Replace(wxT("\\"), wxT("/") );
473  aConfig->Write( m_Ident, prm );
474 }
475 
476 
478  wxArrayString* ptparam,
479  const wxChar* group ) :
480  PARAM_CFG_BASE( ident, PARAM_LIBNAME_LIST, group )
481 {
482  m_Pt_param = ptparam;
483 }
484 
485 
486 void PARAM_CFG_LIBNAME_LIST::ReadParam( wxConfigBase* aConfig ) const
487 {
488  if( !m_Pt_param || !aConfig )
489  return;
490 
491  int indexlib = 1; // We start indexlib to 1 because first
492  // lib name is LibName1
493  wxString libname, id_lib;
494  wxArrayString* libname_list = m_Pt_param;
495 
496  while( 1 )
497  {
498  id_lib = m_Ident;
499  id_lib << indexlib;
500  indexlib++;
501  libname = aConfig->Read( id_lib, wxT( "" ) );
502 
503  if( libname.IsEmpty() )
504  break;
505  // file names are stored using Unix notation
506  // under Window we must use \ instead of /
507  // mainly if there is a server name in path (something like \\server\kicad)
508 #ifdef __WINDOWS__
509  libname.Replace(wxT("/"), wxT("\\"));
510 #endif
511  libname_list->Add( libname );
512  }
513 }
514 
515 
516 void PARAM_CFG_LIBNAME_LIST::SaveParam( wxConfigBase* aConfig ) const
517 {
518  if( !m_Pt_param || !aConfig )
519  return;
520 
521  wxArrayString* libname_list = m_Pt_param;
522 
523  wxString configkey;
524  wxString libname;
525 
526  for( unsigned indexlib = 0; indexlib < libname_list->GetCount(); indexlib++ )
527  {
528  configkey = m_Ident;
529 
530  // We use indexlib+1 because first lib name is LibName1
531  configkey << (indexlib + 1);
532  libname = libname_list->Item( indexlib );
533 
534  // filenames are stored using Unix notation
535  libname.Replace(wxT("\\"), wxT("/") );
536  aConfig->Write( configkey, libname );
537  }
538 }
virtual void SaveParam(wxConfigBase *aConfig) const override
Function SaveParam saves the value of the parameter stored in aConfig.
A list of parameters type.
int m_Default
The default value of the parameter.
EDA_COLOR_T
NOTE: EDA_COLOR_T is deprecated and is kept around for compatibility with legacy canvas.
Definition: colors.h:42
wxString * m_Pt_param
Pointer to the parameter value.
EDA_COLOR_T ColorByName(const wxString &aName)
Find a color by name.
Definition: colors.cpp:72
virtual void ReadParam(wxConfigBase *aConfig) const override
Function ReadParam reads the value of the parameter stored in aConfig.
COLOR4D * m_Pt_param
Pointer to the parameter value.
This file is part of the common library TODO brief description.
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:128
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:195
wxString m_default
The default value of the parameter.
virtual void ReadParam(wxConfigBase *aConfig) const override
Function ReadParam reads the value of the parameter stored in aConfig.
virtual void SaveParam(wxConfigBase *aConfig) const override
Function SaveParam saves the value of the parameter stored in aConfig.
int m_Default
The default value of the parameter.
The common library.
void wxConfigSaveParams(wxConfigBase *aCfg, const PARAM_CFG_ARRAY &aList, const wxString &aGroup)
Function wxConfigSaveParams writes aList of PARAM_CFG_ARRAY elements to save configuration values to ...
PARAM_CFG_BOOL(const wxString &ident, bool *ptparam, int default_val=false, const wxChar *group=NULL)
double * m_Pt_param
Pointer to the parameter value.
int * m_Pt_param
Pointer to the parameter value.
void wxConfigLoadParams(wxConfigBase *aCfg, const PARAM_CFG_ARRAY &aList, const wxString &aGroup)
Function wxConfigLoadParams uses aList of PARAM_CFG_ARRAY elements to load configuration values from ...
bool m_Setup
Install or Project based parameter, true == install.
double m_BIU_to_cfgunit
the factor to convert the saved value in internal value
PARAM_CFG_LIBNAME_LIST(const wxChar *ident, wxArrayString *ptparam, const wxChar *group=NULL)
PARAM_CFG_INT(const wxString &ident, int *ptparam, int default_val=0, int min=std::numeric_limits< int >::min(), int max=std::numeric_limits< int >::max(), const wxChar *group=NULL)
wxArrayString * m_Pt_param
Pointer to the parameter value.
Base window classes and related definitions.
virtual void ReadParam(wxConfigBase *aConfig) const override
Function ReadParam reads the value of the parameter stored in aConfig.
virtual void SaveParam(wxConfigBase *aConfig) const override
Function SaveParam saves the value of the parameter stored in aConfig.
PARAM_CFG_INT_WITH_SCALE(const wxString &ident, int *ptparam, int default_val=0, int min=std::numeric_limits< int >::min(), int max=std::numeric_limits< int >::max(), const wxChar *group=NULL, double aBiu2cfgunit=1.0)
Configuration parameter - Integer Class.
The common library.
int m_Max
Minimum and maximum values of the param type.
virtual void SaveParam(wxConfigBase *aConfig) const override
Function SaveParam saves the value of the parameter stored in aConfig.
double m_Max
Minimum and maximum values of the param type.
Class PARAM_CFG_BASE is a base class which establishes the interface functions ReadParam and SavePara...
Definition: config_params.h:94
paramcfg_id
Type of parameter in the configuration file.
Definition: config_params.h:71
virtual void SaveParam(wxConfigBase *aConfig) const override
Function SaveParam saves the value of the parameter stored in aConfig.
double m_Default
The default value of the parameter.
PARAM_CFG_WXSTRING(const wxString &ident, wxString *ptparam, const wxChar *group=NULL)
bool * m_Pt_param
Pointer to the parameter value.
virtual void SaveParam(wxConfigBase *aConfig) const override
Function SaveParam saves the value of the parameter stored in aConfig.
void ConfigBaseWriteDouble(wxConfigBase *aConfig, const wxString &aKey, double aValue)
Function ConfigBaseWriteDouble This is a helper function to write doubles in config We cannot use wxC...
virtual void ReadParam(wxConfigBase *aConfig) const override
Function ReadParam reads the value of the parameter stored in aConfig.
COLOR4D m_Default
The default value of the parameter.
void wxConfigSaveSetups(wxConfigBase *aCfg, const PARAM_CFG_ARRAY &aList)
Function wxConfigSaveSetups writes aList of PARAM_CFG_ARRAY elements to save configuration values to ...
virtual void ReadParam(wxConfigBase *aConfig) const override
Function ReadParam reads the value of the parameter stored in aConfig.
see class PGM_BASE
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
#define max(a, b)
Definition: auxiliary.h:86
virtual void SaveParam(wxConfigBase *aConfig) const override
Function SaveParam saves the value of the parameter stored in aConfig.
wxString * m_Pt_param
Pointer to the parameter value.
The common library.
PARAM_CFG_SETCOLOR(const wxString &ident, COLOR4D *ptparam, COLOR4D default_val, const wxChar *group=NULL)
virtual void ReadParam(wxConfigBase *aConfig) const override
Function ReadParam reads the value of the parameter stored in aConfig.
PARAM_CFG_DOUBLE(const wxString &ident, double *ptparam, double default_val=0.0, double min=0.0, double max=10000.0, const wxChar *group=NULL)
PARAM_CFG_BASE(const wxString &ident, const paramcfg_id type, const wxChar *group=NULL)
virtual void ReadParam(wxConfigBase *aConfig) const override
Function ReadParam reads the value of the parameter stored in aConfig.
virtual void ReadParam(wxConfigBase *aConfig) const override
Function ReadParam reads the value of the parameter stored in aConfig.
PARAM_CFG_FILENAME(const wxString &ident, wxString *ptparam, const wxChar *group=NULL)
virtual void SaveParam(wxConfigBase *aConfig) const override
Function SaveParam saves the value of the parameter stored in aConfig.
wxString m_Group
Group name (this is like a path in the config data)
Definition: config_params.h:99
void wxConfigLoadSetups(wxConfigBase *aCfg, const PARAM_CFG_ARRAY &aList)
Function wxConfigLoadSetups uses aList of PARAM_CFG_ARRAY elements to load configuration values from ...
paramcfg_id m_Type
Type of parameter.
Definition: config_params.h:98
wxString m_Ident
Keyword in config data.
Definition: config_params.h:97
#define min(a, b)
Definition: auxiliary.h:85
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39