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.

1094 lines
36 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-2019 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/footprint_editor_tools.h"
  26. #include "tools/pad_tool.h"
  27. #include "tools/pcb_actions.h"
  28. #include "tools/pcb_control.h"
  29. #include "tools/pcb_picker_tool.h"
  30. #include "tools/placement_tool.h"
  31. #include "tools/point_editor.h"
  32. #include "tools/pcb_selection_tool.h"
  33. #include <3d_viewer/eda_3d_viewer.h>
  34. #include <bitmaps.h>
  35. #include <board.h>
  36. #include <footprint.h>
  37. #include <confirm.h>
  38. #include <dialogs/panel_fp_editor_color_settings.h>
  39. #include <dialogs/panel_fp_editor_defaults.h>
  40. #include <dialogs/panel_display_options.h>
  41. #include <dialogs/panel_edit_options.h>
  42. #include <footprint_edit_frame.h>
  43. #include <footprint_editor_settings.h>
  44. #include <footprint_info_impl.h>
  45. #include <footprint_tree_pane.h>
  46. #include <footprint_viewer_frame.h>
  47. #include <fp_lib_table.h>
  48. #include <plugins/kicad/kicad_plugin.h>
  49. #include <kiface_i.h>
  50. #include <kiplatform/app.h>
  51. #include <kiway.h>
  52. #include <panel_hotkeys_editor.h>
  53. #include <pcb_draw_panel_gal.h>
  54. #include <pcb_edit_frame.h>
  55. #include <pcbnew.h>
  56. #include <pcbnew_id.h>
  57. #include <pgm_base.h>
  58. #include <project.h>
  59. #include <settings/settings_manager.h>
  60. #include <tool/action_toolbar.h>
  61. #include <tool/common_control.h>
  62. #include <tool/common_tools.h>
  63. #include <tool/selection.h>
  64. #include <tool/tool_dispatcher.h>
  65. #include <tool/tool_manager.h>
  66. #include <tool/zoom_tool.h>
  67. #include <tools/pcb_editor_conditions.h>
  68. #include <tools/pcb_viewer_tools.h>
  69. #include <tools/group_tool.h>
  70. #include <tools/position_relative_tool.h>
  71. #include <widgets/appearance_controls.h>
  72. #include <widgets/infobar.h>
  73. #include <widgets/lib_tree.h>
  74. #include <widgets/paged_dialog.h>
  75. #include <widgets/panel_selection_filter.h>
  76. #include <widgets/progress_reporter.h>
  77. #include <wildcards_and_files_ext.h>
  78. BEGIN_EVENT_TABLE( FOOTPRINT_EDIT_FRAME, PCB_BASE_FRAME )
  79. EVT_MENU( wxID_CLOSE, FOOTPRINT_EDIT_FRAME::CloseFootprintEditor )
  80. EVT_MENU( wxID_EXIT, FOOTPRINT_EDIT_FRAME::OnExitKiCad )
  81. EVT_SIZE( FOOTPRINT_EDIT_FRAME::OnSize )
  82. EVT_CHOICE( ID_ON_ZOOM_SELECT, FOOTPRINT_EDIT_FRAME::OnSelectZoom )
  83. EVT_CHOICE( ID_ON_GRID_SELECT, FOOTPRINT_EDIT_FRAME::OnSelectGrid )
  84. EVT_TOOL( ID_FPEDIT_SAVE_PNG, FOOTPRINT_EDIT_FRAME::OnSaveFootprintAsPng )
  85. EVT_TOOL( ID_LOAD_FOOTPRINT_FROM_BOARD, FOOTPRINT_EDIT_FRAME::LoadFootprintFromBoard )
  86. EVT_TOOL( ID_ADD_FOOTPRINT_TO_BOARD, FOOTPRINT_EDIT_FRAME::SaveFootprintToBoard )
  87. // Horizontal toolbar
  88. EVT_MENU( ID_GRID_SETTINGS, FOOTPRINT_EDIT_FRAME::OnGridSettings )
  89. EVT_COMBOBOX( ID_TOOLBARH_PCB_SELECT_LAYER, FOOTPRINT_EDIT_FRAME::SelectLayer )
  90. // UI update events.
  91. EVT_UPDATE_UI( ID_LOAD_FOOTPRINT_FROM_BOARD,
  92. FOOTPRINT_EDIT_FRAME::OnUpdateLoadFootprintFromBoard )
  93. EVT_UPDATE_UI( ID_ADD_FOOTPRINT_TO_BOARD,
  94. FOOTPRINT_EDIT_FRAME::OnUpdateSaveFootprintToBoard )
  95. EVT_UPDATE_UI( ID_TOOLBARH_PCB_SELECT_LAYER, FOOTPRINT_EDIT_FRAME::OnUpdateLayerSelectBox )
  96. EVT_UPDATE_UI( ID_GEN_IMPORT_GRAPHICS_FILE, FOOTPRINT_EDIT_FRAME::OnUpdateModuleSelected )
  97. END_EVENT_TABLE()
  98. FOOTPRINT_EDIT_FRAME::FOOTPRINT_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent,
  99. EDA_DRAW_PANEL_GAL::GAL_TYPE aBackend ) :
  100. PCB_BASE_EDIT_FRAME( aKiway, aParent, FRAME_FOOTPRINT_EDITOR, wxEmptyString,
  101. wxDefaultPosition, wxDefaultSize,
  102. KICAD_DEFAULT_DRAWFRAME_STYLE, GetFootprintEditorFrameName() )
  103. {
  104. m_showBorderAndTitleBlock = false; // true to show the frame references
  105. m_canvasType = aBackend;
  106. m_aboutTitle = _( "Footprint Editor" );
  107. m_selLayerBox = nullptr;
  108. m_settings = nullptr;
  109. // Give an icon
  110. wxIcon icon;
  111. icon.CopyFromBitmap( KiBitmap( icon_modedit_xpm ) );
  112. SetIcon( icon );
  113. // Create GAL canvas
  114. if( aBackend == EDA_DRAW_PANEL_GAL::GAL_TYPE_UNKNOWN )
  115. m_canvasType = loadCanvasTypeSetting();
  116. else
  117. m_canvasType = aBackend;
  118. PCB_DRAW_PANEL_GAL* drawPanel = new PCB_DRAW_PANEL_GAL( this, -1, wxPoint( 0, 0 ), m_frameSize,
  119. GetGalDisplayOptions(), m_canvasType );
  120. SetCanvas( drawPanel );
  121. SetBoard( new BOARD() );
  122. // This board will only be used to hold a footprint for editing
  123. GetBoard()->SetBoardUse( BOARD_USE::FPHOLDER );
  124. // In Footprint Editor, the default net clearance is not known (it depends on the actual
  125. // board). So we do not show the default clearance, by setting it to 0. The footprint or
  126. // pad specific clearance will be shown.
  127. GetBoard()->GetDesignSettings().GetDefault()->SetClearance( 0 );
  128. // Don't show the default board solder mask clearance in the footprint editor. Only the
  129. // footprint or pad clearance setting should be shown if it is not 0.
  130. GetBoard()->GetDesignSettings().m_SolderMaskMargin = 0;
  131. // restore the last footprint from the project, if any
  132. restoreLastFootprint();
  133. // Ensure all layers and items are visible:
  134. // In footprint editor, some layers have no meaning or cannot be used, but we show all of
  135. // them, at least to be able to edit a bad layer
  136. GetBoard()->SetVisibleAlls();
  137. // However the "no net" mark on pads is useless, because there are no nets in footprint
  138. // editor: make it non visible.
  139. GetBoard()->SetElementVisibility( LAYER_NO_CONNECTS, false );
  140. GetGalDisplayOptions().m_axesEnabled = true;
  141. // In Footprint Editor, set the default paper size to A4 for plot/print
  142. SetPageSettings( PAGE_INFO( PAGE_INFO::A4 ) );
  143. SetScreen( new PCB_SCREEN( GetPageSettings().GetSizeIU() ) );
  144. // Create the manager and dispatcher & route draw panel events to the dispatcher
  145. setupTools();
  146. setupUIConditions();
  147. initLibraryTree();
  148. m_treePane = new FOOTPRINT_TREE_PANE( this );
  149. ReCreateMenuBar();
  150. ReCreateHToolbar();
  151. ReCreateVToolbar();
  152. ReCreateOptToolbar();
  153. m_selectionFilterPanel = new PANEL_SELECTION_FILTER( this );
  154. m_appearancePanel = new APPEARANCE_CONTROLS( this, GetCanvas(), true );
  155. // LoadSettings() *after* creating m_LayersManager, because LoadSettings() initialize
  156. // parameters in m_LayersManager
  157. // NOTE: KifaceSettings() will return PCBNEW_SETTINGS if we started from pcbnew
  158. LoadSettings( GetSettings() );
  159. // Must be set after calling LoadSettings() to be sure these parameters are not dependent
  160. // on what is read in stored settings. Enable one internal layer, because footprints
  161. // support keepout areas that can be on internal layers only (therefore on the first internal
  162. // layer). This is needed to handle these keepout in internal layers only.
  163. GetBoard()->SetCopperLayerCount( 3 );
  164. GetBoard()->SetEnabledLayers( GetBoard()->GetEnabledLayers().set( In1_Cu ) );
  165. GetBoard()->SetVisibleLayers( GetBoard()->GetEnabledLayers() );
  166. GetBoard()->SetLayerName( In1_Cu, _( "Inner layers" ) );
  167. SetActiveLayer( F_SilkS );
  168. m_auimgr.SetManagedWindow( this );
  169. unsigned int auiFlags = wxAUI_MGR_DEFAULT;
  170. #if !defined( _WIN32 )
  171. // Windows cannot redraw the UI fast enough during a live resize and may lead to all kinds
  172. // of graphical glitches
  173. auiFlags |= wxAUI_MGR_LIVE_RESIZE;
  174. #endif
  175. m_auimgr.SetFlags( auiFlags );
  176. // Rows; layers 4 - 6
  177. m_auimgr.AddPane( m_mainToolBar, EDA_PANE().HToolbar().Name( "MainToolbar" )
  178. .Top().Layer( 6 ) );
  179. m_auimgr.AddPane( m_messagePanel, EDA_PANE().Messages().Name( "MsgPanel" )
  180. .Bottom().Layer( 6 ) );
  181. // Columns; layers 1 - 3
  182. m_auimgr.AddPane( m_optionsToolBar, EDA_PANE().VToolbar().Name( "OptToolbar" )
  183. .Left().Layer( 3 ) );
  184. m_auimgr.AddPane( m_treePane, EDA_PANE().Palette().Name( "Footprints" )
  185. .Left().Layer(2)
  186. .Caption( _( "Libraries" ) )
  187. .MinSize( 250, 400 ).Resizable() );
  188. m_auimgr.AddPane( m_drawToolBar, EDA_PANE().VToolbar().Name( "ToolsToolbar" )
  189. .Right().Layer(2) );
  190. m_auimgr.AddPane( m_appearancePanel, EDA_PANE().Name( "LayersManager" )
  191. .Right().Layer( 3 )
  192. .Caption( _( "Appearance" ) ).PaneBorder( false )
  193. .MinSize( 180, -1 ).BestSize( 180, -1 ) );
  194. m_auimgr.AddPane( m_selectionFilterPanel, EDA_PANE().Palette().Name( "SelectionFilter" )
  195. .Right().Layer( 3 ).Position( 2 )
  196. .Caption( _( "Selection Filter" ) ).PaneBorder( false )
  197. .MinSize( 160, -1 ).BestSize( m_selectionFilterPanel->GetBestSize() ) );
  198. // Center
  199. m_auimgr.AddPane( GetCanvas(), EDA_PANE().Canvas().Name( "DrawFrame" )
  200. .Center() );
  201. // The selection filter doesn't need to grow in the vertical direction when docked
  202. m_auimgr.GetPane( "SelectionFilter" ).dock_proportion = 0;
  203. ActivateGalCanvas();
  204. m_auimgr.GetArtProvider()->SetColour( wxAUI_DOCKART_ACTIVE_CAPTION_TEXT_COLOUR,
  205. wxSystemSettings::GetColour( wxSYS_COLOUR_BTNTEXT ) );
  206. m_auimgr.GetArtProvider()->SetColour( wxAUI_DOCKART_INACTIVE_CAPTION_TEXT_COLOUR,
  207. wxSystemSettings::GetColour( wxSYS_COLOUR_BTNTEXT ) );
  208. // Call Update() to fix all pane default sizes.
  209. m_auimgr.Update();
  210. m_infoBar = new WX_INFOBAR( GetCanvas() );
  211. if( m_settings->m_LibWidth > 0 )
  212. {
  213. wxAuiPaneInfo& treePane = m_auimgr.GetPane( "Footprints" );
  214. // wxAUI hack: force width by setting MinSize() and then Fixed()
  215. // thanks to ZenJu http://trac.wxwidgets.org/ticket/13180
  216. treePane.MinSize( m_settings->m_LibWidth, -1 );
  217. treePane.Fixed();
  218. m_auimgr.Update();
  219. // now make it resizable again
  220. treePane.Resizable();
  221. m_auimgr.Update();
  222. // Note: DO NOT call m_auimgr.Update() anywhere after this; it will nuke the size
  223. // back to minimum.
  224. treePane.MinSize( 250, -1 );
  225. }
  226. // Apply saved visibility stuff at the end
  227. FOOTPRINT_EDITOR_SETTINGS* cfg = GetSettings();
  228. m_appearancePanel->SetUserLayerPresets( cfg->m_LayerPresets );
  229. m_appearancePanel->ApplyLayerPreset( cfg->m_ActiveLayerPreset );
  230. GetToolManager()->RunAction( ACTIONS::zoomFitScreen, false );
  231. updateTitle();
  232. initExitKey();
  233. setupUnits( GetSettings() );
  234. // Default shutdown reason until a file is loaded
  235. KIPLATFORM::APP::SetShutdownBlockReason( this, _( "Footprint changes are unsaved" ) );
  236. // Ensure the window is on top
  237. Raise();
  238. Show( true );
  239. }
  240. FOOTPRINT_EDIT_FRAME::~FOOTPRINT_EDIT_FRAME()
  241. {
  242. // Shutdown all running tools
  243. if( m_toolManager )
  244. m_toolManager->ShutdownAllTools();
  245. // save the footprint in the PROJECT
  246. retainLastFootprint();
  247. delete m_selectionFilterPanel;
  248. delete m_appearancePanel;
  249. }
  250. bool FOOTPRINT_EDIT_FRAME::IsContentModified()
  251. {
  252. return GetScreen() && GetScreen()->IsModify() && GetBoard() && GetBoard()->GetFirstFootprint();
  253. }
  254. SELECTION& FOOTPRINT_EDIT_FRAME::GetCurrentSelection()
  255. {
  256. return m_toolManager->GetTool<PCB_SELECTION_TOOL>()->GetSelection();
  257. }
  258. void FOOTPRINT_EDIT_FRAME::SwitchCanvas( EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType )
  259. {
  260. // switches currently used canvas (Cairo / OpenGL).
  261. PCB_BASE_FRAME::SwitchCanvas( aCanvasType );
  262. GetCanvas()->GetGAL()->SetAxesEnabled( true );
  263. // The base class method *does not reinit* the layers manager. We must upate the layer
  264. // widget to match board visibility states, both layers and render columns, and and some
  265. // settings dependent on the canvas.
  266. UpdateUserInterface();
  267. }
  268. void FOOTPRINT_EDIT_FRAME::HardRedraw()
  269. {
  270. SyncLibraryTree( true );
  271. GetCanvas()->ForceRefresh();
  272. }
  273. void FOOTPRINT_EDIT_FRAME::ToggleSearchTree()
  274. {
  275. wxAuiPaneInfo& treePane = m_auimgr.GetPane( m_treePane );
  276. treePane.Show( !IsSearchTreeShown() );
  277. m_auimgr.Update();
  278. }
  279. bool FOOTPRINT_EDIT_FRAME::IsSearchTreeShown()
  280. {
  281. return m_auimgr.GetPane( m_treePane ).IsShown();
  282. }
  283. BOARD_ITEM_CONTAINER* FOOTPRINT_EDIT_FRAME::GetModel() const
  284. {
  285. return GetBoard()->GetFirstFootprint();
  286. }
  287. LIB_ID FOOTPRINT_EDIT_FRAME::GetTreeFPID() const
  288. {
  289. return m_treePane->GetLibTree()->GetSelectedLibId();
  290. }
  291. LIB_TREE_NODE* FOOTPRINT_EDIT_FRAME::GetCurrentTreeNode() const
  292. {
  293. return m_treePane->GetLibTree()->GetCurrentTreeNode();
  294. }
  295. LIB_ID FOOTPRINT_EDIT_FRAME::GetTargetFPID() const
  296. {
  297. LIB_ID id = GetTreeFPID();
  298. if( id.GetLibNickname().empty() )
  299. return GetLoadedFPID();
  300. return id;
  301. }
  302. LIB_ID FOOTPRINT_EDIT_FRAME::GetLoadedFPID() const
  303. {
  304. FOOTPRINT* footprint = GetBoard()->GetFirstFootprint();
  305. if( footprint )
  306. return LIB_ID( footprint->GetFPID().GetLibNickname(), m_footprintNameWhenLoaded );
  307. else
  308. return LIB_ID();
  309. }
  310. void FOOTPRINT_EDIT_FRAME::ClearModify()
  311. {
  312. if( GetBoard()->GetFirstFootprint() )
  313. m_footprintNameWhenLoaded = GetBoard()->GetFirstFootprint()->GetFPID().GetLibItemName();
  314. GetScreen()->ClrModify();
  315. }
  316. bool FOOTPRINT_EDIT_FRAME::IsCurrentFPFromBoard() const
  317. {
  318. FOOTPRINT* footprint = GetBoard()->GetFirstFootprint();
  319. return ( footprint && footprint->GetLink() != niluuid );
  320. }
  321. void FOOTPRINT_EDIT_FRAME::retainLastFootprint()
  322. {
  323. LIB_ID id = GetLoadedFPID();
  324. if( id.IsValid() )
  325. {
  326. Prj().SetRString( PROJECT::PCB_FOOTPRINT_EDITOR_LIB_NICKNAME, id.GetLibNickname() );
  327. Prj().SetRString( PROJECT::PCB_FOOTPRINT_EDITOR_FP_NAME, id.GetLibItemName() );
  328. }
  329. }
  330. void FOOTPRINT_EDIT_FRAME::restoreLastFootprint()
  331. {
  332. const wxString& footprintName = Prj().GetRString( PROJECT::PCB_FOOTPRINT_EDITOR_FP_NAME );
  333. const wxString& libNickname = Prj().GetRString( PROJECT::PCB_FOOTPRINT_EDITOR_LIB_NICKNAME );
  334. if( libNickname.Length() && footprintName.Length() )
  335. {
  336. LIB_ID id;
  337. id.SetLibNickname( libNickname );
  338. id.SetLibItemName( footprintName );
  339. FOOTPRINT* footprint = loadFootprint( id );
  340. if( footprint )
  341. AddFootprintToBoard( footprint );
  342. }
  343. }
  344. void FOOTPRINT_EDIT_FRAME::AddFootprintToBoard( FOOTPRINT* aFootprint )
  345. {
  346. m_revertModule.reset( (FOOTPRINT*) aFootprint->Clone() );
  347. m_footprintNameWhenLoaded = aFootprint->GetFPID().GetLibItemName();
  348. // Pads are always editable in Footprint Editor
  349. aFootprint->SetPadsLocked( false );
  350. PCB_BASE_EDIT_FRAME::AddFootprintToBoard( aFootprint );
  351. if( IsCurrentFPFromBoard() )
  352. {
  353. wxString msg;
  354. msg.Printf( _( "Editing %s from board. Saving will update the board only." ),
  355. aFootprint->GetReference() );
  356. GetInfoBar()->RemoveAllButtons();
  357. GetInfoBar()->ShowMessage( msg, wxICON_INFORMATION );
  358. }
  359. UpdateMsgPanel();
  360. }
  361. const wxChar* FOOTPRINT_EDIT_FRAME::GetFootprintEditorFrameName()
  362. {
  363. return FOOTPRINT_EDIT_FRAME_NAME;
  364. }
  365. BOARD_DESIGN_SETTINGS& FOOTPRINT_EDIT_FRAME::GetDesignSettings() const
  366. {
  367. return GetBoard()->GetDesignSettings();
  368. }
  369. const PCB_PLOT_PARAMS& FOOTPRINT_EDIT_FRAME::GetPlotSettings() const
  370. {
  371. wxFAIL_MSG( "Plotting not supported in Footprint Editor" );
  372. return PCB_BASE_FRAME::GetPlotSettings();
  373. }
  374. void FOOTPRINT_EDIT_FRAME::SetPlotSettings( const PCB_PLOT_PARAMS& aSettings )
  375. {
  376. wxFAIL_MSG( "Plotting not supported in Footprint Editor" );
  377. }
  378. FOOTPRINT_EDITOR_SETTINGS* FOOTPRINT_EDIT_FRAME::GetSettings()
  379. {
  380. if( !m_settings )
  381. m_settings = Pgm().GetSettingsManager().GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>();
  382. return m_settings;
  383. }
  384. void FOOTPRINT_EDIT_FRAME::LoadSettings( APP_SETTINGS_BASE* aCfg )
  385. {
  386. // aCfg will be the PCBNEW_SETTINGS
  387. FOOTPRINT_EDITOR_SETTINGS* cfg = GetSettings();
  388. PCB_BASE_FRAME::LoadSettings( cfg );
  389. GetDesignSettings() = cfg->m_DesignSettings;
  390. m_displayOptions = cfg->m_Display;
  391. m_defaultLibWidth = cfg->m_LibWidth;
  392. GetToolManager()->GetTool<PCB_SELECTION_TOOL>()->GetFilter() = cfg->m_SelectionFilter;
  393. m_selectionFilterPanel->SetCheckboxesFromFilter( cfg->m_SelectionFilter );
  394. }
  395. void FOOTPRINT_EDIT_FRAME::SaveSettings( APP_SETTINGS_BASE* aCfg )
  396. {
  397. // aCfg will be the PCBNEW_SETTINGS
  398. FOOTPRINT_EDITOR_SETTINGS* cfg = GetSettings();
  399. PCB_BASE_FRAME::SaveSettings( cfg );
  400. cfg->m_DesignSettings = GetDesignSettings();
  401. cfg->m_Display = m_displayOptions;
  402. cfg->m_LibWidth = m_treePane->GetSize().x;
  403. cfg->m_SelectionFilter = GetToolManager()->GetTool<PCB_SELECTION_TOOL>()->GetFilter();
  404. cfg->m_LayerPresets = m_appearancePanel->GetUserLayerPresets();
  405. cfg->m_ActiveLayerPreset = m_appearancePanel->GetActiveLayerPreset();
  406. GetSettingsManager()->SaveColorSettings( GetColorSettings(), "board" );
  407. }
  408. COLOR_SETTINGS* FOOTPRINT_EDIT_FRAME::GetColorSettings()
  409. {
  410. return Pgm().GetSettingsManager().GetColorSettings(
  411. GetFootprintEditorSettings()->m_ColorTheme );
  412. }
  413. MAGNETIC_SETTINGS* FOOTPRINT_EDIT_FRAME::GetMagneticItemsSettings()
  414. {
  415. // Get the actual frame settings for magnetic items
  416. FOOTPRINT_EDITOR_SETTINGS* cfg = GetSettings();
  417. wxCHECK( cfg, nullptr );
  418. return &cfg->m_MagneticItems;
  419. }
  420. const BOX2I FOOTPRINT_EDIT_FRAME::GetDocumentExtents( bool aIncludeAllVisible ) const
  421. {
  422. FOOTPRINT* footprint = GetBoard()->GetFirstFootprint();
  423. if( footprint )
  424. {
  425. bool hasGraphicalItem = footprint->Pads().size() || footprint->Zones().size();
  426. if( !hasGraphicalItem )
  427. {
  428. for( const BOARD_ITEM* item : footprint->GraphicalItems() )
  429. {
  430. if( item->Type() == PCB_FP_TEXT_T )
  431. continue;
  432. hasGraphicalItem = true;
  433. break;
  434. }
  435. }
  436. if( hasGraphicalItem )
  437. {
  438. return footprint->GetFootprintRect();
  439. }
  440. else
  441. {
  442. BOX2I newFootprintBB( { 0, 0 }, { 0, 0 } );
  443. newFootprintBB.Inflate( Millimeter2iu( 12 ) );
  444. return newFootprintBB;
  445. }
  446. }
  447. return GetBoardBoundingBox( false );
  448. }
  449. bool FOOTPRINT_EDIT_FRAME::canCloseWindow( wxCloseEvent& aEvent )
  450. {
  451. if( IsContentModified() )
  452. {
  453. // Shutdown blocks must be determined and vetoed as early as possible
  454. if( KIPLATFORM::APP::SupportsShutdownBlockReason() && aEvent.GetId() == wxEVT_QUERY_END_SESSION )
  455. {
  456. aEvent.Veto();
  457. return false;
  458. }
  459. wxString footprintName = GetBoard()->GetFirstFootprint()->GetFPID().GetLibItemName();
  460. wxString msg = _( "Save changes to \"%s\" before closing?" );
  461. if( !HandleUnsavedChanges( this, wxString::Format( msg, footprintName ),
  462. [&]() -> bool
  463. {
  464. return SaveFootprint( GetBoard()->GetFirstFootprint() );
  465. } ) )
  466. {
  467. aEvent.Veto();
  468. return false;
  469. }
  470. }
  471. return true;
  472. }
  473. void FOOTPRINT_EDIT_FRAME::doCloseWindow()
  474. {
  475. // No more vetos
  476. GetCanvas()->SetEventDispatcher( NULL );
  477. GetCanvas()->StopDrawing();
  478. // Do not show the layer manager during closing to avoid flicker
  479. // on some platforms (Windows) that generate useless redraw of items in
  480. // the Layer Manger
  481. m_auimgr.GetPane( "LayersManager" ).Show( false );
  482. m_auimgr.GetPane( "SelectionFilter" ).Show( false );
  483. Pgm().GetSettingsManager().FlushAndRelease( GetSettings() );
  484. Clear_Pcb( false );
  485. }
  486. void FOOTPRINT_EDIT_FRAME::OnExitKiCad( wxCommandEvent& event )
  487. {
  488. Kiway().OnKiCadExit();
  489. }
  490. void FOOTPRINT_EDIT_FRAME::CloseFootprintEditor( wxCommandEvent& Event )
  491. {
  492. Close();
  493. }
  494. void FOOTPRINT_EDIT_FRAME::OnUpdateModuleSelected( wxUpdateUIEvent& aEvent )
  495. {
  496. aEvent.Enable( GetBoard()->GetFirstFootprint() != NULL );
  497. }
  498. void FOOTPRINT_EDIT_FRAME::OnUpdateLoadFootprintFromBoard( wxUpdateUIEvent& aEvent )
  499. {
  500. PCB_EDIT_FRAME* frame = (PCB_EDIT_FRAME*) Kiway().Player( FRAME_PCB_EDITOR, false );
  501. aEvent.Enable( frame && frame->GetBoard()->GetFirstFootprint() != NULL );
  502. }
  503. void FOOTPRINT_EDIT_FRAME::OnUpdateSaveFootprintToBoard( wxUpdateUIEvent& aEvent )
  504. {
  505. PCB_EDIT_FRAME* frame = (PCB_EDIT_FRAME*) Kiway().Player( FRAME_PCB_EDITOR, false );
  506. FOOTPRINT* editorFootprint = GetBoard()->GetFirstFootprint();
  507. bool canInsert = frame && editorFootprint && editorFootprint->GetLink() == niluuid;
  508. // If the source was deleted, the footprint can inserted but not updated in the board.
  509. if( frame && editorFootprint && editorFootprint->GetLink() != niluuid )
  510. {
  511. BOARD* mainpcb = frame->GetBoard();
  512. canInsert = true;
  513. // search if the source footprint was not deleted:
  514. for( FOOTPRINT* candidate : mainpcb->Footprints() )
  515. {
  516. if( editorFootprint->GetLink() == candidate->m_Uuid )
  517. {
  518. canInsert = false;
  519. break;
  520. }
  521. }
  522. }
  523. aEvent.Enable( canInsert );
  524. }
  525. void FOOTPRINT_EDIT_FRAME::ShowChangedLanguage()
  526. {
  527. // call my base class
  528. PCB_BASE_EDIT_FRAME::ShowChangedLanguage();
  529. // We have 2 panes to update.
  530. // For some obscure reason, the AUI manager hides the first modified pane.
  531. // So force show panes
  532. wxAuiPaneInfo& tree_pane_info = m_auimgr.GetPane( m_treePane );
  533. bool tree_shown = tree_pane_info.IsShown();
  534. tree_pane_info.Caption( _( "Libraries" ) );
  535. wxAuiPaneInfo& lm_pane_info = m_auimgr.GetPane( m_appearancePanel );
  536. bool lm_shown = lm_pane_info.IsShown();
  537. lm_pane_info.Caption( _( "Appearance" ) );
  538. // update the layer manager
  539. m_appearancePanel->OnBoardChanged();
  540. UpdateUserInterface();
  541. // Now restore the visibility:
  542. lm_pane_info.Show( lm_shown );
  543. tree_pane_info.Show( tree_shown );
  544. m_auimgr.Update();
  545. }
  546. void FOOTPRINT_EDIT_FRAME::OnModify()
  547. {
  548. PCB_BASE_FRAME::OnModify();
  549. Update3DView( true );
  550. m_treePane->GetLibTree()->RefreshLibTree();
  551. }
  552. void FOOTPRINT_EDIT_FRAME::updateTitle()
  553. {
  554. wxString title;
  555. LIB_ID fpid = GetLoadedFPID();
  556. FOOTPRINT* footprint = GetBoard()->GetFirstFootprint();
  557. bool writable = true;
  558. if( IsCurrentFPFromBoard() )
  559. {
  560. title += wxString::Format( _( "%s [from %s.%s]" ) + wxT( " \u2014 " ),
  561. footprint->GetReference(),
  562. Prj().GetProjectName(),
  563. PcbFileExtension );
  564. }
  565. else if( fpid.IsValid() )
  566. {
  567. try
  568. {
  569. writable = Prj().PcbFootprintLibs()->IsFootprintLibWritable( fpid.GetLibNickname() );
  570. }
  571. catch( const IO_ERROR& )
  572. {
  573. // best efforts...
  574. }
  575. // Note: don't used GetLoadedFPID(); footprint name may have been edited
  576. title += wxString::Format( wxT( "%s %s\u2014 " ),
  577. FROM_UTF8( footprint->GetFPID().Format().c_str() ),
  578. writable ? wxString( wxEmptyString )
  579. : _( "[Read Only]" ) + wxS( " " ) );
  580. }
  581. else if( !fpid.GetLibItemName().empty() )
  582. {
  583. // Note: don't used GetLoadedFPID(); footprint name may have been edited
  584. title += wxString::Format( wxT( "%s %s \u2014 " ),
  585. FROM_UTF8( footprint->GetFPID().GetLibItemName().c_str() ),
  586. _( "[Unsaved]" ) );
  587. }
  588. title += _( "Footprint Editor" );
  589. SetTitle( title );
  590. }
  591. void FOOTPRINT_EDIT_FRAME::UpdateUserInterface()
  592. {
  593. m_appearancePanel->OnBoardChanged();
  594. }
  595. void FOOTPRINT_EDIT_FRAME::UpdateView()
  596. {
  597. GetCanvas()->UpdateColors();
  598. GetCanvas()->DisplayBoard( GetBoard() );
  599. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  600. updateTitle();
  601. }
  602. void FOOTPRINT_EDIT_FRAME::initLibraryTree()
  603. {
  604. FP_LIB_TABLE* fpTable = Prj().PcbFootprintLibs();
  605. WX_PROGRESS_REPORTER progressReporter( this, _( "Loading Footprint Libraries" ), 2 );
  606. GFootprintList.ReadFootprintFiles( fpTable, NULL, &progressReporter );
  607. progressReporter.Show( false );
  608. if( GFootprintList.GetErrorCount() )
  609. GFootprintList.DisplayErrors( this );
  610. m_adapter = FP_TREE_SYNCHRONIZING_ADAPTER::Create( this, fpTable );
  611. auto adapter = static_cast<FP_TREE_SYNCHRONIZING_ADAPTER*>( m_adapter.get() );
  612. adapter->AddLibraries();
  613. }
  614. void FOOTPRINT_EDIT_FRAME::SyncLibraryTree( bool aProgress )
  615. {
  616. FP_LIB_TABLE* fpTable = Prj().PcbFootprintLibs();
  617. auto adapter = static_cast<FP_TREE_SYNCHRONIZING_ADAPTER*>( m_adapter.get() );
  618. LIB_ID target = GetTargetFPID();
  619. bool targetSelected = ( target == m_treePane->GetLibTree()->GetSelectedLibId() );
  620. // Sync FOOTPRINT_INFO list to the libraries on disk
  621. if( aProgress )
  622. {
  623. WX_PROGRESS_REPORTER progressReporter( this, _( "Updating Footprint Libraries" ), 2 );
  624. GFootprintList.ReadFootprintFiles( fpTable, NULL, &progressReporter );
  625. progressReporter.Show( false );
  626. }
  627. else
  628. {
  629. GFootprintList.ReadFootprintFiles( fpTable, NULL, NULL );
  630. }
  631. // Sync the LIB_TREE to the FOOTPRINT_INFO list
  632. adapter->Sync();
  633. m_treePane->GetLibTree()->Unselect();
  634. m_treePane->GetLibTree()->Regenerate( true );
  635. if( target.IsValid() )
  636. {
  637. if( adapter->FindItem( target ) )
  638. {
  639. if( targetSelected )
  640. m_treePane->GetLibTree()->SelectLibId( target );
  641. else
  642. m_treePane->GetLibTree()->CenterLibId( target );
  643. }
  644. else
  645. {
  646. // Try to focus on parent
  647. target.SetLibItemName( wxEmptyString );
  648. m_treePane->GetLibTree()->CenterLibId( target );
  649. }
  650. }
  651. }
  652. void FOOTPRINT_EDIT_FRAME::RegenerateLibraryTree()
  653. {
  654. LIB_ID target = GetTargetFPID();
  655. m_treePane->GetLibTree()->Regenerate( true );
  656. if( target.IsValid() )
  657. m_treePane->GetLibTree()->CenterLibId( target );
  658. }
  659. void FOOTPRINT_EDIT_FRAME::RefreshLibraryTree()
  660. {
  661. m_treePane->GetLibTree()->RefreshLibTree();
  662. }
  663. void FOOTPRINT_EDIT_FRAME::FocusOnLibID( const LIB_ID& aLibID )
  664. {
  665. m_treePane->GetLibTree()->SelectLibId( aLibID );
  666. }
  667. void FOOTPRINT_EDIT_FRAME::OnUpdateLayerAlpha( wxUpdateUIEvent & )
  668. {
  669. m_appearancePanel->OnLayerAlphaChanged();
  670. }
  671. void FOOTPRINT_EDIT_FRAME::InstallPreferences( PAGED_DIALOG* aParent,
  672. PANEL_HOTKEYS_EDITOR* aHotkeysPanel )
  673. {
  674. wxTreebook* book = aParent->GetTreebook();
  675. book->AddPage( new wxPanel( book ), _( "Footprint Editor" ) );
  676. book->AddSubPage( new PANEL_DISPLAY_OPTIONS( this, aParent ), _( "Display Options" ) );
  677. book->AddSubPage( new PANEL_FP_EDITOR_COLOR_SETTINGS( this, book ), _( "Colors" ) );
  678. book->AddSubPage( new PANEL_EDIT_OPTIONS( this, aParent ), _( "Editing Options" ) );
  679. book->AddSubPage( new PANEL_FP_EDITOR_DEFAULTS( this, aParent ), _( "Default Values" ) );
  680. aHotkeysPanel->AddHotKeys( GetToolManager() );
  681. }
  682. void FOOTPRINT_EDIT_FRAME::setupTools()
  683. {
  684. // Create the manager and dispatcher & route draw panel events to the dispatcher
  685. m_toolManager = new TOOL_MANAGER;
  686. m_toolManager->SetEnvironment( GetBoard(), GetCanvas()->GetView(),
  687. GetCanvas()->GetViewControls(), config(), this );
  688. m_actions = new PCB_ACTIONS();
  689. m_toolDispatcher = new TOOL_DISPATCHER( m_toolManager, m_actions );
  690. GetCanvas()->SetEventDispatcher( m_toolDispatcher );
  691. m_toolManager->RegisterTool( new COMMON_CONTROL );
  692. m_toolManager->RegisterTool( new COMMON_TOOLS );
  693. m_toolManager->RegisterTool( new PCB_SELECTION_TOOL );
  694. m_toolManager->RegisterTool( new ZOOM_TOOL );
  695. m_toolManager->RegisterTool( new EDIT_TOOL );
  696. m_toolManager->RegisterTool( new PAD_TOOL );
  697. m_toolManager->RegisterTool( new DRAWING_TOOL );
  698. m_toolManager->RegisterTool( new POINT_EDITOR );
  699. m_toolManager->RegisterTool( new PCB_CONTROL ); // copy/paste
  700. m_toolManager->RegisterTool( new FOOTPRINT_EDITOR_TOOLS );
  701. m_toolManager->RegisterTool( new ALIGN_DISTRIBUTE_TOOL );
  702. m_toolManager->RegisterTool( new PCB_PICKER_TOOL );
  703. m_toolManager->RegisterTool( new POSITION_RELATIVE_TOOL );
  704. m_toolManager->RegisterTool( new PCB_VIEWER_TOOLS );
  705. m_toolManager->RegisterTool( new GROUP_TOOL );
  706. m_toolManager->RegisterTool( new CONVERT_TOOL );
  707. m_toolManager->GetTool<PCB_SELECTION_TOOL>()->SetIsFootprintEditor( true );
  708. m_toolManager->GetTool<EDIT_TOOL>()->SetIsFootprintEditor( true );
  709. m_toolManager->GetTool<PAD_TOOL>()->SetIsFootprintEditor( true );
  710. m_toolManager->GetTool<DRAWING_TOOL>()->SetIsFootprintEditor( true );
  711. m_toolManager->GetTool<POINT_EDITOR>()->SetIsFootprintEditor( true );
  712. m_toolManager->GetTool<PCB_CONTROL>()->SetIsFootprintEditor( true );
  713. m_toolManager->GetTool<PCB_PICKER_TOOL>()->SetIsFootprintEditor( true );
  714. m_toolManager->GetTool<POSITION_RELATIVE_TOOL>()->SetIsFootprintEditor( true );
  715. m_toolManager->GetTool<GROUP_TOOL>()->SetIsFootprintEditor( true );
  716. m_toolManager->GetTool<PCB_VIEWER_TOOLS>()->SetFootprintFrame( true );
  717. m_toolManager->InitTools();
  718. m_toolManager->InvokeTool( "pcbnew.InteractiveSelection" );
  719. }
  720. void FOOTPRINT_EDIT_FRAME::setupUIConditions()
  721. {
  722. PCB_BASE_EDIT_FRAME::setupUIConditions();
  723. ACTION_MANAGER* mgr = m_toolManager->GetActionManager();
  724. PCB_EDITOR_CONDITIONS cond( this );
  725. wxASSERT( mgr );
  726. #define ENABLE( x ) ACTION_CONDITIONS().Enable( x )
  727. #define CHECK( x ) ACTION_CONDITIONS().Check( x )
  728. auto haveFootprintCond =
  729. [this]( const SELECTION& )
  730. {
  731. return GetBoard()->GetFirstFootprint() != nullptr;
  732. };
  733. auto footprintTargettedCond =
  734. [this] ( const SELECTION& )
  735. {
  736. return !GetTargetFPID().GetLibItemName().empty();
  737. };
  738. mgr->SetConditions( ACTIONS::saveAs, ENABLE( footprintTargettedCond ) );
  739. mgr->SetConditions( ACTIONS::revert, ENABLE( cond.ContentModified() ) );
  740. mgr->SetConditions( PCB_ACTIONS::saveToBoard, ENABLE( cond.ContentModified() ) );
  741. mgr->SetConditions( PCB_ACTIONS::saveToLibrary, ENABLE( cond.ContentModified() ) );
  742. mgr->SetConditions( ACTIONS::undo, ENABLE( cond.UndoAvailable() ) );
  743. mgr->SetConditions( ACTIONS::redo, ENABLE( cond.RedoAvailable() ) );
  744. mgr->SetConditions( ACTIONS::toggleGrid, CHECK( cond.GridVisible() ) );
  745. mgr->SetConditions( ACTIONS::toggleCursorStyle, CHECK( cond.FullscreenCursor() ) );
  746. mgr->SetConditions( ACTIONS::millimetersUnits, CHECK( cond.Units( EDA_UNITS::MILLIMETRES ) ) );
  747. mgr->SetConditions( ACTIONS::inchesUnits, CHECK( cond.Units( EDA_UNITS::INCHES ) ) );
  748. mgr->SetConditions( ACTIONS::milsUnits, CHECK( cond.Units( EDA_UNITS::MILS ) ) );
  749. mgr->SetConditions( ACTIONS::acceleratedGraphics, CHECK( cond.CanvasType( EDA_DRAW_PANEL_GAL::GAL_TYPE_OPENGL ) ) );
  750. mgr->SetConditions( ACTIONS::standardGraphics, CHECK( cond.CanvasType( EDA_DRAW_PANEL_GAL::GAL_TYPE_CAIRO ) ) );
  751. mgr->SetConditions( ACTIONS::cut, ENABLE( SELECTION_CONDITIONS::NotEmpty ) );
  752. mgr->SetConditions( ACTIONS::copy, ENABLE( SELECTION_CONDITIONS::NotEmpty ) );
  753. mgr->SetConditions( ACTIONS::paste, ENABLE( SELECTION_CONDITIONS::Idle && cond.NoActiveTool() ) );
  754. mgr->SetConditions( ACTIONS::pasteSpecial, ENABLE( SELECTION_CONDITIONS::Idle && cond.NoActiveTool() ) );
  755. mgr->SetConditions( ACTIONS::doDelete, ENABLE( SELECTION_CONDITIONS::NotEmpty ) );
  756. mgr->SetConditions( ACTIONS::duplicate, ENABLE( SELECTION_CONDITIONS::NotEmpty ) );
  757. mgr->SetConditions( ACTIONS::selectAll, ENABLE( cond.HasItems() ) );
  758. mgr->SetConditions( PCB_ACTIONS::padDisplayMode, CHECK( !cond.PadFillDisplay() ) );
  759. mgr->SetConditions( PCB_ACTIONS::textOutlines, CHECK( !cond.TextFillDisplay() ) );
  760. mgr->SetConditions( PCB_ACTIONS::graphicsOutlines, CHECK( !cond.GraphicsFillDisplay() ) );
  761. mgr->SetConditions( ACTIONS::zoomTool, CHECK( cond.CurrentTool( ACTIONS::zoomTool ) ) );
  762. mgr->SetConditions( ACTIONS::selectionTool, CHECK( cond.CurrentTool( ACTIONS::selectionTool ) ) );
  763. auto highContrastCond =
  764. [this] ( const SELECTION& )
  765. {
  766. return GetDisplayOptions().m_ContrastModeDisplay != HIGH_CONTRAST_MODE::NORMAL;
  767. };
  768. auto footprintTreeCond =
  769. [this] (const SELECTION& )
  770. {
  771. return IsSearchTreeShown();
  772. };
  773. mgr->SetConditions( ACTIONS::highContrastMode, CHECK( highContrastCond ) );
  774. mgr->SetConditions( PCB_ACTIONS::toggleFootprintTree, CHECK( footprintTreeCond ) );
  775. mgr->SetConditions( ACTIONS::print, ENABLE( haveFootprintCond ) );
  776. mgr->SetConditions( PCB_ACTIONS::exportFootprint, ENABLE( haveFootprintCond ) );
  777. mgr->SetConditions( PCB_ACTIONS::footprintProperties, ENABLE( haveFootprintCond ) );
  778. mgr->SetConditions( PCB_ACTIONS::cleanupGraphics, ENABLE( haveFootprintCond ) );
  779. // Only enable a tool if the part is edtable
  780. #define CURRENT_EDIT_TOOL( action ) mgr->SetConditions( action, \
  781. ACTION_CONDITIONS().Enable( haveFootprintCond ).Check( cond.CurrentTool( action ) ) )
  782. CURRENT_EDIT_TOOL( ACTIONS::deleteTool );
  783. CURRENT_EDIT_TOOL( ACTIONS::measureTool );
  784. CURRENT_EDIT_TOOL( PCB_ACTIONS::placePad );
  785. CURRENT_EDIT_TOOL( PCB_ACTIONS::drawLine );
  786. CURRENT_EDIT_TOOL( PCB_ACTIONS::drawRectangle );
  787. CURRENT_EDIT_TOOL( PCB_ACTIONS::drawCircle );
  788. CURRENT_EDIT_TOOL( PCB_ACTIONS::drawArc );
  789. CURRENT_EDIT_TOOL( PCB_ACTIONS::drawPolygon );
  790. CURRENT_EDIT_TOOL( PCB_ACTIONS::drawRuleArea );
  791. CURRENT_EDIT_TOOL( PCB_ACTIONS::placeText );
  792. CURRENT_EDIT_TOOL( PCB_ACTIONS::setAnchor );
  793. CURRENT_EDIT_TOOL( PCB_ACTIONS::gridSetOrigin );
  794. #undef CURRENT_EDIT_TOOL
  795. #undef ENABLE
  796. #undef CHECK
  797. }
  798. void FOOTPRINT_EDIT_FRAME::ActivateGalCanvas()
  799. {
  800. PCB_BASE_EDIT_FRAME::ActivateGalCanvas();
  801. // Be sure the axis are enabled
  802. GetCanvas()->GetGAL()->SetAxesEnabled( true );
  803. UpdateView();
  804. // Ensure the m_Layers settings are using the canvas type:
  805. UpdateUserInterface();
  806. }
  807. void FOOTPRINT_EDIT_FRAME::CommonSettingsChanged( bool aEnvVarsChanged, bool aTextVarsChanged )
  808. {
  809. PCB_BASE_EDIT_FRAME::CommonSettingsChanged( aEnvVarsChanged, aTextVarsChanged );
  810. GetCanvas()->GetView()->UpdateAllLayersColor();
  811. GetCanvas()->ForceRefresh();
  812. UpdateUserInterface();
  813. if( aEnvVarsChanged )
  814. SyncLibraryTree( true );
  815. Layout();
  816. SendSizeEvent();
  817. }
  818. void FOOTPRINT_EDIT_FRAME::OnSaveFootprintAsPng( wxCommandEvent& event )
  819. {
  820. wxString fullFileName;
  821. LIB_ID id = GetLoadedFPID();
  822. if( id.empty() )
  823. {
  824. wxMessageBox( _( "No footprint selected." ) );
  825. return;
  826. }
  827. wxFileName fn( id.GetLibItemName() );
  828. fn.SetExt( "png" );
  829. wxString projectPath = wxPathOnly( Prj().GetProjectFullName() );
  830. wxFileDialog dlg( this, _( "Footprint Image File Name" ), projectPath,
  831. fn.GetFullName(), PngFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
  832. if( dlg.ShowModal() == wxID_CANCEL || dlg.GetPath().IsEmpty() )
  833. return;
  834. // calling wxYield is mandatory under Linux, after closing the file selector dialog
  835. // to refresh the screen before creating the PNG or JPEG image from screen
  836. wxYield();
  837. SaveCanvasImageToFile( this, dlg.GetPath() );
  838. }