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/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
348
349void LIB_TREE::SelectLibId( const LIB_ID& aLibId )
350{
351 selectIfValid( m_adapter->FindItem( aLibId ) );
352}
353
354
355void LIB_TREE::CenterLibId( const LIB_ID& aLibId )
356{
357 centerIfValid( m_adapter->FindItem( aLibId ) );
358}
359
360
362{
363 m_tree_ctrl->Freeze();
364 m_tree_ctrl->UnselectAll();
365 m_tree_ctrl->Thaw();
366}
367
368
369void LIB_TREE::ExpandLibId( const LIB_ID& aLibId )
370{
371 expandIfValid( m_adapter->FindItem( aLibId ) );
372}
373
374
376{
378}
379
380
382{
384}
385
386
387void LIB_TREE::SetSearchString( const wxString& aSearchString )
388{
389 m_query_ctrl->ChangeValue( aSearchString );
390}
391
392
394{
395 return m_query_ctrl->GetValue();
396}
397
398
400{
401 wxString newEntry = GetSearchString();
402
403 std::vector<wxString>& recents = g_recentSearches[ m_recentSearchesKey ];
404
405 if( !newEntry.IsEmpty() )
406 {
407 if( alg::contains( recents, newEntry ) )
408 alg::delete_matching( recents, newEntry );
409
410 if( recents.size() >= RECENT_SEARCHES_MAX )
411 recents.pop_back();
412
413 recents.insert( recents.begin(), newEntry );
414 }
415
416 wxMenu* menu = new wxMenu();
417
418 for( const wxString& recent : recents )
419 menu->Append( menu->GetMenuItemCount() + 1, recent );
420
421 if( recents.empty() )
422 menu->Append( wxID_ANY, _( "recent searches" ) );
423
424 m_query_ctrl->SetMenu( menu );
425}
426
427
428void LIB_TREE::Regenerate( bool aKeepState )
429{
430 STATE current;
431
432 // Store the state
433 if( aKeepState )
434 current = getState();
435
436 wxString filter = m_query_ctrl->GetValue();
437 m_adapter->UpdateSearchString( filter, aKeepState );
439
440 // Restore the state
441 if( aKeepState )
442 setState( current );
443}
444
445
447{
448 m_adapter->RefreshTree();
449}
450
451
453{
454 if( m_query_ctrl )
455 return m_query_ctrl;
456 else
457 return m_tree_ctrl;
458}
459
460
462{
463 if( m_query_ctrl )
464 m_query_ctrl->SetFocus();
465}
466
467
468void LIB_TREE::toggleExpand( const wxDataViewItem& aTreeId )
469{
470 if( !aTreeId.IsOk() )
471 return;
472
473 if( m_tree_ctrl->IsExpanded( aTreeId ) )
474 m_tree_ctrl->Collapse( aTreeId );
475 else
476 m_tree_ctrl->Expand( aTreeId );
477}
478
479
480void LIB_TREE::selectIfValid( const wxDataViewItem& aTreeId )
481{
482 if( aTreeId.IsOk() )
483 {
484 m_tree_ctrl->EnsureVisible( aTreeId );
485 m_tree_ctrl->UnselectAll();
486 m_tree_ctrl->Select( aTreeId );
488 }
489}
490
491
492void LIB_TREE::centerIfValid( const wxDataViewItem& aTreeId )
493{
494 /*
495 * This doesn't actually center because the wxWidgets API is poorly suited to that (and
496 * it might be too noisy as well).
497 *
498 * It does try to keep the given item a bit off the top or bottom of the window.
499 */
500
501 if( aTreeId.IsOk() )
502 {
503 LIB_TREE_NODE* node = m_adapter->GetTreeNodeFor( aTreeId );
504 LIB_TREE_NODE* parent = node->m_Parent;
505 LIB_TREE_NODE* grandParent = parent ? parent->m_Parent : nullptr;
506
507 if( parent )
508 {
509 wxDataViewItemArray siblings;
510 m_adapter->GetChildren( wxDataViewItem( parent ), siblings );
511
512 int idx = siblings.Index( aTreeId );
513
514 if( idx + 5 < (int) siblings.GetCount() )
515 {
516 m_tree_ctrl->EnsureVisible( siblings.Item( idx + 5 ) );
517 }
518 else if( grandParent )
519 {
520 wxDataViewItemArray parentsSiblings;
521 m_adapter->GetChildren( wxDataViewItem( grandParent ), parentsSiblings );
522
523 int p_idx = parentsSiblings.Index( wxDataViewItem( parent ) );
524
525 if( p_idx + 1 < (int) parentsSiblings.GetCount() )
526 m_tree_ctrl->EnsureVisible( parentsSiblings.Item( p_idx + 1 ) );
527 }
528
529 if( idx - 5 >= 0 )
530 m_tree_ctrl->EnsureVisible( siblings.Item( idx - 5 ) );
531 else
532 m_tree_ctrl->EnsureVisible( wxDataViewItem( parent ) );
533 }
534
535 m_tree_ctrl->EnsureVisible( aTreeId );
536 }
537}
538
539
540void LIB_TREE::expandIfValid( const wxDataViewItem& aTreeId )
541{
542 if( aTreeId.IsOk() && !m_tree_ctrl->IsExpanded( aTreeId ) )
543 m_tree_ctrl->Expand( aTreeId );
544}
545
546
548{
549 wxCommandEvent event( EVT_LIBITEM_SELECTED );
550 wxPostEvent( this, event );
551}
552
553
555{
556 wxCommandEvent event( EVT_LIBITEM_CHOSEN );
557 wxPostEvent( this, event );
558}
559
560
562{
563 STATE state;
564 wxDataViewItemArray items;
565 m_adapter->GetChildren( wxDataViewItem( nullptr ), items );
566
567 for( const wxDataViewItem& item : items )
568 {
569 if( m_tree_ctrl->IsExpanded( item ) )
570 state.expanded.push_back( item );
571 }
572
573 state.selection = GetSelectedLibId();
574
575 return state;
576}
577
578
579void LIB_TREE::setState( const STATE& aState )
580{
581 m_tree_ctrl->Freeze();
582
583 for( const wxDataViewItem& item : aState.expanded )
584 m_tree_ctrl->Expand( item );
585
586 // wxDataViewCtrl cannot be frozen when a selection
587 // command is issued, otherwise it selects a random item (Windows)
588 m_tree_ctrl->Thaw();
589
590 if( !aState.selection.GetLibItemName().empty() || !aState.selection.GetLibNickname().empty() )
591 SelectLibId( aState.selection );
592}
593
594
595void LIB_TREE::onQueryText( wxCommandEvent& aEvent )
596{
597 m_debounceTimer->StartOnce( 200 );
598
599 // Required to avoid interaction with SetHint()
600 // See documentation for wxTextEntry::SetHint
601 aEvent.Skip();
602}
603
604
605void LIB_TREE::onDebounceTimer( wxTimerEvent& aEvent )
606{
607 m_inTimerEvent = true;
608 Regenerate( false );
609 m_inTimerEvent = false;
610}
611
612
613void LIB_TREE::onQueryCharHook( wxKeyEvent& aKeyStroke )
614{
615 int hotkey = aKeyStroke.GetKeyCode();
616
617 if( aKeyStroke.GetModifiers() & wxMOD_CONTROL )
618 hotkey += MD_CTRL;
619
620 if( aKeyStroke.GetModifiers() & wxMOD_ALT )
621 hotkey += MD_ALT;
622
623 if( aKeyStroke.GetModifiers() & wxMOD_SHIFT )
624 hotkey += MD_SHIFT;
625
626 if( hotkey == ACTIONS::expandAll.GetHotKey()
627 || hotkey == ACTIONS::expandAll.GetHotKeyAlt() )
628 {
630 return;
631 }
632 else if( hotkey == ACTIONS::collapseAll.GetHotKey()
633 || hotkey == ACTIONS::collapseAll.GetHotKeyAlt() )
634 {
636 return;
637 }
638
639 wxDataViewItem sel = m_tree_ctrl->GetSelection();
640
641 if( !sel.IsOk() )
642 sel = m_adapter->GetCurrentDataViewItem();
643
644 LIB_TREE_NODE::TYPE type = sel.IsOk() ? m_adapter->GetTypeFor( sel )
645 : LIB_TREE_NODE::TYPE::INVALID;
646
647 switch( aKeyStroke.GetKeyCode() )
648 {
649 case WXK_UP:
652 break;
653
654 case WXK_DOWN:
657 break;
658
659 case WXK_ADD:
661
662 if( type == LIB_TREE_NODE::TYPE::LIBRARY )
663 m_tree_ctrl->Expand( sel );
664
665 break;
666
667 case WXK_SUBTRACT:
669
670 if( type == LIB_TREE_NODE::TYPE::LIBRARY )
671 m_tree_ctrl->Collapse( sel );
672
673 break;
674
675 case WXK_RETURN:
676 case WXK_NUMPAD_ENTER:
678
679 if( GetSelectedLibId().IsValid() )
681 else if( type == LIB_TREE_NODE::TYPE::LIBRARY )
682 toggleExpand( sel );
683
684 break;
685
686 default:
687 aKeyStroke.Skip(); // Any other key: pass on to search box directly.
688 break;
689 }
690}
691
692
693void LIB_TREE::onQueryMouseMoved( wxMouseEvent& aEvent )
694{
695#if defined( __WXOSX__ ) || wxCHECK_VERSION( 3, 3, 0 ) // Doesn't work properly on other ports
696 wxPoint pos = aEvent.GetPosition();
697 wxRect ctrlRect = m_query_ctrl->GetScreenRect();
698 int buttonWidth = ctrlRect.GetHeight(); // Presume buttons are square
699
700 if( m_query_ctrl->IsSearchButtonVisible() && pos.x < buttonWidth )
701 SetCursor( wxCURSOR_ARROW );
702 else if( m_query_ctrl->IsCancelButtonVisible() && pos.x > ctrlRect.GetWidth() - buttonWidth )
703 SetCursor( wxCURSOR_ARROW );
704 else
705 SetCursor( wxCURSOR_IBEAM );
706#endif
707}
708
709
710#define PREVIEW_SIZE wxSize( 240, 200 )
711#define HOVER_TIMER_MILLIS 400
712
713
714void LIB_TREE::showPreview( wxDataViewItem aItem )
715{
716 if( aItem.IsOk() && m_adapter->HasPreview( aItem ) )
717 {
718 m_previewItem = aItem;
720
721 wxWindow* topLevelParent = wxGetTopLevelParent( m_parent );
722
723 if( !m_previewWindow )
724 m_previewWindow = new wxPopupWindow( topLevelParent );
725
726 m_previewWindow->SetSize( PREVIEW_SIZE );
727
728 m_adapter->ShowPreview( m_previewWindow, aItem );
729
730 m_previewWindow->SetPosition( wxPoint( m_tree_ctrl->GetScreenRect().GetRight() - 10,
731 wxGetMousePosition().y - PREVIEW_SIZE.y / 2 ) );
732
733 m_previewWindow->Show();
734 }
735}
736
737
739{
740 m_previewItem = wxDataViewItem();
741
742 if( m_previewWindow )
743 m_previewWindow->Hide();
744}
745
746
748{
749 hidePreview();
750
751 if( m_previewWindow )
752 {
753 m_previewWindow->Destroy();
754 m_previewWindow = nullptr;
755 }
756}
757
758
759void LIB_TREE::onIdle( wxIdleEvent& aEvent )
760{
761 // The wxDataViewCtrl won't give us its mouseMoved events so we're forced to use idle
762 // events to track the hover state
763
764 // The dang thing won't give us scroll events either, so we implement a poor-man's
765 // scroll-checker using the last-known positions of the preview or hover items.
766
767 wxWindow* topLevelParent = wxGetTopLevelParent( m_parent );
768 wxWindow* topLevelFocus = wxGetTopLevelParent( wxWindow::FindFocus() );
769
770 bool mouseOverWindow = false;
771 wxPoint screenPos = wxGetMousePosition();
772
773 if( m_tree_ctrl->IsShownOnScreen() )
774 mouseOverWindow |= m_tree_ctrl->GetScreenRect().Contains( screenPos );
775
776 if( m_previewDisabled || topLevelFocus != topLevelParent || !mouseOverWindow )
777 {
778 m_hoverTimer.Stop();
779 hidePreview();
780 return;
781 }
782
783 wxPoint clientPos = m_tree_ctrl->ScreenToClient( screenPos );
784 wxDataViewItem item;
785 wxDataViewColumn* col = nullptr;
786
787 m_tree_ctrl->HitTest( clientPos, item, col );
788
789 if( m_previewItem.IsOk() )
790 {
791 if( item != m_previewItem )
792 {
793#ifdef __WXGTK__
794 // Hide the preview, because Wayland can't move windows.
795 if( wxGetDisplayInfo().type == wxDisplayType::wxDisplayWayland )
796 hidePreview();
797#endif
798 showPreview( item );
799 }
800
801 return;
802 }
803
804 if( m_hoverPos != clientPos )
805 {
806 m_hoverPos = clientPos;
807 m_hoverItem = item;
808 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
809 m_hoverTimer.StartOnce( HOVER_TIMER_MILLIS );
810 }
811}
812
813
814void LIB_TREE::onHoverTimer( wxTimerEvent& aEvent )
815{
816 hidePreview();
817
818 if( !m_tree_ctrl->IsShownOnScreen() || m_previewDisabled )
819 return;
820
821 wxDataViewItem item;
822 wxDataViewColumn* col = nullptr;
823 m_tree_ctrl->HitTest( m_hoverPos, item, col );
824
825 if( item == m_hoverItem && m_tree_ctrl->GetItemRect( item ) == m_hoverItemRect )
826 {
827 if( item != m_tree_ctrl->GetSelection() )
828 showPreview( item );
829 }
830 else // view must have been scrolled
831 {
832 m_hoverItem = item;
833 m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
834 m_hoverTimer.StartOnce( HOVER_TIMER_MILLIS );
835 }
836}
837
838
839void LIB_TREE::onTreeCharHook( wxKeyEvent& aKeyStroke )
840{
841 onQueryCharHook( aKeyStroke );
842
843 if( aKeyStroke.GetSkipped() )
844 {
845 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
846 {
847 int hotkey = aKeyStroke.GetKeyCode();
848
849 if( aKeyStroke.ShiftDown() )
850 hotkey |= MD_SHIFT;
851
852 if( aKeyStroke.AltDown() )
853 hotkey |= MD_ALT;
854
855 if( aKeyStroke.ControlDown() )
856 hotkey |= MD_CTRL;
857
858 if( tool->GetManager()->GetActionManager()->RunHotKey( hotkey ) )
859 aKeyStroke.Skip( false );
860 }
861 }
862}
863
864
865void LIB_TREE::onTreeSelect( wxDataViewEvent& aEvent )
866{
867 if( m_tree_ctrl->IsFrozen() )
868 return;
869
870 if( !m_inTimerEvent )
872
874}
875
876
877void LIB_TREE::onTreeActivate( wxDataViewEvent& aEvent )
878{
879 hidePreview();
880
881 if( !m_inTimerEvent )
883
884 if( !GetSelectedLibId().IsValid() )
885 toggleExpand( m_tree_ctrl->GetSelection() ); // Expand library/part units subtree
886 else
887 postSelectEvent(); // Open symbol/footprint
888}
889
890
891void LIB_TREE::onDetailsLink( wxHtmlLinkEvent& aEvent )
892{
893 const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
894 wxString docname = info.GetHref();
895 PROJECT& prj = Pgm().GetSettingsManager().Prj();
896
897 GetAssociatedDocument( this, docname, &prj );
898}
899
900
901void LIB_TREE::onPreselect( wxCommandEvent& aEvent )
902{
903 hidePreview();
904
905 if( m_details_ctrl )
906 {
907 int unit = 0;
908 LIB_ID id = GetSelectedLibId( &unit );
909
910 if( id.IsValid() )
911 m_details_ctrl->SetPage( m_adapter->GenerateInfo( id, unit ) );
912 else
913 m_details_ctrl->SetPage( wxEmptyString );
914 }
915
916 aEvent.Skip();
917}
918
919
920void LIB_TREE::onItemContextMenu( wxDataViewEvent& aEvent )
921{
922 hidePreview();
923
925 {
926 m_skipNextRightClick = false;
927 return;
928 }
929
930 m_previewDisabled = true;
931
932 if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
933 {
934 if( !GetCurrentTreeNode() )
935 {
936 wxPoint pos = m_tree_ctrl->ScreenToClient( wxGetMousePosition() );
937
938 wxDataViewItem item;
939 wxDataViewColumn* col;
940 m_tree_ctrl->HitTest( pos, item, col );
941
942 if( item.IsOk() )
943 {
944 m_tree_ctrl->SetFocus();
945 m_tree_ctrl->Select( item );
946 wxSafeYield();
947 }
948 }
949
950 tool->Activate();
951 tool->GetManager()->VetoContextMenuMouseWarp();
952 tool->GetToolMenu().ShowContextMenu();
953
955 tool->GetManager()->DispatchContextMenu( evt );
956 }
957 else
958 {
960
961 if( current && current->m_Type == LIB_TREE_NODE::TYPE::LIBRARY )
962 {
963 ACTION_MENU menu( true, nullptr );
964
965 if( current->m_Pinned )
966 {
968
969 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
970 m_adapter->UnpinLibrary( current );
971 }
972 else
973 {
974 menu.Add( ACTIONS::pinLibrary );
975
976 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
977 m_adapter->PinLibrary( current );
978 }
979 }
980 }
981
982 m_previewDisabled = false;
983}
984
985
986void LIB_TREE::onHeaderContextMenu( wxDataViewEvent& aEvent )
987{
988 hidePreview();
989 m_previewDisabled = true;
990
991 ACTION_MENU menu( true, nullptr );
992
994
995 if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
996 {
997 EDA_REORDERABLE_LIST_DIALOG dlg( m_parent, _( "Select Columns" ),
998 m_adapter->GetAvailableColumns(),
999 m_adapter->GetShownColumns() );
1000
1001 if( dlg.ShowModal() == wxID_OK )
1002 m_adapter->SetShownColumns( dlg.EnabledList() );
1003 }
1004
1005 m_previewDisabled = false;
1006}
1007
1008
1009wxDEFINE_EVENT( EVT_LIBITEM_SELECTED, wxCommandEvent );
1010wxDEFINE_EVENT( EVT_LIBITEM_CHOSEN, wxCommandEvent );
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition: bitmap.cpp:110
static TOOL_ACTION pinLibrary
Definition: actions.h:152
static TOOL_ACTION selectLibTreeColumns
Definition: actions.h:224
static TOOL_ACTION unpinLibrary
Definition: actions.h:153
static TOOL_ACTION expandAll
Definition: actions.h:83
static TOOL_ACTION collapseAll
Definition: actions.h:84
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:263
void RefreshLibTree()
Refresh the tree (mainly to update highlighting and asterisking)
Definition: lib_tree.cpp:446
wxPoint m_hoverPos
Definition: lib_tree.h:258
wxTimer * m_debounceTimer
Definition: lib_tree.h:249
void onHoverTimer(wxTimerEvent &aEvent)
Definition: lib_tree.cpp:814
LIB_TREE_NODE * GetCurrentTreeNode() const
Definition: lib_tree.cpp:338
void onQueryMouseMoved(wxMouseEvent &aEvent)
Definition: lib_tree.cpp:693
wxDataViewItem m_previewItem
Definition: lib_tree.h:262
HTML_WINDOW * m_details_ctrl
Definition: lib_tree.h:248
void onTreeActivate(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:877
wxString m_recentSearchesKey
Definition: lib_tree.h:252
void onQueryCharHook(wxKeyEvent &aEvent)
Definition: lib_tree.cpp:613
void CenterLibId(const LIB_ID &aLibId)
Ensure that an item is visible (preferably centered).
Definition: lib_tree.cpp:355
void postSelectEvent()
Post #SYMBOL_SELECTED event to notify the selection handler that a part has been selected.
Definition: lib_tree.cpp:554
wxWindow * GetFocusTarget()
Definition: lib_tree.cpp:452
void onItemContextMenu(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:920
void destroyPreview()
Definition: lib_tree.cpp:747
void ShowChangedLanguage()
Definition: lib_tree.cpp:291
WX_DATAVIEWCTRL * m_tree_ctrl
Definition: lib_tree.h:247
void onQueryText(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:595
void toggleExpand(const wxDataViewItem &aTreeId)
Expand or collapse a node, switching it to the opposite state.
Definition: lib_tree.cpp:468
bool m_skipNextRightClick
Definition: lib_tree.h:256
bool m_inTimerEvent
Definition: lib_tree.h:250
void selectIfValid(const wxDataViewItem &aTreeId)
If a wxDataViewitem is valid, select it and post a selection event.
Definition: lib_tree.cpp:480
void onIdle(wxIdleEvent &aEvent)
Definition: lib_tree.cpp:759
void FocusSearchFieldIfExists()
Focus the search widget if it exists.
Definition: lib_tree.cpp:461
void expandIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:540
void onPreselect(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:901
wxPopupWindow * m_previewWindow
Definition: lib_tree.h:264
void SelectLibId(const LIB_ID &aLibId)
Select an item in the tree widget.
Definition: lib_tree.cpp:349
void updateRecentSearchMenu()
Definition: lib_tree.cpp:399
void hidePreview()
Definition: lib_tree.cpp:738
void showPreview(wxDataViewItem aItem)
Definition: lib_tree.cpp:714
wxString GetSearchString() const
Definition: lib_tree.cpp:393
void onTreeSelect(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:865
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:261
void postPreselectEvent()
Post a wxEVT_DATAVIEW_SELECTION_CHANGED to notify the selection handler that a new part has been pres...
Definition: lib_tree.cpp:547
void Unselect()
Unselect currently selected item in wxDataViewCtrl.
Definition: lib_tree.cpp:361
void onTreeCharHook(wxKeyEvent &aEvent)
Definition: lib_tree.cpp:839
void onDetailsLink(wxHtmlLinkEvent &aEvent)
Definition: lib_tree.cpp:891
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:375
wxBoxSizer * m_filtersSizer
Definition: lib_tree.h:254
wxDataViewItem m_hoverItem
Definition: lib_tree.h:259
~LIB_TREE() override
Definition: lib_tree.cpp:253
void SetSearchString(const wxString &aSearchString)
Save/restore search string.
Definition: lib_tree.cpp:387
void setState(const STATE &aState)
Restore the symbol tree widget state from an object.
Definition: lib_tree.cpp:579
STATE getState() const
Return the symbol tree widget state.
Definition: lib_tree.cpp:561
bool m_previewDisabled
Definition: lib_tree.h:265
void ExpandLibId(const LIB_ID &aLibId)
Expand and item i the tree widget.
Definition: lib_tree.cpp:369
wxSearchCtrl * m_query_ctrl
Definition: lib_tree.h:245
void CollapseAll()
Definition: lib_tree.cpp:381
void centerIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:492
void onDebounceTimer(wxTimerEvent &aEvent)
Definition: lib_tree.cpp:605
wxRect m_hoverItemRect
Definition: lib_tree.h:260
STD_BITMAP_BUTTON * m_sort_ctrl
Definition: lib_tree.h:246
void onHeaderContextMenu(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:986
void Regenerate(bool aKeepState)
Regenerate the tree.
Definition: lib_tree.cpp:428
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > m_adapter
Definition: lib_tree.h:243
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:711
constexpr int RECENT_SEARCHES_MAX
Definition: lib_tree.cpp:46
#define PREVIEW_SIZE
Definition: lib_tree.cpp:710
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:1073
see class PGM_BASE
Structure storing state of the symbol tree widget.
Definition: lib_tree.h:200
LIB_ID selection
Current selection, might be not valid if nothing was selected.
Definition: lib_tree.h:205
std::vector< wxDataViewItem > expanded
List of expanded nodes.
Definition: lib_tree.h:202
@ 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