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>
32 #include <eeschema_config.h>
33 #include <fctsys.h>
34 #include <lib_edit_frame.h>
36 #include <panel_hotkeys_editor.h>
37 #include <pgm_base.h>
38 #include <project/project_file.h>
39 #include <project/net_settings.h>
40 #include <sch_edit_frame.h>
41 #include <sch_junction.h>
42 #include <sch_painter.h>
43 #include <schematic.h>
44 #include <settings/app_settings.h>
46 #include <symbol_lib_table.h>
47 #include <widgets/paged_dialog.h>
49 #include <ws_data_model.h>
51 #include "erc.h"
52 
53 
54 #define FieldNameTemplatesKey wxT( "FieldNameTemplates" )
55 
56 
57 PARAM_CFG_FIELDNAMES::PARAM_CFG_FIELDNAMES( TEMPLATES * ptparam, const wxChar* group ) :
58  PARAM_CFG( wxEmptyString, PARAM_SEVERITIES, group )
59 {
60  m_Pt_param = ptparam;
61 }
62 
63 void PARAM_CFG_FIELDNAMES::ReadParam( wxConfigBase* aConfig ) const
64 {
65  if( !m_Pt_param || !aConfig )
66  return;
67 
68  wxString templateFieldNames = aConfig->Read( FieldNameTemplatesKey, wxEmptyString );
69 
70  if( !templateFieldNames.IsEmpty() )
71  {
72  TEMPLATE_FIELDNAMES_LEXER lexer( TO_UTF8( templateFieldNames ) );
73 
74  try
75  {
76  m_Pt_param->Parse( &lexer, false );
77  }
78  catch( const IO_ERROR& DBG( e ) )
79  {
80  // @todo show error msg
81  DBG( printf( "templatefieldnames parsing error: '%s'\n", TO_UTF8( e.What() ) ); )
82  }
83  }
84 }
85 
86 void PARAM_CFG_FIELDNAMES::SaveParam( wxConfigBase* aConfig ) const
87 {
88  if( !m_Pt_param || !aConfig )
89  return;
90 
92  m_Pt_param->Format( &sf, 0, false );
93 
94  wxString record = FROM_UTF8( sf.GetString().c_str() );
95  record.Replace( wxT("\n"), wxT(""), true ); // strip all newlines
96  record.Replace( wxT(" "), wxT(" "), true ); // double space to single
97 
98  aConfig->Write( FieldNameTemplatesKey, record );
99 }
100 
101 
103 {
104 protected:
106 
107 public:
108  PARAM_CFG_SEVERITIES( ERC_SETTINGS* ptparam, const wxChar* group = nullptr ) :
109  PARAM_CFG( wxEmptyString, PARAM_SEVERITIES, group )
110  {
111  m_Pt_param = ptparam;
112  }
113 
114  void ReadParam( wxConfigBase* aConfig ) const override
115  {
116  if( !m_Pt_param || !aConfig )
117  return;
118 
119  wxString oldPath = aConfig->GetPath();
120 
121  // Read legacy settings first so that modern settings will overwrite them
122  bool flag;
123 
124  if( aConfig->Read( wxT( "ERC_TestSimilarLabels" ), &flag, true ) )
125  {
126  if( flag )
128  else
130  }
131 
132  if( aConfig->Read( wxT( "ERC_CheckUniqueGlobalLabels" ), &flag, true ) )
133  {
134  if( flag )
136  else
138  }
139 
140  if( aConfig->Read( wxT( "ERC_CheckBusDriverConflicts" ), &flag, true ) )
141  {
142  if( flag )
144  else
146  }
147 
148  if( aConfig->Read( wxT( "ERC_CheckBusEntryConflicts" ), &flag, true ) )
149  {
150  if( flag )
152  else
154  }
155 
156  if( aConfig->Read( wxT( "ERC_CheckBusToBusConflicts" ), &flag, true ) )
157  {
158  if( flag )
160  else
162  }
163 
164  if( aConfig->Read( wxT( "ERC_CheckBusToNetConflicts" ), &flag, true ) )
165  {
166  if( flag )
168  else
170  }
171 
172  // TO DO: figure out what we're going to use as keys here so we can read/write these....
173 
174  aConfig->SetPath( oldPath );
175  }
176 
177  void SaveParam( wxConfigBase* aConfig ) const override
178  {
179  if( !m_Pt_param || !aConfig )
180  return;
181 
182  wxString oldPath = aConfig->GetPath();
183 
184  // TO DO: figure out what we're going to use as keys here so we can read/write these....
185 
186  // TO DO: for now just write out the legacy ones so we don't lose them
187  // TO DO: remove this once the new scheme is in place
188  aConfig->Write( wxT( "ERC_TestSimilarLabels" ),
190  aConfig->Write( wxT( "ERC_CheckUniqueGlobalLabels" ),
192  aConfig->Write( wxT( "ERC_CheckBusDriverConflicts" ),
194  aConfig->Write( wxT( "ERC_CheckBusEntryConflicts" ),
196  aConfig->Write( wxT( "ERC_CheckBusToBusConflicts" ),
198  aConfig->Write( wxT( "ERC_CheckBusToNetConflicts" ),
200 
201  aConfig->SetPath( oldPath );
202  }
203 };
204 
205 
208 {
209  return Pgm().GetSettingsManager().GetColorSettings()->GetColor( aLayer );
210 }
211 
212 
213 // Color to draw items flagged invisible, in libedit (they are invisible in Eeschema)
215 {
216  return COLOR4D( DARKGRAY );
217 }
218 
219 
221  PANEL_HOTKEYS_EDITOR* aHotkeysPanel )
222 {
223  wxTreebook* book = aParent->GetTreebook();
224 
225  book->AddPage( new wxPanel( book ), _( "Eeschema" ) );
226  book->AddSubPage( new PANEL_EESCHEMA_DISPLAY_OPTIONS( this, book ), _( "Display Options" ) );
227  book->AddSubPage( new PANEL_EESCHEMA_SETTINGS( this, book ), _( "Editing Options" ) );
228  book->AddSubPage( new PANEL_EESCHEMA_COLOR_SETTINGS( this, book ), _( "Colors" ) );
229  book->AddSubPage( new PANEL_EESCHEMA_TEMPLATE_FIELDNAMES( this, book, true ),
230  _( "Field Name Templates" ) );
231 
232  aHotkeysPanel->AddHotKeys( GetToolManager() );
233 }
234 
235 
237 {
244 
245  // Verify some values, because the config file can be edited by hand,
246  // and have bad values:
249 
250  // Load the page layout decr file, from the filename stored in
251  // BASE_SCREEN::m_PageLayoutDescrFileName, read in config project file
252  // If empty, or not existing, the default descr is loaded
255  Prj().GetProjectPath() );
256 
257  pglayout.SetPageLayout( filename );
258 
259  return true;
260 }
261 
262 
263 void SCH_EDIT_FRAME::ShowSchematicSetupDialog( const wxString& aInitialPage )
264 {
265  DIALOG_SCHEMATIC_SETUP dlg( this );
266 
267  if( !aInitialPage.IsEmpty() )
268  dlg.SetInitialPage( aInitialPage, wxEmptyString );
269 
270  if( dlg.ShowQuasiModal() == wxID_OK )
271  {
274 
275  GetCanvas()->Refresh();
276  }
277 }
278 
279 
281 {
282  wxFileName fn = Schematic().RootScreen()->GetFileName(); //ConfigFileName
283 
284  fn.SetExt( ProjectFileExtension );
285 
286  if( !fn.HasName() || !IsWritable( fn ) )
287  return;
288 
289  GetSettingsManager()->SaveProject( fn.GetFullPath() );
290 }
291 
292 
294 {
295  // For now, axes are forced off in eeschema even if turned on in config
296  eeconfig()->m_Window.grid.axes_enabled = false;
297 
299 
306 
307  if( eeconfig() )
308  {
311  GetRenderSettings()->SetShowPageLimits( eeconfig()->m_Appearance.show_page_limits );
312  }
313 }
314 
315 
317 {
319 
320  // TODO(JE) do we need to keep m_userUnits around?
321  if( eeconfig() )
322  eeconfig()->m_System.units = static_cast<int>( m_userUnits );
323 }
324 
325 
327 {
328  wxCHECK_RET( aCfg, "Call to SCH_BASE_FRAME::SaveSettings with null settings" );
329 
331 
332  if( aCfg->m_Window.grid.sizes.empty() )
333  {
334  /*
335  * Do NOT add others values (mainly grid values in mm), because they can break the
336  * schematic: Because wires and pins are considered as connected when the are to the
337  * same coordinate we cannot mix coordinates in mils (internal units) and mm (that
338  * cannot exactly converted in mils in many cases). In fact schematic must only use
339  * 50 and 25 mils to place labels, wires and components others values are useful only
340  * for graphic items (mainly in library editor) so use integer values in mils only.
341  * The 100 mil grid is added to help conform to the KiCad Library Convention which
342  * states: "Using a 100mil grid, pin ends and origin must lie on grid nodes IEC-60617"
343  */
344  aCfg->m_Window.grid.sizes = { "100 mil",
345  "50 mil",
346  "25 mil",
347  "10 mil",
348  "5 mil",
349  "2 mil",
350  "1 mil" };
351  }
352 
353  if( aCfg->m_Window.zoom_factors.empty() )
354  {
355  aCfg->m_Window.zoom_factors = { 0.1,
356  0.15,
357  0.2,
358  0.3,
359  0.5,
360  0.7,
361  1.0,
362  1.5,
363  2.0,
364  3.0,
365  4.5,
366  6.5,
367  10.0,
368  15.0,
369  20.0,
370  30.0,
371  45.0,
372  65.0,
373  100.0,
374  150.0 };
375  }
376 
377  for( double& factor : aCfg->m_Window.zoom_factors )
378  factor = std::min( factor, MAX_ZOOM_FACTOR );
379 
380  EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( aCfg );
381 
382  if( cfg )
383  {
384  wxString templateFieldNames = cfg->m_Drawing.field_names;
385 
386  if( !templateFieldNames.IsEmpty() )
387  {
388  TEMPLATE_FIELDNAMES_LEXER lexer( TO_UTF8( templateFieldNames ) );
389 
390  try
391  {
392  m_templateFieldNames.Parse( &lexer, true );
393  }
394  catch( const IO_ERROR& DBG( e ) )
395  {
396  // @todo show error msg
397  DBG( printf( "templatefieldnames parsing error: '%s'\n", TO_UTF8( e.What() ) ); )
398  }
399  }
400  }
401 }
402 
403 
405 {
406  wxCHECK_RET( aCfg, "Call to SCH_BASE_FRAME::SaveSettings with null settings" );
407 
409 
410  if( eeconfig() )
411  {
412  // Save template fieldnames
413  STRING_FORMATTER sf;
414  m_templateFieldNames.Format( &sf, 0, true );
415 
416  wxString record = FROM_UTF8( sf.GetString().c_str() );
417  record.Replace( wxT("\n"), wxT(""), true ); // strip all newlines
418  record.Replace( wxT(" "), wxT(" "), true ); // double space to single
419 
420  eeconfig()->m_Drawing.field_names = record.ToStdString();
421  }
422 }
423 
424 
426  PANEL_HOTKEYS_EDITOR* aHotkeysPanel )
427 {
428  wxTreebook* book = aParent->GetTreebook();
429 
430  book->AddPage( new wxPanel( book ), _( "Symbol Editor" ) );
431  book->AddSubPage( new PANEL_GAL_DISPLAY_OPTIONS( this, aParent ), _( "Display Options" ) );
432  book->AddSubPage( new PANEL_LIBEDIT_SETTINGS( this, book ), _( "Editing Options" ) );
433  book->AddSubPage( new PANEL_LIBEDIT_COLOR_SETTINGS( this, book ), _( "Colors" ) );
434 
435  aHotkeysPanel->AddHotKeys( GetToolManager() );
436 }
437 
438 
439 SYMBOL_LIB_TABLE* PROJECT::SchSymbolLibTable()
440 {
441  // This is a lazy loading function, it loads the project specific table when
442  // that table is asked for, not before.
444 
445  // its gotta be NULL or a SYMBOL_LIB_TABLE, or a bug.
446  wxASSERT( !tbl || tbl->Type() == SYMBOL_LIB_TABLE_T );
447 
448  if( !tbl )
449  {
450  // Stack the project specific SYMBOL_LIB_TABLE overlay on top of the global table.
451  // ~SYMBOL_LIB_TABLE() will not touch the fallback table, so multiple projects may
452  // stack this way, all using the same global fallback table.
454 
456 
457  wxString prjPath;
458 
459  wxGetEnv( PROJECT_VAR_NAME, &prjPath );
460 
461  if( !prjPath.IsEmpty() )
462  {
463  wxFileName fn( prjPath, SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
464 
465  try
466  {
467  tbl->Load( fn.GetFullPath() );
468  }
469  catch( const IO_ERROR& ioe )
470  {
471  wxString msg;
472  msg.Printf( _( "An error occurred loading the symbol library table \"%s\"." ),
473  fn.GetFullPath() );
474  DisplayErrorMessage( NULL, msg, ioe.What() );
475  }
476  }
477  }
478 
479  return tbl;
480 }
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 & 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 ResolveNetClassAssignments()
Explodes the list of netclass assignments to include atomic members of composite labels (buses).
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:114
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
TEMPLATES m_templateFieldNames
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:240
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:78
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:252
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:59
EESCHEMA_SETTINGS * eeconfig() const
wxTreebook * GetTreebook()
Definition: paged_dialog.h:45
#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:91
#define NULL
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
KICAD_T Type() override
static int GetSubpartFirstId()
GRID_SETTINGS grid
Definition: app_settings.h:81
int ShowQuasiModal()
VTBL_ENTRY PROJECT_FILE & GetProjectFile() const
Definition: project.h:129
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
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
#define MAX_ZOOM_FACTOR
std::map< int, int > m_Severities
Definition: erc_settings.h:157
Container for ERC settings.
Definition: erc_settings.h:87
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:153
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
#define TO_UTF8(wxstring)
#define DBG(x)
Definition: fctsys.h:33
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:74
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:126
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