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.

789 lines
26 KiB

2 years ago
1 year ago
8 months 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 The 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 <tool/tool_manager.h>
  38. #include <tool/tool_dispatcher.h>
  39. #include <tool/common_tools.h>
  40. #include <tool/zoom_tool.h>
  41. #include <tools/footprint_chooser_selection_tool.h>
  42. #include <tools/pcb_actions.h>
  43. #include <tools/pcb_picker_tool.h>
  44. #include <tools/pcb_viewer_tools.h>
  45. #include "wx/display.h"
  46. #include <footprint_preview_panel.h>
  47. #include <3d_canvas/eda_3d_canvas.h>
  48. #include <project_pcb.h>
  49. #include <widgets/bitmap_button.h>
  50. #include <3d_viewer/eda_3d_viewer_frame.h>
  51. #include <tools/pcb_editor_conditions.h>
  52. static wxArrayString s_FootprintHistoryList;
  53. static unsigned s_FootprintHistoryMaxCount = 8;
  54. static void AddFootprintToHistory( const wxString& aName )
  55. {
  56. // Remove duplicates
  57. for( int ii = (int) s_FootprintHistoryList.GetCount() - 1; ii >= 0; --ii )
  58. {
  59. if( s_FootprintHistoryList[ ii ] == aName )
  60. s_FootprintHistoryList.RemoveAt( (size_t) ii );
  61. }
  62. // Add the new name at the beginning of the history list
  63. s_FootprintHistoryList.Insert( aName, 0 );
  64. // Remove extra names
  65. while( s_FootprintHistoryList.GetCount() >= s_FootprintHistoryMaxCount )
  66. s_FootprintHistoryList.RemoveAt( s_FootprintHistoryList.GetCount() - 1 );
  67. }
  68. BEGIN_EVENT_TABLE( FOOTPRINT_CHOOSER_FRAME, PCB_BASE_FRAME )
  69. EVT_MENU( wxID_CLOSE, FOOTPRINT_CHOOSER_FRAME::closeFootprintChooser )
  70. EVT_BUTTON( wxID_OK, FOOTPRINT_CHOOSER_FRAME::OnOK )
  71. EVT_BUTTON( wxID_CANCEL, FOOTPRINT_CHOOSER_FRAME::closeFootprintChooser )
  72. EVT_PAINT( FOOTPRINT_CHOOSER_FRAME::OnPaint )
  73. END_EVENT_TABLE()
  74. #define MODAL_FRAME ( wxRESIZE_BORDER | wxSYSTEM_MENU | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN \
  75. | wxWANTS_CHARS | wxFRAME_NO_TASKBAR | wxFRAME_FLOAT_ON_PARENT )
  76. FOOTPRINT_CHOOSER_FRAME::FOOTPRINT_CHOOSER_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
  77. PCB_BASE_FRAME( aKiway, aParent, FRAME_FOOTPRINT_CHOOSER, _( "Footprint Chooser" ),
  78. wxDefaultPosition, wxDefaultSize, MODAL_FRAME,
  79. FOOTPRINT_CHOOSER_FRAME_NAME ),
  80. m_filterByPinCount( nullptr ),
  81. m_filterByFPFilters( nullptr ),
  82. m_boardAdapter(),
  83. m_currentCamera( m_trackBallCamera ),
  84. m_trackBallCamera( 2 * RANGE_SCALE_3D ),
  85. m_pinCount( 0 ),
  86. m_firstPaintEvent( true )
  87. {
  88. SetModal( true );
  89. m_showFpMode = true;
  90. m_show3DMode = false;
  91. m_messagePanel->Hide();
  92. wxPanel* bottomPanel = new wxPanel( this );
  93. wxBoxSizer* bottomSizer = new wxBoxSizer( wxVERTICAL );
  94. wxBoxSizer* frameSizer = new wxBoxSizer( wxVERTICAL );
  95. m_chooserPanel = new PANEL_FOOTPRINT_CHOOSER( this, this, s_FootprintHistoryList,
  96. // Filter
  97. [this]( LIB_TREE_NODE& aNode ) -> bool
  98. {
  99. return filterFootprint( aNode );
  100. },
  101. // Accept handler
  102. [this]()
  103. {
  104. wxCommandEvent dummy;
  105. OnOK( dummy );
  106. },
  107. // Escape handler
  108. [this]()
  109. {
  110. DismissModal( false );
  111. } );
  112. frameSizer->Add( m_chooserPanel, 1, wxEXPAND );
  113. SetCanvas( m_chooserPanel->GetViewerPanel()->GetPreviewPanel()->GetCanvas() );
  114. SetBoard( m_chooserPanel->GetViewerPanel()->GetPreviewPanel()->GetBoard() );
  115. // This board will only be used to hold a footprint for viewing
  116. GetBoard()->SetBoardUse( BOARD_USE::FPHOLDER );
  117. build3DCanvas(); // must be called after creating m_chooserPanel
  118. m_preview3DCanvas->Show( !m_showFpMode );
  119. // buttonsSizer contains the BITMAP buttons
  120. wxBoxSizer* buttonsSizer = new wxBoxSizer( wxHORIZONTAL );
  121. buttonsSizer->Add( 0, 0, 1, 0, 5 ); // Add spacer to right-align buttons
  122. BITMAP_BUTTON* separator = new BITMAP_BUTTON( bottomPanel, wxID_ANY, wxNullBitmap );
  123. separator->SetIsSeparator();
  124. buttonsSizer->Add( separator, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 1 );
  125. m_grButton3DView = new BITMAP_BUTTON( bottomPanel, wxID_ANY, wxNullBitmap );
  126. m_grButton3DView->SetIsRadioButton();
  127. m_grButton3DView->SetBitmap( KiBitmapBundle( BITMAPS::shape_3d ) );
  128. m_grButton3DView->Check( !m_showFpMode );
  129. buttonsSizer->Add( m_grButton3DView, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 1 );
  130. m_grButtonFpView = new BITMAP_BUTTON( bottomPanel, wxID_ANY, wxNullBitmap );
  131. m_grButtonFpView->SetIsRadioButton();
  132. m_grButtonFpView->SetBitmap( KiBitmapBundle( BITMAPS::module ) );
  133. m_grButtonFpView->Check( m_showFpMode );
  134. buttonsSizer->Add( m_grButtonFpView, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 1 );
  135. separator = new BITMAP_BUTTON( bottomPanel, wxID_ANY, wxNullBitmap );
  136. separator->SetIsSeparator();
  137. buttonsSizer->Add( separator, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 1 );
  138. m_show3DViewer = new wxCheckBox( bottomPanel, wxID_ANY, _( "Show 3D viewer in own window" ) );
  139. buttonsSizer->Add( m_show3DViewer, 0, wxALL | wxALIGN_CENTER_VERTICAL, 3 );
  140. wxStdDialogButtonSizer* sdbSizer = new wxStdDialogButtonSizer();
  141. wxButton* okButton = new wxButton( bottomPanel, wxID_OK );
  142. wxButton* cancelButton = new wxButton( bottomPanel, wxID_CANCEL );
  143. sdbSizer->AddButton( okButton );
  144. sdbSizer->AddButton( cancelButton );
  145. sdbSizer->Realize();
  146. buttonsSizer->Add( 20, 0, 0, 0, 5 ); // Add spacer
  147. buttonsSizer->Add( sdbSizer, 0, wxALL | wxALIGN_CENTER_VERTICAL, 5 );
  148. bottomSizer->Add( buttonsSizer, 0, wxEXPAND, 5 );
  149. bottomPanel->SetSizer( bottomSizer );
  150. frameSizer->Add( bottomPanel, 0, wxEXPAND );
  151. SetSizer( frameSizer );
  152. SetTitle( GetTitle() + wxString::Format( _( " (%d items loaded)" ),
  153. m_chooserPanel->GetItemCount() ) );
  154. Layout();
  155. m_chooserPanel->FinishSetup();
  156. // Create the manager and dispatcher & route draw panel events to the dispatcher
  157. m_toolManager = new TOOL_MANAGER;
  158. m_toolManager->SetEnvironment( GetBoard(), GetCanvas()->GetView(),
  159. GetCanvas()->GetViewControls(), GetViewerSettingsBase(), this );
  160. m_actions = new PCB_ACTIONS();
  161. m_toolDispatcher = new TOOL_DISPATCHER( m_toolManager );
  162. GetCanvas()->SetEventDispatcher( m_toolDispatcher );
  163. m_toolManager->RegisterTool( new COMMON_TOOLS ); // for std context menus (zoom & grid)
  164. m_toolManager->RegisterTool( new PCB_PICKER_TOOL ); // for setting grid origin
  165. m_toolManager->RegisterTool( new ZOOM_TOOL );
  166. m_toolManager->RegisterTool( new PCB_VIEWER_TOOLS );
  167. m_toolManager->RegisterTool( new FOOTPRINT_CHOOSER_SELECTION_TOOL );
  168. m_toolManager->GetTool<PCB_VIEWER_TOOLS>()->SetFootprintFrame( true );
  169. m_toolManager->GetTool<PCB_VIEWER_TOOLS>()->SetIsDefaultTool( true );
  170. m_toolManager->InitTools();
  171. setupUIConditions();
  172. // Connect Events
  173. m_grButton3DView->Connect( wxEVT_COMMAND_BUTTON_CLICKED ,
  174. wxCommandEventHandler( FOOTPRINT_CHOOSER_FRAME::on3DviewReq ),
  175. nullptr, this );
  176. m_grButtonFpView->Connect( wxEVT_COMMAND_BUTTON_CLICKED ,
  177. wxCommandEventHandler( FOOTPRINT_CHOOSER_FRAME::onFpViewReq ),
  178. nullptr, this );
  179. m_show3DViewer->Connect( wxEVT_COMMAND_CHECKBOX_CLICKED ,
  180. wxCommandEventHandler( FOOTPRINT_CHOOSER_FRAME::onExternalViewer3DEnable ),
  181. nullptr, this );
  182. Connect( FP_SELECTION_EVENT, // custom event fired by a PANEL_FOOTPRINT_CHOOSER
  183. wxCommandEventHandler( FOOTPRINT_CHOOSER_FRAME::onFpChanged ), nullptr, this );
  184. // Needed on Linux to fix the position of widgets in bottomPanel
  185. PostSizeEvent();
  186. }
  187. FOOTPRINT_CHOOSER_FRAME::~FOOTPRINT_CHOOSER_FRAME()
  188. {
  189. // Work around assertion firing when we try to LockCtx on a hidden 3D canvas during dtor
  190. wxCloseEvent dummy;
  191. m_preview3DCanvas->Show();
  192. m_preview3DCanvas->OnCloseWindow( dummy );
  193. // Disconnect board, which is owned by FOOTPRINT_PREVIEW_PANEL.
  194. m_pcb = nullptr;
  195. // Disconnect Events
  196. m_grButton3DView->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED,
  197. wxCommandEventHandler( FOOTPRINT_CHOOSER_FRAME::on3DviewReq ),
  198. nullptr, this );
  199. m_grButtonFpView->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED,
  200. wxCommandEventHandler( FOOTPRINT_CHOOSER_FRAME::onFpViewReq ),
  201. nullptr, this );
  202. m_show3DViewer->Disconnect( wxEVT_COMMAND_CHECKBOX_CLICKED ,
  203. wxCommandEventHandler( FOOTPRINT_CHOOSER_FRAME::onExternalViewer3DEnable ),
  204. nullptr, this );
  205. Disconnect( FP_SELECTION_EVENT,
  206. wxCommandEventHandler( FOOTPRINT_CHOOSER_FRAME::onFpChanged ), nullptr, this );
  207. if( PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() ) )
  208. {
  209. if( m_filterByFPFilters )
  210. cfg->m_FootprintChooser.use_fp_filters = m_filterByFPFilters->GetValue();
  211. if( m_filterByPinCount )
  212. cfg->m_FootprintChooser.filter_on_pin_count = m_filterByPinCount->GetValue();
  213. }
  214. }
  215. void FOOTPRINT_CHOOSER_FRAME::onExternalViewer3DEnable( wxCommandEvent& aEvent )
  216. {
  217. if( aEvent.IsChecked() )
  218. {
  219. if( m_grButton3DView->IsChecked() )
  220. Show3DViewerFrame(); // show external 3D viewer
  221. }
  222. else
  223. {
  224. // Close the external 3D viewer frame, if it is still enabled
  225. EDA_3D_VIEWER_FRAME* viewer3D = Get3DViewerFrame();
  226. if( viewer3D )
  227. viewer3D->Close( true );
  228. }
  229. updatePanelsVisibility();
  230. }
  231. void FOOTPRINT_CHOOSER_FRAME::Show3DViewerFrame()
  232. {
  233. bool do_reload_board = true; // reload board flag
  234. // At EDA_3D_VIEWER_FRAME creation, the current board is loaded, so disable loading
  235. // the current board if the 3D frame is not yet created
  236. if( Get3DViewerFrame() == nullptr )
  237. do_reload_board = false;
  238. EDA_3D_VIEWER_FRAME* draw3DFrame = CreateAndShow3D_Frame();
  239. // A stronger version of Raise() which promotes the window to its parent's level.
  240. KIPLATFORM::UI::ReparentModal( draw3DFrame );
  241. // And load or update the current board (if needed)
  242. if( do_reload_board )
  243. Update3DView( true, true );
  244. }
  245. void FOOTPRINT_CHOOSER_FRAME::Update3DView( bool aMarkDirty, bool aRefresh, const wxString* aTitle )
  246. {
  247. LIB_ID fpID = m_chooserPanel->GetSelectedLibId();
  248. wxString footprintName;
  249. if( fpID.IsValid() )
  250. footprintName << fpID.Format();
  251. wxString title = _( "3D Viewer" ) + wxT( " \u2014 " ) + footprintName;
  252. PCB_BASE_FRAME::Update3DView( aMarkDirty, aRefresh, &title );
  253. }
  254. bool FOOTPRINT_CHOOSER_FRAME::filterByPinCount()
  255. {
  256. if( m_filterByPinCount )
  257. return m_filterByPinCount->GetValue();
  258. if( PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() ) )
  259. return cfg->m_FootprintChooser.filter_on_pin_count;
  260. return false;
  261. }
  262. bool FOOTPRINT_CHOOSER_FRAME::filterByFPFilters()
  263. {
  264. if( m_filterByFPFilters )
  265. return m_filterByFPFilters->GetValue();
  266. if( PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() ) )
  267. return cfg->m_FootprintChooser.use_fp_filters;
  268. return false;
  269. }
  270. bool FOOTPRINT_CHOOSER_FRAME::filterFootprint( LIB_TREE_NODE& aNode )
  271. {
  272. if( aNode.m_Type == LIB_TREE_NODE::TYPE::LIBRARY )
  273. {
  274. // Normally lib nodes get scored by the max of their children's scores. However, if a
  275. // lib node *has* no children then the scorer will call the filter on the lib node itself,
  276. // and we just want to return true if we're not filtering at all.
  277. return !filterByPinCount() && !filterByFPFilters();
  278. }
  279. auto patternMatch =
  280. []( LIB_ID& id, std::vector<std::unique_ptr<EDA_PATTERN_MATCH>>& filters ) -> bool
  281. {
  282. // The matching is case insensitive
  283. wxString name;
  284. for( const std::unique_ptr<EDA_PATTERN_MATCH>& filter : filters )
  285. {
  286. name.Empty();
  287. // If the filter contains a ':' then include the library name in the pattern
  288. if( filter->GetPattern().Contains( wxS( ":" ) ) )
  289. name = id.GetUniStringLibNickname().Lower() + wxS( ":" );
  290. name += id.GetUniStringLibItemName().Lower();
  291. if( filter->Find( name ) )
  292. return true;
  293. }
  294. return false;
  295. };
  296. if( m_pinCount > 0 && filterByPinCount() )
  297. {
  298. if( aNode.m_PinCount != m_pinCount )
  299. return false;
  300. }
  301. if( !m_fpFilters.empty() && filterByFPFilters() )
  302. {
  303. if( !patternMatch( aNode.m_LibId, m_fpFilters ) )
  304. return false;
  305. }
  306. return true;
  307. }
  308. void FOOTPRINT_CHOOSER_FRAME::doCloseWindow()
  309. {
  310. // Only dismiss a modal frame once, so that the return values set by
  311. // the prior DismissModal() are not bashed for ShowModal().
  312. if( !IsDismissed() )
  313. DismissModal( false );
  314. // window to be destroyed by the caller of KIWAY_PLAYER::ShowModal()
  315. }
  316. WINDOW_SETTINGS* FOOTPRINT_CHOOSER_FRAME::GetWindowSettings( APP_SETTINGS_BASE* aCfg )
  317. {
  318. PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( aCfg );
  319. wxCHECK_MSG( cfg, nullptr, wxT( "config not existing" ) );
  320. return &cfg->m_FootprintViewer;
  321. }
  322. COLOR_SETTINGS* FOOTPRINT_CHOOSER_FRAME::GetColorSettings( bool aForceRefresh ) const
  323. {
  324. auto* cfg = Pgm().GetSettingsManager().GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" );
  325. if( cfg )
  326. return Pgm().GetSettingsManager().GetColorSettings( cfg->m_ColorTheme );
  327. else
  328. return Pgm().GetSettingsManager().GetColorSettings();
  329. }
  330. static wxRect s_dialogRect( 0, 0, 0, 0 );
  331. void FOOTPRINT_CHOOSER_FRAME::KiwayMailIn( KIWAY_EXPRESS& mail )
  332. {
  333. const std::string& payload = mail.GetPayload();
  334. switch( mail.Command() )
  335. {
  336. case MAIL_SYMBOL_NETLIST:
  337. {
  338. wxSizer* filtersSizer = m_chooserPanel->GetFiltersSizer();
  339. wxWindow* filtersWindow = filtersSizer->GetContainingWindow();
  340. wxString msg;
  341. m_pinCount = 0;
  342. m_fpFilters.clear();
  343. /*
  344. * Symbol netlist format:
  345. * pinNumber pinName <tab> pinNumber pinName...
  346. * fpFilter fpFilter...
  347. */
  348. std::map<wxString, wxString> pinNames;
  349. std::vector<std::string> strings = split( payload, "\r" );
  350. if( strings.size() >= 1 && !strings[0].empty() )
  351. {
  352. for( const wxString& pin : wxSplit( strings[0], '\t' ) )
  353. pinNames[ pin.BeforeFirst( ' ' ) ] = pin.AfterFirst( ' ' );
  354. m_pinCount = pinNames.size();
  355. if( m_pinCount > 0 )
  356. {
  357. msg.Printf( _( "Filter by pin count (%d)" ), m_pinCount );
  358. m_filterByPinCount = new wxCheckBox( filtersWindow, wxID_ANY, msg );
  359. m_filterByPinCount->Bind( wxEVT_CHECKBOX,
  360. [&]( wxCommandEvent& evt )
  361. {
  362. m_chooserPanel->Regenerate();
  363. } );
  364. if( PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() ) )
  365. m_filterByPinCount->SetValue( cfg->m_FootprintChooser.filter_on_pin_count );
  366. }
  367. }
  368. if( strings.size() >= 2 && !strings[1].empty() )
  369. {
  370. for( const wxString& filter : wxSplit( strings[1], ' ' ) )
  371. {
  372. m_fpFilters.push_back( std::make_unique<EDA_PATTERN_MATCH_WILDCARD_ANCHORED>() );
  373. m_fpFilters.back()->SetPattern( filter.Lower() );
  374. }
  375. msg.Printf( _( "Apply footprint filters (%s)" ), strings[1] );
  376. m_filterByFPFilters = new wxCheckBox( filtersWindow, wxID_ANY, msg );
  377. m_filterByFPFilters->Bind( wxEVT_CHECKBOX,
  378. [&]( wxCommandEvent& evt )
  379. {
  380. m_chooserPanel->Regenerate();
  381. } );
  382. if( PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() ) )
  383. m_filterByFPFilters->SetValue( cfg->m_FootprintChooser.use_fp_filters );
  384. }
  385. if( m_filterByFPFilters )
  386. m_chooserPanel->GetFiltersSizer()->Add( m_filterByFPFilters, 0, wxEXPAND|wxBOTTOM, 4 );
  387. if( m_filterByPinCount )
  388. m_chooserPanel->GetFiltersSizer()->Add( m_filterByPinCount, 0, wxEXPAND|wxBOTTOM, 4 );
  389. m_chooserPanel->GetViewerPanel()->SetPinFunctions( pinNames );
  390. // Save the wxFormBuilder size of the dialog...
  391. if( s_dialogRect.GetSize().x == 0 || s_dialogRect.GetSize().y == 0 )
  392. s_dialogRect = wxRect( wxWindow::GetPosition(), wxWindow::GetSize() );
  393. // ... and then give it a kick to get it to layout the new items
  394. GetSizer()->SetSizeHints( this );
  395. break;
  396. }
  397. default:
  398. break;
  399. }
  400. }
  401. BOARD_ITEM_CONTAINER* FOOTPRINT_CHOOSER_FRAME::GetModel() const
  402. {
  403. return static_cast<FOOTPRINT_PREVIEW_PANEL*>( m_chooserPanel->GetViewerPanel()->GetPreviewPanel() )->GetCurrentFootprint();
  404. }
  405. bool FOOTPRINT_CHOOSER_FRAME::ShowModal( wxString* aFootprint, wxWindow* aParent )
  406. {
  407. if( aFootprint && !aFootprint->IsEmpty() )
  408. {
  409. LIB_ID fpid;
  410. fpid.Parse( *aFootprint, true );
  411. if( fpid.IsValid() )
  412. m_chooserPanel->SetPreselect( fpid );
  413. }
  414. return KIWAY_PLAYER::ShowModal( aFootprint, aParent );
  415. }
  416. void FOOTPRINT_CHOOSER_FRAME::SetPosition( const wxPoint& aNewPosition )
  417. {
  418. PCB_BASE_FRAME::SetPosition( aNewPosition );
  419. s_dialogRect.SetPosition( aNewPosition );
  420. }
  421. bool FOOTPRINT_CHOOSER_FRAME::Show( bool show )
  422. {
  423. bool ret;
  424. // Show or hide the window. If hiding, save current position and size.
  425. // If showing, use previous position and size.
  426. if( show )
  427. {
  428. #ifndef __WINDOWS__
  429. PCB_BASE_FRAME::Raise(); // Needed on OS X and some other window managers (i.e. Unity)
  430. #endif
  431. ret = PCB_BASE_FRAME::Show( show );
  432. // returns a zeroed-out default wxRect if none existed before.
  433. wxRect savedDialogRect = s_dialogRect;
  434. if( savedDialogRect.GetSize().x != 0 && savedDialogRect.GetSize().y != 0 )
  435. {
  436. SetSize( savedDialogRect.GetPosition().x, savedDialogRect.GetPosition().y,
  437. std::max( wxWindow::GetSize().x, savedDialogRect.GetSize().x ),
  438. std::max( wxWindow::GetSize().y, savedDialogRect.GetSize().y ),
  439. 0 );
  440. }
  441. // Be sure that the dialog appears in a visible area
  442. // (the dialog position might have been stored at the time when it was
  443. // shown on another display)
  444. if( wxDisplay::GetFromWindow( this ) == wxNOT_FOUND )
  445. Centre();
  446. }
  447. else
  448. {
  449. s_dialogRect = wxRect( wxWindow::GetPosition(), wxWindow::GetSize() );
  450. ret = PCB_BASE_FRAME::Show( show );
  451. }
  452. return ret;
  453. }
  454. void FOOTPRINT_CHOOSER_FRAME::OnPaint( wxPaintEvent& aEvent )
  455. {
  456. if( m_firstPaintEvent )
  457. {
  458. KIPLATFORM::UI::FixupCancelButtonCmdKeyCollision( this );
  459. KIPLATFORM::UI::ForceFocus( m_chooserPanel->GetFocusTarget() );
  460. m_firstPaintEvent = false;
  461. }
  462. aEvent.Skip();
  463. }
  464. void FOOTPRINT_CHOOSER_FRAME::OnOK( wxCommandEvent& aEvent )
  465. {
  466. LIB_ID fpID = m_chooserPanel->GetSelectedLibId();
  467. if( fpID.IsValid() )
  468. {
  469. wxString footprint = fpID.Format();
  470. AddFootprintToHistory( footprint );
  471. DismissModal( true, footprint );
  472. }
  473. else
  474. {
  475. DismissModal( false );
  476. }
  477. }
  478. void FOOTPRINT_CHOOSER_FRAME::closeFootprintChooser( wxCommandEvent& aEvent )
  479. {
  480. Close( false );
  481. }
  482. void FOOTPRINT_CHOOSER_FRAME::onFpChanged( wxCommandEvent& event )
  483. {
  484. updateViews();
  485. GetToolManager()->RunAction( ACTIONS::measureTool );
  486. }
  487. void FOOTPRINT_CHOOSER_FRAME::build3DCanvas()
  488. {
  489. // Create the dummy board used by the 3D canvas
  490. m_dummyBoard = GetBoard();
  491. m_dummyBoard->SetProject( &Prj(), true );
  492. // This board will only be used to hold a footprint for viewing
  493. m_dummyBoard->SetBoardUse( BOARD_USE::FPHOLDER );
  494. m_boardAdapter.SetBoard( m_dummyBoard );
  495. m_boardAdapter.m_IsBoardView = false;
  496. m_boardAdapter.m_IsPreviewer = true; // Force display 3D models, regardless the 3D viewer options
  497. SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
  498. EDA_3D_VIEWER_SETTINGS* cfg = mgr.GetAppSettings<EDA_3D_VIEWER_SETTINGS>( "3d_viewer" );
  499. m_boardAdapter.m_Cfg = cfg;
  500. // Build the 3D canvas
  501. m_preview3DCanvas = new EDA_3D_CANVAS( m_chooserPanel->m_RightPanel,
  502. OGL_ATT_LIST::GetAttributesList( ANTIALIASING_MODE::AA_8X ),
  503. m_boardAdapter, m_currentCamera,
  504. PROJECT_PCB::Get3DCacheManager( &Prj() ) );
  505. m_chooserPanel->m_RightPanelSizer->Add( m_preview3DCanvas, 1, wxEXPAND, 5 );
  506. m_chooserPanel->m_RightPanel->Layout();
  507. BOARD_DESIGN_SETTINGS& dummy_bds = m_dummyBoard->GetDesignSettings();
  508. dummy_bds.SetBoardThickness( pcbIUScale.mmToIU( 1.6 ) );
  509. dummy_bds.SetEnabledLayers( LSET::FrontMask() | LSET::BackMask() );
  510. BOARD_STACKUP& dummy_board_stackup = m_dummyBoard->GetDesignSettings().GetStackupDescriptor();
  511. dummy_board_stackup.RemoveAll();
  512. dummy_board_stackup.BuildDefaultStackupList( &dummy_bds, 2 );
  513. }
  514. void FOOTPRINT_CHOOSER_FRAME::on3DviewReq( wxCommandEvent& event )
  515. {
  516. if( m_show3DMode == true )
  517. {
  518. if( m_showFpMode == true )
  519. {
  520. m_show3DMode = false;
  521. m_grButton3DView->Check( m_show3DMode );
  522. updatePanelsVisibility();
  523. }
  524. }
  525. else
  526. {
  527. if( m_show3DViewer->IsChecked() )
  528. {
  529. Show3DViewerFrame();
  530. }
  531. else
  532. {
  533. // Close 3D viewer frame, if it is still enabled
  534. EDA_3D_VIEWER_FRAME* viewer3D = Get3DViewerFrame();
  535. if( viewer3D )
  536. viewer3D->Close( true );
  537. }
  538. m_show3DMode = true;
  539. m_grButton3DView->Check( m_show3DMode );
  540. updatePanelsVisibility();
  541. }
  542. }
  543. void FOOTPRINT_CHOOSER_FRAME::onFpViewReq( wxCommandEvent& event )
  544. {
  545. if( m_showFpMode == true )
  546. {
  547. if( m_show3DMode == true )
  548. {
  549. m_showFpMode = false;
  550. m_grButtonFpView->Check( m_showFpMode );
  551. updatePanelsVisibility();
  552. }
  553. }
  554. else
  555. {
  556. m_showFpMode = true;
  557. m_grButtonFpView->Check( m_showFpMode );
  558. updatePanelsVisibility();
  559. }
  560. }
  561. void FOOTPRINT_CHOOSER_FRAME::updateViews()
  562. {
  563. EDA_3D_VIEWER_FRAME* viewer3D = Get3DViewerFrame();
  564. bool reloadFp = viewer3D || m_preview3DCanvas->IsShown();
  565. if( reloadFp )
  566. {
  567. m_dummyBoard->DeleteAllFootprints();
  568. if( m_chooserPanel->m_CurrFootprint )
  569. m_dummyBoard->Add( (FOOTPRINT*)m_chooserPanel->m_CurrFootprint->Clone() );
  570. }
  571. if( m_preview3DCanvas->IsShown() )
  572. {
  573. m_preview3DCanvas->ReloadRequest();
  574. m_preview3DCanvas->Request_refresh();
  575. }
  576. if( viewer3D )
  577. {
  578. Update3DView( true, true );
  579. }
  580. m_chooserPanel->m_RightPanel->Layout();
  581. m_chooserPanel->m_RightPanel->Refresh();
  582. }
  583. void FOOTPRINT_CHOOSER_FRAME::updatePanelsVisibility()
  584. {
  585. FOOTPRINT_PREVIEW_WIDGET* viewFpPanel = m_chooserPanel->GetViewerPanel();
  586. viewFpPanel->Show( m_showFpMode );
  587. m_preview3DCanvas->Show( m_show3DMode );
  588. updateViews();
  589. }
  590. void FOOTPRINT_CHOOSER_FRAME::setupUIConditions()
  591. {
  592. PCB_BASE_FRAME::setupUIConditions();
  593. ACTION_MANAGER* mgr = m_toolManager->GetActionManager();
  594. PCB_EDITOR_CONDITIONS cond( this );
  595. wxASSERT( mgr );
  596. // clang-format off
  597. #define CHECK( x ) ACTION_CONDITIONS().Check( x )
  598. mgr->SetConditions( ACTIONS::toggleGrid, CHECK( cond.GridVisible() ) );
  599. mgr->SetConditions( ACTIONS::toggleCursorStyle, CHECK( cond.FullscreenCursor() ) );
  600. mgr->SetConditions( ACTIONS::millimetersUnits, CHECK( cond.Units( EDA_UNITS::MM ) ) );
  601. mgr->SetConditions( ACTIONS::inchesUnits, CHECK( cond.Units( EDA_UNITS::INCH ) ) );
  602. mgr->SetConditions( ACTIONS::milsUnits, CHECK( cond.Units( EDA_UNITS::MILS ) ) );
  603. mgr->SetConditions( PCB_ACTIONS::showPadNumbers, CHECK( cond.PadNumbersDisplay() ) );
  604. mgr->SetConditions( PCB_ACTIONS::padDisplayMode, CHECK( !cond.PadFillDisplay() ) );
  605. mgr->SetConditions( PCB_ACTIONS::textOutlines, CHECK( !cond.TextFillDisplay() ) );
  606. mgr->SetConditions( PCB_ACTIONS::graphicsOutlines, CHECK( !cond.GraphicsFillDisplay() ) );
  607. #undef CHECK
  608. // clang-format on
  609. }