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.

332 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/project_local_settings.h>
  30. #include <project/net_settings.h>
  31. #include <sch_edit_frame.h>
  32. #include <sch_painter.h>
  33. #include <schematic.h>
  34. #include <widgets/hierarchy_pane.h>
  35. #include <widgets/sch_search_pane.h>
  36. #include <widgets/panel_sch_selection_filter.h>
  37. #include <widgets/properties_panel.h>
  38. #include <settings/app_settings.h>
  39. #include <settings/settings_manager.h>
  40. #include <wildcards_and_files_ext.h>
  41. #include <drawing_sheet/ds_data_model.h>
  42. #include <zoom_defines.h>
  43. #include <sim/spice_settings.h>
  44. #include <tool/tool_manager.h>
  45. #include <tools/ee_selection_tool.h>
  46. /// Helper for all the old plotting/printing code while it still exists
  47. COLOR4D GetLayerColor( SCH_LAYER_ID aLayer )
  48. {
  49. return Pgm().GetSettingsManager().GetColorSettings()->GetColor( aLayer );
  50. }
  51. bool SCH_EDIT_FRAME::LoadProjectSettings()
  52. {
  53. SCHEMATIC_SETTINGS& settings = Schematic().Settings();
  54. settings.m_JunctionSize = GetSchematicJunctionSize();
  55. GetRenderSettings()->SetDefaultPenWidth( settings.m_DefaultLineWidth );
  56. GetRenderSettings()->m_LabelSizeRatio = settings.m_LabelSizeRatio;
  57. GetRenderSettings()->m_TextOffsetRatio = settings.m_TextOffsetRatio;
  58. GetRenderSettings()->m_PinSymbolSize = settings.m_PinSymbolSize;
  59. GetRenderSettings()->SetDashLengthRatio( settings.m_DashedLineDashRatio );
  60. GetRenderSettings()->SetGapLengthRatio( settings.m_DashedLineGapRatio );
  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. PROJECT_LOCAL_SETTINGS& localSettings = Prj().GetLocalSettings();
  69. EE_SELECTION_TOOL* selTool = GetToolManager()->GetTool<EE_SELECTION_TOOL>();
  70. selTool->GetFilter() = localSettings.m_SchSelectionFilter;
  71. m_selectionFilterPanel->SetCheckboxesFromFilter( localSettings.m_SchSelectionFilter );
  72. return true;
  73. }
  74. void SCH_EDIT_FRAME::ShowSchematicSetupDialog( const wxString& aInitialPage )
  75. {
  76. SCH_SCREENS screens( Schematic().Root() );
  77. std::vector<std::shared_ptr<BUS_ALIAS>> oldAliases;
  78. for( SCH_SCREEN* screen = screens.GetFirst(); screen != nullptr; screen = screens.GetNext() )
  79. {
  80. for( const std::shared_ptr<BUS_ALIAS>& alias : screen->GetBusAliases() )
  81. oldAliases.push_back( alias );
  82. }
  83. DIALOG_SCHEMATIC_SETUP dlg( this );
  84. if( !aInitialPage.IsEmpty() )
  85. dlg.SetInitialPage( aInitialPage, wxEmptyString );
  86. // TODO: is QuasiModal required here?
  87. if( dlg.ShowQuasiModal() == wxID_OK )
  88. {
  89. // Mark document as modified so that project settings can be saved as part of doc save
  90. OnModify();
  91. Kiway().CommonSettingsChanged( false, true );
  92. Prj().IncrementTextVarsTicker();
  93. Prj().IncrementNetclassesTicker();
  94. Pgm().GetSettingsManager().SaveProject();
  95. GetRenderSettings()->SetDefaultPenWidth( Schematic().Settings().m_DefaultLineWidth );
  96. GetRenderSettings()->m_LabelSizeRatio = Schematic().Settings().m_LabelSizeRatio;
  97. GetRenderSettings()->m_TextOffsetRatio = Schematic().Settings().m_TextOffsetRatio;
  98. GetRenderSettings()->m_PinSymbolSize = Schematic().Settings().m_PinSymbolSize;
  99. GetRenderSettings()->SetDashLengthRatio( Schematic().Settings().m_DashedLineDashRatio );
  100. GetRenderSettings()->SetGapLengthRatio( Schematic().Settings().m_DashedLineGapRatio );
  101. GetCanvas()->GetView()->MarkDirty();
  102. GetCanvas()->GetView()->UpdateAllItems( KIGFX::REPAINT );
  103. std::vector<std::shared_ptr<BUS_ALIAS>> newAliases;
  104. for( SCH_SCREEN* screen = screens.GetFirst(); screen != nullptr; screen = screens.GetNext() )
  105. {
  106. for( const std::shared_ptr<BUS_ALIAS>& alias : screen->GetBusAliases() )
  107. newAliases.push_back( alias );
  108. }
  109. if( oldAliases != newAliases )
  110. RecalculateConnections( nullptr, GLOBAL_CLEANUP );
  111. RefreshOperatingPointDisplay();
  112. GetCanvas()->Refresh();
  113. }
  114. }
  115. int SCH_EDIT_FRAME::GetSchematicJunctionSize()
  116. {
  117. std::vector<double>& sizeMultipliers = eeconfig()->m_Drawing.junction_size_mult_list;
  118. const std::shared_ptr<NET_SETTINGS>& netSettings = Prj().GetProjectFile().NetSettings();
  119. int sizeChoice = Schematic().Settings().m_JunctionSizeChoice;
  120. int dotSize = netSettings->m_DefaultNetClass->GetWireWidth() * sizeMultipliers[ sizeChoice ];
  121. return std::max( dotSize, 1 );
  122. }
  123. void SCH_EDIT_FRAME::saveProjectSettings()
  124. {
  125. wxFileName fn = Schematic().RootScreen()->GetFileName(); //ConfigFileName
  126. fn.SetExt( FILEEXT::ProjectFileExtension );
  127. if( !fn.HasName() || !IsWritable( fn, false ) )
  128. return;
  129. Schematic().RecordERCExclusions();
  130. if( Kiway().Player( FRAME_SIMULATOR, false ) )
  131. Prj().GetProjectFile().m_SchematicSettings->m_NgspiceSettings->SaveToFile();
  132. // Save the page layout file if doesn't exist yet (e.g. if we opened a non-kicad schematic)
  133. // TODO: We need to remove dependence on BASE_SCREEN
  134. Prj().GetProjectFile().m_SchematicSettings->m_SchDrawingSheetFileName
  135. = BASE_SCREEN::m_DrawingSheetFileName;
  136. if( !BASE_SCREEN::m_DrawingSheetFileName.IsEmpty() )
  137. {
  138. wxFileName layoutfn( DS_DATA_MODEL::ResolvePath( BASE_SCREEN::m_DrawingSheetFileName,
  139. Prj().GetProjectPath() ) );
  140. bool success = true;
  141. if( !layoutfn.IsAbsolute() )
  142. success = layoutfn.MakeAbsolute( Prj().GetProjectPath() );
  143. if( success && layoutfn.IsOk() && !layoutfn.FileExists() )
  144. {
  145. if( layoutfn.DirExists() && layoutfn.IsDirWritable() )
  146. DS_DATA_MODEL::GetTheInstance().Save( layoutfn.GetFullPath() );
  147. }
  148. }
  149. GetSettingsManager()->SaveProject( fn.GetFullPath() );
  150. }
  151. void SCH_EDIT_FRAME::SaveProjectLocalSettings()
  152. {
  153. if( m_schematic )
  154. m_schematic->RecordERCExclusions();
  155. PROJECT_LOCAL_SETTINGS& localSettings = Prj().GetLocalSettings();
  156. EE_SELECTION_TOOL* selTool = GetToolManager()->GetTool<EE_SELECTION_TOOL>();
  157. localSettings.m_SchSelectionFilter = selTool->GetFilter();
  158. }
  159. void SCH_EDIT_FRAME::LoadSettings( APP_SETTINGS_BASE* aCfg )
  160. {
  161. // For now, axes are forced off in Eeschema even if turned on in config
  162. EESCHEMA_SETTINGS* cfg = eeconfig();
  163. cfg->m_Window.grid.axes_enabled = false;
  164. SCH_BASE_FRAME::LoadSettings( cfg );
  165. SCH_SEARCH_DATA* searchData = dynamic_cast<SCH_SEARCH_DATA*>( m_findReplaceData.get() );
  166. if( searchData )
  167. {
  168. searchData->replaceReferences = cfg->m_FindReplaceExtra.replace_references;
  169. searchData->searchAllFields = cfg->m_FindReplaceExtra.search_all_fields;
  170. searchData->searchAllPins = cfg->m_FindReplaceExtra.search_all_pins;
  171. searchData->searchCurrentSheetOnly = cfg->m_FindReplaceExtra.search_current_sheet_only;
  172. searchData->searchSelectedOnly = cfg->m_FindReplaceExtra.search_selected_only;
  173. }
  174. m_show_search = cfg->m_AuiPanels.show_search;
  175. GetRenderSettings()->m_ShowPinsElectricalType = false;
  176. GetRenderSettings()->m_ShowPinNumbers = false;
  177. GetRenderSettings()->SetDefaultFont( cfg->m_Appearance.default_font );
  178. }
  179. void SCH_EDIT_FRAME::SaveSettings( APP_SETTINGS_BASE* aCfg )
  180. {
  181. EESCHEMA_SETTINGS* cfg = eeconfig();
  182. SCH_BASE_FRAME::SaveSettings( cfg );
  183. wxAuiPaneInfo& hierarchy_pane = m_auimgr.GetPane( SchematicHierarchyPaneName() );
  184. if( cfg )
  185. {
  186. cfg->m_System.units = static_cast<int>( GetUserUnits() );
  187. cfg->m_AuiPanels.show_schematic_hierarchy = hierarchy_pane.IsShown();
  188. cfg->m_AuiPanels.schematic_hierarchy_float = hierarchy_pane.IsFloating();
  189. // Other parameters (hierarchy_panel_float_width, hierarchy_panel_float_height,
  190. // and hierarchy_panel_docked_width should have been updated when resizing the
  191. // hierarchy panel
  192. SCH_SEARCH_DATA* searchData = dynamic_cast<SCH_SEARCH_DATA*>( m_findReplaceData.get() );
  193. if( searchData )
  194. {
  195. cfg->m_FindReplaceExtra.replace_references = searchData->replaceReferences;
  196. cfg->m_FindReplaceExtra.search_all_fields = searchData->searchAllFields;
  197. cfg->m_FindReplaceExtra.search_all_pins = searchData->searchAllPins;
  198. cfg->m_FindReplaceExtra.search_current_sheet_only =
  199. searchData->searchCurrentSheetOnly;
  200. cfg->m_FindReplaceExtra.search_selected_only = searchData->searchSelectedOnly;
  201. }
  202. wxAuiPaneInfo& searchPaneInfo = m_auimgr.GetPane( SearchPaneName() );
  203. m_show_search = searchPaneInfo.IsShown();
  204. cfg->m_AuiPanels.show_search = m_show_search;
  205. cfg->m_AuiPanels.search_panel_height = m_searchPane->GetSize().y;
  206. cfg->m_AuiPanels.search_panel_width = m_searchPane->GetSize().x;
  207. cfg->m_AuiPanels.search_panel_dock_direction = searchPaneInfo.dock_direction;
  208. wxAuiPaneInfo& propertiesPane = m_auimgr.GetPane( PropertiesPaneName() );
  209. cfg->m_AuiPanels.show_properties = propertiesPane.IsShown();
  210. cfg->m_AuiPanels.properties_splitter = m_propertiesPanel->SplitterProportion();
  211. cfg->m_AuiPanels.properties_panel_width = m_propertiesPanel->GetSize().x;
  212. wxAuiPaneInfo& netNavigatorPane = m_auimgr.GetPane( NetNavigatorPaneName() );
  213. cfg->m_AuiPanels.show_net_nav_panel = netNavigatorPane.IsShown();
  214. cfg->m_AuiPanels.float_net_nav_panel = netNavigatorPane.IsFloating();
  215. if( netNavigatorPane.IsDocked() )
  216. cfg->m_AuiPanels.net_nav_panel_docked_size = m_netNavigator->GetSize();
  217. else
  218. {
  219. cfg->m_AuiPanels.net_nav_panel_float_pos = netNavigatorPane.floating_pos;
  220. cfg->m_AuiPanels.net_nav_panel_float_size = netNavigatorPane.floating_size;
  221. }
  222. }
  223. }
  224. void SCH_BASE_FRAME::LoadSettings( APP_SETTINGS_BASE* aCfg )
  225. {
  226. wxCHECK_RET( aCfg, "Call to SCH_BASE_FRAME::LoadSettings with null settings" );
  227. EDA_DRAW_FRAME::LoadSettings( aCfg );
  228. if( aCfg->m_Window.grid.grids.empty() )
  229. aCfg->m_Window.grid.grids = aCfg->DefaultGridSizeList();
  230. // Move legacy user grids to grid list
  231. if( !aCfg->m_Window.grid.user_grid_x.empty() )
  232. {
  233. aCfg->m_Window.grid.grids.emplace_back( GRID{ "User Grid", aCfg->m_Window.grid.user_grid_x,
  234. aCfg->m_Window.grid.user_grid_y } );
  235. aCfg->m_Window.grid.user_grid_x = wxEmptyString;
  236. aCfg->m_Window.grid.user_grid_y = wxEmptyString;
  237. }
  238. if( aCfg->m_Window.grid.last_size_idx > (int) aCfg->m_Window.grid.grids.size() )
  239. aCfg->m_Window.grid.last_size_idx = 1;
  240. if( aCfg->m_Window.grid.fast_grid_1 > (int) aCfg->m_Window.grid.grids.size() )
  241. aCfg->m_Window.grid.fast_grid_1 = 1;
  242. if( aCfg->m_Window.grid.fast_grid_2 > (int) aCfg->m_Window.grid.grids.size() )
  243. aCfg->m_Window.grid.fast_grid_2 = 2;
  244. if( aCfg->m_Window.zoom_factors.empty() )
  245. {
  246. aCfg->m_Window.zoom_factors = { ZOOM_LIST_EESCHEMA };
  247. }
  248. }
  249. void SCH_BASE_FRAME::SaveSettings( APP_SETTINGS_BASE* aCfg )
  250. {
  251. wxCHECK_RET( aCfg, wxS( "Call to SCH_BASE_FRAME::SaveSettings with null settings" ) );
  252. EDA_DRAW_FRAME::SaveSettings( aCfg );
  253. }