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.

348 lines
10 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-2023 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 <kiplatform/ui.h>
  27. #include <pcb_base_edit_frame.h>
  28. #include <3d_viewer/eda_3d_viewer_frame.h>
  29. #include <tool/tool_manager.h>
  30. #include <tools/pcb_actions.h>
  31. #include <tools/pcb_selection_tool.h>
  32. #include <pgm_base.h>
  33. #include <board.h>
  34. #include <board_design_settings.h>
  35. #include <gal/graphics_abstraction_layer.h>
  36. #include <pcb_dimension.h>
  37. #include <footprint.h>
  38. #include <footprint_info_impl.h>
  39. #include <project.h>
  40. #include <settings/color_settings.h>
  41. #include <settings/settings_manager.h>
  42. #include <widgets/appearance_controls.h>
  43. #include <widgets/pcb_properties_panel.h>
  44. #include <dialogs/eda_view_switcher.h>
  45. #include <wildcards_and_files_ext.h>
  46. #include <widgets/wx_aui_utils.h>
  47. PCB_BASE_EDIT_FRAME::PCB_BASE_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent,
  48. FRAME_T aFrameType, const wxString& aTitle,
  49. const wxPoint& aPos, const wxSize& aSize, long aStyle,
  50. const wxString& aFrameName ) :
  51. PCB_BASE_FRAME( aKiway, aParent, aFrameType, aTitle, aPos, aSize, aStyle, aFrameName ),
  52. m_undoRedoBlocked( false ),
  53. m_selectionFilterPanel( nullptr ),
  54. m_appearancePanel( nullptr ),
  55. m_tabbedPanel( nullptr )
  56. {
  57. m_darkMode = KIPLATFORM::UI::IsDarkTheme();
  58. Bind( wxEVT_IDLE,
  59. [this]( wxIdleEvent& aEvent )
  60. {
  61. // Handle cursor adjustments. While we can get motion and key events through
  62. // wxWidgets, we can't get modifier-key-up events.
  63. if( m_toolManager )
  64. {
  65. PCB_SELECTION_TOOL* selTool = m_toolManager->GetTool<PCB_SELECTION_TOOL>();
  66. if( selTool )
  67. selTool->OnIdle( aEvent );
  68. }
  69. if( m_darkMode != KIPLATFORM::UI::IsDarkTheme() )
  70. {
  71. onDarkModeToggle();
  72. m_darkMode = KIPLATFORM::UI::IsDarkTheme();
  73. }
  74. } );
  75. }
  76. PCB_BASE_EDIT_FRAME::~PCB_BASE_EDIT_FRAME()
  77. {
  78. GetCanvas()->GetView()->Clear();
  79. }
  80. void PCB_BASE_EDIT_FRAME::doCloseWindow()
  81. {
  82. SETTINGS_MANAGER* mgr = GetSettingsManager();
  83. wxFileName projectName( Prj().GetProjectFullName() );
  84. if( mgr->IsProjectOpen() && wxFileName::IsDirWritable( projectName.GetPath() )
  85. && projectName.Exists() )
  86. {
  87. GFootprintList.WriteCacheToFile( Prj().GetProjectPath() + wxT( "fp-info-cache" ) );
  88. }
  89. // Close the project if we are standalone, so it gets cleaned up properly
  90. if( mgr->IsProjectOpen() && Kiface().IsSingle() )
  91. mgr->UnloadProject( &Prj(), false );
  92. }
  93. bool PCB_BASE_EDIT_FRAME::TryBefore( wxEvent& aEvent )
  94. {
  95. static bool s_presetSwitcherShown = false;
  96. static bool s_viewportSwitcherShown = false;
  97. // wxWidgets generates no key events for the tab key when the ctrl key is held down. One
  98. // way around this is to look at all events and inspect the keyboard state of the tab key.
  99. // However, this runs into issues on some linux VMs where querying the keyboard state is
  100. // very slow. Fortunately we only use ctrl-tab on Mac, so we implement this lovely hack:
  101. #ifdef __WXMAC__
  102. if( wxGetKeyState( WXK_TAB ) )
  103. #else
  104. if( ( aEvent.GetEventType() == wxEVT_CHAR || aEvent.GetEventType() == wxEVT_CHAR_HOOK )
  105. && static_cast<wxKeyEvent&>( aEvent ).GetKeyCode() == WXK_TAB )
  106. #endif
  107. {
  108. if( !s_presetSwitcherShown && wxGetKeyState( PRESET_SWITCH_KEY ) )
  109. {
  110. if( m_appearancePanel && this->IsActive() )
  111. {
  112. const wxArrayString& mru = m_appearancePanel->GetLayerPresetsMRU();
  113. if( mru.size() > 0 )
  114. {
  115. EDA_VIEW_SWITCHER switcher( this, mru, PRESET_SWITCH_KEY );
  116. s_presetSwitcherShown = true;
  117. switcher.ShowModal();
  118. s_presetSwitcherShown = false;
  119. int idx = switcher.GetSelection();
  120. if( idx >= 0 && idx < (int) mru.size() )
  121. m_appearancePanel->ApplyLayerPreset( mru[idx] );
  122. return true;
  123. }
  124. }
  125. }
  126. else if( !s_viewportSwitcherShown && wxGetKeyState( VIEWPORT_SWITCH_KEY ) )
  127. {
  128. if( m_appearancePanel && this->IsActive() )
  129. {
  130. const wxArrayString& mru = m_appearancePanel->GetViewportsMRU();
  131. if( mru.size() > 0 )
  132. {
  133. EDA_VIEW_SWITCHER switcher( this, mru, VIEWPORT_SWITCH_KEY );
  134. s_viewportSwitcherShown = true;
  135. switcher.ShowModal();
  136. s_viewportSwitcherShown = false;
  137. int idx = switcher.GetSelection();
  138. if( idx >= 0 && idx < (int) mru.size() )
  139. m_appearancePanel->ApplyViewport( mru[idx] );
  140. return true;
  141. }
  142. }
  143. }
  144. }
  145. return PCB_BASE_FRAME::TryBefore( aEvent );
  146. }
  147. EDA_ANGLE PCB_BASE_EDIT_FRAME::GetRotationAngle() const
  148. {
  149. // Return a default angle (90 degrees) used for rotate operations.
  150. return ANGLE_90;
  151. }
  152. void PCB_BASE_EDIT_FRAME::ActivateGalCanvas()
  153. {
  154. PCB_BASE_FRAME::ActivateGalCanvas();
  155. GetCanvas()->SyncLayersVisibility( m_pcb );
  156. }
  157. void PCB_BASE_EDIT_FRAME::SetBoard( BOARD* aBoard, PROGRESS_REPORTER* aReporter )
  158. {
  159. bool is_new_board = ( aBoard != m_pcb );
  160. if( is_new_board )
  161. {
  162. if( m_toolManager )
  163. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  164. GetCanvas()->GetView()->Clear();
  165. GetCanvas()->GetView()->InitPreview();
  166. }
  167. PCB_BASE_FRAME::SetBoard( aBoard, aReporter );
  168. GetCanvas()->GetGAL()->SetGridOrigin( VECTOR2D( aBoard->GetDesignSettings().GetGridOrigin() ) );
  169. if( is_new_board )
  170. {
  171. BOARD_DESIGN_SETTINGS& bds = aBoard->GetDesignSettings();
  172. bds.m_DRCEngine = std::make_shared<DRC_ENGINE>( aBoard, &bds );
  173. }
  174. // update the tool manager with the new board and its view.
  175. if( m_toolManager )
  176. {
  177. GetCanvas()->DisplayBoard( aBoard, aReporter );
  178. GetCanvas()->UpdateColors();
  179. m_toolManager->SetEnvironment( aBoard, GetCanvas()->GetView(),
  180. GetCanvas()->GetViewControls(), config(), this );
  181. if( is_new_board )
  182. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  183. }
  184. }
  185. void PCB_BASE_EDIT_FRAME::unitsChangeRefresh()
  186. {
  187. PCB_BASE_FRAME::unitsChangeRefresh();
  188. if( BOARD* board = GetBoard() )
  189. {
  190. board->UpdateUserUnits( board, GetCanvas()->GetView() );
  191. m_toolManager->PostEvent( EVENTS::SelectedItemsModified );
  192. }
  193. ReCreateAuxiliaryToolbar();
  194. UpdateProperties();
  195. }
  196. void PCB_BASE_EDIT_FRAME::SetGridVisibility( bool aVisible )
  197. {
  198. PCB_BASE_FRAME::SetGridVisibility( aVisible );
  199. // Update the grid checkbox in the layer widget
  200. if( m_appearancePanel )
  201. m_appearancePanel->SetObjectVisible( LAYER_GRID, aVisible );
  202. }
  203. void PCB_BASE_EDIT_FRAME::SetObjectVisible( GAL_LAYER_ID aLayer, bool aVisible )
  204. {
  205. if( m_appearancePanel )
  206. m_appearancePanel->SetObjectVisible( aLayer, aVisible );
  207. }
  208. COLOR_SETTINGS* PCB_BASE_EDIT_FRAME::GetColorSettings( bool aForceRefresh ) const
  209. {
  210. return Pgm().GetSettingsManager().GetColorSettings( GetPcbNewSettings()->m_ColorTheme );
  211. }
  212. wxString PCB_BASE_EDIT_FRAME::GetDesignRulesPath()
  213. {
  214. if( !GetBoard() )
  215. return wxEmptyString;
  216. wxFileName fn = GetBoard()->GetFileName();
  217. fn.SetExt( DesignRulesFileExtension );
  218. return Prj().AbsolutePath( fn.GetFullName() );
  219. }
  220. void PCB_BASE_EDIT_FRAME::handleActivateEvent( wxActivateEvent& aEvent )
  221. {
  222. PCB_BASE_FRAME::handleActivateEvent( aEvent );
  223. // The text in the collapsible pane headers need to be updated
  224. if( m_appearancePanel )
  225. m_appearancePanel->RefreshCollapsiblePanes();
  226. }
  227. void PCB_BASE_EDIT_FRAME::onDarkModeToggle()
  228. {
  229. m_appearancePanel->OnDarkModeToggle();
  230. EDA_3D_VIEWER_FRAME* viewer = Get3DViewerFrame();
  231. if( viewer )
  232. viewer->OnDarkModeToggle();
  233. }
  234. void PCB_BASE_EDIT_FRAME::ToggleProperties()
  235. {
  236. if( !m_propertiesPanel )
  237. return;
  238. bool show = !m_propertiesPanel->IsShownOnScreen();
  239. wxAuiPaneInfo& propertiesPaneInfo = m_auimgr.GetPane( PropertiesPaneName() );
  240. propertiesPaneInfo.Show( show );
  241. PCBNEW_SETTINGS* settings = GetPcbNewSettings();
  242. if( show )
  243. {
  244. SetAuiPaneSize( m_auimgr, propertiesPaneInfo,
  245. settings->m_AuiPanels.properties_panel_width, -1 );
  246. }
  247. else
  248. {
  249. settings->m_AuiPanels.properties_panel_width = m_propertiesPanel->GetSize().x;
  250. m_auimgr.Update();
  251. }
  252. }
  253. void PCB_BASE_EDIT_FRAME::GetContextualTextVars( BOARD_ITEM* aSourceItem, const wxString& aCrossRef,
  254. wxArrayString* aTokens )
  255. {
  256. BOARD* board = aSourceItem->GetBoard();
  257. if( !aCrossRef.IsEmpty() )
  258. {
  259. for( FOOTPRINT* candidate : board->Footprints() )
  260. {
  261. if( candidate->GetReference() == aCrossRef )
  262. {
  263. candidate->GetContextualTextVars( aTokens );
  264. break;
  265. }
  266. }
  267. }
  268. else
  269. {
  270. board->GetContextualTextVars( aTokens );
  271. if( FOOTPRINT* footprint = aSourceItem->GetParentFootprint() )
  272. footprint->GetContextualTextVars( aTokens );
  273. }
  274. }