KiCad PCB EDA Suite
schematic_settings.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) 2020 CERN
5  * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Jon Evans <[email protected]>
7  *
8  * This program is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation, either version 3 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <base_screen.h>
23 #include <lib_symbol.h>
24 #include <convert_to_biu.h>
25 #include <default_values.h>
26 #include <eeschema_settings.h>
27 #include <kiface_base.h>
28 #include <macros.h>
29 #include <schematic_settings.h>
31 #include <settings/parameters.h>
32 #include <sim/spice_settings.h>
33 
34 
36 
37 
38 SCHEMATIC_SETTINGS::SCHEMATIC_SETTINGS( JSON_SETTINGS* aParent, const std::string& aPath ) :
39  NESTED_SETTINGS( "schematic", schSettingsSchemaVersion, aParent, aPath ),
40  m_DefaultLineWidth( DEFAULT_LINE_WIDTH_MILS * IU_PER_MILS ),
41  m_DefaultTextSize( DEFAULT_TEXT_SIZE * IU_PER_MILS ),
42  m_LabelSizeRatio( DEFAULT_LABEL_SIZE_RATIO ),
43  m_TextOffsetRatio( DEFAULT_TEXT_OFFSET_RATIO ),
44  m_PinSymbolSize( DEFAULT_TEXT_SIZE * IU_PER_MILS / 2 ),
45  m_JunctionSizeChoice( 3 ),
46  m_JunctionSize( DEFAULT_JUNCTION_DIAM * IU_PER_MILS ),
47  m_AnnotateStartNum( 0 ),
48  m_IntersheetRefsShow( false ),
49  m_IntersheetRefsListOwnPage( true ),
50  m_IntersheetRefsFormatShort( false ),
51  m_IntersheetRefsPrefix( DEFAULT_IREF_PREFIX ),
52  m_IntersheetRefsSuffix( DEFAULT_IREF_SUFFIX ),
53  m_SpiceAdjustPassiveValues( false ),
54  m_NgspiceSimulatorSettings( nullptr )
55 {
56  EESCHEMA_SETTINGS* appSettings = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
57 
58  int defaultLineThickness =
59  appSettings ? appSettings->m_Drawing.default_line_thickness : DEFAULT_LINE_WIDTH_MILS;
60  int defaultTextSize =
61  appSettings ? appSettings->m_Drawing.default_text_size : DEFAULT_TEXT_SIZE;
62  int defaultPinSymbolSize =
63  appSettings ? appSettings->m_Drawing.pin_symbol_size : DEFAULT_TEXT_SIZE / 2;
64  int defaultJunctionSizeChoice =
65  appSettings ? appSettings->m_Drawing.junction_size_choice : 3;
66  bool defaultIntersheetsRefShow =
67  appSettings ? appSettings->m_Drawing.intersheets_ref_show : false;
68  bool defaultIntersheetsRefOwnPage =
69  appSettings ? appSettings->m_Drawing.intersheets_ref_own_page : true;
70  bool defaultIntersheetsRefFormatShort =
71  appSettings ? appSettings->m_Drawing.intersheets_ref_short : false;
72  wxString defaultIntersheetsRefPrefix =
73  appSettings ? appSettings->m_Drawing.intersheets_ref_prefix : DEFAULT_IREF_PREFIX;
74  wxString defaultIntersheetsRefSuffix =
75  appSettings ? appSettings->m_Drawing.intersheets_ref_suffix : DEFAULT_IREF_SUFFIX;
76 
77  m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_show",
78  &m_IntersheetRefsShow, defaultIntersheetsRefShow ) );
79 
80  m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_own_page",
81  &m_IntersheetRefsListOwnPage, defaultIntersheetsRefOwnPage ) );
82 
83  m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_short",
84  &m_IntersheetRefsFormatShort, defaultIntersheetsRefFormatShort ) );
85 
86  m_params.emplace_back( new PARAM<wxString>( "drawing.intersheets_ref_prefix",
87  &m_IntersheetRefsPrefix, defaultIntersheetsRefPrefix ) );
88 
89  m_params.emplace_back( new PARAM<wxString>( "drawing.intersheets_ref_suffix",
90  &m_IntersheetRefsSuffix, defaultIntersheetsRefSuffix ) );
91 
92  m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_line_thickness",
93  &m_DefaultLineWidth, Mils2iu( defaultLineThickness ), Mils2iu( 5 ), Mils2iu( 1000 ),
94  1 / IU_PER_MILS ) );
95 
96  m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_text_size",
97  &m_DefaultTextSize, Mils2iu( defaultTextSize ), Mils2iu( 5 ), Mils2iu( 1000 ),
98  1 / IU_PER_MILS ) );
99 
100  m_params.emplace_back( new PARAM<double>( "drawing.text_offset_ratio",
102 
103  m_params.emplace_back( new PARAM<double>( "drawing.label_size_ratio",
105 
106  m_params.emplace_back( new PARAM_SCALED<int>( "drawing.pin_symbol_size",
107  &m_PinSymbolSize, Mils2iu( defaultPinSymbolSize ), Mils2iu( 0 ), Mils2iu( 1000 ),
108  1 / IU_PER_MILS ) );
109 
110  // m_JunctionSize is only a run-time cache of the calculated size. Do not save it.
111 
112  // User choice for junction dot size ( e.g. none = 0, smallest = 1, small = 2, etc )
113  m_params.emplace_back( new PARAM<int>( "drawing.junction_size_choice",
115  defaultJunctionSizeChoice ) );
116 
117  m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "drawing.field_names",
118  [&]() -> nlohmann::json
119  {
120  nlohmann::json ret = nlohmann::json::array();
121 
122  for( const TEMPLATE_FIELDNAME& field :
124  {
125  ret.push_back( nlohmann::json( {
126  { "name", field.m_Name },
127  { "visible", field.m_Visible },
128  { "url", field.m_URL }
129  } ) );
130  }
131 
132  return ret;
133  },
134  [&]( const nlohmann::json& aJson )
135  {
136  if( !aJson.empty() && aJson.is_array() )
137  {
139 
140  for( const nlohmann::json& entry : aJson )
141  {
142  if( !entry.contains( "name" ) || !entry.contains( "url" )
143  || !entry.contains( "visible" ) )
144  {
145  continue;
146  }
147 
148  TEMPLATE_FIELDNAME field( entry["name"].get<wxString>() );
149  field.m_URL = entry["url"].get<bool>();
150  field.m_Visible = entry["visible"].get<bool>();
152  }
153  }
154 
155  auto* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
156 
157  if( cfg )
158  {
159  // Read global fieldname templates
160  wxString templateFieldNames = cfg->m_Drawing.field_names;
161 
162  if( !templateFieldNames.IsEmpty() )
163  {
164  TEMPLATE_FIELDNAMES_LEXER field_lexer( TO_UTF8( templateFieldNames ) );
165 
166  try
167  {
168  m_TemplateFieldNames.Parse( &field_lexer, true );
169  }
170  catch( const IO_ERROR& )
171  {
172  }
173  }
174  }
175  }, {} ) );
176 
177  // TODO(JE) get rid of this static
178  m_params.emplace_back( new PARAM<wxString>( "page_layout_descr_file",
180 
181  m_params.emplace_back( new PARAM<wxString>( "plot_directory",
182  &m_PlotDirectoryName, "" ) );
183 
184  m_params.emplace_back( new PARAM<wxString>( "net_format_name",
185  &m_NetFormatName, "" ) );
186 
187  m_params.emplace_back( new PARAM<bool>( "spice_adjust_passive_values",
188  &m_SpiceAdjustPassiveValues, false ) );
189 
190  m_params.emplace_back( new PARAM<wxString>( "spice_external_command",
191  &m_SpiceCommandString, "spice \"%I\"" ) );
192 
193  // TODO(JE) should we keep these LIB_SYMBOL:: things around?
194  m_params.emplace_back( new PARAM<int>( "subpart_id_separator",
195  LIB_SYMBOL::SubpartIdSeparatorPtr(), 0, 0, 126 ) );
196 
197  m_params.emplace_back( new PARAM<int>( "subpart_first_id",
198  LIB_SYMBOL::SubpartFirstIdPtr(), 'A', '1', 'z' ) );
199 
200  m_params.emplace_back( new PARAM<int>( "annotate_start_num",
201  &m_AnnotateStartNum, 0 ) );
202 
204  std::make_shared<NGSPICE_SIMULATOR_SETTINGS>( this, "ngspice" );
205 
206  registerMigration( 0, 1, [&]() -> bool
207  {
208  OPT<double> tor = Get<double>( "drawing.text_offset_ratio" );
209 
210  if( tor.is_initialized() )
211  Set( "drawing.label_size_ratio", tor.get() );
212 
213  return true;
214  } );
215 }
216 
217 
219 {
222 
223  if( m_parent )
224  {
226  m_parent = nullptr;
227  }
228 }
#define DEFAULT_LABEL_SIZE_RATIO
The offset of the pin name string from the end of the pin in mils.
void Set(const std::string &aPath, ValueType aVal)
Stores a value into the JSON document Will throw an exception if ValueType isn't something that the l...
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
#define DEFAULT_LINE_WIDTH_MILS
The default wire width in mils. (can be changed in preference menu)
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
void AddTemplateFieldName(const TEMPLATE_FIELDNAME &aFieldName, bool aGlobal)
Insert or append a wanted symbol field name into the field names template.
void Parse(TEMPLATE_FIELDNAMES_LEXER *in, bool aGlobal)
Fill this object from information in the input stream handled by #TEMPLATE_FIELDNAMES_LEXER.
TEMPLATES m_TemplateFieldNames
static int * SubpartFirstIdPtr()
Return a reference to m_subpartFirstId, only for read/save setting functions.
Definition: lib_symbol.h:536
#define DEFAULT_JUNCTION_DIAM
The default bus and wire entry size in mils.
void DeleteAllFieldNameTemplates(bool aGlobal)
Delete the entire contents.
nlohmann::json json
Definition: gerbview.cpp:41
std::shared_ptr< NGSPICE_SIMULATOR_SETTINGS > m_NgspiceSimulatorSettings
Ngspice simulator settings.
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
NESTED_SETTINGS is a JSON_SETTINGS that lives inside a JSON_SETTINGS.
#define DEFAULT_IREF_PREFIX
The intersheets references suffix string.
const int schSettingsSchemaVersion
JSON_SETTINGS * m_parent
A pointer to the parent object to load and store from.
Represents a parameter that has a scaling factor between the value in the file and the value used int...
Definition: parameters.h:333
#define DEFAULT_IREF_SUFFIX
Radius of snap "gravity well".
SCHEMATIC_SETTINGS(JSON_SETTINGS *aParent, const std::string &aPath)
void registerMigration(int aOldSchemaVersion, int aNewSchemaVersion, std::function< bool(void)> aMigrator)
Registers a migration from one schema version to another.
static int * SubpartIdSeparatorPtr()
Return a reference to m_subpartIdSeparator, only for read/save setting functions.
Definition: lib_symbol.h:530
static wxString m_DrawingSheetFileName
the name of the drawing sheet file, or empty to use the default drawing sheet
Definition: base_screen.h:85
#define IU_PER_MILS
Definition: plotter.cpp:130
boost::optional< T > OPT
Definition: optional.h:7
Hold a name of a symbol's field, field value, and default visibility.
#define DEFAULT_TEXT_SIZE
Ratio of the font height to the baseline of the text above the wire.
void ReleaseNestedSettings(NESTED_SETTINGS *aSettings)
Saves and frees a nested settings object, if it exists within this one.
BASE_SCREEN class implementation.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
const TEMPLATE_FIELDNAMES & GetTemplateFieldNames()
Return a template field name list for read only access.
#define DEFAULT_TEXT_OFFSET_RATIO
Ratio of the font height to space around global labels.