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.

392 lines
15 KiB

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