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 );
92 m_sort_ctrl->SetBitmap( KiBitmapBundle( BITMAPS::config ) );
93 m_sort_ctrl->Bind( wxEVT_LEFT_DOWN,
94 [&]( wxMouseEvent& aEvent )
95 {
96 // Build a pop menu:
97 wxMenu menu;
98
99 menu.Append( 4201, _( "Sort by Best Match" ), wxEmptyString, wxITEM_CHECK );
100 menu.Append( 4202, _( "Sort Alphabetically" ), wxEmptyString, wxITEM_CHECK );
101 menu.AppendSeparator();
102 menu.Append( 4203, ACTIONS::expandAll.GetMenuItem() );
103 menu.Append( 4204, ACTIONS::collapseAll.GetMenuItem() );
104
105 if( m_adapter->GetSortMode() == LIB_TREE_MODEL_ADAPTER::BEST_MATCH )
106 menu.Check( 4201, true );
107 else
108 menu.Check( 4202, true );
109
110 // menu_id is the selected submenu id from the popup menu or wxID_NONE
111 int menu_id = m_sort_ctrl->GetPopupMenuSelectionFromUser( menu );
112
113 if( menu_id == 0 || menu_id == 4201 )
114 {
115 m_adapter->SetSortMode( LIB_TREE_MODEL_ADAPTER::BEST_MATCH );
116 Regenerate( true );
117 }
118 else if( menu_id == 1 || menu_id == 4202 )
119 {
120 m_adapter->SetSortMode( LIB_TREE_MODEL_ADAPTER::ALPHABETIC );
121 Regenerate( true );
122 }
123 else if( menu_id == 3 || menu_id == 4203 )
124 {
125 ExpandAll();
126 }
127 else if( menu_id == 4 || menu_id == 4204 )
128 {
129 CollapseAll();
130 }
131 } );
132
133 m_sort_ctrl->Bind( wxEVT_CHAR_HOOK, &LIB_TREE::onTreeCharHook, this );
134 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{
404}
405
406
408{
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 if( aKeyStroke.GetModifiers() & wxMOD_CONTROL )
644 hotkey += MD_CTRL;
645
646 if( aKeyStroke.GetModifiers() & wxMOD_ALT )
647 hotkey += MD_ALT;
648
649 if( aKeyStroke.GetModifiers() & wxMOD_SHIFT )
650 hotkey += MD_SHIFT;
651
652 if( hotkey == ACTIONS::expandAll.GetHotKey()
653 || hotkey == ACTIONS::expandAll.GetHotKeyAlt() )
654 {
656 return;
657 }
658 else if( hotkey == ACTIONS::collapseAll.GetHotKey()
659 || hotkey == ACTIONS::collapseAll.GetHotKeyAlt() )
660 {
662 return;
663 }
664
665 wxDataViewItem sel = m_tree_ctrl->GetSelection();
666
667 if( !sel.IsOk() )
668 sel = m_adapter->GetCurrentDataViewItem();
669
670 LIB_TREE_NODE::TYPE type = sel.IsOk() ? m_adapter->GetTypeFor( sel )
671 : LIB_TREE_NODE::TYPE::INVALID;
672
673 switch( aKeyStroke.GetKeyCode() )
674 {
675 case WXK_UP:
678 break;
679
680 case WXK_DOWN:
683 break;
684
685 case WXK_ADD:
687
688 if( type == LIB_TREE_NODE::TYPE::LIBRARY )
689 m_tree_ctrl->Expand( sel );
690
691 break;
692
693 case WXK_SUBTRACT:
695
696 if( type == LIB_TREE_NODE::TYPE::LIBRARY )
697 m_tree_ctrl->Collapse( sel );
698
699 break;
700
701 case WXK_RETURN:
702 case WXK_NUMPAD_ENTER:
704
705 if( GetSelectedLibId().IsValid() )
707 else if( type == LIB_TREE_NODE::TYPE::LIBRARY )
708 toggleExpand( sel );
709
710 break;
711
712 default:
713 aKeyStroke.Skip(); // Any other key: pass on to search box directly.
714 break;
715 }
716}
717
718
719void LIB_TREE::onQueryMouseMoved( wxMouseEvent& aEvent )
720{
721#if defined( __WXOSX__ ) || wxCHECK_VERSION( 3, 3, 0 ) // Doesn't work properly on other ports
722 wxPoint pos = aEvent.GetPosition();
723 wxRect ctrlRect = m_query_ctrl->GetScreenRect();
724 int buttonWidth = ctrlRect.GetHeight(); // Presume buttons are square
725
726 if( m_query_ctrl->IsSearchButtonVisible() && pos.x < buttonWidth )
727 SetCursor( wxCURSOR_ARROW );
728 else if( m_query_ctrl->IsCancelButtonVisible() && pos.x > ctrlRect.GetWidth() - buttonWidth )
729 SetCursor( wxCURSOR_ARROW );
730 else
731 SetCursor( wxCURSOR_IBEAM );
732#endif
733}
734
735
736#define PREVIEW_SIZE wxSize( 240, 200 )
737#define HOVER_TIMER_MILLIS 400
738
739
740void LIB_TREE::showPreview( wxDataViewItem aItem )
741{
742 if( aItem.IsOk() && m_adapter->HasPreview( aItem ) )
743 {
744 m_previewItem = aItem;
746
747 wxWindow* topLevelParent = wxGetTopLevelParent( m_parent );
748
749 if( !m_previewWindow )
750 m_previewWindow = new wxPopupWindow( topLevelParent );
751
752 m_previewWindow->SetSize( PREVIEW_SIZE );
753
754 m_adapter->ShowPreview( m_previewWindow, aItem );
755
756 m_previewWindow->SetPosition( wxPoint( m_tree_ctrl->GetScreenRect().GetRight() - 10,
757 wxGetMousePosition().y - PREVIEW_SIZE.y / 2 ) );
758
759 m_previewWindow->Show();
760 }
761}
762
763
765{
766 m_previewItem = wxDataViewItem();
767
768 if( m_previewWindow )
769 m_previewWindow->Hide();
770}
771
772
773void LIB_TREE::onIdle( wxIdleEvent& aEvent )
774{
775 // The wxDataViewCtrl won't give us its mouseMoved events so we're forced to use idle
776 // events to track the hover state
777
778 // The dang thing won't give us scroll events either, so we implement a poor-man's
779 // scroll-checker using the last-known positions of the preview or hover items.
780
781 wxWindow* topLevelParent = wxGetTopLevelParent( m_parent );
782 wxWindow* topLevelFocus = wxGetTopLevelParent( wxWindow::FindFocus() );
783
784 bool mouseOverWindow = false;
785 wxPoint screenPos = wxGetMousePosition();
786
787 if( m_tree_ctrl->IsShownOnScreen() )
788 mouseOverWindow |= m_tree_ctrl->GetScreenRect().Contains( screenPos );
789
790 if( m_previewDisabled || topLevelFocus != topLevelParent || !mouseOverWindow )
791 {
792 m_hoverTimer.Stop();
793 hidePreview();
794 return;
795 }
796
797 wxPoint clientPos = m_tree_ctrl->ScreenToClient( screenPos );
798 wxDataViewItem item;
799 wxDataViewColumn* col = nullptr;
800
801 m_tree_ctrl->HitTest( clientPos, item, col );
802
803 if( m_previewItem.IsOk() )
804 {
805 if( item != m_previewItem )
806 {
807#ifdef __WXGTK__
808 // Hide the preview, because Wayland can't move windows.
809 if( wxGetDisplayInfo().type == wxDisplayType::wxDisplayWayland )
810 hidePreview();
811#endif
812 showPreview( item );
813 }
814
815 return;
816 }
817
818 if( m_hoverPos != clientPos )
819 {
820 m_hoverPos = clientPos;
821 m_hoverItem = item;
822 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
823 m_hoverTimer.StartOnce( HOVER_TIMER_MILLIS );
824 }
825}
826
827
828void LIB_TREE::onHoverTimer( wxTimerEvent& aEvent )
829{
830 hidePreview();
831
832 if( !m_tree_ctrl->IsShownOnScreen() || m_previewDisabled )
833 return;
834
835 wxDataViewItem item;
836 wxDataViewColumn* col = nullptr;
837 m_tree_ctrl->HitTest( m_hoverPos, item, col );
838
839 if( item == m_hoverItem && m_tree_ctrl->GetItemRect( item ) == m_hoverItemRect )
840 {
841 if( item != m_tree_ctrl->GetSelection() )
842 showPreview( item );
843 }
844 else // view must have been scrolled
845 {
846 m_hoverItem = item;
847 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
848 m_hoverTimer.StartOnce( HOVER_TIMER_MILLIS );
849 }
850}
851
852
853void LIB_TREE::onTreeCharHook( wxKeyEvent& aKeyStroke )
854{
855 onQueryCharHook( aKeyStroke );
856
857 if( aKeyStroke.GetSkipped() )
858 {
859 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
860 {
861 int hotkey = aKeyStroke.GetKeyCode();
862
863 if( aKeyStroke.ShiftDown() )
864 hotkey |= MD_SHIFT;
865
866 if( aKeyStroke.AltDown() )
867 hotkey |= MD_ALT;
868
869 if( aKeyStroke.ControlDown() )
870 hotkey |= MD_CTRL;
871
872 if( tool->GetManager()->GetActionManager()->RunHotKey( hotkey ) )
873 aKeyStroke.Skip( false );
874 }
875 }
876}
877
878
879void LIB_TREE::onTreeSelect( wxDataViewEvent& aEvent )
880{
881 if( m_tree_ctrl->IsFrozen() )
882 return;
883
884 if( !m_inTimerEvent )
886
888}
889
890
891void LIB_TREE::onTreeActivate( wxDataViewEvent& aEvent )
892{
893 hidePreview();
894
895 if( !m_inTimerEvent )
897
898 if( !GetSelectedLibId().IsValid() )
899 toggleExpand( m_tree_ctrl->GetSelection() ); // Expand library/part units subtree
900 else
901 postSelectEvent(); // Open symbol/footprint
902}
903
904
905void LIB_TREE::onDetailsLink( wxHtmlLinkEvent& aEvent )
906{
907 const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
908 wxString docname = info.GetHref();
909 PROJECT& prj = Pgm().GetSettingsManager().Prj();
910
911 GetAssociatedDocument( this, docname, &prj );
912}
913
914
915void LIB_TREE::onPreselect( wxCommandEvent& aEvent )
916{
917 hidePreview();
918
919 if( m_details_ctrl )
920 {
921 int unit = 0;
922 LIB_ID id = GetSelectedLibId( &unit );
923
924 if( id.IsValid() )
925 m_details_ctrl->SetPage( m_adapter->GenerateInfo( id, unit ) );
926 else
927 m_details_ctrl->SetPage( wxEmptyString );
928 }
929
930 aEvent.Skip();
931}
932
933
934void LIB_TREE::onItemContextMenu( wxDataViewEvent& aEvent )
935{
936 hidePreview();
937
939 {
940 m_skipNextRightClick = false;
941 return;
942 }
943
944 m_previewDisabled = true;
945
946 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
947 {
948 if( !GetCurrentTreeNode() )
949 {
950 wxPoint pos = m_tree_ctrl->ScreenToClient( wxGetMousePosition() );
951
952 wxDataViewItem item;
953 wxDataViewColumn* col;
954 m_tree_ctrl->HitTest( pos, item, col );
955
956 if( item.IsOk() )
957 {
958 m_tree_ctrl->SetFocus();
959 m_tree_ctrl->Select( item );
960 wxSafeYield();
961 }
962 }
963
964 tool->Activate();
965 tool->GetManager()->VetoContextMenuMouseWarp();
966 tool->GetToolMenu().ShowContextMenu();
967
969 tool->GetManager()->DispatchContextMenu( evt );
970 }
971 else
972 {
974
975 if( current && current->m_Type == LIB_TREE_NODE::TYPE::LIBRARY )
976 {
977 ACTION_MENU menu( true, nullptr );
978
979 if( current->m_Pinned )
980 {
982
983 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
984 m_adapter->UnpinLibrary( current );
985 }
986 else
987 {
988 menu.Add( ACTIONS::pinLibrary );
989
990 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
991 m_adapter->PinLibrary( current );
992 }
993 }
994 }
995
996 m_previewDisabled = false;
997}
998
999
1000void LIB_TREE::onHeaderContextMenu( wxDataViewEvent& aEvent )
1001{
1002 hidePreview();
1003 m_previewDisabled = true;
1004
1005 ACTION_MENU menu( true, nullptr );
1006
1008
1009 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
1010 {
1011 EDA_REORDERABLE_LIST_DIALOG dlg( m_parent, _( "Select Columns" ),
1012 m_adapter->GetAvailableColumns(),
1013 m_adapter->GetShownColumns() );
1014
1015 if( dlg.ShowModal() == wxID_OK )
1016 {
1017 m_adapter->SetShownColumns( dlg.EnabledList() );
1018 Regenerate( true );
1019 }
1020 }
1021
1022 m_previewDisabled = false;
1023}
1024
1025
1026wxDEFINE_EVENT( EVT_LIBITEM_SELECTED, wxCommandEvent );
1027wxDEFINE_EVENT( EVT_LIBITEM_CHOSEN, wxCommandEvent );
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition: bitmap.cpp:110
static TOOL_ACTION pinLibrary
Definition: actions.h:159
static TOOL_ACTION selectLibTreeColumns
Definition: actions.h:274
static TOOL_ACTION unpinLibrary
Definition: actions.h:160
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:49
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.
Definition: bitmap_button.h:42
void SetBitmap(const wxBitmapBundle &aBmp)
Set the bitmap shown when the button is enabled.
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: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:828
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:719
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:891
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:934
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:773
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:915
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:764
void showPreview(wxDataViewItem aItem)
Definition: lib_tree.cpp:740
wxString GetSearchString() const
Definition: lib_tree.cpp:419
void onTreeSelect(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:879
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:853
void onDetailsLink(wxHtmlLinkEvent &aEvent)
Definition: lib_tree.cpp:905
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
@ SEARCH
Definition: lib_tree.h:55
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)
Definition: lib_tree.cpp:1000
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:168
bool empty() const
Definition: utf8.h:110
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, 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:737
constexpr int RECENT_SEARCHES_MAX
Definition: lib_tree.cpp:48
#define PREVIEW_SIZE
Definition: lib_tree.cpp:736
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:902
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_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