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