You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
9.6 KiB

  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. #include <base_screen.h>
  22. #include <lib_symbol.h>
  23. #include <convert_to_biu.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>
  29. #include <settings/json_settings_internals.h>
  30. #include <settings/parameters.h>
  31. #include <sim/spice_settings.h>
  32. const int schSettingsSchemaVersion = 1;
  33. SCHEMATIC_SETTINGS::SCHEMATIC_SETTINGS( JSON_SETTINGS* aParent, const std::string& aPath ) :
  34. NESTED_SETTINGS( "schematic", schSettingsSchemaVersion, aParent, aPath ),
  35. m_DefaultLineWidth( DEFAULT_LINE_WIDTH_MILS * IU_PER_MILS ),
  36. m_DefaultTextSize( DEFAULT_TEXT_SIZE * IU_PER_MILS ),
  37. m_LabelSizeRatio( DEFAULT_LABEL_SIZE_RATIO ),
  38. m_TextOffsetRatio( DEFAULT_TEXT_OFFSET_RATIO ),
  39. m_PinSymbolSize( DEFAULT_TEXT_SIZE * IU_PER_MILS / 2 ),
  40. m_JunctionSizeChoice( 3 ),
  41. m_JunctionSize( DEFAULT_JUNCTION_DIAM * IU_PER_MILS ),
  42. m_AnnotateStartNum( 0 ),
  43. m_IntersheetRefsShow( false ),
  44. m_IntersheetRefsListOwnPage( true ),
  45. m_IntersheetRefsFormatShort( false ),
  46. m_IntersheetRefsPrefix( DEFAULT_IREF_PREFIX ),
  47. m_IntersheetRefsSuffix( DEFAULT_IREF_SUFFIX ),
  48. m_DashedLineDashRatio( 12.0 ),
  49. m_DashedLineGapRatio( 3.0 ),
  50. m_SpiceAdjustPassiveValues( false ),
  51. m_NgspiceSimulatorSettings( nullptr )
  52. {
  53. EESCHEMA_SETTINGS* appSettings = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
  54. int defaultLineThickness =
  55. appSettings ? appSettings->m_Drawing.default_line_thickness : DEFAULT_LINE_WIDTH_MILS;
  56. int defaultTextSize =
  57. appSettings ? appSettings->m_Drawing.default_text_size : DEFAULT_TEXT_SIZE;
  58. int defaultPinSymbolSize =
  59. appSettings ? appSettings->m_Drawing.pin_symbol_size : DEFAULT_TEXT_SIZE / 2;
  60. int defaultJunctionSizeChoice =
  61. appSettings ? appSettings->m_Drawing.junction_size_choice : 3;
  62. bool defaultIntersheetsRefShow =
  63. appSettings ? appSettings->m_Drawing.intersheets_ref_show : false;
  64. bool defaultIntersheetsRefOwnPage =
  65. appSettings ? appSettings->m_Drawing.intersheets_ref_own_page : true;
  66. bool defaultIntersheetsRefFormatShort =
  67. appSettings ? appSettings->m_Drawing.intersheets_ref_short : false;
  68. wxString defaultIntersheetsRefPrefix =
  69. appSettings ? appSettings->m_Drawing.intersheets_ref_prefix : DEFAULT_IREF_PREFIX;
  70. wxString defaultIntersheetsRefSuffix =
  71. appSettings ? appSettings->m_Drawing.intersheets_ref_suffix : DEFAULT_IREF_SUFFIX;
  72. m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_show",
  73. &m_IntersheetRefsShow, defaultIntersheetsRefShow ) );
  74. m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_own_page",
  75. &m_IntersheetRefsListOwnPage, defaultIntersheetsRefOwnPage ) );
  76. m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_short",
  77. &m_IntersheetRefsFormatShort, defaultIntersheetsRefFormatShort ) );
  78. m_params.emplace_back( new PARAM<wxString>( "drawing.intersheets_ref_prefix",
  79. &m_IntersheetRefsPrefix, defaultIntersheetsRefPrefix ) );
  80. m_params.emplace_back( new PARAM<wxString>( "drawing.intersheets_ref_suffix",
  81. &m_IntersheetRefsSuffix, defaultIntersheetsRefSuffix ) );
  82. m_params.emplace_back( new PARAM<double>( "drawing.dashed_lines_dash_length_ratio",
  83. &m_DashedLineDashRatio, 12.0 ) ); // Default from ISO 128-2
  84. m_params.emplace_back( new PARAM<double>( "drawing.dashed_lines_gap_length_ratio",
  85. &m_DashedLineGapRatio, 3.0 ) ); // Default from ISO 128-2
  86. m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_line_thickness",
  87. &m_DefaultLineWidth, Mils2iu( defaultLineThickness ), Mils2iu( 5 ), Mils2iu( 1000 ),
  88. 1 / IU_PER_MILS ) );
  89. m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_text_size",
  90. &m_DefaultTextSize, Mils2iu( defaultTextSize ), Mils2iu( 5 ), Mils2iu( 1000 ),
  91. 1 / IU_PER_MILS ) );
  92. m_params.emplace_back( new PARAM<double>( "drawing.text_offset_ratio",
  93. &m_TextOffsetRatio, DEFAULT_TEXT_OFFSET_RATIO, 0.0, 2.0 ) );
  94. m_params.emplace_back( new PARAM<double>( "drawing.label_size_ratio",
  95. &m_LabelSizeRatio, DEFAULT_LABEL_SIZE_RATIO, 0.0, 2.0 ) );
  96. m_params.emplace_back( new PARAM_SCALED<int>( "drawing.pin_symbol_size",
  97. &m_PinSymbolSize, Mils2iu( defaultPinSymbolSize ), Mils2iu( 0 ), Mils2iu( 1000 ),
  98. 1 / IU_PER_MILS ) );
  99. // m_JunctionSize is only a run-time cache of the calculated size. Do not save it.
  100. // User choice for junction dot size ( e.g. none = 0, smallest = 1, small = 2, etc )
  101. m_params.emplace_back( new PARAM<int>( "drawing.junction_size_choice",
  102. &m_JunctionSizeChoice,
  103. defaultJunctionSizeChoice ) );
  104. m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "drawing.field_names",
  105. [&]() -> nlohmann::json
  106. {
  107. nlohmann::json ret = nlohmann::json::array();
  108. for( const TEMPLATE_FIELDNAME& field :
  109. m_TemplateFieldNames.GetTemplateFieldNames( false ) )
  110. {
  111. ret.push_back( nlohmann::json( {
  112. { "name", field.m_Name },
  113. { "visible", field.m_Visible },
  114. { "url", field.m_URL }
  115. } ) );
  116. }
  117. return ret;
  118. },
  119. [&]( const nlohmann::json& aJson )
  120. {
  121. if( !aJson.empty() && aJson.is_array() )
  122. {
  123. m_TemplateFieldNames.DeleteAllFieldNameTemplates( false );
  124. for( const nlohmann::json& entry : aJson )
  125. {
  126. if( !entry.contains( "name" ) || !entry.contains( "url" )
  127. || !entry.contains( "visible" ) )
  128. {
  129. continue;
  130. }
  131. TEMPLATE_FIELDNAME field( entry["name"].get<wxString>() );
  132. field.m_URL = entry["url"].get<bool>();
  133. field.m_Visible = entry["visible"].get<bool>();
  134. m_TemplateFieldNames.AddTemplateFieldName( field, false );
  135. }
  136. }
  137. auto* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
  138. if( cfg )
  139. {
  140. // Read global fieldname templates
  141. wxString templateFieldNames = cfg->m_Drawing.field_names;
  142. if( !templateFieldNames.IsEmpty() )
  143. {
  144. TEMPLATE_FIELDNAMES_LEXER field_lexer( TO_UTF8( templateFieldNames ) );
  145. try
  146. {
  147. m_TemplateFieldNames.Parse( &field_lexer, true );
  148. }
  149. catch( const IO_ERROR& )
  150. {
  151. }
  152. }
  153. }
  154. }, {} ) );
  155. // TODO(JE) get rid of this static
  156. m_params.emplace_back( new PARAM<wxString>( "page_layout_descr_file",
  157. &BASE_SCREEN::m_DrawingSheetFileName, "" ) );
  158. m_params.emplace_back( new PARAM<wxString>( "plot_directory",
  159. &m_PlotDirectoryName, "" ) );
  160. m_params.emplace_back( new PARAM<wxString>( "net_format_name",
  161. &m_NetFormatName, "" ) );
  162. m_params.emplace_back( new PARAM<bool>( "spice_adjust_passive_values",
  163. &m_SpiceAdjustPassiveValues, false ) );
  164. m_params.emplace_back( new PARAM<wxString>( "spice_external_command",
  165. &m_SpiceCommandString, "spice \"%I\"" ) );
  166. // TODO(JE) should we keep these LIB_SYMBOL:: things around?
  167. m_params.emplace_back( new PARAM<int>( "subpart_id_separator",
  168. LIB_SYMBOL::SubpartIdSeparatorPtr(), 0, 0, 126 ) );
  169. m_params.emplace_back( new PARAM<int>( "subpart_first_id",
  170. LIB_SYMBOL::SubpartFirstIdPtr(), 'A', '1', 'z' ) );
  171. m_params.emplace_back( new PARAM<int>( "annotate_start_num",
  172. &m_AnnotateStartNum, 0 ) );
  173. m_NgspiceSimulatorSettings =
  174. std::make_shared<NGSPICE_SIMULATOR_SETTINGS>( this, "ngspice" );
  175. registerMigration( 0, 1, [&]() -> bool
  176. {
  177. OPT<double> tor = Get<double>( "drawing.text_offset_ratio" );
  178. if( tor.is_initialized() )
  179. Set( "drawing.label_size_ratio", tor.get() );
  180. return true;
  181. } );
  182. }
  183. SCHEMATIC_SETTINGS::~SCHEMATIC_SETTINGS()
  184. {
  185. ReleaseNestedSettings( m_NgspiceSimulatorSettings.get() );
  186. m_NgspiceSimulatorSettings.reset();
  187. if( m_parent )
  188. {
  189. m_parent->ReleaseNestedSettings( this );
  190. m_parent = nullptr;
  191. }
  192. }