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->Freeze();
358 m_tree_ctrl->UnselectAll();
359 m_tree_ctrl->Thaw();
360}
361
362
363void LIB_TREE::ExpandLibId( const LIB_ID& aLibId )
364{
365 expandIfValid( m_adapter->FindItem( aLibId ) );
366}
367
368
370{
372}
373
374
376{
378}
379
380
381void LIB_TREE::SetSearchString( const wxString& aSearchString )
382{
383 m_query_ctrl->ChangeValue( aSearchString );
384}
385
386
388{
389 return m_query_ctrl->GetValue();
390}
391
392
394{
395 wxString newEntry = GetSearchString();
396
397 std::vector<wxString>& recents = g_recentSearches[ m_recentSearchesKey ];
398
399 if( !newEntry.IsEmpty() )
400 {
401 if( alg::contains( recents, newEntry ) )
402 alg::delete_matching( recents, newEntry );
403
404 if( recents.size() >= RECENT_SEARCHES_MAX )
405 recents.pop_back();
406
407 recents.insert( recents.begin(), newEntry );
408 }
409
410 wxMenu* menu = new wxMenu();
411
412 for( const wxString& recent : recents )
413 menu->Append( menu->GetMenuItemCount() + 1, recent );
414
415 if( recents.empty() )
416 menu->Append( wxID_ANY, _( "recent searches" ) );
417
418 m_query_ctrl->SetMenu( menu );
419}
420
421
422void LIB_TREE::Regenerate( bool aKeepState )
423{
424 STATE current;
425
426 // Store the state
427 if( aKeepState )
428 current = getState();
429
430 wxString filter = m_query_ctrl->GetValue();
431 m_adapter->UpdateSearchString( filter, aKeepState );
433
434 // Restore the state
435 if( aKeepState )
436 setState( current );
437}
438
439
441{
442 m_adapter->RefreshTree();
443}
444
445
447{
448 if( m_query_ctrl )
449 return m_query_ctrl;
450 else
451 return m_tree_ctrl;
452}
453
454
456{
457 if( m_query_ctrl )
458 m_query_ctrl->SetFocus();
459}
460
461
462void LIB_TREE::toggleExpand( const wxDataViewItem& aTreeId )
463{
464 if( !aTreeId.IsOk() )
465 return;
466
467 if( m_tree_ctrl->IsExpanded( aTreeId ) )
468 m_tree_ctrl->Collapse( aTreeId );
469 else
470 m_tree_ctrl->Expand( aTreeId );
471}
472
473
474void LIB_TREE::selectIfValid( const wxDataViewItem& aTreeId )
475{
476 if( aTreeId.IsOk() )
477 {
478 m_tree_ctrl->EnsureVisible( aTreeId );
479 m_tree_ctrl->UnselectAll();
480 m_tree_ctrl->Select( aTreeId );
482 }
483}
484
485
486void LIB_TREE::centerIfValid( const wxDataViewItem& aTreeId )
487{
488 /*
489 * This doesn't actually center because the wxWidgets API is poorly suited to that (and
490 * it might be too noisy as well).
491 *
492 * It does try to keep the given item a bit off the top or bottom of the window.
493 */
494
495 if( aTreeId.IsOk() )
496 {
497 LIB_TREE_NODE* node = m_adapter->GetTreeNodeFor( aTreeId );
498 LIB_TREE_NODE* parent = node->m_Parent;
499 LIB_TREE_NODE* grandParent = parent ? parent->m_Parent : nullptr;
500
501 if( parent )
502 {
503 wxDataViewItemArray siblings;
504 m_adapter->GetChildren( wxDataViewItem( parent ), siblings );
505
506 int idx = siblings.Index( aTreeId );
507
508 if( idx + 5 < (int) siblings.GetCount() )
509 {
510 m_tree_ctrl->EnsureVisible( siblings.Item( idx + 5 ) );
511 }
512 else if( grandParent )
513 {
514 wxDataViewItemArray parentsSiblings;
515 m_adapter->GetChildren( wxDataViewItem( grandParent ), parentsSiblings );
516
517 int p_idx = parentsSiblings.Index( wxDataViewItem( parent ) );
518
519 if( p_idx + 1 < (int) parentsSiblings.GetCount() )
520 m_tree_ctrl->EnsureVisible( parentsSiblings.Item( p_idx + 1 ) );
521 }
522
523 if( idx - 5 >= 0 )
524 m_tree_ctrl->EnsureVisible( siblings.Item( idx - 5 ) );
525 else
526 m_tree_ctrl->EnsureVisible( wxDataViewItem( parent ) );
527 }
528
529 m_tree_ctrl->EnsureVisible( aTreeId );
530 }
531}
532
533
534void LIB_TREE::expandIfValid( const wxDataViewItem& aTreeId )
535{
536 if( aTreeId.IsOk() && !m_tree_ctrl->IsExpanded( aTreeId ) )
537 m_tree_ctrl->Expand( aTreeId );
538}
539
540
542{
543 wxCommandEvent event( EVT_LIBITEM_SELECTED );
544 wxPostEvent( this, event );
545}
546
547
549{
550 wxCommandEvent event( EVT_LIBITEM_CHOSEN );
551 wxPostEvent( this, event );
552}
553
554
556{
557 STATE state;
558 wxDataViewItemArray items;
559 m_adapter->GetChildren( wxDataViewItem( nullptr ), items );
560
561 for( const wxDataViewItem& item : items )
562 {
563 if( m_tree_ctrl->IsExpanded( item ) )
564 state.expanded.push_back( item );
565 }
566
567 state.selection = GetSelectedLibId();
568
569 return state;
570}
571
572
573void LIB_TREE::setState( const STATE& aState )
574{
575 m_tree_ctrl->Freeze();
576
577 for( const wxDataViewItem& item : aState.expanded )
578 m_tree_ctrl->Expand( item );
579
580 // wxDataViewCtrl cannot be frozen when a selection
581 // command is issued, otherwise it selects a random item (Windows)
582 m_tree_ctrl->Thaw();
583
584 if( !aState.selection.GetLibItemName().empty() || !aState.selection.GetLibNickname().empty() )
585 SelectLibId( aState.selection );
586}
587
588
589void LIB_TREE::onQueryText( wxCommandEvent& aEvent )
590{
591 m_debounceTimer->StartOnce( 200 );
592
593 // Required to avoid interaction with SetHint()
594 // See documentation for wxTextEntry::SetHint
595 aEvent.Skip();
596}
597
598
599void LIB_TREE::onDebounceTimer( wxTimerEvent& aEvent )
600{
601 m_inTimerEvent = true;
602 Regenerate( false );
603 m_inTimerEvent = false;
604}
605
606
607void LIB_TREE::onQueryCharHook( wxKeyEvent& aKeyStroke )
608{
609 int hotkey = aKeyStroke.GetKeyCode();
610
611 if( aKeyStroke.GetModifiers() & wxMOD_CONTROL )
612 hotkey += MD_CTRL;
613
614 if( aKeyStroke.GetModifiers() & wxMOD_ALT )
615 hotkey += MD_ALT;
616
617 if( aKeyStroke.GetModifiers() & wxMOD_SHIFT )
618 hotkey += MD_SHIFT;
619
620 if( hotkey == ACTIONS::expandAll.GetHotKey()
621 || hotkey == ACTIONS::expandAll.GetHotKeyAlt() )
622 {
624 return;
625 }
626 else if( hotkey == ACTIONS::collapseAll.GetHotKey()
627 || hotkey == ACTIONS::collapseAll.GetHotKeyAlt() )
628 {
630 return;
631 }
632
633 wxDataViewItem sel = m_tree_ctrl->GetSelection();
634
635 if( !sel.IsOk() )
636 sel = m_adapter->GetCurrentDataViewItem();
637
638 LIB_TREE_NODE::TYPE type = sel.IsOk() ? m_adapter->GetTypeFor( sel ) : LIB_TREE_NODE::TYPE::INVALID;
639
640 switch( aKeyStroke.GetKeyCode() )
641 {
642 case WXK_UP:
645 break;
646
647 case WXK_DOWN:
650 break;
651
652 case WXK_ADD:
654
655 if( type == LIB_TREE_NODE::TYPE::LIBRARY )
656 m_tree_ctrl->Expand( sel );
657
658 break;
659
660 case WXK_SUBTRACT:
662
663 if( type == LIB_TREE_NODE::TYPE::LIBRARY )
664 m_tree_ctrl->Collapse( sel );
665
666 break;
667
668 case WXK_RETURN:
669 case WXK_NUMPAD_ENTER:
671
672 if( GetSelectedLibId().IsValid() )
674 else if( type == LIB_TREE_NODE::TYPE::LIBRARY )
675 toggleExpand( sel );
676
677 break;
678
679 default:
680 aKeyStroke.Skip(); // Any other key: pass on to search box directly.
681 break;
682 }
683}
684
685
686void LIB_TREE::onQueryMouseMoved( wxMouseEvent& aEvent )
687{
688#if defined( __WXOSX__ ) || wxCHECK_VERSION( 3, 3, 0 ) // Doesn't work properly on other ports
689 wxPoint pos = aEvent.GetPosition();
690 wxRect ctrlRect = m_query_ctrl->GetScreenRect();
691 int buttonWidth = ctrlRect.GetHeight(); // Presume buttons are square
692
693 if( m_query_ctrl->IsSearchButtonVisible() && pos.x < buttonWidth )
694 SetCursor( wxCURSOR_ARROW );
695 else if( m_query_ctrl->IsCancelButtonVisible() && pos.x > ctrlRect.GetWidth() - buttonWidth )
696 SetCursor( wxCURSOR_ARROW );
697 else
698 SetCursor( wxCURSOR_IBEAM );
699#endif
700}
701
702
703#define PREVIEW_SIZE wxSize( 240, 200 )
704#define HOVER_TIMER_MILLIS 400
705
706
707void LIB_TREE::showPreview( wxDataViewItem aItem )
708{
709 if( aItem.IsOk() && m_adapter->HasPreview( aItem ) )
710 {
711 m_previewItem = aItem;
713
714 wxWindow* topLevelParent = wxGetTopLevelParent( m_parent );
715
716 if( !m_previewWindow )
717 m_previewWindow = new wxPopupWindow( topLevelParent );
718
719 m_previewWindow->SetSize( PREVIEW_SIZE );
720
721 m_adapter->ShowPreview( m_previewWindow, aItem );
722
723 m_previewWindow->SetPosition( wxPoint( m_tree_ctrl->GetScreenRect().GetRight() - 10,
724 wxGetMousePosition().y - PREVIEW_SIZE.y / 2 ) );
725
726 m_previewWindow->Show();
727 }
728}
729
730
732{
733 m_previewItem = wxDataViewItem();
734
735 if( m_previewWindow )
736 m_previewWindow->Hide();
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 if( item != m_previewItem )
785 {
786#ifdef __WXGTK__
787 // Hide the preview, because Wayland can't move windows.
788 if( wxGetDisplayInfo().type == wxDisplayType::wxDisplayWayland )
789 hidePreview();
790#endif
791 showPreview( item );
792 }
793
794 return;
795 }
796
797 if( m_hoverPos != clientPos )
798 {
799 m_hoverPos = clientPos;
800 m_hoverItem = item;
801 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
802 m_hoverTimer.StartOnce( HOVER_TIMER_MILLIS );
803 }
804}
805
806
807void LIB_TREE::onHoverTimer( wxTimerEvent& aEvent )
808{
809 hidePreview();
810
811 TOOL_DISPATCHER* toolDispatcher = m_adapter->GetToolDispatcher();
812
813 if( !m_tree_ctrl->IsShownOnScreen() || m_previewDisabled
814 || ( toolDispatcher && toolDispatcher->GetCurrentMenu() ) )
815 return;
816
817 wxDataViewItem item;
818 wxDataViewColumn* col = nullptr;
819 m_tree_ctrl->HitTest( m_hoverPos, item, col );
820
821 if( item == m_hoverItem && m_tree_ctrl->GetItemRect( item ) == m_hoverItemRect )
822 {
823 if( item != m_tree_ctrl->GetSelection() )
824 showPreview( item );
825 }
826 else // view must have been scrolled
827 {
828 m_hoverItem = item;
829 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
830 m_hoverTimer.StartOnce( HOVER_TIMER_MILLIS );
831 }
832}
833
834
835void LIB_TREE::onTreeCharHook( wxKeyEvent& aKeyStroke )
836{
837 onQueryCharHook( aKeyStroke );
838
839 if( aKeyStroke.GetSkipped() )
840 {
841 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
842 {
843 int hotkey = aKeyStroke.GetKeyCode();
844
845 if( aKeyStroke.ShiftDown() )
846 hotkey |= MD_SHIFT;
847
848 if( aKeyStroke.AltDown() )
849 hotkey |= MD_ALT;
850
851 if( aKeyStroke.ControlDown() )
852 hotkey |= MD_CTRL;
853
854 if( tool->GetManager()->GetActionManager()->RunHotKey( hotkey ) )
855 aKeyStroke.Skip( false );
856 }
857 }
858}
859
860
861void LIB_TREE::onTreeSelect( wxDataViewEvent& aEvent )
862{
863 if( m_tree_ctrl->IsFrozen() )
864 return;
865
866 if( !m_inTimerEvent )
868
870}
871
872
873void LIB_TREE::onTreeActivate( wxDataViewEvent& aEvent )
874{
875 hidePreview();
876
877 if( !m_inTimerEvent )
879
880 if( !GetSelectedLibId().IsValid() )
881 toggleExpand( m_tree_ctrl->GetSelection() ); // Expand library/part units subtree
882 else
883 postSelectEvent(); // Open symbol/footprint
884}
885
886
887void LIB_TREE::onDetailsLink( wxHtmlLinkEvent& aEvent )
888{
889 const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
890 wxString docname = info.GetHref();
891 PROJECT& prj = Pgm().GetSettingsManager().Prj();
892
893 GetAssociatedDocument( this, docname, &prj );
894}
895
896
897void LIB_TREE::onPreselect( wxCommandEvent& aEvent )
898{
899 hidePreview();
900
901 if( m_details_ctrl )
902 {
903 int unit = 0;
904 LIB_ID id = GetSelectedLibId( &unit );
905
906 if( id.IsValid() )
907 m_details_ctrl->SetPage( m_adapter->GenerateInfo( id, unit ) );
908 else
909 m_details_ctrl->SetPage( wxEmptyString );
910 }
911
912 aEvent.Skip();
913}
914
915
916void LIB_TREE::onItemContextMenu( wxDataViewEvent& aEvent )
917{
918 hidePreview();
919
921 {
922 m_skipNextRightClick = false;
923 return;
924 }
925
926 m_previewDisabled = true;
927
928 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
929 {
930 if( !GetCurrentTreeNode() )
931 {
932 wxPoint pos = m_tree_ctrl->ScreenToClient( wxGetMousePosition() );
933
934 wxDataViewItem item;
935 wxDataViewColumn* col;
936 m_tree_ctrl->HitTest( pos, item, col );
937
938 if( item.IsOk() )
939 {
940 m_tree_ctrl->SetFocus();
941 m_tree_ctrl->Select( item );
942 wxSafeYield();
943 }
944 }
945
946 tool->Activate();
947 tool->GetManager()->VetoContextMenuMouseWarp();
948 tool->GetToolMenu().ShowContextMenu();
949
951 tool->GetManager()->DispatchContextMenu( evt );
952 }
953 else
954 {
956
957 if( current && current->m_Type == LIB_TREE_NODE::TYPE::LIBRARY )
958 {
959 ACTION_MENU menu( true, nullptr );
960
961 if( current->m_Pinned )
962 {
964
965 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
966 m_adapter->UnpinLibrary( current );
967 }
968 else
969 {
970 menu.Add( ACTIONS::pinLibrary );
971
972 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
973 m_adapter->PinLibrary( current );
974 }
975 }
976 }
977
978 m_previewDisabled = false;
979}
980
981
982void LIB_TREE::onHeaderContextMenu( wxDataViewEvent& aEvent )
983{
984 hidePreview();
985 m_previewDisabled = true;
986
987 ACTION_MENU menu( true, nullptr );
988
990
991 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
992 {
993 EDA_REORDERABLE_LIST_DIALOG dlg( m_parent, _( "Select Columns" ),
994 m_adapter->GetAvailableColumns(),
995 m_adapter->GetShownColumns() );
996
997 if( dlg.ShowModal() == wxID_OK )
998 m_adapter->SetShownColumns( dlg.EnabledList() );
999 }
1000
1001 m_previewDisabled = false;
1002}
1003
1004
1005wxDEFINE_EVENT( EVT_LIBITEM_SELECTED, wxCommandEvent );
1006wxDEFINE_EVENT( EVT_LIBITEM_CHOSEN, wxCommandEvent );
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
static TOOL_ACTION pinLibrary
Definition: actions.h:142
static TOOL_ACTION selectLibTreeColumns
Definition: actions.h:213
static TOOL_ACTION unpinLibrary
Definition: actions.h:143
static TOOL_ACTION expandAll
Definition: actions.h:82
static TOOL_ACTION collapseAll
Definition: actions.h:83
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:50
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:440
wxPoint m_hoverPos
Definition: lib_tree.h:252
wxTimer * m_debounceTimer
Definition: lib_tree.h:243
void onHoverTimer(wxTimerEvent &aEvent)
Definition: lib_tree.cpp:807
LIB_TREE_NODE * GetCurrentTreeNode() const
Definition: lib_tree.cpp:332
void onQueryMouseMoved(wxMouseEvent &aEvent)
Definition: lib_tree.cpp:686
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:873
wxString m_recentSearchesKey
Definition: lib_tree.h:246
void onQueryCharHook(wxKeyEvent &aEvent)
Definition: lib_tree.cpp:607
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:548
wxWindow * GetFocusTarget()
Definition: lib_tree.cpp:446
void onItemContextMenu(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:916
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:589
void toggleExpand(const wxDataViewItem &aTreeId)
Expand or collapse a node, switching it to the opposite state.
Definition: lib_tree.cpp:462
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:474
void onIdle(wxIdleEvent &aEvent)
Definition: lib_tree.cpp:752
void FocusSearchFieldIfExists()
Focus the search widget if it exists.
Definition: lib_tree.cpp:455
void expandIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:534
void onPreselect(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:897
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:393
void hidePreview()
Definition: lib_tree.cpp:731
void showPreview(wxDataViewItem aItem)
Definition: lib_tree.cpp:707
wxString GetSearchString() const
Definition: lib_tree.cpp:387
void onTreeSelect(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:861
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:541
void Unselect()
Unselect currently selected item in wxDataViewCtrl.
Definition: lib_tree.cpp:355
void onTreeCharHook(wxKeyEvent &aEvent)
Definition: lib_tree.cpp:835
void onDetailsLink(wxHtmlLinkEvent &aEvent)
Definition: lib_tree.cpp:887
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:369
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:381
void setState(const STATE &aState)
Restore the symbol tree widget state from an object.
Definition: lib_tree.cpp:573
STATE getState() const
Return the symbol tree widget state.
Definition: lib_tree.cpp:555
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:363
wxSearchCtrl * m_query_ctrl
Definition: lib_tree.h:239
void CollapseAll()
Definition: lib_tree.cpp:375
void centerIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:486
void onDebounceTimer(wxTimerEvent &aEvent)
Definition: lib_tree.cpp:599
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:982
void Regenerate(bool aKeepState)
Regenerate the tree.
Definition: lib_tree.cpp:422
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:61
This file is part of the common library.
#define HOVER_TIMER_MILLIS
Definition: lib_tree.cpp:704
constexpr int RECENT_SEARCHES_MAX
Definition: lib_tree.cpp:46
#define PREVIEW_SIZE
Definition: lib_tree.cpp:703
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