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-2024 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>
37#include <wx/settings.h>
38#include <wx/sizer.h>
39#include <wx/srchctrl.h>
40#include <wx/popupwin.h>
41
42#include <eda_doc.h> // for GetAssociatedDocument()
43#include <pgm_base.h> // for PROJECT
44#include <settings/settings_manager.h> // for PROJECT
45
46constexpr int RECENT_SEARCHES_MAX = 10;
47
48std::map<wxString, std::vector<wxString>> g_recentSearches;
49
50
51LIB_TREE::LIB_TREE( wxWindow* aParent, const wxString& aRecentSearchesKey, LIB_TABLE* aLibTable,
52 wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>& aAdapter, int aFlags,
53 HTML_WINDOW* aDetails ) :
54 wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
55 wxWANTS_CHARS | wxTAB_TRAVERSAL | wxNO_BORDER ),
56 m_adapter( aAdapter ),
57 m_query_ctrl( nullptr ),
58 m_sort_ctrl( nullptr ),
59 m_details_ctrl( nullptr ),
60 m_inTimerEvent( false ),
61 m_recentSearchesKey( aRecentSearchesKey ),
62 m_filtersSizer( nullptr ),
63 m_skipNextRightClick( false ),
64 m_previewWindow( nullptr ),
65 m_previewDisabled( false )
66{
67 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
68
69 m_hoverTimer.SetOwner( this );
70 Bind( wxEVT_TIMER, &LIB_TREE::onHoverTimer, this, m_hoverTimer.GetId() );
71
72 // Search text control
73 if( aFlags & SEARCH )
74 {
75 wxBoxSizer* search_sizer = new wxBoxSizer( wxHORIZONTAL );
76
77 m_query_ctrl = new wxSearchCtrl( this, wxID_ANY );
78
79 m_query_ctrl->ShowCancelButton( true );
80
81#ifdef __WXGTK__
82 // wxSearchCtrl vertical height is not calculated correctly on some GTK setups
83 // See https://gitlab.com/kicad/code/kicad/-/issues/9019
84 m_query_ctrl->SetMinSize( wxSize( -1, GetTextExtent( wxT( "qb" ) ).y + 10 ) );
85#endif
86
87 m_debounceTimer = new wxTimer( this );
88
89 search_sizer->Add( m_query_ctrl, 1, wxEXPAND | wxRIGHT, 5 );
90
91 m_sort_ctrl = new STD_BITMAP_BUTTON( this, wxID_ANY, wxNullBitmap, wxDefaultPosition,
92 wxDefaultSize, wxBU_AUTODRAW|0 );
93 m_sort_ctrl->SetBitmap( KiBitmapBundle( BITMAPS::small_sort_desc ) );
94 m_sort_ctrl->Bind( wxEVT_LEFT_DOWN,
95 [&]( wxMouseEvent& aEvent )
96 {
97 // Build a pop menu:
98 wxMenu menu;
99
100 menu.Append( 4201, _( "Sort by Best Match" ), wxEmptyString, wxITEM_CHECK );
101 menu.Append( 4202, _( "Sort Alphabetically" ), wxEmptyString, wxITEM_CHECK );
102 menu.AppendSeparator();
103 menu.Append( 4203, ACTIONS::expandAll.GetMenuItem() );
104 menu.Append( 4204, ACTIONS::collapseAll.GetMenuItem() );
105
106 if( m_adapter->GetSortMode() == LIB_TREE_MODEL_ADAPTER::BEST_MATCH )
107 menu.Check( 4201, true );
108 else
109 menu.Check( 4202, true );
110
111 // menu_id is the selected submenu id from the popup menu or wxID_NONE
112 int menu_id = m_sort_ctrl->GetPopupMenuSelectionFromUser( menu );
113
114 if( menu_id == 0 || menu_id == 4201 )
115 {
116 m_adapter->SetSortMode( LIB_TREE_MODEL_ADAPTER::BEST_MATCH );
117 Regenerate( true );
118 }
119 else if( menu_id == 1 || menu_id == 4202 )
120 {
121 m_adapter->SetSortMode( LIB_TREE_MODEL_ADAPTER::ALPHABETIC );
122 Regenerate( true );
123 }
124 else if( menu_id == 3 || menu_id == 4203 )
125 {
126 ExpandAll();
127 }
128 else if( menu_id == 4 || menu_id == 4204 )
129 {
130 CollapseAll();
131 }
132 } );
133
134 m_sort_ctrl->Bind( wxEVT_CHAR_HOOK, &LIB_TREE::onTreeCharHook, this );
135
136 search_sizer->Add( m_sort_ctrl, 0, wxEXPAND, 5 );
137
138 sizer->Add( search_sizer, 0, wxEXPAND | wxBOTTOM, 5 );
139
140 m_query_ctrl->Bind( wxEVT_TEXT, &LIB_TREE::onQueryText, this );
141
142 m_query_ctrl->Bind( wxEVT_SEARCH_CANCEL, &LIB_TREE::onQueryText, this );
143 m_query_ctrl->Bind( wxEVT_CHAR_HOOK, &LIB_TREE::onQueryCharHook, this );
144 m_query_ctrl->Bind( wxEVT_MOTION, &LIB_TREE::onQueryMouseMoved, this );
145
146#if defined( __WXOSX__ ) || wxCHECK_VERSION( 3, 3, 0 ) // Doesn't work properly on other ports
147 m_query_ctrl->Bind( wxEVT_LEAVE_WINDOW,
148 [this]( wxMouseEvent& aEvt )
149 {
150 SetCursor( wxCURSOR_ARROW );
151 } );
152#endif
153
154 m_query_ctrl->Bind( wxEVT_MENU,
155 [this]( wxCommandEvent& aEvent )
156 {
157 wxString search;
158 size_t idx = aEvent.GetId() - 1;
159
160 if( idx < g_recentSearches[ m_recentSearchesKey ].size() )
162 },
164
165 Bind( wxEVT_TIMER, &LIB_TREE::onDebounceTimer, this, m_debounceTimer->GetId() );
166 }
167
168 if( aFlags & FILTERS )
169 {
170 m_filtersSizer = new wxBoxSizer( wxVERTICAL );
171 sizer->Add( m_filtersSizer, 0, wxEXPAND | wxLEFT, 4 );
172 }
173
174 // Tree control
175 int dvFlags = ( aFlags & MULTISELECT ) ? wxDV_MULTIPLE : wxDV_SINGLE;
176 m_tree_ctrl = new WX_DATAVIEWCTRL( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, dvFlags );
177 m_adapter->AttachTo( m_tree_ctrl );
178
179#ifdef __WXGTK__
180 // The GTK renderer seems to calculate row height incorrectly sometimes; but can be overridden
181 int rowHeight = FromDIP( 8 ) + GetTextExtent( wxS( "pdI" ) ).y;
182 m_tree_ctrl->SetRowHeight( rowHeight );
183#endif
184
185 sizer->Add( m_tree_ctrl, 5, wxEXPAND, 5 );
186
187 // Description panel
188 if( aFlags & DETAILS )
189 {
190 if( !aDetails )
191 {
192 wxPoint html_size = ConvertDialogToPixels( wxPoint( 80, 80 ) );
193
194 m_details_ctrl = new HTML_WINDOW( this, wxID_ANY, wxDefaultPosition,
195 wxSize( html_size.x, html_size.y ) );
196
197 sizer->Add( m_details_ctrl, 2, wxTOP | wxEXPAND, 5 );
198 }
199 else
200 {
201 m_details_ctrl = aDetails;
202 }
203
204 m_details_ctrl->Bind( wxEVT_HTML_LINK_CLICKED, &LIB_TREE::onDetailsLink, this );
205 }
206
207 SetSizer( sizer );
208
209 m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &LIB_TREE::onTreeActivate, this );
210 m_tree_ctrl->Bind( wxEVT_DATAVIEW_SELECTION_CHANGED, &LIB_TREE::onTreeSelect, this );
211 m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, &LIB_TREE::onItemContextMenu, this );
212 m_tree_ctrl->Bind( wxEVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, &LIB_TREE::onHeaderContextMenu,
213 this );
214
215 // wxDataViewCtrl eats its mouseMoved events, so we're forced to use idle events to track
216 // the hover state
217 Bind( wxEVT_IDLE, &LIB_TREE::onIdle, this );
218
219 // Process hotkeys when the tree control has focus:
220 m_tree_ctrl->Bind( wxEVT_CHAR_HOOK, &LIB_TREE::onTreeCharHook, this );
221
222 Bind( EVT_LIBITEM_SELECTED, &LIB_TREE::onPreselect, this );
223
224 if( m_query_ctrl )
225 {
226 m_query_ctrl->SetDescriptiveText( _( "Filter" ) );
227 m_query_ctrl->SetFocus();
228 m_query_ctrl->SetValue( wxEmptyString );
230
231 // Force an update of the adapter with the empty text to ensure preselect is done
232 Regenerate( false );
233 }
234 else
235 {
236 // There may be a part preselected in the model. Make sure it is displayed.
237 // Regenerate does this in the other branch
239 }
240
241 Layout();
242 sizer->Fit( this );
243
244#ifdef __WXGTK__
245 // Scrollbars must be always enabled to prevent an infinite event loop
246 // more details: http://trac.wxwidgets.org/ticket/18141
247 if( m_details_ctrl )
248 m_details_ctrl->ShowScrollbars( wxSHOW_SB_ALWAYS, wxSHOW_SB_ALWAYS );
249#endif /* __WXGTK__ */
250}
251
252
254{
255 Unbind( wxEVT_TIMER, &LIB_TREE::onHoverTimer, this, m_hoverTimer.GetId() );
256
257 m_tree_ctrl->Unbind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &LIB_TREE::onTreeActivate, this );
258 m_tree_ctrl->Unbind( wxEVT_DATAVIEW_SELECTION_CHANGED, &LIB_TREE::onTreeSelect, this );
259 m_tree_ctrl->Unbind( wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, &LIB_TREE::onItemContextMenu, this );
260 m_tree_ctrl->Unbind( wxEVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, &LIB_TREE::onHeaderContextMenu,
261 this );
262
263 Unbind( wxEVT_IDLE, &LIB_TREE::onIdle, this );
264 m_tree_ctrl->Unbind( wxEVT_CHAR_HOOK, &LIB_TREE::onTreeCharHook, this );
265 Unbind( EVT_LIBITEM_SELECTED, &LIB_TREE::onPreselect, this );
266
267 if( m_query_ctrl )
268 {
269 m_query_ctrl->Unbind( wxEVT_TEXT, &LIB_TREE::onQueryText, this );
270
271 m_query_ctrl->Unbind( wxEVT_SEARCH_CANCEL, &LIB_TREE::onQueryText, this );
272 m_query_ctrl->Unbind( wxEVT_CHAR_HOOK, &LIB_TREE::onQueryCharHook, this );
273 m_query_ctrl->Unbind( wxEVT_MOTION, &LIB_TREE::onQueryMouseMoved, this );
274 }
275
276 // Stop the timer during destruction early to avoid potential race conditions (that do happen)]
277 if( m_debounceTimer )
278 {
279 m_debounceTimer->Stop();
280 Unbind( wxEVT_TIMER, &LIB_TREE::onDebounceTimer, this, m_debounceTimer->GetId() );
281 }
282
283 if( m_details_ctrl )
284 m_details_ctrl->Unbind( wxEVT_HTML_LINK_CLICKED, &LIB_TREE::onDetailsLink, this );
285
286 m_hoverTimer.Stop();
288}
289
290
292{
293 if( m_query_ctrl )
294 m_query_ctrl->SetDescriptiveText( _( "Filter" ) );
295
296 if( m_adapter )
297 m_adapter->ShowChangedLanguage();
298}
299
300
302{
303 wxDataViewItem sel = m_tree_ctrl->GetSelection();
304
305 if( !sel )
306 return LIB_ID();
307
308 if( aUnit )
309 *aUnit = m_adapter->GetUnitFor( sel );
310
311 return m_adapter->GetAliasFor( sel );
312}
313
314
315int LIB_TREE::GetSelectedLibIds( std::vector<LIB_ID>& aSelection, std::vector<int>* aUnit ) const
316{
317 wxDataViewItemArray selection;
318 int count = m_tree_ctrl->GetSelections( selection );
319
320 for( const wxDataViewItem& item : selection )
321 {
322 aSelection.emplace_back( m_adapter->GetAliasFor( item ) );
323
324 if( aUnit )
325 aUnit->emplace_back( m_adapter->GetUnitFor( item ) );
326 }
327
328 return count;
329}
330
331
333{
334 wxDataViewItem sel = m_tree_ctrl->GetSelection();
335
336 if( !sel )
337 return nullptr;
338
339 return m_adapter->GetTreeNodeFor( sel );
340}
341
342
343void LIB_TREE::SelectLibId( const LIB_ID& aLibId )
344{
345 selectIfValid( m_adapter->FindItem( aLibId ) );
346}
347
348
349void LIB_TREE::CenterLibId( const LIB_ID& aLibId )
350{
351 centerIfValid( m_adapter->FindItem( aLibId ) );
352}
353
354
356{
357 m_tree_ctrl->UnselectAll();
358}
359
360
361void LIB_TREE::ExpandLibId( const LIB_ID& aLibId )
362{
363 expandIfValid( m_adapter->FindItem( aLibId ) );
364}
365
366
368{
370}
371
372
374{
376}
377
378
379void LIB_TREE::SetSearchString( const wxString& aSearchString )
380{
381 m_query_ctrl->ChangeValue( aSearchString );
382}
383
384
386{
387 return m_query_ctrl->GetValue();
388}
389
390
392{
393 wxString newEntry = GetSearchString();
394
395 std::vector<wxString>& recents = g_recentSearches[ m_recentSearchesKey ];
396
397 if( !newEntry.IsEmpty() )
398 {
399 if( alg::contains( recents, newEntry ) )
400 alg::delete_matching( recents, newEntry );
401
402 if( recents.size() >= RECENT_SEARCHES_MAX )
403 recents.pop_back();
404
405 recents.insert( recents.begin(), newEntry );
406 }
407
408 wxMenu* menu = new wxMenu();
409
410 for( const wxString& recent : recents )
411 menu->Append( menu->GetMenuItemCount() + 1, recent );
412
413 if( recents.empty() )
414 menu->Append( wxID_ANY, _( "recent searches" ) );
415
416 m_query_ctrl->SetMenu( menu );
417}
418
419
420void LIB_TREE::Regenerate( bool aKeepState )
421{
422 STATE current;
423
424 // Store the state
425 if( aKeepState )
426 current = getState();
427
428 wxString filter = m_query_ctrl->GetValue();
429 m_adapter->UpdateSearchString( filter, aKeepState );
431
432 // Restore the state
433 if( aKeepState )
434 setState( current );
435}
436
437
439{
440 m_adapter->RefreshTree();
441}
442
443
445{
446 if( m_query_ctrl )
447 return m_query_ctrl;
448 else
449 return m_tree_ctrl;
450}
451
452
454{
455 if( m_query_ctrl )
456 m_query_ctrl->SetFocus();
457}
458
459
460void LIB_TREE::toggleExpand( const wxDataViewItem& aTreeId )
461{
462 if( !aTreeId.IsOk() )
463 return;
464
465 if( m_tree_ctrl->IsExpanded( aTreeId ) )
466 m_tree_ctrl->Collapse( aTreeId );
467 else
468 m_tree_ctrl->Expand( aTreeId );
469}
470
471
472void LIB_TREE::selectIfValid( const wxDataViewItem& aTreeId )
473{
474 if( aTreeId.IsOk() )
475 {
476 m_tree_ctrl->EnsureVisible( aTreeId );
477 m_tree_ctrl->UnselectAll();
478 m_tree_ctrl->Select( aTreeId );
480 }
481}
482
483
484void LIB_TREE::centerIfValid( const wxDataViewItem& aTreeId )
485{
486 /*
487 * This doesn't actually center because the wxWidgets API is poorly suited to that (and
488 * it might be too noisy as well).
489 *
490 * It does try to keep the given item a bit off the top or bottom of the window.
491 */
492
493 if( aTreeId.IsOk() )
494 {
495 LIB_TREE_NODE* node = m_adapter->GetTreeNodeFor( aTreeId );
496 LIB_TREE_NODE* parent = node->m_Parent;
497 LIB_TREE_NODE* grandParent = parent ? parent->m_Parent : nullptr;
498
499 if( parent )
500 {
501 wxDataViewItemArray siblings;
502 m_adapter->GetChildren( wxDataViewItem( parent ), siblings );
503
504 int idx = siblings.Index( aTreeId );
505
506 if( idx + 5 < (int) siblings.GetCount() )
507 {
508 m_tree_ctrl->EnsureVisible( siblings.Item( idx + 5 ) );
509 }
510 else if( grandParent )
511 {
512 wxDataViewItemArray parentsSiblings;
513 m_adapter->GetChildren( wxDataViewItem( grandParent ), parentsSiblings );
514
515 int p_idx = parentsSiblings.Index( wxDataViewItem( parent ) );
516
517 if( p_idx + 1 < (int) parentsSiblings.GetCount() )
518 m_tree_ctrl->EnsureVisible( parentsSiblings.Item( p_idx + 1 ) );
519 }
520
521 if( idx - 5 >= 0 )
522 m_tree_ctrl->EnsureVisible( siblings.Item( idx - 5 ) );
523 else
524 m_tree_ctrl->EnsureVisible( wxDataViewItem( parent ) );
525 }
526
527 m_tree_ctrl->EnsureVisible( aTreeId );
528 }
529}
530
531
532void LIB_TREE::expandIfValid( const wxDataViewItem& aTreeId )
533{
534 if( aTreeId.IsOk() && !m_tree_ctrl->IsExpanded( aTreeId ) )
535 m_tree_ctrl->Expand( aTreeId );
536}
537
538
540{
541 wxCommandEvent event( EVT_LIBITEM_SELECTED );
542 wxPostEvent( this, event );
543}
544
545
547{
548 wxCommandEvent event( EVT_LIBITEM_CHOSEN );
549 wxPostEvent( this, event );
550}
551
552
554{
555 STATE state;
556 wxDataViewItemArray items;
557 m_adapter->GetChildren( wxDataViewItem( nullptr ), items );
558
559 for( const wxDataViewItem& item : items )
560 {
561 if( m_tree_ctrl->IsExpanded( item ) )
562 state.expanded.push_back( item );
563 }
564
565 state.selection = GetSelectedLibId();
566
567 return state;
568}
569
570
571void LIB_TREE::setState( const STATE& aState )
572{
573 m_tree_ctrl->Freeze();
574
575 for( const wxDataViewItem& item : aState.expanded )
576 m_tree_ctrl->Expand( item );
577
578 // wxDataViewCtrl cannot be frozen when a selection
579 // command is issued, otherwise it selects a random item (Windows)
580 m_tree_ctrl->Thaw();
581
582 if( !aState.selection.GetLibItemName().empty() || !aState.selection.GetLibNickname().empty() )
583 SelectLibId( aState.selection );
584}
585
586
587void LIB_TREE::onQueryText( wxCommandEvent& aEvent )
588{
589 m_debounceTimer->StartOnce( 200 );
590
591 // Required to avoid interaction with SetHint()
592 // See documentation for wxTextEntry::SetHint
593 aEvent.Skip();
594}
595
596
597void LIB_TREE::onDebounceTimer( wxTimerEvent& aEvent )
598{
599 m_inTimerEvent = true;
600 Regenerate( false );
601 m_inTimerEvent = false;
602}
603
604
605void LIB_TREE::onQueryCharHook( wxKeyEvent& aKeyStroke )
606{
607 int hotkey = aKeyStroke.GetKeyCode();
608
609 if( aKeyStroke.GetModifiers() & wxMOD_CONTROL )
610 hotkey += MD_CTRL;
611
612 if( aKeyStroke.GetModifiers() & wxMOD_ALT )
613 hotkey += MD_ALT;
614
615 if( aKeyStroke.GetModifiers() & wxMOD_SHIFT )
616 hotkey += MD_SHIFT;
617
618 if( hotkey == ACTIONS::expandAll.GetHotKey()
619 || hotkey == ACTIONS::expandAll.GetHotKeyAlt() )
620 {
622 return;
623 }
624 else if( hotkey == ACTIONS::collapseAll.GetHotKey()
625 || hotkey == ACTIONS::collapseAll.GetHotKeyAlt() )
626 {
628 return;
629 }
630
631 wxDataViewItem sel = m_tree_ctrl->GetSelection();
632
633 if( !sel.IsOk() )
634 sel = m_adapter->GetCurrentDataViewItem();
635
636 LIB_TREE_NODE::TYPE type = sel.IsOk() ? m_adapter->GetTypeFor( sel ) : LIB_TREE_NODE::INVALID;
637
638 switch( aKeyStroke.GetKeyCode() )
639 {
640 case WXK_UP:
643 break;
644
645 case WXK_DOWN:
648 break;
649
650 case WXK_ADD:
652
653 if( type == LIB_TREE_NODE::LIBRARY )
654 m_tree_ctrl->Expand( sel );
655
656 break;
657
658 case WXK_SUBTRACT:
660
661 if( type == LIB_TREE_NODE::LIBRARY )
662 m_tree_ctrl->Collapse( sel );
663
664 break;
665
666 case WXK_RETURN:
667 case WXK_NUMPAD_ENTER:
669
670 if( GetSelectedLibId().IsValid() )
672 else if( type == LIB_TREE_NODE::LIBRARY )
673 toggleExpand( sel );
674
675 break;
676
677 default:
678 aKeyStroke.Skip(); // Any other key: pass on to search box directly.
679 break;
680 }
681}
682
683
684void LIB_TREE::onQueryMouseMoved( wxMouseEvent& aEvent )
685{
686#if defined( __WXOSX__ ) || wxCHECK_VERSION( 3, 3, 0 ) // Doesn't work properly on other ports
687 wxPoint pos = aEvent.GetPosition();
688 wxRect ctrlRect = m_query_ctrl->GetScreenRect();
689 int buttonWidth = ctrlRect.GetHeight(); // Presume buttons are square
690
691 if( m_query_ctrl->IsSearchButtonVisible() && pos.x < buttonWidth )
692 SetCursor( wxCURSOR_ARROW );
693 else if( m_query_ctrl->IsCancelButtonVisible() && pos.x > ctrlRect.GetWidth() - buttonWidth )
694 SetCursor( wxCURSOR_ARROW );
695 else
696 SetCursor( wxCURSOR_IBEAM );
697#endif
698}
699
700
701#define PREVIEW_SIZE wxSize( 240, 200 )
702#define HOVER_TIMER_MILLIS 400
703
704
705void LIB_TREE::showPreview( wxDataViewItem aItem )
706{
707 if( aItem.IsOk() && m_adapter->HasPreview( aItem ) )
708 {
709 m_previewItem = aItem;
711
712 wxWindow* topLevelParent = wxGetTopLevelParent( m_parent );
713
714 if( !m_previewWindow )
715 m_previewWindow = new wxPopupWindow( topLevelParent );
716
717 m_previewWindow->SetSize( PREVIEW_SIZE );
718
719 m_adapter->ShowPreview( m_previewWindow, aItem );
720
721 m_previewWindow->SetPosition( wxPoint( m_tree_ctrl->GetScreenRect().GetRight() - 10,
722 wxGetMousePosition().y - PREVIEW_SIZE.y / 2 ) );
723
724 m_previewWindow->Show();
725 }
726}
727
728
730{
731 m_previewItem = wxDataViewItem();
732
733 if( m_previewWindow )
734 {
735 m_previewWindow->Hide();
736 }
737}
738
739
741{
742 hidePreview();
743
744 if( m_previewWindow )
745 {
746 m_previewWindow->Destroy();
747 m_previewWindow = nullptr;
748 }
749}
750
751
752void LIB_TREE::onIdle( wxIdleEvent& aEvent )
753{
754 // The wxDataViewCtrl won't give us its mouseMoved events so we're forced to use idle
755 // events to track the hover state
756
757 // The dang thing won't give us scroll events either, so we implement a poor-man's
758 // scroll-checker using the last-known positions of the preview or hover items.
759
760 wxWindow* topLevelParent = wxGetTopLevelParent( m_parent );
761 wxWindow* topLevelFocus = wxGetTopLevelParent( wxWindow::FindFocus() );
762
763 bool mouseOverWindow = false;
764 wxPoint screenPos = wxGetMousePosition();
765
766 if( m_tree_ctrl->IsShownOnScreen() )
767 mouseOverWindow |= m_tree_ctrl->GetScreenRect().Contains( screenPos );
768
769 if( m_previewDisabled || topLevelFocus != topLevelParent || !mouseOverWindow )
770 {
771 m_hoverTimer.Stop();
772 hidePreview();
773 return;
774 }
775
776 wxPoint clientPos = m_tree_ctrl->ScreenToClient( screenPos );
777 wxDataViewItem item;
778 wxDataViewColumn* col = nullptr;
779
780 m_tree_ctrl->HitTest( clientPos, item, col );
781
782 if( m_previewItem.IsOk() )
783 {
784 // Scroll checker
785 if( m_tree_ctrl->GetItemRect( m_previewItem ) != m_previewItemRect )
786 {
787 hidePreview();
788
789 m_hoverPos = clientPos;
790 m_hoverItem = item;
791 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
792 m_hoverTimer.StartOnce( HOVER_TIMER_MILLIS );
793 return;
794 }
795
796 if( item != m_previewItem )
797 {
798#ifdef __WXGTK__
799 // Hide the preview, because Wayland can't move windows.
800 if( wxGetDisplayInfo().type == wxDisplayType::wxDisplayWayland )
801 hidePreview();
802#endif
803 showPreview( item );
804 }
805
806 return;
807 }
808
809 if( m_hoverPos != clientPos )
810 {
811 m_hoverPos = clientPos;
812 m_hoverItem = item;
813 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
814 m_hoverTimer.StartOnce( HOVER_TIMER_MILLIS );
815 }
816}
817
818
819void LIB_TREE::onHoverTimer( wxTimerEvent& aEvent )
820{
821 hidePreview();
822
823 TOOL_DISPATCHER* toolDispatcher = m_adapter->GetToolDispatcher();
824
825 if( !m_tree_ctrl->IsShownOnScreen() || m_previewDisabled
826 || ( toolDispatcher && toolDispatcher->GetCurrentMenu() ) )
827 return;
828
829 wxDataViewItem item;
830 wxDataViewColumn* col = nullptr;
831 m_tree_ctrl->HitTest( m_hoverPos, item, col );
832
833 if( item == m_hoverItem && m_tree_ctrl->GetItemRect( item ) == m_hoverItemRect )
834 {
835 if( item != m_tree_ctrl->GetSelection() )
836 showPreview( item );
837 }
838 else // view must have been scrolled
839 {
840 m_hoverItem = item;
841 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
842 m_hoverTimer.StartOnce( HOVER_TIMER_MILLIS );
843 }
844}
845
846
847void LIB_TREE::onTreeCharHook( wxKeyEvent& aKeyStroke )
848{
849 onQueryCharHook( aKeyStroke );
850
851 if( aKeyStroke.GetSkipped() )
852 {
853 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
854 {
855 int hotkey = aKeyStroke.GetKeyCode();
856
857 if( aKeyStroke.ShiftDown() )
858 hotkey |= MD_SHIFT;
859
860 if( aKeyStroke.AltDown() )
861 hotkey |= MD_ALT;
862
863 if( aKeyStroke.ControlDown() )
864 hotkey |= MD_CTRL;
865
866 if( tool->GetManager()->GetActionManager()->RunHotKey( hotkey ) )
867 aKeyStroke.Skip( false );
868 }
869 }
870}
871
872
873void LIB_TREE::onTreeSelect( wxDataViewEvent& aEvent )
874{
875 if( m_tree_ctrl->IsFrozen() )
876 return;
877
878 if( !m_inTimerEvent )
880
882}
883
884
885void LIB_TREE::onTreeActivate( wxDataViewEvent& aEvent )
886{
887 hidePreview();
888
889 if( !m_inTimerEvent )
891
892 if( !GetSelectedLibId().IsValid() )
893 toggleExpand( m_tree_ctrl->GetSelection() ); // Expand library/part units subtree
894 else
895 postSelectEvent(); // Open symbol/footprint
896}
897
898
899void LIB_TREE::onDetailsLink( wxHtmlLinkEvent& aEvent )
900{
901 const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
902 wxString docname = info.GetHref();
903 PROJECT& prj = Pgm().GetSettingsManager().Prj();
904
905 GetAssociatedDocument( this, docname, &prj );
906}
907
908
909void LIB_TREE::onPreselect( wxCommandEvent& aEvent )
910{
911 hidePreview();
912
913 if( m_details_ctrl )
914 {
915 int unit = 0;
916 LIB_ID id = GetSelectedLibId( &unit );
917
918 if( id.IsValid() )
919 m_details_ctrl->SetPage( m_adapter->GenerateInfo( id, unit ) );
920 else
921 m_details_ctrl->SetPage( wxEmptyString );
922 }
923
924 aEvent.Skip();
925}
926
927
928void LIB_TREE::onItemContextMenu( wxDataViewEvent& aEvent )
929{
930 hidePreview();
931
933 {
934 m_skipNextRightClick = false;
935 return;
936 }
937
938 m_previewDisabled = true;
939
940 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
941 {
942 if( !GetCurrentTreeNode() )
943 {
944 wxPoint pos = m_tree_ctrl->ScreenToClient( wxGetMousePosition() );
945
946 wxDataViewItem item;
947 wxDataViewColumn* col;
948 m_tree_ctrl->HitTest( pos, item, col );
949
950 if( item.IsOk() )
951 {
952 m_tree_ctrl->SetFocus();
953 m_tree_ctrl->Select( item );
954 wxSafeYield();
955 }
956 }
957
958 tool->Activate();
959 tool->GetManager()->VetoContextMenuMouseWarp();
960 tool->GetToolMenu().ShowContextMenu();
961
963 tool->GetManager()->DispatchContextMenu( evt );
964 }
965 else
966 {
968
969 if( current && current->m_Type == LIB_TREE_NODE::LIBRARY )
970 {
971 ACTION_MENU menu( true, nullptr );
972
973 if( current->m_Pinned )
974 {
976
977 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
978 m_adapter->UnpinLibrary( current );
979 }
980 else
981 {
982 menu.Add( ACTIONS::pinLibrary );
983
984 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
985 m_adapter->PinLibrary( current );
986 }
987 }
988 }
989
990 m_previewDisabled = false;
991}
992
993
994void LIB_TREE::onHeaderContextMenu( wxDataViewEvent& aEvent )
995{
996 hidePreview();
997 m_previewDisabled = true;
998
999 ACTION_MENU menu( true, nullptr );
1000
1002
1003 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
1004 {
1005 EDA_REORDERABLE_LIST_DIALOG dlg( m_parent, _( "Select Columns" ),
1006 m_adapter->GetAvailableColumns(),
1007 m_adapter->GetShownColumns() );
1008
1009 if( dlg.ShowModal() == wxID_OK )
1010 m_adapter->SetShownColumns( dlg.EnabledList() );
1011 }
1012
1013 m_previewDisabled = false;
1014}
1015
1016
1017wxDEFINE_EVENT( EVT_LIBITEM_SELECTED, wxCommandEvent );
1018wxDEFINE_EVENT( EVT_LIBITEM_CHOSEN, wxCommandEvent );
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
static TOOL_ACTION pinLibrary
Definition: actions.h:140
static TOOL_ACTION selectLibTreeColumns
Definition: actions.h:208
static TOOL_ACTION unpinLibrary
Definition: actions.h:141
static TOOL_ACTION expandAll
Definition: actions.h:80
static TOOL_ACTION collapseAll
Definition: actions.h:81
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:51
wxRect m_previewItemRect
Definition: lib_tree.h:257
void RefreshLibTree()
Refreshes the tree (mainly to update highlighting and asterisking)
Definition: lib_tree.cpp:438
wxPoint m_hoverPos
Definition: lib_tree.h:252
wxTimer * m_debounceTimer
Definition: lib_tree.h:243
void onHoverTimer(wxTimerEvent &aEvent)
Definition: lib_tree.cpp:819
LIB_TREE_NODE * GetCurrentTreeNode() const
Definition: lib_tree.cpp:332
void onQueryMouseMoved(wxMouseEvent &aEvent)
Definition: lib_tree.cpp:684
wxDataViewItem m_previewItem
Definition: lib_tree.h:256
HTML_WINDOW * m_details_ctrl
Definition: lib_tree.h:242
void onTreeActivate(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:885
wxString m_recentSearchesKey
Definition: lib_tree.h:246
void onQueryCharHook(wxKeyEvent &aEvent)
Definition: lib_tree.cpp:605
void CenterLibId(const LIB_ID &aLibId)
Ensure that an item is visible (preferably centered).
Definition: lib_tree.cpp:349
void postSelectEvent()
Post SYMBOL_SELECTED event to notify the selection handler that a part has been selected.
Definition: lib_tree.cpp:546
wxWindow * GetFocusTarget()
Definition: lib_tree.cpp:444
void onItemContextMenu(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:928
void destroyPreview()
Definition: lib_tree.cpp:740
void ShowChangedLanguage()
Definition: lib_tree.cpp:291
WX_DATAVIEWCTRL * m_tree_ctrl
Definition: lib_tree.h:241
void onQueryText(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:587
void toggleExpand(const wxDataViewItem &aTreeId)
Expand or collapse a node, switching it to the opposite state.
Definition: lib_tree.cpp:460
bool m_skipNextRightClick
Definition: lib_tree.h:250
bool m_inTimerEvent
Definition: lib_tree.h:244
void selectIfValid(const wxDataViewItem &aTreeId)
If a wxDataViewitem is valid, select it and post a selection event.
Definition: lib_tree.cpp:472
void onIdle(wxIdleEvent &aEvent)
Definition: lib_tree.cpp:752
void FocusSearchFieldIfExists()
Focus the search widget if it exists.
Definition: lib_tree.cpp:453
void expandIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:532
void onPreselect(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:909
wxPopupWindow * m_previewWindow
Definition: lib_tree.h:258
void SelectLibId(const LIB_ID &aLibId)
Select an item in the tree widget.
Definition: lib_tree.cpp:343
void updateRecentSearchMenu()
Definition: lib_tree.cpp:391
void hidePreview()
Definition: lib_tree.cpp:729
void showPreview(wxDataViewItem aItem)
Definition: lib_tree.cpp:705
wxString GetSearchString() const
Definition: lib_tree.cpp:385
void onTreeSelect(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:873
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:315
wxTimer m_hoverTimer
Definition: lib_tree.h:255
void postPreselectEvent()
Post a wxEVT_DATAVIEW_SELECTION_CHANGED to notify the selection handler that a new part has been pres...
Definition: lib_tree.cpp:539
void Unselect()
Unselect currently selected item in wxDataViewCtrl.
Definition: lib_tree.cpp:355
void onTreeCharHook(wxKeyEvent &aEvent)
Definition: lib_tree.cpp:847
void onDetailsLink(wxHtmlLinkEvent &aEvent)
Definition: lib_tree.cpp:899
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:301
@ MULTISELECT
Definition: lib_tree.h:59
@ FILTERS
Definition: lib_tree.h:56
@ DETAILS
Definition: lib_tree.h:57
@ SEARCH
Definition: lib_tree.h:55
void ExpandAll()
Definition: lib_tree.cpp:367
wxBoxSizer * m_filtersSizer
Definition: lib_tree.h:248
wxDataViewItem m_hoverItem
Definition: lib_tree.h:253
~LIB_TREE() override
Definition: lib_tree.cpp:253
void SetSearchString(const wxString &aSearchString)
Save/restore search string.
Definition: lib_tree.cpp:379
void setState(const STATE &aState)
Restore the symbol tree widget state from an object.
Definition: lib_tree.cpp:571
STATE getState() const
Return the symbol tree widget state.
Definition: lib_tree.cpp:553
bool m_previewDisabled
Definition: lib_tree.h:259
void ExpandLibId(const LIB_ID &aLibId)
Expand and item i the tree widget.
Definition: lib_tree.cpp:361
wxSearchCtrl * m_query_ctrl
Definition: lib_tree.h:239
void CollapseAll()
Definition: lib_tree.cpp:373
void centerIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:484
void onDebounceTimer(wxTimerEvent &aEvent)
Definition: lib_tree.cpp:597
wxRect m_hoverItemRect
Definition: lib_tree.h:254
STD_BITMAP_BUTTON * m_sort_ctrl
Definition: lib_tree.h:240
void onHeaderContextMenu(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:994
void Regenerate(bool aKeepState)
Regenerate the tree.
Definition: lib_tree.cpp:420
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > m_adapter
Definition: lib_tree.h:237
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:142
Container for project specific data.
Definition: project.h:62
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
A bitmap button widget that behaves like a standard dialog button except with an icon.
void SetBitmap(const wxBitmapBundle &aBmp)
ACTION_MENU * GetCurrentMenu() const
Generic, UI-independent tool event.
Definition: tool_event.h:167
bool empty() const
Definition: utf8.h:104
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)
bool GetAssociatedDocument(wxWindow *aParent, const wxString &aDocName, PROJECT *aProject, SEARCH_STACK *aPaths)
Open a document (file) with the suitable browser.
Definition: eda_doc.cpp:60
This file is part of the common library.
#define HOVER_TIMER_MILLIS
Definition: lib_tree.cpp:702
constexpr int RECENT_SEARCHES_MAX
Definition: lib_tree.cpp:46
#define PREVIEW_SIZE
Definition: lib_tree.cpp:701
std::map< wxString, std::vector< wxString > > g_recentSearches
Definition: lib_tree.cpp:48
wxDEFINE_EVENT(EVT_LIBITEM_SELECTED, wxCommandEvent)
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:165
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:100
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1059
see class PGM_BASE
Structure storing state of the symbol tree widget.
Definition: lib_tree.h:194
LIB_ID selection
Definition: lib_tree.h:199
std::vector< wxDataViewItem > expanded
< List of expanded nodes
Definition: lib_tree.h:196
@ 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