KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_symbol_chooser.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2014 Henner Zeller <[email protected]>
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, see <https://www.gnu.org/licenses/>.
19 */
20
22
23#include <pgm_base.h>
25#include <kiface_base.h>
26#include <sch_base_frame.h>
27#include <project_sch.h>
29#include <widgets/lib_tree.h>
35#include <eeschema_settings.h>
37#include <string_utils.h>
38#include <symbol_library_common.h> // For SYMBOL_LIBRARY_FILTER
39#include <algorithm>
40#include <wx/button.h>
41#include <wx/clipbrd.h>
42#include <wx/display.h>
43#include <wx/panel.h>
44#include <wx/sizer.h>
45#include <wx/splitter.h>
46#include <wx/timer.h>
47#include <wx/wxhtml.h>
48#include <wx/log.h>
49
50
54
56 const SYMBOL_LIBRARY_FILTER* aFilter,
57 std::vector<PICKED_SYMBOL>& aHistoryList,
58 std::vector<PICKED_SYMBOL>& aAlreadyPlaced,
59 bool aAllowFieldEdits, bool aShowFootprints, bool& aCancelled,
60 std::function<void()> aAcceptHandler,
61 std::function<void()> aEscapeHandler ) :
62 wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize ),
63 m_symbol_preview( nullptr ),
64 m_hsplitter( nullptr ),
65 m_vsplitter( nullptr ),
66 m_fp_sel_ctrl( nullptr ),
67 m_fp_preview( nullptr ),
68 m_tree( nullptr ),
69 m_details( nullptr ),
70 m_acceptHandler( std::move( aAcceptHandler ) ),
71 m_escapeHandler( std::move( aEscapeHandler ) ),
72 m_showPower( false ),
73 m_allow_field_edits( aAllowFieldEdits ),
74 m_show_footprints( aShowFootprints )
75{
76 m_frame = aFrame;
77
80 PROJECT_FILE& project = m_frame->Prj().GetProjectFile();
81
82 // Make sure settings are loaded before we start running multi-threaded symbol loaders
85
87 SYMBOL_TREE_MODEL_ADAPTER* adapter = static_cast<SYMBOL_TREE_MODEL_ADAPTER*>( m_adapter.get() );
88
89 if( aFilter )
90 {
91 const wxArrayString& liblist = aFilter->GetAllowedLibList();
92
93 for( const wxString& nickname : liblist )
94 {
95 if( libmgr->HasLibrary( nickname, true ) )
96 {
97 bool pinned = alg::contains( session.pinned_symbol_libs, nickname )
98 || alg::contains( project.m_PinnedSymbolLibs, nickname );
99
100 std::optional<LIBRARY_TABLE_ROW*> row = libmgr->GetRow( nickname );
101
102 if( row.has_value() && !row.value()->Hidden() )
103 adapter->AddLibrary( nickname, pinned );
104 }
105 }
106
107 adapter->AssignIntrinsicRanks();
108
109 if( aFilter->GetFilterPowerSymbols() )
110 {
111 static std::function<bool( LIB_TREE_NODE& )> powerFilter =
112 []( LIB_TREE_NODE& aNode ) -> bool
113 {
114 return aNode.m_IsPower;
115 };
116
117 adapter->SetFilter( &powerFilter );
118
119 m_showPower = true;
120 m_show_footprints = false;
121 }
122 }
123
124 std::vector<LIB_SYMBOL> history_list_storage;
125 std::vector<LIB_TREE_ITEM*> history_list;
126 std::vector<LIB_SYMBOL> already_placed_storage;
127 std::vector<LIB_TREE_ITEM*> already_placed;
128
129 // Lambda to encapsulate the common logic
130 auto processList =
131 [&]( const std::vector<PICKED_SYMBOL>& inputList,
132 std::vector<LIB_SYMBOL>& storageList,
133 std::vector<LIB_TREE_ITEM*>& resultList )
134 {
135 storageList.reserve( inputList.size() );
136
137 for( const PICKED_SYMBOL& i : inputList )
138 {
139 LIB_SYMBOL* symbol = m_frame->GetLibSymbol( i.LibId );
140
141 if( symbol )
142 {
143 storageList.emplace_back( *symbol );
144
145 for( const auto& [fieldType, fieldValue] : i.Fields )
146 {
147 SCH_FIELD* field = storageList.back().GetField( fieldType );
148
149 if( field )
150 field->SetText( fieldValue );
151 }
152
153 resultList.push_back( &storageList.back() );
154 }
155 }
156 };
157
158 // Sort the already placed list since it is potentially from multiple sessions,
159 // but not the most recent list since we want this listed by most recent usage.
160 std::sort( aAlreadyPlaced.begin(), aAlreadyPlaced.end(),
161 []( PICKED_SYMBOL const& a, PICKED_SYMBOL const& b )
162 {
163 return a.LibId.GetLibItemName() < b.LibId.GetLibItemName();
164 } );
165
166 processList( aHistoryList, history_list_storage, history_list );
167 processList( aAlreadyPlaced, already_placed_storage, already_placed );
168
169 adapter->DoAddLibrary( wxT( "-- " ) + _( "Recently Used" ) + wxT( " --" ), wxEmptyString,
170 history_list, false, true )
171 .m_IsRecentlyUsedGroup = true;
172
173 if( !aHistoryList.empty() )
174 adapter->SetPreselectNode( aHistoryList[0].LibId, aHistoryList[0].Unit );
175
176 adapter->DoAddLibrary( wxT( "-- " ) + _( "Already Placed" ) + wxT( " --" ), wxEmptyString,
177 already_placed, false, true )
179
180 adapter->AddLibraries( m_frame );
181
182 // -------------------------------------------------------------------------------------
183 // Construct the actual panel
184 //
185
186 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
187
188 // Use a slightly different layout, with a details pane spanning the entire window,
189 // if we're not showing footprints.
191 {
192 m_hsplitter = new wxSplitterWindow( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
193 wxSP_LIVE_UPDATE | wxSP_NOBORDER | wxSP_3DSASH );
194
195 //Avoid the splitter window being assigned as the Parent to additional windows
196 m_hsplitter->SetExtraStyle( wxWS_EX_TRANSIENT );
197
198 sizer->Add( m_hsplitter, 1, wxEXPAND, 5 );
199 }
200 else
201 {
202 m_vsplitter = new wxSplitterWindow( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
203 wxSP_LIVE_UPDATE | wxSP_NOBORDER | wxSP_3DSASH );
204
205 m_hsplitter = new wxSplitterWindow( m_vsplitter, wxID_ANY, wxDefaultPosition, wxDefaultSize,
206 wxSP_LIVE_UPDATE | wxSP_NOBORDER | wxSP_3DSASH );
207
208 // Avoid the splitter window being assigned as the parent to additional windows.
209 m_vsplitter->SetExtraStyle( wxWS_EX_TRANSIENT );
210 m_hsplitter->SetExtraStyle( wxWS_EX_TRANSIENT );
211
212 wxPanel* detailsPanel = new wxPanel( m_vsplitter );
213 wxBoxSizer* detailsSizer = new wxBoxSizer( wxVERTICAL );
214 detailsPanel->SetSizer( detailsSizer );
215
216 m_details = new HTML_WINDOW( detailsPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize );
217 detailsSizer->Add( m_details, 1, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 5 );
218 detailsPanel->Layout();
219 detailsSizer->Fit( detailsPanel );
220
221 m_vsplitter->SetSashGravity( 0.5 );
222 m_vsplitter->SetMinimumPaneSize( 20 );
223 m_vsplitter->SplitHorizontally( m_hsplitter, detailsPanel );
224
225 sizer->Add( m_vsplitter, 1, wxEXPAND | wxBOTTOM, 5 );
226 }
227
228 wxPanel* treePanel = new wxPanel( m_hsplitter );
229 wxBoxSizer* treeSizer = new wxBoxSizer( wxVERTICAL );
230 treePanel->SetSizer( treeSizer );
231
232 m_tree = new LIB_TREE( treePanel, m_showPower ? wxT( "power" ) : wxT( "symbols" ), m_adapter,
234
235 treeSizer->Add( m_tree, 1, wxALL | wxEXPAND, 5 );
236 treePanel->Layout();
237 treeSizer->Fit( treePanel );
238
239 m_adapter->FinishTreeInitialization();
240
241 if( m_showPower )
242 m_tree->SetSearchString( g_powerSearchString );
243 else
244 m_tree->SetSearchString( g_symbolSearchString );
245
246 m_hsplitter->SetSashGravity( 0.8 );
247 m_hsplitter->SetMinimumPaneSize( 20 );
248 m_hsplitter->SplitVertically( treePanel, constructRightPanel( m_hsplitter ) );
249
250 m_dbl_click_timer = new wxTimer( this );
251 m_open_libs_timer = new wxTimer( this );
252
253 SetSizer( sizer );
254
255 Layout();
256
257 Bind( wxEVT_TIMER, &PANEL_SYMBOL_CHOOSER::onCloseTimer, this, m_dbl_click_timer->GetId() );
258 Bind( wxEVT_TIMER, &PANEL_SYMBOL_CHOOSER::onOpenLibsTimer, this, m_open_libs_timer->GetId() );
259 Bind( EVT_LIBITEM_SELECTED, &PANEL_SYMBOL_CHOOSER::onSymbolSelected, this );
260 Bind( EVT_LIBITEM_CHOSEN, &PANEL_SYMBOL_CHOOSER::onSymbolChosen, this );
261 aFrame->Bind( wxEVT_MENU_OPEN, &PANEL_SYMBOL_CHOOSER::onMenuOpen, this );
262 aFrame->Bind( wxEVT_MENU_CLOSE, &PANEL_SYMBOL_CHOOSER::onMenuClose, this );
263
264 if( m_fp_sel_ctrl )
265 m_fp_sel_ctrl->Bind( EVT_FOOTPRINT_SELECTED, &PANEL_SYMBOL_CHOOSER::onFootprintSelected, this );
266
267 if( m_details )
268 m_details->Bind( wxEVT_CHAR_HOOK, &PANEL_SYMBOL_CHOOSER::OnDetailsCharHook, this );
269
270 // Open the user's previously opened libraries on timer expiration.
271 // This is done on a timer because we need a gross hack to keep GTK from garbling the
272 // display. Must be longer than the search debounce timer.
273 m_open_libs_timer->StartOnce( 300 );
274}
275
276
278{
279 m_frame->Unbind( wxEVT_MENU_OPEN, &PANEL_SYMBOL_CHOOSER::onMenuOpen, this );
280 m_frame->Unbind( wxEVT_MENU_CLOSE, &PANEL_SYMBOL_CHOOSER::onMenuClose, this );
281 Unbind( wxEVT_TIMER, &PANEL_SYMBOL_CHOOSER::onCloseTimer, this );
282 Unbind( EVT_LIBITEM_SELECTED, &PANEL_SYMBOL_CHOOSER::onSymbolSelected, this );
283 Unbind( EVT_LIBITEM_CHOSEN, &PANEL_SYMBOL_CHOOSER::onSymbolChosen, this );
284
285 // Stop the timer during destruction early to avoid potential race conditions (that do happen)
286 m_dbl_click_timer->Stop();
287 m_open_libs_timer->Stop();
288 delete m_dbl_click_timer;
289 delete m_open_libs_timer;
290
291 if( m_showPower )
292 g_powerSearchString = m_tree->GetSearchString();
293 else
294 g_symbolSearchString = m_tree->GetSearchString();
295
296 if( m_fp_sel_ctrl )
297 m_fp_sel_ctrl->Unbind( EVT_FOOTPRINT_SELECTED, &PANEL_SYMBOL_CHOOSER::onFootprintSelected, this );
298
299 if( m_details )
300 m_details->Unbind( wxEVT_CHAR_HOOK, &PANEL_SYMBOL_CHOOSER::OnDetailsCharHook, this );
301
302 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
303 {
304 // Save any changes to column widths, etc.
305 m_adapter->SaveSettings();
306
307 cfg->m_SymChooserPanel.width = GetParent()->ToDIP( GetParent()->GetSize().x );
308 cfg->m_SymChooserPanel.height = GetParent()->ToDIP( GetParent()->GetSize().y );
309
310 cfg->m_SymChooserPanel.sash_pos_h = m_hsplitter->GetSashPosition();
311
312 if( m_vsplitter )
313 cfg->m_SymChooserPanel.sash_pos_v = m_vsplitter->GetSashPosition();
314
315 cfg->m_SymChooserPanel.sort_mode = m_tree->GetSortMode();
316 }
317
318 m_frame = nullptr;
319}
320
321
322void PANEL_SYMBOL_CHOOSER::onMenuOpen( wxMenuEvent& aEvent )
323{
324 m_tree->BlockPreview( true );
325 aEvent.Skip();
326}
327
328
329void PANEL_SYMBOL_CHOOSER::onMenuClose( wxMenuEvent& aEvent )
330{
331 m_tree->BlockPreview( false );
332 aEvent.Skip();
333}
334
335
336void PANEL_SYMBOL_CHOOSER::OnChar( wxKeyEvent& aEvent )
337{
338 if( aEvent.GetKeyCode() == WXK_ESCAPE )
339 {
340 wxObject* eventSource = aEvent.GetEventObject();
341
342 if( wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( eventSource ) )
343 {
344 // First escape cancels search string value
345 if( textCtrl->GetValue() == m_tree->GetSearchString()
346 && !m_tree->GetSearchString().IsEmpty() )
347 {
348 m_tree->SetSearchString( wxEmptyString );
349 return;
350 }
351 }
352
354 }
355 else
356 {
357 aEvent.Skip();
358 }
359}
360
361
362wxPanel* PANEL_SYMBOL_CHOOSER::constructRightPanel( wxWindow* aParent )
363{
365
366 if( m_frame->GetCanvas() )
367 backend = m_frame->GetCanvas()->GetBackend();
368 else if( COMMON_SETTINGS* cfg = Pgm().GetCommonSettings() )
369 backend = static_cast<EDA_DRAW_PANEL_GAL::GAL_TYPE>( cfg->m_Graphics.canvas_type );
370
371 wxPanel* panel = new wxPanel( aParent );
372 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
373
374 m_symbol_preview = new SYMBOL_PREVIEW_WIDGET( panel, &m_frame->Kiway(), true, backend );
375 m_symbol_preview->SetLayoutDirection( wxLayout_LeftToRight );
376
378 {
379 sizer->Add( m_symbol_preview, 11, wxEXPAND | wxALL, 5 );
380
382 sizer->Add( m_fp_sel_ctrl, 0, wxEXPAND | wxLEFT | wxRIGHT, 5 );
383
384 m_fp_preview = new FOOTPRINT_PREVIEW_WIDGET( panel, m_frame->Kiway() );
385 m_fp_preview->SetUserUnits( m_frame->GetUserUnits() );
386
387 if( m_fp_preview )
388 sizer->Add( m_fp_preview, 10, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 5 );
389 }
390 else
391 {
392 sizer->Add( m_symbol_preview, 1, wxEXPAND | wxALL, 5 );
393 }
394
395 panel->SetSizer( sizer );
396 panel->Layout();
397 sizer->Fit( panel );
398
399 return panel;
400}
401
402
404{
405 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
406 {
407 auto horizPixelsFromDU =
408 [&]( int x ) -> int
409 {
410 wxSize sz( x, 0 );
411 return GetParent()->ConvertDialogToPixels( sz ).x;
412 };
413
414 EESCHEMA_SETTINGS::PANEL_SYM_CHOOSER& panelCfg = cfg->m_SymChooserPanel;
415
416 // The persisted size is stored in DIP so it is independent of the monitor it was saved
417 // on. Restoring raw pixels would scale the window by the DPI ratio when reopened on a
418 // different-scale display, producing a window that spills across monitors.
419 int w = panelCfg.width > 40 ? GetParent()->FromDIP( panelCfg.width ) : horizPixelsFromDU( 440 );
420 int h = panelCfg.height > 40 ? GetParent()->FromDIP( panelCfg.height ) : horizPixelsFromDU( 340 );
421
422 // Cap to the work area so a stale pre-DIP setting cannot reopen the window across monitors.
423 if( int display = wxDisplay::GetFromWindow( GetParent() ); display != wxNOT_FOUND )
424 {
425 wxRect workArea = wxDisplay( display ).GetClientArea();
426 w = std::min( w, workArea.GetWidth() );
427 h = std::min( h, workArea.GetHeight() );
428 }
429
430 GetParent()->SetSize( wxSize( w, h ) );
431 GetParent()->Layout();
432
433 // We specify the width of the right window (m_symbol_view_panel), because specify
434 // the width of the left window does not work as expected when SetSashGravity() is called
435
436 if( panelCfg.sash_pos_h < 0 )
437 panelCfg.sash_pos_h = horizPixelsFromDU( 220 );
438
439 if( panelCfg.sash_pos_v < 0 )
440 panelCfg.sash_pos_v = horizPixelsFromDU( 230 );
441
442 m_hsplitter->SetSashPosition( panelCfg.sash_pos_h );
443
444 if( m_vsplitter )
445 m_vsplitter->SetSashPosition( panelCfg.sash_pos_v );
446
447 m_adapter->SetSortMode( (LIB_TREE_MODEL_ADAPTER::SORT_MODE) panelCfg.sort_mode );
448 }
449
450 if( m_fp_preview && m_fp_preview->IsInitialized() )
451 {
452 // This hides the GAL panel and shows the status label
453 m_fp_preview->SetStatusText( wxEmptyString );
454 }
455
456 if( m_fp_sel_ctrl )
457 m_fp_sel_ctrl->Load( m_frame->Kiway(), m_frame->Prj() );
458}
459
460
462{
463 if( m_details && e.GetKeyCode() == 'C' && e.ControlDown() &&
464 !e.AltDown() && !e.ShiftDown() && !e.MetaDown() )
465 {
466 wxString txt = m_details->SelectionToText();
467 wxLogNull doNotLog; // disable logging of failed clipboard actions
468
469 if( wxTheClipboard->Open() )
470 {
471 wxTheClipboard->SetData( new wxTextDataObject( txt ) );
472 wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
473 wxTheClipboard->Close();
474 }
475 }
476 else
477 {
478 e.Skip();
479 }
480}
481
482
484{
485 m_compatCallback = std::move( aFunc );
486
487 if( SYMBOL_TREE_MODEL_ADAPTER* symAdapter =
488 dynamic_cast<SYMBOL_TREE_MODEL_ADAPTER*>( m_adapter.get() ) )
489 {
490 symAdapter->SetCompatibilityCallback( m_compatCallback );
491 }
492}
493
494
496{
497 m_adapter->SetPreselectNode( aPreselect, 0 );
498
499 if( m_tree && aPreselect.IsValid() )
500 m_tree->SelectLibId( aPreselect );
501}
502
503
505{
506 return m_tree->GetSelectedLibId( aUnit );
507}
508
509
511{
512 m_symbol_preview->GetCanvas()->SetEvtHandlerEnabled( false );
513 m_symbol_preview->GetCanvas()->StopDrawing();
514
515 if( m_fp_preview )
516 {
517 m_fp_preview->GetPreviewPanel()->GetCanvas()->SetEvtHandlerEnabled( false );
518 m_fp_preview->GetPreviewPanel()->GetCanvas()->StopDrawing();
519 }
520}
521
522
523void PANEL_SYMBOL_CHOOSER::onCloseTimer( wxTimerEvent& aEvent )
524{
525 // Hack because of eaten MouseUp event. See PANEL_SYMBOL_CHOOSER::onSymbolChosen
526 // for the beginning of this spaghetti noodle.
527
528 wxMouseState state = wxGetMouseState();
529
530 if( state.LeftIsDown() )
531 {
532 // Mouse hasn't been raised yet, so fire the timer again. Otherwise the
533 // purpose of this timer is defeated.
535 }
536 else
537 {
539 }
540}
541
542
543void PANEL_SYMBOL_CHOOSER::onOpenLibsTimer( wxTimerEvent& aEvent )
544{
545 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
546 m_adapter->OpenLibs( cfg->m_LibTree.open_libs );
547}
548
549
551{
552 if( !m_fp_preview || !m_fp_preview->IsInitialized() )
553 return;
554
555 LIB_SYMBOL* symbol = nullptr;
556
557 try
558 {
559 symbol = PROJECT_SCH::SymbolLibAdapter( &m_frame->Prj() )->LoadSymbol( aLibId );
560 }
561 catch( const IO_ERROR& ioe )
562 {
563 wxLogError( _( "Error loading symbol %s from library '%s'." ) + wxS( "\n%s" ),
564 aLibId.GetLibItemName().wx_str(),
565 aLibId.GetLibNickname().wx_str(),
566 ioe.What() );
567 }
568
569 if( !symbol )
570 return;
571
572 SCH_FIELD* fp_field = symbol->GetField( FIELD_T::FOOTPRINT );
573 wxString fp_name = fp_field ? fp_field->GetFullText() : wxString( "" );
574
575 m_fp_override.Empty();
576 showFootprint( fp_name );
577}
578
579
580void PANEL_SYMBOL_CHOOSER::showFootprint( wxString const& aFootprint )
581{
582 if( !m_fp_preview || !m_fp_preview->IsInitialized() )
583 return;
584
585 if( aFootprint == wxEmptyString )
586 {
587 m_fp_preview->SetStatusText( _( "No footprint specified" ) );
588 }
589 else
590 {
591 LIB_ID lib_id;
592
593 if( lib_id.Parse( aFootprint ) == -1 && lib_id.IsValid() )
594 {
595 m_fp_preview->ClearStatus();
596 m_fp_preview->DisplayFootprint( lib_id );
597 }
598 else
599 {
600 m_fp_preview->SetStatusText( _( "Invalid footprint specified" ) );
601 }
602 }
603}
604
605
607{
608 if( !m_fp_sel_ctrl )
609 return;
610
611 m_fp_sel_ctrl->ClearFilters();
612
613 LIB_SYMBOL* symbol = nullptr;
614
615 if( aLibId.IsValid() )
616 {
617 try
618 {
619 symbol = PROJECT_SCH::SymbolLibAdapter( &m_frame->Prj() )->LoadSymbol( aLibId );
620 }
621 catch( const IO_ERROR& ioe )
622 {
623 wxLogError( _( "Error loading symbol %s from library '%s'." ) + wxS( "\n%s" ),
624 aLibId.GetLibItemName().wx_str(),
625 aLibId.GetLibNickname().wx_str(),
626 ioe.What() );
627 }
628 }
629
630 if( symbol != nullptr )
631 {
632 int pinCount = symbol->GetGraphicalPins( 0 /* all units */, 1 /* single bodyStyle */ ).size();
633 SCH_FIELD* fp_field = symbol->GetField( FIELD_T::FOOTPRINT );
634 wxString fp_name = fp_field ? fp_field->GetFullText() : wxString( "" );
635
636 if( !m_fp_override.IsEmpty() )
637 fp_name = m_fp_override;
638
639 // Explicitly associated footprints (issue #2282) are listed ahead of the glob matches in
640 // written order and bypass the pin-count filter; a mapped EP/NC footprint legally has more
641 // pads than the symbol has pins.
642 for( const ASSOCIATED_FOOTPRINT& assoc : symbol->GetEffectiveAssociatedFootprints() )
643 m_fp_sel_ctrl->AddAlwaysIncludedFootprint( assoc.m_FootprintLibId );
644
645 m_fp_sel_ctrl->FilterByPinCount( pinCount );
646 m_fp_sel_ctrl->FilterByFootprintFilters( symbol->GetFPFilters(), true );
647 m_fp_sel_ctrl->SetDefaultFootprint( fp_name );
648 m_fp_sel_ctrl->UpdateList();
649 m_fp_sel_ctrl->Enable();
650 }
651 else
652 {
653 m_fp_sel_ctrl->UpdateList();
654 m_fp_sel_ctrl->Disable();
655 }
656}
657
658
659void PANEL_SYMBOL_CHOOSER::onFootprintSelected( wxCommandEvent& aEvent )
660{
661 m_fp_override = aEvent.GetString();
662
663 std::erase_if( m_field_edits,
664 []( std::pair<FIELD_T, wxString> const& i )
665 {
666 return i.first == FIELD_T::FOOTPRINT;
667 } );
668
669 m_field_edits.emplace_back( std::make_pair( FIELD_T::FOOTPRINT, m_fp_override ) );
670
672}
673
674
675void PANEL_SYMBOL_CHOOSER::onSymbolSelected( wxCommandEvent& aEvent )
676{
677 LIB_TREE_NODE* node = m_tree->GetCurrentTreeNode();
678
679 if( node && node->m_LibId.IsValid() )
680 {
681 m_symbol_preview->DisplaySymbol( node->m_LibId, node->m_Unit );
682
683 if( !node->m_Footprint.IsEmpty() )
684 {
685 wxCommandEvent evt( EVT_FOOTPRINT_SELECTED );
686 evt.SetString( node->m_Footprint);
687 onFootprintSelected( evt );
688 }
689 else
690 {
691 showFootprintFor( node->m_LibId );
692 }
693
695
697 {
698 std::vector<VARIANT_COMPAT_RESULT> issues = m_compatCallback( node->m_LibId );
699
700 if( !issues.empty() )
701 {
702 wxString html = wxS( "<br><b>" ) + _( "Compatibility Warnings:" ) + wxS( "</b><ul>" );
703
704 for( const VARIANT_COMPAT_RESULT& issue : issues )
705 html += wxS( "<li>" ) + EscapeHTML( issue.detail ) + wxS( "</li>" );
706
707 html += wxS( "</ul>" );
708 m_details->AppendToPage( html );
709 }
710 }
711 }
712 else
713 {
714 m_symbol_preview->SetStatusText( _( "No symbol selected" ) );
715
716 if( m_fp_preview && m_fp_preview->IsInitialized() )
717 m_fp_preview->SetStatusText( wxEmptyString );
718
720 }
721}
722
723
724void PANEL_SYMBOL_CHOOSER::onSymbolChosen( wxCommandEvent& aEvent )
725{
726 if( m_tree->GetSelectedLibId().IsValid() )
727 {
728 // Got a selection. We can't just end the modal dialog here, because wx leaks some events
729 // back to the parent window (in particular, the MouseUp following a double click).
730 //
731 // NOW, here's where it gets really fun. wxTreeListCtrl eats MouseUp. This isn't really
732 // feasible to bypass without a fully custom wxDataViewCtrl implementation, and even then
733 // might not be fully possible (docs are vague). To get around this, we use a one-shot
734 // timer to schedule the dialog close.
735 //
736 // See PANEL_SYMBOL_CHOOSER::onCloseTimer for the other end of this spaghetti noodle.
738 }
739}
740
741
743{
744 LIB_ID savedSelection = m_tree->GetSelectedLibId();
745 m_tree->Regenerate( true );
746
747 if( savedSelection.IsValid() )
748 m_tree->CenterLibId( savedSelection );
749}
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
@ GAL_TYPE_OPENGL
OpenGL implementation.
Add dark theme support to wxHtmlWindow.
Definition html_window.h:31
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library tables.
std::optional< LIBRARY_TABLE_ROW * > GetRow(const wxString &aNickname, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH) const
Like LIBRARY_MANAGER::GetRow but filtered to the LIBRARY_TABLE_TYPE of this adapter.
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:65
bool IsValid() const
Check if this LID_ID is valid.
Definition lib_id.h:168
const UTF8 & GetLibItemName() const
Definition lib_id.h:98
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition lib_id.h:83
Define a library symbol object.
Definition lib_symbol.h:114
std::vector< const SCH_PIN * > GetGraphicalPins(int aUnit=0, int aBodyStyle=0) const
Graphical pins: Return schematic pin objects as drawn (unexpanded), filtered by unit/body.
const std::vector< ASSOCIATED_FOOTPRINT > & GetEffectiveAssociatedFootprints() const
Definition lib_symbol.h:290
SCH_FIELD * GetField(const wxString &aFieldName)
Find a field within this symbol matching aFieldName; return nullptr if not found.
wxArrayString GetFPFilters() const
Definition lib_symbol.h:242
void SetPreselectNode(const LIB_ID &aLibId, int aUnit)
Set the symbol name to be selected if there are no search results.
void AssignIntrinsicRanks()
Sort the tree and assign ranks after adding libraries.
LIB_TREE_NODE_LIBRARY & DoAddLibrary(const wxString &aNodeName, const wxString &aDesc, const std::vector< LIB_TREE_ITEM * > &aItemList, bool pinned, bool presorted)
Add the given list of symbols by alias.
void SetFilter(std::function< bool(LIB_TREE_NODE &aNode)> *aFilter)
Set the filter.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
bool m_IsAlreadyPlacedGroup
wxString m_Footprint
Widget displaying a tree of symbols with optional search text control and description panel.
Definition lib_tree.h:46
@ ALL_WIDGETS
Definition lib_tree.h:55
SYMBOL_PREVIEW_WIDGET * m_symbol_preview
PANEL_SYMBOL_CHOOSER(SCH_BASE_FRAME *aFrame, wxWindow *aParent, const SYMBOL_LIBRARY_FILTER *aFilter, std::vector< PICKED_SYMBOL > &aHistoryList, std::vector< PICKED_SYMBOL > &aAlreadyPlaced, bool aAllowFieldEdits, bool aShowFootprints, bool &aCancelled, std::function< void()> aAcceptHandler, std::function< void()> aEscapeHandler)
Create dialog to choose symbol.
void onSymbolSelected(wxCommandEvent &aEvent)
void showFootprintFor(const LIB_ID &aLibId)
Look up the footprint for a given symbol specified in the LIB_ID and display it.
void onMenuClose(wxMenuEvent &aEvent)
wxSplitterWindow * m_hsplitter
void SetCompatibilityCallback(SYMBOL_COMPAT_FUNC aFunc)
FOOTPRINT_SELECT_WIDGET * m_fp_sel_ctrl
std::function< void()> m_escapeHandler
void OnDetailsCharHook(wxKeyEvent &aEvt)
static wxString g_symbolSearchString
void onMenuOpen(wxMenuEvent &aEvent)
Handle parent frame menu events to block tree preview.
std::vector< std::pair< FIELD_T, wxString > > m_field_edits
void onCloseTimer(wxTimerEvent &aEvent)
wxSplitterWindow * m_vsplitter
static SCH_BASE_FRAME * m_frame
void showFootprint(const wxString &aFootprint)
Display the given footprint by name.
void populateFootprintSelector(const LIB_ID &aLibId)
Populate the footprint selector for a given alias.
void onOpenLibsTimer(wxTimerEvent &aEvent)
void onFootprintSelected(wxCommandEvent &aEvent)
static wxString g_powerSearchString
void OnChar(wxKeyEvent &aEvent)
void SetPreselect(const LIB_ID &aPreselect)
std::function< void()> m_acceptHandler
static constexpr int DBLCLICK_DELAY
SYMBOL_COMPAT_FUNC m_compatCallback
FOOTPRINT_PREVIEW_WIDGET * m_fp_preview
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > m_adapter
LIB_ID GetSelectedLibId(int *aUnit=nullptr) const
To be called after this dialog returns from ShowModal().
void onSymbolChosen(wxCommandEvent &aEvent)
Handle the selection of an item.
wxPanel * constructRightPanel(wxWindow *aParent)
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:562
The backing store for a PROJECT, in JSON format.
static SYMBOL_LIBRARY_ADAPTER * SymbolLibAdapter(PROJECT *aProject)
Accessor for project symbol library manager adapter.
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
wxString GetFullText(int unit=1) const
Return the text of a field.
void SetText(const wxString &aText) override
An interface to the global shared library manager that is schematic-specific and linked to one projec...
LIB_SYMBOL * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_SYMBOL having aName from the library given by aNickname.
Helper object to filter a list of libraries.
const wxArrayString & GetAllowedLibList() const
void AddLibraries(SCH_BASE_FRAME *aFrame)
Add all the libraries in a SYMBOL_LIB_TABLE to the model.
static wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > Create(SCH_BASE_FRAME *aParent, SYMBOL_LIBRARY_ADAPTER *aLibs)
Factory function: create a model adapter in a reference-counting container.
void AddLibrary(wxString const &aLibNickname, bool pinned)
wxString wx_str() const
Definition utf8.cpp:41
#define _(s)
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition kicad_algo.h:96
STL namespace.
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
T * GetAppSettings(const char *aFilename)
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
A first-class footprint choice on a LIB_SYMBOL, tied to a named pin map.
Definition pin_map.h:158
std::vector< wxString > pinned_symbol_libs
@ FOOTPRINT
Field Name Module PCB, i.e. "16DIP300".
std::function< std::vector< VARIANT_COMPAT_RESULT >(const LIB_ID &)> SYMBOL_COMPAT_FUNC
Callback that evaluates variant symbol compatibility for a given candidate LIB_ID.