KiCad PCB EDA Suite
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 (C) 2014-2021 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>
26 #include <macros.h>
27 #include <wxdataviewctrl_helpers.h>
28 #include <wx/artprov.h>
29 #include <wx/sizer.h>
30 #include <tool/tool_interactive.h>
31 #include <tool/tool_manager.h>
32 #include <wx/srchctrl.h>
33 #include <wx/settings.h>
34 #include <wx/statbmp.h>
35 #include <wx/timer.h>
36 
37 
38 LIB_TREE::LIB_TREE( wxWindow* aParent, LIB_TABLE* aLibTable,
39  wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>& aAdapter, WIDGETS aWidgets,
40  HTML_WINDOW* aDetails ) :
41  wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
42  wxWANTS_CHARS | wxTAB_TRAVERSAL | wxNO_BORDER ),
43  m_lib_table( aLibTable ), m_adapter( aAdapter ), m_query_ctrl( nullptr ),
44  m_details_ctrl( nullptr )
45 {
46  wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
47 
48  // Search text control
49  if( aWidgets & SEARCH )
50  {
51  wxBoxSizer* search_sizer = new wxBoxSizer( wxHORIZONTAL );
52 
53  m_query_ctrl = new wxSearchCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
54  wxDefaultSize );
55 
56  m_query_ctrl->ShowCancelButton( true );
57 
58 #ifdef __WXGTK__
59  // wxSearchCtrl vertical height is not calculated correctly on some GTK setups
60  // See https://gitlab.com/kicad/code/kicad/-/issues/9019
61  m_query_ctrl->SetMinSize( wxSize( -1, GetTextExtent( wxT( "qb" ) ).y + 10 ) );
62 #endif
63 
64  m_debounceTimer = new wxTimer( this );
65 
66  search_sizer->Add( m_query_ctrl, 1, wxEXPAND, 5 );
67 
68  sizer->Add( search_sizer, 0, wxEXPAND, 5 );
69 
70  m_query_ctrl->Bind( wxEVT_TEXT, &LIB_TREE::onQueryText, this );
71 
72 #if wxCHECK_VERSION( 3, 1, 1 )
73  m_query_ctrl->Bind( wxEVT_SEARCH, &LIB_TREE::onQueryEnter, this );
74  m_query_ctrl->Bind( wxEVT_SEARCH_CANCEL, &LIB_TREE::onQueryText, this );
75 #else
76  m_query_ctrl->Bind( wxEVT_TEXT_ENTER, &LIB_TREE::onQueryEnter, this );
77  m_query_ctrl->Bind( wxEVT_SEARCHCTRL_CANCEL_BTN, &LIB_TREE::onQueryText, this );
78 #endif
79  m_query_ctrl->Bind( wxEVT_CHAR_HOOK, &LIB_TREE::onQueryCharHook, this );
80  m_query_ctrl->Bind( wxEVT_MOTION, &LIB_TREE::onQueryMouseMoved, this );
81 
82 
83  Bind( wxEVT_TIMER, &LIB_TREE::onDebounceTimer, this, m_debounceTimer->GetId() );
84  }
85 
86  // Tree control
87  m_tree_ctrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
88  wxDV_SINGLE );
89  m_adapter->AttachTo( m_tree_ctrl );
90 
91  if( aWidgets & DETAILS )
92  sizer->AddSpacer( 5 );
93 
94  sizer->Add( m_tree_ctrl, 5, wxRIGHT | wxBOTTOM | wxEXPAND, 1 );
95 
96  // Description panel
97  if( aWidgets & DETAILS )
98  {
99  if( !aDetails )
100  {
101  wxPoint html_size = ConvertDialogToPixels( wxPoint( 80, 80 ) );
102 
103  m_details_ctrl = new HTML_WINDOW( this, wxID_ANY, wxDefaultPosition,
104  wxSize( html_size.x, html_size.y ),
105  wxHW_SCROLLBAR_AUTO );
106 
107  sizer->Add( m_details_ctrl, 2, wxTOP | wxEXPAND, 5 );
108  }
109  else
110  {
111  m_details_ctrl = aDetails;
112  }
113 
114  m_details_ctrl->Bind( wxEVT_HTML_LINK_CLICKED, &LIB_TREE::onDetailsLink, this );
115  }
116 
117  SetSizer( sizer );
118 
119  m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &LIB_TREE::onTreeActivate, this );
120  m_tree_ctrl->Bind( wxEVT_DATAVIEW_SELECTION_CHANGED, &LIB_TREE::onTreeSelect, this );
121  m_tree_ctrl->Bind( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, &LIB_TREE::onContextMenu, this );
122 
123  Bind( SYMBOL_PRESELECTED, &LIB_TREE::onPreselect, this );
124 
125  if( m_query_ctrl )
126  {
127  m_query_ctrl->SetDescriptiveText( _( "Filter" ) );
128  m_query_ctrl->SetFocus();
129  m_query_ctrl->SetValue( wxEmptyString );
130 
131  // Force an update of the adapter with the empty text to ensure preselect is done
132  Regenerate( false );
133  }
134  else
135  {
136  // There may be a part preselected in the model. Make sure it is displayed.
137  // Regenerate does this in the other branch
139  }
140 
141  Layout();
142  sizer->Fit( this );
143 
144 #ifdef __WXGTK__
145  // Scrollbars must be always enabled to prevent an infinite event loop
146  // more details: http://trac.wxwidgets.org/ticket/18141
147  if( m_details_ctrl )
148  m_details_ctrl->ShowScrollbars( wxSHOW_SB_ALWAYS, wxSHOW_SB_ALWAYS );
149 #endif /* __WXGTK__ */
150 }
151 
152 
154 {
155  // Stop the timer during destruction early to avoid potential race conditions (that do happen)
156  m_debounceTimer->Stop();
157 
158  // Save the column widths to the config file
159  m_adapter->SaveColWidths();
160 
161  m_adapter->SavePinnedItems();
162 }
163 
164 
166 {
167  wxDataViewItem sel = m_tree_ctrl->GetSelection();
168 
169  if( !sel )
170  return LIB_ID();
171 
172  if( aUnit )
173  *aUnit = m_adapter->GetUnitFor( sel );
174 
175  return m_adapter->GetAliasFor( sel );
176 }
177 
178 
180 {
181  wxDataViewItem sel = m_tree_ctrl->GetSelection();
182 
183  if( !sel )
184  return nullptr;
185 
186  return m_adapter->GetTreeNodeFor( sel );
187 }
188 
189 
190 void LIB_TREE::SelectLibId( const LIB_ID& aLibId )
191 {
192  selectIfValid( m_adapter->FindItem( aLibId ) );
193 }
194 
195 
196 void LIB_TREE::CenterLibId( const LIB_ID& aLibId )
197 {
198  centerIfValid( m_adapter->FindItem( aLibId ) );
199 }
200 
201 
203 {
204  m_tree_ctrl->UnselectAll();
205 }
206 
207 void LIB_TREE::ExpandLibId( const LIB_ID& aLibId )
208 {
209  expandIfValid( m_adapter->FindItem( aLibId ) );
210 }
211 
212 
213 void LIB_TREE::Regenerate( bool aKeepState )
214 {
215  STATE current;
216 
217  // Store the state
218  if( aKeepState )
219  current = getState();
220 
221  wxString filter = m_query_ctrl->GetValue();
222  m_adapter->UpdateSearchString( filter, aKeepState );
224 
225  // Restore the state
226  if( aKeepState )
227  setState( current );
228 }
229 
230 
232 {
233  m_adapter->RefreshTree();
234 }
235 
236 
238 {
239  if( m_query_ctrl )
240  return m_query_ctrl;
241  else
242  return m_tree_ctrl;
243 }
244 
245 
246 void LIB_TREE::toggleExpand( const wxDataViewItem& aTreeId )
247 {
248  if( !aTreeId.IsOk() )
249  return;
250 
251  if( m_tree_ctrl->IsExpanded( aTreeId ) )
252  m_tree_ctrl->Collapse( aTreeId );
253  else
254  m_tree_ctrl->Expand( aTreeId );
255 }
256 
257 
258 void LIB_TREE::selectIfValid( const wxDataViewItem& aTreeId )
259 {
260  if( aTreeId.IsOk() )
261  {
262  m_tree_ctrl->EnsureVisible( aTreeId );
263  m_tree_ctrl->Select( aTreeId );
265  }
266 }
267 
268 
269 void LIB_TREE::centerIfValid( const wxDataViewItem& aTreeId )
270 {
271  /*
272  * This doesn't actually center because the wxWidgets API is poorly suited to that (and
273  * it might be too noisy as well).
274  *
275  * It does try to keep the given item a bit off the top or bottom of the window.
276  */
277 
278  if( aTreeId.IsOk() )
279  {
280  LIB_TREE_NODE* node = m_adapter->GetTreeNodeFor( aTreeId );
281  LIB_TREE_NODE* parent = node->m_Parent;
282  LIB_TREE_NODE* grandParent = parent ? parent->m_Parent : nullptr;
283 
284  if( parent )
285  {
286  wxDataViewItemArray siblings;
287  m_adapter->GetChildren( wxDataViewItem( parent ), siblings );
288 
289  int idx = siblings.Index( aTreeId );
290 
291  if( idx + 5 < (int) siblings.GetCount() )
292  {
293  m_tree_ctrl->EnsureVisible( siblings.Item( idx + 5 ) );
294  }
295  else if( grandParent )
296  {
297  wxDataViewItemArray parentsSiblings;
298  m_adapter->GetChildren( wxDataViewItem( grandParent ), parentsSiblings );
299 
300  int p_idx = parentsSiblings.Index( wxDataViewItem( parent ) );
301 
302  if( p_idx + 1 < (int) parentsSiblings.GetCount() )
303  m_tree_ctrl->EnsureVisible( parentsSiblings.Item( p_idx + 1 ) );
304  }
305 
306  if( idx - 5 >= 0 )
307  m_tree_ctrl->EnsureVisible( siblings.Item( idx - 5 ) );
308  else
309  m_tree_ctrl->EnsureVisible( wxDataViewItem( parent ) );
310  }
311 
312  m_tree_ctrl->EnsureVisible( aTreeId );
313  }
314 }
315 
316 
317 void LIB_TREE::expandIfValid( const wxDataViewItem& aTreeId )
318 {
319  if( aTreeId.IsOk() && !m_tree_ctrl->IsExpanded( aTreeId ) )
320  m_tree_ctrl->Expand( aTreeId );
321 }
322 
323 
325 {
326  wxCommandEvent event( SYMBOL_PRESELECTED );
327  wxPostEvent( this, event );
328 }
329 
330 
332 {
333  wxCommandEvent event( SYMBOL_SELECTED );
334  wxPostEvent( this, event );
335 }
336 
337 
339 {
340  STATE state;
341  wxDataViewItemArray items;
342  m_adapter->GetChildren( wxDataViewItem( nullptr ), items );
343 
344  for( const wxDataViewItem& item : items )
345  {
346  if( m_tree_ctrl->IsExpanded( item ) )
347  state.expanded.push_back( item );
348  }
349 
350  state.selection = GetSelectedLibId();
351 
352  return state;
353 }
354 
355 
356 void LIB_TREE::setState( const STATE& aState )
357 {
358  m_tree_ctrl->Freeze();
359 
360  for( const wxDataViewItem& item : aState.expanded )
361  m_tree_ctrl->Expand( item );
362 
363  // wxDataViewCtrl cannot be frozen when a selection
364  // command is issued, otherwise it selects a random item (Windows)
365  m_tree_ctrl->Thaw();
366 
367  if( !aState.selection.GetLibItemName().empty() || !aState.selection.GetLibNickname().empty() )
368  SelectLibId( aState.selection );
369 }
370 
371 
372 void LIB_TREE::onQueryText( wxCommandEvent& aEvent )
373 {
374  m_debounceTimer->StartOnce( 200 );
375 
376  // Required to avoid interaction with SetHint()
377  // See documentation for wxTextEntry::SetHint
378  aEvent.Skip();
379 }
380 
381 
382 void LIB_TREE::onQueryEnter( wxCommandEvent& aEvent )
383 {
384  if( GetSelectedLibId().IsValid() )
385  postSelectEvent();
386 }
387 
388 
389 void LIB_TREE::onDebounceTimer( wxTimerEvent& aEvent )
390 {
391  Regenerate( false );
392 }
393 
394 
395 void LIB_TREE::onQueryCharHook( wxKeyEvent& aKeyStroke )
396 {
397  auto const sel = m_tree_ctrl->GetSelection();
398  auto type = sel.IsOk() ? m_adapter->GetTypeFor( sel ) : LIB_TREE_NODE::INVALID;
399 
400  switch( aKeyStroke.GetKeyCode() )
401  {
402  case WXK_UP:
404  break;
405 
406  case WXK_DOWN:
408  break;
409 
410  case WXK_ADD:
411  if( type == LIB_TREE_NODE::LIB )
412  m_tree_ctrl->Expand( sel );
413 
414  break;
415 
416  case WXK_SUBTRACT:
417  if( type == LIB_TREE_NODE::LIB )
418  m_tree_ctrl->Collapse( sel );
419 
420  break;
421 
422  case WXK_RETURN:
423  if( type == LIB_TREE_NODE::LIB )
424  {
425  toggleExpand( sel );
426  break;
427  }
428  // Intentionally fall through, so the selected symbol will be treated as the selected one
430 
431  default:
432  aKeyStroke.Skip(); // Any other key: pass on to search box directly.
433  break;
434  }
435 }
436 
437 
438 void LIB_TREE::onQueryMouseMoved( wxMouseEvent& aEvent )
439 {
440  wxPoint pos = aEvent.GetPosition();
441  wxRect ctrlRect = m_query_ctrl->GetScreenRect();
442  int buttonWidth = ctrlRect.GetHeight(); // Presume buttons are square
443 
444  if( m_query_ctrl->IsSearchButtonVisible() && pos.x < buttonWidth )
445  SetCursor( wxCURSOR_ARROW );
446  else if( m_query_ctrl->IsCancelButtonVisible() && pos.x > ctrlRect.GetWidth() - buttonWidth )
447  SetCursor( wxCURSOR_ARROW );
448  else
449  SetCursor( wxCURSOR_IBEAM );
450 }
451 
452 
453 void LIB_TREE::onTreeSelect( wxDataViewEvent& aEvent )
454 {
455  if( !m_tree_ctrl->IsFrozen() )
457 }
458 
459 
460 void LIB_TREE::onTreeActivate( wxDataViewEvent& aEvent )
461 {
462  if( !GetSelectedLibId().IsValid() )
463  {
464  // Expand library/part units subtree
465  toggleExpand( m_tree_ctrl->GetSelection() );
466  }
467  else
468  {
469  postSelectEvent();
470  }
471 }
472 
473 
474 void LIB_TREE::onDetailsLink( wxHtmlLinkEvent& aEvent )
475 {
476  const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
477  ::wxLaunchDefaultBrowser( info.GetHref() );
478 }
479 
480 
481 void LIB_TREE::onPreselect( wxCommandEvent& aEvent )
482 {
483  if( m_details_ctrl )
484  {
485  int unit = 0;
486  LIB_ID id = GetSelectedLibId( &unit );
487 
488  if( id.IsValid() )
489  m_details_ctrl->SetPage( m_adapter->GenerateInfo( id, unit ) );
490  else
491  m_details_ctrl->SetPage( wxEmptyString );
492  }
493 
494  aEvent.Skip();
495 }
496 
497 
498 void LIB_TREE::onContextMenu( wxDataViewEvent& aEvent )
499 {
500  TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool();
501 
502  if( tool )
503  {
504  tool->Activate();
506  tool->GetToolMenu().ShowContextMenu();
507 
509  tool->GetManager()->DispatchContextMenu( evt );
510  }
511 }
512 
513 
514 wxDEFINE_EVENT( SYMBOL_PRESELECTED, wxCommandEvent );
515 wxDEFINE_EVENT( SYMBOL_SELECTED, wxCommandEvent );
void selectIfValid(const wxDataViewItem &aTreeId)
If a wxDataViewitem is valid, select it and post a selection event.
Definition: lib_tree.cpp:258
void DispatchContextMenu(const TOOL_EVENT &aEvent)
Handle context menu related events.
const UTF8 & GetLibItemName() const
Definition: lib_id.h:104
wxSearchCtrl * m_query_ctrl
Definition: lib_tree.h:180
void toggleExpand(const wxDataViewItem &aTreeId)
Expand or collapse a node, switching it to the opposite state.
Definition: lib_tree.cpp:246
STATE getState() const
Return the symbol tree widget state.
Definition: lib_tree.cpp:338
wxDataViewItem GetPrevItem(wxDataViewCtrl const &aView, wxDataViewItem const &aItem)
Get the previous item in list order.
void onQueryEnter(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:382
wxWindow * GetFocusTarget()
Definition: lib_tree.cpp:237
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > m_adapter
Definition: lib_tree.h:178
void centerIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:269
void postSelectEvent()
Post SYMBOL_SELECTED event to notify the selection handler that a part has been selected.
Definition: lib_tree.cpp:331
wxDataViewCtrl * m_tree_ctrl
Definition: lib_tree.h:181
TOOL_MENU & GetToolMenu()
LIB_ID selection
Definition: lib_tree.h:148
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:83
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
LIB_TREE(wxWindow *aParent, LIB_TABLE *aLibTable, wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > &aAdapter, WIDGETS aWidgets=ALL, HTML_WINDOW *aDetails=nullptr)
Construct a symbol tree.
Definition: lib_tree.cpp:38
~LIB_TREE() override
Definition: lib_tree.cpp:153
void onPreselect(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:481
This file contains miscellaneous commonly used macros and functions.
TOOL_MANAGER * GetManager() const
Return the instance of TOOL_MANAGER that takes care of the tool.
Definition: tool_base.h:143
void onQueryCharHook(wxKeyEvent &aEvent)
Definition: lib_tree.cpp:395
HTML_WINDOW * m_details_ctrl
Definition: lib_tree.h:182
LIB_TREE_NODE * GetCurrentTreeNode() const
Definition: lib_tree.cpp:179
void onQueryMouseMoved(wxMouseEvent &aEvent)
Definition: lib_tree.cpp:438
LIB_TREE_NODE * m_Parent
void onTreeActivate(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:460
Generic, UI-independent tool event.
Definition: tool_event.h:152
void onQueryText(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:372
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:165
void Regenerate(bool aKeepState)
Regenerate the tree.
Definition: lib_tree.cpp:213
void Unselect()
Unselect currently selected item in wxDataViewCtrl.
Definition: lib_tree.cpp:202
void onTreeSelect(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:453
bool SetPage(const wxString &aSource) override
Definition: html_window.cpp:38
#define _(s)
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:90
wxTimer * m_debounceTimer
Definition: lib_tree.h:183
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
wxDataViewItem GetNextItem(wxDataViewCtrl const &aView, wxDataViewItem const &aItem)
Get the next item in list order.
void onDetailsLink(wxHtmlLinkEvent &aEvent)
Definition: lib_tree.cpp:474
void RefreshLibTree()
Refreshes the tree (mainly to update highlighting and asterisking)
Definition: lib_tree.cpp:231
void onDebounceTimer(wxTimerEvent &aEvent)
Definition: lib_tree.cpp:389
Add dark theme support to wxHtmlWindow.
Definition: html_window.h:33
void ExpandLibId(const LIB_ID &aLibId)
Expand and item i the tree widget.
Definition: lib_tree.cpp:207
WIDGETS
< Flags to select extra widgets
Definition: lib_tree.h:49
void VetoContextMenuMouseWarp()
Disable mouse warping after the current context menu is closed.
Definition: tool_manager.h:422
void expandIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:317
std::vector< wxDataViewItem > expanded
< List of expanded nodes
Definition: lib_tree.h:144
void Activate()
Run the tool.
Structure storing state of the symbol tree widget.
Definition: lib_tree.h:141
void postPreselectEvent()
Post a wxEVT_DATAVIEW_SELECTION_CHANGED to notify the selection handler that a new part has been pres...
Definition: lib_tree.cpp:324
wxDEFINE_EVENT(SYMBOL_PRESELECTED, wxCommandEvent)
void ShowContextMenu(SELECTION &aSelection)
Helper function to set and immediately show a CONDITIONAL_MENU in concert with the given SELECTION.
Definition: tool_menu.cpp:59
wxDataViewCtrl helper functions.
void setState(const STATE &aState)
Restore the symbol tree widget state from an object.
Definition: lib_tree.cpp:356
void CenterLibId(const LIB_ID &aLibId)
Ensure that an item is visible (preferably centered).
Definition: lib_tree.cpp:196
void SelectLibId(const LIB_ID &aLibId)
Select an item in the tree widget.
Definition: lib_tree.cpp:190
bool empty() const
Definition: utf8.h:103
void onContextMenu(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:498
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.