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