KiCad PCB EDA Suite
eeschema_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) 2014-2019 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 <class_library.h>
25 #include <confirm.h>
33 #include <eeschema_config.h>
34 #include <fctsys.h>
35 #include <kiway.h>
36 #include <lib_edit_frame.h>
38 #include <panel_hotkeys_editor.h>
39 #include <pgm_base.h>
40 #include <project/project_file.h>
41 #include <project/net_settings.h>
42 #include <sch_edit_frame.h>
43 #include <sch_painter.h>
44 #include <schematic.h>
45 #include <settings/app_settings.h>
47 #include <symbol_lib_table.h>
48 #include <widgets/paged_dialog.h>
50 #include <ws_data_model.h>
51 
52 
53 #define FieldNameTemplatesKey wxT( "FieldNameTemplates" )
54 
55 
56 PARAM_CFG_FIELDNAMES::PARAM_CFG_FIELDNAMES( TEMPLATES * ptparam, const wxChar* group ) :
57  PARAM_CFG( wxEmptyString, PARAM_SEVERITIES, group )
58 {
59  m_Pt_param = ptparam;
60 }
61 
62 void PARAM_CFG_FIELDNAMES::ReadParam( wxConfigBase* aConfig ) const
63 {
64  if( !m_Pt_param || !aConfig )
65  return;
66 
67  wxString templateFieldNames = aConfig->Read( FieldNameTemplatesKey, wxEmptyString );
68 
69  if( !templateFieldNames.IsEmpty() )
70  {
71  TEMPLATE_FIELDNAMES_LEXER lexer( TO_UTF8( templateFieldNames ) );
72 
73  try
74  {
75  m_Pt_param->Parse( &lexer, false );
76  }
77  catch( const IO_ERROR& )
78  {
79  // @todo show error msg
80  }
81  }
82 }
83 
84 void PARAM_CFG_FIELDNAMES::SaveParam( wxConfigBase* aConfig ) const
85 {
86  if( !m_Pt_param || !aConfig )
87  return;
88 
90  m_Pt_param->Format( &sf, 0, false );
91 
92  wxString record = FROM_UTF8( sf.GetString().c_str() );
93  record.Replace( wxT("\n"), wxT(""), true ); // strip all newlines
94  record.Replace( wxT(" "), wxT(" "), true ); // double space to single
95 
96  aConfig->Write( FieldNameTemplatesKey, record );
97 }
98 
99 
101 {
102 protected:
104 
105 public:
106  PARAM_CFG_SEVERITIES( ERC_SETTINGS* ptparam, const wxChar* group = nullptr ) :
107  PARAM_CFG( wxEmptyString, PARAM_SEVERITIES, group )
108  {
109  m_Pt_param = ptparam;
110  }
111 
112  void ReadParam( wxConfigBase* aConfig ) const override
113  {
114  if( !m_Pt_param || !aConfig )
115  return;
116 
117  wxString oldPath = aConfig->GetPath();
118 
119  // Read legacy settings first so that modern settings will overwrite them
120  bool flag;
121 
122  if( aConfig->Read( wxT( "ERC_TestSimilarLabels" ), &flag, true ) )
123  {
124  if( flag )
126  else
128  }
129 
130  if( aConfig->Read( wxT( "ERC_CheckUniqueGlobalLabels" ), &flag, true ) )
131  {
132  if( flag )
134  else
136  }
137 
138  if( aConfig->Read( wxT( "ERC_CheckBusDriverConflicts" ), &flag, true ) )
139  {
140  if( flag )
142  else
144  }
145 
146  if( aConfig->Read( wxT( "ERC_CheckBusEntryConflicts" ), &flag, true ) )
147  {
148  if( flag )
150  else
152  }
153 
154  if( aConfig->Read( wxT( "ERC_CheckBusToBusConflicts" ), &flag, true ) )
155  {
156  if( flag )
158  else
160  }
161 
162  if( aConfig->Read( wxT( "ERC_CheckBusToNetConflicts" ), &flag, true ) )
163  {
164  if( flag )
166  else
168  }
169 
170  // TO DO: figure out what we're going to use as keys here so we can read/write these....
171 
172  aConfig->SetPath( oldPath );
173  }
174 
175  void SaveParam( wxConfigBase* aConfig ) const override
176  {
177  if( !m_Pt_param || !aConfig )
178  return;
179 
180  wxString oldPath = aConfig->GetPath();
181 
182  // TO DO: figure out what we're going to use as keys here so we can read/write these....
183 
184  // TO DO: for now just write out the legacy ones so we don't lose them
185  // TO DO: remove this once the new scheme is in place
186  aConfig->Write( wxT( "ERC_TestSimilarLabels" ),
188  aConfig->Write( wxT( "ERC_CheckUniqueGlobalLabels" ),
190  aConfig->Write( wxT( "ERC_CheckBusDriverConflicts" ),
192  aConfig->Write( wxT( "ERC_CheckBusEntryConflicts" ),
194  aConfig->Write( wxT( "ERC_CheckBusToBusConflicts" ),
196  aConfig->Write( wxT( "ERC_CheckBusToNetConflicts" ),
198 
199  aConfig->SetPath( oldPath );
200  }
201 };
202 
203 
206 {
207  return Pgm().GetSettingsManager().GetColorSettings()->GetColor( aLayer );
208 }
209 
210 
211 // Color to draw items flagged invisible, in libedit (they are invisible in Eeschema)
213 {
214  return COLOR4D( DARKGRAY );
215 }
216 
217 
219  PANEL_HOTKEYS_EDITOR* aHotkeysPanel )
220 {
221  wxTreebook* book = aParent->GetTreebook();
222 
223  book->AddPage( new wxPanel( book ), _( "Eeschema" ) );
224  book->AddSubPage( new PANEL_EESCHEMA_DISPLAY_OPTIONS( this, book ), _( "Display Options" ) );
225  book->AddSubPage( new PANEL_EESCHEMA_SETTINGS( this, book ), _( "Editing Options" ) );
226  book->AddSubPage( new PANEL_EESCHEMA_COLOR_SETTINGS( this, book ), _( "Colors" ) );
227  book->AddSubPage( new PANEL_EESCHEMA_TEMPLATE_FIELDNAMES( this, book, true ),
228  _( "Field Name Templates" ) );
229 
230  aHotkeysPanel->AddHotKeys( GetToolManager() );
231 }
232 
233 
235 {
242 
243  // Verify some values, because the config file can be edited by hand,
244  // and have bad values:
247 
248  // Load the page layout decr file, from the filename stored in
249  // BASE_SCREEN::m_PageLayoutDescrFileName, read in config project file
250  // If empty, or not existing, the default descr is loaded
253  Prj().GetProjectPath() );
254 
255  pglayout.SetPageLayout( filename );
256 
257  return true;
258 }
259 
260 
261 void SCH_EDIT_FRAME::ShowSchematicSetupDialog( const wxString& aInitialPage )
262 {
263  DIALOG_SCHEMATIC_SETUP dlg( this );
264 
265  if( !aInitialPage.IsEmpty() )
266  dlg.SetInitialPage( aInitialPage, wxEmptyString );
267 
268  if( dlg.ShowQuasiModal() == wxID_OK )
269  {
272 
273  Kiway().CommonSettingsChanged( false, true );
274  GetCanvas()->Refresh();
275  }
276 }
277 
278 
280 {
281  wxFileName fn = Schematic().RootScreen()->GetFileName(); //ConfigFileName
282 
283  fn.SetExt( ProjectFileExtension );
284 
285  if( !fn.HasName() || !IsWritable( fn ) )
286  return;
287 
288  GetSettingsManager()->SaveProject( fn.GetFullPath() );
289 }
290 
291 
293 {
294  // For now, axes are forced off in eeschema even if turned on in config
295  eeconfig()->m_Window.grid.axes_enabled = false;
296 
298 
305 
306  if( eeconfig() )
307  {
310  GetRenderSettings()->SetShowPageLimits( eeconfig()->m_Appearance.show_page_limits );
311  }
312 }
313 
314 
316 {
318 
319  // TODO(JE) do we need to keep m_userUnits around?
320  if( eeconfig() )
321  eeconfig()->m_System.units = static_cast<int>( m_userUnits );
322 }
323 
324 
326 {
327  wxCHECK_RET( aCfg, "Call to SCH_BASE_FRAME::SaveSettings with null settings" );
328 
330 
331  if( aCfg->m_Window.grid.sizes.empty() )
332  {
333  /*
334  * Do NOT add others values (mainly grid values in mm), because they can break the
335  * schematic: Because wires and pins are considered as connected when the are to the
336  * same coordinate we cannot mix coordinates in mils (internal units) and mm (that
337  * cannot exactly converted in mils in many cases). In fact schematic must only use
338  * 50 and 25 mils to place labels, wires and components others values are useful only
339  * for graphic items (mainly in library editor) so use integer values in mils only.
340  * The 100 mil grid is added to help conform to the KiCad Library Convention which
341  * states: "Using a 100mil grid, pin ends and origin must lie on grid nodes IEC-60617"
342  */
343  aCfg->m_Window.grid.sizes = { "100 mil",
344  "50 mil",
345  "25 mil",
346  "10 mil",
347  "5 mil",
348  "2 mil",
349  "1 mil" };
350  }
351 
352  if( aCfg->m_Window.zoom_factors.empty() )
353  {
354  aCfg->m_Window.zoom_factors = { 0.1,
355  0.15,
356  0.2,
357  0.3,
358  0.5,
359  0.7,
360  1.0,
361  1.5,
362  2.0,
363  3.0,
364  4.5,
365  6.5,
366  10.0,
367  15.0,
368  20.0,
369  30.0,
370  45.0,
371  65.0,
372  100.0,
373  150.0 };
374  }
375 
376  for( double& factor : aCfg->m_Window.zoom_factors )
377  factor = std::min( factor, MAX_ZOOM_FACTOR );
378 }
379 
380 
382 {
383  wxCHECK_RET( aCfg, "Call to SCH_BASE_FRAME::SaveSettings with null settings" );
384 
386 }
387 
388 
390  PANEL_HOTKEYS_EDITOR* aHotkeysPanel )
391 {
392  wxTreebook* book = aParent->GetTreebook();
393 
394  book->AddPage( new wxPanel( book ), _( "Symbol Editor" ) );
395  book->AddSubPage( new PANEL_GAL_DISPLAY_OPTIONS( this, aParent ), _( "Display Options" ) );
396  book->AddSubPage( new PANEL_LIBEDIT_SETTINGS( this, book ), _( "Editing Options" ) );
397  book->AddSubPage( new PANEL_LIBEDIT_COLOR_SETTINGS( this, book ), _( "Colors" ) );
398 
399  aHotkeysPanel->AddHotKeys( GetToolManager() );
400 }
401 
402 
403 SYMBOL_LIB_TABLE* PROJECT::SchSymbolLibTable()
404 {
405  // This is a lazy loading function, it loads the project specific table when
406  // that table is asked for, not before.
408 
409  // its gotta be NULL or a SYMBOL_LIB_TABLE, or a bug.
410  wxASSERT( !tbl || tbl->Type() == SYMBOL_LIB_TABLE_T );
411 
412  if( !tbl )
413  {
414  // Stack the project specific SYMBOL_LIB_TABLE overlay on top of the global table.
415  // ~SYMBOL_LIB_TABLE() will not touch the fallback table, so multiple projects may
416  // stack this way, all using the same global fallback table.
418 
420 
421  wxString prjPath;
422 
423  wxGetEnv( PROJECT_VAR_NAME, &prjPath );
424 
425  if( !prjPath.IsEmpty() )
426  {
427  wxFileName fn( prjPath, SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
428 
429  try
430  {
431  tbl->Load( fn.GetFullPath() );
432  }
433  catch( const IO_ERROR& ioe )
434  {
435  wxString msg;
436  msg.Printf( _( "An error occurred loading the symbol library table \"%s\"." ),
437  fn.GetFullPath() );
438  DisplayErrorMessage( NULL, msg, ioe.What() );
439  }
440  }
441  }
442 
443  return tbl;
444 }
void Format(OUTPUTFORMATTER *out, int nestLevel, bool aGlobal) const
Function Format serializes this object out as text into the given OUTPUTFORMATTER.
WS_DATA_MODEL handles the graphic items list to draw/plot the frame and title block.
Definition: ws_data_model.h:39
static const wxString & GetSymbolLibTableFileName()
static SYMBOL_LIB_TABLE & GetGlobalLibTable()
const wxString & GetFileName() const
Definition: sch_screen.h:185
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_holder.h:56
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:114
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:252
SETTINGS_MANAGER * GetSettingsManager() const
void SaveProjectSettings() override
Save changes to the project settings to the project (.pro) file.
void InstallPreferences(PAGED_DIALOG *aParent, PANEL_HOTKEYS_EDITOR *aHotkeysPanel) override
Allows Libedit to install its preferences panel into the preferences dialog.
This file is part of the common library.
const std::string ProjectFileExtension
void SetInitialPage(const wxString &aPage, const wxString &aParentPage=wxEmptyString)
bool IsTestEnabled(int aErrorCode) const
Definition: erc_settings.h:115
void Parse(TEMPLATE_FIELDNAMES_LEXER *in, bool aGlobal)
Function Parse fills this object from information in the input stream handled by TEMPLATE_FIELDNAMES_...
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
Definition: project.h:38
#define MAX_ZOOM_FACTOR
NET_SETTINGS & NetSettings()
Definition: project_file.h:92
KIGFX::SCH_RENDER_SETTINGS * GetRenderSettings()
void AddHotKeys(TOOL_MANAGER *aToolMgr)
void SetPageLayout(const wxString &aFullFileName=wxEmptyString, bool Append=false)
Populates the list with a custom layout, or the default layout, if no custom layout available.
VTBL_ENTRY _ELEM * GetElem(ELEM_T aIndex)
Typically wrapped somewhere else in a more meaningful function wrapper.
Definition: project.cpp:248
bool IsWritable(const wxFileName &aFileName)
Checks if aFileName can be written.
ERC_SETTINGS * m_Pt_param
Pointer to the parameter value.
COLOR4D GetInvisibleItemColor()
std::vector< double > zoom_factors
Definition: app_settings.h:86
static WS_DATA_MODEL & GetTheInstance()
static function: returns the instance of WS_DATA_MODEL used in the application
VTBL_ENTRY void SetElem(ELEM_T aIndex, _ELEM *aElem)
Definition: project.cpp:260
void LoadSettings(APP_SETTINGS_BASE *aCfg) override
Load common frame parameters from a configuration file.
static wxString m_PageLayoutDescrFileName
the name of the page layout descr file, or emty to used the default pagelayout
Definition: base_screen.h:57
EESCHEMA_SETTINGS * eeconfig() const
wxTreebook * GetTreebook()
Definition: paged_dialog.h:50
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:100
#define FieldNameTemplatesKey
PARAM_CFG_FIELDNAMES(TEMPLATES *ptparam, const wxChar *group=nullptr)
static int GetSubpartIdSeparator()
void SaveSettings(APP_SETTINGS_BASE *aCfg) override
Saves common frame parameters to a configuration data file.
std::vector< wxString > sizes
Definition: app_settings.h:52
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:99
#define NULL
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
KICAD_T Type() override
static int GetSubpartFirstId()
GRID_SETTINGS grid
Definition: app_settings.h:89
int ShowQuasiModal()
VTBL_ENTRY PROJECT_FILE & GetProjectFile() const
Definition: project.h:141
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
void ResolveNetClassAssignments(bool aRebuildFromScratch=false)
Explodes the list of netclass assignments to include atomic members of composite labels (buses).
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
PARAM_CFG_SEVERITIES(ERC_SETTINGS *ptparam, const wxChar *group=nullptr)
SCHEMATIC & Schematic() const
Definition of file extensions used in Kicad.
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Helper for all the old plotting/printing code while it still exists.
SCH_LAYER_ID
Eeschema drawing layers.
const std::string & GetString()
Definition: richio.h:475
std::map< int, int > m_Severities
Definition: erc_settings.h:158
Container for ERC settings.
Definition: erc_settings.h:88
SCHEMATIC_SETTINGS * m_defaults
void Load(const wxString &aFileName)
Load the library table using the path defined by aFileName aFallBackTable.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
bool LoadProjectSettings()
Loads the KiCad project file (*.pro) settings specific to Eeschema.
void SaveParam(wxConfigBase *aConfig) const override
Function SaveParam saves the value of the parameter stored in aConfig.
TEMPLATES * m_Pt_param
Pointer to the parameter value.
WINDOW_SETTINGS m_Window
Definition: app_settings.h:171
void ReadParam(wxConfigBase *aConfig) const override
Function ReadParam reads the value of the parameter stored in aConfig.
see class PGM_BASE
void InstallPreferences(PAGED_DIALOG *aParent, PANEL_HOTKEYS_EDITOR *aHotkeysPanel) override
Allows Eeschema to install its preferences panels into the preferences dialog.
static const wxString MakeFullFileName(const wxString &aShortFileName, const wxString &aProjectPath)
Static function.
PARAM_CFG is a base class which establishes the interface functions ReadParam and SaveParam,...
Definition: config_params.h:81
#define _(s)
Definition: 3d_actions.cpp:33
void LoadSettings(APP_SETTINGS_BASE *aCfg) override
Load common frame parameters from a configuration file.
static void SetSubpartIdNotation(int aSep, int aFirstId)
Set the separator char between the subpart id and the reference 0 (no separator) or '.
void SaveSettings(APP_SETTINGS_BASE *aCfg) override
Saves common frame parameters to a configuration data file.
bool SaveProject(const wxString &aFullPath=wxEmptyString)
Saves a loaded project.
void ReadParam(wxConfigBase *aConfig) const override
Function ReadParam reads the value of the parameter stored in aConfig.
EDA_UNITS m_userUnits
VTBL_ENTRY void CommonSettingsChanged(bool aEnvVarsChanged, bool aTextVarsChanged)
Function CommonSettingsChanged Calls CommonSettingsChanged() on all KIWAY_PLAYERs.
Definition: kiway.cpp:513
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:78
void SaveSettings(APP_SETTINGS_BASE *aCfg) override
Saves common frame parameters to a configuration data file.
Definition for part library class.
SCH_SCREEN * RootScreen() const
Helper to retreive the screen of the root sheet.
Definition: schematic.cpp:112
void LoadSettings(APP_SETTINGS_BASE *aCfg) override
Load common frame parameters from a configuration file.
void SetDefaultPenWidth(int aWidth)
void ShowSchematicSetupDialog(const wxString &aInitialPage=wxEmptyString)
STRING_FORMATTER implements OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:445
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
void SaveParam(wxConfigBase *aConfig) const override
Function SaveParam saves the value of the parameter stored in aConfig.
void SetShowPageLimits(bool aDraw)
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99