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.

334 lines
12 KiB

18 years ago
18 years ago
3 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014-2023 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <mutex>
  20. #include <wx/ffile.h>
  21. #include <symbol_library.h>
  22. #include <confirm.h>
  23. #include <dialogs/dialog_schematic_setup.h>
  24. #include <kiway.h>
  25. #include <symbol_edit_frame.h>
  26. #include <dialogs/panel_gal_display_options.h>
  27. #include <pgm_base.h>
  28. #include <project/project_file.h>
  29. #include <project/net_settings.h>
  30. #include <sch_edit_frame.h>
  31. #include <sch_painter.h>
  32. #include <schematic.h>
  33. #include <widgets/hierarchy_pane.h>
  34. #include <widgets/sch_search_pane.h>
  35. #include <widgets/properties_panel.h>
  36. #include <settings/app_settings.h>
  37. #include <settings/settings_manager.h>
  38. #include <symbol_lib_table.h>
  39. #include <wildcards_and_files_ext.h>
  40. #include <drawing_sheet/ds_data_model.h>
  41. #include <zoom_defines.h>
  42. #include <sim/spice_settings.h>
  43. /// Helper for all the old plotting/printing code while it still exists
  44. COLOR4D GetLayerColor( SCH_LAYER_ID aLayer )
  45. {
  46. return Pgm().GetSettingsManager().GetColorSettings()->GetColor( aLayer );
  47. }
  48. bool SCH_EDIT_FRAME::LoadProjectSettings()
  49. {
  50. SCHEMATIC_SETTINGS& settings = Schematic().Settings();
  51. settings.m_JunctionSize = GetSchematicJunctionSize();
  52. GetRenderSettings()->SetDefaultPenWidth( settings.m_DefaultLineWidth );
  53. GetRenderSettings()->m_LabelSizeRatio = settings.m_LabelSizeRatio;
  54. GetRenderSettings()->m_TextOffsetRatio = settings.m_TextOffsetRatio;
  55. GetRenderSettings()->m_PinSymbolSize = settings.m_PinSymbolSize;
  56. GetRenderSettings()->SetDashLengthRatio( settings.m_DashedLineDashRatio );
  57. GetRenderSettings()->SetGapLengthRatio( settings.m_DashedLineGapRatio );
  58. // Verify some values, because the config file can be edited by hand, and have bad values:
  59. LIB_SYMBOL::SetSubpartIdNotation( LIB_SYMBOL::GetSubpartIdSeparator(),
  60. LIB_SYMBOL::GetSubpartFirstId() );
  61. BASE_SCREEN::m_DrawingSheetFileName = settings.m_SchDrawingSheetFileName;
  62. // Load the drawing sheet from the filename stored in BASE_SCREEN::m_DrawingSheetFileName.
  63. // If empty, or not existing, the default drawing sheet is loaded.
  64. wxString filename = DS_DATA_MODEL::ResolvePath( BASE_SCREEN::m_DrawingSheetFileName,
  65. Prj().GetProjectPath() );
  66. if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename ) )
  67. ShowInfoBarError( _( "Error loading drawing sheet." ), true );
  68. return true;
  69. }
  70. void SCH_EDIT_FRAME::ShowSchematicSetupDialog( const wxString& aInitialPage )
  71. {
  72. DIALOG_SCHEMATIC_SETUP dlg( this );
  73. if( !aInitialPage.IsEmpty() )
  74. dlg.SetInitialPage( aInitialPage, wxEmptyString );
  75. if( dlg.ShowQuasiModal() == wxID_OK )
  76. {
  77. // Mark document as modified so that project settings can be saved as part of doc save
  78. OnModify();
  79. Kiway().CommonSettingsChanged( false, true );
  80. Prj().IncrementTextVarsTicker();
  81. Prj().IncrementNetclassesTicker();
  82. GetRenderSettings()->SetDefaultPenWidth( Schematic().Settings().m_DefaultLineWidth );
  83. GetRenderSettings()->m_LabelSizeRatio = Schematic().Settings().m_LabelSizeRatio;
  84. GetRenderSettings()->m_TextOffsetRatio = Schematic().Settings().m_TextOffsetRatio;
  85. GetRenderSettings()->m_PinSymbolSize = Schematic().Settings().m_PinSymbolSize;
  86. GetRenderSettings()->SetDashLengthRatio( Schematic().Settings().m_DashedLineDashRatio );
  87. GetRenderSettings()->SetGapLengthRatio( Schematic().Settings().m_DashedLineGapRatio );
  88. GetCanvas()->GetView()->MarkDirty();
  89. GetCanvas()->GetView()->UpdateAllItems( KIGFX::REPAINT );
  90. RefreshOperatingPointDisplay();
  91. GetCanvas()->Refresh();
  92. }
  93. }
  94. int SCH_EDIT_FRAME::GetSchematicJunctionSize()
  95. {
  96. std::vector<double>& sizeMultipliers = eeconfig()->m_Drawing.junction_size_mult_list;
  97. const std::shared_ptr<NET_SETTINGS>& netSettings = Prj().GetProjectFile().NetSettings();
  98. int sizeChoice = Schematic().Settings().m_JunctionSizeChoice;
  99. int dotSize = netSettings->m_DefaultNetClass->GetWireWidth() * sizeMultipliers[ sizeChoice ];
  100. return std::max( dotSize, 1 );
  101. }
  102. void SCH_EDIT_FRAME::saveProjectSettings()
  103. {
  104. wxFileName fn = Schematic().RootScreen()->GetFileName(); //ConfigFileName
  105. fn.SetExt( ProjectFileExtension );
  106. if( !fn.HasName() || !IsWritable( fn, false ) )
  107. return;
  108. Schematic().RecordERCExclusions();
  109. if( Kiway().Player( FRAME_SIMULATOR, false ) )
  110. Prj().GetProjectFile().m_SchematicSettings->m_NgspiceSettings->SaveToFile();
  111. // Save the page layout file if doesn't exist yet (e.g. if we opened a non-kicad schematic)
  112. // TODO: We need to remove dependence on BASE_SCREEN
  113. Prj().GetProjectFile().m_SchematicSettings->m_SchDrawingSheetFileName
  114. = BASE_SCREEN::m_DrawingSheetFileName;
  115. if( !BASE_SCREEN::m_DrawingSheetFileName.IsEmpty() )
  116. {
  117. wxFileName layoutfn( DS_DATA_MODEL::ResolvePath( BASE_SCREEN::m_DrawingSheetFileName,
  118. Prj().GetProjectPath() ) );
  119. bool success = true;
  120. if( !layoutfn.IsAbsolute() )
  121. success = layoutfn.MakeAbsolute( Prj().GetProjectPath() );
  122. if( success && layoutfn.IsOk() && !layoutfn.FileExists() )
  123. {
  124. if( layoutfn.DirExists() && layoutfn.IsDirWritable() )
  125. DS_DATA_MODEL::GetTheInstance().Save( layoutfn.GetFullPath() );
  126. }
  127. }
  128. GetSettingsManager()->SaveProject( fn.GetFullPath() );
  129. }
  130. void SCH_EDIT_FRAME::SaveProjectLocalSettings()
  131. {
  132. if( m_schematic )
  133. m_schematic->RecordERCExclusions();
  134. }
  135. void SCH_EDIT_FRAME::LoadSettings( APP_SETTINGS_BASE* aCfg )
  136. {
  137. // For now, axes are forced off in Eeschema even if turned on in config
  138. EESCHEMA_SETTINGS* cfg = eeconfig();
  139. cfg->m_Window.grid.axes_enabled = false;
  140. SCH_BASE_FRAME::LoadSettings( cfg );
  141. SCH_SEARCH_DATA* searchData = dynamic_cast<SCH_SEARCH_DATA*>( m_findReplaceData.get() );
  142. if( searchData )
  143. {
  144. searchData->replaceReferences = cfg->m_FindReplaceExtra.replace_references;
  145. searchData->searchAllFields = cfg->m_FindReplaceExtra.search_all_fields;
  146. searchData->searchAllPins = cfg->m_FindReplaceExtra.search_all_pins;
  147. searchData->searchCurrentSheetOnly = cfg->m_FindReplaceExtra.search_current_sheet_only;
  148. searchData->searchSelectedOnly = cfg->m_FindReplaceExtra.search_selected_only;
  149. }
  150. m_show_search = cfg->m_AuiPanels.show_search;
  151. GetRenderSettings()->m_ShowPinsElectricalType = false;
  152. GetRenderSettings()->m_ShowPinNumbers = false;
  153. GetRenderSettings()->SetDefaultFont( cfg->m_Appearance.default_font );
  154. }
  155. void SCH_EDIT_FRAME::SaveSettings( APP_SETTINGS_BASE* aCfg )
  156. {
  157. EESCHEMA_SETTINGS* cfg = eeconfig();
  158. SCH_BASE_FRAME::SaveSettings( cfg );
  159. wxAuiPaneInfo& hierarchy_pane = m_auimgr.GetPane( SchematicHierarchyPaneName() );
  160. if( cfg )
  161. {
  162. cfg->m_System.units = static_cast<int>( GetUserUnits() );
  163. cfg->m_AuiPanels.show_schematic_hierarchy = hierarchy_pane.IsShown();
  164. cfg->m_AuiPanels.schematic_hierarchy_float = hierarchy_pane.IsFloating();
  165. // Other parameters (hierarchy_panel_float_width, hierarchy_panel_float_height,
  166. // and hierarchy_panel_docked_width should have been updated when resizing the
  167. // hierarchy panel
  168. SCH_SEARCH_DATA* searchData = dynamic_cast<SCH_SEARCH_DATA*>( m_findReplaceData.get() );
  169. if( searchData )
  170. {
  171. cfg->m_FindReplaceExtra.replace_references = searchData->replaceReferences;
  172. cfg->m_FindReplaceExtra.search_all_fields = searchData->searchAllFields;
  173. cfg->m_FindReplaceExtra.search_all_pins = searchData->searchAllPins;
  174. cfg->m_FindReplaceExtra.search_current_sheet_only =
  175. searchData->searchCurrentSheetOnly;
  176. cfg->m_FindReplaceExtra.search_selected_only = searchData->searchSelectedOnly;
  177. }
  178. wxAuiPaneInfo& searchPaneInfo = m_auimgr.GetPane( SearchPaneName() );
  179. m_show_search = searchPaneInfo.IsShown();
  180. cfg->m_AuiPanels.show_search = m_show_search;
  181. cfg->m_AuiPanels.search_panel_height = m_searchPane->GetSize().y;
  182. cfg->m_AuiPanels.search_panel_width = m_searchPane->GetSize().x;
  183. cfg->m_AuiPanels.search_panel_dock_direction = searchPaneInfo.dock_direction;
  184. wxAuiPaneInfo& propertiesPane = m_auimgr.GetPane( PropertiesPaneName() );
  185. cfg->m_AuiPanels.show_properties = propertiesPane.IsShown();
  186. cfg->m_AuiPanels.properties_splitter = m_propertiesPanel->SplitterProportion();
  187. }
  188. }
  189. void SCH_BASE_FRAME::LoadSettings( APP_SETTINGS_BASE* aCfg )
  190. {
  191. wxCHECK_RET( aCfg, "Call to SCH_BASE_FRAME::LoadSettings with null settings" );
  192. EDA_DRAW_FRAME::LoadSettings( aCfg );
  193. if( aCfg->m_Window.grid.grids.empty() )
  194. aCfg->m_Window.grid.grids = aCfg->DefaultGridSizeList();
  195. // Move legacy user grids to grid list
  196. if( !aCfg->m_Window.grid.user_grid_x.empty() )
  197. {
  198. aCfg->m_Window.grid.grids.emplace_back( GRID{ "User Grid", aCfg->m_Window.grid.user_grid_x,
  199. aCfg->m_Window.grid.user_grid_y } );
  200. aCfg->m_Window.grid.user_grid_x = wxEmptyString;
  201. aCfg->m_Window.grid.user_grid_y = wxEmptyString;
  202. }
  203. if( aCfg->m_Window.grid.last_size_idx > (int) aCfg->m_Window.grid.grids.size() )
  204. aCfg->m_Window.grid.last_size_idx = 1;
  205. if( aCfg->m_Window.grid.fast_grid_1 > (int) aCfg->m_Window.grid.grids.size() )
  206. aCfg->m_Window.grid.fast_grid_1 = 1;
  207. if( aCfg->m_Window.grid.fast_grid_2 > (int) aCfg->m_Window.grid.grids.size() )
  208. aCfg->m_Window.grid.fast_grid_2 = 2;
  209. if( aCfg->m_Window.zoom_factors.empty() )
  210. {
  211. aCfg->m_Window.zoom_factors = { ZOOM_LIST_EESCHEMA };
  212. }
  213. }
  214. void SCH_BASE_FRAME::SaveSettings( APP_SETTINGS_BASE* aCfg )
  215. {
  216. wxCHECK_RET( aCfg, wxS( "Call to SCH_BASE_FRAME::SaveSettings with null settings" ) );
  217. EDA_DRAW_FRAME::SaveSettings( aCfg );
  218. }
  219. static std::mutex s_symbolTableMutex;
  220. SYMBOL_LIB_TABLE* PROJECT::SchSymbolLibTable()
  221. {
  222. std::lock_guard<std::mutex> lock( s_symbolTableMutex );
  223. // This is a lazy loading function, it loads the project specific table when
  224. // that table is asked for, not before.
  225. SYMBOL_LIB_TABLE* tbl = (SYMBOL_LIB_TABLE*) GetElem( ELEM_SYMBOL_LIB_TABLE );
  226. // its gotta be NULL or a SYMBOL_LIB_TABLE, or a bug.
  227. wxASSERT( !tbl || tbl->Type() == SYMBOL_LIB_TABLE_T );
  228. if( !tbl )
  229. {
  230. // Stack the project specific SYMBOL_LIB_TABLE overlay on top of the global table.
  231. // ~SYMBOL_LIB_TABLE() will not touch the fallback table, so multiple projects may
  232. // stack this way, all using the same global fallback table.
  233. tbl = new SYMBOL_LIB_TABLE( &SYMBOL_LIB_TABLE::GetGlobalLibTable() );
  234. SetElem( ELEM_SYMBOL_LIB_TABLE, tbl );
  235. wxString prjPath;
  236. wxGetEnv( PROJECT_VAR_NAME, &prjPath );
  237. if( !prjPath.IsEmpty() )
  238. {
  239. wxFileName fn( prjPath, SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
  240. try
  241. {
  242. tbl->Load( fn.GetFullPath() );
  243. }
  244. catch( const IO_ERROR& ioe )
  245. {
  246. wxString msg;
  247. msg.Printf( _( "Error loading the symbol library table '%s'." ),
  248. fn.GetFullPath() );
  249. DisplayErrorMessage( nullptr, msg, ioe.What() );
  250. }
  251. }
  252. }
  253. return tbl;
  254. }