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 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
21#include <widgets/lib_tree.h>
24#include <core/kicad_algo.h>
25#include <algorithm>
26#include <macros.h>
27#include <bitmaps.h>
30#include <tool/tool_manager.h>
31#include <tool/action_manager.h>
32#include <tool/actions.h>
35#include <wx/settings.h>
36#include <wx/sizer.h>
37#include <wx/srchctrl.h>
38#include <wx/statline.h>
39#include <wx/popupwin.h>
40
41#include <eda_doc.h> // for GetAssociatedDocument()
42#include <pgm_base.h> // for PROJECT
43#include <settings/settings_manager.h> // for PROJECT
44
45constexpr int RECENT_SEARCHES_MAX = 10;
46
47std::map<wxString, std::vector<wxString>> g_recentSearches;
48
49
50LIB_TREE::LIB_TREE( wxWindow* aParent, const wxString& aRecentSearchesKey,
51 wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>& aAdapter, int aFlags,
52 HTML_WINDOW* aDetails ) :
53 wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
54 wxWANTS_CHARS | wxTAB_TRAVERSAL | wxNO_BORDER ),
55 m_adapter( aAdapter ),
56 m_query_ctrl( nullptr ),
57 m_sort_ctrl( nullptr ),
58 m_details_ctrl( nullptr ),
59 m_inTimerEvent( false ),
60 m_recentSearchesKey( aRecentSearchesKey ),
61 m_filtersSizer( nullptr ),
62 m_skipNextRightClick( false ),
64 [this]()
65 {
67 },
69 m_hoverDebounce(
70 [this]()
71 {
72 onHoverPreview();
73 },
75 m_previewWindow( nullptr ),
76 m_previewDisabled( false )
77{
78 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
79
80 // Search text control
81 if( aFlags & SEARCH )
82 {
83 wxBoxSizer* search_sizer = new wxBoxSizer( wxHORIZONTAL );
84
85 m_query_ctrl = new wxSearchCtrl( this, wxID_ANY );
86
87 m_query_ctrl->ShowCancelButton( true );
88
89 search_sizer->Add( m_query_ctrl, 1, wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 );
90
91 wxStaticLine* separator = new wxStaticLine( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLI_VERTICAL );
92 search_sizer->Add( separator, 0, wxEXPAND|wxTOP|wxBOTTOM, 3 );
93
94 m_sort_ctrl = new BITMAP_BUTTON( this, wxID_ANY, wxNullBitmap, wxDefaultPosition,
95 wxDefaultSize, wxBU_AUTODRAW|0 );
96 m_sort_ctrl->SetBitmap( KiBitmapBundle( BITMAPS::config ) );
97 m_sort_ctrl->Bind( wxEVT_LEFT_DOWN,
98 [&]( wxMouseEvent& aEvent )
99 {
100 // Build a pop menu:
101 wxMenu menu;
102
103 menu.Append( 4201, _( "Sort by Best Match" ), wxEmptyString, wxITEM_CHECK );
104 menu.Append( 4202, _( "Sort Alphabetically" ), wxEmptyString, wxITEM_CHECK );
105 menu.AppendSeparator();
106 menu.Append( 4203, ACTIONS::expandAll.GetMenuItem() );
107 menu.Append( 4204, ACTIONS::collapseAll.GetMenuItem() );
108
109 if( m_adapter->GetSortMode() == LIB_TREE_MODEL_ADAPTER::BEST_MATCH )
110 menu.Check( 4201, true );
111 else
112 menu.Check( 4202, true );
113
114 // menu_id is the selected submenu id from the popup menu or wxID_NONE
115 int menu_id = m_sort_ctrl->GetPopupMenuSelectionFromUser( menu );
116
117 if( menu_id == 0 || menu_id == 4201 )
118 {
119 m_adapter->SetSortMode( LIB_TREE_MODEL_ADAPTER::BEST_MATCH );
120 Regenerate( true );
121 }
122 else if( menu_id == 1 || menu_id == 4202 )
123 {
124 m_adapter->SetSortMode( LIB_TREE_MODEL_ADAPTER::ALPHABETIC );
125 Regenerate( true );
126 }
127 else if( menu_id == 3 || menu_id == 4203 )
128 {
129 ExpandAll();
130 }
131 else if( menu_id == 4 || menu_id == 4204 )
132 {
133 CollapseAll();
134 }
135 } );
136
137 m_sort_ctrl->Bind( wxEVT_CHAR_HOOK, &LIB_TREE::onTreeCharHook, this );
138 search_sizer->Add( m_sort_ctrl, 0, wxALIGN_CENTER_VERTICAL, 5 );
139
140 sizer->Add( search_sizer, 0, wxEXPAND, 5 );
141
142 m_query_ctrl->Bind( wxEVT_TEXT, &LIB_TREE::onQueryText, this );
143
144 m_query_ctrl->Bind( wxEVT_SEARCH_CANCEL, &LIB_TREE::onQueryText, this );
145 m_query_ctrl->Bind( wxEVT_CHAR_HOOK, &LIB_TREE::onQueryCharHook, this );
146 m_query_ctrl->Bind( wxEVT_MOTION, &LIB_TREE::onQueryMouseMoved, this );
147
148#if defined( __WXOSX__ ) // Doesn't work properly on other ports
149 m_query_ctrl->Bind( wxEVT_LEAVE_WINDOW,
150 [this]( wxMouseEvent& aEvt )
151 {
152 SetCursor( wxCURSOR_ARROW );
153 } );
154#endif
155
156 m_query_ctrl->Bind( wxEVT_MENU,
157 [this]( wxCommandEvent& aEvent )
158 {
159 size_t idx = aEvent.GetId() - 1;
160
161 if( idx < g_recentSearches[ m_recentSearchesKey ].size() )
162 m_query_ctrl->SetValue( g_recentSearches[ m_recentSearchesKey ][idx] );
163 },
165 }
166
167 if( aFlags & FILTERS )
168 {
169 m_filtersSizer = new wxBoxSizer( wxVERTICAL );
170 sizer->Add( m_filtersSizer, 0, wxEXPAND | wxLEFT, 4 );
171 }
172
173 // Tree control
174 int dvFlags = ( aFlags & MULTISELECT ) ? wxDV_MULTIPLE : wxDV_SINGLE;
175 m_tree_ctrl = new WX_DATAVIEWCTRL( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, dvFlags );
176 m_adapter->AttachTo( m_tree_ctrl );
177
178#ifdef __WXGTK__
179 // The GTK renderer seems to calculate row height incorrectly sometimes; but can be overridden
180 int rowHeight = FromDIP( 6 ) + GetTextExtent( wxS( "pdI" ) ).y;
181 m_tree_ctrl->SetRowHeight( rowHeight );
182#endif
183
184 sizer->Add( m_tree_ctrl, 5, wxEXPAND, 5 );
185
186 // Description panel
187 if( aFlags & DETAILS )
188 {
189 if( !aDetails )
190 {
191 wxPoint html_size = ConvertDialogToPixels( wxPoint( 80, 80 ) );
192
193 m_details_ctrl = new HTML_WINDOW( this, wxID_ANY, wxDefaultPosition,
194 wxSize( html_size.x, html_size.y ) );
195
196 sizer->Add( m_details_ctrl, 2, wxTOP | wxEXPAND, 5 );
197 }
198 else
199 {
200 m_details_ctrl = aDetails;
201 }
202
203 m_details_ctrl->Bind( wxEVT_HTML_LINK_CLICKED, &LIB_TREE::onDetailsLink, this );
204 }
205
206 SetSizer( sizer );
207
208 m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &LIB_TREE::onTreeActivate, this );
209 m_tree_ctrl->Bind( wxEVT_DATAVIEW_SELECTION_CHANGED, &LIB_TREE::onTreeSelect, this );
210 m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, &LIB_TREE::onItemContextMenu, this );
211 m_tree_ctrl->Bind( wxEVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, &LIB_TREE::onHeaderContextMenu,
212 this );
213
214 // wxDataViewCtrl eats its mouseMoved events, so we're forced to use idle events to track
215 // the hover state
216 Bind( wxEVT_IDLE, &LIB_TREE::onIdle, this );
217
218 // Process hotkeys when the tree control has focus:
219 m_tree_ctrl->Bind( wxEVT_CHAR_HOOK, &LIB_TREE::onTreeCharHook, this );
220
221 Bind( EVT_LIBITEM_SELECTED, &LIB_TREE::onPreselect, this );
222
223 if( m_query_ctrl )
224 {
225 m_query_ctrl->SetDescriptiveText( _( "Filter" ) );
226 m_query_ctrl->SetFocus();
227 m_query_ctrl->ChangeValue( wxEmptyString );
228 updateRecentSearchMenu();
229
230 // Force an update of the adapter with the empty text to ensure preselect is done
231 Regenerate( false );
232 }
233 else
234 {
235 // There may be a part preselected in the model. Make sure it is displayed.
236 // Regenerate does this in the other branch
237 postPreselectEvent();
238 }
239
240 Layout();
241 sizer->Fit( this );
242
243#ifdef __WXGTK__
244 // Scrollbars must be always enabled to prevent an infinite event loop
245 // more details: http://trac.wxwidgets.org/ticket/18141
246 if( m_details_ctrl )
247 m_details_ctrl->ShowScrollbars( wxSHOW_SB_ALWAYS, wxSHOW_SB_ALWAYS );
248#endif /* __WXGTK__ */
249}
250
251
253{
254 m_tree_ctrl->Unbind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &LIB_TREE::onTreeActivate, this );
255 m_tree_ctrl->Unbind( wxEVT_DATAVIEW_SELECTION_CHANGED, &LIB_TREE::onTreeSelect, this );
256 m_tree_ctrl->Unbind( wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, &LIB_TREE::onItemContextMenu, this );
257 m_tree_ctrl->Unbind( wxEVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, &LIB_TREE::onHeaderContextMenu,
258 this );
259
260 Unbind( wxEVT_IDLE, &LIB_TREE::onIdle, this );
261 m_tree_ctrl->Unbind( wxEVT_CHAR_HOOK, &LIB_TREE::onTreeCharHook, this );
262 Unbind( EVT_LIBITEM_SELECTED, &LIB_TREE::onPreselect, this );
263
264 if( m_query_ctrl )
265 {
266 m_query_ctrl->Unbind( wxEVT_TEXT, &LIB_TREE::onQueryText, this );
267
268 m_query_ctrl->Unbind( wxEVT_SEARCH_CANCEL, &LIB_TREE::onQueryText, this );
269 m_query_ctrl->Unbind( wxEVT_CHAR_HOOK, &LIB_TREE::onQueryCharHook, this );
270 m_query_ctrl->Unbind( wxEVT_MOTION, &LIB_TREE::onQueryMouseMoved, this );
271 }
272
273 if( m_details_ctrl )
274 m_details_ctrl->Unbind( wxEVT_HTML_LINK_CLICKED, &LIB_TREE::onDetailsLink, this );
275
276 // Drop pending debounces before the tree starts tearing down.
277 m_queryDebounce.Cancel();
278 m_hoverDebounce.Cancel();
279}
280
281
283{
284 m_hoverDebounce.Cancel();
285 m_previewDisabled = true;
286
287 if( m_previewWindow )
288 {
289 // Shutdown the preview window's canvas
290 m_adapter->ShutdownPreview( m_previewWindow );
291
292 m_previewWindow->Hide();
293 m_previewWindow->Destroy();
294 m_previewWindow = nullptr;
295 }
296}
297
298
300{
301 if( m_query_ctrl )
302 m_query_ctrl->SetDescriptiveText( _( "Filter" ) );
303
304 if( m_adapter )
305 m_adapter->ShowChangedLanguage();
306}
307
308
310{
311 wxDataViewItem sel = m_tree_ctrl->GetSelection();
312
313 if( !sel )
314 return LIB_ID();
315
316 if( m_adapter->GetTreeNodeFor( sel )->m_IsAlreadyPlacedGroup
317 || m_adapter->GetTreeNodeFor( sel )->m_IsRecentlyUsedGroup )
318 {
319 return LIB_ID();
320 }
321
322 if( aUnit )
323 *aUnit = m_adapter->GetUnitFor( sel );
324
325 return m_adapter->GetAliasFor( sel );
326}
327
328
329int LIB_TREE::GetSelectedLibIds( std::vector<LIB_ID>& aSelection, std::vector<int>* aUnit ) const
330{
331 wxDataViewItemArray selection;
332 int count = m_tree_ctrl->GetSelections( selection );
333
334 for( const wxDataViewItem& item : selection )
335 {
336 aSelection.emplace_back( m_adapter->GetAliasFor( item ) );
337
338 if( aUnit )
339 aUnit->emplace_back( m_adapter->GetUnitFor( item ) );
340 }
341
342 return count;
343}
344
345
347{
348 wxDataViewItem sel = m_tree_ctrl->GetSelection();
349
350 if( !sel )
351 return nullptr;
352
353 return m_adapter->GetTreeNodeFor( sel );
354}
355
356int LIB_TREE::GetSelectedTreeNodes( std::vector<LIB_TREE_NODE*>& aSelection ) const
357{
358 wxDataViewItemArray selection;
359 int count = m_tree_ctrl->GetSelections( selection );
360
361 for( const wxDataViewItem& item : selection )
362 {
363 aSelection.push_back( m_adapter->GetTreeNodeFor( item ) );
364 }
365
366 return count;
367}
368
369
370void LIB_TREE::SelectLibId( const LIB_ID& aLibId )
371{
372 wxDataViewItem item = m_adapter->FindItem( aLibId );
373
374 // Expand the parent library so the row is visible and can be selected/scrolled to.
375 if( isShown( item ) )
376 m_tree_ctrl->ExpandAncestors( item );
377
378 selectIfValid( item );
379}
380
381
382void LIB_TREE::CenterLibId( const LIB_ID& aLibId )
383{
384 centerIfValid( m_adapter->FindItem( aLibId ) );
385}
386
387
389{
390 m_tree_ctrl->Freeze();
391 m_tree_ctrl->UnselectAll();
392 m_tree_ctrl->Thaw();
393}
394
395
396void LIB_TREE::ExpandLibId( const LIB_ID& aLibId )
397{
398 expandIfValid( m_adapter->FindItem( aLibId ) );
399}
400
401
402std::vector<LIB_ID> LIB_TREE::GetExpandedLibraries() const
403{
404 std::vector<LIB_ID> expanded;
405 wxDataViewItemArray items;
406 m_adapter->GetChildren( wxDataViewItem( nullptr ), items );
407
408 for( const wxDataViewItem& item : items )
409 {
410 if( m_tree_ctrl->IsExpanded( item ) )
411 {
412 if( LIB_TREE_NODE* node = m_adapter->GetTreeNodeFor( item ) )
413 expanded.push_back( node->m_LibId );
414 }
415 }
416
417 return expanded;
418}
419
420
422{
423 m_tree_ctrl->ExpandAll();
424}
425
426
428{
429 m_tree_ctrl->CollapseAll();
430}
431
432
433void LIB_TREE::SetSearchString( const wxString& aSearchString )
434{
435 m_query_ctrl->ChangeValue( aSearchString );
436}
437
438
440{
441 return m_query_ctrl->GetValue();
442}
443
444
446{
447 wxString newEntry = GetSearchString();
448
449 std::vector<wxString>& recents = g_recentSearches[ m_recentSearchesKey ];
450
451 if( !newEntry.IsEmpty() )
452 {
453 if( alg::contains( recents, newEntry ) )
454 std::erase( recents, newEntry );
455
456 if( recents.size() >= RECENT_SEARCHES_MAX )
457 recents.pop_back();
458
459 recents.insert( recents.begin(), newEntry );
460 }
461
462 wxMenu* menu = new wxMenu();
463
464 for( const wxString& recent : recents )
465 menu->Append( menu->GetMenuItemCount() + 1, recent );
466
467 if( recents.empty() )
468 menu->Append( wxID_ANY, _( "recent searches" ) );
469
470 m_query_ctrl->SetMenu( menu );
471}
472
473
474void LIB_TREE::Regenerate( bool aKeepState )
475{
476 STATE current;
477
478 // Store the state
479 if( aKeepState )
480 current = getState();
481
482 wxString filter = m_query_ctrl ? m_query_ctrl->GetValue() : wxString();
483 m_adapter->UpdateSearchString( filter, aKeepState );
485
486 // Restore the state
487 if( aKeepState )
488 setState( current );
489}
490
491
493{
494 m_adapter->RefreshTree();
495}
496
497
498bool LIB_TREE::isShown( const wxDataViewItem& aTreeId ) const
499{
500 // A row the filter hides has no native row to select
501 return aTreeId.IsOk() && m_adapter->GetTreeNodeFor( aTreeId )->m_Score > 0;
502}
503
504
506{
507 if( m_query_ctrl )
508 return m_query_ctrl;
509 else
510 return m_tree_ctrl;
511}
512
513
515{
516 if( m_query_ctrl )
517 m_query_ctrl->SetFocus();
518}
519
520
521void LIB_TREE::toggleExpand( const wxDataViewItem& aTreeId )
522{
523 if( !aTreeId.IsOk() )
524 return;
525
526 if( m_tree_ctrl->IsExpanded( aTreeId ) )
527 m_tree_ctrl->Collapse( aTreeId );
528 else
529 m_tree_ctrl->Expand( aTreeId );
530}
531
532
533void LIB_TREE::selectIfValid( const wxDataViewItem& aTreeId )
534{
535 if( isShown( aTreeId ) )
536 {
538 m_tree_ctrl->UnselectAll();
539 m_tree_ctrl->Select( aTreeId );
541 }
542}
543
544
545void LIB_TREE::centerIfValid( const wxDataViewItem& aTreeId )
546{
547 /*
548 * This doesn't actually center because the wxWidgets API is poorly suited to that (and
549 * it might be too noisy as well).
550 *
551 * It does try to keep the given item a bit off the top or bottom of the window.
552 */
553
554 if( isShown( aTreeId ) )
555 {
556 LIB_TREE_NODE* node = m_adapter->GetTreeNodeFor( aTreeId );
557 LIB_TREE_NODE* parent = node->m_Parent;
558 LIB_TREE_NODE* grandParent = parent ? parent->m_Parent : nullptr;
559
560 if( parent )
561 {
562 wxDataViewItemArray siblings;
563 m_adapter->GetChildren( wxDataViewItem( parent ), siblings );
564
565 int idx = siblings.Index( aTreeId );
566
567 if( idx + 5 < (int) siblings.GetCount() )
568 {
569 EnsureVisibleIfEnabled( m_tree_ctrl, siblings.Item( idx + 5 ) );
570 }
571 else if( grandParent )
572 {
573 wxDataViewItemArray parentsSiblings;
574 m_adapter->GetChildren( wxDataViewItem( grandParent ), parentsSiblings );
575
576 int p_idx = parentsSiblings.Index( wxDataViewItem( parent ) );
577
578 if( p_idx + 1 < (int) parentsSiblings.GetCount() )
579 EnsureVisibleIfEnabled( m_tree_ctrl, parentsSiblings.Item( p_idx + 1 ) );
580 }
581
582 if( idx - 5 >= 0 )
583 EnsureVisibleIfEnabled( m_tree_ctrl, siblings.Item( idx - 5 ) );
584 else
585 EnsureVisibleIfEnabled( m_tree_ctrl, wxDataViewItem( parent ) );
586 }
587
589 }
590}
591
592
593void LIB_TREE::expandIfValid( const wxDataViewItem& aTreeId )
594{
595 if( isShown( aTreeId ) && !m_tree_ctrl->IsExpanded( aTreeId ) )
596 m_tree_ctrl->Expand( aTreeId );
597}
598
599
601{
602 wxCommandEvent event( EVT_LIBITEM_SELECTED );
603 wxPostEvent( this, event );
604}
605
606
608{
609 wxCommandEvent event( EVT_LIBITEM_CHOSEN );
610 wxPostEvent( this, event );
611}
612
613
615{
616 STATE state;
617 wxDataViewItemArray items;
618 m_adapter->GetChildren( wxDataViewItem( nullptr ), items );
619
620 for( const wxDataViewItem& item : items )
621 {
622 if( m_tree_ctrl->IsExpanded( item ) )
623 state.expanded.push_back( item );
624 }
625
626 state.selection = GetSelectedLibId();
627
628 state.scrollpos = {
629 m_tree_ctrl->HasScrollbar( wxHORIZONTAL ) ? m_tree_ctrl->GetScrollPos( wxHORIZONTAL ) : 0,
630 m_tree_ctrl->HasScrollbar( wxVERTICAL ) ? m_tree_ctrl->GetScrollPos( wxVERTICAL ) : 0
631 };
632
633 return state;
634}
635
636
637void LIB_TREE::setState( const STATE& aState )
638{
639 m_tree_ctrl->Freeze();
640
641 for( const wxDataViewItem& item : aState.expanded )
642 m_tree_ctrl->Expand( item );
643
644 // TODO(JE) probably remove this; it fights with centerIfValid
645 // m_tree_ctrl->SetScrollPos( wxHORIZONTAL, aState.scrollpos.x );
646 // m_tree_ctrl->SetScrollPos( wxVERTICAL, aState.scrollpos.y );
647
648 // wxDataViewCtrl cannot be frozen when a selection
649 // command is issued, otherwise it selects a random item (Windows)
650 m_tree_ctrl->Thaw();
651
652 SelectLibId( aState.selection );
653}
654
655
656void LIB_TREE::onQueryText( wxCommandEvent& aEvent )
657{
658 m_queryDebounce.Restart();
659
660 // Required to avoid interaction with SetHint()
661 // See documentation for wxTextEntry::SetHint
662 aEvent.Skip();
663}
664
665
667{
668 m_inTimerEvent = true;
669 Regenerate( false );
670 m_inTimerEvent = false;
671}
672
673
674void LIB_TREE::onQueryCharHook( wxKeyEvent& aKeyStroke )
675{
676 int hotkey = aKeyStroke.GetKeyCode();
677
678 int mods = aKeyStroke.GetModifiers();
679
680 // the flag wxMOD_ALTGR is defined in wxWidgets as wxMOD_CONTROL|wxMOD_ALT
681 // So AltGr key cannot used as modifier key because it is the same as Alt key + Ctrl key.
682#if CAN_USE_ALTGR_KEY
683 if( wxmods & wxMOD_ALTGR )
684 mods |= MD_ALTGR;
685 else
686#endif
687 {
688 if( mods & wxMOD_CONTROL )
689 hotkey += MD_CTRL;
690
691 if( mods & wxMOD_ALT )
692 hotkey += MD_ALT;
693 }
694
695 if( mods & wxMOD_SHIFT )
696 hotkey += MD_SHIFT;
697
698#ifdef wxMOD_META
699 if( mods & wxMOD_META )
700 hotkey += MD_META;
701#endif
702
703#ifdef wxMOD_WIN
704 if( mods & wxMOD_WIN )
705 hotkey += MD_SUPER;
706#endif
707
708 if( hotkey == ACTIONS::expandAll.GetHotKey()
709 || hotkey == ACTIONS::expandAll.GetHotKeyAlt() )
710 {
711 m_tree_ctrl->ExpandAll();
712 return;
713 }
714 else if( hotkey == ACTIONS::collapseAll.GetHotKey()
715 || hotkey == ACTIONS::collapseAll.GetHotKeyAlt() )
716 {
717 m_tree_ctrl->CollapseAll();
718 return;
719 }
720
721 wxDataViewItem sel = m_tree_ctrl->GetSelection();
722
723 if( !sel.IsOk() )
724 sel = m_adapter->GetCurrentDataViewItem();
725
726 LIB_TREE_NODE::TYPE type = sel.IsOk() ? m_adapter->GetTypeFor( sel )
727 : LIB_TREE_NODE::TYPE::INVALID;
728
729 switch( aKeyStroke.GetKeyCode() )
730 {
731 case WXK_UP:
733 selectIfValid( m_tree_ctrl->GetPrevItem( sel ) );
734 break;
735
736 case WXK_DOWN:
738 selectIfValid( m_tree_ctrl->GetNextItem( sel ) );
739 break;
740
741 case WXK_ADD:
743
744 if( type == LIB_TREE_NODE::TYPE::LIBRARY )
745 m_tree_ctrl->Expand( sel );
746
747 break;
748
749 case WXK_SUBTRACT:
751
752 if( type == LIB_TREE_NODE::TYPE::LIBRARY )
753 m_tree_ctrl->Collapse( sel );
754
755 break;
756
757 case WXK_RETURN:
758 case WXK_NUMPAD_ENTER:
760
761 if( GetSelectedLibId().IsValid() )
763 else if( type == LIB_TREE_NODE::TYPE::LIBRARY )
764 toggleExpand( sel );
765
766 break;
767
768 default:
769 aKeyStroke.Skip(); // Any other key: pass on to search box directly.
770 break;
771 }
772}
773
774
775void LIB_TREE::onQueryMouseMoved( wxMouseEvent& aEvent )
776{
777#if defined( __WXOSX__ ) // Doesn't work properly on other ports
778 wxPoint pos = aEvent.GetPosition();
779 wxRect ctrlRect = m_query_ctrl->GetScreenRect();
780 int buttonWidth = ctrlRect.GetHeight(); // Presume buttons are square
781
782 if( m_query_ctrl->IsSearchButtonVisible() && pos.x < buttonWidth )
783 SetCursor( wxCURSOR_ARROW );
784 else if( m_query_ctrl->IsCancelButtonVisible() && pos.x > ctrlRect.GetWidth() - buttonWidth )
785 SetCursor( wxCURSOR_ARROW );
786 else
787 SetCursor( wxCURSOR_IBEAM );
788#endif
789}
790
791
792#define PREVIEW_SIZE wxSize( 240, 200 )
793
794
795void LIB_TREE::showPreview( wxDataViewItem aItem )
796{
797 if( aItem.IsOk() && m_adapter->HasPreview( aItem ) )
798 {
799 m_previewItem = aItem;
801
802 wxWindow* topLevelParent = wxGetTopLevelParent( m_parent );
803
804 if( !m_previewWindow )
805 m_previewWindow = new wxPopupWindow( topLevelParent );
806
807 m_previewWindow->SetSize( PREVIEW_SIZE );
808
809 m_adapter->ShowPreview( m_previewWindow, aItem );
810
811 m_previewWindow->SetPosition( wxPoint( m_tree_ctrl->GetScreenRect().GetRight() - 10,
812 wxGetMousePosition().y - PREVIEW_SIZE.y / 2 ) );
813
814 m_previewWindow->Show();
815 }
816}
817
818
820{
821 m_previewItem = wxDataViewItem();
822
823 if( m_previewWindow )
824 m_previewWindow->Hide();
825}
826
827
828void LIB_TREE::onIdle( wxIdleEvent& aEvent )
829{
830 // The wxDataViewCtrl won't give us its mouseMoved events so we're forced to use idle
831 // events to track the hover state
832
833 // The dang thing won't give us scroll events either, so we implement a poor-man's
834 // scroll-checker using the last-known positions of the preview or hover items.
835
836 wxWindow* topLevelParent = wxGetTopLevelParent( m_parent );
837 wxWindow* topLevelFocus = wxGetTopLevelParent( wxWindow::FindFocus() );
838
839 bool mouseOverWindow = false;
840 wxPoint screenPos = wxGetMousePosition();
841
842 if( m_tree_ctrl->IsShownOnScreen() )
843 mouseOverWindow |= m_tree_ctrl->GetScreenRect().Contains( screenPos );
844
845 if( m_previewDisabled || topLevelFocus != topLevelParent || !mouseOverWindow )
846 {
847 m_hoverDebounce.Cancel();
848 hidePreview();
849 return;
850 }
851
852 wxPoint clientPos = m_tree_ctrl->ScreenToClient( screenPos );
853 wxDataViewItem item;
854 wxDataViewColumn* col = nullptr;
855
856 m_tree_ctrl->HitTest( clientPos, item, col );
857
858 if( m_previewItem.IsOk() )
859 {
860 if( item != m_previewItem )
861 {
862#ifdef __WXGTK__
863 // Hide the preview, because Wayland can't move windows.
864 if( wxGetDisplayInfo().type == wxDisplayType::wxDisplayWayland )
865 hidePreview();
866#endif
867 showPreview( item );
868 }
869
870 return;
871 }
872
873 if( m_hoverPos != clientPos )
874 {
875 m_hoverPos = clientPos;
876 m_hoverItem = item;
877 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
878 m_hoverDebounce.Restart();
879 }
880}
881
882
884{
885 hidePreview();
886
887 if( !m_tree_ctrl->IsShownOnScreen() || m_previewDisabled )
888 return;
889
890 wxDataViewItem item;
891 wxDataViewColumn* col = nullptr;
892 m_tree_ctrl->HitTest( m_hoverPos, item, col );
893
894 if( item == m_hoverItem && m_tree_ctrl->GetItemRect( item ) == m_hoverItemRect )
895 {
896 if( item != m_tree_ctrl->GetSelection() )
897 showPreview( item );
898 }
899 else // view must have been scrolled
900 {
901 m_hoverItem = item;
902 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
903 m_hoverDebounce.Restart();
904 }
905}
906
907
908void LIB_TREE::onTreeCharHook( wxKeyEvent& aKeyStroke )
909{
910 // Bare Left/Right in the tree control collapse/expand the focused node. Consume them
911 // here so they do not fall through to the global cursorLeft/cursorRight canvas actions.
912 // Note that this handler is also bound to the m_query_ctrl, which does *not* want this
913 // behaviour, so we need to check the source of the event.
914 if( aKeyStroke.GetModifiers() == wxMOD_NONE
915 && ( aKeyStroke.GetKeyCode() == WXK_LEFT || aKeyStroke.GetKeyCode() == WXK_RIGHT )
916 && aKeyStroke.GetEventObject() == m_tree_ctrl )
917 {
918 wxDataViewItem sel = m_tree_ctrl->GetSelection();
919
920 if( !sel.IsOk() )
921 sel = m_adapter->GetCurrentDataViewItem();
922
923 if( sel.IsOk() )
924 {
925 if( aKeyStroke.GetKeyCode() == WXK_RIGHT )
926 m_tree_ctrl->Expand( sel );
927 else
928 m_tree_ctrl->Collapse( sel );
929 }
930
931 return;
932 }
933
934 onQueryCharHook( aKeyStroke );
935
936 if( aKeyStroke.GetSkipped() )
937 {
938 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
939 {
940 int hotkey = aKeyStroke.GetKeyCode();
941
942 int mods = aKeyStroke.GetModifiers();
943
944 if( mods & wxMOD_ALTGR )
945 hotkey |= MD_ALTGR;
946 else
947 {
948 if( mods & wxMOD_ALT )
949 hotkey |= MD_ALT;
950
951 if( mods & wxMOD_CONTROL )
952 hotkey |= MD_CTRL;
953 }
954
955 if( mods & wxMOD_SHIFT )
956 hotkey |= MD_SHIFT;
957
958#ifdef wxMOD_META
959 if( mods & wxMOD_META )
960 hotkey |= MD_META;
961#endif
962
963#ifdef wxMOD_WIN
964 if( mods & wxMOD_WIN )
965 hotkey |= MD_SUPER;
966#endif
967
968 if( tool->GetManager()->GetActionManager()->RunHotKey( hotkey ) )
969 aKeyStroke.Skip( false );
970 }
971 }
972}
973
974
975void LIB_TREE::onTreeSelect( wxDataViewEvent& aEvent )
976{
977 if( m_tree_ctrl->IsFrozen() )
978 return;
979
980 if( !m_inTimerEvent )
982
984}
985
986
987void LIB_TREE::onTreeActivate( wxDataViewEvent& aEvent )
988{
989 hidePreview();
990
991 if( !m_inTimerEvent )
993
994 if( !GetSelectedLibId().IsValid() )
995 toggleExpand( m_tree_ctrl->GetSelection() ); // Expand library/part units subtree
996 else
997 postSelectEvent(); // Open symbol/footprint
998}
999
1000
1001void LIB_TREE::onDetailsLink( wxHtmlLinkEvent& aEvent )
1002{
1003 const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
1004 wxString docname = info.GetHref();
1005 PROJECT& prj = Pgm().GetSettingsManager().Prj();
1006
1007 GetAssociatedDocument( this, docname, &prj );
1008}
1009
1010
1011void LIB_TREE::onPreselect( wxCommandEvent& aEvent )
1012{
1013 hidePreview();
1014
1015 if( m_details_ctrl )
1016 {
1017 int unit = 0;
1018 LIB_ID id = GetSelectedLibId( &unit );
1019
1020 if( id.IsValid() )
1021 m_details_ctrl->SetPage( m_adapter->GenerateInfo( id, unit ) );
1022 else
1023 m_details_ctrl->SetPage( wxEmptyString );
1024 }
1025
1026 aEvent.Skip();
1027}
1028
1029
1030void LIB_TREE::onItemContextMenu( wxDataViewEvent& aEvent )
1031{
1032 hidePreview();
1033
1035 {
1036 m_skipNextRightClick = false;
1037 return;
1038 }
1039
1040 m_previewDisabled = true;
1041
1042 // Select the item under the cursor before showing the context menu. On some platforms
1043 // (notably macOS), right-clicking does not automatically change the selection.
1044 wxDataViewItem item = aEvent.GetItem();
1045
1046 if( item.IsOk() )
1047 {
1048 m_tree_ctrl->SetFocus();
1049
1050 if( !m_tree_ctrl->IsSelected( item ) )
1051 {
1052 m_tree_ctrl->UnselectAll();
1053 m_tree_ctrl->Select( item );
1054 wxSafeYield();
1055 }
1056 }
1057
1058 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
1059 {
1060 tool->Activate();
1061 tool->GetManager()->VetoContextMenuMouseWarp();
1062 tool->GetToolMenu().ShowContextMenu();
1063
1065 tool->GetManager()->DispatchContextMenu( evt );
1066 }
1067 else
1068 {
1069 LIB_TREE_NODE* current = GetCurrentTreeNode();
1070
1071 if( current && current->m_Type == LIB_TREE_NODE::TYPE::LIBRARY && m_adapter->CanPinLibraries() )
1072 {
1073 ACTION_MENU menu( true, nullptr );
1074
1075 if( current->m_Pinned )
1076 {
1077 menu.Add( ACTIONS::unpinLibrary );
1078
1079 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
1080 m_adapter->UnpinLibrary( current );
1081 }
1082 else
1083 {
1084 menu.Add( ACTIONS::pinLibrary );
1085
1086 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
1087 m_adapter->PinLibrary( current );
1088 }
1089 }
1090 }
1091
1092 m_previewDisabled = false;
1093}
1094
1095
1096void LIB_TREE::onHeaderContextMenu( wxDataViewEvent& aEvent )
1097{
1098 hidePreview();
1099 m_previewDisabled = true;
1100
1101 ACTION_MENU menu( true, nullptr );
1102
1104
1105 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
1106 {
1107 EDA_REORDERABLE_LIST_DIALOG dlg( m_parent, _( "Select Columns" ),
1108 m_adapter->GetAvailableColumns(),
1109 m_adapter->GetShownColumns() );
1110
1111 if( dlg.ShowModal() == wxID_OK )
1112 {
1113 m_adapter->SetShownColumns( dlg.EnabledList() );
1114 Regenerate( true );
1115 }
1116 }
1117
1118 m_previewDisabled = false;
1119}
1120
1121
1122wxDEFINE_EVENT( EVT_LIBITEM_SELECTED, wxCommandEvent );
1123wxDEFINE_EVENT( EVT_LIBITEM_CHOSEN, wxCommandEvent );
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
static TOOL_ACTION pinLibrary
Definition actions.h:159
static TOOL_ACTION selectLibTreeColumns
Definition actions.h:278
static TOOL_ACTION unpinLibrary
Definition actions.h:160
static TOOL_ACTION expandAll
Definition actions.h:87
static TOOL_ACTION collapseAll
Definition actions.h:88
Define the structure of a menu based on ACTIONs.
Definition action_menu.h:43
wxMenuItem * Add(const wxString &aLabel, int aId, BITMAPS aIcon)
Add a wxWidgets-style entry to the menu.
A bitmap button widget that behaves like an AUI toolbar item's button when it is drawn.
int ShowModal() override
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:31
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
LIB_TREE_NODE * m_Parent
wxRect m_previewItemRect
Definition lib_tree.h:286
void onHoverPreview()
Show the hovered item's preview once the mouse stops moving.
Definition lib_tree.cpp:883
void RefreshLibTree()
Refresh the tree (mainly to update highlighting and asterisking)
Definition lib_tree.cpp:492
bool isShown(const wxDataViewItem &aTreeId) const
True when aTreeId has a row the filter shows.
Definition lib_tree.cpp:498
wxPoint m_hoverPos
Definition lib_tree.h:275
LIB_TREE_NODE * GetCurrentTreeNode() const
Retrieve the tree node for the first selected item.
Definition lib_tree.cpp:346
void onQueryMouseMoved(wxMouseEvent &aEvent)
Definition lib_tree.cpp:775
wxDataViewItem m_previewItem
Definition lib_tree.h:285
WX_DEBOUNCED_ACTION m_queryDebounce
Debounces the query typed in the search control.
Definition lib_tree.h:280
HTML_WINDOW * m_details_ctrl
Definition lib_tree.h:266
void onTreeActivate(wxDataViewEvent &aEvent)
Definition lib_tree.cpp:987
wxString m_recentSearchesKey
Definition lib_tree.h:269
LIB_TREE(wxWindow *aParent, const wxString &aRecentSearchesKey, wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > &aAdapter, int aFlags=ALL_WIDGETS, HTML_WINDOW *aDetails=nullptr)
Construct a symbol tree.
Definition lib_tree.cpp:50
void onQueryCharHook(wxKeyEvent &aEvent)
Definition lib_tree.cpp:674
void CenterLibId(const LIB_ID &aLibId)
Ensure that an item is visible (preferably centered).
Definition lib_tree.cpp:382
void postSelectEvent()
Post EVT_LIBITEM_CHOSEN event to notify the selection handler that a part has been selected.
Definition lib_tree.cpp:607
wxWindow * GetFocusTarget()
Definition lib_tree.cpp:505
void ShutdownPreviews()
Definition lib_tree.cpp:282
void onQueryDebounce()
Regenerate the tree from the search text once typing pauses.
Definition lib_tree.cpp:666
void onItemContextMenu(wxDataViewEvent &aEvent)
void ShowChangedLanguage()
Definition lib_tree.cpp:299
WX_DATAVIEWCTRL * m_tree_ctrl
Definition lib_tree.h:265
void onQueryText(wxCommandEvent &aEvent)
Definition lib_tree.cpp:656
void toggleExpand(const wxDataViewItem &aTreeId)
Expand or collapse a node, switching it to the opposite state.
Definition lib_tree.cpp:521
int GetSelectedTreeNodes(std::vector< LIB_TREE_NODE * > &aSelection) const
Retrieve a list of pointers to selected tree nodes for trees that allow multi-selection.
Definition lib_tree.cpp:356
bool m_skipNextRightClick
Definition lib_tree.h:273
bool m_inTimerEvent
Definition lib_tree.h:267
void selectIfValid(const wxDataViewItem &aTreeId)
If a wxDataViewitem is valid, select it and post a selection event.
Definition lib_tree.cpp:533
void onIdle(wxIdleEvent &aEvent)
Definition lib_tree.cpp:828
void FocusSearchFieldIfExists()
Focus the search widget if it exists.
Definition lib_tree.cpp:514
std::vector< LIB_ID > GetExpandedLibraries() const
Definition lib_tree.cpp:402
void expandIfValid(const wxDataViewItem &aTreeId)
Definition lib_tree.cpp:593
void onPreselect(wxCommandEvent &aEvent)
wxPopupWindow * m_previewWindow
Definition lib_tree.h:287
void SelectLibId(const LIB_ID &aLibId)
Select an item in the tree widget.
Definition lib_tree.cpp:370
void updateRecentSearchMenu()
Definition lib_tree.cpp:445
void hidePreview()
Definition lib_tree.cpp:819
void showPreview(wxDataViewItem aItem)
Definition lib_tree.cpp:795
wxString GetSearchString() const
Definition lib_tree.cpp:439
void onTreeSelect(wxDataViewEvent &aEvent)
Definition lib_tree.cpp:975
int GetSelectedLibIds(std::vector< LIB_ID > &aSelection, std::vector< int > *aUnit=nullptr) const
Retrieve a list of selections for trees that allow multi-selection.
Definition lib_tree.cpp:329
void postPreselectEvent()
Post a wxEVT_DATAVIEW_SELECTION_CHANGED to notify the selection handler that a new part has been pres...
Definition lib_tree.cpp:600
void Unselect()
Unselect currently selected item in wxDataViewCtrl.
Definition lib_tree.cpp:388
BITMAP_BUTTON * m_sort_ctrl
Definition lib_tree.h:264
void onTreeCharHook(wxKeyEvent &aEvent)
Definition lib_tree.cpp:908
void onDetailsLink(wxHtmlLinkEvent &aEvent)
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:309
void ExpandAll()
Definition lib_tree.cpp:421
wxBoxSizer * m_filtersSizer
Definition lib_tree.h:271
wxDataViewItem m_hoverItem
Definition lib_tree.h:276
~LIB_TREE() override
Definition lib_tree.cpp:252
void SetSearchString(const wxString &aSearchString)
Save/restore search string.
Definition lib_tree.cpp:433
void setState(const STATE &aState)
Restore the symbol tree widget state from an object.
Definition lib_tree.cpp:637
STATE getState() const
Return the symbol tree widget state.
Definition lib_tree.cpp:614
bool m_previewDisabled
Definition lib_tree.h:288
void ExpandLibId(const LIB_ID &aLibId)
Expand and item i the tree widget.
Definition lib_tree.cpp:396
wxSearchCtrl * m_query_ctrl
Definition lib_tree.h:263
void CollapseAll()
Definition lib_tree.cpp:427
void centerIfValid(const wxDataViewItem &aTreeId)
Definition lib_tree.cpp:545
wxRect m_hoverItemRect
Definition lib_tree.h:277
WX_DEBOUNCED_ACTION m_hoverDebounce
Debounces the item preview shown on hover.
Definition lib_tree.h:283
void onHeaderContextMenu(wxDataViewEvent &aEvent)
void Regenerate(bool aKeepState)
Regenerate the tree.
Definition lib_tree.cpp:474
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > m_adapter
Definition lib_tree.h:261
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:124
Container for project specific data.
Definition project.h:63
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
Generic, UI-independent tool event.
Definition tool_event.h:167
Extension of the wxDataViewCtrl to include some helper functions for working with items.
static constexpr int TEXT_INPUT_DEBOUNCE_MS
A typical debounce period for text input events, in milliseconds.
static constexpr int HOVER_PREVIEW_DEBOUNCE_MS
A typical debounce period for hover preview events, in milliseconds.
#define _(s)
bool GetAssociatedDocument(wxWindow *aParent, const wxString &aDocName, PROJECT *aProject, SEARCH_STACK *aPaths, std::vector< EMBEDDED_FILES * > aFilesStack)
Open a document (file) with the suitable browser.
Definition eda_doc.cpp:59
This file is part of the common library.
static const S_FILTER FILTERS[]
Definition image.cpp:330
constexpr int RECENT_SEARCHES_MAX
Definition lib_tree.cpp:45
#define PREVIEW_SIZE
Definition lib_tree.cpp:792
std::map< wxString, std::vector< wxString > > g_recentSearches
Definition lib_tree.cpp:47
wxDEFINE_EVENT(EVT_LIBITEM_SELECTED, wxCommandEvent)
This file contains miscellaneous commonly used macros and functions.
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition kicad_algo.h:96
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
Structure storing state of the symbol tree widget.
Definition lib_tree.h:211
LIB_ID selection
Current selection, might be not valid if nothing was selected.
Definition lib_tree.h:216
VECTOR2I scrollpos
Definition lib_tree.h:218
std::vector< wxDataViewItem > expanded
List of expanded nodes.
Definition lib_tree.h:213
@ TA_MOUSE_CLICK
Definition tool_event.h:63
@ MD_META
Definition tool_event.h:143
@ MD_ALT
Definition tool_event.h:141
@ MD_CTRL
Definition tool_event.h:140
@ MD_SUPER
Definition tool_event.h:142
@ MD_ALTGR
Definition tool_event.h:144
@ MD_SHIFT
Definition tool_event.h:139
@ TC_MOUSE
Definition tool_event.h:51
@ BUT_RIGHT
Definition tool_event.h:129
void EnsureVisibleIfEnabled(wxDataViewCtrl *aWidget, const wxDataViewItem &aItem)
Scroll aItem into view only when aWidget is currently enabled.