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-2022 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 <default_values.h>
25#include <eeschema_settings.h>
26#include <kiface_base.h>
27#include <macros.h>
28#include <schematic_settings.h>
30#include <settings/parameters.h>
31#include <sim/spice_settings.h>
32
33
35
36
37SCHEMATIC_SETTINGS::SCHEMATIC_SETTINGS( JSON_SETTINGS* aParent, const std::string& aPath ) :
38 NESTED_SETTINGS( "schematic", schSettingsSchemaVersion, aParent, aPath ),
39 m_DefaultLineWidth( DEFAULT_LINE_WIDTH_MILS * schIUScale.IU_PER_MILS ),
40 m_DefaultTextSize( DEFAULT_TEXT_SIZE * schIUScale.IU_PER_MILS ),
41 m_LabelSizeRatio( DEFAULT_LABEL_SIZE_RATIO ),
42 m_TextOffsetRatio( DEFAULT_TEXT_OFFSET_RATIO ),
43 m_PinSymbolSize( DEFAULT_TEXT_SIZE * schIUScale.IU_PER_MILS / 2 ),
44 m_JunctionSizeChoice( 3 ),
46 m_AnnotateStartNum( 0 ),
47 m_IntersheetRefsShow( false ),
48 m_IntersheetRefsListOwnPage( true ),
49 m_IntersheetRefsFormatShort( false ),
50 m_IntersheetRefsPrefix( DEFAULT_IREF_PREFIX ),
51 m_IntersheetRefsSuffix( DEFAULT_IREF_SUFFIX ),
52 m_DashedLineDashRatio( 12.0 ),
53 m_DashedLineGapRatio( 3.0 ),
54 m_SpiceAdjustPassiveValues( false ),
55 m_SpiceCurSheetAsRoot( false ),
56 m_SpiceSaveAllVoltages( false ),
57 m_SpiceSaveAllCurrents( false ),
58 m_SpiceModelCurSheetAsRoot( true ),
59 m_NgspiceSimulatorSettings( nullptr )
60{
61 EESCHEMA_SETTINGS* appSettings = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
62
63 int defaultLineThickness =
64 appSettings ? appSettings->m_Drawing.default_line_thickness : DEFAULT_LINE_WIDTH_MILS;
65 int defaultTextSize =
66 appSettings ? appSettings->m_Drawing.default_text_size : DEFAULT_TEXT_SIZE;
67 int defaultPinSymbolSize =
68 appSettings ? appSettings->m_Drawing.pin_symbol_size : DEFAULT_TEXT_SIZE / 2;
69 int defaultJunctionSizeChoice =
70 appSettings ? appSettings->m_Drawing.junction_size_choice : 3;
71 bool defaultIntersheetsRefShow =
72 appSettings ? appSettings->m_Drawing.intersheets_ref_show : false;
73 bool defaultIntersheetsRefOwnPage =
74 appSettings ? appSettings->m_Drawing.intersheets_ref_own_page : true;
75 bool defaultIntersheetsRefFormatShort =
76 appSettings ? appSettings->m_Drawing.intersheets_ref_short : false;
77 wxString defaultIntersheetsRefPrefix =
78 appSettings ? appSettings->m_Drawing.intersheets_ref_prefix : DEFAULT_IREF_PREFIX;
79 wxString defaultIntersheetsRefSuffix =
80 appSettings ? appSettings->m_Drawing.intersheets_ref_suffix : DEFAULT_IREF_SUFFIX;
81
82 m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_show",
83 &m_IntersheetRefsShow, defaultIntersheetsRefShow ) );
84
85 m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_own_page",
86 &m_IntersheetRefsListOwnPage, defaultIntersheetsRefOwnPage ) );
87
88 m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_short",
89 &m_IntersheetRefsFormatShort, defaultIntersheetsRefFormatShort ) );
90
91 m_params.emplace_back( new PARAM<wxString>( "drawing.intersheets_ref_prefix",
92 &m_IntersheetRefsPrefix, defaultIntersheetsRefPrefix ) );
93
94 m_params.emplace_back( new PARAM<wxString>( "drawing.intersheets_ref_suffix",
95 &m_IntersheetRefsSuffix, defaultIntersheetsRefSuffix ) );
96
97 m_params.emplace_back( new PARAM<double>( "drawing.dashed_lines_dash_length_ratio",
98 &m_DashedLineDashRatio, 12.0 ) ); // Default from ISO 128-2
99
100 m_params.emplace_back( new PARAM<double>( "drawing.dashed_lines_gap_length_ratio",
101 &m_DashedLineGapRatio, 3.0 ) ); // Default from ISO 128-2
102
103 m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_line_thickness",
104 &m_DefaultLineWidth, schIUScale.MilsToIU( defaultLineThickness ), schIUScale.MilsToIU( 5 ), schIUScale.MilsToIU( 1000 ),
105 1 / schIUScale.IU_PER_MILS ) );
106
107 m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_text_size",
108 &m_DefaultTextSize, schIUScale.MilsToIU( defaultTextSize ), schIUScale.MilsToIU( 5 ), schIUScale.MilsToIU( 1000 ),
109 1 / schIUScale.IU_PER_MILS ) );
110
111 m_params.emplace_back( new PARAM<double>( "drawing.text_offset_ratio",
113
114 m_params.emplace_back( new PARAM<double>( "drawing.label_size_ratio",
116
117 m_params.emplace_back( new PARAM_SCALED<int>( "drawing.pin_symbol_size",
118 &m_PinSymbolSize, schIUScale.MilsToIU( defaultPinSymbolSize ), schIUScale.MilsToIU( 0 ), schIUScale.MilsToIU( 1000 ),
119 1 / schIUScale.IU_PER_MILS ) );
120
121 // m_JunctionSize is only a run-time cache of the calculated size. Do not save it.
122
123 // User choice for junction dot size ( e.g. none = 0, smallest = 1, small = 2, etc )
124 m_params.emplace_back( new PARAM<int>( "drawing.junction_size_choice",
126 defaultJunctionSizeChoice ) );
127
128 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "drawing.field_names",
129 [&]() -> nlohmann::json
130 {
131 nlohmann::json ret = nlohmann::json::array();
132
133 for( const TEMPLATE_FIELDNAME& field :
135 {
136 ret.push_back( nlohmann::json( {
137 { "name", field.m_Name },
138 { "visible", field.m_Visible },
139 { "url", field.m_URL }
140 } ) );
141 }
142
143 return ret;
144 },
145 [&]( const nlohmann::json& aJson )
146 {
147 if( !aJson.empty() && aJson.is_array() )
148 {
150
151 for( const nlohmann::json& entry : aJson )
152 {
153 if( !entry.contains( "name" ) || !entry.contains( "url" )
154 || !entry.contains( "visible" ) )
155 {
156 continue;
157 }
158
159 TEMPLATE_FIELDNAME field( entry["name"].get<wxString>() );
160 field.m_URL = entry["url"].get<bool>();
161 field.m_Visible = entry["visible"].get<bool>();
163 }
164 }
165
166 auto* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
167
168 if( cfg )
169 {
170 // Read global fieldname templates
171 wxString templateFieldNames = cfg->m_Drawing.field_names;
172
173 if( !templateFieldNames.IsEmpty() )
174 {
175 TEMPLATE_FIELDNAMES_LEXER field_lexer( TO_UTF8( templateFieldNames ) );
176
177 try
178 {
179 m_TemplateFieldNames.Parse( &field_lexer, true );
180 }
181 catch( const IO_ERROR& )
182 {
183 }
184 }
185 }
186 }, {} ) );
187
188 // TODO(JE) get rid of this static
189 m_params.emplace_back( new PARAM<wxString>( "page_layout_descr_file",
191
192 m_params.emplace_back( new PARAM<wxString>( "plot_directory",
193 &m_PlotDirectoryName, "" ) );
194
195 m_params.emplace_back( new PARAM<wxString>( "net_format_name",
196 &m_NetFormatName, "" ) );
197
198 m_params.emplace_back( new PARAM<bool>( "spice_adjust_passive_values",
199 &m_SpiceAdjustPassiveValues, false ) );
200
201 m_params.emplace_back( new PARAM<bool>( "spice_save_all_voltages",
202 &m_SpiceSaveAllVoltages, false ) );
203
204 m_params.emplace_back( new PARAM<bool>( "spice_save_all_currents",
205 &m_SpiceSaveAllCurrents, false ) );
206
207 m_params.emplace_back( new PARAM<wxString>( "spice_external_command",
208 &m_SpiceCommandString, "spice \"%I\"" ) );
209
210 // TODO(JE) should we keep these LIB_SYMBOL:: things around?
211 m_params.emplace_back( new PARAM<int>( "subpart_id_separator",
212 LIB_SYMBOL::SubpartIdSeparatorPtr(), 0, 0, 126 ) );
213
214 m_params.emplace_back( new PARAM<int>( "subpart_first_id",
215 LIB_SYMBOL::SubpartFirstIdPtr(), 'A', '1', 'z' ) );
216
217 m_params.emplace_back( new PARAM<int>( "annotate_start_num",
218 &m_AnnotateStartNum, 0 ) );
219
221 std::make_shared<NGSPICE_SIMULATOR_SETTINGS>( this, "ngspice" );
222
223 registerMigration( 0, 1,
224 [&]() -> bool
225 {
226 std::optional<double> tor = Get<double>( "drawing.text_offset_ratio" );
227
228 if( tor )
229 Set( "drawing.label_size_ratio", *tor );
230
231 return true;
232 } );
233}
234
235
237{
240
241 if( m_parent )
242 {
244 m_parent = nullptr;
245 }
246}
BASE_SCREEN class implementation.
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:111
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
static wxString m_DrawingSheetFileName
the name of the drawing sheet file, or empty to use the default drawing sheet
Definition: base_screen.h:85
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:76
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
void registerMigration(int aOldSchemaVersion, int aNewSchemaVersion, std::function< bool(void)> aMigrator)
Registers a migration from one schema version to another.
void ReleaseNestedSettings(NESTED_SETTINGS *aSettings)
Saves and frees a nested settings object, if it exists within this one.
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...
APP_SETTINGS_BASE * KifaceSettings() const
Definition: kiface_base.h:93
static int * SubpartIdSeparatorPtr()
Return a reference to m_subpartIdSeparator, only for read/save setting functions.
Definition: lib_symbol.h:586
static int * SubpartFirstIdPtr()
Return a reference to m_subpartFirstId, only for read/save setting functions.
Definition: lib_symbol.h:592
NESTED_SETTINGS is a JSON_SETTINGS that lives inside a JSON_SETTINGS.
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:335
SCHEMATIC_SETTINGS(JSON_SETTINGS *aParent, const std::string &aPath)
TEMPLATES m_TemplateFieldNames
std::shared_ptr< NGSPICE_SIMULATOR_SETTINGS > m_NgspiceSimulatorSettings
Ngspice simulator settings.
void AddTemplateFieldName(const TEMPLATE_FIELDNAME &aFieldName, bool aGlobal)
Insert or append a wanted symbol field name into the field names template.
void DeleteAllFieldNameTemplates(bool aGlobal)
Delete the entire contents.
void Parse(TEMPLATE_FIELDNAMES_LEXER *in, bool aGlobal)
Fill this object from information in the input stream handled by #TEMPLATE_FIELDNAMES_LEXER.
const TEMPLATE_FIELDNAMES & GetTemplateFieldNames()
Return a template field name list for read only access.
#define DEFAULT_IREF_PREFIX
The intersheets references suffix string.
#define DEFAULT_LABEL_SIZE_RATIO
The offset of the pin name string from the end of the pin in mils.
#define DEFAULT_JUNCTION_DIAM
The default bus and wire entry size in mils.
#define DEFAULT_LINE_WIDTH_MILS
The default wire width in mils. (can be changed in preference menu)
#define DEFAULT_TEXT_OFFSET_RATIO
Ratio of the font height to space around global labels.
#define DEFAULT_IREF_SUFFIX
Radius of snap "gravity well".
#define DEFAULT_TEXT_SIZE
Ratio of the font height to the baseline of the text above the wire.
nlohmann::json json
Definition: gerbview.cpp:44
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
#define IU_PER_MILS
Definition: plotter.cpp:128
const int schSettingsSchemaVersion
const double IU_PER_MILS
Definition: base_units.h:78
constexpr int MilsToIU(int mils) const
Definition: base_units.h:94
Hold a name of a symbol's field, field value, and default visibility.