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.

854 lines
26 KiB

* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
17 years ago
12 years ago
12 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 <fctsys.h>
  23. #include <kiface_i.h>
  24. #include <kiway.h>
  25. #include <project.h>
  26. #include <kicad_plugin.h>
  27. #include <pcb_draw_panel_gal.h>
  28. #include <confirm.h>
  29. #include <pcb_edit_frame.h>
  30. #include <3d_viewer/eda_3d_viewer.h>
  31. #include <fp_lib_table.h>
  32. #include <bitmaps.h>
  33. #include <class_board.h>
  34. #include <class_module.h>
  35. #include <pcbnew.h>
  36. #include <pcbnew_id.h>
  37. #include <footprint_edit_frame.h>
  38. #include <footprint_viewer_frame.h>
  39. #include <wildcards_and_files_ext.h>
  40. #include <pcb_layer_widget.h>
  41. #include <tool/tool_manager.h>
  42. #include <tool/common_control.h>
  43. #include <tool/common_tools.h>
  44. #include <tool/tool_dispatcher.h>
  45. #include <tool/action_toolbar.h>
  46. #include <tool/zoom_tool.h>
  47. #include <footprint_tree_pane.h>
  48. #include <widgets/lib_tree.h>
  49. #include <footprint_info_impl.h>
  50. #include <widgets/paged_dialog.h>
  51. #include <dialogs/panel_modedit_settings.h>
  52. #include <dialogs/panel_modedit_defaults.h>
  53. #include <dialogs/panel_modedit_display_options.h>
  54. #include <panel_hotkeys_editor.h>
  55. #include <tools/position_relative_tool.h>
  56. #include <widgets/progress_reporter.h>
  57. #include "tools/selection_tool.h"
  58. #include "tools/edit_tool.h"
  59. #include "tools/drawing_tool.h"
  60. #include "tools/point_editor.h"
  61. #include "tools/pcbnew_control.h"
  62. #include "tools/footprint_editor_tools.h"
  63. #include "tools/placement_tool.h"
  64. #include "tools/pcbnew_picker_tool.h"
  65. #include "tools/pad_tool.h"
  66. #include "tools/pcb_actions.h"
  67. BEGIN_EVENT_TABLE( FOOTPRINT_EDIT_FRAME, PCB_BASE_FRAME )
  68. EVT_CLOSE( FOOTPRINT_EDIT_FRAME::OnCloseWindow )
  69. EVT_MENU( wxID_CLOSE, FOOTPRINT_EDIT_FRAME::CloseModuleEditor )
  70. EVT_MENU( wxID_EXIT, FOOTPRINT_EDIT_FRAME::OnExitKiCad )
  71. EVT_SIZE( FOOTPRINT_EDIT_FRAME::OnSize )
  72. EVT_CHOICE( ID_ON_ZOOM_SELECT, FOOTPRINT_EDIT_FRAME::OnSelectZoom )
  73. EVT_CHOICE( ID_ON_GRID_SELECT, FOOTPRINT_EDIT_FRAME::OnSelectGrid )
  74. EVT_TOOL( ID_MODEDIT_SAVE_PNG, FOOTPRINT_EDIT_FRAME::OnSaveFootprintAsPng )
  75. EVT_TOOL( ID_MODEDIT_CHECK, FOOTPRINT_EDIT_FRAME::Process_Special_Functions )
  76. EVT_TOOL( ID_MODEDIT_LOAD_MODULE_FROM_BOARD, FOOTPRINT_EDIT_FRAME::LoadModuleFromBoard )
  77. EVT_TOOL( ID_ADD_FOOTPRINT_TO_BOARD, FOOTPRINT_EDIT_FRAME::Process_Special_Functions )
  78. // Horizontal toolbar
  79. EVT_MENU( ID_GRID_SETTINGS, FOOTPRINT_EDIT_FRAME::OnGridSettings )
  80. EVT_COMBOBOX( ID_TOOLBARH_PCB_SELECT_LAYER, FOOTPRINT_EDIT_FRAME::Process_Special_Functions )
  81. // UI update events.
  82. EVT_UPDATE_UI( ID_MODEDIT_LOAD_MODULE_FROM_BOARD,
  83. FOOTPRINT_EDIT_FRAME::OnUpdateLoadModuleFromBoard )
  84. EVT_UPDATE_UI( ID_ADD_FOOTPRINT_TO_BOARD,
  85. FOOTPRINT_EDIT_FRAME::OnUpdateInsertModuleInBoard )
  86. EVT_UPDATE_UI( ID_TOOLBARH_PCB_SELECT_LAYER, FOOTPRINT_EDIT_FRAME::OnUpdateLayerSelectBox )
  87. EVT_UPDATE_UI( ID_GEN_IMPORT_GRAPHICS_FILE, FOOTPRINT_EDIT_FRAME::OnUpdateModuleSelected )
  88. END_EVENT_TABLE()
  89. static const wxChar defaultLibWidthEntry[] = wxT( "ModeditLibWidth" );
  90. FOOTPRINT_EDIT_FRAME::FOOTPRINT_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent,
  91. EDA_DRAW_PANEL_GAL::GAL_TYPE aBackend ) :
  92. PCB_BASE_EDIT_FRAME( aKiway, aParent, FRAME_FOOTPRINT_EDITOR, wxEmptyString,
  93. wxDefaultPosition, wxDefaultSize,
  94. KICAD_DEFAULT_DRAWFRAME_STYLE, GetFootprintEditorFrameName() )
  95. {
  96. m_showBorderAndTitleBlock = false; // true to show the frame references
  97. m_FrameSize = ConvertDialogToPixels( wxSize( 500, 350 ) ); // default in case of no prefs
  98. m_canvasType = aBackend;
  99. m_AboutTitle = "ModEdit";
  100. m_selLayerBox = nullptr;
  101. // Give an icon
  102. wxIcon icon;
  103. icon.CopyFromBitmap( KiBitmap( icon_modedit_xpm ) );
  104. SetIcon( icon );
  105. // Create GAL canvas
  106. if( aBackend == EDA_DRAW_PANEL_GAL::GAL_TYPE_UNKNOWN )
  107. m_canvasType = LoadCanvasTypeSetting();
  108. else
  109. m_canvasType = aBackend;
  110. PCB_DRAW_PANEL_GAL* drawPanel = new PCB_DRAW_PANEL_GAL( this, -1, wxPoint( 0, 0 ), m_FrameSize,
  111. GetGalDisplayOptions(), m_canvasType );
  112. SetCanvas( drawPanel );
  113. SetBoard( new BOARD() );
  114. // Enable one internal layer, because footprints support keepout areas that
  115. // can be on internal layers only (therefore on the first internal layer)
  116. // This is needed to handle these keepout in internal layers only
  117. GetBoard()->SetEnabledLayers( GetBoard()->GetEnabledLayers().set( In1_Cu ) );
  118. GetBoard()->SetVisibleLayers( GetBoard()->GetEnabledLayers() );
  119. GetBoard()->SetLayerName( In1_Cu, _( "Inner layers" ) );
  120. // In modedit, the default net clearance is not known.
  121. // (it depends on the actual board)
  122. // So we do not show the default clearance, by setting it to 0
  123. // The footprint or pad specific clearance will be shown
  124. GetBoard()->GetDesignSettings().GetDefault()->SetClearance( 0 );
  125. // Don't show the default board solder mask clearance in the footprint editor. Only the
  126. // footprint or pad clearance setting should be shown if it is not 0.
  127. GetBoard()->GetDesignSettings().m_SolderMaskMargin = 0;
  128. // restore the last footprint from the project, if any
  129. restoreLastFootprint();
  130. // Ensure all layers and items are visible:
  131. // In footprint editor, some layers have no meaning or
  132. // cannot be used, but we show all of them, at least to be able
  133. // to edit a bad layer
  134. GetBoard()->SetVisibleAlls();
  135. // However the "no net" mark on pads is useless, because there is
  136. // no net in footprint editor: make it non visible
  137. GetBoard()->SetElementVisibility( LAYER_NO_CONNECTS, false );
  138. m_Layers = new PCB_LAYER_WIDGET( this, GetCanvas(), true );
  139. // LoadSettings() *after* creating m_LayersManager, because LoadSettings()
  140. // initialize parameters in m_LayersManager
  141. LoadSettings( config() );
  142. GetGalDisplayOptions().m_axesEnabled = true;
  143. SetScreen( new PCB_SCREEN( GetPageSettings().GetSizeIU() ) );
  144. GetScreen()->SetMaxUndoItems( m_UndoRedoCountMax );
  145. GetScreen()->AddGrid( m_UserGridSize, EDA_UNITS_T::UNSCALED_UNITS, ID_POPUP_GRID_USER );
  146. GetScreen()->SetGrid( ID_POPUP_GRID_LEVEL_1000 + m_LastGridSizeId );
  147. // In modedit, set the default paper size to A4:
  148. // this should be OK for all footprint to plot/print
  149. SetPageSettings( PAGE_INFO( PAGE_INFO::A4 ) );
  150. SetSize( m_FramePos.x, m_FramePos.y, m_FrameSize.x, m_FrameSize.y );
  151. // Create the manager and dispatcher & route draw panel events to the dispatcher
  152. setupTools();
  153. initLibraryTree();
  154. m_treePane = new FOOTPRINT_TREE_PANE( this );
  155. ReCreateMenuBar();
  156. ReCreateHToolbar();
  157. ReCreateVToolbar();
  158. ReCreateOptToolbar();
  159. m_Layers->ReFill();
  160. m_Layers->ReFillRender();
  161. GetScreen()->m_Active_Layer = F_SilkS;
  162. m_Layers->SelectLayer( F_SilkS );
  163. m_Layers->OnLayerSelected();
  164. m_auimgr.SetManagedWindow( this );
  165. // Horizontal items; layers 4 - 6
  166. m_auimgr.AddPane( m_mainToolBar, EDA_PANE().HToolbar().Name( "MainToolbar" ).Top().Layer(6) );
  167. m_auimgr.AddPane( m_messagePanel, EDA_PANE().Messages().Name( "MsgPanel" ).Bottom().Layer(6) );
  168. // Vertical items; layers 1 - 3
  169. m_auimgr.AddPane( m_optionsToolBar, EDA_PANE().VToolbar().Name( "OptToolbar" ).Left().Layer(3) );
  170. m_auimgr.AddPane( m_treePane, EDA_PANE().Palette().Name( "Footprints" ).Left().Layer(1)
  171. .Caption( _( "Libraries" ) ).MinSize( 250, 400 )
  172. .BestSize( m_defaultLibWidth, -1 ) );
  173. m_auimgr.AddPane( m_drawToolBar, EDA_PANE().VToolbar().Name( "ToolsToolbar" ).Right().Layer(1) );
  174. m_auimgr.AddPane( m_Layers, EDA_PANE().Palette().Name( "LayersManager" ).Right().Layer(3)
  175. .Caption( _( "Layers Manager" ) ).PaneBorder( false )
  176. .MinSize( 80, -1 ).BestSize( m_Layers->GetBestSize() ) );
  177. m_auimgr.AddPane( GetCanvas(), EDA_PANE().Canvas().Name( "DrawFrame" ).Center() );
  178. GetCanvas()->GetView()->SetScale( GetZoomLevelCoeff() / GetScreen()->GetZoom() );
  179. ActivateGalCanvas();
  180. m_auimgr.Update();
  181. GetToolManager()->RunAction( ACTIONS::gridPreset, true, m_LastGridSizeId );
  182. GetToolManager()->RunAction( ACTIONS::zoomFitScreen, false );
  183. updateTitle();
  184. InitExitKey();
  185. Raise(); // On some window managers, this is needed
  186. Show( true );
  187. }
  188. FOOTPRINT_EDIT_FRAME::~FOOTPRINT_EDIT_FRAME()
  189. {
  190. // save the footprint in the PROJECT
  191. retainLastFootprint();
  192. delete m_Layers;
  193. }
  194. void FOOTPRINT_EDIT_FRAME::SwitchCanvas( EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType )
  195. {
  196. // switches currently used canvas (Cairo / OpenGL).
  197. PCB_BASE_FRAME::SwitchCanvas( aCanvasType );
  198. GetCanvas()->GetGAL()->SetAxesEnabled( true );
  199. GetCanvas()->GetGAL()->SetGridSize( VECTOR2D( GetScreen()->GetGridSize() ) );
  200. // The base class method *does not reinit* the layers manager. We must upate the layer
  201. // widget to match board visibility states, both layers and render columns, and and some
  202. // settings dependent on the canvas.
  203. UpdateUserInterface();
  204. }
  205. void FOOTPRINT_EDIT_FRAME::HardRedraw()
  206. {
  207. SyncLibraryTree( true );
  208. GetCanvas()->ForceRefresh();
  209. }
  210. void FOOTPRINT_EDIT_FRAME::ToggleSearchTree()
  211. {
  212. auto& treePane = m_auimgr.GetPane( m_treePane );
  213. treePane.Show( !IsSearchTreeShown() );
  214. m_auimgr.Update();
  215. }
  216. bool FOOTPRINT_EDIT_FRAME::IsSearchTreeShown()
  217. {
  218. return m_auimgr.GetPane( m_treePane ).IsShown();
  219. }
  220. BOARD_ITEM_CONTAINER* FOOTPRINT_EDIT_FRAME::GetModel() const
  221. {
  222. return GetBoard()->GetFirstModule();
  223. }
  224. LIB_ID FOOTPRINT_EDIT_FRAME::GetTreeFPID() const
  225. {
  226. return m_treePane->GetLibTree()->GetSelectedLibId();
  227. }
  228. LIB_ID FOOTPRINT_EDIT_FRAME::GetTargetFPID() const
  229. {
  230. LIB_ID id = GetTreeFPID();
  231. if( id.GetLibNickname().empty() )
  232. return GetLoadedFPID();
  233. return id;
  234. }
  235. LIB_ID FOOTPRINT_EDIT_FRAME::GetLoadedFPID() const
  236. {
  237. MODULE* module = GetBoard()->GetFirstModule();
  238. if( module )
  239. return LIB_ID( module->GetFPID().GetLibNickname(), m_footprintNameWhenLoaded );
  240. else
  241. return LIB_ID();
  242. }
  243. bool FOOTPRINT_EDIT_FRAME::IsCurrentFPFromBoard() const
  244. {
  245. MODULE* module = GetBoard()->GetFirstModule();
  246. return ( module && module->GetLink() > 0 );
  247. }
  248. void FOOTPRINT_EDIT_FRAME::retainLastFootprint()
  249. {
  250. LIB_ID id = GetLoadedFPID();
  251. if( id.IsValid() )
  252. {
  253. Prj().SetRString( PROJECT::PCB_FOOTPRINT_EDITOR_NICKNAME, id.GetLibNickname() );
  254. Prj().SetRString( PROJECT::PCB_FOOTPRINT_EDITOR_FPNAME, id.GetLibItemName() );
  255. }
  256. }
  257. void FOOTPRINT_EDIT_FRAME::restoreLastFootprint()
  258. {
  259. const wxString& curFootprintName = Prj().GetRString( PROJECT::PCB_FOOTPRINT_EDITOR_FPNAME );
  260. const wxString& curNickname = Prj().GetRString( PROJECT::PCB_FOOTPRINT_EDITOR_NICKNAME );
  261. if( curNickname.Length() && curFootprintName.Length() )
  262. {
  263. LIB_ID id;
  264. id.SetLibNickname( curNickname );
  265. id.SetLibItemName( curFootprintName );
  266. MODULE* module = loadFootprint( id );
  267. if( module )
  268. AddModuleToBoard( module );
  269. }
  270. }
  271. void FOOTPRINT_EDIT_FRAME::AddModuleToBoard( MODULE* aFootprint )
  272. {
  273. m_revertModule.reset( (MODULE*) aFootprint->Clone() );
  274. m_footprintNameWhenLoaded = aFootprint->GetFPID().GetLibItemName();
  275. // Pads are always editable in Footprint Editor
  276. aFootprint->SetPadsLocked( false );
  277. PCB_BASE_EDIT_FRAME::AddModuleToBoard( aFootprint );
  278. UpdateMsgPanel();
  279. }
  280. const wxChar* FOOTPRINT_EDIT_FRAME::GetFootprintEditorFrameName()
  281. {
  282. return FOOTPRINT_EDIT_FRAME_NAME;
  283. }
  284. BOARD_DESIGN_SETTINGS& FOOTPRINT_EDIT_FRAME::GetDesignSettings() const
  285. {
  286. return GetBoard()->GetDesignSettings();
  287. }
  288. void FOOTPRINT_EDIT_FRAME::SetDesignSettings( const BOARD_DESIGN_SETTINGS& aSettings )
  289. {
  290. GetBoard()->SetDesignSettings( aSettings );
  291. }
  292. const PCB_PLOT_PARAMS& FOOTPRINT_EDIT_FRAME::GetPlotSettings() const
  293. {
  294. // get the settings from the parent editor, not our BOARD.
  295. PCB_BASE_FRAME* parentFrame = (PCB_BASE_FRAME*) Kiway().Player( FRAME_PCB_EDITOR, true );
  296. wxASSERT( parentFrame );
  297. return parentFrame->GetPlotSettings();
  298. }
  299. void FOOTPRINT_EDIT_FRAME::SetPlotSettings( const PCB_PLOT_PARAMS& aSettings )
  300. {
  301. // set the settings into parent editor, not our BOARD.
  302. PCB_BASE_FRAME* parentFrame = (PCB_BASE_FRAME*) Kiway().Player( FRAME_PCB_EDITOR, true );
  303. wxASSERT( parentFrame );
  304. parentFrame->SetPlotSettings( aSettings );
  305. }
  306. void FOOTPRINT_EDIT_FRAME::LoadSettings( wxConfigBase* aCfg )
  307. {
  308. PCB_BASE_FRAME::LoadSettings( aCfg );
  309. wxConfigLoadSetups( aCfg, GetConfigurationSettings() );
  310. m_configSettings.Load( aCfg ); // mainly, load the color config
  311. // Ensure some params are valid
  312. BOARD_DESIGN_SETTINGS& settings = GetDesignSettings();
  313. // Usually, graphic items are drawn on F_SilkS or F_Fab layer
  314. // Force these layers if not default
  315. if( ( settings.m_RefDefaultlayer != F_SilkS ) && ( settings.m_RefDefaultlayer != F_Fab ) )
  316. settings.m_RefDefaultlayer = F_SilkS;
  317. if( ( settings.m_ValueDefaultlayer != F_SilkS ) && ( settings.m_ValueDefaultlayer != F_Fab ) )
  318. settings.m_ValueDefaultlayer = F_Fab;
  319. aCfg->Read( defaultLibWidthEntry, &m_defaultLibWidth, 250 );
  320. }
  321. void FOOTPRINT_EDIT_FRAME::SaveSettings( wxConfigBase* aCfg )
  322. {
  323. m_configSettings.Save( aCfg );
  324. PCB_BASE_FRAME::SaveSettings( aCfg );
  325. wxConfigSaveSetups( aCfg, GetConfigurationSettings() );
  326. aCfg->Write( defaultLibWidthEntry, m_treePane->GetSize().x );
  327. }
  328. const BOX2I FOOTPRINT_EDIT_FRAME::GetDocumentExtents() const
  329. {
  330. MODULE* module = GetBoard()->GetFirstModule();
  331. if( module )
  332. return module->GetFootprintRect();
  333. else
  334. return GetBoardBoundingBox( false );
  335. }
  336. void FOOTPRINT_EDIT_FRAME::OnCloseWindow( wxCloseEvent& Event )
  337. {
  338. if( GetScreen()->IsModify() && GetBoard()->GetFirstModule() )
  339. {
  340. wxString footprintName = GetBoard()->GetFirstModule()->GetFPID().GetLibItemName();
  341. wxString msg = _( "Save changes to \"%s\" before closing?" );
  342. if( !HandleUnsavedChanges( this, wxString::Format( msg, footprintName ),
  343. [&]() -> bool { return SaveFootprint( GetBoard()->GetFirstModule() ); } ) )
  344. {
  345. Event.Veto();
  346. return;
  347. }
  348. }
  349. GetCanvas()->SetEventDispatcher( NULL );
  350. GetCanvas()->StopDrawing();
  351. // Do not show the layer manager during closing to avoid flicker
  352. // on some platforms (Windows) that generate useless redraw of items in
  353. // the Layer Manger
  354. m_auimgr.GetPane( "LayersManager" ).Show( false );
  355. Clear_Pcb( false );
  356. // Close the editor
  357. Event.Skip();
  358. }
  359. void FOOTPRINT_EDIT_FRAME::OnExitKiCad( wxCommandEvent& event )
  360. {
  361. Kiway().OnKiCadExit();
  362. }
  363. void FOOTPRINT_EDIT_FRAME::CloseModuleEditor( wxCommandEvent& Event )
  364. {
  365. Close();
  366. }
  367. void FOOTPRINT_EDIT_FRAME::OnUpdateModuleSelected( wxUpdateUIEvent& aEvent )
  368. {
  369. aEvent.Enable( GetBoard()->GetFirstModule() != NULL );
  370. }
  371. void FOOTPRINT_EDIT_FRAME::OnUpdateLoadModuleFromBoard( wxUpdateUIEvent& aEvent )
  372. {
  373. PCB_EDIT_FRAME* frame = (PCB_EDIT_FRAME*) Kiway().Player( FRAME_PCB_EDITOR, false );
  374. aEvent.Enable( frame && frame->GetBoard()->GetFirstModule() != NULL );
  375. }
  376. void FOOTPRINT_EDIT_FRAME::OnUpdateInsertModuleInBoard( wxUpdateUIEvent& aEvent )
  377. {
  378. PCB_EDIT_FRAME* frame = (PCB_EDIT_FRAME*) Kiway().Player( FRAME_PCB_EDITOR, false );
  379. MODULE* module_in_edit = GetBoard()->GetFirstModule();
  380. bool canInsert = frame && module_in_edit && !module_in_edit->GetLink();
  381. // If the source was deleted, the module can inserted but not updated in the board.
  382. if( frame && module_in_edit && module_in_edit->GetLink() ) // this is not a new module
  383. {
  384. BOARD* mainpcb = frame->GetBoard();
  385. canInsert = true;
  386. // search if the source module was not deleted:
  387. for( auto source_module : mainpcb->Modules() )
  388. {
  389. if( module_in_edit->GetLink() == source_module->GetTimeStamp() )
  390. {
  391. canInsert = false;
  392. break;
  393. }
  394. }
  395. }
  396. aEvent.Enable( canInsert );
  397. }
  398. void FOOTPRINT_EDIT_FRAME::ReFillLayerWidget()
  399. {
  400. m_Layers->Freeze();
  401. m_Layers->ReFill();
  402. m_Layers->Thaw();
  403. wxAuiPaneInfo& lyrs = m_auimgr.GetPane( m_Layers );
  404. wxSize bestz = m_Layers->GetBestSize();
  405. lyrs.MinSize( bestz );
  406. lyrs.BestSize( bestz );
  407. lyrs.FloatingSize( bestz );
  408. if( lyrs.IsDocked() )
  409. m_auimgr.Update();
  410. else
  411. m_Layers->SetSize( bestz );
  412. }
  413. void FOOTPRINT_EDIT_FRAME::ShowChangedLanguage()
  414. {
  415. // call my base class
  416. PCB_BASE_EDIT_FRAME::ShowChangedLanguage();
  417. // We have 2 panes to update.
  418. // For some obscure reason, the AUI manager hides the first modified pane.
  419. // So force show panes
  420. wxAuiPaneInfo& tree_pane_info = m_auimgr.GetPane( m_treePane );
  421. bool tree_shown = tree_pane_info.IsShown();
  422. tree_pane_info.Caption( _( "Libraries" ) );
  423. wxAuiPaneInfo& lm_pane_info = m_auimgr.GetPane( m_Layers );
  424. bool lm_shown = lm_pane_info.IsShown();
  425. lm_pane_info.Caption( _( "Layers Manager" ) );
  426. // update the layer manager
  427. m_Layers->SetLayersManagerTabsText();
  428. UpdateUserInterface();
  429. // Now restore the visibility:
  430. lm_pane_info.Show( lm_shown );
  431. tree_pane_info.Show( tree_shown );
  432. m_auimgr.Update();
  433. }
  434. void FOOTPRINT_EDIT_FRAME::OnModify()
  435. {
  436. PCB_BASE_FRAME::OnModify();
  437. Update3DView( false );
  438. m_treePane->GetLibTree()->Refresh();
  439. }
  440. void FOOTPRINT_EDIT_FRAME::updateTitle()
  441. {
  442. wxString title = _( "Footprint Editor" );
  443. LIB_ID fpid = GetLoadedFPID();
  444. bool writable = true;
  445. if( IsCurrentFPFromBoard() )
  446. {
  447. title += wxString::Format( wxT( " \u2014 %s [from %s.%s]" ),
  448. GetBoard()->GetFirstModule()->GetReference(), Prj().GetProjectName(),
  449. PcbFileExtension );
  450. }
  451. else if( fpid.IsValid() )
  452. {
  453. try
  454. {
  455. writable = Prj().PcbFootprintLibs()->IsFootprintLibWritable( fpid.GetLibNickname() );
  456. }
  457. catch( const IO_ERROR& )
  458. {
  459. // best efforts...
  460. }
  461. // Note: don't used GetLoadedFPID(); footprint name may have been edited
  462. title += wxString::Format( wxT( " \u2014 %s %s" ),
  463. FROM_UTF8( GetBoard()->GetFirstModule()->GetFPID().Format().c_str() ),
  464. writable ? wxString( wxEmptyString ) : _( "[Read Only]" ) );
  465. }
  466. else if( !fpid.GetLibItemName().empty() )
  467. {
  468. // Note: don't used GetLoadedFPID(); footprint name may have been edited
  469. title += wxString::Format( wxT( " \u2014 %s %s" ),
  470. FROM_UTF8( GetBoard()->GetFirstModule()->GetFPID().GetLibItemName().c_str() ),
  471. _( "[Unsaved]" ) );
  472. }
  473. SetTitle( title );
  474. }
  475. void FOOTPRINT_EDIT_FRAME::UpdateUserInterface()
  476. {
  477. // Update the layer manager and other widgets from the board setup
  478. // (layer and items visibility, colors ...)
  479. // Update the layer manager
  480. m_Layers->Freeze();
  481. ReFillLayerWidget();
  482. m_Layers->ReFillRender();
  483. // update the layer widget to match board visibility states.
  484. m_Layers->SyncLayerVisibilities();
  485. GetCanvas()->SyncLayersVisibility( m_Pcb );
  486. m_Layers->SelectLayer( GetActiveLayer() );
  487. m_Layers->OnLayerSelected();
  488. m_Layers->Thaw();
  489. }
  490. void FOOTPRINT_EDIT_FRAME::updateView()
  491. {
  492. GetCanvas()->UseColorScheme( &Settings().Colors() );
  493. GetCanvas()->DisplayBoard( GetBoard() );
  494. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  495. m_toolManager->RunAction( ACTIONS::zoomFitScreen, true );
  496. updateTitle();
  497. }
  498. void FOOTPRINT_EDIT_FRAME::initLibraryTree()
  499. {
  500. FP_LIB_TABLE* fpTable = Prj().PcbFootprintLibs();
  501. WX_PROGRESS_REPORTER progressReporter( this, _( "Loading Footprint Libraries" ), 2 );
  502. GFootprintList.ReadFootprintFiles( fpTable, NULL, &progressReporter );
  503. progressReporter.Show( false );
  504. if( GFootprintList.GetErrorCount() )
  505. GFootprintList.DisplayErrors( this );
  506. m_adapter = FP_TREE_SYNCHRONIZING_ADAPTER::Create( this, fpTable );
  507. auto adapter = static_cast<FP_TREE_SYNCHRONIZING_ADAPTER*>( m_adapter.get() );
  508. adapter->AddLibraries();
  509. }
  510. void FOOTPRINT_EDIT_FRAME::SyncLibraryTree( bool aProgress )
  511. {
  512. FP_LIB_TABLE* fpTable = Prj().PcbFootprintLibs();
  513. auto adapter = static_cast<FP_TREE_SYNCHRONIZING_ADAPTER*>( m_adapter.get() );
  514. LIB_ID target = GetTargetFPID();
  515. bool targetSelected = ( target == m_treePane->GetLibTree()->GetSelectedLibId() );
  516. // Sync FOOTPRINT_INFO list to the libraries on disk
  517. if( aProgress )
  518. {
  519. WX_PROGRESS_REPORTER progressReporter( this, _( "Updating Footprint Libraries" ), 2 );
  520. GFootprintList.ReadFootprintFiles( fpTable, NULL, &progressReporter );
  521. progressReporter.Show( false );
  522. }
  523. else
  524. {
  525. GFootprintList.ReadFootprintFiles( fpTable, NULL, NULL );
  526. }
  527. // Sync the LIB_TREE to the FOOTPRINT_INFO list
  528. adapter->Sync();
  529. m_treePane->GetLibTree()->Unselect();
  530. m_treePane->Regenerate();
  531. if( target.IsValid() )
  532. {
  533. if( adapter->FindItem( target ) )
  534. {
  535. if( targetSelected )
  536. m_treePane->GetLibTree()->SelectLibId( target );
  537. else
  538. m_treePane->GetLibTree()->CenterLibId( target );
  539. }
  540. else
  541. {
  542. // Try to focus on parent
  543. target.SetLibItemName( wxEmptyString );
  544. m_treePane->GetLibTree()->CenterLibId( target );
  545. }
  546. }
  547. }
  548. void FOOTPRINT_EDIT_FRAME::FocusOnLibID( const LIB_ID& aLibID )
  549. {
  550. m_treePane->GetLibTree()->SelectLibId( aLibID );
  551. }
  552. bool FOOTPRINT_EDIT_FRAME::IsElementVisible( GAL_LAYER_ID aElement ) const
  553. {
  554. return GetBoard()->IsElementVisible( aElement );
  555. }
  556. void FOOTPRINT_EDIT_FRAME::SetElementVisibility( GAL_LAYER_ID aElement, bool aNewState )
  557. {
  558. GetCanvas()->GetView()->SetLayerVisible( aElement , aNewState );
  559. GetBoard()->SetElementVisibility( aElement, aNewState );
  560. m_Layers->SetRenderState( aElement, aNewState );
  561. }
  562. void FOOTPRINT_EDIT_FRAME::OnUpdateLayerAlpha( wxUpdateUIEvent & )
  563. {
  564. m_Layers->SyncLayerAlphaIndicators();
  565. }
  566. void FOOTPRINT_EDIT_FRAME::InstallPreferences( PAGED_DIALOG* aParent,
  567. PANEL_HOTKEYS_EDITOR* aHotkeysPanel )
  568. {
  569. wxTreebook* book = aParent->GetTreebook();
  570. book->AddPage( new wxPanel( book ), _( "Footprint Editor" ) );
  571. book->AddSubPage( new PANEL_MODEDIT_DISPLAY_OPTIONS( this, aParent ), _( "Display Options" ) );
  572. book->AddSubPage( new PANEL_MODEDIT_SETTINGS( this, aParent ), _( "Editing Options" ) );
  573. book->AddSubPage( new PANEL_MODEDIT_DEFAULTS( this, aParent ), _( "Default Values" ) );
  574. aHotkeysPanel->AddHotKeys( GetToolManager() );
  575. }
  576. void FOOTPRINT_EDIT_FRAME::setupTools()
  577. {
  578. // Create the manager and dispatcher & route draw panel events to the dispatcher
  579. m_toolManager = new TOOL_MANAGER;
  580. m_toolManager->SetEnvironment( GetBoard(), GetCanvas()->GetView(),
  581. GetCanvas()->GetViewControls(), this );
  582. m_actions = new PCB_ACTIONS();
  583. m_toolDispatcher = new TOOL_DISPATCHER( m_toolManager, m_actions );
  584. GetCanvas()->SetEventDispatcher( m_toolDispatcher );
  585. m_toolManager->RegisterTool( new COMMON_CONTROL );
  586. m_toolManager->RegisterTool( new COMMON_TOOLS );
  587. m_toolManager->RegisterTool( new SELECTION_TOOL );
  588. m_toolManager->RegisterTool( new ZOOM_TOOL );
  589. m_toolManager->RegisterTool( new EDIT_TOOL );
  590. m_toolManager->RegisterTool( new PAD_TOOL );
  591. m_toolManager->RegisterTool( new DRAWING_TOOL );
  592. m_toolManager->RegisterTool( new POINT_EDITOR );
  593. m_toolManager->RegisterTool( new PCBNEW_CONTROL );
  594. m_toolManager->RegisterTool( new MODULE_EDITOR_TOOLS );
  595. m_toolManager->RegisterTool( new ALIGN_DISTRIBUTE_TOOL );
  596. m_toolManager->RegisterTool( new PCBNEW_PICKER_TOOL );
  597. m_toolManager->RegisterTool( new POSITION_RELATIVE_TOOL );
  598. m_toolManager->GetTool<PCBNEW_PICKER_TOOL>()->SetEditModules( true );
  599. m_toolManager->GetTool<PCBNEW_CONTROL>()->SetEditModules( true );
  600. m_toolManager->GetTool<PAD_TOOL>()->SetEditModules( true );
  601. m_toolManager->GetTool<SELECTION_TOOL>()->SetEditModules( true );
  602. m_toolManager->GetTool<EDIT_TOOL>()->SetEditModules( true );
  603. m_toolManager->GetTool<DRAWING_TOOL>()->SetEditModules( true );
  604. m_toolManager->GetTool<POINT_EDITOR>()->SetEditModules( true );
  605. m_toolManager->InitTools();
  606. m_toolManager->InvokeTool( "pcbnew.InteractiveSelection" );
  607. }
  608. void FOOTPRINT_EDIT_FRAME::ActivateGalCanvas()
  609. {
  610. PCB_BASE_EDIT_FRAME::ActivateGalCanvas();
  611. // Be sure the axis are enabled
  612. GetCanvas()->GetGAL()->SetAxesEnabled( true );
  613. updateView();
  614. // Ensure the m_Layers settings are using the canvas type:
  615. UpdateUserInterface();
  616. }
  617. void FOOTPRINT_EDIT_FRAME::CommonSettingsChanged( bool aEnvVarsChanged )
  618. {
  619. PCB_BASE_EDIT_FRAME::CommonSettingsChanged( aEnvVarsChanged );
  620. if( aEnvVarsChanged )
  621. SyncLibraryTree( true );
  622. Layout();
  623. SendSizeEvent();
  624. }
  625. void FOOTPRINT_EDIT_FRAME::OnSaveFootprintAsPng( wxCommandEvent& event )
  626. {
  627. wxString fullFileName;
  628. LIB_ID id = GetLoadedFPID();
  629. if( id.empty() )
  630. {
  631. wxMessageBox( _( "No footprint selected." ) );
  632. return;
  633. }
  634. wxFileName fn( id.GetLibItemName() );
  635. fn.SetExt( "png" );
  636. wxString projectPath = wxPathOnly( Prj().GetProjectFullName() );
  637. wxFileDialog dlg( this, _( "Footprint Image File Name" ), projectPath,
  638. fn.GetFullName(), PngFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
  639. if( dlg.ShowModal() == wxID_CANCEL || dlg.GetPath().IsEmpty() )
  640. return;
  641. // calling wxYield is mandatory under Linux, after closing the file selector dialog
  642. // to refresh the screen before creating the PNG or JPEG image from screen
  643. wxYield();
  644. SaveCanvasImageToFile( this, dlg.GetPath() );
  645. }