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.

257 lines
7.8 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-2021 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/dialog_grid_settings.h>
  41. #include <dialogs/eda_view_switcher.h>
  42. #include <wildcards_and_files_ext.h>
  43. #include <collectors.h>
  44. PCB_BASE_EDIT_FRAME::PCB_BASE_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent,
  45. FRAME_T aFrameType, const wxString& aTitle,
  46. const wxPoint& aPos, const wxSize& aSize, long aStyle,
  47. const wxString& aFrameName ) :
  48. PCB_BASE_FRAME( aKiway, aParent, aFrameType, aTitle, aPos, aSize, aStyle, aFrameName ),
  49. m_rotationAngle( 900 ), m_undoRedoBlocked( false ),
  50. m_selectionFilterPanel( nullptr ),
  51. m_appearancePanel( nullptr )
  52. {
  53. Bind( wxEVT_IDLE,
  54. [this]( wxIdleEvent& aEvent )
  55. {
  56. // Handle cursor adjustments. While we can get motion and key events through
  57. // wxWidgets, we can't get modifier-key-up events.
  58. if( m_toolManager )
  59. {
  60. PCB_SELECTION_TOOL* selTool = m_toolManager->GetTool<PCB_SELECTION_TOOL>();
  61. if( selTool )
  62. selTool->OnIdle( aEvent );
  63. }
  64. } );
  65. }
  66. PCB_BASE_EDIT_FRAME::~PCB_BASE_EDIT_FRAME()
  67. {
  68. GetCanvas()->GetView()->Clear();
  69. }
  70. void PCB_BASE_EDIT_FRAME::doCloseWindow()
  71. {
  72. SETTINGS_MANAGER* mgr = GetSettingsManager();
  73. wxFileName projectName( Prj().GetProjectFullName() );
  74. if( mgr->IsProjectOpen() && wxFileName::IsDirWritable( projectName.GetPath() )
  75. && projectName.Exists() )
  76. {
  77. GFootprintList.WriteCacheToFile( Prj().GetProjectPath() + "fp-info-cache" );
  78. }
  79. // Close the project if we are standalone, so it gets cleaned up properly
  80. if( mgr->IsProjectOpen() && Kiface().IsSingle() )
  81. mgr->UnloadProject( &Prj(), false );
  82. }
  83. bool PCB_BASE_EDIT_FRAME::TryBefore( wxEvent& aEvent )
  84. {
  85. static bool s_switcherShown = false;
  86. if( !s_switcherShown && wxGetKeyState( WXK_RAW_CONTROL ) && wxGetKeyState( WXK_TAB ) )
  87. {
  88. if( m_appearancePanel && this->IsActive() )
  89. {
  90. const wxArrayString& mru = m_appearancePanel->GetLayerPresetsMRU();
  91. EDA_VIEW_SWITCHER switcher( this, mru );
  92. s_switcherShown = true;
  93. switcher.ShowModal();
  94. s_switcherShown = false;
  95. int idx = switcher.GetSelection();
  96. if( idx >= 0 && idx < (int) mru.size() )
  97. m_appearancePanel->ApplyLayerPreset( mru[idx] );
  98. return true;
  99. }
  100. }
  101. return PCB_BASE_FRAME::TryBefore( aEvent );
  102. }
  103. void PCB_BASE_EDIT_FRAME::SetRotationAngle( int aRotationAngle )
  104. {
  105. wxCHECK2_MSG( aRotationAngle > 0 && aRotationAngle <= 900, aRotationAngle = 900,
  106. wxT( "Invalid rotation angle, defaulting to 90." ) );
  107. m_rotationAngle = aRotationAngle;
  108. }
  109. void PCB_BASE_EDIT_FRAME::ActivateGalCanvas()
  110. {
  111. PCB_BASE_FRAME::ActivateGalCanvas();
  112. GetCanvas()->SyncLayersVisibility( m_pcb );
  113. }
  114. void PCB_BASE_EDIT_FRAME::SetBoard( BOARD* aBoard, PROGRESS_REPORTER* aReporter )
  115. {
  116. bool new_board = ( aBoard != m_pcb );
  117. if( new_board )
  118. {
  119. if( m_toolManager )
  120. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  121. GetCanvas()->GetView()->Clear();
  122. GetCanvas()->GetView()->InitPreview();
  123. }
  124. PCB_BASE_FRAME::SetBoard( aBoard, aReporter );
  125. GetCanvas()->GetGAL()->SetGridOrigin( VECTOR2D( aBoard->GetDesignSettings().GetGridOrigin() ) );
  126. if( new_board )
  127. {
  128. BOARD_DESIGN_SETTINGS& bds = aBoard->GetDesignSettings();
  129. bds.m_DRCEngine = std::make_shared<DRC_ENGINE>( aBoard, &bds );
  130. }
  131. // update the tool manager with the new board and its view.
  132. if( m_toolManager )
  133. {
  134. GetCanvas()->DisplayBoard( aBoard, aReporter );
  135. GetCanvas()->UpdateColors();
  136. m_toolManager->SetEnvironment( aBoard, GetCanvas()->GetView(),
  137. GetCanvas()->GetViewControls(), config(), this );
  138. if( new_board )
  139. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  140. }
  141. }
  142. void PCB_BASE_EDIT_FRAME::unitsChangeRefresh()
  143. {
  144. PCB_BASE_FRAME::unitsChangeRefresh();
  145. if( BOARD* board = GetBoard() )
  146. {
  147. EDA_UNITS units = GetUserUnits();
  148. KIGFX::VIEW* view = GetCanvas()->GetView();
  149. INSPECTOR_FUNC inspector =
  150. [units, view]( EDA_ITEM* aItem, void* aTestData )
  151. {
  152. PCB_DIMENSION_BASE* dimension = static_cast<PCB_DIMENSION_BASE*>( aItem );
  153. if( dimension->GetUnitsMode() == DIM_UNITS_MODE::AUTOMATIC )
  154. {
  155. dimension->SetUnits( units );
  156. dimension->Update();
  157. view->Update( dimension );
  158. }
  159. return SEARCH_RESULT::CONTINUE;
  160. };
  161. board->Visit( inspector, nullptr, GENERAL_COLLECTOR::Dimensions );
  162. }
  163. ReCreateAuxiliaryToolbar();
  164. }
  165. void PCB_BASE_EDIT_FRAME::SetGridVisibility( bool aVisible )
  166. {
  167. PCB_BASE_FRAME::SetGridVisibility( aVisible );
  168. // Update the grid checkbox in the layer widget
  169. if( m_appearancePanel )
  170. m_appearancePanel->SetObjectVisible( LAYER_GRID, aVisible );
  171. }
  172. void PCB_BASE_EDIT_FRAME::SetObjectVisible( GAL_LAYER_ID aLayer, bool aVisible )
  173. {
  174. if( m_appearancePanel )
  175. m_appearancePanel->SetObjectVisible( aLayer, aVisible );
  176. }
  177. COLOR_SETTINGS* PCB_BASE_EDIT_FRAME::GetColorSettings() const
  178. {
  179. return Pgm().GetSettingsManager().GetColorSettings( GetPcbNewSettings()->m_ColorTheme );
  180. }
  181. wxString PCB_BASE_EDIT_FRAME::GetDesignRulesPath()
  182. {
  183. if( !GetBoard() )
  184. return wxEmptyString;
  185. wxFileName fn = GetBoard()->GetFileName();
  186. fn.SetExt( DesignRulesFileExtension );
  187. return Prj().AbsolutePath( fn.GetFullName() );
  188. }
  189. void PCB_BASE_EDIT_FRAME::handleActivateEvent( wxActivateEvent& aEvent )
  190. {
  191. EDA_DRAW_FRAME::handleActivateEvent( aEvent );
  192. // The text in the collapsible pane headers need to be updated
  193. if( m_appearancePanel )
  194. m_appearancePanel->RefreshCollapsiblePanes();
  195. }
  196. void PCB_BASE_EDIT_FRAME::OnLayerAlphaChanged()
  197. {
  198. if( m_appearancePanel )
  199. m_appearancePanel->OnLayerAlphaChanged();
  200. }