KiCad PCB EDA Suite
Loading...
Searching...
No Matches
lib_tree.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2014 Henner Zeller <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, 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( 6 ) + 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 )
639 : LIB_TREE_NODE::TYPE::INVALID;
640
641 switch( aKeyStroke.GetKeyCode() )
642 {
643 case WXK_UP:
646 break;
647
648 case WXK_DOWN:
651 break;
652
653 case WXK_ADD:
655
656 if( type == LIB_TREE_NODE::TYPE::LIBRARY )
657 m_tree_ctrl->Expand( sel );
658
659 break;
660
661 case WXK_SUBTRACT:
663
664 if( type == LIB_TREE_NODE::TYPE::LIBRARY )
665 m_tree_ctrl->Collapse( sel );
666
667 break;
668
669 case WXK_RETURN:
670 case WXK_NUMPAD_ENTER:
672
673 if( GetSelectedLibId().IsValid() )
675 else if( type == LIB_TREE_NODE::TYPE::LIBRARY )
676 toggleExpand( sel );
677
678 break;
679
680 default:
681 aKeyStroke.Skip(); // Any other key: pass on to search box directly.
682 break;
683 }
684}
685
686
687void LIB_TREE::onQueryMouseMoved( wxMouseEvent& aEvent )
688{
689#if defined( __WXOSX__ ) || wxCHECK_VERSION( 3, 3, 0 ) // Doesn't work properly on other ports
690 wxPoint pos = aEvent.GetPosition();
691 wxRect ctrlRect = m_query_ctrl->GetScreenRect();
692 int buttonWidth = ctrlRect.GetHeight(); // Presume buttons are square
693
694 if( m_query_ctrl->IsSearchButtonVisible() && pos.x < buttonWidth )
695 SetCursor( wxCURSOR_ARROW );
696 else if( m_query_ctrl->IsCancelButtonVisible() && pos.x > ctrlRect.GetWidth() - buttonWidth )
697 SetCursor( wxCURSOR_ARROW );
698 else
699 SetCursor( wxCURSOR_IBEAM );
700#endif
701}
702
703
704#define PREVIEW_SIZE wxSize( 240, 200 )
705#define HOVER_TIMER_MILLIS 400
706
707
708void LIB_TREE::showPreview( wxDataViewItem aItem )
709{
710 if( aItem.IsOk() && m_adapter->HasPreview( aItem ) )
711 {
712 m_previewItem = aItem;
714
715 wxWindow* topLevelParent = wxGetTopLevelParent( m_parent );
716
717 if( !m_previewWindow )
718 m_previewWindow = new wxPopupWindow( topLevelParent );
719
720 m_previewWindow->SetSize( PREVIEW_SIZE );
721
722 m_adapter->ShowPreview( m_previewWindow, aItem );
723
724 m_previewWindow->SetPosition( wxPoint( m_tree_ctrl->GetScreenRect().GetRight() - 10,
725 wxGetMousePosition().y - PREVIEW_SIZE.y / 2 ) );
726
727 m_previewWindow->Show();
728 }
729}
730
731
733{
734 m_previewItem = wxDataViewItem();
735
736 if( m_previewWindow )
737 m_previewWindow->Hide();
738}
739
740
742{
743 hidePreview();
744
745 if( m_previewWindow )
746 {
747 m_previewWindow->Destroy();
748 m_previewWindow = nullptr;
749 }
750}
751
752
753void LIB_TREE::onIdle( wxIdleEvent& aEvent )
754{
755 // The wxDataViewCtrl won't give us its mouseMoved events so we're forced to use idle
756 // events to track the hover state
757
758 // The dang thing won't give us scroll events either, so we implement a poor-man's
759 // scroll-checker using the last-known positions of the preview or hover items.
760
761 wxWindow* topLevelParent = wxGetTopLevelParent( m_parent );
762 wxWindow* topLevelFocus = wxGetTopLevelParent( wxWindow::FindFocus() );
763
764 bool mouseOverWindow = false;
765 wxPoint screenPos = wxGetMousePosition();
766
767 if( m_tree_ctrl->IsShownOnScreen() )
768 mouseOverWindow |= m_tree_ctrl->GetScreenRect().Contains( screenPos );
769
770 if( m_previewDisabled || topLevelFocus != topLevelParent || !mouseOverWindow )
771 {
772 m_hoverTimer.Stop();
773 hidePreview();
774 return;
775 }
776
777 wxPoint clientPos = m_tree_ctrl->ScreenToClient( screenPos );
778 wxDataViewItem item;
779 wxDataViewColumn* col = nullptr;
780
781 m_tree_ctrl->HitTest( clientPos, item, col );
782
783 if( m_previewItem.IsOk() )
784 {
785 if( item != m_previewItem )
786 {
787#ifdef __WXGTK__
788 // Hide the preview, because Wayland can't move windows.
789 if( wxGetDisplayInfo().type == wxDisplayType::wxDisplayWayland )
790 hidePreview();
791#endif
792 showPreview( item );
793 }
794
795 return;
796 }
797
798 if( m_hoverPos != clientPos )
799 {
800 m_hoverPos = clientPos;
801 m_hoverItem = item;
802 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
803 m_hoverTimer.StartOnce( HOVER_TIMER_MILLIS );
804 }
805}
806
807
808void LIB_TREE::onHoverTimer( wxTimerEvent& aEvent )
809{
810 hidePreview();
811
812 if( !m_tree_ctrl->IsShownOnScreen() || m_previewDisabled )
813 return;
814
815 wxDataViewItem item;
816 wxDataViewColumn* col = nullptr;
817 m_tree_ctrl->HitTest( m_hoverPos, item, col );
818
819 if( item == m_hoverItem && m_tree_ctrl->GetItemRect( item ) == m_hoverItemRect )
820 {
821 if( item != m_tree_ctrl->GetSelection() )
822 showPreview( item );
823 }
824 else // view must have been scrolled
825 {
826 m_hoverItem = item;
827 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
828 m_hoverTimer.StartOnce( HOVER_TIMER_MILLIS );
829 }
830}
831
832
833void LIB_TREE::onTreeCharHook( wxKeyEvent& aKeyStroke )
834{
835 onQueryCharHook( aKeyStroke );
836
837 if( aKeyStroke.GetSkipped() )
838 {
839 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
840 {
841 int hotkey = aKeyStroke.GetKeyCode();
842
843 if( aKeyStroke.ShiftDown() )
844 hotkey |= MD_SHIFT;
845
846 if( aKeyStroke.AltDown() )
847 hotkey |= MD_ALT;
848
849 if( aKeyStroke.ControlDown() )
850 hotkey |= MD_CTRL;
851
852 if( tool->GetManager()->GetActionManager()->RunHotKey( hotkey ) )
853 aKeyStroke.Skip( false );
854 }
855 }
856}
857
858
859void LIB_TREE::onTreeSelect( wxDataViewEvent& aEvent )
860{
861 if( m_tree_ctrl->IsFrozen() )
862 return;
863
864 if( !m_inTimerEvent )
866
868}
869
870
871void LIB_TREE::onTreeActivate( wxDataViewEvent& aEvent )
872{
873 hidePreview();
874
875 if( !m_inTimerEvent )
877
878 if( !GetSelectedLibId().IsValid() )
879 toggleExpand( m_tree_ctrl->GetSelection() ); // Expand library/part units subtree
880 else
881 postSelectEvent(); // Open symbol/footprint
882}
883
884
885void LIB_TREE::onDetailsLink( wxHtmlLinkEvent& aEvent )
886{
887 const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
888 wxString docname = info.GetHref();
889 PROJECT& prj = Pgm().GetSettingsManager().Prj();
890
891 GetAssociatedDocument( this, docname, &prj );
892}
893
894
895void LIB_TREE::onPreselect( wxCommandEvent& aEvent )
896{
897 hidePreview();
898
899 if( m_details_ctrl )
900 {
901 int unit = 0;
902 LIB_ID id = GetSelectedLibId( &unit );
903
904 if( id.IsValid() )
905 m_details_ctrl->SetPage( m_adapter->GenerateInfo( id, unit ) );
906 else
907 m_details_ctrl->SetPage( wxEmptyString );
908 }
909
910 aEvent.Skip();
911}
912
913
914void LIB_TREE::onItemContextMenu( wxDataViewEvent& aEvent )
915{
916 hidePreview();
917
919 {
920 m_skipNextRightClick = false;
921 return;
922 }
923
924 m_previewDisabled = true;
925
926 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
927 {
928 if( !GetCurrentTreeNode() )
929 {
930 wxPoint pos = m_tree_ctrl->ScreenToClient( wxGetMousePosition() );
931
932 wxDataViewItem item;
933 wxDataViewColumn* col;
934 m_tree_ctrl->HitTest( pos, item, col );
935
936 if( item.IsOk() )
937 {
938 m_tree_ctrl->SetFocus();
939 m_tree_ctrl->Select( item );
940 wxSafeYield();
941 }
942 }
943
944 tool->Activate();
945 tool->GetManager()->VetoContextMenuMouseWarp();
946 tool->GetToolMenu().ShowContextMenu();
947
949 tool->GetManager()->DispatchContextMenu( evt );
950 }
951 else
952 {
954
955 if( current && current->m_Type == LIB_TREE_NODE::TYPE::LIBRARY )
956 {
957 ACTION_MENU menu( true, nullptr );
958
959 if( current->m_Pinned )
960 {
962
963 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
964 m_adapter->UnpinLibrary( current );
965 }
966 else
967 {
968 menu.Add( ACTIONS::pinLibrary );
969
970 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
971 m_adapter->PinLibrary( current );
972 }
973 }
974 }
975
976 m_previewDisabled = false;
977}
978
979
980void LIB_TREE::onHeaderContextMenu( wxDataViewEvent& aEvent )
981{
982 hidePreview();
983 m_previewDisabled = true;
984
985 ACTION_MENU menu( true, nullptr );
986
988
989 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
990 {
991 EDA_REORDERABLE_LIST_DIALOG dlg( m_parent, _( "Select Columns" ),
992 m_adapter->GetAvailableColumns(),
993 m_adapter->GetShownColumns() );
994
995 if( dlg.ShowModal() == wxID_OK )
996 m_adapter->SetShownColumns( dlg.EnabledList() );
997 }
998
999 m_previewDisabled = false;
1000}
1001
1002
1003wxDEFINE_EVENT( EVT_LIBITEM_SELECTED, wxCommandEvent );
1004wxDEFINE_EVENT( EVT_LIBITEM_CHOSEN, wxCommandEvent );
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
static TOOL_ACTION pinLibrary
Definition: actions.h:152
static TOOL_ACTION selectLibTreeColumns
Definition: actions.h:224
static TOOL_ACTION unpinLibrary
Definition: actions.h:153
static TOOL_ACTION expandAll
Definition: actions.h:83
static TOOL_ACTION collapseAll
Definition: actions.h:84
Define 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.
int ShowModal() override
A dialog which allows selecting a list of items from a list of available items, and reordering those ...
const std::vector< wxString > & EnabledList()
Add dark theme support to wxHtmlWindow.
Definition: html_window.h:35
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:263
void RefreshLibTree()
Refresh the tree (mainly to update highlighting and asterisking)
Definition: lib_tree.cpp:440
wxPoint m_hoverPos
Definition: lib_tree.h:258
wxTimer * m_debounceTimer
Definition: lib_tree.h:249
void onHoverTimer(wxTimerEvent &aEvent)
Definition: lib_tree.cpp:808
LIB_TREE_NODE * GetCurrentTreeNode() const
Definition: lib_tree.cpp:332
void onQueryMouseMoved(wxMouseEvent &aEvent)
Definition: lib_tree.cpp:687
wxDataViewItem m_previewItem
Definition: lib_tree.h:262
HTML_WINDOW * m_details_ctrl
Definition: lib_tree.h:248
void onTreeActivate(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:871
wxString m_recentSearchesKey
Definition: lib_tree.h:252
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:914
void destroyPreview()
Definition: lib_tree.cpp:741
void ShowChangedLanguage()
Definition: lib_tree.cpp:291
WX_DATAVIEWCTRL * m_tree_ctrl
Definition: lib_tree.h:247
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:256
bool m_inTimerEvent
Definition: lib_tree.h:250
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:753
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:895
wxPopupWindow * m_previewWindow
Definition: lib_tree.h:264
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:732
void showPreview(wxDataViewItem aItem)
Definition: lib_tree.cpp:708
wxString GetSearchString() const
Definition: lib_tree.cpp:387
void onTreeSelect(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:859
int GetSelectedLibIds(std::vector< LIB_ID > &aSelection, std::vector< int > *aUnit=nullptr) const
Retrieve a list of selections for trees that allow multi-selection.
Definition: lib_tree.cpp:315
wxTimer m_hoverTimer
Definition: lib_tree.h:261
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:833
void onDetailsLink(wxHtmlLinkEvent &aEvent)
Definition: lib_tree.cpp:885
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:254
wxDataViewItem m_hoverItem
Definition: lib_tree.h:259
~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:265
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:245
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:260
STD_BITMAP_BUTTON * m_sort_ctrl
Definition: lib_tree.h:246
void onHeaderContextMenu(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:980
void Regenerate(bool aKeepState)
Regenerate the tree.
Definition: lib_tree.cpp:422
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > m_adapter
Definition: lib_tree.h:243
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:125
Container for project specific data.
Definition: project.h:64
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)
Generic, UI-independent tool event.
Definition: tool_event.h:168
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, EMBEDDED_FILES *aFiles)
Open a document (file) with the suitable browser.
Definition: eda_doc.cpp:62
This file is part of the common library.
#define HOVER_TIMER_MILLIS
Definition: lib_tree.cpp:705
constexpr int RECENT_SEARCHES_MAX
Definition: lib_tree.cpp:46
#define PREVIEW_SIZE
Definition: lib_tree.cpp:704
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:1073
see class PGM_BASE
Structure storing state of the symbol tree widget.
Definition: lib_tree.h:200
LIB_ID selection
Current selection, might be not valid if nothing was selected.
Definition: lib_tree.h:205
std::vector< wxDataViewItem > expanded
List of expanded nodes.
Definition: lib_tree.h:202
@ TA_MOUSE_CLICK
Definition: tool_event.h:67
@ TC_MOUSE
Definition: tool_event.h:55
@ MD_ALT
Definition: tool_event.h:145
@ MD_CTRL
Definition: tool_event.h:144
@ MD_SHIFT
Definition: tool_event.h:143
@ BUT_RIGHT
Definition: tool_event.h:133