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.

243 lines
9.7 KiB

18 years ago
14 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  6. * Copyright (C) 2012 Wayne Stambaugh <stambaughw@gmail.com>
  7. * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. * or you may search the http://www.gnu.org website for the version 2 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. #include <pcb_edit_frame.h>
  27. #include <tool/tool_manager.h>
  28. #include <tools/pcb_selection_tool.h>
  29. #include <board_design_settings.h>
  30. #include <drawing_sheet/ds_data_model.h>
  31. #include <pcbplot.h>
  32. #include <pcb_painter.h>
  33. #include <project.h>
  34. #include <widgets/appearance_controls.h>
  35. #include <widgets/panel_selection_filter.h>
  36. #include <project/net_settings.h>
  37. #include <project/project_file.h>
  38. #include <project/project_local_settings.h>
  39. bool PCB_EDIT_FRAME::LoadProjectSettings()
  40. {
  41. PROJECT_FILE& project = Prj().GetProjectFile();
  42. PROJECT_LOCAL_SETTINGS& localSettings = Prj().GetLocalSettings();
  43. BASE_SCREEN::m_DrawingSheetFileName = project.m_BoardDrawingSheetFile;
  44. // Load the drawing sheet from the filename stored in BASE_SCREEN::m_DrawingSheetFileName.
  45. // If empty, or not existing, the default drawing sheet is loaded.
  46. wxString filename = DS_DATA_MODEL::ResolvePath( BASE_SCREEN::m_DrawingSheetFileName,
  47. Prj().GetProjectPath());
  48. if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename ) )
  49. ShowInfoBarError( _( "Error loading drawing sheet." ), true );
  50. // Load render settings that aren't stored in PCB_DISPLAY_OPTIONS
  51. std::shared_ptr<NET_SETTINGS>& netSettings = project.NetSettings();
  52. KIGFX::RENDER_SETTINGS* rs = GetCanvas()->GetView()->GetPainter()->GetSettings();
  53. KIGFX::PCB_RENDER_SETTINGS* renderSettings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( rs );
  54. const NETINFO_LIST& nets = GetBoard()->GetNetInfo();
  55. std::set<int>& hiddenNets = renderSettings->GetHiddenNets();
  56. hiddenNets.clear();
  57. for( const wxString& hidden : localSettings.m_HiddenNets )
  58. {
  59. if( NETINFO_ITEM* net = nets.GetNetItem( hidden ) )
  60. hiddenNets.insert( net->GetNetCode() );
  61. }
  62. for( NETINFO_ITEM* net : nets )
  63. {
  64. if( localSettings.m_HiddenNetclasses.count( net->GetNetClass()->GetName() ) )
  65. hiddenNets.insert( net->GetNetCode() );
  66. }
  67. std::map<int, KIGFX::COLOR4D>& netColors = renderSettings->GetNetColorMap();
  68. netColors.clear();
  69. for( const auto& [ netname, color ] : netSettings->m_NetColorAssignments )
  70. {
  71. if( color != COLOR4D::UNSPECIFIED )
  72. {
  73. if( NETINFO_ITEM* net = GetBoard()->GetNetInfo().GetNetItem( netname ) )
  74. netColors[ net->GetNetCode() ] = color;
  75. }
  76. }
  77. std::map<wxString, KIGFX::COLOR4D>& netclassColors = renderSettings->GetNetclassColorMap();
  78. netclassColors.clear();
  79. for( const auto& [ name, netclass ] : netSettings->m_NetClasses )
  80. netclassColors[ name ] = netclass->GetPcbColor();
  81. m_appearancePanel->SetUserLayerPresets( project.m_LayerPresets );
  82. m_appearancePanel->SetUserViewports( project.m_Viewports );
  83. PCB_SELECTION_TOOL* selTool = GetToolManager()->GetTool<PCB_SELECTION_TOOL>();
  84. SELECTION_FILTER_OPTIONS& filterOpts = selTool->GetFilter();
  85. filterOpts = localSettings.m_SelectionFilter;
  86. m_selectionFilterPanel->SetCheckboxesFromFilter( filterOpts );
  87. PCB_DISPLAY_OPTIONS opts = GetDisplayOptions();
  88. opts.m_ContrastModeDisplay = localSettings.m_ContrastModeDisplay;
  89. opts.m_NetColorMode = localSettings.m_NetColorMode;
  90. opts.m_TrackOpacity = localSettings.m_TrackOpacity;
  91. opts.m_ViaOpacity = localSettings.m_ViaOpacity;
  92. opts.m_PadOpacity = localSettings.m_PadOpacity;
  93. opts.m_ZoneOpacity = localSettings.m_ZoneOpacity;
  94. opts.m_ZoneDisplayMode = localSettings.m_ZoneDisplayMode;
  95. opts.m_ImageOpacity = localSettings.m_ImageOpacity;
  96. // No refresh here: callers of LoadProjectSettings refresh later
  97. SetDisplayOptions( opts, false );
  98. BOARD_DESIGN_SETTINGS& bds = GetDesignSettings();
  99. bds.m_UseConnectedTrackWidth = localSettings.m_AutoTrackWidth;
  100. wxFileName fn( GetCurrentFileName() );
  101. fn.MakeRelativeTo( Prj().GetProjectPath() );
  102. LoadWindowState( fn.GetFullPath() );
  103. return true;
  104. }
  105. void PCB_EDIT_FRAME::SaveProjectLocalSettings()
  106. {
  107. wxFileName fn = Prj().GetProjectFullName();
  108. // Check for the filename before checking IsWritable as this
  109. // will throw errors on bad names. Here, we just want to not
  110. // save the Settings if we don't have a name
  111. if( !fn.IsOk() )
  112. return;
  113. if( !fn.IsDirWritable() )
  114. return;
  115. PROJECT_FILE& project = Prj().GetProjectFile();
  116. // TODO: Can this be pulled out of BASE_SCREEN?
  117. project.m_BoardDrawingSheetFile = BASE_SCREEN::m_DrawingSheetFileName;
  118. project.m_LayerPresets = m_appearancePanel->GetUserLayerPresets();
  119. project.m_Viewports = m_appearancePanel->GetUserViewports();
  120. RecordDRCExclusions();
  121. // Save render settings that aren't stored in PCB_DISPLAY_OPTIONS
  122. std::shared_ptr<NET_SETTINGS>& netSettings = project.NetSettings();
  123. const NETINFO_LIST& nets = GetBoard()->GetNetInfo();
  124. KIGFX::RENDER_SETTINGS* rs = GetCanvas()->GetView()->GetPainter()->GetSettings();
  125. KIGFX::PCB_RENDER_SETTINGS* renderSettings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( rs );
  126. netSettings->m_NetColorAssignments.clear();
  127. for( const auto& [ netcode, color ] : renderSettings->GetNetColorMap() )
  128. {
  129. if( NETINFO_ITEM* net = nets.GetNetItem( netcode ) )
  130. netSettings->m_NetColorAssignments[ net->GetNetname() ] = color;
  131. }
  132. std::map<wxString, KIGFX::COLOR4D>& netclassColors = renderSettings->GetNetclassColorMap();
  133. // NOTE: this assumes netclasses will have already been updated, which I think is the case
  134. for( const auto& [ name, netclass ] : netSettings->m_NetClasses )
  135. {
  136. if( netclassColors.count( name ) )
  137. netclass->SetPcbColor( netclassColors.at( name ) );
  138. }
  139. /**
  140. * The below automatically saves the project on exit, which is what we want to do if the project
  141. * already exists. If the project doesn't already exist, we don't want to create it through
  142. * this function call, because this will happen automatically when the user exits even if they
  143. * didn't save a new board with a valid filename (usually an imported board).
  144. *
  145. * The explicit save action in PCB_EDIT_FRAME::SavePcbFile will call SaveProject directly,
  146. * so if the user does choose to save the board, the project file will get created then.
  147. */
  148. if( !Prj().IsNullProject() && fn.Exists() )
  149. GetSettingsManager()->SaveProject();
  150. }
  151. void PCB_EDIT_FRAME::saveProjectSettings()
  152. {
  153. wxFileName fn = Prj().GetProjectFullName();
  154. // Check for the filename before checking IsWritable as this
  155. // will throw errors on bad names. Here, we just want to not
  156. // save the Settings if we don't have a name
  157. if( !fn.IsOk() )
  158. return;
  159. if( !fn.IsDirWritable() )
  160. return;
  161. PROJECT_LOCAL_SETTINGS& localSettings = Prj().GetLocalSettings();
  162. // Save appearance control settings
  163. localSettings.m_ActiveLayer = GetActiveLayer();
  164. localSettings.m_ActiveLayerPreset = m_appearancePanel->GetActiveLayerPreset();
  165. const PCB_DISPLAY_OPTIONS& displayOpts = GetDisplayOptions();
  166. localSettings.m_ContrastModeDisplay = displayOpts.m_ContrastModeDisplay;
  167. localSettings.m_NetColorMode = displayOpts.m_NetColorMode;
  168. localSettings.m_TrackOpacity = displayOpts.m_TrackOpacity;
  169. localSettings.m_ViaOpacity = displayOpts.m_ViaOpacity;
  170. localSettings.m_PadOpacity = displayOpts.m_PadOpacity;
  171. localSettings.m_ZoneOpacity = displayOpts.m_ZoneOpacity;
  172. localSettings.m_ZoneDisplayMode = displayOpts.m_ZoneDisplayMode;
  173. localSettings.m_ImageOpacity = displayOpts.m_ImageOpacity;
  174. // Save Design settings
  175. const BOARD_DESIGN_SETTINGS& bds = GetDesignSettings();
  176. localSettings.m_AutoTrackWidth = bds.m_UseConnectedTrackWidth;
  177. // Net display settings
  178. const NETINFO_LIST& nets = GetBoard()->GetNetInfo();
  179. KIGFX::RENDER_SETTINGS* rs = GetCanvas()->GetView()->GetPainter()->GetSettings();
  180. KIGFX::PCB_RENDER_SETTINGS* renderSettings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( rs );
  181. localSettings.m_HiddenNets.clear();
  182. for( int netcode : renderSettings->GetHiddenNets() )
  183. {
  184. if( NETINFO_ITEM* net = nets.GetNetItem( netcode ) )
  185. localSettings.m_HiddenNets.emplace_back( net->GetNetname() );
  186. }
  187. PCB_SELECTION_TOOL* selTool = GetToolManager()->GetTool<PCB_SELECTION_TOOL>();
  188. SELECTION_FILTER_OPTIONS& filterOpts = selTool->GetFilter();
  189. localSettings.m_SelectionFilter = filterOpts;
  190. }