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( item.IsOk() )
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->GetValue();
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
499{
500 if( m_query_ctrl )
501 return m_query_ctrl;
502 else
503 return m_tree_ctrl;
504}
505
506
508{
509 if( m_query_ctrl )
510 m_query_ctrl->SetFocus();
511}
512
513
514void LIB_TREE::toggleExpand( const wxDataViewItem& aTreeId )
515{
516 if( !aTreeId.IsOk() )
517 return;
518
519 if( m_tree_ctrl->IsExpanded( aTreeId ) )
520 m_tree_ctrl->Collapse( aTreeId );
521 else
522 m_tree_ctrl->Expand( aTreeId );
523}
524
525
526void LIB_TREE::selectIfValid( const wxDataViewItem& aTreeId )
527{
528 if( aTreeId.IsOk() )
529 {
531 m_tree_ctrl->UnselectAll();
532 m_tree_ctrl->Select( aTreeId );
534 }
535}
536
537
538void LIB_TREE::centerIfValid( const wxDataViewItem& aTreeId )
539{
540 /*
541 * This doesn't actually center because the wxWidgets API is poorly suited to that (and
542 * it might be too noisy as well).
543 *
544 * It does try to keep the given item a bit off the top or bottom of the window.
545 */
546
547 if( aTreeId.IsOk() )
548 {
549 LIB_TREE_NODE* node = m_adapter->GetTreeNodeFor( aTreeId );
550 LIB_TREE_NODE* parent = node->m_Parent;
551 LIB_TREE_NODE* grandParent = parent ? parent->m_Parent : nullptr;
552
553 if( parent )
554 {
555 wxDataViewItemArray siblings;
556 m_adapter->GetChildren( wxDataViewItem( parent ), siblings );
557
558 int idx = siblings.Index( aTreeId );
559
560 if( idx + 5 < (int) siblings.GetCount() )
561 {
562 EnsureVisibleIfEnabled( m_tree_ctrl, siblings.Item( idx + 5 ) );
563 }
564 else if( grandParent )
565 {
566 wxDataViewItemArray parentsSiblings;
567 m_adapter->GetChildren( wxDataViewItem( grandParent ), parentsSiblings );
568
569 int p_idx = parentsSiblings.Index( wxDataViewItem( parent ) );
570
571 if( p_idx + 1 < (int) parentsSiblings.GetCount() )
572 EnsureVisibleIfEnabled( m_tree_ctrl, parentsSiblings.Item( p_idx + 1 ) );
573 }
574
575 if( idx - 5 >= 0 )
576 EnsureVisibleIfEnabled( m_tree_ctrl, siblings.Item( idx - 5 ) );
577 else
578 EnsureVisibleIfEnabled( m_tree_ctrl, wxDataViewItem( parent ) );
579 }
580
582 }
583}
584
585
586void LIB_TREE::expandIfValid( const wxDataViewItem& aTreeId )
587{
588 if( aTreeId.IsOk() && !m_tree_ctrl->IsExpanded( aTreeId ) )
589 m_tree_ctrl->Expand( aTreeId );
590}
591
592
594{
595 wxCommandEvent event( EVT_LIBITEM_SELECTED );
596 wxPostEvent( this, event );
597}
598
599
601{
602 wxCommandEvent event( EVT_LIBITEM_CHOSEN );
603 wxPostEvent( this, event );
604}
605
606
608{
609 STATE state;
610 wxDataViewItemArray items;
611 m_adapter->GetChildren( wxDataViewItem( nullptr ), items );
612
613 for( const wxDataViewItem& item : items )
614 {
615 if( m_tree_ctrl->IsExpanded( item ) )
616 state.expanded.push_back( item );
617 }
618
619 state.selection = GetSelectedLibId();
620
621 state.scrollpos = {
622 m_tree_ctrl->HasScrollbar( wxHORIZONTAL ) ? m_tree_ctrl->GetScrollPos( wxHORIZONTAL ) : 0,
623 m_tree_ctrl->HasScrollbar( wxVERTICAL ) ? m_tree_ctrl->GetScrollPos( wxVERTICAL ) : 0
624 };
625
626 return state;
627}
628
629
630void LIB_TREE::setState( const STATE& aState )
631{
632 m_tree_ctrl->Freeze();
633
634 for( const wxDataViewItem& item : aState.expanded )
635 m_tree_ctrl->Expand( item );
636
637 // TODO(JE) probably remove this; it fights with centerIfValid
638 // m_tree_ctrl->SetScrollPos( wxHORIZONTAL, aState.scrollpos.x );
639 // m_tree_ctrl->SetScrollPos( wxVERTICAL, aState.scrollpos.y );
640
641 // wxDataViewCtrl cannot be frozen when a selection
642 // command is issued, otherwise it selects a random item (Windows)
643 m_tree_ctrl->Thaw();
644
645 if( !aState.selection.GetLibItemName().empty() || !aState.selection.GetLibNickname().empty() )
646 SelectLibId( aState.selection );
647}
648
649
650void LIB_TREE::onQueryText( wxCommandEvent& aEvent )
651{
652 m_queryDebounce.Restart();
653
654 // Required to avoid interaction with SetHint()
655 // See documentation for wxTextEntry::SetHint
656 aEvent.Skip();
657}
658
659
661{
662 m_inTimerEvent = true;
663 Regenerate( false );
664 m_inTimerEvent = false;
665}
666
667
668void LIB_TREE::onQueryCharHook( wxKeyEvent& aKeyStroke )
669{
670 int hotkey = aKeyStroke.GetKeyCode();
671
672 int mods = aKeyStroke.GetModifiers();
673
674 // the flag wxMOD_ALTGR is defined in wxWidgets as wxMOD_CONTROL|wxMOD_ALT
675 // So AltGr key cannot used as modifier key because it is the same as Alt key + Ctrl key.
676#if CAN_USE_ALTGR_KEY
677 if( wxmods & wxMOD_ALTGR )
678 mods |= MD_ALTGR;
679 else
680#endif
681 {
682 if( mods & wxMOD_CONTROL )
683 hotkey += MD_CTRL;
684
685 if( mods & wxMOD_ALT )
686 hotkey += MD_ALT;
687 }
688
689 if( mods & wxMOD_SHIFT )
690 hotkey += MD_SHIFT;
691
692#ifdef wxMOD_META
693 if( mods & wxMOD_META )
694 hotkey += MD_META;
695#endif
696
697#ifdef wxMOD_WIN
698 if( mods & wxMOD_WIN )
699 hotkey += MD_SUPER;
700#endif
701
702 if( hotkey == ACTIONS::expandAll.GetHotKey()
703 || hotkey == ACTIONS::expandAll.GetHotKeyAlt() )
704 {
705 m_tree_ctrl->ExpandAll();
706 return;
707 }
708 else if( hotkey == ACTIONS::collapseAll.GetHotKey()
709 || hotkey == ACTIONS::collapseAll.GetHotKeyAlt() )
710 {
711 m_tree_ctrl->CollapseAll();
712 return;
713 }
714
715 wxDataViewItem sel = m_tree_ctrl->GetSelection();
716
717 if( !sel.IsOk() )
718 sel = m_adapter->GetCurrentDataViewItem();
719
720 LIB_TREE_NODE::TYPE type = sel.IsOk() ? m_adapter->GetTypeFor( sel )
721 : LIB_TREE_NODE::TYPE::INVALID;
722
723 switch( aKeyStroke.GetKeyCode() )
724 {
725 case WXK_UP:
727 selectIfValid( m_tree_ctrl->GetPrevItem( sel ) );
728 break;
729
730 case WXK_DOWN:
732 selectIfValid( m_tree_ctrl->GetNextItem( sel ) );
733 break;
734
735 case WXK_ADD:
737
738 if( type == LIB_TREE_NODE::TYPE::LIBRARY )
739 m_tree_ctrl->Expand( sel );
740
741 break;
742
743 case WXK_SUBTRACT:
745
746 if( type == LIB_TREE_NODE::TYPE::LIBRARY )
747 m_tree_ctrl->Collapse( sel );
748
749 break;
750
751 case WXK_RETURN:
752 case WXK_NUMPAD_ENTER:
754
755 if( GetSelectedLibId().IsValid() )
757 else if( type == LIB_TREE_NODE::TYPE::LIBRARY )
758 toggleExpand( sel );
759
760 break;
761
762 default:
763 aKeyStroke.Skip(); // Any other key: pass on to search box directly.
764 break;
765 }
766}
767
768
769void LIB_TREE::onQueryMouseMoved( wxMouseEvent& aEvent )
770{
771#if defined( __WXOSX__ ) // Doesn't work properly on other ports
772 wxPoint pos = aEvent.GetPosition();
773 wxRect ctrlRect = m_query_ctrl->GetScreenRect();
774 int buttonWidth = ctrlRect.GetHeight(); // Presume buttons are square
775
776 if( m_query_ctrl->IsSearchButtonVisible() && pos.x < buttonWidth )
777 SetCursor( wxCURSOR_ARROW );
778 else if( m_query_ctrl->IsCancelButtonVisible() && pos.x > ctrlRect.GetWidth() - buttonWidth )
779 SetCursor( wxCURSOR_ARROW );
780 else
781 SetCursor( wxCURSOR_IBEAM );
782#endif
783}
784
785
786#define PREVIEW_SIZE wxSize( 240, 200 )
787
788
789void LIB_TREE::showPreview( wxDataViewItem aItem )
790{
791 if( aItem.IsOk() && m_adapter->HasPreview( aItem ) )
792 {
793 m_previewItem = aItem;
795
796 wxWindow* topLevelParent = wxGetTopLevelParent( m_parent );
797
798 if( !m_previewWindow )
799 m_previewWindow = new wxPopupWindow( topLevelParent );
800
801 m_previewWindow->SetSize( PREVIEW_SIZE );
802
803 m_adapter->ShowPreview( m_previewWindow, aItem );
804
805 m_previewWindow->SetPosition( wxPoint( m_tree_ctrl->GetScreenRect().GetRight() - 10,
806 wxGetMousePosition().y - PREVIEW_SIZE.y / 2 ) );
807
808 m_previewWindow->Show();
809 }
810}
811
812
814{
815 m_previewItem = wxDataViewItem();
816
817 if( m_previewWindow )
818 m_previewWindow->Hide();
819}
820
821
822void LIB_TREE::onIdle( wxIdleEvent& aEvent )
823{
824 // The wxDataViewCtrl won't give us its mouseMoved events so we're forced to use idle
825 // events to track the hover state
826
827 // The dang thing won't give us scroll events either, so we implement a poor-man's
828 // scroll-checker using the last-known positions of the preview or hover items.
829
830 wxWindow* topLevelParent = wxGetTopLevelParent( m_parent );
831 wxWindow* topLevelFocus = wxGetTopLevelParent( wxWindow::FindFocus() );
832
833 bool mouseOverWindow = false;
834 wxPoint screenPos = wxGetMousePosition();
835
836 if( m_tree_ctrl->IsShownOnScreen() )
837 mouseOverWindow |= m_tree_ctrl->GetScreenRect().Contains( screenPos );
838
839 if( m_previewDisabled || topLevelFocus != topLevelParent || !mouseOverWindow )
840 {
841 m_hoverDebounce.Cancel();
842 hidePreview();
843 return;
844 }
845
846 wxPoint clientPos = m_tree_ctrl->ScreenToClient( screenPos );
847 wxDataViewItem item;
848 wxDataViewColumn* col = nullptr;
849
850 m_tree_ctrl->HitTest( clientPos, item, col );
851
852 if( m_previewItem.IsOk() )
853 {
854 if( item != m_previewItem )
855 {
856#ifdef __WXGTK__
857 // Hide the preview, because Wayland can't move windows.
858 if( wxGetDisplayInfo().type == wxDisplayType::wxDisplayWayland )
859 hidePreview();
860#endif
861 showPreview( item );
862 }
863
864 return;
865 }
866
867 if( m_hoverPos != clientPos )
868 {
869 m_hoverPos = clientPos;
870 m_hoverItem = item;
871 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
872 m_hoverDebounce.Restart();
873 }
874}
875
876
878{
879 hidePreview();
880
881 if( !m_tree_ctrl->IsShownOnScreen() || m_previewDisabled )
882 return;
883
884 wxDataViewItem item;
885 wxDataViewColumn* col = nullptr;
886 m_tree_ctrl->HitTest( m_hoverPos, item, col );
887
888 if( item == m_hoverItem && m_tree_ctrl->GetItemRect( item ) == m_hoverItemRect )
889 {
890 if( item != m_tree_ctrl->GetSelection() )
891 showPreview( item );
892 }
893 else // view must have been scrolled
894 {
895 m_hoverItem = item;
896 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
897 m_hoverDebounce.Restart();
898 }
899}
900
901
902void LIB_TREE::onTreeCharHook( wxKeyEvent& aKeyStroke )
903{
904 // Bare Left/Right collapse/expand the focused node. Consume them here so they
905 // do not fall through to the global cursorLeft/cursorRight canvas actions. This
906 // is the tree-only hook, so the search box keeps normal caret movement.
907 if( aKeyStroke.GetModifiers() == wxMOD_NONE
908 && ( aKeyStroke.GetKeyCode() == WXK_LEFT || aKeyStroke.GetKeyCode() == WXK_RIGHT ) )
909 {
910 wxDataViewItem sel = m_tree_ctrl->GetSelection();
911
912 if( !sel.IsOk() )
913 sel = m_adapter->GetCurrentDataViewItem();
914
915 if( sel.IsOk() )
916 {
917 if( aKeyStroke.GetKeyCode() == WXK_RIGHT )
918 m_tree_ctrl->Expand( sel );
919 else
920 m_tree_ctrl->Collapse( sel );
921 }
922
923 return;
924 }
925
926 onQueryCharHook( aKeyStroke );
927
928 if( aKeyStroke.GetSkipped() )
929 {
930 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
931 {
932 int hotkey = aKeyStroke.GetKeyCode();
933
934 int mods = aKeyStroke.GetModifiers();
935
936 if( mods & wxMOD_ALTGR )
937 hotkey |= MD_ALTGR;
938 else
939 {
940 if( mods & wxMOD_ALT )
941 hotkey |= MD_ALT;
942
943 if( mods & wxMOD_CONTROL )
944 hotkey |= MD_CTRL;
945 }
946
947 if( mods & wxMOD_SHIFT )
948 hotkey |= MD_SHIFT;
949
950#ifdef wxMOD_META
951 if( mods & wxMOD_META )
952 hotkey |= MD_META;
953#endif
954
955#ifdef wxMOD_WIN
956 if( mods & wxMOD_WIN )
957 hotkey |= MD_SUPER;
958#endif
959
960 if( tool->GetManager()->GetActionManager()->RunHotKey( hotkey ) )
961 aKeyStroke.Skip( false );
962 }
963 }
964}
965
966
967void LIB_TREE::onTreeSelect( wxDataViewEvent& aEvent )
968{
969 if( m_tree_ctrl->IsFrozen() )
970 return;
971
972 if( !m_inTimerEvent )
974
976}
977
978
979void LIB_TREE::onTreeActivate( wxDataViewEvent& aEvent )
980{
981 hidePreview();
982
983 if( !m_inTimerEvent )
985
986 if( !GetSelectedLibId().IsValid() )
987 toggleExpand( m_tree_ctrl->GetSelection() ); // Expand library/part units subtree
988 else
989 postSelectEvent(); // Open symbol/footprint
990}
991
992
993void LIB_TREE::onDetailsLink( wxHtmlLinkEvent& aEvent )
994{
995 const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
996 wxString docname = info.GetHref();
997 PROJECT& prj = Pgm().GetSettingsManager().Prj();
998
999 GetAssociatedDocument( this, docname, &prj );
1000}
1001
1002
1003void LIB_TREE::onPreselect( wxCommandEvent& aEvent )
1004{
1005 hidePreview();
1006
1007 if( m_details_ctrl )
1008 {
1009 int unit = 0;
1010 LIB_ID id = GetSelectedLibId( &unit );
1011
1012 if( id.IsValid() )
1013 m_details_ctrl->SetPage( m_adapter->GenerateInfo( id, unit ) );
1014 else
1015 m_details_ctrl->SetPage( wxEmptyString );
1016 }
1017
1018 aEvent.Skip();
1019}
1020
1021
1022void LIB_TREE::onItemContextMenu( wxDataViewEvent& aEvent )
1023{
1024 hidePreview();
1025
1027 {
1028 m_skipNextRightClick = false;
1029 return;
1030 }
1031
1032 m_previewDisabled = true;
1033
1034 // Select the item under the cursor before showing the context menu. On some platforms
1035 // (notably macOS), right-clicking does not automatically change the selection.
1036 wxDataViewItem item = aEvent.GetItem();
1037
1038 if( item.IsOk() )
1039 {
1040 m_tree_ctrl->SetFocus();
1041
1042 if( !m_tree_ctrl->IsSelected( item ) )
1043 {
1044 m_tree_ctrl->UnselectAll();
1045 m_tree_ctrl->Select( item );
1046 wxSafeYield();
1047 }
1048 }
1049
1050 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
1051 {
1052 tool->Activate();
1053 tool->GetManager()->VetoContextMenuMouseWarp();
1054 tool->GetToolMenu().ShowContextMenu();
1055
1057 tool->GetManager()->DispatchContextMenu( evt );
1058 }
1059 else
1060 {
1061 LIB_TREE_NODE* current = GetCurrentTreeNode();
1062
1063 if( current && current->m_Type == LIB_TREE_NODE::TYPE::LIBRARY )
1064 {
1065 ACTION_MENU menu( true, nullptr );
1066
1067 if( current->m_Pinned )
1068 {
1069 menu.Add( ACTIONS::unpinLibrary );
1070
1071 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
1072 m_adapter->UnpinLibrary( current );
1073 }
1074 else
1075 {
1076 menu.Add( ACTIONS::pinLibrary );
1077
1078 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
1079 m_adapter->PinLibrary( current );
1080 }
1081 }
1082 }
1083
1084 m_previewDisabled = false;
1085}
1086
1087
1088void LIB_TREE::onHeaderContextMenu( wxDataViewEvent& aEvent )
1089{
1090 hidePreview();
1091 m_previewDisabled = true;
1092
1093 ACTION_MENU menu( true, nullptr );
1094
1096
1097 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
1098 {
1099 EDA_REORDERABLE_LIST_DIALOG dlg( m_parent, _( "Select Columns" ),
1100 m_adapter->GetAvailableColumns(),
1101 m_adapter->GetShownColumns() );
1102
1103 if( dlg.ShowModal() == wxID_OK )
1104 {
1105 m_adapter->SetShownColumns( dlg.EnabledList() );
1106 Regenerate( true );
1107 }
1108 }
1109
1110 m_previewDisabled = false;
1111}
1112
1113
1114wxDEFINE_EVENT( EVT_LIBITEM_SELECTED, wxCommandEvent );
1115wxDEFINE_EVENT( EVT_LIBITEM_CHOSEN, wxCommandEvent );
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
static TOOL_ACTION pinLibrary
Definition actions.h:158
static TOOL_ACTION selectLibTreeColumns
Definition actions.h:276
static TOOL_ACTION unpinLibrary
Definition actions.h:159
static TOOL_ACTION expandAll
Definition actions.h:86
static TOOL_ACTION collapseAll
Definition actions.h:87
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
const UTF8 & GetLibItemName() const
Definition lib_id.h:98
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition lib_id.h:83
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
LIB_TREE_NODE * m_Parent
wxRect m_previewItemRect
Definition lib_tree.h:283
void onHoverPreview()
Show the hovered item's preview once the mouse stops moving.
Definition lib_tree.cpp:877
void RefreshLibTree()
Refresh the tree (mainly to update highlighting and asterisking)
Definition lib_tree.cpp:492
wxPoint m_hoverPos
Definition lib_tree.h:272
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:769
wxDataViewItem m_previewItem
Definition lib_tree.h:282
WX_DEBOUNCED_ACTION m_queryDebounce
Debounces the query typed in the search control.
Definition lib_tree.h:277
HTML_WINDOW * m_details_ctrl
Definition lib_tree.h:263
void onTreeActivate(wxDataViewEvent &aEvent)
Definition lib_tree.cpp:979
wxString m_recentSearchesKey
Definition lib_tree.h:266
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:668
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:600
wxWindow * GetFocusTarget()
Definition lib_tree.cpp:498
void ShutdownPreviews()
Definition lib_tree.cpp:282
void onQueryDebounce()
Regenerate the tree from the search text once typing pauses.
Definition lib_tree.cpp:660
void onItemContextMenu(wxDataViewEvent &aEvent)
void ShowChangedLanguage()
Definition lib_tree.cpp:299
WX_DATAVIEWCTRL * m_tree_ctrl
Definition lib_tree.h:262
void onQueryText(wxCommandEvent &aEvent)
Definition lib_tree.cpp:650
void toggleExpand(const wxDataViewItem &aTreeId)
Expand or collapse a node, switching it to the opposite state.
Definition lib_tree.cpp:514
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:270
bool m_inTimerEvent
Definition lib_tree.h:264
void selectIfValid(const wxDataViewItem &aTreeId)
If a wxDataViewitem is valid, select it and post a selection event.
Definition lib_tree.cpp:526
void onIdle(wxIdleEvent &aEvent)
Definition lib_tree.cpp:822
void FocusSearchFieldIfExists()
Focus the search widget if it exists.
Definition lib_tree.cpp:507
std::vector< LIB_ID > GetExpandedLibraries() const
Definition lib_tree.cpp:402
void expandIfValid(const wxDataViewItem &aTreeId)
Definition lib_tree.cpp:586
void onPreselect(wxCommandEvent &aEvent)
wxPopupWindow * m_previewWindow
Definition lib_tree.h:284
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:813
void showPreview(wxDataViewItem aItem)
Definition lib_tree.cpp:789
wxString GetSearchString() const
Definition lib_tree.cpp:439
void onTreeSelect(wxDataViewEvent &aEvent)
Definition lib_tree.cpp:967
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:593
void Unselect()
Unselect currently selected item in wxDataViewCtrl.
Definition lib_tree.cpp:388
BITMAP_BUTTON * m_sort_ctrl
Definition lib_tree.h:261
void onTreeCharHook(wxKeyEvent &aEvent)
Definition lib_tree.cpp:902
void onDetailsLink(wxHtmlLinkEvent &aEvent)
Definition lib_tree.cpp:993
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:268
wxDataViewItem m_hoverItem
Definition lib_tree.h:273
~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:630
STATE getState() const
Return the symbol tree widget state.
Definition lib_tree.cpp:607
bool m_previewDisabled
Definition lib_tree.h:285
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:260
void CollapseAll()
Definition lib_tree.cpp:427
void centerIfValid(const wxDataViewItem &aTreeId)
Definition lib_tree.cpp:538
wxRect m_hoverItemRect
Definition lib_tree.h:274
WX_DEBOUNCED_ACTION m_hoverDebounce
Debounces the item preview shown on hover.
Definition lib_tree.h:280
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:258
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:123
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
bool empty() const
Definition utf8.h:105
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:786
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.