KiCad PCB EDA Suite
LIB_TREE Class Reference

Widget displaying a tree of symbols with optional search text control and description panel.Custom event sent when a new symbol is preselected. More...

#include <lib_tree.h>

Inheritance diagram for LIB_TREE:

Classes

struct  STATE
 Structure storing state of the symbol tree widget. More...
 

Public Types

enum  FLAGS {
  NONE = 0x00 , SEARCH = 0x01 , DETAILS = 0x02 , ALL_WIDGETS = 0x0F ,
  MULTISELECT = 0x10
}
 < Flags to select extra widgets and options More...
 

Public Member Functions

 LIB_TREE (wxWindow *aParent, const wxString &aRecentSearchesKey, LIB_TABLE *aLibTable, wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > &aAdapter, int aFlags=ALL_WIDGETS, HTML_WINDOW *aDetails=nullptr)
 Construct a symbol tree. More...
 
 ~LIB_TREE () override
 
LIB_ID GetSelectedLibId (int *aUnit=nullptr) const
 For multi-unit symbols, if the user selects the symbol itself rather than picking an individual unit, 0 will be returned in aUnit. More...
 
int GetSelectionCount () const
 
int GetSelectedLibIds (std::vector< LIB_ID > &aSelection, std::vector< int > *aUnit=nullptr) const
 Retrieves a list of selections for trees that allow multi-selection. More...
 
LIB_TREE_NODEGetCurrentTreeNode () const
 
void SelectLibId (const LIB_ID &aLibId)
 Select an item in the tree widget. More...
 
void CenterLibId (const LIB_ID &aLibId)
 Ensure that an item is visible (preferably centered). More...
 
void Unselect ()
 Unselect currently selected item in wxDataViewCtrl. More...
 
void ExpandLibId (const LIB_ID &aLibId)
 Expand and item i the tree widget. More...
 
void SetSearchString (const wxString &aSearchString)
 Save/restore search string. More...
 
wxString GetSearchString () const
 
void Regenerate (bool aKeepState)
 Regenerate the tree. More...
 
void RefreshLibTree ()
 Refreshes the tree (mainly to update highlighting and asterisking) More...
 
wxWindow * GetFocusTarget ()
 
void FocusSearchFieldIfExists ()
 Focus the search widget if it exists. More...
 

Protected Member Functions

void toggleExpand (const wxDataViewItem &aTreeId)
 Expand or collapse a node, switching it to the opposite state. More...
 
void selectIfValid (const wxDataViewItem &aTreeId)
 If a wxDataViewitem is valid, select it and post a selection event. More...
 
void centerIfValid (const wxDataViewItem &aTreeId)
 
void expandIfValid (const wxDataViewItem &aTreeId)
 
void postPreselectEvent ()
 Post a wxEVT_DATAVIEW_SELECTION_CHANGED to notify the selection handler that a new part has been preselected. More...
 
void postSelectEvent ()
 Post SYMBOL_SELECTED event to notify the selection handler that a part has been selected. More...
 
STATE getState () const
 Return the symbol tree widget state. More...
 
void setState (const STATE &aState)
 Restore the symbol tree widget state from an object. More...
 
void updateRecentSearchMenu ()
 
void onQueryText (wxCommandEvent &aEvent)
 
void onQueryCharHook (wxKeyEvent &aEvent)
 
void onQueryMouseMoved (wxMouseEvent &aEvent)
 
void onTreeSelect (wxDataViewEvent &aEvent)
 
void onTreeActivate (wxDataViewEvent &aEvent)
 
void onTreeCharHook (wxKeyEvent &aEvent)
 
void onSize (wxSizeEvent &aEvent)
 
void onDetailsLink (wxHtmlLinkEvent &aEvent)
 
void onPreselect (wxCommandEvent &aEvent)
 
void onItemContextMenu (wxDataViewEvent &aEvent)
 
void onHeaderContextMenu (wxDataViewEvent &aEvent)
 
void onDebounceTimer (wxTimerEvent &aEvent)
 

Protected Attributes

LIB_TABLEm_lib_table
 
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTERm_adapter
 
wxSearchCtrl * m_query_ctrl
 
wxDataViewCtrl * m_tree_ctrl
 
HTML_WINDOWm_details_ctrl
 
wxTimer * m_debounceTimer
 
bool m_inTimerEvent
 
LIB_ID m_last_libid
 
wxString m_recentSearchesKey
 
bool m_skipNextRightClick
 

Detailed Description

Widget displaying a tree of symbols with optional search text control and description panel.Custom event sent when a new symbol is preselected.

Definition at line 45 of file lib_tree.h.

Member Enumeration Documentation

◆ FLAGS

< Flags to select extra widgets and options

Enumerator
NONE 
SEARCH 
DETAILS 
ALL_WIDGETS 
MULTISELECT 

Definition at line 49 of file lib_tree.h.

50 {
51 NONE = 0x00,
52 SEARCH = 0x01,
53 DETAILS = 0x02,
54 ALL_WIDGETS = 0x0F,
55 MULTISELECT = 0x10
56 };
@ NONE
Definition: lib_tree.h:51
@ MULTISELECT
Definition: lib_tree.h:55
@ DETAILS
Definition: lib_tree.h:53
@ ALL_WIDGETS
Definition: lib_tree.h:54
@ SEARCH
Definition: lib_tree.h:52

Constructor & Destructor Documentation

◆ LIB_TREE()

LIB_TREE::LIB_TREE ( wxWindow *  aParent,
const wxString &  aRecentSearchesKey,
LIB_TABLE aLibTable,
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > &  aAdapter,
int  aFlags = ALL_WIDGETS,
HTML_WINDOW aDetails = nullptr 
)

Construct a symbol tree.

Parameters
aParentparent window containing this tree widget
aRecentSearchesKeya key into a global map storing recent searches (usually "power", "symbols", or "footprints", but could be further differentiated)
aLibTabletable containing libraries and items to display
aAdaptera LIB_TREE_MODEL_ADAPTER instance to use
aFlagsselection of sub-widgets to include and other options
aDetailsif not null, a custom HTML_WINDOW to hold symbol details. If null this will be created inside the LIB_TREE.

Definition at line 44 of file lib_tree.cpp.

46 :
47 wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
48 wxWANTS_CHARS | wxTAB_TRAVERSAL | wxNO_BORDER ),
49 m_lib_table( aLibTable ), m_adapter( aAdapter ), m_query_ctrl( nullptr ),
50 m_details_ctrl( nullptr ),
51 m_inTimerEvent( false ),
52 m_recentSearchesKey( aRecentSearchesKey ),
54{
55 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
56
57 // Search text control
58 if( aFlags & SEARCH )
59 {
60 wxBoxSizer* search_sizer = new wxBoxSizer( wxHORIZONTAL );
61
62 m_query_ctrl = new wxSearchCtrl( this, wxID_ANY );
63
64 m_query_ctrl->ShowCancelButton( true );
65
66#ifdef __WXGTK__
67 // wxSearchCtrl vertical height is not calculated correctly on some GTK setups
68 // See https://gitlab.com/kicad/code/kicad/-/issues/9019
69 m_query_ctrl->SetMinSize( wxSize( -1, GetTextExtent( wxT( "qb" ) ).y + 10 ) );
70#endif
71
72 m_debounceTimer = new wxTimer( this );
73
74 search_sizer->Add( m_query_ctrl, 1, wxEXPAND, 5 );
75
76 sizer->Add( search_sizer, 0, wxEXPAND, 5 );
77
78 m_query_ctrl->Bind( wxEVT_TEXT, &LIB_TREE::onQueryText, this );
79
80#if wxCHECK_VERSION( 3, 1, 1 )
81 m_query_ctrl->Bind( wxEVT_SEARCH_CANCEL, &LIB_TREE::onQueryText, this );
82#else
83 m_query_ctrl->Bind( wxEVT_SEARCHCTRL_CANCEL_BTN, &LIB_TREE::onQueryText, this );
84#endif
85 m_query_ctrl->Bind( wxEVT_CHAR_HOOK, &LIB_TREE::onQueryCharHook, this );
86 m_query_ctrl->Bind( wxEVT_MOTION, &LIB_TREE::onQueryMouseMoved, this );
87 m_query_ctrl->Bind( wxEVT_LEAVE_WINDOW,
88 [this] ( wxMouseEvent& aEvt )
89 {
90 SetCursor( wxCURSOR_ARROW );
91 } );
92
93 m_query_ctrl->Bind( wxEVT_MENU,
94 [this]( wxCommandEvent& aEvent )
95 {
96 wxString search;
97 size_t idx = aEvent.GetId() - 1;
98
99 if( idx < g_recentSearches[ m_recentSearchesKey ].size() )
101 },
103
104 Bind( wxEVT_TIMER, &LIB_TREE::onDebounceTimer, this, m_debounceTimer->GetId() );
105 }
106
107 // Tree control
108 int dvFlags = ( aFlags & MULTISELECT ) ? wxDV_MULTIPLE : wxDV_SINGLE;
109 m_tree_ctrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, dvFlags );
110 m_adapter->AttachTo( m_tree_ctrl );
111
112 if( aFlags & DETAILS )
113 sizer->AddSpacer( 5 );
114
115 sizer->Add( m_tree_ctrl, 5, wxRIGHT | wxBOTTOM | wxEXPAND, 1 );
116
117 // Description panel
118 if( aFlags & DETAILS )
119 {
120 if( !aDetails )
121 {
122 wxPoint html_size = ConvertDialogToPixels( wxPoint( 80, 80 ) );
123
124 m_details_ctrl = new HTML_WINDOW( this, wxID_ANY, wxDefaultPosition,
125 wxSize( html_size.x, html_size.y ),
126 wxHW_SCROLLBAR_AUTO );
127
128 sizer->Add( m_details_ctrl, 2, wxTOP | wxEXPAND, 5 );
129 }
130 else
131 {
132 m_details_ctrl = aDetails;
133 }
134
135 m_details_ctrl->Bind( wxEVT_HTML_LINK_CLICKED, &LIB_TREE::onDetailsLink, this );
136 }
137
138 SetSizer( sizer );
139
140 m_tree_ctrl->Bind( wxEVT_SIZE, &LIB_TREE::onSize, this );
141 m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &LIB_TREE::onTreeActivate, this );
142 m_tree_ctrl->Bind( wxEVT_DATAVIEW_SELECTION_CHANGED, &LIB_TREE::onTreeSelect, this );
143 m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, &LIB_TREE::onItemContextMenu, this );
144 m_tree_ctrl->Bind( wxEVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, &LIB_TREE::onHeaderContextMenu,
145 this );
146
147 // Process hotkeys when the tree control has focus:
148 m_tree_ctrl->Bind( wxEVT_CHAR_HOOK, &LIB_TREE::onTreeCharHook, this );
149
150 Bind( SYMBOL_PRESELECTED, &LIB_TREE::onPreselect, this );
151
152 if( m_query_ctrl )
153 {
154 m_query_ctrl->SetDescriptiveText( _( "Filter" ) );
155 m_query_ctrl->SetFocus();
156 m_query_ctrl->SetValue( wxEmptyString );
158
159 // Force an update of the adapter with the empty text to ensure preselect is done
160 Regenerate( false );
161 }
162 else
163 {
164 // There may be a part preselected in the model. Make sure it is displayed.
165 // Regenerate does this in the other branch
167 }
168
169 Layout();
170 sizer->Fit( this );
171
172#ifdef __WXGTK__
173 // Scrollbars must be always enabled to prevent an infinite event loop
174 // more details: http://trac.wxwidgets.org/ticket/18141
175 if( m_details_ctrl )
176 m_details_ctrl->ShowScrollbars( wxSHOW_SB_ALWAYS, wxSHOW_SB_ALWAYS );
177#endif /* __WXGTK__ */
178}
Add dark theme support to wxHtmlWindow.
Definition: html_window.h:34
wxTimer * m_debounceTimer
Definition: lib_tree.h:222
void onQueryMouseMoved(wxMouseEvent &aEvent)
Definition: lib_tree.cpp:532
LIB_TABLE * m_lib_table
Definition: lib_tree.h:215
HTML_WINDOW * m_details_ctrl
Definition: lib_tree.h:221
void onTreeActivate(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:583
wxString m_recentSearchesKey
Definition: lib_tree.h:226
wxDataViewCtrl * m_tree_ctrl
Definition: lib_tree.h:220
void onQueryCharHook(wxKeyEvent &aEvent)
Definition: lib_tree.cpp:482
void onSize(wxSizeEvent &aEvent)
Definition: lib_tree.cpp:592
void onItemContextMenu(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:622
void onQueryText(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:464
bool m_skipNextRightClick
Definition: lib_tree.h:228
bool m_inTimerEvent
Definition: lib_tree.h:223
void onPreselect(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:605
void updateRecentSearchMenu()
Definition: lib_tree.cpp:266
void onTreeSelect(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:573
void postPreselectEvent()
Post a wxEVT_DATAVIEW_SELECTION_CHANGED to notify the selection handler that a new part has been pres...
Definition: lib_tree.cpp:416
void onTreeCharHook(wxKeyEvent &aEvent)
Definition: lib_tree.cpp:547
void onDetailsLink(wxHtmlLinkEvent &aEvent)
Definition: lib_tree.cpp:598
wxSearchCtrl * m_query_ctrl
Definition: lib_tree.h:219
void onDebounceTimer(wxTimerEvent &aEvent)
Definition: lib_tree.cpp:474
void onHeaderContextMenu(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:692
void Regenerate(bool aKeepState)
Regenerate the tree.
Definition: lib_tree.cpp:295
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > m_adapter
Definition: lib_tree.h:217
#define _(s)
constexpr int RECENT_SEARCHES_MAX
Definition: lib_tree.cpp:39
std::map< wxString, std::vector< wxString > > g_recentSearches
Definition: lib_tree.cpp:41

References _, DETAILS, g_recentSearches, m_adapter, m_debounceTimer, m_details_ctrl, m_query_ctrl, m_recentSearchesKey, m_tree_ctrl, MULTISELECT, onDebounceTimer(), onDetailsLink(), onHeaderContextMenu(), onItemContextMenu(), onPreselect(), onQueryCharHook(), onQueryMouseMoved(), onQueryText(), onSize(), onTreeActivate(), onTreeCharHook(), onTreeSelect(), postPreselectEvent(), RECENT_SEARCHES_MAX, Regenerate(), SEARCH, and updateRecentSearchMenu().

◆ ~LIB_TREE()

LIB_TREE::~LIB_TREE ( )
override

Definition at line 181 of file lib_tree.cpp.

182{
183 // Stop the timer during destruction early to avoid potential race conditions (that do happen)
184 m_debounceTimer->Stop();
185}

References m_debounceTimer.

Member Function Documentation

◆ centerIfValid()

void LIB_TREE::centerIfValid ( const wxDataViewItem &  aTreeId)
protected

Definition at line 361 of file lib_tree.cpp.

362{
363 /*
364 * This doesn't actually center because the wxWidgets API is poorly suited to that (and
365 * it might be too noisy as well).
366 *
367 * It does try to keep the given item a bit off the top or bottom of the window.
368 */
369
370 if( aTreeId.IsOk() )
371 {
372 LIB_TREE_NODE* node = m_adapter->GetTreeNodeFor( aTreeId );
373 LIB_TREE_NODE* parent = node->m_Parent;
374 LIB_TREE_NODE* grandParent = parent ? parent->m_Parent : nullptr;
375
376 if( parent )
377 {
378 wxDataViewItemArray siblings;
379 m_adapter->GetChildren( wxDataViewItem( parent ), siblings );
380
381 int idx = siblings.Index( aTreeId );
382
383 if( idx + 5 < (int) siblings.GetCount() )
384 {
385 m_tree_ctrl->EnsureVisible( siblings.Item( idx + 5 ) );
386 }
387 else if( grandParent )
388 {
389 wxDataViewItemArray parentsSiblings;
390 m_adapter->GetChildren( wxDataViewItem( grandParent ), parentsSiblings );
391
392 int p_idx = parentsSiblings.Index( wxDataViewItem( parent ) );
393
394 if( p_idx + 1 < (int) parentsSiblings.GetCount() )
395 m_tree_ctrl->EnsureVisible( parentsSiblings.Item( p_idx + 1 ) );
396 }
397
398 if( idx - 5 >= 0 )
399 m_tree_ctrl->EnsureVisible( siblings.Item( idx - 5 ) );
400 else
401 m_tree_ctrl->EnsureVisible( wxDataViewItem( parent ) );
402 }
403
404 m_tree_ctrl->EnsureVisible( aTreeId );
405 }
406}
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
LIB_TREE_NODE * m_Parent

References m_adapter, LIB_TREE_NODE::m_Parent, and m_tree_ctrl.

Referenced by CenterLibId().

◆ CenterLibId()

void LIB_TREE::CenterLibId ( const LIB_ID aLibId)

◆ expandIfValid()

void LIB_TREE::expandIfValid ( const wxDataViewItem &  aTreeId)
protected

Definition at line 409 of file lib_tree.cpp.

410{
411 if( aTreeId.IsOk() && !m_tree_ctrl->IsExpanded( aTreeId ) )
412 m_tree_ctrl->Expand( aTreeId );
413}

References m_tree_ctrl.

Referenced by ExpandLibId().

◆ ExpandLibId()

void LIB_TREE::ExpandLibId ( const LIB_ID aLibId)

Expand and item i the tree widget.

Definition at line 248 of file lib_tree.cpp.

249{
250 expandIfValid( m_adapter->FindItem( aLibId ) );
251}
void expandIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:409

References expandIfValid(), and m_adapter.

Referenced by SYMBOL_EDIT_FRAME::KiwayMailIn(), FOOTPRINT_EDIT_FRAME::LoadFootprintFromLibrary(), and SYMBOL_EDIT_FRAME::LoadSymbol().

◆ FocusSearchFieldIfExists()

void LIB_TREE::FocusSearchFieldIfExists ( )

Focus the search widget if it exists.

Definition at line 328 of file lib_tree.cpp.

329{
330 if( m_query_ctrl )
331 {
332 m_query_ctrl->SetFocus();
333 }
334}

References m_query_ctrl.

Referenced by FOOTPRINT_TREE_PANE::FocusSearchFieldIfExists().

◆ GetCurrentTreeNode()

LIB_TREE_NODE * LIB_TREE::GetCurrentTreeNode ( ) const

Definition at line 219 of file lib_tree.cpp.

220{
221 wxDataViewItem sel = m_tree_ctrl->GetSelection();
222
223 if( !sel )
224 return nullptr;
225
226 return m_adapter->GetTreeNodeFor( sel );
227}

References m_adapter, and m_tree_ctrl.

Referenced by SYMBOL_EDIT_FRAME::GetCurrentTreeNode(), FOOTPRINT_EDIT_FRAME::GetCurrentTreeNode(), DIALOG_CHOOSE_SYMBOL::OnComponentPreselected(), and onItemContextMenu().

◆ GetFocusTarget()

wxWindow * LIB_TREE::GetFocusTarget ( )

Definition at line 319 of file lib_tree.cpp.

320{
321 if( m_query_ctrl )
322 return m_query_ctrl;
323 else
324 return m_tree_ctrl;
325}

References m_query_ctrl, and m_tree_ctrl.

Referenced by DIALOG_CHOOSE_FOOTPRINT::DIALOG_CHOOSE_FOOTPRINT(), and DIALOG_CHOOSE_SYMBOL::DIALOG_CHOOSE_SYMBOL().

◆ GetSearchString()

wxString LIB_TREE::GetSearchString ( ) const

Definition at line 260 of file lib_tree.cpp.

261{
262 return m_query_ctrl->GetValue();
263}

References m_query_ctrl.

Referenced by updateRecentSearchMenu(), and DIALOG_CHOOSE_SYMBOL::~DIALOG_CHOOSE_SYMBOL().

◆ GetSelectedLibId()

LIB_ID LIB_TREE::GetSelectedLibId ( int *  aUnit = nullptr) const

For multi-unit symbols, if the user selects the symbol itself rather than picking an individual unit, 0 will be returned in aUnit.

Beware that this is an invalid unit number - this should be replaced with whatever default is desired (usually 1).

Parameters
aUnitif not NULL, the selected unit is filled in here.
Returns
the library id of the symbol that has been selected.

Definition at line 188 of file lib_tree.cpp.

189{
190 wxDataViewItem sel = m_tree_ctrl->GetSelection();
191
192 if( !sel )
193 return LIB_ID();
194
195 if( aUnit )
196 *aUnit = m_adapter->GetUnitFor( sel );
197
198 return m_adapter->GetAliasFor( sel );
199}
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49

References m_adapter, and m_tree_ctrl.

Referenced by DIALOG_CHOOSE_FOOTPRINT::GetSelectedLibId(), DIALOG_CHOOSE_SYMBOL::GetSelectedLibId(), getState(), FOOTPRINT_EDIT_FRAME::GetTreeFPID(), SYMBOL_EDIT_FRAME::GetTreeLIBID(), DIALOG_CHOOSE_FOOTPRINT::OnComponentPreselected(), DIALOG_CHOOSE_SYMBOL::OnComponentSelected(), DIALOG_CHOOSE_FOOTPRINT::OnComponentSelected(), onPreselect(), onQueryCharHook(), onTreeActivate(), SYMBOL_EDIT_FRAME::SyncLibraries(), and FOOTPRINT_EDIT_FRAME::SyncLibraryTree().

◆ GetSelectedLibIds()

int LIB_TREE::GetSelectedLibIds ( std::vector< LIB_ID > &  aSelection,
std::vector< int > *  aUnit = nullptr 
) const

Retrieves a list of selections for trees that allow multi-selection.

See also
GetSelectedLibId for details on how aUnit will be filled.
Parameters
aSelectionwill be filled with a list of selected LIB_IDs
aUnitis an optional pointer to a list to fill with unit numbers
Returns
the number of selected items

Definition at line 202 of file lib_tree.cpp.

203{
204 wxDataViewItemArray selection;
205 int count = m_tree_ctrl->GetSelections( selection );
206
207 for( const wxDataViewItem& item : selection )
208 {
209 aSelection.emplace_back( m_adapter->GetAliasFor( item ) );
210
211 if( aUnit )
212 aUnit->emplace_back( m_adapter->GetUnitFor( item ) );
213 }
214
215 return count;
216}

References m_adapter, and m_tree_ctrl.

Referenced by SYMBOL_EDIT_FRAME::GetTreeLIBIDs().

◆ GetSelectionCount()

int LIB_TREE::GetSelectionCount ( ) const
inline

Definition at line 87 of file lib_tree.h.

88 {
89 return m_tree_ctrl->GetSelectedItemsCount();
90 }

References m_tree_ctrl.

Referenced by SYMBOL_EDIT_FRAME::GetTreeSelectionCount().

◆ getState()

LIB_TREE::STATE LIB_TREE::getState ( ) const
protected

Return the symbol tree widget state.

Definition at line 430 of file lib_tree.cpp.

431{
432 STATE state;
433 wxDataViewItemArray items;
434 m_adapter->GetChildren( wxDataViewItem( nullptr ), items );
435
436 for( const wxDataViewItem& item : items )
437 {
438 if( m_tree_ctrl->IsExpanded( item ) )
439 state.expanded.push_back( item );
440 }
441
442 state.selection = GetSelectedLibId();
443
444 return state;
445}
LIB_ID GetSelectedLibId(int *aUnit=nullptr) const
For multi-unit symbols, if the user selects the symbol itself rather than picking an individual unit,...
Definition: lib_tree.cpp:188

References LIB_TREE::STATE::expanded, GetSelectedLibId(), m_adapter, m_tree_ctrl, and LIB_TREE::STATE::selection.

Referenced by Regenerate().

◆ onDebounceTimer()

void LIB_TREE::onDebounceTimer ( wxTimerEvent &  aEvent)
protected

Definition at line 474 of file lib_tree.cpp.

475{
476 m_inTimerEvent = true;
477 Regenerate( false );
478 m_inTimerEvent = false;
479}

References m_inTimerEvent, and Regenerate().

Referenced by LIB_TREE().

◆ onDetailsLink()

void LIB_TREE::onDetailsLink ( wxHtmlLinkEvent &  aEvent)
protected

Definition at line 598 of file lib_tree.cpp.

599{
600 const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
601 ::wxLaunchDefaultBrowser( info.GetHref() );
602}

References info.

Referenced by LIB_TREE().

◆ onHeaderContextMenu()

void LIB_TREE::onHeaderContextMenu ( wxDataViewEvent &  aEvent)
protected

Definition at line 692 of file lib_tree.cpp.

693{
694 ACTION_MENU menu( true, nullptr );
695
696 menu.Add( ACTIONS::selectColumns );
697
698 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
699 {
700 EDA_REORDERABLE_LIST_DIALOG dlg( m_parent, _( "Select Columns" ),
701 m_adapter->GetAvailableColumns(),
702 m_adapter->GetShownColumns() );
703
704 if( dlg.ShowModal() == wxID_OK )
705 m_adapter->SetShownColumns( dlg.EnabledList() );
706 }
707
708#if !wxCHECK_VERSION( 3, 1, 0 )
709 // wxGTK 3.0 sends item right click events for header right clicks
711#endif
712}
static TOOL_ACTION selectColumns
Definition: actions.h:176
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:49
A dialog which allows selecting a list of items from a list of available items, and reordering those ...

References _, ACTION_MENU::Add(), EDA_REORDERABLE_LIST_DIALOG::EnabledList(), m_adapter, m_skipNextRightClick, and ACTIONS::selectColumns.

Referenced by LIB_TREE().

◆ onItemContextMenu()

void LIB_TREE::onItemContextMenu ( wxDataViewEvent &  aEvent)
protected

Definition at line 622 of file lib_tree.cpp.

623{
625 {
626 m_skipNextRightClick = false;
627 return;
628 }
629
630 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
631 {
632 if( !GetCurrentTreeNode() )
633 {
634 wxPoint pos = m_tree_ctrl->ScreenToClient( wxGetMousePosition() );
635
636 // What we actually want is the height of the column header, but wxWidgets gives us
637 // no way to get that, so we use the height of the search ctrl as a proxy. And it's
638 // not even a very good proxy on Mac....
639 int headerHeight = m_tree_ctrl->GetPosition().y;
640#ifdef __WXMAC__
641 headerHeight += 5;
642#endif
643
644 pos.y -= headerHeight;
645
646 wxDataViewItem item;
647 wxDataViewColumn* col;
648 m_tree_ctrl->HitTest( pos, item, col );
649
650 if( item.IsOk() )
651 {
652 m_tree_ctrl->SetFocus();
653 m_tree_ctrl->Select( item );
654 wxSafeYield();
655 }
656 }
657
658 tool->Activate();
659 tool->GetManager()->VetoContextMenuMouseWarp();
660 tool->GetToolMenu().ShowContextMenu();
661
663 tool->GetManager()->DispatchContextMenu( evt );
664 }
665 else
666 {
668
669 if( current && current->m_Type == LIB_TREE_NODE::LIB )
670 {
671 ACTION_MENU menu( true, nullptr );
672
673 if( current->m_Pinned )
674 {
675 menu.Add( ACTIONS::unpinLibrary );
676
677 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
678 m_adapter->UnpinLibrary( current );
679 }
680 else
681 {
682 menu.Add( ACTIONS::pinLibrary );
683
684 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
685 m_adapter->PinLibrary( current );
686 }
687 }
688 }
689}
static TOOL_ACTION pinLibrary
Definition: actions.h:113
static TOOL_ACTION unpinLibrary
Definition: actions.h:114
enum TYPE m_Type
LIB_TREE_NODE * GetCurrentTreeNode() const
Definition: lib_tree.cpp:219
Generic, UI-independent tool event.
Definition: tool_event.h:156
@ TA_MOUSE_CLICK
Definition: tool_event.h:62
@ TC_MOUSE
Definition: tool_event.h:50
@ BUT_RIGHT
Definition: tool_event.h:128

References ACTION_MENU::Add(), BUT_RIGHT, GetCurrentTreeNode(), LIB_TREE_NODE::LIB, m_adapter, LIB_TREE_NODE::m_Pinned, m_skipNextRightClick, m_tree_ctrl, LIB_TREE_NODE::m_Type, ACTIONS::pinLibrary, TA_MOUSE_CLICK, TC_MOUSE, and ACTIONS::unpinLibrary.

Referenced by LIB_TREE().

◆ onPreselect()

void LIB_TREE::onPreselect ( wxCommandEvent &  aEvent)
protected

Definition at line 605 of file lib_tree.cpp.

606{
607 if( m_details_ctrl )
608 {
609 int unit = 0;
610 LIB_ID id = GetSelectedLibId( &unit );
611
612 if( id.IsValid() )
613 m_details_ctrl->SetPage( m_adapter->GenerateInfo( id, unit ) );
614 else
615 m_details_ctrl->SetPage( wxEmptyString );
616 }
617
618 aEvent.Skip();
619}
bool SetPage(const wxString &aSource) override
Definition: html_window.cpp:38
bool IsValid(const std::string &aString, SIM_VALUE::TYPE aValueType=SIM_VALUE::TYPE_FLOAT, NOTATION aNotation=NOTATION::SI)
Definition: sim_value.cpp:128

References GetSelectedLibId(), SIM_VALUE_GRAMMAR::IsValid(), m_adapter, m_details_ctrl, and HTML_WINDOW::SetPage().

Referenced by LIB_TREE().

◆ onQueryCharHook()

void LIB_TREE::onQueryCharHook ( wxKeyEvent &  aEvent)
protected

Definition at line 482 of file lib_tree.cpp.

483{
484 const wxDataViewItem sel = m_tree_ctrl->GetSelection();
485 LIB_TREE_NODE::TYPE type = sel.IsOk() ? m_adapter->GetTypeFor( sel ) : LIB_TREE_NODE::INVALID;
486
487 switch( aKeyStroke.GetKeyCode() )
488 {
489 case WXK_UP:
492 break;
493
494 case WXK_DOWN:
497 break;
498
499 case WXK_ADD:
501
502 if( type == LIB_TREE_NODE::LIB )
503 m_tree_ctrl->Expand( sel );
504
505 break;
506
507 case WXK_SUBTRACT:
509
510 if( type == LIB_TREE_NODE::LIB )
511 m_tree_ctrl->Collapse( sel );
512
513 break;
514
515 case WXK_RETURN:
517
518 if( GetSelectedLibId().IsValid() )
520 else if( type == LIB_TREE_NODE::LIB )
521 toggleExpand( sel );
522
523 break;
524
525 default:
526 aKeyStroke.Skip(); // Any other key: pass on to search box directly.
527 break;
528 }
529}
void postSelectEvent()
Post SYMBOL_SELECTED event to notify the selection handler that a part has been selected.
Definition: lib_tree.cpp:423
void toggleExpand(const wxDataViewItem &aTreeId)
Expand or collapse a node, switching it to the opposite state.
Definition: lib_tree.cpp:337
void selectIfValid(const wxDataViewItem &aTreeId)
If a wxDataViewitem is valid, select it and post a selection event.
Definition: lib_tree.cpp:349
wxDataViewItem GetPrevItem(wxDataViewCtrl const &aView, wxDataViewItem const &aItem)
Get the previous item in list order.
wxDataViewItem GetNextItem(wxDataViewCtrl const &aView, wxDataViewItem const &aItem)
Get the next item in list order.

References GetNextItem(), GetPrevItem(), GetSelectedLibId(), LIB_TREE_NODE::INVALID, SIM_VALUE_GRAMMAR::IsValid(), LIB_TREE_NODE::LIB, m_adapter, m_tree_ctrl, postSelectEvent(), selectIfValid(), toggleExpand(), and updateRecentSearchMenu().

Referenced by LIB_TREE(), and onTreeCharHook().

◆ onQueryMouseMoved()

void LIB_TREE::onQueryMouseMoved ( wxMouseEvent &  aEvent)
protected

Definition at line 532 of file lib_tree.cpp.

533{
534 wxPoint pos = aEvent.GetPosition();
535 wxRect ctrlRect = m_query_ctrl->GetScreenRect();
536 int buttonWidth = ctrlRect.GetHeight(); // Presume buttons are square
537
538 if( m_query_ctrl->IsSearchButtonVisible() && pos.x < buttonWidth )
539 SetCursor( wxCURSOR_ARROW );
540 else if( m_query_ctrl->IsCancelButtonVisible() && pos.x > ctrlRect.GetWidth() - buttonWidth )
541 SetCursor( wxCURSOR_ARROW );
542 else
543 SetCursor( wxCURSOR_IBEAM );
544}

References m_query_ctrl.

Referenced by LIB_TREE().

◆ onQueryText()

void LIB_TREE::onQueryText ( wxCommandEvent &  aEvent)
protected

Definition at line 464 of file lib_tree.cpp.

465{
466 m_debounceTimer->StartOnce( 200 );
467
468 // Required to avoid interaction with SetHint()
469 // See documentation for wxTextEntry::SetHint
470 aEvent.Skip();
471}

References m_debounceTimer.

Referenced by LIB_TREE().

◆ onSize()

void LIB_TREE::onSize ( wxSizeEvent &  aEvent)
protected

Definition at line 592 of file lib_tree.cpp.

593{
594 m_adapter->OnSize( aEvent );
595}

References m_adapter.

Referenced by LIB_TREE().

◆ onTreeActivate()

void LIB_TREE::onTreeActivate ( wxDataViewEvent &  aEvent)
protected

Definition at line 583 of file lib_tree.cpp.

584{
585 if( !GetSelectedLibId().IsValid() )
586 toggleExpand( m_tree_ctrl->GetSelection() ); // Expand library/part units subtree
587 else
588 postSelectEvent(); // Open symbol/footprint
589}

References GetSelectedLibId(), SIM_VALUE_GRAMMAR::IsValid(), m_tree_ctrl, postSelectEvent(), and toggleExpand().

Referenced by LIB_TREE().

◆ onTreeCharHook()

void LIB_TREE::onTreeCharHook ( wxKeyEvent &  aEvent)
protected

Definition at line 547 of file lib_tree.cpp.

548{
549 onQueryCharHook( aKeyStroke );
550
551 if( aKeyStroke.GetSkipped() )
552 {
553 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
554 {
555 int hotkey = aKeyStroke.GetKeyCode();
556
557 if( aKeyStroke.ShiftDown() )
558 hotkey |= MD_SHIFT;
559
560 if( aKeyStroke.AltDown() )
561 hotkey |= MD_ALT;
562
563 if( aKeyStroke.ControlDown() )
564 hotkey |= MD_CTRL;
565
566 if( tool->GetManager()->GetActionManager()->RunHotKey( hotkey ) )
567 aKeyStroke.Skip( false );
568 }
569 }
570}
@ MD_ALT
Definition: tool_event.h:140
@ MD_CTRL
Definition: tool_event.h:139
@ MD_SHIFT
Definition: tool_event.h:138

References m_adapter, MD_ALT, MD_CTRL, MD_SHIFT, and onQueryCharHook().

Referenced by LIB_TREE().

◆ onTreeSelect()

void LIB_TREE::onTreeSelect ( wxDataViewEvent &  aEvent)
protected

Definition at line 573 of file lib_tree.cpp.

574{
575 if( !m_inTimerEvent )
577
578 if( !m_tree_ctrl->IsFrozen() )
580}

References m_inTimerEvent, m_tree_ctrl, postPreselectEvent(), and updateRecentSearchMenu().

Referenced by LIB_TREE().

◆ postPreselectEvent()

void LIB_TREE::postPreselectEvent ( )
protected

Post a wxEVT_DATAVIEW_SELECTION_CHANGED to notify the selection handler that a new part has been preselected.

Definition at line 416 of file lib_tree.cpp.

417{
418 wxCommandEvent event( SYMBOL_PRESELECTED );
419 wxPostEvent( this, event );
420}

Referenced by LIB_TREE(), onTreeSelect(), Regenerate(), and selectIfValid().

◆ postSelectEvent()

void LIB_TREE::postSelectEvent ( )
protected

Post SYMBOL_SELECTED event to notify the selection handler that a part has been selected.

Definition at line 423 of file lib_tree.cpp.

424{
425 wxCommandEvent event( SYMBOL_SELECTED );
426 wxPostEvent( this, event );
427}

Referenced by onQueryCharHook(), and onTreeActivate().

◆ RefreshLibTree()

◆ Regenerate()

void LIB_TREE::Regenerate ( bool  aKeepState)

Regenerate the tree.

Definition at line 295 of file lib_tree.cpp.

296{
297 STATE current;
298
299 // Store the state
300 if( aKeepState )
301 current = getState();
302
303 wxString filter = m_query_ctrl->GetValue();
304 m_adapter->UpdateSearchString( filter, aKeepState );
306
307 // Restore the state
308 if( aKeepState )
309 setState( current );
310}
void setState(const STATE &aState)
Restore the symbol tree widget state from an object.
Definition: lib_tree.cpp:448
STATE getState() const
Return the symbol tree widget state.
Definition: lib_tree.cpp:430

References filter, getState(), m_adapter, m_query_ctrl, postPreselectEvent(), and setState().

Referenced by LIB_TREE(), onDebounceTimer(), SYMBOL_EDIT_FRAME::RegenerateLibraryTree(), FOOTPRINT_EDIT_FRAME::RegenerateLibraryTree(), SYMBOL_EDIT_FRAME::SyncLibraries(), and FOOTPRINT_EDIT_FRAME::SyncLibraryTree().

◆ selectIfValid()

void LIB_TREE::selectIfValid ( const wxDataViewItem &  aTreeId)
protected

If a wxDataViewitem is valid, select it and post a selection event.

Definition at line 349 of file lib_tree.cpp.

350{
351 if( aTreeId.IsOk() )
352 {
353 m_tree_ctrl->EnsureVisible( aTreeId );
354 m_tree_ctrl->UnselectAll();
355 m_tree_ctrl->Select( aTreeId );
357 }
358}

References m_tree_ctrl, and postPreselectEvent().

Referenced by onQueryCharHook(), and SelectLibId().

◆ SelectLibId()

◆ SetSearchString()

void LIB_TREE::SetSearchString ( const wxString &  aSearchString)

Save/restore search string.

Definition at line 254 of file lib_tree.cpp.

255{
256 m_query_ctrl->ChangeValue( aSearchString );
257}

References m_query_ctrl.

Referenced by DIALOG_CHOOSE_SYMBOL::DIALOG_CHOOSE_SYMBOL().

◆ setState()

void LIB_TREE::setState ( const STATE aState)
protected

Restore the symbol tree widget state from an object.

Definition at line 448 of file lib_tree.cpp.

449{
450 m_tree_ctrl->Freeze();
451
452 for( const wxDataViewItem& item : aState.expanded )
453 m_tree_ctrl->Expand( item );
454
455 // wxDataViewCtrl cannot be frozen when a selection
456 // command is issued, otherwise it selects a random item (Windows)
457 m_tree_ctrl->Thaw();
458
459 if( !aState.selection.GetLibItemName().empty() || !aState.selection.GetLibNickname().empty() )
460 SelectLibId( aState.selection );
461}
void SelectLibId(const LIB_ID &aLibId)
Select an item in the tree widget.
Definition: lib_tree.cpp:230

References UTF8::empty(), LIB_TREE::STATE::expanded, LIB_ID::GetLibItemName(), LIB_ID::GetLibNickname(), m_tree_ctrl, LIB_TREE::STATE::selection, and SelectLibId().

Referenced by Regenerate().

◆ toggleExpand()

void LIB_TREE::toggleExpand ( const wxDataViewItem &  aTreeId)
protected

Expand or collapse a node, switching it to the opposite state.

Definition at line 337 of file lib_tree.cpp.

338{
339 if( !aTreeId.IsOk() )
340 return;
341
342 if( m_tree_ctrl->IsExpanded( aTreeId ) )
343 m_tree_ctrl->Collapse( aTreeId );
344 else
345 m_tree_ctrl->Expand( aTreeId );
346}

References m_tree_ctrl.

Referenced by onQueryCharHook(), and onTreeActivate().

◆ Unselect()

◆ updateRecentSearchMenu()

void LIB_TREE::updateRecentSearchMenu ( )
protected

Definition at line 266 of file lib_tree.cpp.

267{
268 wxString newEntry = GetSearchString();
269
270 std::vector<wxString>& recents = g_recentSearches[ m_recentSearchesKey ];
271
272 if( !newEntry.IsEmpty() )
273 {
274 if( alg::contains( recents, newEntry ) )
275 alg::delete_matching( recents, newEntry );
276
277 if( recents.size() >= RECENT_SEARCHES_MAX )
278 recents.pop_back();
279
280 recents.insert( recents.begin(), newEntry );
281 }
282
283 wxMenu* menu = new wxMenu();
284
285 for( const wxString& recent : recents )
286 menu->Append( menu->GetMenuItemCount() + 1, recent );
287
288 if( recents.empty() )
289 menu->Append( wxID_ANY, _( "recent searches" ) );
290
291 m_query_ctrl->SetMenu( menu );
292}
wxString GetSearchString() const
Definition: lib_tree.cpp:260
void delete_matching(_Container &__c, _Value __value)
Covers for the horrifically named std::remove and std::remove_if (neither of which remove anything).
Definition: kicad_algo.h:164
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:99

References _, alg::contains(), alg::delete_matching(), g_recentSearches, GetSearchString(), m_query_ctrl, m_recentSearchesKey, recent, and RECENT_SEARCHES_MAX.

Referenced by LIB_TREE(), onQueryCharHook(), and onTreeSelect().

Member Data Documentation

◆ m_adapter

◆ m_debounceTimer

wxTimer* LIB_TREE::m_debounceTimer
protected

Definition at line 222 of file lib_tree.h.

Referenced by LIB_TREE(), onQueryText(), and ~LIB_TREE().

◆ m_details_ctrl

HTML_WINDOW* LIB_TREE::m_details_ctrl
protected

Definition at line 221 of file lib_tree.h.

Referenced by LIB_TREE(), and onPreselect().

◆ m_inTimerEvent

bool LIB_TREE::m_inTimerEvent
protected

Definition at line 223 of file lib_tree.h.

Referenced by onDebounceTimer(), and onTreeSelect().

◆ m_last_libid

LIB_ID LIB_TREE::m_last_libid
protected

Definition at line 225 of file lib_tree.h.

◆ m_lib_table

LIB_TABLE* LIB_TREE::m_lib_table
protected

Definition at line 215 of file lib_tree.h.

◆ m_query_ctrl

wxSearchCtrl* LIB_TREE::m_query_ctrl
protected

◆ m_recentSearchesKey

wxString LIB_TREE::m_recentSearchesKey
protected

Definition at line 226 of file lib_tree.h.

Referenced by LIB_TREE(), and updateRecentSearchMenu().

◆ m_skipNextRightClick

bool LIB_TREE::m_skipNextRightClick
protected

Definition at line 228 of file lib_tree.h.

Referenced by onHeaderContextMenu(), and onItemContextMenu().

◆ m_tree_ctrl


The documentation for this class was generated from the following files: