KiCad PCB EDA Suite
advanced_config.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) 2018 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <advanced_config.h>
25 
26 #include <common.h>
27 #include <config_params.h>
28 
29 #include <wx/config.h>
30 #include <wx/filename.h>
31 #include <wx/log.h>
32 
33 /*
34  * Flag to enable advanced config debugging
35  *
36  * Use "KICAD_ADVANCED_CONFIG" to enable.
37  *
38  * @ingroup trace_env_vars
39  */
40 static const wxChar AdvancedConfigMask[] = wxT( "KICAD_ADVANCED_CONFIG" );
41 
48 namespace AC_KEYS
49 {
50 
62 static const wxChar EnableSvgImport[] = wxT( "EnableSvgImport" );
63 
69 static const wxChar AllowLegacyCanvasInGtk3[] = wxT( "AllowLegacyCanvasInGtk3" );
70 
71 } // namespace KEYS
72 
73 
74 /*
75  * Get a simple string for common parameters.
76  *
77  * This isn't exhaustive, but it covers most common types that might be
78  * used in the advance config
79  */
80 wxString dumpParamCfg( const PARAM_CFG_BASE& aParam )
81 {
82  wxString s = aParam.m_Ident + ": ";
83 
84  /*
85  * This implementation is rather simplistic, but it is
86  * effective enough for simple uses. A better implementation would be
87  * some kind of visitor, but that's somewhat more work.
88  */
89  switch( aParam.m_Type )
90  {
93  s << *static_cast<const PARAM_CFG_INT&>( aParam ).m_Pt_param;
94  break;
96  s << *static_cast<const PARAM_CFG_DOUBLE&>( aParam ).m_Pt_param;
97  break;
99  s << *static_cast<const PARAM_CFG_WXSTRING&>( aParam ).m_Pt_param;
100  break;
102  s << *static_cast<const PARAM_CFG_FILENAME&>( aParam ).m_Pt_param;
103  break;
105  s << ( *static_cast<const PARAM_CFG_BOOL&>( aParam ).m_Pt_param ? "true" : "false" );
106  break;
107  default: s << "Unsupported PARAM_CFG variant: " << aParam.m_Type;
108  }
109 
110  return s;
111 }
112 
113 
117 static void dumpCfg( const PARAM_CFG_ARRAY& aArray )
118 {
119  // only dump if we need to
120  if( !wxLog::IsAllowedTraceMask( AdvancedConfigMask ) )
121  return;
122 
123  for( const auto& param : aArray )
124  {
125  wxLogTrace( AdvancedConfigMask, dumpParamCfg( param ) );
126  }
127 }
128 
129 
135 static wxFileName getAdvancedCfgFilename()
136 {
137  const static wxString cfg_filename{ "kicad_advanced" };
138  return wxFileName( GetKicadConfigPath(), cfg_filename );
139 }
140 
141 
143 {
144  wxLogTrace( AdvancedConfigMask, "Init advanced config" );
145 
146  // Init defaults - this is done in case the config doesn't exist,
147  // then the values will remain as set here.
148  m_enableSvgImport = false;
149  m_allowLegacyCanvasInGtk3 = false;
150 
151  loadFromConfigFile();
152 }
153 
154 
156 {
157  static ADVANCED_CFG instance;
158  return instance;
159 }
160 
161 
163 {
164  const auto k_advanced = getAdvancedCfgFilename();
165 
166  if( !k_advanced.FileExists() )
167  {
168  wxLogTrace( AdvancedConfigMask, "File does not exist %s", k_advanced.GetFullPath() );
169  return;
170  }
171 
172  wxLogTrace( AdvancedConfigMask, "Loading advanced config from: %s", k_advanced.GetFullPath() );
173 
174  wxFileConfig file_cfg( "", "", k_advanced.GetFullPath() );
175  loadSettings( file_cfg );
176 }
177 
178 
179 void ADVANCED_CFG::loadSettings( wxConfigBase& aCfg )
180 {
181  PARAM_CFG_ARRAY configParams;
182 
183  configParams.push_back(
184  new PARAM_CFG_BOOL( true, AC_KEYS::EnableSvgImport, &m_enableSvgImport, false ) );
185 
186  configParams.push_back( new PARAM_CFG_BOOL(
187  true, AC_KEYS::AllowLegacyCanvasInGtk3, &m_allowLegacyCanvasInGtk3, false ) );
188 
189  wxConfigLoadSetups( &aCfg, configParams );
190 
191  dumpCfg( configParams );
192 }
193 
194 
196 {
197  // default is to allow
198  bool allow = true;
199 
200  // on GTK3, check the config
201 #ifdef __WXGTK3__
202  allow = m_allowLegacyCanvasInGtk3;
203 #endif
204 
205  return allow;
206 }
A list of parameters type.
List of known keys for advanced configuration options.
The common library.
static const wxChar AllowLegacyCanvasInGtk3[]
Allow legacy canvas to be shown in GTK3.
static const wxChar EnableSvgImport[]
Currently (Version 5.1) SVG import is disabled by default, to avoid issues SVG needs some enhancement...
void loadSettings(wxConfigBase &aCfg)
void loadFromConfigFile()
Load the config from the normal config file.
wxString dumpParamCfg(const PARAM_CFG_BASE &aParam)
Configuration parameter - Boolean Class.
Class PARAM_CFG_BASE is a base class which establishes the interface functions ReadParam and SavePara...
Definition: config_params.h:99
wxString GetKicadConfigPath()
Return the user configuration path used to store KiCad&#39;s configuration files.
Definition: common.cpp:262
bool AllowLegacyCanvas() const
Helper to determine if legacy canvas is allowed (according to platform and config) ...
static wxFileName getAdvancedCfgFilename()
Get the filename for the advanced config file.
Class containing "advanced" configuration options.
static void dumpCfg(const PARAM_CFG_ARRAY &aArray)
Dump the configs in the given array to trace.
The common library.
static const ADVANCED_CFG & GetCfg()
Get the singleton instance&#39;s config, which is shared by all consumers of advanced config...
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.
wxString m_Ident
Keyword in config data.
static const wxChar AdvancedConfigMask[]