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 Wayne Stambaugh <stambaughw@gmail.com>
6  * Copyright (C) 1992-2017 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 <eda_base_frame.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, const wxString& legacy )
139 {
140  m_Ident = ident;
141  m_Type = type;
142  m_Group = group;
143  m_Setup = false;
144 
145  m_Ident_legacy = legacy;
146 }
147 
148 
149 PARAM_CFG_INT::PARAM_CFG_INT( const wxString& ident, int* ptparam, int default_val,
150  int min, int max, const wxChar* group, const wxString& legacy ) :
151  PARAM_CFG_BASE( ident, PARAM_INT, group, legacy )
152 {
153  m_Pt_param = ptparam;
154  m_Default = default_val;
155  m_Min = min;
156  m_Max = max;
157 }
158 
159 
160 PARAM_CFG_INT::PARAM_CFG_INT( bool setup, const wxString& ident, int* ptparam, int default_val,
161  int min, int max, const wxChar* group, const wxString& legacy ) :
162  PARAM_CFG_BASE( ident, PARAM_INT, group, legacy )
163 {
164  m_Pt_param = ptparam;
165  m_Default = default_val;
166  m_Min = min;
167  m_Max = max;
168  m_Setup = setup;
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 = m_Default;
178 
179  if( !aConfig->Read( m_Ident, &itmp ) && m_Ident_legacy != wxEmptyString )
180  aConfig->Read( m_Ident_legacy, &itmp );
181 
182  if( (itmp < m_Min) || (itmp > m_Max) )
183  itmp = m_Default;
184 
185  *m_Pt_param = itmp;
186 }
187 
188 
189 void PARAM_CFG_INT::SaveParam( wxConfigBase* aConfig ) const
190 {
191  if( !m_Pt_param || !aConfig )
192  return;
193 
194  aConfig->Write( m_Ident, *m_Pt_param );
195 }
196 
197 
198 PARAM_CFG_INT_WITH_SCALE::PARAM_CFG_INT_WITH_SCALE( const wxString& ident, int* ptparam,
199  int default_val, int min, int max,
200  const wxChar* group, double aBiu2cfgunit,
201  const wxString& legacy_ident ) :
202  PARAM_CFG_INT( ident, ptparam, default_val, min, max, group, legacy_ident )
203 {
205  m_BIU_to_cfgunit = aBiu2cfgunit;
206 }
207 
208 
209 PARAM_CFG_INT_WITH_SCALE::PARAM_CFG_INT_WITH_SCALE( bool setup, const wxString& ident, int* ptparam,
210  int default_val, int min, int max,
211  const wxChar* group, double aBiu2cfgunit,
212  const wxString& legacy_ident ) :
213  PARAM_CFG_INT( setup, ident, ptparam, default_val, min, max, group, legacy_ident )
214 {
216  m_BIU_to_cfgunit = aBiu2cfgunit;
217 }
218 
219 
220 void PARAM_CFG_INT_WITH_SCALE::ReadParam( wxConfigBase* aConfig ) const
221 {
222  if( !m_Pt_param || !aConfig )
223  return;
224 
225  double dtmp = (double) m_Default * m_BIU_to_cfgunit;
226  if( !aConfig->Read( m_Ident, &dtmp ) && m_Ident_legacy != wxEmptyString )
227  aConfig->Read( m_Ident_legacy, &dtmp );
228 
229  int itmp = KiROUND( dtmp / m_BIU_to_cfgunit );
230 
231  if( (itmp < m_Min) || (itmp > m_Max) )
232  itmp = m_Default;
233 
234  *m_Pt_param = itmp;
235 }
236 
237 
238 void PARAM_CFG_INT_WITH_SCALE::SaveParam( wxConfigBase* aConfig ) const
239 {
240  if( !m_Pt_param || !aConfig )
241  return;
242 
243  // We cannot use aConfig->Write for a double, because
244  // this function uses a format with very few digits in mantissa,
245  // and truncature issues are frequent.
246  // We uses our function.
248 }
249 
250 
251 PARAM_CFG_SETCOLOR::PARAM_CFG_SETCOLOR( const wxString& ident, COLOR4D* ptparam,
252  COLOR4D default_val,
253  const wxChar* group ) :
254  PARAM_CFG_BASE( ident, PARAM_SETCOLOR, group )
255 {
256  m_Pt_param = ptparam;
257  m_Default = default_val;
258 }
259 
260 
262  const wxString& ident,
263  COLOR4D* ptparam,
264  COLOR4D default_val,
265  const wxChar* group ) :
266  PARAM_CFG_BASE( ident, PARAM_SETCOLOR, group )
267 {
268  m_Pt_param = ptparam;
269  m_Default = default_val;
270  m_Setup = Insetup;
271 }
272 
273 
274 void PARAM_CFG_SETCOLOR::ReadParam( wxConfigBase* aConfig ) const
275 {
276  if( !m_Pt_param || !aConfig )
277  return;
278 
279  // First try reading old format
280  EDA_COLOR_T itmp = ColorByName( aConfig->Read( m_Ident, wxT( "NONE" ) ) );
281  COLOR4D wtmp = COLOR4D::UNSPECIFIED;
282 
283  if( itmp == UNSPECIFIED_COLOR )
284  {
285  // Next try reading new format
286  if( !wtmp.SetFromWxString( aConfig->Read( m_Ident, wxT( "NONE" ) ) ) )
287  wtmp = m_Default;
288  }
289  else
290  {
291  wtmp = COLOR4D( itmp );
292  }
293 
294  *m_Pt_param = wtmp;
295 }
296 
297 
298 void PARAM_CFG_SETCOLOR::SaveParam( wxConfigBase* aConfig ) const
299 {
300  if( !m_Pt_param || !aConfig )
301  return;
302 
303  aConfig->Write( m_Ident, m_Pt_param->ToColour().GetAsString( wxC2S_CSS_SYNTAX ) );
304 }
305 
306 
307 PARAM_CFG_DOUBLE::PARAM_CFG_DOUBLE( const wxString& ident, double* ptparam,
308  double default_val, double min, double max,
309  const wxChar* group ) :
310  PARAM_CFG_BASE( ident, PARAM_DOUBLE, group )
311 {
312  m_Pt_param = ptparam;
313  m_Default = default_val;
314  m_Min = min;
315  m_Max = max;
316 }
317 
318 
320  const wxString& ident,
321  double* ptparam,
322  double default_val,
323  double min,
324  double max,
325  const wxChar* group ) :
326  PARAM_CFG_BASE( ident, PARAM_DOUBLE, group )
327 {
328  m_Pt_param = ptparam;
329  m_Default = default_val;
330  m_Min = min;
331  m_Max = max;
332  m_Setup = Insetup;
333 }
334 
335 
336 void PARAM_CFG_DOUBLE::ReadParam( wxConfigBase* aConfig ) const
337 {
338  if( !m_Pt_param || !aConfig )
339  return;
340 
341  double dtmp = m_Default;
342  aConfig->Read( m_Ident, &dtmp );
343 
344  if( (dtmp < m_Min) || (dtmp > m_Max) )
345  dtmp = m_Default;
346 
347  *m_Pt_param = dtmp;
348 }
349 
350 
351 void PARAM_CFG_DOUBLE::SaveParam( wxConfigBase* aConfig ) const
352 {
353  if( !m_Pt_param || !aConfig )
354  return;
355 
356  // We cannot use aConfig->Write for a double, because
357  // this function uses a format with very few digits in mantissa,
358  // and truncature issues are frequent.
359  // We uses our function.
361 }
362 
363 
364 PARAM_CFG_BOOL::PARAM_CFG_BOOL( const wxString& ident, bool* ptparam,
365  int default_val, const wxChar* group ) :
366  PARAM_CFG_BASE( ident, PARAM_BOOL, group )
367 {
368  m_Pt_param = ptparam;
369  m_Default = default_val ? true : false;
370 }
371 
372 
374  const wxString& ident,
375  bool* ptparam,
376  int default_val,
377  const wxChar* group ) :
378  PARAM_CFG_BASE( ident, PARAM_BOOL, group )
379 {
380  m_Pt_param = ptparam;
381  m_Default = default_val ? true : false;
382  m_Setup = Insetup;
383 }
384 
385 
386 void PARAM_CFG_BOOL::ReadParam( wxConfigBase* aConfig ) const
387 {
388  if( !m_Pt_param || !aConfig )
389  return;
390 
391  int itmp = aConfig->Read( m_Ident, (int) m_Default );
392 
393  *m_Pt_param = itmp ? true : false;
394 }
395 
396 
397 void PARAM_CFG_BOOL::SaveParam( wxConfigBase* aConfig ) const
398 {
399  if( !m_Pt_param || !aConfig )
400  return;
401 
402  aConfig->Write( m_Ident, *m_Pt_param );
403 }
404 
405 
407  wxString* ptparam,
408  const wxChar* group ) :
409  PARAM_CFG_BASE( ident, PARAM_WXSTRING, group )
410 {
411  m_Pt_param = ptparam;
412 }
413 
414 
415 PARAM_CFG_WXSTRING::PARAM_CFG_WXSTRING( bool Insetup, const wxString& ident,
416  wxString* ptparam,
417  const wxString& default_val,
418  const wxChar* group ) :
419  PARAM_CFG_BASE( ident, PARAM_WXSTRING, group )
420 {
421  m_Pt_param = ptparam;
422  m_Setup = Insetup;
423  m_default = default_val;
424 }
425 
426 
427 void PARAM_CFG_WXSTRING::ReadParam( wxConfigBase* aConfig ) const
428 {
429  if( !m_Pt_param || !aConfig )
430  return;
431 
432  *m_Pt_param = aConfig->Read( m_Ident, m_default );
433 }
434 
435 
436 void PARAM_CFG_WXSTRING::SaveParam( wxConfigBase* aConfig ) const
437 {
438  if( !m_Pt_param || !aConfig )
439  return;
440 
441  aConfig->Write( m_Ident, *m_Pt_param );
442 }
443 
444 
446  wxString* ptparam,
447  const wxChar* group ) :
448  PARAM_CFG_BASE( ident, PARAM_FILENAME, group )
449 {
450  m_Pt_param = ptparam;
451 }
452 
453 
454 void PARAM_CFG_FILENAME::ReadParam( wxConfigBase* aConfig ) const
455 {
456  if( !m_Pt_param || !aConfig )
457  return;
458 
459  wxString prm = aConfig->Read( m_Ident );
460  // file names are stored using Unix notation
461  // under Window we must use \ instead of /
462  // mainly if there is a server name in path (something like \\server\kicad)
463 #ifdef __WINDOWS__
464  prm.Replace(wxT("/"), wxT("\\"));
465 #endif
466  *m_Pt_param = prm;
467 }
468 
469 
470 void PARAM_CFG_FILENAME::SaveParam( wxConfigBase* aConfig ) const
471 {
472  if( !m_Pt_param || !aConfig )
473  return;
474 
475  wxString prm = *m_Pt_param;
476  // filenames are stored using Unix notation
477  prm.Replace(wxT("\\"), wxT("/") );
478  aConfig->Write( m_Ident, prm );
479 }
480 
481 
483  wxArrayString* ptparam,
484  const wxChar* group ) :
485  PARAM_CFG_BASE( ident, PARAM_LIBNAME_LIST, group )
486 {
487  m_Pt_param = ptparam;
488 }
489 
490 
491 void PARAM_CFG_LIBNAME_LIST::ReadParam( wxConfigBase* aConfig ) const
492 {
493  if( !m_Pt_param || !aConfig )
494  return;
495 
496  int indexlib = 1; // We start indexlib to 1 because first
497  // lib name is LibName1
498  wxString libname, id_lib;
499  wxArrayString* libname_list = m_Pt_param;
500 
501  while( 1 )
502  {
503  id_lib = m_Ident;
504  id_lib << indexlib;
505  indexlib++;
506  libname = aConfig->Read( id_lib, wxT( "" ) );
507 
508  if( libname.IsEmpty() )
509  break;
510  // file names are stored using Unix notation
511  // under Window we must use \ instead of /
512  // mainly if there is a server name in path (something like \\server\kicad)
513 #ifdef __WINDOWS__
514  libname.Replace(wxT("/"), wxT("\\"));
515 #endif
516  libname_list->Add( libname );
517  }
518 }
519 
520 
521 void PARAM_CFG_LIBNAME_LIST::SaveParam( wxConfigBase* aConfig ) const
522 {
523  if( !m_Pt_param || !aConfig )
524  return;
525 
526  wxArrayString* libname_list = m_Pt_param;
527 
528  wxString configkey;
529  wxString libname;
530 
531  for( unsigned indexlib = 0; indexlib < libname_list->GetCount(); indexlib++ )
532  {
533  configkey = m_Ident;
534 
535  // We use indexlib+1 because first lib name is LibName1
536  configkey << (indexlib + 1);
537  libname = libname_list->Item( indexlib );
538 
539  // filenames are stored using Unix notation
540  libname.Replace(wxT("\\"), wxT("/") );
541  aConfig->Write( configkey, libname );
542  }
543 }
virtual void SaveParam(wxConfigBase *aConfig) const override
Function SaveParam saves the value of the parameter stored in aConfig.
wxString m_Ident_legacy
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)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:119
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown...
Definition: common.h:178
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_BASE(const wxString &ident, const paramcfg_id type, const wxChar *group=NULL, const wxString &legacy_ident=wxEmptyString)
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)
wxArrayString * m_Pt_param
Pointer to the parameter value.
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.
Configuration parameter - Integer Class.
The common library.
Base window classes and related definitions.
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:99
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_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=nullptr, const wxString &legacy_ident=wxEmptyString)
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)
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)
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.
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, const wxString &legacy_ident=wxEmptyString)
wxString m_Ident
Keyword in config data.
#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