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.

687 lines
23 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014 Henner Zeller <h.zeller@acm.org>
  5. * Copyright (C) 2016-2023 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 <symbol_library.h> // For SYMBOL_LIBRARY_FILTER
  26. #include <panel_symbol_chooser.h>
  27. #include <kiface_base.h>
  28. #include <sch_base_frame.h>
  29. #include <project_sch.h>
  30. #include <widgets/lib_tree.h>
  31. #include <widgets/symbol_preview_widget.h>
  32. #include <widgets/footprint_preview_widget.h>
  33. #include <widgets/footprint_select_widget.h>
  34. #include <settings/settings_manager.h>
  35. #include <project/project_file.h>
  36. #include <eeschema_settings.h>
  37. #include <symbol_editor_settings.h>
  38. #include <wx/button.h>
  39. #include <wx/clipbrd.h>
  40. #include <wx/panel.h>
  41. #include <wx/sizer.h>
  42. #include <wx/splitter.h>
  43. #include <wx/timer.h>
  44. #include <wx/wxhtml.h>
  45. wxString PANEL_SYMBOL_CHOOSER::g_symbolSearchString;
  46. wxString PANEL_SYMBOL_CHOOSER::g_powerSearchString;
  47. PANEL_SYMBOL_CHOOSER::PANEL_SYMBOL_CHOOSER( SCH_BASE_FRAME* aFrame, wxWindow* aParent,
  48. const SYMBOL_LIBRARY_FILTER* aFilter,
  49. std::vector<PICKED_SYMBOL>& aHistoryList,
  50. std::vector<PICKED_SYMBOL>& aAlreadyPlaced,
  51. bool aAllowFieldEdits, bool aShowFootprints,
  52. std::function<void()> aAcceptHandler,
  53. std::function<void()> aEscapeHandler ) :
  54. wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize ),
  55. m_symbol_preview( nullptr ),
  56. m_hsplitter( nullptr ),
  57. m_vsplitter( nullptr ),
  58. m_fp_sel_ctrl( nullptr ),
  59. m_fp_preview( nullptr ),
  60. m_tree( nullptr ),
  61. m_details( nullptr ),
  62. m_frame( aFrame ),
  63. m_acceptHandler( std::move( aAcceptHandler ) ),
  64. m_escapeHandler( std::move( aEscapeHandler ) ),
  65. m_showPower( false ),
  66. m_allow_field_edits( aAllowFieldEdits ),
  67. m_show_footprints( aShowFootprints )
  68. {
  69. SYMBOL_LIB_TABLE* libs = PROJECT_SCH::SchSymbolLibTable( &m_frame->Prj() );
  70. COMMON_SETTINGS::SESSION& session = Pgm().GetCommonSettings()->m_Session;
  71. PROJECT_FILE& project = m_frame->Prj().GetProjectFile();
  72. // Make sure settings are loaded before we start running multi-threaded symbol loaders
  73. Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
  74. Pgm().GetSettingsManager().GetAppSettings<SYMBOL_EDITOR_SETTINGS>();
  75. m_adapter = SYMBOL_TREE_MODEL_ADAPTER::Create( m_frame, libs );
  76. SYMBOL_TREE_MODEL_ADAPTER* adapter = static_cast<SYMBOL_TREE_MODEL_ADAPTER*>( m_adapter.get() );
  77. bool loaded = false;
  78. if( aFilter )
  79. {
  80. const wxArrayString& liblist = aFilter->GetAllowedLibList();
  81. for( const wxString& nickname : liblist )
  82. {
  83. if( libs->HasLibrary( nickname, true ) )
  84. {
  85. loaded = true;
  86. bool pinned = alg::contains( session.pinned_symbol_libs, nickname )
  87. || alg::contains( project.m_PinnedSymbolLibs, nickname );
  88. if( libs->FindRow( nickname )->GetIsVisible() )
  89. adapter->AddLibrary( nickname, pinned );
  90. }
  91. }
  92. adapter->AssignIntrinsicRanks();
  93. if( aFilter->GetFilterPowerSymbols() )
  94. {
  95. // HACK ALERT: when loading symbols we presume that *any* filter is a power symbol
  96. // filter. So the filter only needs to return true for libraries.
  97. static std::function<bool( LIB_TREE_NODE& )> powerFilter =
  98. []( LIB_TREE_NODE& aNode ) -> bool
  99. {
  100. return true;
  101. };
  102. adapter->SetFilter( &powerFilter );
  103. m_showPower = true;
  104. m_show_footprints = false;
  105. }
  106. }
  107. std::vector<LIB_SYMBOL> history_list_storage;
  108. std::vector<LIB_TREE_ITEM*> history_list;
  109. std::vector<LIB_SYMBOL> already_placed_storage;
  110. std::vector<LIB_TREE_ITEM*> already_placed;
  111. // Lambda to encapsulate the common logic
  112. auto processList = [&]( const std::vector<PICKED_SYMBOL>& inputList,
  113. std::vector<LIB_SYMBOL>& storageList,
  114. std::vector<LIB_TREE_ITEM*>& resultList )
  115. {
  116. storageList.reserve( inputList.size() );
  117. for( const PICKED_SYMBOL& i : inputList )
  118. {
  119. LIB_SYMBOL* symbol = m_frame->GetLibSymbol( i.LibId );
  120. if( symbol )
  121. {
  122. storageList.emplace_back( *symbol );
  123. for( const std::pair<int, wxString>& fieldDef : i.Fields )
  124. {
  125. LIB_FIELD* field = storageList.back().GetFieldById( fieldDef.first );
  126. if( field )
  127. field->SetText( fieldDef.second );
  128. }
  129. resultList.push_back( &storageList.back() );
  130. }
  131. }
  132. };
  133. // Sort the already placed list since it is potentially from multiple sessions,
  134. // but not the most recent list since we want this listed by most recent usage.
  135. std::sort( aAlreadyPlaced.begin(), aAlreadyPlaced.end(),
  136. []( PICKED_SYMBOL const& a, PICKED_SYMBOL const& b )
  137. {
  138. return a.LibId.GetLibItemName() < b.LibId.GetLibItemName();
  139. } );
  140. processList( aHistoryList, history_list_storage, history_list );
  141. processList( aAlreadyPlaced, already_placed_storage, already_placed );
  142. adapter->DoAddLibrary( wxT( "-- " ) + _( "Recently Used" ) + wxT( " --" ), wxEmptyString,
  143. history_list, false, true );
  144. if( !aHistoryList.empty() )
  145. adapter->SetPreselectNode( aHistoryList[0].LibId, aHistoryList[0].Unit );
  146. adapter->DoAddLibrary( wxT( "-- " ) + _( "Already Placed" ) + wxT( " --" ), wxEmptyString,
  147. already_placed, false, true );
  148. const std::vector< wxString > libNicknames = libs->GetLogicalLibs();
  149. if( !loaded )
  150. {
  151. if( !adapter->AddLibraries( libNicknames, m_frame ) )
  152. {
  153. // loading cancelled by user
  154. m_acceptHandler();
  155. }
  156. }
  157. // -------------------------------------------------------------------------------------
  158. // Construct the actual panel
  159. //
  160. wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
  161. // Use a slightly different layout, with a details pane spanning the entire window,
  162. // if we're not showing footprints.
  163. if( m_show_footprints )
  164. {
  165. m_hsplitter = new wxSplitterWindow( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
  166. wxSP_LIVE_UPDATE | wxSP_NOBORDER | wxSP_3DSASH );
  167. //Avoid the splitter window being assigned as the Parent to additional windows
  168. m_hsplitter->SetExtraStyle( wxWS_EX_TRANSIENT );
  169. sizer->Add( m_hsplitter, 1, wxEXPAND, 5 );
  170. }
  171. else
  172. {
  173. m_vsplitter = new wxSplitterWindow( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
  174. wxSP_LIVE_UPDATE | wxSP_NOBORDER | wxSP_3DSASH );
  175. m_hsplitter = new wxSplitterWindow( m_vsplitter, wxID_ANY, wxDefaultPosition, wxDefaultSize,
  176. wxSP_LIVE_UPDATE | wxSP_NOBORDER | wxSP_3DSASH );
  177. // Avoid the splitter window being assigned as the parent to additional windows.
  178. m_vsplitter->SetExtraStyle( wxWS_EX_TRANSIENT );
  179. m_hsplitter->SetExtraStyle( wxWS_EX_TRANSIENT );
  180. wxPanel* detailsPanel = new wxPanel( m_vsplitter );
  181. wxBoxSizer* detailsSizer = new wxBoxSizer( wxVERTICAL );
  182. detailsPanel->SetSizer( detailsSizer );
  183. m_details = new HTML_WINDOW( detailsPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize );
  184. detailsSizer->Add( m_details, 1, wxEXPAND, 5 );
  185. detailsPanel->Layout();
  186. detailsSizer->Fit( detailsPanel );
  187. m_vsplitter->SetSashGravity( 0.5 );
  188. m_vsplitter->SetMinimumPaneSize( 20 );
  189. m_vsplitter->SplitHorizontally( m_hsplitter, detailsPanel );
  190. sizer->Add( m_vsplitter, 1, wxEXPAND, 5 );
  191. }
  192. wxPanel* treePanel = new wxPanel( m_hsplitter );
  193. wxBoxSizer* treeSizer = new wxBoxSizer( wxVERTICAL );
  194. treePanel->SetSizer( treeSizer );
  195. m_tree = new LIB_TREE( treePanel, m_showPower ? wxT( "power" ) : wxT( "symbols" ),
  196. libs, m_adapter, LIB_TREE::FLAGS::ALL_WIDGETS, m_details );
  197. treeSizer->Add( m_tree, 1, wxEXPAND, 5 );
  198. treePanel->Layout();
  199. treeSizer->Fit( treePanel );
  200. m_adapter->FinishTreeInitialization();
  201. if( m_showPower )
  202. m_tree->SetSearchString( g_powerSearchString );
  203. else
  204. m_tree->SetSearchString( g_symbolSearchString );
  205. m_hsplitter->SetSashGravity( 0.8 );
  206. m_hsplitter->SetMinimumPaneSize( 20 );
  207. m_hsplitter->SplitVertically( treePanel, constructRightPanel( m_hsplitter ) );
  208. m_dbl_click_timer = new wxTimer( this );
  209. m_open_libs_timer = new wxTimer( this );
  210. SetSizer( sizer );
  211. Layout();
  212. Bind( wxEVT_TIMER, &PANEL_SYMBOL_CHOOSER::onCloseTimer, this, m_dbl_click_timer->GetId() );
  213. Bind( wxEVT_TIMER, &PANEL_SYMBOL_CHOOSER::onOpenLibsTimer, this, m_open_libs_timer->GetId() );
  214. Bind( EVT_LIBITEM_SELECTED, &PANEL_SYMBOL_CHOOSER::onSymbolSelected, this );
  215. Bind( EVT_LIBITEM_CHOSEN, &PANEL_SYMBOL_CHOOSER::onSymbolChosen, this );
  216. Bind( wxEVT_CHAR_HOOK, &PANEL_SYMBOL_CHOOSER::OnChar, this );
  217. if( m_fp_sel_ctrl )
  218. {
  219. m_fp_sel_ctrl->Bind( EVT_FOOTPRINT_SELECTED, &PANEL_SYMBOL_CHOOSER::onFootprintSelected,
  220. this );
  221. }
  222. if( m_details )
  223. {
  224. m_details->Connect( wxEVT_CHAR_HOOK,
  225. wxKeyEventHandler( PANEL_SYMBOL_CHOOSER::OnDetailsCharHook ),
  226. nullptr, this );
  227. }
  228. // Open the user's previously opened libraries on timer expiration.
  229. // This is done on a timer because we need a gross hack to keep GTK from garbling the
  230. // display. Must be longer than the search debounce timer.
  231. m_open_libs_timer->StartOnce( 300 );
  232. }
  233. PANEL_SYMBOL_CHOOSER::~PANEL_SYMBOL_CHOOSER()
  234. {
  235. Unbind( wxEVT_TIMER, &PANEL_SYMBOL_CHOOSER::onCloseTimer, this );
  236. Unbind( EVT_LIBITEM_SELECTED, &PANEL_SYMBOL_CHOOSER::onSymbolSelected, this );
  237. Unbind( EVT_LIBITEM_CHOSEN, &PANEL_SYMBOL_CHOOSER::onSymbolChosen, this );
  238. Unbind( wxEVT_CHAR_HOOK, &PANEL_SYMBOL_CHOOSER::OnChar, this );
  239. // Stop the timer during destruction early to avoid potential race conditions (that do happen)
  240. m_dbl_click_timer->Stop();
  241. m_open_libs_timer->Stop();
  242. delete m_dbl_click_timer;
  243. delete m_open_libs_timer;
  244. if( m_showPower )
  245. g_powerSearchString = m_tree->GetSearchString();
  246. else
  247. g_symbolSearchString = m_tree->GetSearchString();
  248. if( m_fp_sel_ctrl )
  249. {
  250. m_fp_sel_ctrl->Unbind( EVT_FOOTPRINT_SELECTED, &PANEL_SYMBOL_CHOOSER::onFootprintSelected,
  251. this );
  252. }
  253. if( m_details )
  254. {
  255. m_details->Disconnect( wxEVT_CHAR_HOOK,
  256. wxKeyEventHandler( PANEL_SYMBOL_CHOOSER::OnDetailsCharHook ),
  257. nullptr, this );
  258. }
  259. if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
  260. {
  261. // Save any changes to column widths, etc.
  262. m_adapter->SaveSettings();
  263. cfg->m_SymChooserPanel.width = GetParent()->GetSize().x;
  264. cfg->m_SymChooserPanel.height = GetParent()->GetSize().y;
  265. cfg->m_SymChooserPanel.sash_pos_h = m_hsplitter->GetSashPosition();
  266. if( m_vsplitter )
  267. cfg->m_SymChooserPanel.sash_pos_v = m_vsplitter->GetSashPosition();
  268. cfg->m_SymChooserPanel.sort_mode = m_tree->GetSortMode();
  269. }
  270. }
  271. void PANEL_SYMBOL_CHOOSER::OnChar( wxKeyEvent& aEvent )
  272. {
  273. if( aEvent.GetKeyCode() == WXK_ESCAPE )
  274. {
  275. wxObject* eventSource = aEvent.GetEventObject();
  276. if( wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( eventSource ) )
  277. {
  278. // First escape cancels search string value
  279. if( textCtrl->GetValue() == m_tree->GetSearchString()
  280. && !m_tree->GetSearchString().IsEmpty() )
  281. {
  282. m_tree->SetSearchString( wxEmptyString );
  283. return;
  284. }
  285. }
  286. m_escapeHandler();
  287. }
  288. else
  289. {
  290. aEvent.Skip();
  291. }
  292. }
  293. wxPanel* PANEL_SYMBOL_CHOOSER::constructRightPanel( wxWindow* aParent )
  294. {
  295. EDA_DRAW_PANEL_GAL::GAL_TYPE backend;
  296. if( m_frame->GetCanvas() )
  297. {
  298. backend = m_frame->GetCanvas()->GetBackend();
  299. }
  300. else
  301. {
  302. EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
  303. backend = (EDA_DRAW_PANEL_GAL::GAL_TYPE) cfg->m_Graphics.canvas_type;
  304. }
  305. wxPanel* panel = new wxPanel( aParent );
  306. wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
  307. m_symbol_preview = new SYMBOL_PREVIEW_WIDGET( panel, &m_frame->Kiway(), true, backend );
  308. m_symbol_preview->SetLayoutDirection( wxLayout_LeftToRight );
  309. if( m_show_footprints )
  310. {
  311. FOOTPRINT_LIST* fp_list = FOOTPRINT_LIST::GetInstance( m_frame->Kiway() );
  312. sizer->Add( m_symbol_preview, 11, wxEXPAND | wxBOTTOM, 5 );
  313. if ( fp_list )
  314. {
  315. if( m_allow_field_edits )
  316. m_fp_sel_ctrl = new FOOTPRINT_SELECT_WIDGET( m_frame, panel, fp_list, true );
  317. m_fp_preview = new FOOTPRINT_PREVIEW_WIDGET( panel, m_frame->Kiway() );
  318. m_fp_preview->SetUserUnits( m_frame->GetUserUnits() );
  319. }
  320. if( m_fp_sel_ctrl )
  321. sizer->Add( m_fp_sel_ctrl, 0, wxEXPAND | wxTOP | wxBOTTOM, 4 );
  322. if( m_fp_preview )
  323. sizer->Add( m_fp_preview, 10, wxEXPAND, 5 );
  324. }
  325. else
  326. {
  327. sizer->Add( m_symbol_preview, 1, wxEXPAND, 5 );
  328. }
  329. panel->SetSizer( sizer );
  330. panel->Layout();
  331. sizer->Fit( panel );
  332. return panel;
  333. }
  334. void PANEL_SYMBOL_CHOOSER::FinishSetup()
  335. {
  336. if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
  337. {
  338. auto horizPixelsFromDU =
  339. [&]( int x ) -> int
  340. {
  341. wxSize sz( x, 0 );
  342. return GetParent()->ConvertDialogToPixels( sz ).x;
  343. };
  344. EESCHEMA_SETTINGS::PANEL_SYM_CHOOSER& panelCfg = cfg->m_SymChooserPanel;
  345. int w = panelCfg.width > 40 ? panelCfg.width : horizPixelsFromDU( 440 );
  346. int h = panelCfg.height > 40 ? panelCfg.height : horizPixelsFromDU( 340 );
  347. GetParent()->SetSize( wxSize( w, h ) );
  348. GetParent()->Layout();
  349. // We specify the width of the right window (m_symbol_view_panel), because specify
  350. // the width of the left window does not work as expected when SetSashGravity() is called
  351. if( panelCfg.sash_pos_h < 0 )
  352. panelCfg.sash_pos_h = horizPixelsFromDU( 220 );
  353. if( panelCfg.sash_pos_v < 0 )
  354. panelCfg.sash_pos_v = horizPixelsFromDU( 230 );
  355. m_hsplitter->SetSashPosition( panelCfg.sash_pos_h );
  356. if( m_vsplitter )
  357. m_vsplitter->SetSashPosition( panelCfg.sash_pos_v );
  358. m_adapter->SetSortMode( (LIB_TREE_MODEL_ADAPTER::SORT_MODE) panelCfg.sort_mode );
  359. }
  360. if( m_fp_preview && m_fp_preview->IsInitialized() )
  361. {
  362. // This hides the GAL panel and shows the status label
  363. m_fp_preview->SetStatusText( wxEmptyString );
  364. }
  365. if( m_fp_sel_ctrl )
  366. m_fp_sel_ctrl->Load( m_frame->Kiway(), m_frame->Prj() );
  367. }
  368. void PANEL_SYMBOL_CHOOSER::OnDetailsCharHook( wxKeyEvent& e )
  369. {
  370. if( m_details && e.GetKeyCode() == 'C' && e.ControlDown() &&
  371. !e.AltDown() && !e.ShiftDown() && !e.MetaDown() )
  372. {
  373. wxString txt = m_details->SelectionToText();
  374. wxLogNull doNotLog; // disable logging of failed clipboard actions
  375. if( wxTheClipboard->Open() )
  376. {
  377. wxTheClipboard->SetData( new wxTextDataObject( txt ) );
  378. wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
  379. wxTheClipboard->Close();
  380. }
  381. }
  382. else
  383. {
  384. e.Skip();
  385. }
  386. }
  387. void PANEL_SYMBOL_CHOOSER::SetPreselect( const LIB_ID& aPreselect )
  388. {
  389. m_adapter->SetPreselectNode( aPreselect, 0 );
  390. }
  391. LIB_ID PANEL_SYMBOL_CHOOSER::GetSelectedLibId( int* aUnit ) const
  392. {
  393. return m_tree->GetSelectedLibId( aUnit );
  394. }
  395. void PANEL_SYMBOL_CHOOSER::onCloseTimer( wxTimerEvent& aEvent )
  396. {
  397. // Hack because of eaten MouseUp event. See PANEL_SYMBOL_CHOOSER::onSymbolChosen
  398. // for the beginning of this spaghetti noodle.
  399. wxMouseState state = wxGetMouseState();
  400. if( state.LeftIsDown() )
  401. {
  402. // Mouse hasn't been raised yet, so fire the timer again. Otherwise the
  403. // purpose of this timer is defeated.
  404. m_dbl_click_timer->StartOnce( PANEL_SYMBOL_CHOOSER::DBLCLICK_DELAY );
  405. }
  406. else
  407. {
  408. m_acceptHandler();
  409. }
  410. }
  411. void PANEL_SYMBOL_CHOOSER::onOpenLibsTimer( wxTimerEvent& aEvent )
  412. {
  413. if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
  414. m_adapter->OpenLibs( cfg->m_LibTree.open_libs );
  415. }
  416. void PANEL_SYMBOL_CHOOSER::showFootprintFor( LIB_ID const& aLibId )
  417. {
  418. if( !m_fp_preview || !m_fp_preview->IsInitialized() )
  419. return;
  420. LIB_SYMBOL* symbol = nullptr;
  421. try
  422. {
  423. symbol = PROJECT_SCH::SchSymbolLibTable( &m_frame->Prj() )->LoadSymbol( aLibId );
  424. }
  425. catch( const IO_ERROR& ioe )
  426. {
  427. wxLogError( _( "Error loading symbol %s from library '%s'." ) + wxS( "\n%s" ),
  428. aLibId.GetLibItemName().wx_str(),
  429. aLibId.GetLibNickname().wx_str(),
  430. ioe.What() );
  431. }
  432. if( !symbol )
  433. return;
  434. LIB_FIELD* fp_field = symbol->GetFieldById( FOOTPRINT_FIELD );
  435. wxString fp_name = fp_field ? fp_field->GetFullText() : wxString( "" );
  436. showFootprint( fp_name );
  437. }
  438. void PANEL_SYMBOL_CHOOSER::showFootprint( wxString const& aFootprint )
  439. {
  440. if( !m_fp_preview || !m_fp_preview->IsInitialized() )
  441. return;
  442. if( aFootprint == wxEmptyString )
  443. {
  444. m_fp_preview->SetStatusText( _( "No footprint specified" ) );
  445. }
  446. else
  447. {
  448. LIB_ID lib_id;
  449. if( lib_id.Parse( aFootprint ) == -1 && lib_id.IsValid() )
  450. {
  451. m_fp_preview->ClearStatus();
  452. m_fp_preview->DisplayFootprint( lib_id );
  453. }
  454. else
  455. {
  456. m_fp_preview->SetStatusText( _( "Invalid footprint specified" ) );
  457. }
  458. }
  459. }
  460. void PANEL_SYMBOL_CHOOSER::populateFootprintSelector( LIB_ID const& aLibId )
  461. {
  462. if( !m_fp_sel_ctrl )
  463. return;
  464. m_fp_sel_ctrl->ClearFilters();
  465. LIB_SYMBOL* symbol = nullptr;
  466. if( aLibId.IsValid() )
  467. {
  468. try
  469. {
  470. symbol = PROJECT_SCH::SchSymbolLibTable( &m_frame->Prj() )->LoadSymbol( aLibId );
  471. }
  472. catch( const IO_ERROR& ioe )
  473. {
  474. wxLogError( _( "Error loading symbol %s from library '%s'." ) + wxS( "\n%s" ),
  475. aLibId.GetLibItemName().wx_str(),
  476. aLibId.GetLibNickname().wx_str(),
  477. ioe.What() );
  478. }
  479. }
  480. if( symbol != nullptr )
  481. {
  482. LIB_PINS temp_pins;
  483. LIB_FIELD* fp_field = symbol->GetFieldById( FOOTPRINT_FIELD );
  484. wxString fp_name = fp_field ? fp_field->GetFullText() : wxString( "" );
  485. // All units, but only a single De Morgan variant.
  486. if( symbol->HasConversion() )
  487. symbol->GetPins( temp_pins, 0, 1 );
  488. else
  489. symbol->GetPins( temp_pins );
  490. m_fp_sel_ctrl->FilterByPinCount( temp_pins.size() );
  491. m_fp_sel_ctrl->FilterByFootprintFilters( symbol->GetFPFilters(), true );
  492. m_fp_sel_ctrl->SetDefaultFootprint( fp_name );
  493. m_fp_sel_ctrl->UpdateList();
  494. m_fp_sel_ctrl->Enable();
  495. }
  496. else
  497. {
  498. m_fp_sel_ctrl->UpdateList();
  499. m_fp_sel_ctrl->Disable();
  500. }
  501. }
  502. void PANEL_SYMBOL_CHOOSER::onFootprintSelected( wxCommandEvent& aEvent )
  503. {
  504. m_fp_override = aEvent.GetString();
  505. alg::delete_if( m_field_edits, []( std::pair<int, wxString> const& i )
  506. {
  507. return i.first == FOOTPRINT_FIELD;
  508. } );
  509. m_field_edits.emplace_back( std::make_pair( FOOTPRINT_FIELD, m_fp_override ) );
  510. showFootprint( m_fp_override );
  511. }
  512. void PANEL_SYMBOL_CHOOSER::onSymbolSelected( wxCommandEvent& aEvent )
  513. {
  514. LIB_TREE_NODE* node = m_tree->GetCurrentTreeNode();
  515. if( node && node->m_LibId.IsValid() )
  516. {
  517. m_symbol_preview->DisplaySymbol( node->m_LibId, node->m_Unit );
  518. if( !node->m_Footprint.IsEmpty() )
  519. showFootprint( node->m_Footprint );
  520. else
  521. showFootprintFor( node->m_LibId );
  522. populateFootprintSelector( node->m_LibId );
  523. }
  524. else
  525. {
  526. m_symbol_preview->SetStatusText( _( "No symbol selected" ) );
  527. if( m_fp_preview && m_fp_preview->IsInitialized() )
  528. m_fp_preview->SetStatusText( wxEmptyString );
  529. populateFootprintSelector( LIB_ID() );
  530. }
  531. }
  532. void PANEL_SYMBOL_CHOOSER::onSymbolChosen( wxCommandEvent& aEvent )
  533. {
  534. if( m_tree->GetSelectedLibId().IsValid() )
  535. {
  536. // Got a selection. We can't just end the modal dialog here, because wx leaks some events
  537. // back to the parent window (in particular, the MouseUp following a double click).
  538. //
  539. // NOW, here's where it gets really fun. wxTreeListCtrl eats MouseUp. This isn't really
  540. // feasible to bypass without a fully custom wxDataViewCtrl implementation, and even then
  541. // might not be fully possible (docs are vague). To get around this, we use a one-shot
  542. // timer to schedule the dialog close.
  543. //
  544. // See PANEL_SYMBOL_CHOOSER::onCloseTimer for the other end of this spaghetti noodle.
  545. m_dbl_click_timer->StartOnce( PANEL_SYMBOL_CHOOSER::DBLCLICK_DELAY );
  546. }
  547. }