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.

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