KiCad PCB EDA Suite
Loading...
Searching...
No Matches
lib_tree.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 (C) 2014-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <widgets/lib_tree.h>
27#include <core/kicad_algo.h>
28#include <macros.h>
29#include <bitmaps.h>
32#include <tool/tool_manager.h>
33#include <tool/action_manager.h>
34#include <tool/actions.h>
36#include <wx/settings.h>
37#include <wx/sizer.h>
38#include <wx/srchctrl.h>
39#include <wx/popupwin.h>
40#include "wx/app.h"
41
42constexpr int RECENT_SEARCHES_MAX = 10;
43
44std::map<wxString, std::vector<wxString>> g_recentSearches;
45
46
47LIB_TREE::LIB_TREE( wxWindow* aParent, const wxString& aRecentSearchesKey, LIB_TABLE* aLibTable,
48 wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>& aAdapter, int aFlags,
49 HTML_WINDOW* aDetails ) :
50 wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
51 wxWANTS_CHARS | wxTAB_TRAVERSAL | wxNO_BORDER ),
52 m_adapter( aAdapter ),
53 m_query_ctrl( nullptr ),
54 m_sort_ctrl( nullptr ),
55 m_details_ctrl( nullptr ),
56 m_inTimerEvent( false ),
57 m_recentSearchesKey( aRecentSearchesKey ),
58 m_skipNextRightClick( false ),
59 m_previewWindow( nullptr ),
60 m_previewDisabled( false )
61{
62 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
63
64 m_hoverTimer.SetOwner( this );
65 Bind( wxEVT_TIMER, &LIB_TREE::onHoverTimer, this, m_hoverTimer.GetId() );
66
67 // Search text control
68 if( aFlags & SEARCH )
69 {
70 wxBoxSizer* search_sizer = new wxBoxSizer( wxHORIZONTAL );
71
72 m_query_ctrl = new wxSearchCtrl( this, wxID_ANY );
73
74 m_query_ctrl->ShowCancelButton( true );
75
76#ifdef __WXGTK__
77 // wxSearchCtrl vertical height is not calculated correctly on some GTK setups
78 // See https://gitlab.com/kicad/code/kicad/-/issues/9019
79 m_query_ctrl->SetMinSize( wxSize( -1, GetTextExtent( wxT( "qb" ) ).y + 10 ) );
80#endif
81
82 m_debounceTimer = new wxTimer( this );
83
84 search_sizer->Add( m_query_ctrl, 1, wxEXPAND, 5 );
85
86 m_sort_ctrl = new STD_BITMAP_BUTTON( this, wxID_ANY, wxNullBitmap, wxDefaultPosition,
87 wxDefaultSize, wxBU_AUTODRAW|0 );
88 m_sort_ctrl->SetBitmap( KiBitmap( BITMAPS::small_sort_desc ) );
89 m_sort_ctrl->Bind( wxEVT_LEFT_DOWN,
90 [&]( wxMouseEvent& aEvent )
91 {
92 // Build a pop menu:
93 wxMenu menu;
94
95 menu.Append( 4201, _( "Sort by Best Match" ), wxEmptyString, wxITEM_CHECK );
96 menu.Append( 4202, _( "Sort Alphabetically" ), wxEmptyString, wxITEM_CHECK );
97
98 if( m_adapter->GetSortMode() == LIB_TREE_MODEL_ADAPTER::BEST_MATCH )
99 menu.Check( 4201, true );
100 else
101 menu.Check( 4202, true );
102
103 // menu_id is the selected submenu id from the popup menu or wxID_NONE
104 int menu_id = m_sort_ctrl->GetPopupMenuSelectionFromUser( menu );
105
106 if( menu_id == 0 || menu_id == 4201 )
107 {
108 m_adapter->SetSortMode( LIB_TREE_MODEL_ADAPTER::BEST_MATCH );
109 Regenerate( true );
110 }
111 else if( menu_id == 1 || menu_id == 4202 )
112 {
113 m_adapter->SetSortMode( LIB_TREE_MODEL_ADAPTER::ALPHABETIC );
114 Regenerate( true );
115 }
116 } );
117
118 search_sizer->Add( m_sort_ctrl, 0, wxEXPAND|wxALL, 1 );
119
120 sizer->Add( search_sizer, 0, wxEXPAND, 5 );
121
122 m_query_ctrl->Bind( wxEVT_TEXT, &LIB_TREE::onQueryText, this );
123
124 m_query_ctrl->Bind( wxEVT_SEARCH_CANCEL, &LIB_TREE::onQueryText, this );
125 m_query_ctrl->Bind( wxEVT_CHAR_HOOK, &LIB_TREE::onQueryCharHook, this );
126 m_query_ctrl->Bind( wxEVT_MOTION, &LIB_TREE::onQueryMouseMoved, this );
127 m_query_ctrl->Bind( wxEVT_LEAVE_WINDOW,
128 [this] ( wxMouseEvent& aEvt )
129 {
130 SetCursor( wxCURSOR_ARROW );
131 } );
132
133 m_query_ctrl->Bind( wxEVT_MENU,
134 [this]( wxCommandEvent& aEvent )
135 {
136 wxString search;
137 size_t idx = aEvent.GetId() - 1;
138
139 if( idx < g_recentSearches[ m_recentSearchesKey ].size() )
141 },
143
144 Bind( wxEVT_TIMER, &LIB_TREE::onDebounceTimer, this, m_debounceTimer->GetId() );
145 }
146
147 // Tree control
148 int dvFlags = ( aFlags & MULTISELECT ) ? wxDV_MULTIPLE : wxDV_SINGLE;
149 m_tree_ctrl = new WX_DATAVIEWCTRL( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, dvFlags );
150 m_adapter->AttachTo( m_tree_ctrl );
151
152 if( aFlags & DETAILS )
153 sizer->AddSpacer( 5 );
154
155 sizer->Add( m_tree_ctrl, 5, wxRIGHT | wxBOTTOM | wxEXPAND, 1 );
156
157 // Description panel
158 if( aFlags & DETAILS )
159 {
160 if( !aDetails )
161 {
162 wxPoint html_size = ConvertDialogToPixels( wxPoint( 80, 80 ) );
163
164 m_details_ctrl = new HTML_WINDOW( this, wxID_ANY, wxDefaultPosition,
165 wxSize( html_size.x, html_size.y ),
166 wxHW_SCROLLBAR_AUTO );
167
168 sizer->Add( m_details_ctrl, 2, wxTOP | wxEXPAND, 5 );
169 }
170 else
171 {
172 m_details_ctrl = aDetails;
173 }
174
175 m_details_ctrl->Bind( wxEVT_HTML_LINK_CLICKED, &LIB_TREE::onDetailsLink, this );
176 }
177
178 SetSizer( sizer );
179
180 m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &LIB_TREE::onTreeActivate, this );
181 m_tree_ctrl->Bind( wxEVT_DATAVIEW_SELECTION_CHANGED, &LIB_TREE::onTreeSelect, this );
182 m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, &LIB_TREE::onItemContextMenu, this );
183 m_tree_ctrl->Bind( wxEVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, &LIB_TREE::onHeaderContextMenu,
184 this );
185
186 // wxDataViewCtrl eats its mouseMoved events, so we're forced to use idle events to track
187 // the hover state
188 Bind( wxEVT_IDLE, &LIB_TREE::onIdle, this );
189
190 // Process hotkeys when the tree control has focus:
191 m_tree_ctrl->Bind( wxEVT_CHAR_HOOK, &LIB_TREE::onTreeCharHook, this );
192
193 Bind( SYMBOL_PRESELECTED, &LIB_TREE::onPreselect, this );
194
195 if( m_query_ctrl )
196 {
197 m_query_ctrl->SetDescriptiveText( _( "Filter" ) );
198 m_query_ctrl->SetFocus();
199 m_query_ctrl->SetValue( wxEmptyString );
201
202 // Force an update of the adapter with the empty text to ensure preselect is done
203 Regenerate( false );
204 }
205 else
206 {
207 // There may be a part preselected in the model. Make sure it is displayed.
208 // Regenerate does this in the other branch
210 }
211
212 Layout();
213 sizer->Fit( this );
214
215#ifdef __WXGTK__
216 // Scrollbars must be always enabled to prevent an infinite event loop
217 // more details: http://trac.wxwidgets.org/ticket/18141
218 if( m_details_ctrl )
219 m_details_ctrl->ShowScrollbars( wxSHOW_SB_ALWAYS, wxSHOW_SB_ALWAYS );
220#endif /* __WXGTK__ */
221}
222
223
225{
226 // Stop the timer during destruction early to avoid potential race conditions (that do happen)
227 m_debounceTimer->Stop();
228
229 m_hoverTimer.Stop();
230 hidePreview();
231}
232
233
235{
236 if( m_query_ctrl )
237 m_query_ctrl->SetDescriptiveText( _( "Filter" ) );
238
239 if( m_adapter )
240 m_adapter->ShowChangedLanguage();
241}
242
243
245{
246 wxDataViewItem sel = m_tree_ctrl->GetSelection();
247
248 if( !sel )
249 return LIB_ID();
250
251 if( aUnit )
252 *aUnit = m_adapter->GetUnitFor( sel );
253
254 return m_adapter->GetAliasFor( sel );
255}
256
257
258int LIB_TREE::GetSelectedLibIds( std::vector<LIB_ID>& aSelection, std::vector<int>* aUnit ) const
259{
260 wxDataViewItemArray selection;
261 int count = m_tree_ctrl->GetSelections( selection );
262
263 for( const wxDataViewItem& item : selection )
264 {
265 aSelection.emplace_back( m_adapter->GetAliasFor( item ) );
266
267 if( aUnit )
268 aUnit->emplace_back( m_adapter->GetUnitFor( item ) );
269 }
270
271 return count;
272}
273
274
276{
277 wxDataViewItem sel = m_tree_ctrl->GetSelection();
278
279 if( !sel )
280 return nullptr;
281
282 return m_adapter->GetTreeNodeFor( sel );
283}
284
285
286void LIB_TREE::SelectLibId( const LIB_ID& aLibId )
287{
288 selectIfValid( m_adapter->FindItem( aLibId ) );
289}
290
291
292void LIB_TREE::CenterLibId( const LIB_ID& aLibId )
293{
294 centerIfValid( m_adapter->FindItem( aLibId ) );
295}
296
297
299{
300 m_tree_ctrl->UnselectAll();
301}
302
303
304void LIB_TREE::ExpandLibId( const LIB_ID& aLibId )
305{
306 expandIfValid( m_adapter->FindItem( aLibId ) );
307}
308
309
310void LIB_TREE::SetSearchString( const wxString& aSearchString )
311{
312 m_query_ctrl->ChangeValue( aSearchString );
313}
314
315
317{
318 return m_query_ctrl->GetValue();
319}
320
321
323{
324 wxString newEntry = GetSearchString();
325
326 std::vector<wxString>& recents = g_recentSearches[ m_recentSearchesKey ];
327
328 if( !newEntry.IsEmpty() )
329 {
330 if( alg::contains( recents, newEntry ) )
331 alg::delete_matching( recents, newEntry );
332
333 if( recents.size() >= RECENT_SEARCHES_MAX )
334 recents.pop_back();
335
336 recents.insert( recents.begin(), newEntry );
337 }
338
339 wxMenu* menu = new wxMenu();
340
341 for( const wxString& recent : recents )
342 menu->Append( menu->GetMenuItemCount() + 1, recent );
343
344 if( recents.empty() )
345 menu->Append( wxID_ANY, _( "recent searches" ) );
346
347 m_query_ctrl->SetMenu( menu );
348}
349
350
351void LIB_TREE::Regenerate( bool aKeepState )
352{
353 STATE current;
354
355 // Store the state
356 if( aKeepState )
357 current = getState();
358
359 wxString filter = m_query_ctrl->GetValue();
360 m_adapter->UpdateSearchString( filter, aKeepState );
362
363 // Restore the state
364 if( aKeepState )
365 setState( current );
366}
367
368
370{
371 m_adapter->RefreshTree();
372}
373
374
376{
377 if( m_query_ctrl )
378 return m_query_ctrl;
379 else
380 return m_tree_ctrl;
381}
382
383
385{
386 if( m_query_ctrl )
387 m_query_ctrl->SetFocus();
388}
389
390
391void LIB_TREE::toggleExpand( const wxDataViewItem& aTreeId )
392{
393 if( !aTreeId.IsOk() )
394 return;
395
396 if( m_tree_ctrl->IsExpanded( aTreeId ) )
397 m_tree_ctrl->Collapse( aTreeId );
398 else
399 m_tree_ctrl->Expand( aTreeId );
400}
401
402
403void LIB_TREE::selectIfValid( const wxDataViewItem& aTreeId )
404{
405 if( aTreeId.IsOk() )
406 {
407 m_tree_ctrl->EnsureVisible( aTreeId );
408 m_tree_ctrl->UnselectAll();
409 m_tree_ctrl->Select( aTreeId );
411 }
412}
413
414
415void LIB_TREE::centerIfValid( const wxDataViewItem& aTreeId )
416{
417 /*
418 * This doesn't actually center because the wxWidgets API is poorly suited to that (and
419 * it might be too noisy as well).
420 *
421 * It does try to keep the given item a bit off the top or bottom of the window.
422 */
423
424 if( aTreeId.IsOk() )
425 {
426 LIB_TREE_NODE* node = m_adapter->GetTreeNodeFor( aTreeId );
427 LIB_TREE_NODE* parent = node->m_Parent;
428 LIB_TREE_NODE* grandParent = parent ? parent->m_Parent : nullptr;
429
430 if( parent )
431 {
432 wxDataViewItemArray siblings;
433 m_adapter->GetChildren( wxDataViewItem( parent ), siblings );
434
435 int idx = siblings.Index( aTreeId );
436
437 if( idx + 5 < (int) siblings.GetCount() )
438 {
439 m_tree_ctrl->EnsureVisible( siblings.Item( idx + 5 ) );
440 }
441 else if( grandParent )
442 {
443 wxDataViewItemArray parentsSiblings;
444 m_adapter->GetChildren( wxDataViewItem( grandParent ), parentsSiblings );
445
446 int p_idx = parentsSiblings.Index( wxDataViewItem( parent ) );
447
448 if( p_idx + 1 < (int) parentsSiblings.GetCount() )
449 m_tree_ctrl->EnsureVisible( parentsSiblings.Item( p_idx + 1 ) );
450 }
451
452 if( idx - 5 >= 0 )
453 m_tree_ctrl->EnsureVisible( siblings.Item( idx - 5 ) );
454 else
455 m_tree_ctrl->EnsureVisible( wxDataViewItem( parent ) );
456 }
457
458 m_tree_ctrl->EnsureVisible( aTreeId );
459 }
460}
461
462
463void LIB_TREE::expandIfValid( const wxDataViewItem& aTreeId )
464{
465 if( aTreeId.IsOk() && !m_tree_ctrl->IsExpanded( aTreeId ) )
466 m_tree_ctrl->Expand( aTreeId );
467}
468
469
471{
472 wxCommandEvent event( SYMBOL_PRESELECTED );
473 wxPostEvent( this, event );
474}
475
476
478{
479 wxCommandEvent event( SYMBOL_SELECTED );
480 wxPostEvent( this, event );
481}
482
483
485{
486 STATE state;
487 wxDataViewItemArray items;
488 m_adapter->GetChildren( wxDataViewItem( nullptr ), items );
489
490 for( const wxDataViewItem& item : items )
491 {
492 if( m_tree_ctrl->IsExpanded( item ) )
493 state.expanded.push_back( item );
494 }
495
496 state.selection = GetSelectedLibId();
497
498 return state;
499}
500
501
502void LIB_TREE::setState( const STATE& aState )
503{
504 m_tree_ctrl->Freeze();
505
506 for( const wxDataViewItem& item : aState.expanded )
507 m_tree_ctrl->Expand( item );
508
509 // wxDataViewCtrl cannot be frozen when a selection
510 // command is issued, otherwise it selects a random item (Windows)
511 m_tree_ctrl->Thaw();
512
513 if( !aState.selection.GetLibItemName().empty() || !aState.selection.GetLibNickname().empty() )
514 SelectLibId( aState.selection );
515}
516
517
518void LIB_TREE::onQueryText( wxCommandEvent& aEvent )
519{
520 m_debounceTimer->StartOnce( 200 );
521
522 // Required to avoid interaction with SetHint()
523 // See documentation for wxTextEntry::SetHint
524 aEvent.Skip();
525}
526
527
528void LIB_TREE::onDebounceTimer( wxTimerEvent& aEvent )
529{
530 m_inTimerEvent = true;
531 Regenerate( false );
532 m_inTimerEvent = false;
533}
534
535
536void LIB_TREE::onQueryCharHook( wxKeyEvent& aKeyStroke )
537{
538 wxDataViewItem sel = m_tree_ctrl->GetSelection();
539
540 if( !sel.IsOk() )
541 sel = m_adapter->GetCurrentDataViewItem();
542
543 LIB_TREE_NODE::TYPE type = sel.IsOk() ? m_adapter->GetTypeFor( sel ) : LIB_TREE_NODE::INVALID;
544
545 switch( aKeyStroke.GetKeyCode() )
546 {
547 case WXK_UP:
550 break;
551
552 case WXK_DOWN:
555 break;
556
557 case WXK_ADD:
559
560 if( type == LIB_TREE_NODE::LIB )
561 m_tree_ctrl->Expand( sel );
562
563 break;
564
565 case WXK_SUBTRACT:
567
568 if( type == LIB_TREE_NODE::LIB )
569 m_tree_ctrl->Collapse( sel );
570
571 break;
572
573 case WXK_RETURN:
574 case WXK_NUMPAD_ENTER:
576
577 if( GetSelectedLibId().IsValid() )
579 else if( type == LIB_TREE_NODE::LIB )
580 toggleExpand( sel );
581
582 break;
583
584 default:
585 aKeyStroke.Skip(); // Any other key: pass on to search box directly.
586 break;
587 }
588}
589
590
591void LIB_TREE::onQueryMouseMoved( wxMouseEvent& aEvent )
592{
593 wxPoint pos = aEvent.GetPosition();
594 wxRect ctrlRect = m_query_ctrl->GetScreenRect();
595 int buttonWidth = ctrlRect.GetHeight(); // Presume buttons are square
596
597 if( m_query_ctrl->IsSearchButtonVisible() && pos.x < buttonWidth )
598 SetCursor( wxCURSOR_ARROW );
599 else if( m_query_ctrl->IsCancelButtonVisible() && pos.x > ctrlRect.GetWidth() - buttonWidth )
600 SetCursor( wxCURSOR_ARROW );
601 else
602 SetCursor( wxCURSOR_IBEAM );
603}
604
605
606#define PREVIEW_SIZE wxSize( 240, 200 )
607#define HOVER_TIMER_MILLIS 400
608
609
610void LIB_TREE::showPreview( wxDataViewItem aItem )
611{
612 if( aItem.IsOk() && m_adapter->HasPreview( aItem ) )
613 {
614 m_previewItem = aItem;
616
617 wxWindow* topLevelParent = wxGetTopLevelParent( m_parent );
618
619 m_previewWindow = new wxPopupWindow( topLevelParent );
620 m_previewWindow->SetPosition( wxPoint( m_tree_ctrl->GetScreenRect().GetRight() - 10,
621 wxGetMousePosition().y - PREVIEW_SIZE.y / 2 ) );
622 m_previewWindow->SetSize( PREVIEW_SIZE );
623
624 m_previewWindow->Freeze();
625 m_previewWindow->Show();
626
627 m_adapter->ShowPreview( m_previewWindow, aItem );
628 m_previewWindow->Thaw();
629 }
630}
631
632
634{
635 m_previewItem = wxDataViewItem();
636
637 if( m_previewWindow )
638 {
639 m_previewWindow->Hide();
640 m_previewWindow->Destroy();
641 m_previewWindow = nullptr;
642 }
643}
644
645
646void LIB_TREE::onIdle( wxIdleEvent& aEvent )
647{
648 // The wxDataViewCtrl won't give us its mouseMoved events so we're forced to use idle
649 // events to track the hover state
650
651 // The dang thing won't give us scroll events either, so we implement a poor-man's
652 // scroll-checker using the last-known positions of the preview or hover items.
653
654 wxWindow* topLevelParent = wxGetTopLevelParent( m_parent );
655 wxWindow* topLevelFocus = wxGetTopLevelParent( wxWindow::FindFocus() );
656
657 wxPoint screenPos = wxGetMousePosition();
658 wxRect screenRect = m_tree_ctrl->GetScreenRect();
659
660 if( !m_tree_ctrl->IsShown() || m_previewDisabled
661 || topLevelFocus != topLevelParent
662 || !screenRect.Contains( screenPos ) )
663 {
664 m_hoverTimer.Stop();
665 hidePreview();
666 return;
667 }
668
669 wxPoint clientPos = m_tree_ctrl->ScreenToClient( screenPos );
670 wxDataViewItem item;
671 wxDataViewColumn* col = nullptr;
672
673 m_tree_ctrl->HitTest( clientPos, item, col );
674
675 if( m_previewItem.IsOk() )
676 {
677 // Scroll checker
678 if( m_tree_ctrl->GetItemRect( m_previewItem ) != m_previewItemRect )
679 {
680 hidePreview();
681
682 m_hoverPos = clientPos;
683 m_hoverItem = item;
684 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
685 m_hoverTimer.StartOnce( HOVER_TIMER_MILLIS );
686 return;
687 }
688
689 if( item != m_previewItem )
690 {
691 hidePreview();
692 showPreview( item );
693 }
694
695 return;
696 }
697
698 if( m_hoverPos != clientPos )
699 {
700 m_hoverPos = clientPos;
701 m_hoverItem = item;
702 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
703 m_hoverTimer.StartOnce( HOVER_TIMER_MILLIS );
704 }
705}
706
707
708void LIB_TREE::onHoverTimer( wxTimerEvent& aEvent )
709{
710 hidePreview();
711
712 if( !m_tree_ctrl->IsShown() || m_previewDisabled )
713 return;
714
715 wxDataViewItem item;
716 wxDataViewColumn* col = nullptr;
717 m_tree_ctrl->HitTest( m_hoverPos, item, col );
718
719 if( item == m_hoverItem && m_tree_ctrl->GetItemRect( item ) == m_hoverItemRect )
720 {
721 if( item != m_tree_ctrl->GetSelection() )
722 showPreview( item );
723 }
724 else // view must have been scrolled
725 {
726 m_hoverItem = item;
727 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
728 m_hoverTimer.StartOnce( HOVER_TIMER_MILLIS );
729 }
730}
731
732
733void LIB_TREE::onTreeCharHook( wxKeyEvent& aKeyStroke )
734{
735 onQueryCharHook( aKeyStroke );
736
737 if( aKeyStroke.GetSkipped() )
738 {
739 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
740 {
741 int hotkey = aKeyStroke.GetKeyCode();
742
743 if( aKeyStroke.ShiftDown() )
744 hotkey |= MD_SHIFT;
745
746 if( aKeyStroke.AltDown() )
747 hotkey |= MD_ALT;
748
749 if( aKeyStroke.ControlDown() )
750 hotkey |= MD_CTRL;
751
752 if( tool->GetManager()->GetActionManager()->RunHotKey( hotkey ) )
753 aKeyStroke.Skip( false );
754 }
755 }
756}
757
758
759void LIB_TREE::onTreeSelect( wxDataViewEvent& aEvent )
760{
761 if( !m_inTimerEvent )
763
764 if( !m_tree_ctrl->IsFrozen() )
766}
767
768
769void LIB_TREE::onTreeActivate( wxDataViewEvent& aEvent )
770{
771 hidePreview();
772
773 if( !GetSelectedLibId().IsValid() )
774 toggleExpand( m_tree_ctrl->GetSelection() ); // Expand library/part units subtree
775 else
776 postSelectEvent(); // Open symbol/footprint
777}
778
779
780void LIB_TREE::onDetailsLink( wxHtmlLinkEvent& aEvent )
781{
782 const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
783 ::wxLaunchDefaultBrowser( info.GetHref() );
784}
785
786
787void LIB_TREE::onPreselect( wxCommandEvent& aEvent )
788{
789 hidePreview();
790
791 if( m_details_ctrl )
792 {
793 int unit = 0;
794 LIB_ID id = GetSelectedLibId( &unit );
795
796 if( id.IsValid() )
797 m_details_ctrl->SetPage( m_adapter->GenerateInfo( id, unit ) );
798 else
799 m_details_ctrl->SetPage( wxEmptyString );
800 }
801
802 aEvent.Skip();
803}
804
805
806void LIB_TREE::onItemContextMenu( wxDataViewEvent& aEvent )
807{
808 hidePreview();
809
811 {
812 m_skipNextRightClick = false;
813 return;
814 }
815
816 m_previewDisabled = true;
817
818 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
819 {
820 if( !GetCurrentTreeNode() )
821 {
822 wxPoint pos = m_tree_ctrl->ScreenToClient( wxGetMousePosition() );
823
824 // What we actually want is the height of the column header, but wxWidgets gives us
825 // no way to get that, so we use the height of the search ctrl as a proxy. And it's
826 // not even a very good proxy on Mac....
827 int headerHeight = m_tree_ctrl->GetPosition().y;
828#ifdef __WXMAC__
829 headerHeight += 5;
830#endif
831
832 pos.y -= headerHeight;
833
834 wxDataViewItem item;
835 wxDataViewColumn* col;
836 m_tree_ctrl->HitTest( pos, item, col );
837
838 if( item.IsOk() )
839 {
840 m_tree_ctrl->SetFocus();
841 m_tree_ctrl->Select( item );
842 wxSafeYield();
843 }
844 }
845
846 tool->Activate();
847 tool->GetManager()->VetoContextMenuMouseWarp();
848 tool->GetToolMenu().ShowContextMenu();
849
851 tool->GetManager()->DispatchContextMenu( evt );
852 }
853 else
854 {
856
857 if( current && current->m_Type == LIB_TREE_NODE::LIB )
858 {
859 ACTION_MENU menu( true, nullptr );
860
861 if( current->m_Pinned )
862 {
864
865 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
866 m_adapter->UnpinLibrary( current );
867 }
868 else
869 {
870 menu.Add( ACTIONS::pinLibrary );
871
872 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
873 m_adapter->PinLibrary( current );
874 }
875 }
876 }
877
878 m_previewDisabled = false;
879}
880
881
882void LIB_TREE::onHeaderContextMenu( wxDataViewEvent& aEvent )
883{
884 hidePreview();
885 m_previewDisabled = true;
886
887 ACTION_MENU menu( true, nullptr );
888
890
891 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
892 {
893 EDA_REORDERABLE_LIST_DIALOG dlg( m_parent, _( "Select Columns" ),
894 m_adapter->GetAvailableColumns(),
895 m_adapter->GetShownColumns() );
896
897 if( dlg.ShowModal() == wxID_OK )
898 m_adapter->SetShownColumns( dlg.EnabledList() );
899 }
900
901 m_previewDisabled = false;
902}
903
904
905wxDEFINE_EVENT( SYMBOL_PRESELECTED, wxCommandEvent );
906wxDEFINE_EVENT( SYMBOL_SELECTED, wxCommandEvent );
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:106
static TOOL_ACTION selectColumns
Definition: actions.h:181
static TOOL_ACTION pinLibrary
Definition: actions.h:114
static TOOL_ACTION unpinLibrary
Definition: actions.h:115
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:49
wxMenuItem * Add(const wxString &aLabel, int aId, BITMAPS aIcon)
Add a wxWidgets-style entry to the menu.
A dialog which allows selecting a list of items from a list of available items, and reordering those ...
const std::vector< wxString > & EnabledList()
Add dark theme support to wxHtmlWindow.
Definition: html_window.h:34
bool SetPage(const wxString &aSource) override
Definition: html_window.cpp:38
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:87
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
enum TYPE m_Type
LIB_TREE_NODE * m_Parent
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.
Definition: lib_tree.cpp:47
wxRect m_previewItemRect
Definition: lib_tree.h:249
void RefreshLibTree()
Refreshes the tree (mainly to update highlighting and asterisking)
Definition: lib_tree.cpp:369
wxPoint m_hoverPos
Definition: lib_tree.h:244
wxTimer * m_debounceTimer
Definition: lib_tree.h:237
void onHoverTimer(wxTimerEvent &aEvent)
Definition: lib_tree.cpp:708
LIB_TREE_NODE * GetCurrentTreeNode() const
Definition: lib_tree.cpp:275
void onQueryMouseMoved(wxMouseEvent &aEvent)
Definition: lib_tree.cpp:591
wxDataViewItem m_previewItem
Definition: lib_tree.h:248
HTML_WINDOW * m_details_ctrl
Definition: lib_tree.h:236
void onTreeActivate(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:769
wxString m_recentSearchesKey
Definition: lib_tree.h:240
void onQueryCharHook(wxKeyEvent &aEvent)
Definition: lib_tree.cpp:536
void CenterLibId(const LIB_ID &aLibId)
Ensure that an item is visible (preferably centered).
Definition: lib_tree.cpp:292
void postSelectEvent()
Post SYMBOL_SELECTED event to notify the selection handler that a part has been selected.
Definition: lib_tree.cpp:477
wxWindow * GetFocusTarget()
Definition: lib_tree.cpp:375
void onItemContextMenu(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:806
void ShowChangedLanguage()
Definition: lib_tree.cpp:234
WX_DATAVIEWCTRL * m_tree_ctrl
Definition: lib_tree.h:235
void onQueryText(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:518
void toggleExpand(const wxDataViewItem &aTreeId)
Expand or collapse a node, switching it to the opposite state.
Definition: lib_tree.cpp:391
bool m_skipNextRightClick
Definition: lib_tree.h:242
bool m_inTimerEvent
Definition: lib_tree.h:238
void selectIfValid(const wxDataViewItem &aTreeId)
If a wxDataViewitem is valid, select it and post a selection event.
Definition: lib_tree.cpp:403
void onIdle(wxIdleEvent &aEvent)
Definition: lib_tree.cpp:646
void FocusSearchFieldIfExists()
Focus the search widget if it exists.
Definition: lib_tree.cpp:384
void expandIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:463
void onPreselect(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:787
wxPopupWindow * m_previewWindow
Definition: lib_tree.h:250
void SelectLibId(const LIB_ID &aLibId)
Select an item in the tree widget.
Definition: lib_tree.cpp:286
void updateRecentSearchMenu()
Definition: lib_tree.cpp:322
void hidePreview()
Definition: lib_tree.cpp:633
void showPreview(wxDataViewItem aItem)
Definition: lib_tree.cpp:610
wxString GetSearchString() const
Definition: lib_tree.cpp:316
void onTreeSelect(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:759
int GetSelectedLibIds(std::vector< LIB_ID > &aSelection, std::vector< int > *aUnit=nullptr) const
Retrieves a list of selections for trees that allow multi-selection.
Definition: lib_tree.cpp:258
wxTimer m_hoverTimer
Definition: lib_tree.h:247
void postPreselectEvent()
Post a wxEVT_DATAVIEW_SELECTION_CHANGED to notify the selection handler that a new part has been pres...
Definition: lib_tree.cpp:470
void Unselect()
Unselect currently selected item in wxDataViewCtrl.
Definition: lib_tree.cpp:298
void onTreeCharHook(wxKeyEvent &aEvent)
Definition: lib_tree.cpp:733
void onDetailsLink(wxHtmlLinkEvent &aEvent)
Definition: lib_tree.cpp:780
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:244
@ MULTISELECT
Definition: lib_tree.h:58
@ DETAILS
Definition: lib_tree.h:56
@ SEARCH
Definition: lib_tree.h:55
wxDataViewItem m_hoverItem
Definition: lib_tree.h:245
~LIB_TREE() override
Definition: lib_tree.cpp:224
void SetSearchString(const wxString &aSearchString)
Save/restore search string.
Definition: lib_tree.cpp:310
void setState(const STATE &aState)
Restore the symbol tree widget state from an object.
Definition: lib_tree.cpp:502
STATE getState() const
Return the symbol tree widget state.
Definition: lib_tree.cpp:484
bool m_previewDisabled
Definition: lib_tree.h:251
void ExpandLibId(const LIB_ID &aLibId)
Expand and item i the tree widget.
Definition: lib_tree.cpp:304
wxSearchCtrl * m_query_ctrl
Definition: lib_tree.h:233
void centerIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:415
void onDebounceTimer(wxTimerEvent &aEvent)
Definition: lib_tree.cpp:528
wxRect m_hoverItemRect
Definition: lib_tree.h:246
STD_BITMAP_BUTTON * m_sort_ctrl
Definition: lib_tree.h:234
void onHeaderContextMenu(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:882
void Regenerate(bool aKeepState)
Regenerate the tree.
Definition: lib_tree.cpp:351
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > m_adapter
Definition: lib_tree.h:231
A bitmap button widget that behaves like a standard dialog button except with an icon.
void SetBitmap(const wxBitmapBundle &aBmp)
Generic, UI-independent tool event.
Definition: tool_event.h:167
bool empty() const
Definition: utf8.h:103
Extension of the wxDataViewCtrl to include some helper functions for working with items.
wxDataViewItem GetNextItem(wxDataViewItem const &aItem)
Get the next item in list order.
wxDataViewItem GetPrevItem(wxDataViewItem const &aItem)
Get the previous item in list order.
#define _(s)
#define HOVER_TIMER_MILLIS
Definition: lib_tree.cpp:607
constexpr int RECENT_SEARCHES_MAX
Definition: lib_tree.cpp:42
#define PREVIEW_SIZE
Definition: lib_tree.cpp:606
wxDEFINE_EVENT(SYMBOL_PRESELECTED, wxCommandEvent)
std::map< wxString, std::vector< wxString > > g_recentSearches
Definition: lib_tree.cpp:44
This file contains miscellaneous commonly used macros and functions.
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
Structure storing state of the symbol tree widget.
Definition: lib_tree.h:188
LIB_ID selection
Definition: lib_tree.h:194
std::vector< wxDataViewItem > expanded
< List of expanded nodes
Definition: lib_tree.h:190
@ TA_MOUSE_CLICK
Definition: tool_event.h:66
@ TC_MOUSE
Definition: tool_event.h:54
@ MD_ALT
Definition: tool_event.h:144
@ MD_CTRL
Definition: tool_event.h:143
@ MD_SHIFT
Definition: tool_event.h:142
@ BUT_RIGHT
Definition: tool_event.h:132