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.

222 lines
6.7 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 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_i.h>
  26. #include <pcb_base_edit_frame.h>
  27. #include <tool/tool_manager.h>
  28. #include <pcbnew_settings.h>
  29. #include <pgm_base.h>
  30. #include <class_board.h>
  31. #include "footprint_info_impl.h"
  32. #include <project.h>
  33. #include <settings/color_settings.h>
  34. #include <settings/settings_manager.h>
  35. #include <tools/pcb_actions.h>
  36. #include <dialogs/dialog_grid_settings.h>
  37. #include <widgets/appearance_controls.h>
  38. #include <dialogs/eda_view_switcher.h>
  39. #include <layer_widget.h>
  40. #include <class_dimension.h>
  41. #include <wildcards_and_files_ext.h>
  42. PCB_BASE_EDIT_FRAME::PCB_BASE_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent,
  43. FRAME_T aFrameType, const wxString& aTitle,
  44. const wxPoint& aPos, const wxSize& aSize, long aStyle,
  45. const wxString& aFrameName ) :
  46. PCB_BASE_FRAME( aKiway, aParent, aFrameType, aTitle, aPos, aSize, aStyle, aFrameName ),
  47. m_rotationAngle( 900 ), m_undoRedoBlocked( false ),
  48. m_Layers( nullptr ),
  49. m_selectionFilterPanel( nullptr ),
  50. m_appearancePanel( nullptr )
  51. {
  52. if( !GFootprintList.GetCount() )
  53. {
  54. wxTextFile footprintInfoCache( Prj().GetProjectPath() + "fp-info-cache" );
  55. GFootprintList.ReadCacheFromFile( &footprintInfoCache );
  56. }
  57. }
  58. PCB_BASE_EDIT_FRAME::~PCB_BASE_EDIT_FRAME()
  59. {
  60. GetCanvas()->GetView()->Clear();
  61. }
  62. void PCB_BASE_EDIT_FRAME::doCloseWindow()
  63. {
  64. SETTINGS_MANAGER* mgr = GetSettingsManager();
  65. if( mgr->IsProjectOpen() && wxFileName::IsDirWritable( Prj().GetProjectPath() ) )
  66. {
  67. wxTextFile footprintInfoCache( Prj().GetProjectPath() + "fp-info-cache" );
  68. GFootprintList.WriteCacheToFile( &footprintInfoCache );
  69. }
  70. // Close the project if we are standalone, so it gets cleaned up properly
  71. if( mgr->IsProjectOpen() && Kiface().IsSingle() )
  72. mgr->UnloadProject( &Prj() );
  73. }
  74. bool PCB_BASE_EDIT_FRAME::TryBefore( wxEvent& aEvent )
  75. {
  76. static bool s_switcherShown = false;
  77. if( !s_switcherShown && wxGetKeyState( WXK_RAW_CONTROL ) && wxGetKeyState( WXK_TAB ) )
  78. {
  79. if( m_appearancePanel && this->IsActive() )
  80. {
  81. const wxArrayString& mru = m_appearancePanel->GetLayerPresetsMRU();
  82. EDA_VIEW_SWITCHER switcher( this, mru );
  83. s_switcherShown = true;
  84. switcher.ShowModal();
  85. s_switcherShown = false;
  86. int idx = switcher.GetSelection();
  87. if( idx >= 0 && idx < (int) mru.size() )
  88. m_appearancePanel->ApplyLayerPreset( mru[idx] );
  89. return true;
  90. }
  91. }
  92. return PCB_BASE_FRAME::TryBefore( aEvent );
  93. }
  94. void PCB_BASE_EDIT_FRAME::SetRotationAngle( int aRotationAngle )
  95. {
  96. wxCHECK2_MSG( aRotationAngle > 0 && aRotationAngle <= 900, aRotationAngle = 900,
  97. wxT( "Invalid rotation angle, defaulting to 90." ) );
  98. m_rotationAngle = aRotationAngle;
  99. }
  100. void PCB_BASE_EDIT_FRAME::ActivateGalCanvas()
  101. {
  102. PCB_BASE_FRAME::ActivateGalCanvas();
  103. GetCanvas()->SyncLayersVisibility( m_Pcb );
  104. }
  105. void PCB_BASE_EDIT_FRAME::SetBoard( BOARD* aBoard )
  106. {
  107. bool new_board = ( aBoard != m_Pcb );
  108. if( new_board )
  109. {
  110. if( m_toolManager )
  111. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  112. GetCanvas()->GetView()->Clear();
  113. GetCanvas()->GetView()->InitPreview();
  114. }
  115. PCB_BASE_FRAME::SetBoard( aBoard );
  116. GetCanvas()->GetGAL()->SetGridOrigin( VECTOR2D( aBoard->GetDesignSettings().m_GridOrigin ) );
  117. // update the tool manager with the new board and its view.
  118. if( m_toolManager )
  119. {
  120. GetCanvas()->DisplayBoard( aBoard );
  121. GetCanvas()->UpdateColors();
  122. m_toolManager->SetEnvironment( aBoard, GetCanvas()->GetView(),
  123. GetCanvas()->GetViewControls(), config(), this );
  124. if( new_board )
  125. {
  126. BOARD_DESIGN_SETTINGS& bds = aBoard->GetDesignSettings();
  127. bds.m_DRCEngine = std::make_shared<DRC_ENGINE>( aBoard, &bds );
  128. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  129. }
  130. }
  131. }
  132. void PCB_BASE_EDIT_FRAME::unitsChangeRefresh()
  133. {
  134. PCB_BASE_FRAME::unitsChangeRefresh();
  135. if( BOARD* board = GetBoard() )
  136. {
  137. EDA_UNITS units = GetUserUnits();
  138. KIGFX::VIEW* view = GetCanvas()->GetView();
  139. INSPECTOR_FUNC inspector =
  140. [units, view]( EDA_ITEM* aItem, void* aTestData )
  141. {
  142. DIMENSION* dimension = static_cast<DIMENSION*>( aItem );
  143. if( dimension->GetUnitsMode() == DIM_UNITS_MODE::AUTOMATIC )
  144. {
  145. dimension->SetUnits( units, false );
  146. dimension->Update();
  147. view->Update( dimension );
  148. }
  149. return SEARCH_RESULT::CONTINUE;
  150. };
  151. board->Visit( inspector, nullptr, GENERAL_COLLECTOR::Dimensions );
  152. }
  153. ReCreateAuxiliaryToolbar();
  154. }
  155. void PCB_BASE_EDIT_FRAME::SetGridVisibility( bool aVisible )
  156. {
  157. PCB_BASE_FRAME::SetGridVisibility( aVisible );
  158. // Update the grid checkbox in the layer widget
  159. if( m_appearancePanel )
  160. m_appearancePanel->SetObjectVisible( LAYER_GRID, aVisible );
  161. }
  162. COLOR_SETTINGS* PCB_BASE_EDIT_FRAME::GetColorSettings()
  163. {
  164. return Pgm().GetSettingsManager().GetColorSettings( GetPcbNewSettings()->m_ColorTheme );
  165. }
  166. wxString PCB_BASE_EDIT_FRAME::GetDesignRulesPath()
  167. {
  168. if( !GetBoard() )
  169. return wxEmptyString;
  170. wxFileName fn = GetBoard()->GetFileName();
  171. fn.SetExt( DesignRulesFileExtension );
  172. return Prj().AbsolutePath( fn.GetFullName() );
  173. }