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