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, LIB_TABLE* aLibTable,
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 return state;
602}
603
604
605void LIB_TREE::setState( const STATE& aState )
606{
607 m_tree_ctrl->Freeze();
608
609 for( const wxDataViewItem& item : aState.expanded )
610 m_tree_ctrl->Expand( item );
611
612 // wxDataViewCtrl cannot be frozen when a selection
613 // command is issued, otherwise it selects a random item (Windows)
614 m_tree_ctrl->Thaw();
615
616 if( !aState.selection.GetLibItemName().empty() || !aState.selection.GetLibNickname().empty() )
617 SelectLibId( aState.selection );
618}
619
620
621void LIB_TREE::onQueryText( wxCommandEvent& aEvent )
622{
623 m_debounceTimer->StartOnce( 200 );
624
625 // Required to avoid interaction with SetHint()
626 // See documentation for wxTextEntry::SetHint
627 aEvent.Skip();
628}
629
630
631void LIB_TREE::onDebounceTimer( wxTimerEvent& aEvent )
632{
633 m_inTimerEvent = true;
634 Regenerate( false );
635 m_inTimerEvent = false;
636}
637
638
639void LIB_TREE::onQueryCharHook( wxKeyEvent& aKeyStroke )
640{
641 int hotkey = aKeyStroke.GetKeyCode();
642
643 int mods = aKeyStroke.GetModifiers();
644
645 // the flag wxMOD_ALTGR is defined in wxWidgets as wxMOD_CONTROL|wxMOD_ALT
646 // So AltGr key cannot used as modifier key because it is the same as Alt key + Ctrl key.
647#if CAN_USE_ALTGR_KEY
648 if( wxmods & wxMOD_ALTGR )
649 mods |= MD_ALTGR;
650 else
651#endif
652 {
653 if( mods & wxMOD_CONTROL )
654 hotkey += MD_CTRL;
655
656 if( mods & wxMOD_ALT )
657 hotkey += MD_ALT;
658 }
659
660 if( mods & wxMOD_SHIFT )
661 hotkey += MD_SHIFT;
662
663#ifdef wxMOD_META
664 if( mods & wxMOD_META )
665 hotkey += MD_META;
666#endif
667
668#ifdef wxMOD_WIN
669 if( mods & wxMOD_WIN )
670 hotkey += MD_SUPER;
671#endif
672
673 if( hotkey == ACTIONS::expandAll.GetHotKey()
674 || hotkey == ACTIONS::expandAll.GetHotKeyAlt() )
675 {
676 m_tree_ctrl->ExpandAll();
677 return;
678 }
679 else if( hotkey == ACTIONS::collapseAll.GetHotKey()
680 || hotkey == ACTIONS::collapseAll.GetHotKeyAlt() )
681 {
682 m_tree_ctrl->CollapseAll();
683 return;
684 }
685
686 wxDataViewItem sel = m_tree_ctrl->GetSelection();
687
688 if( !sel.IsOk() )
689 sel = m_adapter->GetCurrentDataViewItem();
690
691 LIB_TREE_NODE::TYPE type = sel.IsOk() ? m_adapter->GetTypeFor( sel )
692 : LIB_TREE_NODE::TYPE::INVALID;
693
694 switch( aKeyStroke.GetKeyCode() )
695 {
696 case WXK_UP:
698 selectIfValid( m_tree_ctrl->GetPrevItem( sel ) );
699 break;
700
701 case WXK_DOWN:
703 selectIfValid( m_tree_ctrl->GetNextItem( sel ) );
704 break;
705
706 case WXK_ADD:
708
709 if( type == LIB_TREE_NODE::TYPE::LIBRARY )
710 m_tree_ctrl->Expand( sel );
711
712 break;
713
714 case WXK_SUBTRACT:
716
717 if( type == LIB_TREE_NODE::TYPE::LIBRARY )
718 m_tree_ctrl->Collapse( sel );
719
720 break;
721
722 case WXK_RETURN:
723 case WXK_NUMPAD_ENTER:
725
726 if( GetSelectedLibId().IsValid() )
728 else if( type == LIB_TREE_NODE::TYPE::LIBRARY )
729 toggleExpand( sel );
730
731 break;
732
733 default:
734 aKeyStroke.Skip(); // Any other key: pass on to search box directly.
735 break;
736 }
737}
738
739
740void LIB_TREE::onQueryMouseMoved( wxMouseEvent& aEvent )
741{
742#if defined( __WXOSX__ ) || wxCHECK_VERSION( 3, 3, 0 ) // Doesn't work properly on other ports
743 wxPoint pos = aEvent.GetPosition();
744 wxRect ctrlRect = m_query_ctrl->GetScreenRect();
745 int buttonWidth = ctrlRect.GetHeight(); // Presume buttons are square
746
747 if( m_query_ctrl->IsSearchButtonVisible() && pos.x < buttonWidth )
748 SetCursor( wxCURSOR_ARROW );
749 else if( m_query_ctrl->IsCancelButtonVisible() && pos.x > ctrlRect.GetWidth() - buttonWidth )
750 SetCursor( wxCURSOR_ARROW );
751 else
752 SetCursor( wxCURSOR_IBEAM );
753#endif
754}
755
756
757#define PREVIEW_SIZE wxSize( 240, 200 )
758#define HOVER_TIMER_MILLIS 400
759
760
761void LIB_TREE::showPreview( wxDataViewItem aItem )
762{
763 if( aItem.IsOk() && m_adapter->HasPreview( aItem ) )
764 {
765 m_previewItem = aItem;
767
768 wxWindow* topLevelParent = wxGetTopLevelParent( m_parent );
769
770 if( !m_previewWindow )
771 m_previewWindow = new wxPopupWindow( topLevelParent );
772
773 m_previewWindow->SetSize( PREVIEW_SIZE );
774
775 m_adapter->ShowPreview( m_previewWindow, aItem );
776
777 m_previewWindow->SetPosition( wxPoint( m_tree_ctrl->GetScreenRect().GetRight() - 10,
778 wxGetMousePosition().y - PREVIEW_SIZE.y / 2 ) );
779
780 m_previewWindow->Show();
781 }
782}
783
784
786{
787 m_previewItem = wxDataViewItem();
788
789 if( m_previewWindow )
790 m_previewWindow->Hide();
791}
792
793
794void LIB_TREE::onIdle( wxIdleEvent& aEvent )
795{
796 // The wxDataViewCtrl won't give us its mouseMoved events so we're forced to use idle
797 // events to track the hover state
798
799 // The dang thing won't give us scroll events either, so we implement a poor-man's
800 // scroll-checker using the last-known positions of the preview or hover items.
801
802 wxWindow* topLevelParent = wxGetTopLevelParent( m_parent );
803 wxWindow* topLevelFocus = wxGetTopLevelParent( wxWindow::FindFocus() );
804
805 bool mouseOverWindow = false;
806 wxPoint screenPos = wxGetMousePosition();
807
808 if( m_tree_ctrl->IsShownOnScreen() )
809 mouseOverWindow |= m_tree_ctrl->GetScreenRect().Contains( screenPos );
810
811 if( m_previewDisabled || topLevelFocus != topLevelParent || !mouseOverWindow )
812 {
813 m_hoverTimer.Stop();
814 hidePreview();
815 return;
816 }
817
818 wxPoint clientPos = m_tree_ctrl->ScreenToClient( screenPos );
819 wxDataViewItem item;
820 wxDataViewColumn* col = nullptr;
821
822 m_tree_ctrl->HitTest( clientPos, item, col );
823
824 if( m_previewItem.IsOk() )
825 {
826 if( item != m_previewItem )
827 {
828#ifdef __WXGTK__
829 // Hide the preview, because Wayland can't move windows.
830 if( wxGetDisplayInfo().type == wxDisplayType::wxDisplayWayland )
831 hidePreview();
832#endif
833 showPreview( item );
834 }
835
836 return;
837 }
838
839 if( m_hoverPos != clientPos )
840 {
841 m_hoverPos = clientPos;
842 m_hoverItem = item;
843 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
844 m_hoverTimer.StartOnce( HOVER_TIMER_MILLIS );
845 }
846}
847
848
849void LIB_TREE::onHoverTimer( wxTimerEvent& aEvent )
850{
851 hidePreview();
852
853 if( !m_tree_ctrl->IsShownOnScreen() || m_previewDisabled )
854 return;
855
856 wxDataViewItem item;
857 wxDataViewColumn* col = nullptr;
858 m_tree_ctrl->HitTest( m_hoverPos, item, col );
859
860 if( item == m_hoverItem && m_tree_ctrl->GetItemRect( item ) == m_hoverItemRect )
861 {
862 if( item != m_tree_ctrl->GetSelection() )
863 showPreview( item );
864 }
865 else // view must have been scrolled
866 {
867 m_hoverItem = item;
868 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
869 m_hoverTimer.StartOnce( HOVER_TIMER_MILLIS );
870 }
871}
872
873
874void LIB_TREE::onTreeCharHook( wxKeyEvent& aKeyStroke )
875{
876 onQueryCharHook( aKeyStroke );
877
878 if( aKeyStroke.GetSkipped() )
879 {
880 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
881 {
882 int hotkey = aKeyStroke.GetKeyCode();
883
884 int mods = aKeyStroke.GetModifiers();
885
886 if( mods & wxMOD_ALTGR )
887 hotkey |= MD_ALTGR;
888 else
889 {
890 if( mods & wxMOD_ALT )
891 hotkey |= MD_ALT;
892
893 if( mods & wxMOD_CONTROL )
894 hotkey |= MD_CTRL;
895 }
896
897 if( mods & wxMOD_SHIFT )
898 hotkey |= MD_SHIFT;
899
900#ifdef wxMOD_META
901 if( mods & wxMOD_META )
902 hotkey |= MD_META;
903#endif
904
905#ifdef wxMOD_WIN
906 if( mods & wxMOD_WIN )
907 hotkey |= MD_SUPER;
908#endif
909
910 if( tool->GetManager()->GetActionManager()->RunHotKey( hotkey ) )
911 aKeyStroke.Skip( false );
912 }
913 }
914}
915
916
917void LIB_TREE::onTreeSelect( wxDataViewEvent& aEvent )
918{
919 if( m_tree_ctrl->IsFrozen() )
920 return;
921
922 if( !m_inTimerEvent )
924
926}
927
928
929void LIB_TREE::onTreeActivate( wxDataViewEvent& aEvent )
930{
931 hidePreview();
932
933 if( !m_inTimerEvent )
935
936 if( !GetSelectedLibId().IsValid() )
937 toggleExpand( m_tree_ctrl->GetSelection() ); // Expand library/part units subtree
938 else
939 postSelectEvent(); // Open symbol/footprint
940}
941
942
943void LIB_TREE::onDetailsLink( wxHtmlLinkEvent& aEvent )
944{
945 const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
946 wxString docname = info.GetHref();
947 PROJECT& prj = Pgm().GetSettingsManager().Prj();
948
949 GetAssociatedDocument( this, docname, &prj );
950}
951
952
953void LIB_TREE::onPreselect( wxCommandEvent& aEvent )
954{
955 hidePreview();
956
957 if( m_details_ctrl )
958 {
959 int unit = 0;
960 LIB_ID id = GetSelectedLibId( &unit );
961
962 if( id.IsValid() )
963 m_details_ctrl->SetPage( m_adapter->GenerateInfo( id, unit ) );
964 else
965 m_details_ctrl->SetPage( wxEmptyString );
966 }
967
968 aEvent.Skip();
969}
970
971
972void LIB_TREE::onItemContextMenu( wxDataViewEvent& aEvent )
973{
974 hidePreview();
975
977 {
978 m_skipNextRightClick = false;
979 return;
980 }
981
982 m_previewDisabled = true;
983
984 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
985 {
986 if( !GetCurrentTreeNode() )
987 {
988 wxPoint pos = m_tree_ctrl->ScreenToClient( wxGetMousePosition() );
989
990 wxDataViewItem item;
991 wxDataViewColumn* col;
992 m_tree_ctrl->HitTest( pos, item, col );
993
994 if( item.IsOk() )
995 {
996 m_tree_ctrl->SetFocus();
997 m_tree_ctrl->Select( item );
998 wxSafeYield();
999 }
1000 }
1001
1002 tool->Activate();
1003 tool->GetManager()->VetoContextMenuMouseWarp();
1004 tool->GetToolMenu().ShowContextMenu();
1005
1007 tool->GetManager()->DispatchContextMenu( evt );
1008 }
1009 else
1010 {
1011 LIB_TREE_NODE* current = GetCurrentTreeNode();
1012
1013 if( current && current->m_Type == LIB_TREE_NODE::TYPE::LIBRARY )
1014 {
1015 ACTION_MENU menu( true, nullptr );
1016
1017 if( current->m_Pinned )
1018 {
1019 menu.Add( ACTIONS::unpinLibrary );
1020
1021 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
1022 m_adapter->UnpinLibrary( current );
1023 }
1024 else
1025 {
1026 menu.Add( ACTIONS::pinLibrary );
1027
1028 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
1029 m_adapter->PinLibrary( current );
1030 }
1031 }
1032 }
1033
1034 m_previewDisabled = false;
1035}
1036
1037
1038void LIB_TREE::onHeaderContextMenu( wxDataViewEvent& aEvent )
1039{
1040 hidePreview();
1041 m_previewDisabled = true;
1042
1043 ACTION_MENU menu( true, nullptr );
1044
1046
1047 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
1048 {
1049 EDA_REORDERABLE_LIST_DIALOG dlg( m_parent, _( "Select Columns" ),
1050 m_adapter->GetAvailableColumns(),
1051 m_adapter->GetShownColumns() );
1052
1053 if( dlg.ShowModal() == wxID_OK )
1054 {
1055 m_adapter->SetShownColumns( dlg.EnabledList() );
1056 Regenerate( true );
1057 }
1058 }
1059
1060 m_previewDisabled = false;
1061}
1062
1063
1064wxDEFINE_EVENT( EVT_LIBITEM_SELECTED, wxCommandEvent );
1065wxDEFINE_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
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.
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:53
wxRect m_previewItemRect
Definition lib_tree.h:277
void RefreshLibTree()
Refresh the tree (mainly to update highlighting and asterisking)
Definition lib_tree.cpp:472
wxPoint m_hoverPos
Definition lib_tree.h:272
wxTimer * m_debounceTimer
Definition lib_tree.h:263
void onHoverTimer(wxTimerEvent &aEvent)
Definition lib_tree.cpp:849
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:740
wxDataViewItem m_previewItem
Definition lib_tree.h:276
HTML_WINDOW * m_details_ctrl
Definition lib_tree.h:262
void onTreeActivate(wxDataViewEvent &aEvent)
Definition lib_tree.cpp:929
wxString m_recentSearchesKey
Definition lib_tree.h:266
void onQueryCharHook(wxKeyEvent &aEvent)
Definition lib_tree.cpp:639
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:972
void ShowChangedLanguage()
Definition lib_tree.cpp:304
WX_DATAVIEWCTRL * m_tree_ctrl
Definition lib_tree.h:261
void onQueryText(wxCommandEvent &aEvent)
Definition lib_tree.cpp:621
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:270
bool m_inTimerEvent
Definition lib_tree.h:264
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:794
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:953
wxPopupWindow * m_previewWindow
Definition lib_tree.h:278
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:785
void showPreview(wxDataViewItem aItem)
Definition lib_tree.cpp:761
wxString GetSearchString() const
Definition lib_tree.cpp:419
void onTreeSelect(wxDataViewEvent &aEvent)
Definition lib_tree.cpp:917
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:275
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:260
void onTreeCharHook(wxKeyEvent &aEvent)
Definition lib_tree.cpp:874
void onDetailsLink(wxHtmlLinkEvent &aEvent)
Definition lib_tree.cpp:943
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:59
@ FILTERS
Definition lib_tree.h:56
@ DETAILS
Definition lib_tree.h:57
void ExpandAll()
Definition lib_tree.cpp:401
wxBoxSizer * m_filtersSizer
Definition lib_tree.h:268
wxDataViewItem m_hoverItem
Definition lib_tree.h:273
~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:605
STATE getState() const
Return the symbol tree widget state.
Definition lib_tree.cpp:587
bool m_previewDisabled
Definition lib_tree.h:279
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:259
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:631
wxRect m_hoverItemRect
Definition lib_tree.h:274
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:257
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:125
Container for project specific data.
Definition project.h:65
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:758
constexpr int RECENT_SEARCHES_MAX
Definition lib_tree.cpp:48
#define PREVIEW_SIZE
Definition lib_tree.cpp:757
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:913
see class PGM_BASE
Structure storing state of the symbol tree widget.
Definition lib_tree.h:215
LIB_ID selection
Current selection, might be not valid if nothing was selected.
Definition lib_tree.h:220
std::vector< wxDataViewItem > expanded
List of expanded nodes.
Definition lib_tree.h:217
@ 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