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.

1387 lines
46 KiB

5 years ago
5 years ago
12 years ago
5 years ago
12 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 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) 2015 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  6. * Copyright (C) 2015-2016 Wayne Stambaugh <stambaughw@gmail.com>
  7. * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
  8. *
  9. * This program is free software: you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation, either version 3 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include "tools/convert_tool.h"
  23. #include "tools/drawing_tool.h"
  24. #include "tools/edit_tool.h"
  25. #include "tools/pcb_edit_table_tool.h"
  26. #include "tools/footprint_editor_control.h"
  27. #include "tools/pad_tool.h"
  28. #include "tools/pcb_actions.h"
  29. #include "tools/pcb_control.h"
  30. #include "tools/pcb_picker_tool.h"
  31. #include "tools/placement_tool.h"
  32. #include "tools/pcb_point_editor.h"
  33. #include "tools/pcb_selection_tool.h"
  34. #include <python/scripting/pcb_scripting_tool.h>
  35. #include <3d_viewer/eda_3d_viewer_frame.h>
  36. #include <bitmaps.h>
  37. #include <board.h>
  38. #include <footprint.h>
  39. #include <confirm.h>
  40. #include <footprint_edit_frame.h>
  41. #include <footprint_editor_settings.h>
  42. #include <footprint_info_impl.h>
  43. #include <footprint_tree_pane.h>
  44. #include <fp_lib_table.h>
  45. #include <gal/graphics_abstraction_layer.h>
  46. #include <kiface_base.h>
  47. #include <kiplatform/app.h>
  48. #include <kiway.h>
  49. #include <macros.h>
  50. #include <pcb_draw_panel_gal.h>
  51. #include <pcb_edit_frame.h>
  52. #include <pcbnew_id.h>
  53. #include <pgm_base.h>
  54. #include <project.h>
  55. #include <project_pcb.h>
  56. #include <settings/settings_manager.h>
  57. #include <tool/action_toolbar.h>
  58. #include <tool/common_control.h>
  59. #include <tool/common_tools.h>
  60. #include <tool/properties_tool.h>
  61. #include <tool/selection.h>
  62. #include <tool/tool_dispatcher.h>
  63. #include <tool/tool_manager.h>
  64. #include <tool/zoom_tool.h>
  65. #include <tools/pcb_editor_conditions.h>
  66. #include <tools/pcb_viewer_tools.h>
  67. #include <tools/group_tool.h>
  68. #include <tools/position_relative_tool.h>
  69. #include <widgets/appearance_controls.h>
  70. #include <widgets/lib_tree.h>
  71. #include <widgets/panel_selection_filter.h>
  72. #include <widgets/pcb_properties_panel.h>
  73. #include <widgets/wx_progress_reporters.h>
  74. #include <wildcards_and_files_ext.h>
  75. #include <wx/filedlg.h>
  76. #include <widgets/wx_aui_utils.h>
  77. BEGIN_EVENT_TABLE( FOOTPRINT_EDIT_FRAME, PCB_BASE_FRAME )
  78. EVT_MENU( wxID_CLOSE, FOOTPRINT_EDIT_FRAME::CloseFootprintEditor )
  79. EVT_MENU( wxID_EXIT, FOOTPRINT_EDIT_FRAME::OnExitKiCad )
  80. EVT_SIZE( FOOTPRINT_EDIT_FRAME::OnSize )
  81. EVT_CHOICE( ID_ON_ZOOM_SELECT, FOOTPRINT_EDIT_FRAME::OnSelectZoom )
  82. EVT_CHOICE( ID_ON_GRID_SELECT, FOOTPRINT_EDIT_FRAME::OnSelectGrid )
  83. EVT_TOOL( ID_FPEDIT_SAVE_PNG, FOOTPRINT_EDIT_FRAME::OnSaveFootprintAsPng )
  84. EVT_TOOL( ID_LOAD_FOOTPRINT_FROM_BOARD, FOOTPRINT_EDIT_FRAME::OnLoadFootprintFromBoard )
  85. EVT_TOOL( ID_ADD_FOOTPRINT_TO_BOARD, FOOTPRINT_EDIT_FRAME::OnSaveFootprintToBoard )
  86. // Horizontal toolbar
  87. EVT_COMBOBOX( ID_TOOLBARH_PCB_SELECT_LAYER, FOOTPRINT_EDIT_FRAME::SelectLayer )
  88. // UI update events.
  89. EVT_UPDATE_UI( ID_LOAD_FOOTPRINT_FROM_BOARD,
  90. FOOTPRINT_EDIT_FRAME::OnUpdateLoadFootprintFromBoard )
  91. EVT_UPDATE_UI( ID_ADD_FOOTPRINT_TO_BOARD,
  92. FOOTPRINT_EDIT_FRAME::OnUpdateSaveFootprintToBoard )
  93. EVT_UPDATE_UI( ID_TOOLBARH_PCB_SELECT_LAYER, FOOTPRINT_EDIT_FRAME::OnUpdateLayerSelectBox )
  94. // Drop files event
  95. EVT_DROP_FILES( FOOTPRINT_EDIT_FRAME::OnDropFiles )
  96. END_EVENT_TABLE()
  97. FOOTPRINT_EDIT_FRAME::FOOTPRINT_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
  98. PCB_BASE_EDIT_FRAME( aKiway, aParent, FRAME_FOOTPRINT_EDITOR, wxEmptyString,
  99. wxDefaultPosition, wxDefaultSize,
  100. KICAD_DEFAULT_DRAWFRAME_STYLE, GetFootprintEditorFrameName() ),
  101. m_show_layer_manager_tools( true )
  102. {
  103. m_showBorderAndTitleBlock = false; // true to show the frame references
  104. m_aboutTitle = _HKI( "KiCad Footprint Editor" );
  105. m_selLayerBox = nullptr;
  106. m_editorSettings = nullptr;
  107. // Give an icon
  108. wxIcon icon;
  109. wxIconBundle icon_bundle;
  110. icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_modedit, 48 ) );
  111. icon_bundle.AddIcon( icon );
  112. icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_modedit, 128 ) );
  113. icon_bundle.AddIcon( icon );
  114. icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_modedit, 256 ) );
  115. icon_bundle.AddIcon( icon );
  116. icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_modedit_32 ) );
  117. icon_bundle.AddIcon( icon );
  118. icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_modedit_16 ) );
  119. icon_bundle.AddIcon( icon );
  120. SetIcons( icon_bundle );
  121. // Create GAL canvas
  122. m_canvasType = loadCanvasTypeSetting( GetSettings() );
  123. PCB_DRAW_PANEL_GAL* drawPanel = new PCB_DRAW_PANEL_GAL( this, -1, wxPoint( 0, 0 ), m_frameSize,
  124. GetGalDisplayOptions(), m_canvasType );
  125. SetCanvas( drawPanel );
  126. CreateInfoBar();
  127. SetBoard( new BOARD() );
  128. // This board will only be used to hold a footprint for editing
  129. GetBoard()->SetBoardUse( BOARD_USE::FPHOLDER );
  130. // In Footprint Editor, the default net clearance is not known (it depends on the actual
  131. // board). So we do not show the default clearance, by setting it to 0. The footprint or
  132. // pad specific clearance will be shown.
  133. GetBoard()->GetDesignSettings().m_NetSettings->m_DefaultNetClass->SetClearance( 0 );
  134. // Don't show the default board solder mask expansion in the footprint editor. Only the
  135. // footprint or pad mask expansions settings should be shown.
  136. GetBoard()->GetDesignSettings().m_SolderMaskExpansion = 0;
  137. // Ensure all layers and items are visible:
  138. // In footprint editor, some layers have no meaning or cannot be used, but we show all of
  139. // them, at least to be able to edit a bad layer
  140. GetBoard()->SetVisibleAlls();
  141. GetGalDisplayOptions().m_axesEnabled = true;
  142. // In Footprint Editor, set the default paper size to A4 for plot/print
  143. SetPageSettings( PAGE_INFO( PAGE_INFO::A4 ) );
  144. SetScreen( new PCB_SCREEN( GetPageSettings().GetSizeIU( pcbIUScale.IU_PER_MILS ) ) );
  145. // Create the manager and dispatcher & route draw panel events to the dispatcher
  146. setupTools();
  147. setupUIConditions();
  148. initLibraryTree();
  149. m_treePane = new FOOTPRINT_TREE_PANE( this );
  150. // restore the last footprint from the project, if any, after the library has been init'ed
  151. restoreLastFootprint();
  152. ReCreateMenuBar();
  153. ReCreateHToolbar();
  154. ReCreateVToolbar();
  155. ReCreateOptToolbar();
  156. m_selectionFilterPanel = new PANEL_SELECTION_FILTER( this );
  157. m_appearancePanel = new APPEARANCE_CONTROLS( this, GetCanvas(), true );
  158. m_propertiesPanel = new PCB_PROPERTIES_PANEL( this, this );
  159. // LoadSettings() *after* creating m_LayersManager, because LoadSettings() initialize
  160. // parameters in m_LayersManager
  161. // NOTE: KifaceSettings() will return PCBNEW_SETTINGS if we started from pcbnew
  162. LoadSettings( GetSettings() );
  163. float proportion = GetFootprintEditorSettings()->m_AuiPanels.properties_splitter;
  164. m_propertiesPanel->SetSplitterProportion( proportion );
  165. // Must be set after calling LoadSettings() to be sure these parameters are not dependent
  166. // on what is read in stored settings. Enable one internal layer, because footprints
  167. // support keepout areas that can be on internal layers only (therefore on the first internal
  168. // layer). This is needed to handle these keepout in internal layers only.
  169. GetBoard()->SetCopperLayerCount( 3 );
  170. GetBoard()->SetEnabledLayers( GetBoard()->GetEnabledLayers().set( In1_Cu ) );
  171. GetBoard()->SetVisibleLayers( GetBoard()->GetEnabledLayers() );
  172. GetBoard()->SetLayerName( In1_Cu, _( "Inner layers" ) );
  173. SetActiveLayer( F_SilkS );
  174. m_auimgr.SetManagedWindow( this );
  175. unsigned int auiFlags = wxAUI_MGR_DEFAULT;
  176. #if !defined( _WIN32 )
  177. // Windows cannot redraw the UI fast enough during a live resize and may lead to all kinds
  178. // of graphical glitches
  179. auiFlags |= wxAUI_MGR_LIVE_RESIZE;
  180. #endif
  181. m_auimgr.SetFlags( auiFlags );
  182. // Rows; layers 4 - 6
  183. m_auimgr.AddPane( m_mainToolBar, EDA_PANE().HToolbar().Name( "MainToolbar" )
  184. .Top().Layer( 6 ) );
  185. m_auimgr.AddPane( m_messagePanel, EDA_PANE().Messages().Name( "MsgPanel" )
  186. .Bottom().Layer( 6 ) );
  187. // Columns; layers 1 - 3
  188. m_auimgr.AddPane( m_treePane, EDA_PANE().Palette().Name( "Footprints" )
  189. .Left().Layer( 4 )
  190. .Caption( _( "Libraries" ) )
  191. .MinSize( FromDIP( 250 ), -1 ).BestSize( FromDIP( 250 ), -1 ) );
  192. m_auimgr.AddPane( m_propertiesPanel, EDA_PANE().Name( PropertiesPaneName() )
  193. .Left().Layer( 3 )
  194. .Caption( _( "Properties" ) ).PaneBorder( false )
  195. .MinSize( FromDIP( wxSize( 240, 60 ) ) ).BestSize( FromDIP( wxSize( 300, 200 ) ) ) );
  196. m_auimgr.AddPane( m_optionsToolBar, EDA_PANE().VToolbar().Name( "OptToolbar" )
  197. .Left().Layer( 2 ) );
  198. m_auimgr.AddPane( m_drawToolBar, EDA_PANE().VToolbar().Name( "ToolsToolbar" )
  199. .Right().Layer(2) );
  200. m_auimgr.AddPane( m_appearancePanel, EDA_PANE().Name( "LayersManager" )
  201. .Right().Layer( 3 )
  202. .Caption( _( "Appearance" ) ).PaneBorder( false )
  203. .MinSize( FromDIP( 180 ), -1 ).BestSize( FromDIP( 180 ), -1 ) );
  204. m_auimgr.AddPane( m_selectionFilterPanel, EDA_PANE().Palette().Name( "SelectionFilter" )
  205. .Right().Layer( 3 ).Position( 2 )
  206. .Caption( _( "Selection Filter" ) ).PaneBorder( false )
  207. .MinSize( FromDIP( 180 ), -1 ).BestSize( FromDIP( 180 ), -1 ) );
  208. // Center
  209. m_auimgr.AddPane( GetCanvas(), EDA_PANE().Canvas().Name( "DrawFrame" )
  210. .Center() );
  211. m_auimgr.GetPane( "LayersManager" ).Show( m_show_layer_manager_tools );
  212. m_auimgr.GetPane( "SelectionFilter" ).Show( m_show_layer_manager_tools );
  213. m_auimgr.GetPane( PropertiesPaneName() ).Show( GetSettings()->m_AuiPanels.show_properties );
  214. // The selection filter doesn't need to grow in the vertical direction when docked
  215. m_auimgr.GetPane( "SelectionFilter" ).dock_proportion = 0;
  216. m_acceptedExts.emplace( FILEEXT::KiCadFootprintLibPathExtension, &ACTIONS::ddAddLibrary );
  217. m_acceptedExts.emplace( FILEEXT::KiCadFootprintFileExtension, &PCB_ACTIONS::ddImportFootprint );
  218. DragAcceptFiles( true );
  219. ActivateGalCanvas();
  220. FinishAUIInitialization();
  221. // Apply saved visibility stuff at the end
  222. if( FOOTPRINT_EDITOR_SETTINGS* cfg = dynamic_cast<FOOTPRINT_EDITOR_SETTINGS*>( GetSettings() ) )
  223. {
  224. wxAuiPaneInfo& treePane = m_auimgr.GetPane( "Footprints" );
  225. wxAuiPaneInfo& layersManager = m_auimgr.GetPane( "LayersManager" );
  226. if( cfg->m_LibWidth > 0 )
  227. SetAuiPaneSize( m_auimgr, treePane, cfg->m_LibWidth, -1 );
  228. if( cfg->m_AuiPanels.right_panel_width > 0 )
  229. SetAuiPaneSize( m_auimgr, layersManager, cfg->m_AuiPanels.right_panel_width, -1 );
  230. m_appearancePanel->SetUserLayerPresets( cfg->m_LayerPresets );
  231. m_appearancePanel->ApplyLayerPreset( cfg->m_ActiveLayerPreset );
  232. m_appearancePanel->SetTabIndex( cfg->m_AuiPanels.appearance_panel_tab );
  233. }
  234. GetToolManager()->PostAction( ACTIONS::zoomFitScreen );
  235. UpdateTitle();
  236. setupUnits( GetSettings() );
  237. resolveCanvasType();
  238. // Default shutdown reason until a file is loaded
  239. KIPLATFORM::APP::SetShutdownBlockReason( this, _( "Footprint changes are unsaved" ) );
  240. // Catch unhandled accelerator command characters that were no handled by the library tree
  241. // panel.
  242. Bind( wxEVT_CHAR, &TOOL_DISPATCHER::DispatchWxEvent, m_toolDispatcher );
  243. Bind( wxEVT_CHAR_HOOK, &TOOL_DISPATCHER::DispatchWxEvent, m_toolDispatcher );
  244. // Ensure the window is on top
  245. Raise();
  246. Show( true );
  247. // Register a call to update the toolbar sizes. It can't be done immediately because
  248. // it seems to require some sizes calculated that aren't yet (at least on GTK).
  249. CallAfter(
  250. [this]()
  251. {
  252. // Ensure the controls on the toolbars all are correctly sized
  253. UpdateToolbarControlSizes();
  254. m_treePane->FocusSearchFieldIfExists();
  255. } );
  256. }
  257. FOOTPRINT_EDIT_FRAME::~FOOTPRINT_EDIT_FRAME()
  258. {
  259. // Shutdown all running tools
  260. if( m_toolManager )
  261. m_toolManager->ShutdownAllTools();
  262. // save the footprint in the PROJECT
  263. retainLastFootprint();
  264. // Clear the watched file
  265. setFPWatcher( nullptr );
  266. delete m_selectionFilterPanel;
  267. delete m_appearancePanel;
  268. delete m_treePane;
  269. }
  270. void FOOTPRINT_EDIT_FRAME::UpdateMsgPanel()
  271. {
  272. EDA_DRAW_FRAME::UpdateMsgPanel();
  273. FOOTPRINT* fp = static_cast<FOOTPRINT*>( GetModel() );
  274. if( fp )
  275. {
  276. std::vector<MSG_PANEL_ITEM> msgItems;
  277. fp->GetMsgPanelInfo( this, msgItems );
  278. SetMsgPanel( msgItems );
  279. }
  280. }
  281. bool FOOTPRINT_EDIT_FRAME::IsContentModified() const
  282. {
  283. return GetScreen() && GetScreen()->IsContentModified()
  284. && GetBoard() && GetBoard()->GetFirstFootprint();
  285. }
  286. SELECTION& FOOTPRINT_EDIT_FRAME::GetCurrentSelection()
  287. {
  288. return m_toolManager->GetTool<PCB_SELECTION_TOOL>()->GetSelection();
  289. }
  290. void FOOTPRINT_EDIT_FRAME::SwitchCanvas( EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType )
  291. {
  292. // switches currently used canvas (Cairo / OpenGL).
  293. PCB_BASE_FRAME::SwitchCanvas( aCanvasType );
  294. GetCanvas()->GetGAL()->SetAxesEnabled( true );
  295. // The base class method *does not reinit* the layers manager. We must update the layer
  296. // widget to match board visibility states, both layers and render columns, and and some
  297. // settings dependent on the canvas.
  298. UpdateUserInterface();
  299. }
  300. void FOOTPRINT_EDIT_FRAME::HardRedraw()
  301. {
  302. SyncLibraryTree( true );
  303. GetCanvas()->ForceRefresh();
  304. }
  305. void FOOTPRINT_EDIT_FRAME::ToggleSearchTree()
  306. {
  307. wxAuiPaneInfo& treePane = m_auimgr.GetPane( m_treePane );
  308. treePane.Show( !IsSearchTreeShown() );
  309. if( IsSearchTreeShown() )
  310. {
  311. // SetAuiPaneSize also updates m_auimgr
  312. SetAuiPaneSize( m_auimgr, treePane, m_editorSettings->m_LibWidth, -1 );
  313. }
  314. else
  315. {
  316. m_editorSettings->m_LibWidth = m_treePane->GetSize().x;
  317. m_auimgr.Update();
  318. }
  319. }
  320. void FOOTPRINT_EDIT_FRAME::ToggleLayersManager()
  321. {
  322. FOOTPRINT_EDITOR_SETTINGS* settings = GetSettings();
  323. wxAuiPaneInfo& layersManager = m_auimgr.GetPane( "LayersManager" );
  324. wxAuiPaneInfo& selectionFilter = m_auimgr.GetPane( "SelectionFilter" );
  325. // show auxiliary Vertical layers and visibility manager toolbar
  326. m_show_layer_manager_tools = !m_show_layer_manager_tools;
  327. layersManager.Show( m_show_layer_manager_tools );
  328. selectionFilter.Show( m_show_layer_manager_tools );
  329. if( m_show_layer_manager_tools )
  330. {
  331. SetAuiPaneSize( m_auimgr, layersManager, settings->m_AuiPanels.right_panel_width, -1 );
  332. }
  333. else
  334. {
  335. settings->m_AuiPanels.right_panel_width = m_appearancePanel->GetSize().x;
  336. m_auimgr.Update();
  337. }
  338. }
  339. bool FOOTPRINT_EDIT_FRAME::IsSearchTreeShown() const
  340. {
  341. return const_cast<wxAuiManager&>( m_auimgr ).GetPane( m_treePane ).IsShown();
  342. }
  343. BOARD_ITEM_CONTAINER* FOOTPRINT_EDIT_FRAME::GetModel() const
  344. {
  345. return GetBoard()->GetFirstFootprint();
  346. }
  347. LIB_ID FOOTPRINT_EDIT_FRAME::GetTreeFPID() const
  348. {
  349. return m_treePane->GetLibTree()->GetSelectedLibId();
  350. }
  351. LIB_TREE_NODE* FOOTPRINT_EDIT_FRAME::GetCurrentTreeNode() const
  352. {
  353. return m_treePane->GetLibTree()->GetCurrentTreeNode();
  354. }
  355. LIB_ID FOOTPRINT_EDIT_FRAME::GetTargetFPID() const
  356. {
  357. LIB_ID id;
  358. if( IsSearchTreeShown() )
  359. id = GetTreeFPID();
  360. if( id.GetLibNickname().empty() )
  361. id = GetLoadedFPID();
  362. return id;
  363. }
  364. LIB_ID FOOTPRINT_EDIT_FRAME::GetLoadedFPID() const
  365. {
  366. FOOTPRINT* footprint = GetBoard()->GetFirstFootprint();
  367. if( footprint )
  368. return LIB_ID( footprint->GetFPID().GetLibNickname(), m_footprintNameWhenLoaded );
  369. else
  370. return LIB_ID();
  371. }
  372. void FOOTPRINT_EDIT_FRAME::ClearModify()
  373. {
  374. if( GetBoard()->GetFirstFootprint() )
  375. {
  376. m_footprintNameWhenLoaded =
  377. GetBoard()->GetFirstFootprint()->GetFPID().GetUniStringLibItemName();
  378. }
  379. GetScreen()->SetContentModified( false );
  380. }
  381. bool FOOTPRINT_EDIT_FRAME::IsCurrentFPFromBoard() const
  382. {
  383. // If we've already vetted closing this window, then we have no FP anymore
  384. if( m_isClosing || !GetBoard() )
  385. return false;
  386. FOOTPRINT* footprint = GetBoard()->GetFirstFootprint();
  387. return ( footprint && footprint->GetLink() != niluuid );
  388. }
  389. void FOOTPRINT_EDIT_FRAME::retainLastFootprint()
  390. {
  391. LIB_ID id = GetLoadedFPID();
  392. if( id.IsValid() )
  393. {
  394. Prj().SetRString( PROJECT::PCB_FOOTPRINT_EDITOR_LIB_NICKNAME, id.GetLibNickname() );
  395. Prj().SetRString( PROJECT::PCB_FOOTPRINT_EDITOR_FP_NAME, id.GetLibItemName() );
  396. }
  397. }
  398. void FOOTPRINT_EDIT_FRAME::restoreLastFootprint()
  399. {
  400. const wxString& footprintName = Prj().GetRString( PROJECT::PCB_FOOTPRINT_EDITOR_FP_NAME );
  401. const wxString& libNickname = Prj().GetRString( PROJECT::PCB_FOOTPRINT_EDITOR_LIB_NICKNAME );
  402. if( libNickname.Length() && footprintName.Length() )
  403. {
  404. LIB_ID id;
  405. id.SetLibNickname( libNickname );
  406. id.SetLibItemName( footprintName );
  407. FOOTPRINT* footprint = loadFootprint( id );
  408. if( footprint )
  409. AddFootprintToBoard( footprint );
  410. }
  411. }
  412. void FOOTPRINT_EDIT_FRAME::ReloadFootprint( FOOTPRINT* aFootprint )
  413. {
  414. GetBoard()->DeleteAllFootprints();
  415. m_originalFootprintCopy.reset( static_cast<FOOTPRINT*>( aFootprint->Clone() ) );
  416. m_originalFootprintCopy->SetParent( nullptr );
  417. m_footprintNameWhenLoaded = aFootprint->GetFPID().GetUniStringLibItemName();
  418. PCB_BASE_EDIT_FRAME::AddFootprintToBoard( aFootprint );
  419. // Ensure item UUIDs are valid
  420. // ("old" footprints can have null uuids that create issues in fp editor)
  421. aFootprint->FixUuids();
  422. if( IsCurrentFPFromBoard() )
  423. {
  424. wxString msg;
  425. msg.Printf( _( "Editing %s from board. Saving will update the board only." ),
  426. aFootprint->GetReference() );
  427. if( WX_INFOBAR* infobar = GetInfoBar() )
  428. {
  429. infobar->RemoveAllButtons();
  430. infobar->AddCloseButton();
  431. infobar->ShowMessage( msg, wxICON_INFORMATION );
  432. }
  433. }
  434. else
  435. {
  436. if( WX_INFOBAR* infobar = GetInfoBar() )
  437. infobar->Dismiss();
  438. }
  439. UpdateMsgPanel();
  440. }
  441. void FOOTPRINT_EDIT_FRAME::AddFootprintToBoard( FOOTPRINT* aFootprint )
  442. {
  443. ReloadFootprint( aFootprint );
  444. if( IsCurrentFPFromBoard() )
  445. setFPWatcher( nullptr );
  446. else
  447. setFPWatcher( aFootprint );
  448. }
  449. const wxChar* FOOTPRINT_EDIT_FRAME::GetFootprintEditorFrameName()
  450. {
  451. return FOOTPRINT_EDIT_FRAME_NAME;
  452. }
  453. BOARD_DESIGN_SETTINGS& FOOTPRINT_EDIT_FRAME::GetDesignSettings() const
  454. {
  455. return GetBoard()->GetDesignSettings();
  456. }
  457. const PCB_PLOT_PARAMS& FOOTPRINT_EDIT_FRAME::GetPlotSettings() const
  458. {
  459. wxFAIL_MSG( wxT( "Plotting not supported in Footprint Editor" ) );
  460. return PCB_BASE_FRAME::GetPlotSettings();
  461. }
  462. void FOOTPRINT_EDIT_FRAME::SetPlotSettings( const PCB_PLOT_PARAMS& aSettings )
  463. {
  464. wxFAIL_MSG( wxT( "Plotting not supported in Footprint Editor" ) );
  465. }
  466. FOOTPRINT_EDITOR_SETTINGS* FOOTPRINT_EDIT_FRAME::GetSettings()
  467. {
  468. if( !m_editorSettings )
  469. m_editorSettings = Pgm().GetSettingsManager().GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>();
  470. return m_editorSettings;
  471. }
  472. APP_SETTINGS_BASE* FOOTPRINT_EDIT_FRAME::config() const
  473. {
  474. return m_editorSettings ? m_editorSettings
  475. : Pgm().GetSettingsManager().GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>();
  476. }
  477. void FOOTPRINT_EDIT_FRAME::LoadSettings( APP_SETTINGS_BASE* aCfg )
  478. {
  479. // Get our own settings; aCfg will be the PCBNEW_SETTINGS because we're part of the pcbnew
  480. // compile unit
  481. FOOTPRINT_EDITOR_SETTINGS* cfg = GetSettings();
  482. if( cfg )
  483. {
  484. PCB_BASE_FRAME::LoadSettings( cfg );
  485. GetDesignSettings() = cfg->m_DesignSettings;
  486. m_displayOptions = cfg->m_Display;
  487. m_show_layer_manager_tools = cfg->m_AuiPanels.show_layer_manager;
  488. GetToolManager()->GetTool<PCB_SELECTION_TOOL>()->GetFilter() = cfg->m_SelectionFilter;
  489. m_selectionFilterPanel->SetCheckboxesFromFilter( cfg->m_SelectionFilter );
  490. m_treePane->GetLibTree()->SetSortMode( (LIB_TREE_MODEL_ADAPTER::SORT_MODE) cfg->m_LibrarySortMode );
  491. }
  492. }
  493. void FOOTPRINT_EDIT_FRAME::resolveCanvasType()
  494. {
  495. // Load canvas type from the FOOTPRINT_EDITOR_SETTINGS:
  496. m_canvasType = loadCanvasTypeSetting( GetSettings() );
  497. // If we had an OpenGL failure this session, use the fallback GAL but don't update the
  498. // user preference silently:
  499. if( m_openGLFailureOccured && m_canvasType == EDA_DRAW_PANEL_GAL::GAL_TYPE_OPENGL )
  500. m_canvasType = EDA_DRAW_PANEL_GAL::GAL_FALLBACK;
  501. }
  502. void FOOTPRINT_EDIT_FRAME::SaveSettings( APP_SETTINGS_BASE* aCfg )
  503. {
  504. GetGalDisplayOptions().m_axesEnabled = true;
  505. // Get our own settings; aCfg will be the PCBNEW_SETTINGS because we're part of the pcbnew
  506. // compile unit
  507. FOOTPRINT_EDITOR_SETTINGS* cfg = GetSettings();
  508. if( cfg )
  509. {
  510. PCB_BASE_FRAME::SaveSettings( cfg );
  511. cfg->m_DesignSettings = GetDesignSettings();
  512. cfg->m_Display = m_displayOptions;
  513. cfg->m_LibWidth = m_treePane->GetSize().x;
  514. cfg->m_SelectionFilter = GetToolManager()->GetTool<PCB_SELECTION_TOOL>()->GetFilter();
  515. cfg->m_AuiPanels.show_layer_manager = m_show_layer_manager_tools;
  516. if( m_propertiesPanel )
  517. {
  518. cfg->m_AuiPanels.show_properties = m_propertiesPanel->IsShownOnScreen();
  519. cfg->m_AuiPanels.properties_panel_width = m_propertiesPanel->GetSize().x;
  520. cfg->m_AuiPanels.properties_splitter = m_propertiesPanel->SplitterProportion();
  521. }
  522. cfg->m_LibrarySortMode = m_treePane->GetLibTree()->GetSortMode();
  523. if( m_appearancePanel )
  524. {
  525. cfg->m_AuiPanels.right_panel_width = m_appearancePanel->GetSize().x;
  526. cfg->m_AuiPanels.appearance_panel_tab = m_appearancePanel->GetTabIndex();
  527. cfg->m_LayerPresets = m_appearancePanel->GetUserLayerPresets();
  528. cfg->m_ActiveLayerPreset = m_appearancePanel->GetActiveLayerPreset();
  529. }
  530. }
  531. }
  532. EDA_ANGLE FOOTPRINT_EDIT_FRAME::GetRotationAngle() const
  533. {
  534. FOOTPRINT_EDITOR_SETTINGS* cfg = const_cast<FOOTPRINT_EDIT_FRAME*>( this )->GetSettings();
  535. return cfg ? cfg->m_RotationAngle : ANGLE_90;
  536. }
  537. COLOR_SETTINGS* FOOTPRINT_EDIT_FRAME::GetColorSettings( bool aForceRefresh ) const
  538. {
  539. wxString currentTheme = GetFootprintEditorSettings()->m_ColorTheme;
  540. return Pgm().GetSettingsManager().GetColorSettings( currentTheme );
  541. }
  542. MAGNETIC_SETTINGS* FOOTPRINT_EDIT_FRAME::GetMagneticItemsSettings()
  543. {
  544. // Get the actual frame settings for magnetic items
  545. FOOTPRINT_EDITOR_SETTINGS* cfg = GetSettings();
  546. wxCHECK( cfg, nullptr );
  547. return &cfg->m_MagneticItems;
  548. }
  549. const BOX2I FOOTPRINT_EDIT_FRAME::GetDocumentExtents( bool aIncludeAllVisible ) const
  550. {
  551. FOOTPRINT* footprint = GetBoard()->GetFirstFootprint();
  552. if( footprint )
  553. {
  554. bool hasGraphicalItem = footprint->Pads().size() || footprint->Zones().size();
  555. if( !hasGraphicalItem )
  556. {
  557. for( const BOARD_ITEM* item : footprint->GraphicalItems() )
  558. {
  559. if( item->Type() == PCB_TEXT_T || item->Type() == PCB_TEXTBOX_T )
  560. continue;
  561. hasGraphicalItem = true;
  562. break;
  563. }
  564. }
  565. if( hasGraphicalItem )
  566. {
  567. return footprint->GetBoundingBox( false, false );
  568. }
  569. else
  570. {
  571. BOX2I newFootprintBB( { 0, 0 }, { 0, 0 } );
  572. newFootprintBB.Inflate( pcbIUScale.mmToIU( 12 ) );
  573. return newFootprintBB;
  574. }
  575. }
  576. return GetBoardBoundingBox( false );
  577. }
  578. bool FOOTPRINT_EDIT_FRAME::CanCloseFPFromBoard( bool doClose )
  579. {
  580. if( IsContentModified() )
  581. {
  582. wxString footprintName = GetBoard()->GetFirstFootprint()->GetReference();
  583. wxString msg = _( "Save changes to '%s' before closing?" );
  584. if( !HandleUnsavedChanges( this, wxString::Format( msg, footprintName ),
  585. [&]() -> bool
  586. {
  587. return SaveFootprint( GetBoard()->GetFirstFootprint() );
  588. } ) )
  589. {
  590. return false;
  591. }
  592. }
  593. if( doClose )
  594. {
  595. GetInfoBar()->ShowMessageFor( wxEmptyString, 1 );
  596. Clear_Pcb( false );
  597. UpdateTitle();
  598. }
  599. return true;
  600. }
  601. bool FOOTPRINT_EDIT_FRAME::canCloseWindow( wxCloseEvent& aEvent )
  602. {
  603. if( IsContentModified() )
  604. {
  605. // Shutdown blocks must be determined and vetoed as early as possible
  606. if( KIPLATFORM::APP::SupportsShutdownBlockReason() &&
  607. aEvent.GetId() == wxEVT_QUERY_END_SESSION )
  608. {
  609. aEvent.Veto();
  610. return false;
  611. }
  612. wxString footprintName = GetBoard()->GetFirstFootprint()->GetFPID().GetLibItemName();
  613. if( IsCurrentFPFromBoard() )
  614. footprintName = GetBoard()->GetFirstFootprint()->GetReference();
  615. wxString msg = _( "Save changes to '%s' before closing?" );
  616. if( !HandleUnsavedChanges( this, wxString::Format( msg, footprintName ),
  617. [&]() -> bool
  618. {
  619. return SaveFootprint( GetBoard()->GetFirstFootprint() );
  620. } ) )
  621. {
  622. aEvent.Veto();
  623. return false;
  624. }
  625. }
  626. PAD_TOOL* padTool = m_toolManager->GetTool<PAD_TOOL>();
  627. if( padTool->InPadEditMode() )
  628. padTool->ExitPadEditMode();
  629. // Save footprint tree column widths
  630. m_adapter->SaveSettings();
  631. return PCB_BASE_EDIT_FRAME::canCloseWindow( aEvent );
  632. }
  633. void FOOTPRINT_EDIT_FRAME::doCloseWindow()
  634. {
  635. // No more vetos
  636. GetCanvas()->SetEventDispatcher( nullptr );
  637. GetCanvas()->StopDrawing();
  638. // Do not show the layer manager during closing to avoid flicker
  639. // on some platforms (Windows) that generate useless redraw of items in
  640. // the Layer Manager
  641. m_auimgr.GetPane( wxT( "LayersManager" ) ).Show( false );
  642. m_auimgr.GetPane( wxT( "SelectionFilter" ) ).Show( false );
  643. Clear_Pcb( false );
  644. SETTINGS_MANAGER* mgr = GetSettingsManager();
  645. if( mgr->IsProjectOpen() && wxFileName::IsDirWritable( Prj().GetProjectPath() ) )
  646. {
  647. GFootprintList.WriteCacheToFile( Prj().GetProjectPath() + wxT( "fp-info-cache" ) );
  648. }
  649. }
  650. void FOOTPRINT_EDIT_FRAME::OnExitKiCad( wxCommandEvent& event )
  651. {
  652. Kiway().OnKiCadExit();
  653. }
  654. void FOOTPRINT_EDIT_FRAME::CloseFootprintEditor( wxCommandEvent& Event )
  655. {
  656. Close();
  657. }
  658. void FOOTPRINT_EDIT_FRAME::OnUpdateLoadFootprintFromBoard( wxUpdateUIEvent& aEvent )
  659. {
  660. PCB_EDIT_FRAME* frame = (PCB_EDIT_FRAME*) Kiway().Player( FRAME_PCB_EDITOR, false );
  661. aEvent.Enable( frame != nullptr );
  662. }
  663. void FOOTPRINT_EDIT_FRAME::OnUpdateSaveFootprintToBoard( wxUpdateUIEvent& aEvent )
  664. {
  665. PCB_EDIT_FRAME* frame = (PCB_EDIT_FRAME*) Kiway().Player( FRAME_PCB_EDITOR, false );
  666. FOOTPRINT* editorFootprint = GetBoard()->GetFirstFootprint();
  667. bool canInsert = frame && editorFootprint && editorFootprint->GetLink() == niluuid;
  668. // If the source was deleted, the footprint can inserted but not updated in the board.
  669. if( frame && editorFootprint && editorFootprint->GetLink() != niluuid )
  670. {
  671. BOARD* mainpcb = frame->GetBoard();
  672. canInsert = true;
  673. // search if the source footprint was not deleted:
  674. for( FOOTPRINT* candidate : mainpcb->Footprints() )
  675. {
  676. if( editorFootprint->GetLink() == candidate->m_Uuid )
  677. {
  678. canInsert = false;
  679. break;
  680. }
  681. }
  682. }
  683. aEvent.Enable( canInsert );
  684. }
  685. void FOOTPRINT_EDIT_FRAME::ShowChangedLanguage()
  686. {
  687. // call my base class
  688. PCB_BASE_EDIT_FRAME::ShowChangedLanguage();
  689. // We have 2 panes to update.
  690. // For some obscure reason, the AUI manager hides the first modified pane.
  691. // So force show panes
  692. wxAuiPaneInfo& tree_pane_info = m_auimgr.GetPane( m_treePane );
  693. bool tree_shown = tree_pane_info.IsShown();
  694. tree_pane_info.Caption( _( "Libraries" ) );
  695. wxAuiPaneInfo& lm_pane_info = m_auimgr.GetPane( m_appearancePanel );
  696. bool lm_shown = lm_pane_info.IsShown();
  697. lm_pane_info.Caption( _( "Appearance" ) );
  698. wxAuiPaneInfo& sf_pane_info = m_auimgr.GetPane( m_selectionFilterPanel );
  699. sf_pane_info.Caption( _( "Selection Filter" ) );
  700. // update the layer manager
  701. m_appearancePanel->OnLanguageChanged();
  702. m_selectionFilterPanel->OnLanguageChanged();
  703. UpdateUserInterface();
  704. // Now restore the visibility:
  705. lm_pane_info.Show( lm_shown );
  706. tree_pane_info.Show( tree_shown );
  707. m_auimgr.Update();
  708. m_treePane->GetLibTree()->ShowChangedLanguage();
  709. UpdateTitle();
  710. }
  711. void FOOTPRINT_EDIT_FRAME::OnModify()
  712. {
  713. PCB_BASE_FRAME::OnModify();
  714. Update3DView( true, true );
  715. m_treePane->GetLibTree()->RefreshLibTree();
  716. if( !GetTitle().StartsWith( wxT( "*" ) ) )
  717. UpdateTitle();
  718. }
  719. void FOOTPRINT_EDIT_FRAME::UpdateTitle()
  720. {
  721. wxString title;
  722. LIB_ID fpid = GetLoadedFPID();
  723. FOOTPRINT* footprint = GetBoard()->GetFirstFootprint();
  724. bool writable = true;
  725. if( IsCurrentFPFromBoard() )
  726. {
  727. if( IsContentModified() )
  728. title = wxT( "*" );
  729. title += footprint->GetReference();
  730. title += wxS( " " ) + wxString::Format( _( "[from %s]" ), Prj().GetProjectName()
  731. + wxT( "." )
  732. + FILEEXT::PcbFileExtension );
  733. }
  734. else if( fpid.IsValid() )
  735. {
  736. try
  737. {
  738. writable = PROJECT_PCB::PcbFootprintLibs( &Prj() )->IsFootprintLibWritable( fpid.GetLibNickname() );
  739. }
  740. catch( const IO_ERROR& )
  741. {
  742. // best efforts...
  743. }
  744. // Note: don't used GetLoadedFPID(); footprint name may have been edited
  745. if( IsContentModified() )
  746. title = wxT( "*" );
  747. title += From_UTF8( footprint->GetFPID().Format().c_str() );
  748. if( !writable )
  749. title += wxS( " " ) + _( "[Read Only]" );
  750. }
  751. else if( !fpid.GetLibItemName().empty() )
  752. {
  753. // Note: don't used GetLoadedFPID(); footprint name may have been edited
  754. if( IsContentModified() )
  755. title = wxT( "*" );
  756. title += From_UTF8( footprint->GetFPID().GetLibItemName().c_str() );
  757. title += wxS( " " ) + _( "[Unsaved]" );
  758. }
  759. else
  760. {
  761. title = _( "[no footprint loaded]" );
  762. }
  763. title += wxT( " \u2014 " ) + _( "Footprint Editor" );
  764. SetTitle( title );
  765. }
  766. void FOOTPRINT_EDIT_FRAME::UpdateUserInterface()
  767. {
  768. m_appearancePanel->OnBoardChanged();
  769. }
  770. void FOOTPRINT_EDIT_FRAME::UpdateView()
  771. {
  772. GetCanvas()->UpdateColors();
  773. GetCanvas()->DisplayBoard( GetBoard() );
  774. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  775. UpdateTitle();
  776. }
  777. void FOOTPRINT_EDIT_FRAME::initLibraryTree()
  778. {
  779. FP_LIB_TABLE* fpTable = PROJECT_PCB::PcbFootprintLibs( &Prj() );
  780. WX_PROGRESS_REPORTER progressReporter( this, _( "Loading Footprint Libraries" ), 2 );
  781. if( GFootprintList.GetCount() == 0 )
  782. GFootprintList.ReadCacheFromFile( Prj().GetProjectPath() + wxT( "fp-info-cache" ) );
  783. GFootprintList.ReadFootprintFiles( fpTable, nullptr, &progressReporter );
  784. progressReporter.Show( false );
  785. if( GFootprintList.GetErrorCount() )
  786. GFootprintList.DisplayErrors( this );
  787. m_adapter = FP_TREE_SYNCHRONIZING_ADAPTER::Create( this, fpTable );
  788. auto adapter = static_cast<FP_TREE_SYNCHRONIZING_ADAPTER*>( m_adapter.get() );
  789. adapter->AddLibraries( this );
  790. }
  791. void FOOTPRINT_EDIT_FRAME::SyncLibraryTree( bool aProgress )
  792. {
  793. FP_LIB_TABLE* fpTable = PROJECT_PCB::PcbFootprintLibs( &Prj() );
  794. auto adapter = static_cast<FP_TREE_SYNCHRONIZING_ADAPTER*>( m_adapter.get() );
  795. LIB_ID target = GetTargetFPID();
  796. bool targetSelected = ( target == m_treePane->GetLibTree()->GetSelectedLibId() );
  797. // Sync FOOTPRINT_INFO list to the libraries on disk
  798. if( aProgress )
  799. {
  800. WX_PROGRESS_REPORTER progressReporter( this, _( "Updating Footprint Libraries" ), 2 );
  801. GFootprintList.ReadFootprintFiles( fpTable, nullptr, &progressReporter );
  802. progressReporter.Show( false );
  803. }
  804. else
  805. {
  806. GFootprintList.ReadFootprintFiles( fpTable, nullptr, nullptr );
  807. }
  808. // Sync the LIB_TREE to the FOOTPRINT_INFO list
  809. adapter->Sync( fpTable );
  810. m_treePane->GetLibTree()->Unselect();
  811. m_treePane->GetLibTree()->Regenerate( true );
  812. if( target.IsValid() )
  813. {
  814. if( adapter->FindItem( target ) )
  815. {
  816. if( targetSelected )
  817. m_treePane->GetLibTree()->SelectLibId( target );
  818. else
  819. m_treePane->GetLibTree()->CenterLibId( target );
  820. }
  821. else
  822. {
  823. // Try to focus on parent
  824. target.SetLibItemName( wxEmptyString );
  825. m_treePane->GetLibTree()->CenterLibId( target );
  826. }
  827. }
  828. }
  829. void FOOTPRINT_EDIT_FRAME::RegenerateLibraryTree()
  830. {
  831. LIB_ID target = GetTargetFPID();
  832. m_treePane->GetLibTree()->Regenerate( true );
  833. if( target.IsValid() )
  834. m_treePane->GetLibTree()->CenterLibId( target );
  835. }
  836. void FOOTPRINT_EDIT_FRAME::RefreshLibraryTree()
  837. {
  838. m_treePane->GetLibTree()->RefreshLibTree();
  839. }
  840. void FOOTPRINT_EDIT_FRAME::FocusOnLibID( const LIB_ID& aLibID )
  841. {
  842. m_treePane->GetLibTree()->SelectLibId( aLibID );
  843. }
  844. void FOOTPRINT_EDIT_FRAME::OnDisplayOptionsChanged()
  845. {
  846. m_appearancePanel->UpdateDisplayOptions();
  847. }
  848. void FOOTPRINT_EDIT_FRAME::setupTools()
  849. {
  850. // Create the manager and dispatcher & route draw panel events to the dispatcher
  851. m_toolManager = new TOOL_MANAGER;
  852. m_toolManager->SetEnvironment( GetBoard(), GetCanvas()->GetView(),
  853. GetCanvas()->GetViewControls(), config(), this );
  854. m_actions = new PCB_ACTIONS();
  855. m_toolDispatcher = new TOOL_DISPATCHER( m_toolManager );
  856. GetCanvas()->SetEventDispatcher( m_toolDispatcher );
  857. m_toolManager->RegisterTool( new COMMON_CONTROL );
  858. m_toolManager->RegisterTool( new COMMON_TOOLS );
  859. m_toolManager->RegisterTool( new PCB_SELECTION_TOOL );
  860. m_toolManager->RegisterTool( new ZOOM_TOOL );
  861. m_toolManager->RegisterTool( new EDIT_TOOL );
  862. m_toolManager->RegisterTool( new PCB_EDIT_TABLE_TOOL );
  863. m_toolManager->RegisterTool( new PAD_TOOL );
  864. m_toolManager->RegisterTool( new DRAWING_TOOL );
  865. m_toolManager->RegisterTool( new PCB_POINT_EDITOR );
  866. m_toolManager->RegisterTool( new PCB_CONTROL ); // copy/paste
  867. m_toolManager->RegisterTool( new FOOTPRINT_EDITOR_CONTROL );
  868. m_toolManager->RegisterTool( new ALIGN_DISTRIBUTE_TOOL );
  869. m_toolManager->RegisterTool( new PCB_PICKER_TOOL );
  870. m_toolManager->RegisterTool( new POSITION_RELATIVE_TOOL );
  871. m_toolManager->RegisterTool( new PCB_VIEWER_TOOLS );
  872. m_toolManager->RegisterTool( new GROUP_TOOL );
  873. m_toolManager->RegisterTool( new CONVERT_TOOL );
  874. m_toolManager->RegisterTool( new SCRIPTING_TOOL );
  875. m_toolManager->RegisterTool( new PROPERTIES_TOOL );
  876. for( TOOL_BASE* tool : m_toolManager->Tools() )
  877. {
  878. if( PCB_TOOL_BASE* pcbTool = dynamic_cast<PCB_TOOL_BASE*>( tool ) )
  879. pcbTool->SetIsFootprintEditor( true );
  880. }
  881. m_toolManager->GetTool<PCB_VIEWER_TOOLS>()->SetFootprintFrame( true );
  882. m_toolManager->InitTools();
  883. m_toolManager->InvokeTool( "pcbnew.InteractiveSelection" );
  884. // Load or reload wizard plugins in case they changed since the last time the frame opened
  885. // Because the board editor has also a plugin python menu,
  886. // call the PCB_EDIT_FRAME RunAction() if the board editor is running
  887. // Otherwise run the current RunAction().
  888. PCB_EDIT_FRAME* pcbframe = static_cast<PCB_EDIT_FRAME*>( Kiway().Player( FRAME_PCB_EDITOR, false ) );
  889. if( pcbframe )
  890. pcbframe->GetToolManager()->RunAction( PCB_ACTIONS::pluginsReload );
  891. else
  892. m_toolManager->RunAction( PCB_ACTIONS::pluginsReload );
  893. }
  894. void FOOTPRINT_EDIT_FRAME::setupUIConditions()
  895. {
  896. PCB_BASE_EDIT_FRAME::setupUIConditions();
  897. ACTION_MANAGER* mgr = m_toolManager->GetActionManager();
  898. PCB_EDITOR_CONDITIONS cond( this );
  899. wxASSERT( mgr );
  900. #define ENABLE( x ) ACTION_CONDITIONS().Enable( x )
  901. #define CHECK( x ) ACTION_CONDITIONS().Check( x )
  902. auto haveFootprintCond =
  903. [this]( const SELECTION& )
  904. {
  905. return GetBoard() && GetBoard()->GetFirstFootprint() != nullptr;
  906. };
  907. auto footprintTargettedCond =
  908. [this]( const SELECTION& )
  909. {
  910. return !GetTargetFPID().GetLibItemName().empty();
  911. };
  912. mgr->SetConditions( ACTIONS::saveAs, ENABLE( footprintTargettedCond ) );
  913. mgr->SetConditions( ACTIONS::revert, ENABLE( cond.ContentModified() ) );
  914. mgr->SetConditions( ACTIONS::save, ENABLE( SELECTION_CONDITIONS::ShowAlways ) );
  915. mgr->SetConditions( ACTIONS::undo, ENABLE( cond.UndoAvailable() ) );
  916. mgr->SetConditions( ACTIONS::redo, ENABLE( cond.RedoAvailable() ) );
  917. mgr->SetConditions( ACTIONS::toggleGrid, CHECK( cond.GridVisible() ) );
  918. mgr->SetConditions( ACTIONS::toggleGridOverrides, CHECK( cond.GridOverrides() ) );
  919. mgr->SetConditions( ACTIONS::toggleCursorStyle, CHECK( cond.FullscreenCursor() ) );
  920. mgr->SetConditions( ACTIONS::millimetersUnits, CHECK( cond.Units( EDA_UNITS::MILLIMETRES ) ) );
  921. mgr->SetConditions( ACTIONS::inchesUnits, CHECK( cond.Units( EDA_UNITS::INCHES ) ) );
  922. mgr->SetConditions( ACTIONS::milsUnits, CHECK( cond.Units( EDA_UNITS::MILS ) ) );
  923. mgr->SetConditions( ACTIONS::cut, ENABLE( cond.HasItems() ) );
  924. mgr->SetConditions( ACTIONS::copy, ENABLE( cond.HasItems() ) );
  925. mgr->SetConditions( ACTIONS::paste, ENABLE( SELECTION_CONDITIONS::Idle && cond.NoActiveTool() ) );
  926. mgr->SetConditions( ACTIONS::pasteSpecial, ENABLE( SELECTION_CONDITIONS::Idle && cond.NoActiveTool() ) );
  927. mgr->SetConditions( ACTIONS::doDelete, ENABLE( cond.HasItems() ) );
  928. mgr->SetConditions( ACTIONS::duplicate, ENABLE( cond.HasItems() ) );
  929. mgr->SetConditions( ACTIONS::selectAll, ENABLE( cond.HasItems() ) );
  930. mgr->SetConditions( ACTIONS::unselectAll, ENABLE( cond.HasItems() ) );
  931. mgr->SetConditions( PCB_ACTIONS::rotateCw, ENABLE( cond.HasItems() ) );
  932. mgr->SetConditions( PCB_ACTIONS::rotateCcw, ENABLE( cond.HasItems() ) );
  933. mgr->SetConditions( PCB_ACTIONS::mirrorH, ENABLE( cond.HasItems() ) );
  934. mgr->SetConditions( PCB_ACTIONS::mirrorV, ENABLE( cond.HasItems() ) );
  935. mgr->SetConditions( PCB_ACTIONS::group, ENABLE( SELECTION_CONDITIONS::NotEmpty ) );
  936. mgr->SetConditions( PCB_ACTIONS::ungroup, ENABLE( SELECTION_CONDITIONS::HasType( PCB_GROUP_T ) ) );
  937. mgr->SetConditions( PCB_ACTIONS::padDisplayMode, CHECK( !cond.PadFillDisplay() ) );
  938. mgr->SetConditions( PCB_ACTIONS::textOutlines, CHECK( !cond.TextFillDisplay() ) );
  939. mgr->SetConditions( PCB_ACTIONS::graphicsOutlines, CHECK( !cond.GraphicsFillDisplay() ) );
  940. mgr->SetConditions( ACTIONS::zoomTool, CHECK( cond.CurrentTool( ACTIONS::zoomTool ) ) );
  941. mgr->SetConditions( ACTIONS::selectionTool, CHECK( cond.CurrentTool( ACTIONS::selectionTool ) ) );
  942. auto constrainedDrawingModeCond =
  943. [this]( const SELECTION& )
  944. {
  945. return GetSettings()->m_Use45Limit;
  946. };
  947. auto highContrastCond =
  948. [this]( const SELECTION& )
  949. {
  950. return GetDisplayOptions().m_ContrastModeDisplay != HIGH_CONTRAST_MODE::NORMAL;
  951. };
  952. auto boardFlippedCond =
  953. [this]( const SELECTION& )
  954. {
  955. return GetCanvas() && GetCanvas()->GetView()->IsMirroredX();
  956. };
  957. auto footprintTreeCond =
  958. [this](const SELECTION& )
  959. {
  960. return IsSearchTreeShown();
  961. };
  962. auto layerManagerCond =
  963. [this]( const SELECTION& )
  964. {
  965. return m_auimgr.GetPane( "LayersManager" ).IsShown();
  966. };
  967. auto propertiesCond =
  968. [this] ( const SELECTION& )
  969. {
  970. return m_auimgr.GetPane( PropertiesPaneName() ).IsShown();
  971. };
  972. mgr->SetConditions( PCB_ACTIONS::toggleHV45Mode, CHECK( constrainedDrawingModeCond ) );
  973. mgr->SetConditions( ACTIONS::highContrastMode, CHECK( highContrastCond ) );
  974. mgr->SetConditions( PCB_ACTIONS::flipBoard, CHECK( boardFlippedCond ) );
  975. mgr->SetConditions( ACTIONS::toggleBoundingBoxes, CHECK( cond.BoundingBoxes() ) );
  976. mgr->SetConditions( PCB_ACTIONS::showFootprintTree, CHECK( footprintTreeCond ) );
  977. mgr->SetConditions( PCB_ACTIONS::showLayersManager, CHECK( layerManagerCond ) );
  978. mgr->SetConditions( PCB_ACTIONS::showProperties, CHECK( propertiesCond ) );
  979. mgr->SetConditions( ACTIONS::print, ENABLE( haveFootprintCond ) );
  980. mgr->SetConditions( PCB_ACTIONS::exportFootprint, ENABLE( haveFootprintCond ) );
  981. mgr->SetConditions( PCB_ACTIONS::placeImportedGraphics, ENABLE( haveFootprintCond ) );
  982. mgr->SetConditions( PCB_ACTIONS::footprintProperties, ENABLE( haveFootprintCond ) );
  983. mgr->SetConditions( PCB_ACTIONS::editTextAndGraphics, ENABLE( haveFootprintCond ) );
  984. mgr->SetConditions( PCB_ACTIONS::checkFootprint, ENABLE( haveFootprintCond ) );
  985. mgr->SetConditions( PCB_ACTIONS::repairFootprint, ENABLE( haveFootprintCond ) );
  986. mgr->SetConditions( PCB_ACTIONS::cleanupGraphics, ENABLE( haveFootprintCond ) );
  987. auto isArcKeepCenterMode =
  988. [this]( const SELECTION& )
  989. {
  990. return GetSettings()->m_ArcEditMode == ARC_EDIT_MODE::KEEP_CENTER_ADJUST_ANGLE_RADIUS;
  991. };
  992. auto isArcKeepEndpointMode =
  993. [this]( const SELECTION& )
  994. {
  995. return GetSettings()->m_ArcEditMode == ARC_EDIT_MODE::KEEP_ENDPOINTS_OR_START_DIRECTION;
  996. };
  997. mgr->SetConditions( PCB_ACTIONS::pointEditorArcKeepCenter, CHECK( isArcKeepCenterMode ) );
  998. mgr->SetConditions( PCB_ACTIONS::pointEditorArcKeepEndpoint, CHECK( isArcKeepEndpointMode ) );
  999. // Only enable a tool if the part is edtable
  1000. #define CURRENT_EDIT_TOOL( action ) \
  1001. mgr->SetConditions( action, ACTION_CONDITIONS().Enable( haveFootprintCond ) \
  1002. .Check( cond.CurrentTool( action ) ) )
  1003. CURRENT_EDIT_TOOL( ACTIONS::deleteTool );
  1004. CURRENT_EDIT_TOOL( ACTIONS::measureTool );
  1005. CURRENT_EDIT_TOOL( PCB_ACTIONS::placePad );
  1006. CURRENT_EDIT_TOOL( PCB_ACTIONS::drawLine );
  1007. CURRENT_EDIT_TOOL( PCB_ACTIONS::drawRectangle );
  1008. CURRENT_EDIT_TOOL( PCB_ACTIONS::drawCircle );
  1009. CURRENT_EDIT_TOOL( PCB_ACTIONS::drawArc );
  1010. CURRENT_EDIT_TOOL( PCB_ACTIONS::drawPolygon );
  1011. CURRENT_EDIT_TOOL( PCB_ACTIONS::drawRuleArea );
  1012. CURRENT_EDIT_TOOL( PCB_ACTIONS::placeReferenceImage );
  1013. CURRENT_EDIT_TOOL( PCB_ACTIONS::placeText );
  1014. CURRENT_EDIT_TOOL( PCB_ACTIONS::drawTextBox );
  1015. CURRENT_EDIT_TOOL( PCB_ACTIONS::drawAlignedDimension );
  1016. CURRENT_EDIT_TOOL( PCB_ACTIONS::drawOrthogonalDimension );
  1017. CURRENT_EDIT_TOOL( PCB_ACTIONS::drawCenterDimension );
  1018. CURRENT_EDIT_TOOL( PCB_ACTIONS::drawRadialDimension );
  1019. CURRENT_EDIT_TOOL( PCB_ACTIONS::drawLeader );
  1020. CURRENT_EDIT_TOOL( PCB_ACTIONS::setAnchor );
  1021. CURRENT_EDIT_TOOL( PCB_ACTIONS::gridSetOrigin );
  1022. #undef CURRENT_EDIT_TOOL
  1023. #undef ENABLE
  1024. #undef CHECK
  1025. }
  1026. void FOOTPRINT_EDIT_FRAME::ActivateGalCanvas()
  1027. {
  1028. PCB_BASE_EDIT_FRAME::ActivateGalCanvas();
  1029. // Be sure the axis are enabled
  1030. GetCanvas()->GetGAL()->SetAxesEnabled( true );
  1031. UpdateView();
  1032. // Ensure the m_Layers settings are using the canvas type:
  1033. UpdateUserInterface();
  1034. }
  1035. void FOOTPRINT_EDIT_FRAME::CommonSettingsChanged( bool aEnvVarsChanged, bool aTextVarsChanged )
  1036. {
  1037. PCB_BASE_EDIT_FRAME::CommonSettingsChanged( aEnvVarsChanged, aTextVarsChanged );
  1038. auto cfg = Pgm().GetSettingsManager().GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>();
  1039. GetGalDisplayOptions().ReadWindowSettings( cfg->m_Window );
  1040. GetBoard()->GetDesignSettings() = cfg->m_DesignSettings;
  1041. GetCanvas()->GetView()->UpdateAllLayersColor();
  1042. GetCanvas()->GetView()->MarkTargetDirty( KIGFX::TARGET_NONCACHED );
  1043. GetCanvas()->ForceRefresh();
  1044. UpdateUserInterface();
  1045. if( aEnvVarsChanged )
  1046. SyncLibraryTree( true );
  1047. Layout();
  1048. SendSizeEvent();
  1049. }
  1050. void FOOTPRINT_EDIT_FRAME::OnSaveFootprintAsPng( wxCommandEvent& event )
  1051. {
  1052. LIB_ID id = GetLoadedFPID();
  1053. if( id.empty() )
  1054. {
  1055. DisplayErrorMessage( this, _( "No footprint selected." ) );
  1056. return;
  1057. }
  1058. wxFileName fn( id.GetLibItemName() );
  1059. fn.SetExt( wxT( "png" ) );
  1060. wxString projectPath = wxPathOnly( Prj().GetProjectFullName() );
  1061. wxFileDialog dlg( this, _( "Export View as PNG" ), projectPath, fn.GetFullName(),
  1062. FILEEXT::PngFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
  1063. if( dlg.ShowModal() == wxID_CANCEL || dlg.GetPath().IsEmpty() )
  1064. return;
  1065. // calling wxYield is mandatory under Linux, after closing the file selector dialog
  1066. // to refresh the screen before creating the PNG or JPEG image from screen
  1067. wxYield();
  1068. this->SaveCanvasImageToFile( dlg.GetPath(), BITMAP_TYPE::PNG );
  1069. }