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.

296 lines
9.0 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014 CERN
  5. * Copyright (C) 2020-2022 KiCad Developers, see AUTHORS.txt for contributors.
  6. * @author Maciej Suminski <maciej.suminski@cern.ch>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <kiface_base.h>
  26. #include <pcb_base_edit_frame.h>
  27. #include <tool/tool_manager.h>
  28. #include <tools/pcb_actions.h>
  29. #include <tools/pcb_selection_tool.h>
  30. #include <pcbnew_settings.h>
  31. #include <pgm_base.h>
  32. #include <board.h>
  33. #include <board_design_settings.h>
  34. #include <pcb_dimension.h>
  35. #include <footprint_info_impl.h>
  36. #include <project.h>
  37. #include <settings/color_settings.h>
  38. #include <settings/settings_manager.h>
  39. #include <widgets/appearance_controls.h>
  40. #include <dialogs/eda_view_switcher.h>
  41. #include <wildcards_and_files_ext.h>
  42. #include <collectors.h>
  43. PCB_BASE_EDIT_FRAME::PCB_BASE_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent,
  44. FRAME_T aFrameType, const wxString& aTitle,
  45. const wxPoint& aPos, const wxSize& aSize, long aStyle,
  46. const wxString& aFrameName ) :
  47. PCB_BASE_FRAME( aKiway, aParent, aFrameType, aTitle, aPos, aSize, aStyle, aFrameName ),
  48. m_undoRedoBlocked( false ),
  49. m_selectionFilterPanel( nullptr ),
  50. m_appearancePanel( nullptr )
  51. {
  52. Bind( wxEVT_IDLE,
  53. [this]( wxIdleEvent& aEvent )
  54. {
  55. // Handle cursor adjustments. While we can get motion and key events through
  56. // wxWidgets, we can't get modifier-key-up events.
  57. if( m_toolManager )
  58. {
  59. PCB_SELECTION_TOOL* selTool = m_toolManager->GetTool<PCB_SELECTION_TOOL>();
  60. if( selTool )
  61. selTool->OnIdle( aEvent );
  62. }
  63. } );
  64. }
  65. PCB_BASE_EDIT_FRAME::~PCB_BASE_EDIT_FRAME()
  66. {
  67. GetCanvas()->GetView()->Clear();
  68. }
  69. void PCB_BASE_EDIT_FRAME::doCloseWindow()
  70. {
  71. SETTINGS_MANAGER* mgr = GetSettingsManager();
  72. wxFileName projectName( Prj().GetProjectFullName() );
  73. if( mgr->IsProjectOpen() && wxFileName::IsDirWritable( projectName.GetPath() )
  74. && projectName.Exists() )
  75. {
  76. GFootprintList.WriteCacheToFile( Prj().GetProjectPath() + wxT( "fp-info-cache" ) );
  77. }
  78. // Close the project if we are standalone, so it gets cleaned up properly
  79. if( mgr->IsProjectOpen() && Kiface().IsSingle() )
  80. mgr->UnloadProject( &Prj(), false );
  81. }
  82. bool PCB_BASE_EDIT_FRAME::TryBefore( wxEvent& aEvent )
  83. {
  84. static bool s_presetSwitcherShown = false;
  85. static bool s_viewportSwitcherShown = false;
  86. #ifdef __WXMAC__
  87. wxKeyCode presetSwitchKey = WXK_RAW_CONTROL;
  88. wxKeyCode viewSwitchKey = WXK_ALT;
  89. #else
  90. wxKeyCode presetSwitchKey = WXK_RAW_CONTROL;
  91. wxKeyCode viewSwitchKey = WXK_WINDOWS_LEFT;
  92. #endif
  93. if( aEvent.GetEventType() != wxEVT_CHAR && aEvent.GetEventType() != wxEVT_CHAR_HOOK )
  94. return PCB_BASE_FRAME::TryBefore( aEvent );
  95. if( !s_presetSwitcherShown && wxGetKeyState( presetSwitchKey ) && wxGetKeyState( WXK_TAB ) )
  96. {
  97. if( m_appearancePanel && this->IsActive() )
  98. {
  99. const wxArrayString& mru = m_appearancePanel->GetLayerPresetsMRU();
  100. if( mru.size() > 0 )
  101. {
  102. EDA_VIEW_SWITCHER switcher( this, mru, presetSwitchKey );
  103. s_presetSwitcherShown = true;
  104. switcher.ShowModal();
  105. s_presetSwitcherShown = false;
  106. int idx = switcher.GetSelection();
  107. if( idx >= 0 && idx < (int) mru.size() )
  108. m_appearancePanel->ApplyLayerPreset( mru[idx] );
  109. return true;
  110. }
  111. }
  112. }
  113. else if( !s_viewportSwitcherShown && wxGetKeyState( viewSwitchKey ) && wxGetKeyState( WXK_TAB ) )
  114. {
  115. if( m_appearancePanel && this->IsActive() )
  116. {
  117. const wxArrayString& mru = m_appearancePanel->GetViewportsMRU();
  118. if( mru.size() > 0 )
  119. {
  120. EDA_VIEW_SWITCHER switcher( this, mru, viewSwitchKey );
  121. s_viewportSwitcherShown = true;
  122. switcher.ShowModal();
  123. s_viewportSwitcherShown = false;
  124. int idx = switcher.GetSelection();
  125. if( idx >= 0 && idx < (int) mru.size() )
  126. m_appearancePanel->ApplyViewport( mru[idx] );
  127. return true;
  128. }
  129. }
  130. }
  131. return PCB_BASE_FRAME::TryBefore( aEvent );
  132. }
  133. EDA_ANGLE PCB_BASE_EDIT_FRAME::GetRotationAngle() const
  134. {
  135. // Return a default angle (90 degrees) used for rotate operations.
  136. return ANGLE_90;
  137. }
  138. void PCB_BASE_EDIT_FRAME::ActivateGalCanvas()
  139. {
  140. PCB_BASE_FRAME::ActivateGalCanvas();
  141. GetCanvas()->SyncLayersVisibility( m_pcb );
  142. }
  143. void PCB_BASE_EDIT_FRAME::SetBoard( BOARD* aBoard, PROGRESS_REPORTER* aReporter )
  144. {
  145. bool new_board = ( aBoard != m_pcb );
  146. if( new_board )
  147. {
  148. if( m_toolManager )
  149. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  150. GetCanvas()->GetView()->Clear();
  151. GetCanvas()->GetView()->InitPreview();
  152. }
  153. PCB_BASE_FRAME::SetBoard( aBoard, aReporter );
  154. GetCanvas()->GetGAL()->SetGridOrigin( VECTOR2D( aBoard->GetDesignSettings().GetGridOrigin() ) );
  155. if( new_board )
  156. {
  157. BOARD_DESIGN_SETTINGS& bds = aBoard->GetDesignSettings();
  158. bds.m_DRCEngine = std::make_shared<DRC_ENGINE>( aBoard, &bds );
  159. }
  160. // update the tool manager with the new board and its view.
  161. if( m_toolManager )
  162. {
  163. GetCanvas()->DisplayBoard( aBoard, aReporter );
  164. GetCanvas()->UpdateColors();
  165. m_toolManager->SetEnvironment( aBoard, GetCanvas()->GetView(),
  166. GetCanvas()->GetViewControls(), config(), this );
  167. if( new_board )
  168. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  169. }
  170. }
  171. void PCB_BASE_EDIT_FRAME::unitsChangeRefresh()
  172. {
  173. PCB_BASE_FRAME::unitsChangeRefresh();
  174. if( BOARD* board = GetBoard() )
  175. {
  176. EDA_UNITS units = GetUserUnits();
  177. KIGFX::VIEW* view = GetCanvas()->GetView();
  178. bool selectedItemModified = false;
  179. INSPECTOR_FUNC inspector =
  180. [units, view, &selectedItemModified]( EDA_ITEM* aItem, void* aTestData )
  181. {
  182. PCB_DIMENSION_BASE* dimension = static_cast<PCB_DIMENSION_BASE*>( aItem );
  183. if( dimension->GetUnitsMode() == DIM_UNITS_MODE::AUTOMATIC )
  184. {
  185. dimension->SetUnits( units );
  186. dimension->Update();
  187. if( dimension->IsSelected() )
  188. selectedItemModified = true;
  189. view->Update( dimension );
  190. }
  191. return SEARCH_RESULT::CONTINUE;
  192. };
  193. board->Visit( inspector, nullptr, GENERAL_COLLECTOR::Dimensions );
  194. if( selectedItemModified )
  195. m_toolManager->PostEvent( EVENTS::SelectedItemsModified );
  196. }
  197. ReCreateAuxiliaryToolbar();
  198. }
  199. void PCB_BASE_EDIT_FRAME::SetGridVisibility( bool aVisible )
  200. {
  201. PCB_BASE_FRAME::SetGridVisibility( aVisible );
  202. // Update the grid checkbox in the layer widget
  203. if( m_appearancePanel )
  204. m_appearancePanel->SetObjectVisible( LAYER_GRID, aVisible );
  205. }
  206. void PCB_BASE_EDIT_FRAME::SetObjectVisible( GAL_LAYER_ID aLayer, bool aVisible )
  207. {
  208. if( m_appearancePanel )
  209. m_appearancePanel->SetObjectVisible( aLayer, aVisible );
  210. }
  211. COLOR_SETTINGS* PCB_BASE_EDIT_FRAME::GetColorSettings( bool aForceRefresh ) const
  212. {
  213. return Pgm().GetSettingsManager().GetColorSettings( GetPcbNewSettings()->m_ColorTheme );
  214. }
  215. wxString PCB_BASE_EDIT_FRAME::GetDesignRulesPath()
  216. {
  217. if( !GetBoard() )
  218. return wxEmptyString;
  219. wxFileName fn = GetBoard()->GetFileName();
  220. fn.SetExt( DesignRulesFileExtension );
  221. return Prj().AbsolutePath( fn.GetFullName() );
  222. }
  223. void PCB_BASE_EDIT_FRAME::handleActivateEvent( wxActivateEvent& aEvent )
  224. {
  225. PCB_BASE_FRAME::handleActivateEvent( aEvent );
  226. // The text in the collapsible pane headers need to be updated
  227. if( m_appearancePanel )
  228. m_appearancePanel->RefreshCollapsiblePanes();
  229. }