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.

712 lines
23 KiB

2 years ago
1 year ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2023 CERN
  5. * Copyright (C) 2024 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <pgm_base.h>
  25. #include <kiface_base.h>
  26. #include <kiway.h>
  27. #include <kiway_express.h>
  28. #include <board.h>
  29. #include <wx/button.h>
  30. #include <wx/checkbox.h>
  31. #include <kiplatform/ui.h>
  32. #include <lset.h>
  33. #include <widgets/panel_footprint_chooser.h>
  34. #include <settings/settings_manager.h>
  35. #include <footprint_editor_settings.h>
  36. #include <footprint_chooser_frame.h>
  37. #include "wx/display.h"
  38. #include <3d_canvas/eda_3d_canvas.h>
  39. #include <project_pcb.h>
  40. #include <widgets/bitmap_button.h>
  41. #include <3d_viewer/eda_3d_viewer_frame.h>
  42. static wxArrayString s_FootprintHistoryList;
  43. static unsigned s_FootprintHistoryMaxCount = 8;
  44. static void AddFootprintToHistory( const wxString& aName )
  45. {
  46. // Remove duplicates
  47. for( int ii = (int) s_FootprintHistoryList.GetCount() - 1; ii >= 0; --ii )
  48. {
  49. if( s_FootprintHistoryList[ ii ] == aName )
  50. s_FootprintHistoryList.RemoveAt( (size_t) ii );
  51. }
  52. // Add the new name at the beginning of the history list
  53. s_FootprintHistoryList.Insert( aName, 0 );
  54. // Remove extra names
  55. while( s_FootprintHistoryList.GetCount() >= s_FootprintHistoryMaxCount )
  56. s_FootprintHistoryList.RemoveAt( s_FootprintHistoryList.GetCount() - 1 );
  57. }
  58. BEGIN_EVENT_TABLE( FOOTPRINT_CHOOSER_FRAME, PCB_BASE_FRAME )
  59. EVT_MENU( wxID_CLOSE, FOOTPRINT_CHOOSER_FRAME::closeFootprintChooser )
  60. EVT_BUTTON( wxID_OK, FOOTPRINT_CHOOSER_FRAME::OnOK )
  61. EVT_BUTTON( wxID_CANCEL, FOOTPRINT_CHOOSER_FRAME::closeFootprintChooser )
  62. EVT_PAINT( FOOTPRINT_CHOOSER_FRAME::OnPaint )
  63. END_EVENT_TABLE()
  64. #define MODAL_FRAME ( wxRESIZE_BORDER | wxSYSTEM_MENU | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN \
  65. | wxWANTS_CHARS | wxFRAME_NO_TASKBAR | wxFRAME_FLOAT_ON_PARENT )
  66. FOOTPRINT_CHOOSER_FRAME::FOOTPRINT_CHOOSER_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
  67. PCB_BASE_FRAME( aKiway, aParent, FRAME_FOOTPRINT_CHOOSER, _( "Footprint Chooser" ),
  68. wxDefaultPosition, wxDefaultSize, MODAL_FRAME,
  69. FOOTPRINT_CHOOSER_FRAME_NAME ),
  70. m_filterByPinCount( nullptr ),
  71. m_filterByFPFilters( nullptr ),
  72. m_boardAdapter(),
  73. m_currentCamera( m_trackBallCamera ),
  74. m_trackBallCamera( 2 * RANGE_SCALE_3D ),
  75. m_pinCount( 0 ),
  76. m_firstPaintEvent( true )
  77. {
  78. SetModal( true );
  79. m_showFpMode = true;
  80. m_show3DMode = false;
  81. m_messagePanel->Hide();
  82. wxPanel* bottomPanel = new wxPanel( this );
  83. wxBoxSizer* bottomSizer = new wxBoxSizer( wxVERTICAL );
  84. wxBoxSizer* frameSizer = new wxBoxSizer( wxVERTICAL );
  85. m_chooserPanel = new PANEL_FOOTPRINT_CHOOSER( this, this, s_FootprintHistoryList,
  86. // Filter
  87. [this]( LIB_TREE_NODE& aNode ) -> bool
  88. {
  89. return filterFootprint( aNode );
  90. },
  91. // Accept handler
  92. [this]()
  93. {
  94. wxCommandEvent dummy;
  95. OnOK( dummy );
  96. },
  97. // Escape handler
  98. [this]()
  99. {
  100. DismissModal( false );
  101. } );
  102. frameSizer->Add( m_chooserPanel, 1, wxEXPAND );
  103. SetBoard( new BOARD() );
  104. // This board will only be used to hold a footprint for viewing
  105. GetBoard()->SetBoardUse( BOARD_USE::FPHOLDER );
  106. build3DCanvas(); // must be called after creating m_chooserPanel
  107. m_preview3DCanvas->Show( !m_showFpMode );
  108. // buttonsSizer contains the BITMAP buttons
  109. wxBoxSizer* buttonsSizer = new wxBoxSizer( wxHORIZONTAL );
  110. buttonsSizer->Add( 0, 0, 1, 0, 5 ); // Add spacer to right-align buttons
  111. BITMAP_BUTTON* separator = new BITMAP_BUTTON( bottomPanel, wxID_ANY, wxNullBitmap );
  112. separator->SetIsSeparator();
  113. buttonsSizer->Add( separator, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 1 );
  114. m_grButton3DView = new BITMAP_BUTTON( bottomPanel, wxID_ANY, wxNullBitmap );
  115. m_grButton3DView->SetIsRadioButton();
  116. m_grButton3DView->SetBitmap( KiBitmapBundle( BITMAPS::shape_3d ) );
  117. m_grButton3DView->Check( !m_showFpMode );
  118. buttonsSizer->Add( m_grButton3DView, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 1 );
  119. m_grButtonFpView = new BITMAP_BUTTON( bottomPanel, wxID_ANY, wxNullBitmap );
  120. m_grButtonFpView->SetIsRadioButton();
  121. m_grButtonFpView->SetBitmap( KiBitmapBundle( BITMAPS::module ) );
  122. m_grButtonFpView->Check( m_showFpMode );
  123. buttonsSizer->Add( m_grButtonFpView, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 1 );
  124. separator = new BITMAP_BUTTON( bottomPanel, wxID_ANY, wxNullBitmap );
  125. separator->SetIsSeparator();
  126. buttonsSizer->Add( separator, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 1 );
  127. m_show3DViewer = new wxCheckBox( bottomPanel, wxID_ANY, _( "Show 3D viewer in own window" ) );
  128. buttonsSizer->Add( m_show3DViewer, 0, wxALL | wxALIGN_CENTER_VERTICAL, 3 );
  129. wxStdDialogButtonSizer* sdbSizer = new wxStdDialogButtonSizer();
  130. wxButton* okButton = new wxButton( bottomPanel, wxID_OK );
  131. wxButton* cancelButton = new wxButton( bottomPanel, wxID_CANCEL );
  132. sdbSizer->AddButton( okButton );
  133. sdbSizer->AddButton( cancelButton );
  134. sdbSizer->Realize();
  135. buttonsSizer->Add( 20, 0, 0, 0, 5 ); // Add spacer
  136. buttonsSizer->Add( sdbSizer, 0, wxALL | wxALIGN_CENTER_VERTICAL, 5 );
  137. bottomSizer->Add( buttonsSizer, 0, wxEXPAND, 5 );
  138. bottomPanel->SetSizer( bottomSizer );
  139. frameSizer->Add( bottomPanel, 0, wxEXPAND );
  140. SetSizer( frameSizer );
  141. SetTitle( GetTitle() + wxString::Format( _( " (%d items loaded)" ),
  142. m_chooserPanel->GetItemCount() ) );
  143. Layout();
  144. m_chooserPanel->FinishSetup();
  145. // Connect Events
  146. m_grButton3DView->Connect( wxEVT_COMMAND_BUTTON_CLICKED ,
  147. wxCommandEventHandler( FOOTPRINT_CHOOSER_FRAME::on3DviewReq ),
  148. nullptr, this );
  149. m_grButtonFpView->Connect( wxEVT_COMMAND_BUTTON_CLICKED ,
  150. wxCommandEventHandler( FOOTPRINT_CHOOSER_FRAME::onFpViewReq ),
  151. nullptr, this );
  152. m_show3DViewer->Connect( wxEVT_COMMAND_CHECKBOX_CLICKED ,
  153. wxCommandEventHandler( FOOTPRINT_CHOOSER_FRAME::onExternalViewer3DEnable ),
  154. nullptr, this );
  155. Connect( FP_SELECTION_EVENT, // custom event fired by a PANEL_FOOTPRINT_CHOOSER
  156. wxCommandEventHandler( FOOTPRINT_CHOOSER_FRAME::onFpChanged ), nullptr, this );
  157. // Needed on Linux to fix the position of widgets in bottomPanel
  158. PostSizeEvent();
  159. }
  160. FOOTPRINT_CHOOSER_FRAME::~FOOTPRINT_CHOOSER_FRAME()
  161. {
  162. // Disconnect Events
  163. m_grButton3DView->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED,
  164. wxCommandEventHandler( FOOTPRINT_CHOOSER_FRAME::on3DviewReq ),
  165. nullptr, this );
  166. m_grButtonFpView->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED,
  167. wxCommandEventHandler( FOOTPRINT_CHOOSER_FRAME::onFpViewReq ),
  168. nullptr, this );
  169. m_show3DViewer->Disconnect( wxEVT_COMMAND_CHECKBOX_CLICKED ,
  170. wxCommandEventHandler( FOOTPRINT_CHOOSER_FRAME::onExternalViewer3DEnable ),
  171. nullptr, this );
  172. Disconnect( FP_SELECTION_EVENT,
  173. wxCommandEventHandler( FOOTPRINT_CHOOSER_FRAME::onFpChanged ), nullptr, this );
  174. if( PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() ) )
  175. {
  176. if( m_filterByFPFilters )
  177. cfg->m_FootprintChooser.use_fp_filters = m_filterByFPFilters->GetValue();
  178. if( m_filterByPinCount )
  179. cfg->m_FootprintChooser.filter_on_pin_count = m_filterByPinCount->GetValue();
  180. }
  181. }
  182. void FOOTPRINT_CHOOSER_FRAME::onExternalViewer3DEnable( wxCommandEvent& aEvent )
  183. {
  184. if( aEvent.IsChecked() )
  185. {
  186. if( m_grButton3DView->IsChecked() )
  187. Show3DViewerFrame(); // show external 3D viewer
  188. }
  189. else
  190. {
  191. // Close the external 3D viewer frame, if it is still enabled
  192. EDA_3D_VIEWER_FRAME* viewer3D = Get3DViewerFrame();
  193. if( viewer3D )
  194. viewer3D->Close( true );
  195. }
  196. updatePanelsVisibility();
  197. }
  198. void FOOTPRINT_CHOOSER_FRAME::Show3DViewerFrame()
  199. {
  200. bool do_reload_board = true; // reload board flag
  201. // At EDA_3D_VIEWER_FRAME creation, the current board is loaded, so disable loading
  202. // the current board if the 3D frame is not yet created
  203. if( Get3DViewerFrame() == nullptr )
  204. do_reload_board = false;
  205. EDA_3D_VIEWER_FRAME* draw3DFrame = CreateAndShow3D_Frame();
  206. // A stronger version of Raise() which promotes the window to its parent's level.
  207. KIPLATFORM::UI::ReparentModal( draw3DFrame );
  208. // And load or update the current board (if needed)
  209. if( do_reload_board )
  210. Update3DView( true, true );
  211. }
  212. void FOOTPRINT_CHOOSER_FRAME::Update3DView( bool aMarkDirty,
  213. bool aRefresh, const wxString* aTitle )
  214. {
  215. LIB_ID fpID = m_chooserPanel->GetSelectedLibId();
  216. wxString footprintName;
  217. if( fpID.IsValid() )
  218. footprintName << fpID.Format();
  219. wxString title = _( "3D Viewer" ) + wxT( " \u2014 " ) + footprintName;
  220. PCB_BASE_FRAME::Update3DView( aMarkDirty, aRefresh, &title );
  221. }
  222. bool FOOTPRINT_CHOOSER_FRAME::filterByPinCount()
  223. {
  224. if( m_filterByPinCount )
  225. return m_filterByPinCount->GetValue();
  226. if( PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() ) )
  227. return cfg->m_FootprintChooser.filter_on_pin_count;
  228. return false;
  229. }
  230. bool FOOTPRINT_CHOOSER_FRAME::filterByFPFilters()
  231. {
  232. if( m_filterByFPFilters )
  233. return m_filterByFPFilters->GetValue();
  234. if( PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() ) )
  235. return cfg->m_FootprintChooser.use_fp_filters;
  236. return false;
  237. }
  238. bool FOOTPRINT_CHOOSER_FRAME::filterFootprint( LIB_TREE_NODE& aNode )
  239. {
  240. if( aNode.m_Type == LIB_TREE_NODE::TYPE::LIBRARY )
  241. {
  242. // Normally lib nodes get scored by the max of their children's scores. However, if a
  243. // lib node *has* no children then the scorer will call the filter on the lib node itself,
  244. // and we just want to return true if we're not filtering at all.
  245. return !filterByPinCount() && !filterByFPFilters();
  246. }
  247. auto patternMatch =
  248. []( LIB_ID& id, std::vector<std::unique_ptr<EDA_PATTERN_MATCH>>& filters ) -> bool
  249. {
  250. // The matching is case insensitive
  251. wxString name;
  252. for( const std::unique_ptr<EDA_PATTERN_MATCH>& filter : filters )
  253. {
  254. name.Empty();
  255. // If the filter contains a ':' then include the library name in the pattern
  256. if( filter->GetPattern().Contains( wxS( ":" ) ) )
  257. name = id.GetUniStringLibNickname().Lower() + wxS( ":" );
  258. name += id.GetUniStringLibItemName().Lower();
  259. if( filter->Find( name ) )
  260. return true;
  261. }
  262. return false;
  263. };
  264. if( m_pinCount > 0 && filterByPinCount() )
  265. {
  266. if( aNode.m_PinCount != m_pinCount )
  267. return false;
  268. }
  269. if( !m_fpFilters.empty() && filterByFPFilters() )
  270. {
  271. if( !patternMatch( aNode.m_LibId, m_fpFilters ) )
  272. return false;
  273. }
  274. return true;
  275. }
  276. void FOOTPRINT_CHOOSER_FRAME::doCloseWindow()
  277. {
  278. // Only dismiss a modal frame once, so that the return values set by
  279. // the prior DismissModal() are not bashed for ShowModal().
  280. if( !IsDismissed() )
  281. DismissModal( false );
  282. // window to be destroyed by the caller of KIWAY_PLAYER::ShowModal()
  283. }
  284. WINDOW_SETTINGS* FOOTPRINT_CHOOSER_FRAME::GetWindowSettings( APP_SETTINGS_BASE* aCfg )
  285. {
  286. PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( aCfg );
  287. wxCHECK_MSG( cfg, nullptr, wxT( "config not existing" ) );
  288. return &cfg->m_FootprintViewer;
  289. }
  290. COLOR_SETTINGS* FOOTPRINT_CHOOSER_FRAME::GetColorSettings( bool aForceRefresh ) const
  291. {
  292. auto* cfg = Pgm().GetSettingsManager().GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" );
  293. if( cfg )
  294. return Pgm().GetSettingsManager().GetColorSettings( cfg->m_ColorTheme );
  295. else
  296. return Pgm().GetSettingsManager().GetColorSettings();
  297. }
  298. static wxRect s_dialogRect( 0, 0, 0, 0 );
  299. void FOOTPRINT_CHOOSER_FRAME::KiwayMailIn( KIWAY_EXPRESS& mail )
  300. {
  301. const std::string& payload = mail.GetPayload();
  302. switch( mail.Command() )
  303. {
  304. case MAIL_SYMBOL_NETLIST:
  305. {
  306. wxSizer* filtersSizer = m_chooserPanel->GetFiltersSizer();
  307. wxWindow* filtersWindow = filtersSizer->GetContainingWindow();
  308. wxString msg;
  309. m_pinCount = 0;
  310. m_fpFilters.clear();
  311. /*
  312. * Symbol netlist format:
  313. * pinNumber pinName <tab> pinNumber pinName...
  314. * fpFilter fpFilter...
  315. */
  316. std::map<wxString, wxString> pinNames;
  317. std::vector<std::string> strings = split( payload, "\r" );
  318. if( strings.size() >= 1 && !strings[0].empty() )
  319. {
  320. for( const wxString& pin : wxSplit( strings[0], '\t' ) )
  321. pinNames[ pin.BeforeFirst( ' ' ) ] = pin.AfterFirst( ' ' );
  322. m_pinCount = pinNames.size();
  323. if( m_pinCount > 0 )
  324. {
  325. msg.Printf( _( "Filter by pin count (%d)" ), m_pinCount );
  326. m_filterByPinCount = new wxCheckBox( filtersWindow, wxID_ANY, msg );
  327. m_filterByPinCount->Bind( wxEVT_CHECKBOX,
  328. [&]( wxCommandEvent& evt )
  329. {
  330. m_chooserPanel->Regenerate();
  331. } );
  332. if( PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() ) )
  333. m_filterByPinCount->SetValue( cfg->m_FootprintChooser.filter_on_pin_count );
  334. }
  335. }
  336. if( strings.size() >= 2 && !strings[1].empty() )
  337. {
  338. for( const wxString& filter : wxSplit( strings[1], ' ' ) )
  339. {
  340. m_fpFilters.push_back( std::make_unique<EDA_PATTERN_MATCH_WILDCARD_ANCHORED>() );
  341. m_fpFilters.back()->SetPattern( filter.Lower() );
  342. }
  343. msg.Printf( _( "Apply footprint filters (%s)" ), strings[1] );
  344. m_filterByFPFilters = new wxCheckBox( filtersWindow, wxID_ANY, msg );
  345. m_filterByFPFilters->Bind( wxEVT_CHECKBOX,
  346. [&]( wxCommandEvent& evt )
  347. {
  348. m_chooserPanel->Regenerate();
  349. } );
  350. if( PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() ) )
  351. m_filterByFPFilters->SetValue( cfg->m_FootprintChooser.use_fp_filters );
  352. }
  353. if( m_filterByFPFilters )
  354. m_chooserPanel->GetFiltersSizer()->Add( m_filterByFPFilters, 0, wxEXPAND|wxBOTTOM, 4 );
  355. if( m_filterByPinCount )
  356. m_chooserPanel->GetFiltersSizer()->Add( m_filterByPinCount, 0, wxEXPAND|wxBOTTOM, 4 );
  357. m_chooserPanel->GetViewerPanel()->SetPinFunctions( pinNames );
  358. // Save the wxFormBuilder size of the dialog...
  359. if( s_dialogRect.GetSize().x == 0 || s_dialogRect.GetSize().y == 0 )
  360. s_dialogRect = wxRect( wxWindow::GetPosition(), wxWindow::GetSize() );
  361. // ... and then give it a kick to get it to layout the new items
  362. GetSizer()->SetSizeHints( this );
  363. break;
  364. }
  365. default:
  366. break;
  367. }
  368. }
  369. bool FOOTPRINT_CHOOSER_FRAME::ShowModal( wxString* aFootprint, wxWindow* aParent )
  370. {
  371. if( aFootprint && !aFootprint->IsEmpty() )
  372. {
  373. LIB_ID fpid;
  374. fpid.Parse( *aFootprint, true );
  375. if( fpid.IsValid() )
  376. m_chooserPanel->SetPreselect( fpid );
  377. }
  378. return KIWAY_PLAYER::ShowModal( aFootprint, aParent );
  379. }
  380. void FOOTPRINT_CHOOSER_FRAME::SetPosition( const wxPoint& aNewPosition )
  381. {
  382. PCB_BASE_FRAME::SetPosition( aNewPosition );
  383. s_dialogRect.SetPosition( aNewPosition );
  384. }
  385. bool FOOTPRINT_CHOOSER_FRAME::Show( bool show )
  386. {
  387. bool ret;
  388. // Show or hide the window. If hiding, save current position and size.
  389. // If showing, use previous position and size.
  390. if( show )
  391. {
  392. #ifndef __WINDOWS__
  393. PCB_BASE_FRAME::Raise(); // Needed on OS X and some other window managers (i.e. Unity)
  394. #endif
  395. ret = PCB_BASE_FRAME::Show( show );
  396. // returns a zeroed-out default wxRect if none existed before.
  397. wxRect savedDialogRect = s_dialogRect;
  398. if( savedDialogRect.GetSize().x != 0 && savedDialogRect.GetSize().y != 0 )
  399. {
  400. SetSize( savedDialogRect.GetPosition().x, savedDialogRect.GetPosition().y,
  401. std::max( wxWindow::GetSize().x, savedDialogRect.GetSize().x ),
  402. std::max( wxWindow::GetSize().y, savedDialogRect.GetSize().y ),
  403. 0 );
  404. }
  405. // Be sure that the dialog appears in a visible area
  406. // (the dialog position might have been stored at the time when it was
  407. // shown on another display)
  408. if( wxDisplay::GetFromWindow( this ) == wxNOT_FOUND )
  409. Centre();
  410. }
  411. else
  412. {
  413. s_dialogRect = wxRect( wxWindow::GetPosition(), wxWindow::GetSize() );
  414. ret = PCB_BASE_FRAME::Show( show );
  415. }
  416. return ret;
  417. }
  418. void FOOTPRINT_CHOOSER_FRAME::OnPaint( wxPaintEvent& aEvent )
  419. {
  420. if( m_firstPaintEvent )
  421. {
  422. KIPLATFORM::UI::FixupCancelButtonCmdKeyCollision( this );
  423. KIPLATFORM::UI::ForceFocus( m_chooserPanel->GetFocusTarget() );
  424. m_firstPaintEvent = false;
  425. }
  426. aEvent.Skip();
  427. }
  428. void FOOTPRINT_CHOOSER_FRAME::OnOK( wxCommandEvent& aEvent )
  429. {
  430. LIB_ID fpID = m_chooserPanel->GetSelectedLibId();
  431. if( fpID.IsValid() )
  432. {
  433. wxString footprint = fpID.Format();
  434. AddFootprintToHistory( footprint );
  435. DismissModal( true, footprint );
  436. }
  437. else
  438. {
  439. DismissModal( false );
  440. }
  441. }
  442. void FOOTPRINT_CHOOSER_FRAME::closeFootprintChooser( wxCommandEvent& aEvent )
  443. {
  444. Close( false );
  445. }
  446. void FOOTPRINT_CHOOSER_FRAME::onFpChanged( wxCommandEvent& event )
  447. {
  448. updateViews();
  449. }
  450. void FOOTPRINT_CHOOSER_FRAME::build3DCanvas()
  451. {
  452. // Create the dummy board used by the 3D canvas
  453. m_dummyBoard = GetBoard();
  454. m_dummyBoard->SetProject( &Prj(), true );
  455. // This board will only be used to hold a footprint for viewing
  456. m_dummyBoard->SetBoardUse( BOARD_USE::FPHOLDER );
  457. m_boardAdapter.SetBoard( m_dummyBoard );
  458. m_boardAdapter.m_IsBoardView = false;
  459. m_boardAdapter.m_IsPreviewer = true; // Force display 3D models, regardless the 3D viewer options
  460. SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
  461. EDA_3D_VIEWER_SETTINGS* cfg = mgr.GetAppSettings<EDA_3D_VIEWER_SETTINGS>( "3d_viewer" );
  462. m_boardAdapter.m_Cfg = cfg;
  463. // Build the 3D canvas
  464. m_preview3DCanvas = new EDA_3D_CANVAS( m_chooserPanel->m_RightPanel,
  465. OGL_ATT_LIST::GetAttributesList( ANTIALIASING_MODE::AA_8X ),
  466. m_boardAdapter, m_currentCamera,
  467. PROJECT_PCB::Get3DCacheManager( &Prj() ) );
  468. m_chooserPanel->m_RightPanelSizer->Add( m_preview3DCanvas, 1, wxALL | wxEXPAND, 5 );
  469. m_chooserPanel->m_RightPanel->Layout();
  470. BOARD_DESIGN_SETTINGS& dummy_bds = m_dummyBoard->GetDesignSettings();
  471. dummy_bds.SetBoardThickness( pcbIUScale.mmToIU( 1.6 ) );
  472. dummy_bds.SetEnabledLayers( LSET::FrontMask() | LSET::BackMask() );
  473. BOARD_STACKUP& dummy_board_stackup = m_dummyBoard->GetDesignSettings().GetStackupDescriptor();
  474. dummy_board_stackup.RemoveAll();
  475. dummy_board_stackup.BuildDefaultStackupList( &dummy_bds, 2 );
  476. }
  477. void FOOTPRINT_CHOOSER_FRAME::on3DviewReq( wxCommandEvent& event )
  478. {
  479. if( m_show3DMode == true )
  480. {
  481. if( m_showFpMode == true )
  482. {
  483. m_show3DMode = false;
  484. m_grButton3DView->Check( m_show3DMode );
  485. updatePanelsVisibility();
  486. }
  487. }
  488. else
  489. {
  490. if( m_show3DViewer->IsChecked() )
  491. {
  492. Show3DViewerFrame();
  493. }
  494. else
  495. {
  496. // Close 3D viewer frame, if it is still enabled
  497. EDA_3D_VIEWER_FRAME* viewer3D = Get3DViewerFrame();
  498. if( viewer3D )
  499. viewer3D->Close( true );
  500. }
  501. m_show3DMode = true;
  502. m_grButton3DView->Check( m_show3DMode );
  503. updatePanelsVisibility();
  504. }
  505. }
  506. void FOOTPRINT_CHOOSER_FRAME::onFpViewReq( wxCommandEvent& event )
  507. {
  508. if( m_showFpMode == true )
  509. {
  510. if( m_show3DMode == true )
  511. {
  512. m_showFpMode = false;
  513. m_grButtonFpView->Check( m_showFpMode );
  514. updatePanelsVisibility();
  515. }
  516. }
  517. else
  518. {
  519. m_showFpMode = true;
  520. m_grButtonFpView->Check( m_showFpMode );
  521. updatePanelsVisibility();
  522. }
  523. }
  524. void FOOTPRINT_CHOOSER_FRAME::updateViews()
  525. {
  526. EDA_3D_VIEWER_FRAME* viewer3D = Get3DViewerFrame();
  527. bool reloadFp = viewer3D || m_preview3DCanvas->IsShown();
  528. if( reloadFp )
  529. {
  530. m_dummyBoard->DeleteAllFootprints();
  531. if( m_chooserPanel->m_CurrFootprint )
  532. m_dummyBoard->Add( (FOOTPRINT*)m_chooserPanel->m_CurrFootprint->Clone() );
  533. }
  534. if( m_preview3DCanvas->IsShown() )
  535. {
  536. m_preview3DCanvas->ReloadRequest();
  537. m_preview3DCanvas->Request_refresh();
  538. }
  539. if( viewer3D )
  540. {
  541. Update3DView( true, true );
  542. }
  543. m_chooserPanel->m_RightPanel->Layout();
  544. m_chooserPanel->m_RightPanel->Refresh();
  545. }
  546. void FOOTPRINT_CHOOSER_FRAME::updatePanelsVisibility()
  547. {
  548. FOOTPRINT_PREVIEW_WIDGET* viewFpPanel = m_chooserPanel->GetViewerPanel();
  549. viewFpPanel->Show( m_showFpMode );
  550. m_preview3DCanvas->Show( m_show3DMode );
  551. updateViews();
  552. }