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 <jon@craftyjon.com>
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_DefaultWireThickness( DEFAULT_WIRE_WIDTH_MILS * IU_PER_MILS ),
42  m_DefaultBusThickness( DEFAULT_BUS_WIDTH_MILS * IU_PER_MILS ),
43  m_DefaultTextSize( DEFAULT_TEXT_SIZE * IU_PER_MILS ),
44  m_LabelSizeRatio( DEFAULT_LABEL_SIZE_RATIO ),
45  m_TextOffsetRatio( DEFAULT_TEXT_OFFSET_RATIO ),
46  m_PinSymbolSize( DEFAULT_TEXT_SIZE * IU_PER_MILS / 2 ),
47  m_JunctionSizeChoice( 3 ),
48  m_JunctionSize( DEFAULT_JUNCTION_DIAM * IU_PER_MILS ),
49  m_AnnotateStartNum( 0 ),
50  m_IntersheetRefsShow( false ),
51  m_IntersheetRefsListOwnPage( true ),
52  m_IntersheetRefsFormatShort( false ),
53  m_IntersheetRefsPrefix( DEFAULT_IREF_PREFIX ),
54  m_IntersheetRefsSuffix( DEFAULT_IREF_SUFFIX ),
55  m_SpiceAdjustPassiveValues( false ),
56  m_NgspiceSimulatorSettings( nullptr )
57 {
58  EESCHEMA_SETTINGS* appSettings = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
59 
60  int defaultLineThickness =
61  appSettings ? appSettings->m_Drawing.default_line_thickness : DEFAULT_LINE_WIDTH_MILS;
62  int defaultWireThickness =
63  appSettings ? appSettings->m_Drawing.default_wire_thickness : DEFAULT_WIRE_WIDTH_MILS;
64  int defaultBusThickness =
65  appSettings ? appSettings->m_Drawing.default_bus_thickness : DEFAULT_BUS_WIDTH_MILS;
66  int defaultTextSize =
67  appSettings ? appSettings->m_Drawing.default_text_size : DEFAULT_TEXT_SIZE;
68  int defaultPinSymbolSize =
69  appSettings ? appSettings->m_Drawing.pin_symbol_size : DEFAULT_TEXT_SIZE / 2;
70  int defaultJunctionSizeChoice =
71  appSettings ? appSettings->m_Drawing.junction_size_choice : 3;
72  bool defaultIntersheetsRefShow =
73  appSettings ? appSettings->m_Drawing.intersheets_ref_show : false;
74  bool defaultIntersheetsRefOwnPage =
75  appSettings ? appSettings->m_Drawing.intersheets_ref_own_page : true;
76  bool defaultIntersheetsRefFormatShort =
77  appSettings ? appSettings->m_Drawing.intersheets_ref_short : false;
78  wxString defaultIntersheetsRefPrefix =
79  appSettings ? appSettings->m_Drawing.intersheets_ref_prefix : DEFAULT_IREF_PREFIX;
80  wxString defaultIntersheetsRefSuffix =
81  appSettings ? appSettings->m_Drawing.intersheets_ref_suffix : DEFAULT_IREF_SUFFIX;
82 
83  m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_show",
84  &m_IntersheetRefsShow, defaultIntersheetsRefShow ) );
85 
86  m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_own_page",
87  &m_IntersheetRefsListOwnPage, defaultIntersheetsRefOwnPage ) );
88 
89  m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_short",
90  &m_IntersheetRefsFormatShort, defaultIntersheetsRefFormatShort ) );
91 
92  m_params.emplace_back( new PARAM<wxString>( "drawing.intersheets_ref_prefix",
93  &m_IntersheetRefsPrefix, defaultIntersheetsRefPrefix ) );
94 
95  m_params.emplace_back( new PARAM<wxString>( "drawing.intersheets_ref_suffix",
96  &m_IntersheetRefsSuffix, defaultIntersheetsRefSuffix ) );
97 
98  m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_line_thickness",
99  &m_DefaultLineWidth, Mils2iu( defaultLineThickness ), Mils2iu( 5 ), Mils2iu( 1000 ),
100  1 / IU_PER_MILS ) );
101 
102  m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_wire_thickness",
103  &m_DefaultWireThickness, Mils2iu( defaultWireThickness ), Mils2iu( 5 ), Mils2iu( 1000 ),
104  1 / IU_PER_MILS ) );
105 
106  m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_bus_thickness",
107  &m_DefaultBusThickness, Mils2iu( defaultBusThickness ),
108  Mils2iu( 5 ), Mils2iu( 1000 ), 1 / IU_PER_MILS ) );
109 
110  m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_text_size",
111  &m_DefaultTextSize, Mils2iu( defaultTextSize ), Mils2iu( 5 ), Mils2iu( 1000 ),
112  1 / IU_PER_MILS ) );
113 
114  m_params.emplace_back( new PARAM<double>( "drawing.text_offset_ratio",
116 
117  m_params.emplace_back( new PARAM<double>( "drawing.label_size_ratio",
119 
120  m_params.emplace_back( new PARAM_SCALED<int>( "drawing.pin_symbol_size",
121  &m_PinSymbolSize, Mils2iu( defaultPinSymbolSize ), Mils2iu( 5 ), Mils2iu( 1000 ),
122  1 / IU_PER_MILS ) );
123 
124  // m_JunctionSize is only a run-time cache of the calculated size. Do not save it.
125 
126  // User choice for junction dot size ( e.g. none = 0, smallest = 1, small = 2, etc )
127  m_params.emplace_back( new PARAM<int>( "drawing.junction_size_choice",
129  defaultJunctionSizeChoice ) );
130 
131  m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "drawing.field_names",
132  [&]() -> nlohmann::json
133  {
134  nlohmann::json ret = nlohmann::json::array();
135 
136  for( const TEMPLATE_FIELDNAME& field :
138  {
139  ret.push_back( nlohmann::json( {
140  { "name", field.m_Name },
141  { "visible", field.m_Visible },
142  { "url", field.m_URL }
143  } ) );
144  }
145 
146  return ret;
147  },
148  [&]( const nlohmann::json& aJson )
149  {
150  if( !aJson.empty() && aJson.is_array() )
151  {
153 
154  for( const nlohmann::json& entry : aJson )
155  {
156  if( !entry.contains( "name" ) || !entry.contains( "url" )
157  || !entry.contains( "visible" ) )
158  {
159  continue;
160  }
161 
162  TEMPLATE_FIELDNAME field( entry["name"].get<wxString>() );
163  field.m_URL = entry["url"].get<bool>();
164  field.m_Visible = entry["visible"].get<bool>();
166  }
167  }
168 
169  auto* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
170 
171  if( cfg )
172  {
173  // Read global fieldname templates
174  wxString templateFieldNames = cfg->m_Drawing.field_names;
175 
176  if( !templateFieldNames.IsEmpty() )
177  {
178  TEMPLATE_FIELDNAMES_LEXER field_lexer( TO_UTF8( templateFieldNames ) );
179 
180  try
181  {
182  m_TemplateFieldNames.Parse( &field_lexer, true );
183  }
184  catch( const IO_ERROR& )
185  {
186  }
187  }
188  }
189  }, {} ) );
190 
191  // TODO(JE) get rid of this static
192  m_params.emplace_back( new PARAM<wxString>( "page_layout_descr_file",
194 
195  m_params.emplace_back( new PARAM<wxString>( "plot_directory",
196  &m_PlotDirectoryName, "" ) );
197 
198  m_params.emplace_back( new PARAM<wxString>( "net_format_name",
199  &m_NetFormatName, "" ) );
200 
201  m_params.emplace_back( new PARAM<bool>( "spice_adjust_passive_values",
202  &m_SpiceAdjustPassiveValues, false ) );
203 
204  m_params.emplace_back( new PARAM<wxString>( "spice_external_command",
205  &m_SpiceCommandString, "spice \"%I\"" ) );
206 
207  // TODO(JE) should we keep these LIB_SYMBOL:: things around?
208  m_params.emplace_back( new PARAM<int>( "subpart_id_separator",
209  LIB_SYMBOL::SubpartIdSeparatorPtr(), 0, 0, 126 ) );
210 
211  m_params.emplace_back( new PARAM<int>( "subpart_first_id",
212  LIB_SYMBOL::SubpartFirstIdPtr(), 'A', '1', 'z' ) );
213 
214  m_params.emplace_back( new PARAM<int>( "annotate_start_num",
215  &m_AnnotateStartNum, 0 ) );
216 
218  std::make_shared<NGSPICE_SIMULATOR_SETTINGS>( this, "ngspice" );
219 
220  registerMigration( 0, 1, [&]() -> bool
221  {
222  OPT<double> tor = Get<double>( "drawing.text_offset_ratio" );
223 
224  if( tor.is_initialized() )
225  Set( "drawing.label_size_ratio", tor.get() );
226 
227  return true;
228  } );
229 }
230 
231 
233 {
236 
237  if( m_parent )
238  {
240  m_parent = nullptr;
241  }
242 }
#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_WIRE_WIDTH_MILS
The default bus width in mils. (can be changed in preference menu)
#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:525
#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)
#define DEFAULT_BUS_WIDTH_MILS
The default noconnect size in mils.
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:519
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:136
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.