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.

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