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-2020 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 
27 #include <common.h> // for LOCALE_IO
28 #include <config_params.h> // for PARAM_CFG_INT_WITH_SCALE, PARAM_CFG_...
29 #include <gal/color4d.h> // for COLOR4D
30 #include <math/util.h> // for KiROUND
31 #include <wx/config.h> // for wxConfigBase
32 #include <wx/debug.h> // for wxASSERT
33 #include <wx/wx.h> // for wxString, operator!=, operator==
34 
35 void wxConfigLoadParams( wxConfigBase* aCfg, const std::vector<PARAM_CFG*>& aList,
36  const wxString& aGroup )
37 {
38  wxASSERT( aCfg );
39 
40  for( PARAM_CFG* param : aList )
41  {
42  if( !!param->m_Group )
43  aCfg->SetPath( param->m_Group );
44  else
45  aCfg->SetPath( aGroup );
46 
47  if( param->m_Setup )
48  continue;
49 
50  param->ReadParam( aCfg );
51  }
52 }
53 
54 
55 void wxConfigLoadSetups( wxConfigBase* aCfg, const std::vector<PARAM_CFG*>& aList )
56 {
57  wxASSERT( aCfg );
58 
59  for( PARAM_CFG* param : aList )
60  {
61  if( !param->m_Setup )
62  continue;
63 
64  param->ReadParam( aCfg );
65  }
66 }
67 
68 
69 void wxConfigSaveParams( wxConfigBase* aCfg, const std::vector<PARAM_CFG*>& aList,
70  const wxString& aGroup )
71 {
72  wxASSERT( aCfg );
73 
74  for( PARAM_CFG* param : aList )
75  {
76  if( !!param->m_Group )
77  aCfg->SetPath( param->m_Group );
78  else
79  aCfg->SetPath( aGroup );
80 
81  if( param->m_Setup )
82  continue;
83 
84  if( param->m_Type == PARAM_COMMAND_ERASE ) // Erase all data
85  {
86  if( !!param->m_Ident )
87  aCfg->DeleteGroup( param->m_Ident );
88  }
89  else
90  {
91  param->SaveParam( aCfg );
92  }
93  }
94 }
95 
96 
97 void wxConfigSaveSetups( wxConfigBase* aCfg, const std::vector<PARAM_CFG*>& aList )
98 {
99  wxASSERT( aCfg );
100 
101  for( PARAM_CFG* param : aList )
102  {
103  if( !param->m_Setup )
104  continue;
105 
106  if( param->m_Type == PARAM_COMMAND_ERASE ) // Erase all data
107  {
108  if( !!param->m_Ident )
109  aCfg->DeleteGroup( param->m_Ident );
110  }
111  else
112  {
113  param->SaveParam( aCfg );
114  }
115  }
116 }
117 
118 
119 void ConfigBaseWriteDouble( wxConfigBase* aConfig, const wxString& aKey, double aValue )
120 {
121  // Use a single strategy, regardless of wx version.
122  // Want C locale float string.
123 
124  LOCALE_IO toggle;
125  wxString tnumber = wxString::Format( wxT( "%.16g" ), aValue );
126 
127  aConfig->Write( aKey, tnumber );
128 }
129 
130 
131 PARAM_CFG::PARAM_CFG( const wxString& ident, const paramcfg_id type,
132  const wxChar* group, const wxString& legacy )
133 {
134  m_Ident = ident;
135  m_Type = type;
136  m_Group = group;
137  m_Setup = false;
138 
139  m_Ident_legacy = legacy;
140 }
141 
142 
143 PARAM_CFG_INT::PARAM_CFG_INT( const wxString& ident, int* ptparam, int default_val,
144  int min, int max, const wxChar* group, const wxString& legacy ) :
145  PARAM_CFG( ident, PARAM_INT, group, legacy )
146 {
147  m_Pt_param = ptparam;
148  m_Default = default_val;
149  m_Min = min;
150  m_Max = max;
151 }
152 
153 
154 PARAM_CFG_INT::PARAM_CFG_INT( bool setup, const wxString& ident, int* ptparam, int default_val,
155  int min, int max, const wxChar* group, const wxString& legacy ) :
156  PARAM_CFG( ident, PARAM_INT, group, legacy )
157 {
158  m_Pt_param = ptparam;
159  m_Default = default_val;
160  m_Min = min;
161  m_Max = max;
162  m_Setup = setup;
163 }
164 
165 
166 void PARAM_CFG_INT::ReadParam( wxConfigBase* aConfig ) const
167 {
168  if( !m_Pt_param || !aConfig )
169  return;
170 
171  int itmp = m_Default;
172 
173  if( !aConfig->Read( m_Ident, &itmp ) && m_Ident_legacy != wxEmptyString )
174  aConfig->Read( m_Ident_legacy, &itmp );
175 
176  if( (itmp < m_Min) || (itmp > m_Max) )
177  itmp = m_Default;
178 
179  *m_Pt_param = itmp;
180 }
181 
182 
183 void PARAM_CFG_INT::SaveParam( wxConfigBase* aConfig ) const
184 {
185  if( !m_Pt_param || !aConfig )
186  return;
187 
188  aConfig->Write( m_Ident, *m_Pt_param );
189 }
190 
191 
192 PARAM_CFG_INT_WITH_SCALE::PARAM_CFG_INT_WITH_SCALE( const wxString& ident, int* ptparam,
193  int default_val, int min, int max,
194  const wxChar* group, double aBiu2cfgunit,
195  const wxString& legacy_ident ) :
196  PARAM_CFG_INT( ident, ptparam, default_val, min, max, group, legacy_ident )
197 {
199  m_BIU_to_cfgunit = aBiu2cfgunit;
200 }
201 
202 
203 PARAM_CFG_INT_WITH_SCALE::PARAM_CFG_INT_WITH_SCALE( bool setup, const wxString& ident, int* ptparam,
204  int default_val, int min, int max,
205  const wxChar* group, double aBiu2cfgunit,
206  const wxString& legacy_ident ) :
207  PARAM_CFG_INT( setup, ident, ptparam, default_val, min, max, group, legacy_ident )
208 {
210  m_BIU_to_cfgunit = aBiu2cfgunit;
211 }
212 
213 
214 void PARAM_CFG_INT_WITH_SCALE::ReadParam( wxConfigBase* aConfig ) const
215 {
216  if( !m_Pt_param || !aConfig )
217  return;
218 
219  double dtmp = (double) m_Default * m_BIU_to_cfgunit;
220  if( !aConfig->Read( m_Ident, &dtmp ) && m_Ident_legacy != wxEmptyString )
221  aConfig->Read( m_Ident_legacy, &dtmp );
222 
223  int itmp = KiROUND( dtmp / m_BIU_to_cfgunit );
224 
225  if( (itmp < m_Min) || (itmp > m_Max) )
226  itmp = m_Default;
227 
228  *m_Pt_param = itmp;
229 }
230 
231 
232 void PARAM_CFG_INT_WITH_SCALE::SaveParam( wxConfigBase* aConfig ) const
233 {
234  if( !m_Pt_param || !aConfig )
235  return;
236 
237  // We cannot use aConfig->Write for a double, because
238  // this function uses a format with very few digits in mantissa,
239  // and truncature issues are frequent.
240  // We uses our function.
242 }
243 
244 
245 PARAM_CFG_DOUBLE::PARAM_CFG_DOUBLE( const wxString& ident, double* ptparam,
246  double default_val, double min, double max,
247  const wxChar* group ) :
248  PARAM_CFG( ident, PARAM_DOUBLE, group )
249 {
250  m_Pt_param = ptparam;
251  m_Default = default_val;
252  m_Min = min;
253  m_Max = max;
254 }
255 
256 
258  const wxString& ident,
259  double* ptparam,
260  double default_val,
261  double min,
262  double max,
263  const wxChar* group ) :
264  PARAM_CFG( ident, PARAM_DOUBLE, group )
265 {
266  m_Pt_param = ptparam;
267  m_Default = default_val;
268  m_Min = min;
269  m_Max = max;
270  m_Setup = Insetup;
271 }
272 
273 
274 void PARAM_CFG_DOUBLE::ReadParam( wxConfigBase* aConfig ) const
275 {
276  if( !m_Pt_param || !aConfig )
277  return;
278 
279  double dtmp = m_Default;
280  aConfig->Read( m_Ident, &dtmp );
281 
282  if( (dtmp < m_Min) || (dtmp > m_Max) )
283  dtmp = m_Default;
284 
285  *m_Pt_param = dtmp;
286 }
287 
288 
289 void PARAM_CFG_DOUBLE::SaveParam( wxConfigBase* aConfig ) const
290 {
291  if( !m_Pt_param || !aConfig )
292  return;
293 
294  // We cannot use aConfig->Write for a double, because
295  // this function uses a format with very few digits in mantissa,
296  // and truncature issues are frequent.
297  // We uses our function.
299 }
300 
301 
302 PARAM_CFG_BOOL::PARAM_CFG_BOOL( const wxString& ident, bool* ptparam, int default_val,
303  const wxChar* group, const wxString& legacy ) :
304  PARAM_CFG( ident, PARAM_BOOL, group, legacy )
305 {
306  m_Pt_param = ptparam;
307  m_Default = default_val ? true : false;
308 }
309 
310 
311 PARAM_CFG_BOOL::PARAM_CFG_BOOL( bool Insetup, const wxString& ident, bool* ptparam,
312  int default_val, const wxChar* group, const wxString& legacy ) :
313  PARAM_CFG( ident, PARAM_BOOL, group, legacy )
314 {
315  m_Pt_param = ptparam;
316  m_Default = default_val ? true : false;
317  m_Setup = Insetup;
318 }
319 
320 
321 void PARAM_CFG_BOOL::ReadParam( wxConfigBase* aConfig ) const
322 {
323  if( !m_Pt_param || !aConfig )
324  return;
325 
326  int itmp = (int) m_Default;
327 
328  if( !aConfig->Read( m_Ident, &itmp ) && m_Ident_legacy != wxEmptyString )
329  aConfig->Read( m_Ident_legacy, &itmp );
330 
331  *m_Pt_param = itmp ? true : false;
332 }
333 
334 
335 void PARAM_CFG_BOOL::SaveParam( wxConfigBase* aConfig ) const
336 {
337  if( !m_Pt_param || !aConfig )
338  return;
339 
340  aConfig->Write( m_Ident, *m_Pt_param );
341 }
342 
343 
344 PARAM_CFG_WXSTRING::PARAM_CFG_WXSTRING( const wxString& ident, wxString* ptparam,
345  const wxChar* group ) :
346  PARAM_CFG( ident, PARAM_WXSTRING, group )
347 {
348  m_Pt_param = ptparam;
349 }
350 
351 
352 PARAM_CFG_WXSTRING::PARAM_CFG_WXSTRING( bool Insetup, const wxString& ident, wxString* ptparam,
353  const wxString& default_val, const wxChar* group ) :
354  PARAM_CFG( ident, PARAM_WXSTRING, group )
355 {
356  m_Pt_param = ptparam;
357  m_Setup = Insetup;
358  m_default = default_val;
359 }
360 
361 
362 void PARAM_CFG_WXSTRING::ReadParam( wxConfigBase* aConfig ) const
363 {
364  if( !m_Pt_param || !aConfig )
365  return;
366 
367  *m_Pt_param = aConfig->Read( m_Ident, m_default );
368 }
369 
370 
371 void PARAM_CFG_WXSTRING::SaveParam( wxConfigBase* aConfig ) const
372 {
373  if( !m_Pt_param || !aConfig )
374  return;
375 
376  aConfig->Write( m_Ident, *m_Pt_param );
377 }
378 
379 
380 PARAM_CFG_WXSTRING_SET::PARAM_CFG_WXSTRING_SET( const wxString& ident, std::set<wxString>* ptparam,
381  const wxChar* group ) :
382  PARAM_CFG( ident, PARAM_WXSTRING_SET, group )
383 {
384  m_Pt_param = ptparam;
385 }
386 
387 
388 PARAM_CFG_WXSTRING_SET::PARAM_CFG_WXSTRING_SET( bool Insetup, const wxString& ident,
389  std::set<wxString>* ptparam, const wxChar* group ) :
390  PARAM_CFG( ident, PARAM_WXSTRING, group )
391 {
392  m_Pt_param = ptparam;
393  m_Setup = Insetup;
394 }
395 
396 
397 void PARAM_CFG_WXSTRING_SET::ReadParam( wxConfigBase* aConfig ) const
398 {
399  if( !m_Pt_param || !aConfig )
400  return;
401 
402  for( int i = 1; true; ++i )
403  {
404  wxString key, data;
405 
406  key = m_Ident;
407  key << i;
408  data = aConfig->Read( key, wxT( "" ) );
409 
410  if( data.IsEmpty() )
411  break;
412 
413  m_Pt_param->insert( data );
414  }
415 }
416 
417 
418 void PARAM_CFG_WXSTRING_SET::SaveParam( wxConfigBase* aConfig ) const
419 {
420  if( !m_Pt_param || !aConfig )
421  return;
422 
423  int i = 1;
424 
425  for( const wxString& str : *m_Pt_param )
426  {
427  wxString key, data;
428 
429  key = m_Ident;
430  key << i++;
431 
432  aConfig->Write( key, str );
433  }
434 }
435 
436 
438  wxString* ptparam,
439  const wxChar* group ) :
440  PARAM_CFG( ident, PARAM_FILENAME, group )
441 {
442  m_Pt_param = ptparam;
443 }
444 
445 
446 void PARAM_CFG_FILENAME::ReadParam( wxConfigBase* aConfig ) const
447 {
448  if( !m_Pt_param || !aConfig )
449  return;
450 
451  wxString prm = aConfig->Read( m_Ident );
452  // file names are stored using Unix notation
453  // under Window we must use \ instead of /
454  // mainly if there is a server name in path (something like \\server\kicad)
455 #ifdef __WINDOWS__
456  prm.Replace(wxT("/"), wxT("\\"));
457 #endif
458  *m_Pt_param = prm;
459 }
460 
461 
462 void PARAM_CFG_FILENAME::SaveParam( wxConfigBase* aConfig ) const
463 {
464  if( !m_Pt_param || !aConfig )
465  return;
466 
467  wxString prm = *m_Pt_param;
468  // filenames are stored using Unix notation
469  prm.Replace(wxT("\\"), wxT("/") );
470  aConfig->Write( m_Ident, prm );
471 }
472 
473 
475  wxArrayString* ptparam,
476  const wxChar* group ) :
477  PARAM_CFG( ident, PARAM_LIBNAME_LIST, group )
478 {
479  m_Pt_param = ptparam;
480 }
481 
482 
483 void PARAM_CFG_LIBNAME_LIST::ReadParam( wxConfigBase* aConfig ) const
484 {
485  if( !m_Pt_param || !aConfig )
486  return;
487 
488  int indexlib = 1; // We start indexlib to 1 because first
489  // lib name is LibName1
490  wxString libname, id_lib;
491  wxArrayString* libname_list = m_Pt_param;
492 
493  while( 1 )
494  {
495  id_lib = m_Ident;
496  id_lib << indexlib;
497  indexlib++;
498  libname = aConfig->Read( id_lib, wxT( "" ) );
499 
500  if( libname.IsEmpty() )
501  break;
502  // file names are stored using Unix notation
503  // under Window we must use \ instead of /
504  // mainly if there is a server name in path (something like \\server\kicad)
505 #ifdef __WINDOWS__
506  libname.Replace(wxT("/"), wxT("\\"));
507 #endif
508  libname_list->Add( libname );
509  }
510 }
511 
512 
513 void PARAM_CFG_LIBNAME_LIST::SaveParam( wxConfigBase* aConfig ) const
514 {
515  if( !m_Pt_param || !aConfig )
516  return;
517 
518  wxArrayString* libname_list = m_Pt_param;
519 
520  wxString configkey;
521  wxString libname;
522 
523  for( unsigned indexlib = 0; indexlib < libname_list->GetCount(); indexlib++ )
524  {
525  configkey = m_Ident;
526 
527  // We use indexlib+1 because first lib name is LibName1
528  configkey << (indexlib + 1);
529  libname = libname_list->Item( indexlib );
530 
531  // filenames are stored using Unix notation
532  libname.Replace(wxT("\\"), wxT("/") );
533  aConfig->Write( configkey, libname );
534  }
535 }
bool m_Setup
Install or Project based parameter, true == install.
Definition: config_params.h:87
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:86
int m_Default
The default value of the parameter.
wxString * 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.
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:216
wxString m_default
The default value of the parameter.
void wxConfigLoadParams(wxConfigBase *aCfg, const std::vector< PARAM_CFG * > &aList, const wxString &aGroup)
Function wxConfigLoadParams uses aList of PARAM_CFG to load configuration values from aCfg.
PARAM_CFG_WXSTRING_SET(const wxString &ident, std::set< wxString > *ptparam, const wxChar *group=NULL)
void wxConfigLoadSetups(wxConfigBase *aCfg, const std::vector< PARAM_CFG * > &aList)
Function wxConfigLoadSetups uses aList of PARAM_CFG to load configuration values from aCfg.
virtual void SaveParam(wxConfigBase *aConfig) const override
Function SaveParam saves the value of the parameter stored in aConfig.
wxString m_Ident
Keyword in config data.
Definition: config_params.h:84
int m_Default
The default value of the parameter.
virtual void SaveParam(wxConfigBase *aConfig) const override
Function SaveParam saves 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.
wxString m_Ident_legacy
Definition: config_params.h:91
double * m_Pt_param
Pointer to the parameter value.
int * m_Pt_param
Pointer to the parameter value.
void wxConfigSaveSetups(wxConfigBase *aCfg, const std::vector< PARAM_CFG * > &aList)
Function wxConfigSaveSetups writes aList of PARAM_CFG to save configuration values to aCfg.
PARAM_CFG_BOOL(const wxString &ident, bool *ptparam, int default_val=false, const wxChar *group=NULL, const wxString &legacy_ident=wxEmptyString)
double m_BIU_to_cfgunit
the factor to convert the saved value in internal value
std::set< wxString > * m_Pt_param
Pointer to the parameter 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.
PARAM_CFG(const wxString &ident, const paramcfg_id type, const wxChar *group=NULL, const wxString &legacy_ident=wxEmptyString)
virtual void SaveParam(wxConfigBase *aConfig) const override
Function SaveParam saves the value of the parameter stored in aConfig.
Configuration parameter - Integer Class.
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.
paramcfg_id
Type of parameter in the configuration file.
Definition: config_params.h:53
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.
virtual void ReadParam(wxConfigBase *aConfig) const override
Function ReadParam reads the value of the parameter stored in aConfig.
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
PARAM_CFG is a base class which establishes the interface functions ReadParam and SaveParam,...
Definition: config_params.h:81
paramcfg_id m_Type
Type of parameter.
Definition: config_params.h:85
void wxConfigSaveParams(wxConfigBase *aCfg, const std::vector< PARAM_CFG * > &aList, const wxString &aGroup)
Function wxConfigSaveParams writes aList of PARAM_CFG to save configuration values to aCfg.
wxString * m_Pt_param
Pointer to the parameter value.
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:68
The common library.
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.
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)