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