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