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#include <wx/choice.h>
50#include <wx/stattext.h>
51#include <lib_symbol.h>
52
53
57
59 const SYMBOL_LIBRARY_FILTER* aFilter,
60 std::vector<PICKED_SYMBOL>& aHistoryList,
61 std::vector<PICKED_SYMBOL>& aAlreadyPlaced,
62 bool aAllowFieldEdits, bool aShowFootprints, bool& aCancelled,
63 std::function<void()> aAcceptHandler,
64 std::function<void()> aEscapeHandler ) :
65 wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize ),
66 m_symbol_preview( nullptr ),
67 m_hsplitter( nullptr ),
68 m_vsplitter( nullptr ),
69 m_fp_sel_ctrl( nullptr ),
70 m_fp_preview( nullptr ),
71 m_tree( nullptr ),
72 m_details( nullptr ),
73 m_acceptHandler( std::move( aAcceptHandler ) ),
74 m_escapeHandler( std::move( aEscapeHandler ) ),
75 m_showPower( false ),
76 m_allow_field_edits( aAllowFieldEdits ),
77 m_show_footprints( aShowFootprints ),
78 m_bodyStyleLabel( nullptr ),
79 m_bodyStyleChoice( nullptr ),
81{
82 m_frame = aFrame;
83
86 PROJECT_FILE& project = m_frame->Prj().GetProjectFile();
87
88 // Make sure settings are loaded before we start running multi-threaded symbol loaders
91
93 SYMBOL_TREE_MODEL_ADAPTER* adapter = static_cast<SYMBOL_TREE_MODEL_ADAPTER*>( m_adapter.get() );
94
95 if( aFilter )
96 {
97 const wxArrayString& liblist = aFilter->GetAllowedLibList();
98
99 for( const wxString& nickname : liblist )
100 {
101 if( libmgr->HasLibrary( nickname, true ) )
102 {
103 bool pinned = alg::contains( session.pinned_symbol_libs, nickname )
104 || alg::contains( project.m_PinnedSymbolLibs, nickname );
105
106 std::optional<LIBRARY_TABLE_ROW*> row = libmgr->GetRow( nickname );
107
108 if( row.has_value() && !row.value()->Hidden() )
109 adapter->AddLibrary( nickname, pinned );
110 }
111 }
112
113 adapter->AssignIntrinsicRanks();
114
115 if( aFilter->GetFilterPowerSymbols() )
116 {
117 static std::function<bool( LIB_TREE_NODE& )> powerFilter =
118 []( LIB_TREE_NODE& aNode ) -> bool
119 {
120 return aNode.m_IsPower;
121 };
122
123 adapter->SetFilter( &powerFilter );
124
125 m_showPower = true;
126 m_show_footprints = false;
127 }
128 }
129
130 std::vector<LIB_SYMBOL> history_list_storage;
131 std::vector<LIB_TREE_ITEM*> history_list;
132 std::vector<LIB_SYMBOL> already_placed_storage;
133 std::vector<LIB_TREE_ITEM*> already_placed;
134
135 // Lambda to encapsulate the common logic
136 auto processList =
137 [&]( const std::vector<PICKED_SYMBOL>& inputList,
138 std::vector<LIB_SYMBOL>& storageList,
139 std::vector<LIB_TREE_ITEM*>& resultList )
140 {
141 storageList.reserve( inputList.size() );
142
143 for( const PICKED_SYMBOL& i : inputList )
144 {
145 LIB_SYMBOL* symbol = m_frame->GetLibSymbol( i.LibId );
146
147 if( symbol )
148 {
149 storageList.emplace_back( *symbol );
150
151 for( const auto& [fieldType, fieldValue] : i.Fields )
152 {
153 SCH_FIELD* field = storageList.back().GetField( fieldType );
154
155 if( field )
156 field->SetText( fieldValue );
157 }
158
159 resultList.push_back( &storageList.back() );
160 }
161 }
162 };
163
164 // Sort the already placed list since it is potentially from multiple sessions,
165 // but not the most recent list since we want this listed by most recent usage.
166 std::sort( aAlreadyPlaced.begin(), aAlreadyPlaced.end(),
167 []( PICKED_SYMBOL const& a, PICKED_SYMBOL const& b )
168 {
169 return a.LibId.GetLibItemName() < b.LibId.GetLibItemName();
170 } );
171
172 processList( aHistoryList, history_list_storage, history_list );
173 processList( aAlreadyPlaced, already_placed_storage, already_placed );
174
175 adapter->DoAddLibrary( wxT( "-- " ) + _( "Recently Used" ) + wxT( " --" ), wxEmptyString,
176 history_list, false, true )
177 .m_IsRecentlyUsedGroup = true;
178
179 if( !aHistoryList.empty() )
180 adapter->SetPreselectNode( aHistoryList[0].LibId, aHistoryList[0].Unit );
181
182 adapter->DoAddLibrary( wxT( "-- " ) + _( "Already Placed" ) + wxT( " --" ), wxEmptyString,
183 already_placed, false, true )
185
186 adapter->AddLibraries( m_frame );
187
188 // -------------------------------------------------------------------------------------
189 // Construct the actual panel
190 //
191
192 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
193
194 // Use a slightly different layout, with a details pane spanning the entire window,
195 // if we're not showing footprints.
197 {
198 m_hsplitter = new wxSplitterWindow( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
199 wxSP_LIVE_UPDATE | wxSP_NOBORDER | wxSP_3DSASH );
200
201 //Avoid the splitter window being assigned as the Parent to additional windows
202 m_hsplitter->SetExtraStyle( wxWS_EX_TRANSIENT );
203
204 sizer->Add( m_hsplitter, 1, wxEXPAND, 5 );
205 }
206 else
207 {
208 m_vsplitter = new wxSplitterWindow( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
209 wxSP_LIVE_UPDATE | wxSP_NOBORDER | wxSP_3DSASH );
210
211 m_hsplitter = new wxSplitterWindow( m_vsplitter, wxID_ANY, wxDefaultPosition, wxDefaultSize,
212 wxSP_LIVE_UPDATE | wxSP_NOBORDER | wxSP_3DSASH );
213
214 // Avoid the splitter window being assigned as the parent to additional windows.
215 m_vsplitter->SetExtraStyle( wxWS_EX_TRANSIENT );
216 m_hsplitter->SetExtraStyle( wxWS_EX_TRANSIENT );
217
218 wxPanel* detailsPanel = new wxPanel( m_vsplitter );
219 wxBoxSizer* detailsSizer = new wxBoxSizer( wxVERTICAL );
220 detailsPanel->SetSizer( detailsSizer );
221
222 m_details = new HTML_WINDOW( detailsPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize );
223 detailsSizer->Add( m_details, 1, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 5 );
224 detailsPanel->Layout();
225 detailsSizer->Fit( detailsPanel );
226
227 m_vsplitter->SetSashGravity( 0.5 );
228 m_vsplitter->SetMinimumPaneSize( 20 );
229 m_vsplitter->SplitHorizontally( m_hsplitter, detailsPanel );
230
231 sizer->Add( m_vsplitter, 1, wxEXPAND | wxBOTTOM, 5 );
232 }
233
234 wxPanel* treePanel = new wxPanel( m_hsplitter );
235 wxBoxSizer* treeSizer = new wxBoxSizer( wxVERTICAL );
236 treePanel->SetSizer( treeSizer );
237
238 m_tree = new LIB_TREE( treePanel, m_showPower ? wxT( "power" ) : wxT( "symbols" ), m_adapter,
240
241 treeSizer->Add( m_tree, 1, wxALL | wxEXPAND, 5 );
242 treePanel->Layout();
243 treeSizer->Fit( treePanel );
244
245 m_adapter->FinishTreeInitialization();
246
247 if( m_showPower )
248 m_tree->SetSearchString( g_powerSearchString );
249 else
250 m_tree->SetSearchString( g_symbolSearchString );
251
252 m_hsplitter->SetSashGravity( 0.8 );
253 m_hsplitter->SetMinimumPaneSize( 20 );
254 m_hsplitter->SplitVertically( treePanel, constructRightPanel( m_hsplitter ) );
255
256 m_dbl_click_timer = new wxTimer( this );
257 m_open_libs_timer = new wxTimer( this );
258
259 SetSizer( sizer );
260
261 Layout();
262
263 Bind( wxEVT_TIMER, &PANEL_SYMBOL_CHOOSER::onCloseTimer, this, m_dbl_click_timer->GetId() );
264 Bind( wxEVT_TIMER, &PANEL_SYMBOL_CHOOSER::onOpenLibsTimer, this, m_open_libs_timer->GetId() );
265 Bind( EVT_LIBITEM_SELECTED, &PANEL_SYMBOL_CHOOSER::onSymbolSelected, this );
266 Bind( EVT_LIBITEM_CHOSEN, &PANEL_SYMBOL_CHOOSER::onSymbolChosen, this );
267 aFrame->Bind( wxEVT_MENU_OPEN, &PANEL_SYMBOL_CHOOSER::onMenuOpen, this );
268 aFrame->Bind( wxEVT_MENU_CLOSE, &PANEL_SYMBOL_CHOOSER::onMenuClose, this );
269
270 if( m_fp_sel_ctrl )
271 m_fp_sel_ctrl->Bind( EVT_FOOTPRINT_SELECTED, &PANEL_SYMBOL_CHOOSER::onFootprintSelected, this );
272
275
276 if( m_details )
277 m_details->Bind( wxEVT_CHAR_HOOK, &PANEL_SYMBOL_CHOOSER::OnDetailsCharHook, this );
278
279 // Open the user's previously opened libraries on timer expiration.
280 // This is done on a timer because we need a gross hack to keep GTK from garbling the
281 // display. Must be longer than the search debounce timer.
282 m_open_libs_timer->StartOnce( 300 );
283}
284
285
287{
288 m_frame->Unbind( wxEVT_MENU_OPEN, &PANEL_SYMBOL_CHOOSER::onMenuOpen, this );
289 m_frame->Unbind( wxEVT_MENU_CLOSE, &PANEL_SYMBOL_CHOOSER::onMenuClose, this );
290 Unbind( wxEVT_TIMER, &PANEL_SYMBOL_CHOOSER::onCloseTimer, this );
291 Unbind( EVT_LIBITEM_SELECTED, &PANEL_SYMBOL_CHOOSER::onSymbolSelected, this );
292 Unbind( EVT_LIBITEM_CHOSEN, &PANEL_SYMBOL_CHOOSER::onSymbolChosen, this );
293
294 // Stop the timer during destruction early to avoid potential race conditions (that do happen)
295 m_dbl_click_timer->Stop();
296 m_open_libs_timer->Stop();
297 delete m_dbl_click_timer;
298 delete m_open_libs_timer;
299
300 if( m_showPower )
301 g_powerSearchString = m_tree->GetSearchString();
302 else
303 g_symbolSearchString = m_tree->GetSearchString();
304
305 if( m_fp_sel_ctrl )
306 m_fp_sel_ctrl->Unbind( EVT_FOOTPRINT_SELECTED, &PANEL_SYMBOL_CHOOSER::onFootprintSelected, this );
307
308 if( m_details )
309 m_details->Unbind( wxEVT_CHAR_HOOK, &PANEL_SYMBOL_CHOOSER::OnDetailsCharHook, this );
310
311 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
312 {
313 // Save any changes to column widths, etc.
314 m_adapter->SaveSettings();
315
316 cfg->m_SymChooserPanel.width = GetParent()->ToDIP( GetParent()->GetSize().x );
317 cfg->m_SymChooserPanel.height = GetParent()->ToDIP( GetParent()->GetSize().y );
318
319 cfg->m_SymChooserPanel.sash_pos_h = m_hsplitter->GetSashPosition();
320
321 if( m_vsplitter )
322 cfg->m_SymChooserPanel.sash_pos_v = m_vsplitter->GetSashPosition();
323
324 cfg->m_SymChooserPanel.sort_mode = m_tree->GetSortMode();
325 }
326
327 m_frame = nullptr;
328}
329
330
331void PANEL_SYMBOL_CHOOSER::onMenuOpen( wxMenuEvent& aEvent )
332{
333 m_tree->BlockPreview( true );
334 aEvent.Skip();
335}
336
337
338void PANEL_SYMBOL_CHOOSER::onMenuClose( wxMenuEvent& aEvent )
339{
340 m_tree->BlockPreview( false );
341 aEvent.Skip();
342}
343
344
345void PANEL_SYMBOL_CHOOSER::OnChar( wxKeyEvent& aEvent )
346{
347 if( aEvent.GetKeyCode() == WXK_ESCAPE )
348 {
349 wxObject* eventSource = aEvent.GetEventObject();
350
351 if( wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( eventSource ) )
352 {
353 // First escape cancels search string value
354 if( textCtrl->GetValue() == m_tree->GetSearchString()
355 && !m_tree->GetSearchString().IsEmpty() )
356 {
357 m_tree->SetSearchString( wxEmptyString );
358 return;
359 }
360 }
361
363 }
364 else
365 {
366 aEvent.Skip();
367 }
368}
369
370
371wxPanel* PANEL_SYMBOL_CHOOSER::constructRightPanel( wxWindow* aParent )
372{
374
375 if( m_frame->GetCanvas() )
376 backend = m_frame->GetCanvas()->GetBackend();
377 else if( COMMON_SETTINGS* cfg = Pgm().GetCommonSettings() )
378 backend = static_cast<EDA_DRAW_PANEL_GAL::GAL_TYPE>( cfg->m_Graphics.canvas_type );
379
380 wxPanel* panel = new wxPanel( aParent );
381 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
382
383 m_symbol_preview = new SYMBOL_PREVIEW_WIDGET( panel, &m_frame->Kiway(), true, backend );
384 m_symbol_preview->SetLayoutDirection( wxLayout_LeftToRight );
385
386 wxBoxSizer* bodyStyleSizer = new wxBoxSizer( wxHORIZONTAL );
387 m_bodyStyleLabel = new wxStaticText( panel, wxID_ANY, _( "Body style:" ) );
388 m_bodyStyleChoice = new wxChoice( panel, wxID_ANY );
389 m_bodyStyleChoice->Enable( false );
390 bodyStyleSizer->Add( m_bodyStyleLabel, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxTOP | wxBOTTOM, 5 );
391 bodyStyleSizer->Add( m_bodyStyleChoice, 1, wxEXPAND | wxALL, 5 );
392 sizer->Add( bodyStyleSizer, 0, wxEXPAND | wxBOTTOM, 5 );
393
395 {
396 sizer->Add( m_symbol_preview, 11, wxEXPAND | wxALL, 5 );
397
399 sizer->Add( m_fp_sel_ctrl, 0, wxEXPAND | wxLEFT | wxRIGHT, 5 );
400
401 m_fp_preview = new FOOTPRINT_PREVIEW_WIDGET( panel, m_frame->Kiway() );
402 m_fp_preview->SetUserUnits( m_frame->GetUserUnits() );
403
404 if( m_fp_preview )
405 sizer->Add( m_fp_preview, 10, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 5 );
406 }
407 else
408 {
409 sizer->Add( m_symbol_preview, 1, wxEXPAND | wxALL, 5 );
410 }
411
412 panel->SetSizer( sizer );
413 panel->Layout();
414 sizer->Fit( panel );
415
416 return panel;
417}
418
419
421{
422 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
423 {
424 auto horizPixelsFromDU =
425 [&]( int x ) -> int
426 {
427 wxSize sz( x, 0 );
428 return GetParent()->ConvertDialogToPixels( sz ).x;
429 };
430
431 EESCHEMA_SETTINGS::PANEL_SYM_CHOOSER& panelCfg = cfg->m_SymChooserPanel;
432
433 // The persisted size is stored in DIP so it is independent of the monitor it was saved
434 // on. Restoring raw pixels would scale the window by the DPI ratio when reopened on a
435 // different-scale display, producing a window that spills across monitors.
436 int w = panelCfg.width > 40 ? GetParent()->FromDIP( panelCfg.width ) : horizPixelsFromDU( 440 );
437 int h = panelCfg.height > 40 ? GetParent()->FromDIP( panelCfg.height ) : horizPixelsFromDU( 340 );
438
439 // Cap to the work area so a stale pre-DIP setting cannot reopen the window across monitors.
440 if( int display = wxDisplay::GetFromWindow( GetParent() ); display != wxNOT_FOUND )
441 {
442 wxRect workArea = wxDisplay( display ).GetClientArea();
443 w = std::min( w, workArea.GetWidth() );
444 h = std::min( h, workArea.GetHeight() );
445 }
446
447 GetParent()->SetSize( wxSize( w, h ) );
448 GetParent()->Layout();
449
450 // We specify the width of the right window (m_symbol_view_panel), because specify
451 // the width of the left window does not work as expected when SetSashGravity() is called
452
453 if( panelCfg.sash_pos_h < 0 )
454 panelCfg.sash_pos_h = horizPixelsFromDU( 220 );
455
456 if( panelCfg.sash_pos_v < 0 )
457 panelCfg.sash_pos_v = horizPixelsFromDU( 230 );
458
459 m_hsplitter->SetSashPosition( panelCfg.sash_pos_h );
460
461 if( m_vsplitter )
462 m_vsplitter->SetSashPosition( panelCfg.sash_pos_v );
463
464 m_adapter->SetSortMode( (LIB_TREE_MODEL_ADAPTER::SORT_MODE) panelCfg.sort_mode );
465 }
466
467 if( m_fp_preview && m_fp_preview->IsInitialized() )
468 {
469 // This hides the GAL panel and shows the status label
470 m_fp_preview->SetStatusText( wxEmptyString );
471 }
472
473 if( m_fp_sel_ctrl )
474 m_fp_sel_ctrl->Load( m_frame->Kiway(), m_frame->Prj() );
475}
476
477
479{
480 if( m_details && e.GetKeyCode() == 'C' && e.ControlDown() &&
481 !e.AltDown() && !e.ShiftDown() && !e.MetaDown() )
482 {
483 wxString txt = m_details->SelectionToText();
484 wxLogNull doNotLog; // disable logging of failed clipboard actions
485
486 if( wxTheClipboard->Open() )
487 {
488 wxTheClipboard->SetData( new wxTextDataObject( txt ) );
489 wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
490 wxTheClipboard->Close();
491 }
492 }
493 else
494 {
495 e.Skip();
496 }
497}
498
499
501{
502 m_compatCallback = std::move( aFunc );
503
504 if( SYMBOL_TREE_MODEL_ADAPTER* symAdapter =
505 dynamic_cast<SYMBOL_TREE_MODEL_ADAPTER*>( m_adapter.get() ) )
506 {
507 symAdapter->SetCompatibilityCallback( m_compatCallback );
508 }
509}
510
511
513{
514 m_adapter->SetPreselectNode( aPreselect, 0 );
515
516 if( m_tree && aPreselect.IsValid() )
517 m_tree->SelectLibId( aPreselect );
518}
519
520
521LIB_ID PANEL_SYMBOL_CHOOSER::GetSelectedLibId( int* aUnit, int* aBodyStyle ) const
522{
523 LIB_ID id = m_tree->GetSelectedLibId( aUnit );
524
525 if( aBodyStyle )
526 *aBodyStyle = m_selectedBodyStyle;
527
528 return id;
529}
530
531
533{
534 int bodyStyleCount = aSymbol ? std::max( aSymbol->GetBodyStyleCount(), 1 ) : 1;
535
536 m_bodyStyleChoice->Enable( bodyStyleCount > 1 );
537 m_bodyStyleChoice->Clear();
538
539 if( bodyStyleCount > 1 )
540 {
541 for( int i = 1; i <= bodyStyleCount; i++ )
542 m_bodyStyleChoice->Append( aSymbol->GetBodyStyleDescription( i, false ) );
543
544 if( m_selectedBodyStyle > static_cast<int>( m_bodyStyleChoice->GetCount() ) )
546
547 m_bodyStyleChoice->SetSelection( m_selectedBodyStyle - 1 );
548 }
549 else
550 {
552 }
553}
554
555
556void PANEL_SYMBOL_CHOOSER::onSelectBodyStyle( wxCommandEvent& aEvent )
557{
558 int selection = m_bodyStyleChoice->GetSelection();
559
560 if( selection < 0 )
561 return;
562
563 m_selectedBodyStyle = selection + 1;
564
565 LIB_TREE_NODE* node = m_tree->GetCurrentTreeNode();
566
567 if( node && node->m_LibId.IsValid() )
568 m_symbol_preview->DisplaySymbol( node->m_LibId, node->m_Unit, m_selectedBodyStyle );
569}
570
571
573{
574 m_symbol_preview->GetCanvas()->SetEvtHandlerEnabled( false );
575 m_symbol_preview->GetCanvas()->StopDrawing();
576
577 if( m_fp_preview )
578 {
579 m_fp_preview->GetPreviewPanel()->GetCanvas()->SetEvtHandlerEnabled( false );
580 m_fp_preview->GetPreviewPanel()->GetCanvas()->StopDrawing();
581 }
582}
583
584
585void PANEL_SYMBOL_CHOOSER::onCloseTimer( wxTimerEvent& aEvent )
586{
587 // Hack because of eaten MouseUp event. See PANEL_SYMBOL_CHOOSER::onSymbolChosen
588 // for the beginning of this spaghetti noodle.
589
590 wxMouseState state = wxGetMouseState();
591
592 if( state.LeftIsDown() )
593 {
594 // Mouse hasn't been raised yet, so fire the timer again. Otherwise the
595 // purpose of this timer is defeated.
597 }
598 else
599 {
601 }
602}
603
604
605void PANEL_SYMBOL_CHOOSER::onOpenLibsTimer( wxTimerEvent& aEvent )
606{
607 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
608 m_adapter->OpenLibs( cfg->m_LibTree.open_libs );
609}
610
611
613{
614 if( !m_fp_preview || !m_fp_preview->IsInitialized() )
615 return;
616
617 LIB_SYMBOL* symbol = nullptr;
618
619 try
620 {
621 symbol = PROJECT_SCH::SymbolLibAdapter( &m_frame->Prj() )->LoadSymbol( aLibId );
622 }
623 catch( const IO_ERROR& ioe )
624 {
625 wxLogError( _( "Error loading symbol %s from library '%s'." ) + wxS( "\n%s" ),
626 aLibId.GetLibItemName().wx_str(),
627 aLibId.GetLibNickname().wx_str(),
628 ioe.What() );
629 }
630
631 if( !symbol )
632 return;
633
634 SCH_FIELD* fp_field = symbol->GetField( FIELD_T::FOOTPRINT );
635 wxString fp_name = fp_field ? fp_field->GetFullText() : wxString( "" );
636
637 m_fp_override.Empty();
638 showFootprint( fp_name );
639}
640
641
642void PANEL_SYMBOL_CHOOSER::showFootprint( wxString const& aFootprint )
643{
644 if( !m_fp_preview || !m_fp_preview->IsInitialized() )
645 return;
646
647 if( aFootprint == wxEmptyString )
648 {
649 m_fp_preview->SetStatusText( _( "No footprint specified" ) );
650 }
651 else
652 {
653 LIB_ID lib_id;
654
655 if( lib_id.Parse( aFootprint ) == -1 && lib_id.IsValid() )
656 {
657 m_fp_preview->ClearStatus();
658 m_fp_preview->DisplayFootprint( lib_id );
659 }
660 else
661 {
662 m_fp_preview->SetStatusText( _( "Invalid footprint specified" ) );
663 }
664 }
665}
666
667
669{
670 if( !m_fp_sel_ctrl )
671 return;
672
673 m_fp_sel_ctrl->ClearFilters();
674
675 LIB_SYMBOL* symbol = nullptr;
676
677 if( aLibId.IsValid() )
678 {
679 try
680 {
681 symbol = PROJECT_SCH::SymbolLibAdapter( &m_frame->Prj() )->LoadSymbol( aLibId );
682 }
683 catch( const IO_ERROR& ioe )
684 {
685 wxLogError( _( "Error loading symbol %s from library '%s'." ) + wxS( "\n%s" ),
686 aLibId.GetLibItemName().wx_str(),
687 aLibId.GetLibNickname().wx_str(),
688 ioe.What() );
689 }
690 }
691
692 if( symbol != nullptr )
693 {
694 int pinCount = symbol->GetGraphicalPins( 0 /* all units */, 1 /* single bodyStyle */ ).size();
695 SCH_FIELD* fp_field = symbol->GetField( FIELD_T::FOOTPRINT );
696 wxString fp_name = fp_field ? fp_field->GetFullText() : wxString( "" );
697
698 if( !m_fp_override.IsEmpty() )
699 fp_name = m_fp_override;
700
701 // Explicitly associated footprints (issue #2282) are listed ahead of the glob matches in
702 // written order and bypass the pin-count filter; a mapped EP/NC footprint legally has more
703 // pads than the symbol has pins.
704 for( const ASSOCIATED_FOOTPRINT& assoc : symbol->GetEffectiveAssociatedFootprints() )
705 m_fp_sel_ctrl->AddAlwaysIncludedFootprint( assoc.m_FootprintLibId );
706
707 m_fp_sel_ctrl->FilterByPinCount( pinCount );
708 m_fp_sel_ctrl->FilterByFootprintFilters( symbol->GetFPFilters(), true );
709 m_fp_sel_ctrl->SetDefaultFootprint( fp_name );
710 m_fp_sel_ctrl->UpdateList();
711 m_fp_sel_ctrl->Enable();
712 }
713 else
714 {
715 m_fp_sel_ctrl->UpdateList();
716 m_fp_sel_ctrl->Disable();
717 }
718}
719
720
721void PANEL_SYMBOL_CHOOSER::onFootprintSelected( wxCommandEvent& aEvent )
722{
723 m_fp_override = aEvent.GetString();
724
725 std::erase_if( m_field_edits,
726 []( std::pair<FIELD_T, wxString> const& i )
727 {
728 return i.first == FIELD_T::FOOTPRINT;
729 } );
730
731 m_field_edits.emplace_back( std::make_pair( FIELD_T::FOOTPRINT, m_fp_override ) );
732
734}
735
736
737void PANEL_SYMBOL_CHOOSER::onSymbolSelected( wxCommandEvent& aEvent )
738{
739 LIB_TREE_NODE* node = m_tree->GetCurrentTreeNode();
740
741 if( node && node->m_LibId.IsValid() )
742 {
743 LIB_SYMBOL* symbol = m_frame->GetLibSymbol( node->m_LibId );
744
745 updateBodyStyleChoice( symbol );
746
747 m_symbol_preview->DisplaySymbol( node->m_LibId, node->m_Unit, m_selectedBodyStyle );
748
749 if( !node->m_Footprint.IsEmpty() )
750 {
751 wxCommandEvent evt( EVT_FOOTPRINT_SELECTED );
752 evt.SetString( node->m_Footprint);
753 onFootprintSelected( evt );
754 }
755 else
756 {
757 showFootprintFor( node->m_LibId );
758 }
759
761
763 {
764 std::vector<VARIANT_COMPAT_RESULT> issues = m_compatCallback( node->m_LibId );
765
766 if( !issues.empty() )
767 {
768 wxString html = wxS( "<br><b>" ) + _( "Compatibility Warnings:" ) + wxS( "</b><ul>" );
769
770 for( const VARIANT_COMPAT_RESULT& issue : issues )
771 html += wxS( "<li>" ) + EscapeHTML( issue.detail ) + wxS( "</li>" );
772
773 html += wxS( "</ul>" );
774 m_details->AppendToPage( html );
775 }
776 }
777 }
778 else
779 {
780 m_symbol_preview->SetStatusText( _( "No symbol selected" ) );
781
782 if( m_fp_preview && m_fp_preview->IsInitialized() )
783 m_fp_preview->SetStatusText( wxEmptyString );
784
785 updateBodyStyleChoice( nullptr );
787 }
788}
789
790
791void PANEL_SYMBOL_CHOOSER::onSymbolChosen( wxCommandEvent& aEvent )
792{
793 if( m_tree->GetSelectedLibId().IsValid() )
794 {
795 // Got a selection. We can't just end the modal dialog here, because wx leaks some events
796 // back to the parent window (in particular, the MouseUp following a double click).
797 //
798 // NOW, here's where it gets really fun. wxTreeListCtrl eats MouseUp. This isn't really
799 // feasible to bypass without a fully custom wxDataViewCtrl implementation, and even then
800 // might not be fully possible (docs are vague). To get around this, we use a one-shot
801 // timer to schedule the dialog close.
802 //
803 // See PANEL_SYMBOL_CHOOSER::onCloseTimer for the other end of this spaghetti noodle.
805 }
806}
807
808
810{
811 LIB_ID savedSelection = m_tree->GetSelectedLibId();
812 m_tree->Regenerate( true );
813
814 if( savedSelection.IsValid() )
815 m_tree->CenterLibId( savedSelection );
816}
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:119
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:295
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:247
int GetBodyStyleCount() const override
The body styles are a property of the drawings, which a derived symbol inherits from its root symbol ...
wxString GetBodyStyleDescription(int aBodyStyle, bool aLabel) const override
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 updateBodyStyleChoice(LIB_SYMBOL *aSymbol)
void onSelectBodyStyle(wxCommandEvent &aEvent)
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
LIB_ID GetSelectedLibId(int *aUnit=nullptr, int *aBodyStyle=nullptr) const
To be called after this dialog returns from ShowModal().
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > m_adapter
void onSymbolChosen(wxCommandEvent &aEvent)
Handle the selection of an item.
wxPanel * constructRightPanel(wxWindow *aParent)
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:546
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:159
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.