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