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 <h.zeller@acm.org>
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 <wx/html/htmlwin.h>
31 #include <tool/tool_interactive.h>
32 #include <tool/tool_manager.h>
33 #include <wx/srchctrl.h>
34 #include <wx/settings.h>
35 #include <wx/statbmp.h>
36 #include <wx/timer.h>
37 
38 
39 LIB_TREE::LIB_TREE( wxWindow* aParent, LIB_TABLE* aLibTable,
40  wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>& aAdapter,
41  WIDGETS aWidgets, wxHtmlWindow* aDetails )
42  : wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
43  wxWANTS_CHARS | wxTAB_TRAVERSAL | wxNO_BORDER ),
44  m_lib_table( aLibTable ),
45  m_adapter( aAdapter ),
46  m_query_ctrl( nullptr ),
47  m_details_ctrl( nullptr )
48 {
49  wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
50 
51  // Search text control
52  if( aWidgets & SEARCH )
53  {
54  wxBoxSizer* search_sizer = new wxBoxSizer( wxHORIZONTAL );
55 
56  m_query_ctrl = new wxSearchCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
57  wxDefaultSize );
58 
59  m_query_ctrl->ShowCancelButton( true );
60 
61 #ifdef __WXGTK__
62  // wxSearchCtrl vertical height is not calculated correctly on some GTK setups
63  // See https://gitlab.com/kicad/code/kicad/-/issues/9019
64  m_query_ctrl->SetMinSize( wxSize( -1, GetTextExtent( wxT( "qb" ) ).y + 10 ) );
65 #endif
66 
67  m_debounceTimer = new wxTimer( this );
68 
69  search_sizer->Add( m_query_ctrl, 1, wxEXPAND, 5 );
70 
71  sizer->Add( search_sizer, 0, wxEXPAND, 5 );
72 
73  m_query_ctrl->Bind( wxEVT_TEXT, &LIB_TREE::onQueryText, this );
74 
75 #if wxCHECK_VERSION( 3, 1, 1 )
76  m_query_ctrl->Bind( wxEVT_SEARCH, &LIB_TREE::onQueryEnter, this );
77 #else
78  m_query_ctrl->Bind( wxEVT_TEXT_ENTER, &LIB_TREE::onQueryEnter, this );
79 #endif
80 
81  m_query_ctrl->Bind( wxEVT_CHAR_HOOK, &LIB_TREE::onQueryCharHook, this );
82 
83 
84  Bind( wxEVT_TIMER, &LIB_TREE::onDebounceTimer, this, m_debounceTimer->GetId() );
85  }
86 
87  // Tree control
88  m_tree_ctrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
89  wxDV_SINGLE );
90  m_adapter->AttachTo( m_tree_ctrl );
91 
92  if( aWidgets & DETAILS )
93  sizer->AddSpacer( 5 );
94 
95  sizer->Add( m_tree_ctrl, 5, wxRIGHT | wxBOTTOM | wxEXPAND, 1 );
96 
97  // Description panel
98  if( aWidgets & DETAILS )
99  {
100  if( !aDetails )
101  {
102  wxPoint html_size = ConvertDialogToPixels( wxPoint( 80, 80 ) );
103 
104  m_details_ctrl = new wxHtmlWindow( this, wxID_ANY, wxDefaultPosition,
105  wxSize( html_size.x, html_size.y ),
106  wxHW_SCROLLBAR_AUTO );
107 
108  sizer->Add( m_details_ctrl, 2, wxTOP | wxEXPAND, 5 );
109  }
110  else
111  {
112  m_details_ctrl = aDetails;
113  }
114 
115  m_details_ctrl->Bind( wxEVT_HTML_LINK_CLICKED, &LIB_TREE::onDetailsLink, this );
116  }
117 
118  SetSizer( sizer );
119 
120  m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &LIB_TREE::onTreeActivate, this );
121  m_tree_ctrl->Bind( wxEVT_DATAVIEW_SELECTION_CHANGED, &LIB_TREE::onTreeSelect, this );
122  m_tree_ctrl->Bind( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, &LIB_TREE::onContextMenu, this );
123 
124  Bind( SYMBOL_PRESELECTED, &LIB_TREE::onPreselect, this );
125 
126  // If wxTextCtrl::SetHint() is called before binding wxEVT_TEXT, the event
127  // handler will intermittently fire.
128  if( m_query_ctrl )
129  {
130  m_query_ctrl->SetDescriptiveText( _( "Filter" ) );
131  m_query_ctrl->SetFocus();
132  m_query_ctrl->SetValue( wxEmptyString );
133 
134  // Force an update of the adapter with the empty text to ensure preselect is done
135  Regenerate( false );
136  }
137  else
138  {
139  // There may be a part preselected in the model. Make sure it is displayed.
140  // Regenerate does this in the other branch
142  }
143 
144  Layout();
145  sizer->Fit( this );
146 
147 #ifdef __WXGTK__
148  // Scrollbars must be always enabled to prevent an infinite event loop
149  // more details: http://trac.wxwidgets.org/ticket/18141
150  if( m_details_ctrl )
151  m_details_ctrl->ShowScrollbars( wxSHOW_SB_ALWAYS, wxSHOW_SB_ALWAYS );
152 #endif /* __WXGTK__ */
153 }
154 
155 
157 {
158  // Stop the timer during destruction early to avoid potential race conditions (that do happen)
159  m_debounceTimer->Stop();
160 
161  // Save the column widths to the config file
162  m_adapter->SaveColWidths();
163 
164  m_adapter->SavePinnedItems();
165 }
166 
167 
169 {
170  wxDataViewItem sel = m_tree_ctrl->GetSelection();
171 
172  if( !sel )
173  return LIB_ID();
174 
175  if( aUnit )
176  *aUnit = m_adapter->GetUnitFor( sel );
177 
178  return m_adapter->GetAliasFor( sel );
179 }
180 
181 
183 {
184  wxDataViewItem sel = m_tree_ctrl->GetSelection();
185 
186  if( !sel )
187  return nullptr;
188 
189  return m_adapter->GetTreeNodeFor( sel );
190 }
191 
192 
193 void LIB_TREE::SelectLibId( const LIB_ID& aLibId )
194 {
195  selectIfValid( m_adapter->FindItem( aLibId ) );
196 }
197 
198 
199 void LIB_TREE::CenterLibId( const LIB_ID& aLibId )
200 {
201  centerIfValid( m_adapter->FindItem( aLibId ) );
202 }
203 
204 
206 {
207  m_tree_ctrl->UnselectAll();
208 }
209 
210 void LIB_TREE::ExpandLibId( const LIB_ID& aLibId )
211 {
212  expandIfValid( m_adapter->FindItem( aLibId ) );
213 }
214 
215 
216 void LIB_TREE::Regenerate( bool aKeepState )
217 {
218  STATE current;
219 
220  // Store the state
221  if( aKeepState )
222  current = getState();
223 
224  wxString filter = m_query_ctrl->GetValue();
225  m_adapter->UpdateSearchString( filter, aKeepState );
227 
228  // Restore the state
229  if( aKeepState )
230  setState( current );
231 }
232 
233 
235 {
236  m_adapter->RefreshTree();
237 }
238 
239 
241 {
242  if( m_query_ctrl )
243  return m_query_ctrl;
244  else
245  return m_tree_ctrl;
246 }
247 
248 
249 void LIB_TREE::toggleExpand( const wxDataViewItem& aTreeId )
250 {
251  if( !aTreeId.IsOk() )
252  return;
253 
254  if( m_tree_ctrl->IsExpanded( aTreeId ) )
255  m_tree_ctrl->Collapse( aTreeId );
256  else
257  m_tree_ctrl->Expand( aTreeId );
258 }
259 
260 
261 void LIB_TREE::selectIfValid( const wxDataViewItem& aTreeId )
262 {
263  if( aTreeId.IsOk() )
264  {
265  m_tree_ctrl->EnsureVisible( aTreeId );
266  m_tree_ctrl->Select( aTreeId );
268  }
269 }
270 
271 
272 void LIB_TREE::centerIfValid( const wxDataViewItem& aTreeId )
273 {
274  if( aTreeId.IsOk() )
275  m_tree_ctrl->EnsureVisible( aTreeId );
276 }
277 
278 
279 void LIB_TREE::expandIfValid( const wxDataViewItem& aTreeId )
280 {
281  if( aTreeId.IsOk() && !m_tree_ctrl->IsExpanded( aTreeId ) )
282  m_tree_ctrl->Expand( aTreeId );
283 }
284 
285 
287 {
288  wxCommandEvent event( SYMBOL_PRESELECTED );
289  wxPostEvent( this, event );
290 }
291 
292 
294 {
295  wxCommandEvent event( SYMBOL_SELECTED );
296  wxPostEvent( this, event );
297 }
298 
299 
301 {
302  STATE state;
303  wxDataViewItemArray items;
304  m_adapter->GetChildren( wxDataViewItem( nullptr ), items );
305 
306  for( const auto& item : items )
307  {
308  if( m_tree_ctrl->IsExpanded( item ) )
309  state.expanded.push_back( item );
310  }
311 
312  state.selection = GetSelectedLibId();
313 
314  return state;
315 }
316 
317 
318 void LIB_TREE::setState( const STATE& aState )
319 {
320  m_tree_ctrl->Freeze();
321 
322  for( const auto& item : aState.expanded )
323  m_tree_ctrl->Expand( item );
324 
325  // wxDataViewCtrl cannot be frozen when a selection
326  // command is issued, otherwise it selects a random item (Windows)
327  m_tree_ctrl->Thaw();
328 
329  if( !aState.selection.GetLibItemName().empty() || !aState.selection.GetLibNickname().empty() )
330  SelectLibId( aState.selection );
331 }
332 
333 
334 void LIB_TREE::onQueryText( wxCommandEvent& aEvent )
335 {
336  m_debounceTimer->StartOnce( 200 );
337 
338  // Required to avoid interaction with SetHint()
339  // See documentation for wxTextEntry::SetHint
340  aEvent.Skip();
341 }
342 
343 
344 void LIB_TREE::onQueryEnter( wxCommandEvent& aEvent )
345 {
346  if( GetSelectedLibId().IsValid() )
347  postSelectEvent();
348 }
349 
350 
351 void LIB_TREE::onDebounceTimer( wxTimerEvent& aEvent )
352 {
353  Regenerate( false );
354 }
355 
356 
357 void LIB_TREE::onQueryCharHook( wxKeyEvent& aKeyStroke )
358 {
359  auto const sel = m_tree_ctrl->GetSelection();
360  auto type = sel.IsOk() ? m_adapter->GetTypeFor( sel ) : LIB_TREE_NODE::INVALID;
361 
362  switch( aKeyStroke.GetKeyCode() )
363  {
364  case WXK_UP:
366  break;
367 
368  case WXK_DOWN:
370  break;
371 
372  case WXK_ADD:
373  if( type == LIB_TREE_NODE::LIB )
374  m_tree_ctrl->Expand( sel );
375 
376  break;
377 
378  case WXK_SUBTRACT:
379  if( type == LIB_TREE_NODE::LIB )
380  m_tree_ctrl->Collapse( sel );
381 
382  break;
383 
384  case WXK_RETURN:
385  if( type == LIB_TREE_NODE::LIB )
386  {
387  toggleExpand( sel );
388  break;
389  }
390  // Intentionally fall through, so the selected symbol will be treated as the selected one
392 
393  default:
394  aKeyStroke.Skip(); // Any other key: pass on to search box directly.
395  break;
396  }
397 }
398 
399 
400 void LIB_TREE::onTreeSelect( wxDataViewEvent& aEvent )
401 {
402  if( !m_tree_ctrl->IsFrozen() )
404 }
405 
406 
407 void LIB_TREE::onTreeActivate( wxDataViewEvent& aEvent )
408 {
409  if( !GetSelectedLibId().IsValid() )
410  {
411  // Expand library/part units subtree
412  toggleExpand( m_tree_ctrl->GetSelection() );
413  }
414  else
415  {
416  postSelectEvent();
417  }
418 }
419 
420 
421 void LIB_TREE::onDetailsLink( wxHtmlLinkEvent& aEvent )
422 {
423  const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
424  ::wxLaunchDefaultBrowser( info.GetHref() );
425 }
426 
427 
428 void LIB_TREE::onPreselect( wxCommandEvent& aEvent )
429 {
430  if( m_details_ctrl )
431  {
432  int unit = 0;
433  LIB_ID id = GetSelectedLibId( &unit );
434 
435  wxString htmlColor = GetBackgroundColour().GetAsString( wxC2S_HTML_SYNTAX );
436  wxString textColor = GetForegroundColour().GetAsString( wxC2S_HTML_SYNTAX );
437  wxString linkColor = wxSystemSettings::GetColour( wxSYS_COLOUR_HOTLIGHT )
438  .GetAsString( wxC2S_HTML_SYNTAX );
439 
440  wxString html = wxString::Format( wxT( "<html><body bgcolor='%s' text='%s' link='%s'>" ),
441  htmlColor, textColor, linkColor );
442 
443  if( id.IsValid() )
444  html.Append( m_adapter->GenerateInfo( id, unit ) );
445 
446  html.Append( wxT( "</body></html>" ) );
447 
448  m_details_ctrl->SetPage( html );
449  }
450 
451  aEvent.Skip();
452 }
453 
454 
455 void LIB_TREE::onContextMenu( wxDataViewEvent& aEvent )
456 {
457  TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool();
458 
459  if( tool )
460  {
461  tool->Activate();
463  tool->GetToolMenu().ShowContextMenu();
464 
466  tool->GetManager()->DispatchContextMenu( evt );
467  }
468 }
469 
470 
471 wxDEFINE_EVENT( SYMBOL_PRESELECTED, wxCommandEvent );
472 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:261
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:179
void toggleExpand(const wxDataViewItem &aTreeId)
Expand or collapse a node, switching it to the opposite state.
Definition: lib_tree.cpp:249
LIB_TREE(wxWindow *aParent, LIB_TABLE *aLibTable, wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > &aAdapter, WIDGETS aWidgets=ALL, wxHtmlWindow *aDetails=nullptr)
Construct a symbol tree.
Definition: lib_tree.cpp:39
STATE getState() const
Return the symbol tree widget state.
Definition: lib_tree.cpp:300
wxDataViewItem GetPrevItem(wxDataViewCtrl const &aView, wxDataViewItem const &aItem)
Get the previous item in list order.
void onQueryEnter(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:344
wxWindow * GetFocusTarget()
Definition: lib_tree.cpp:240
wxHtmlWindow * m_details_ctrl
Definition: lib_tree.h:181
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > m_adapter
Definition: lib_tree.h:177
void centerIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:272
void postSelectEvent()
Post SYMBOL_SELECTED event to notify the selection handler that a part has been selected.
Definition: lib_tree.cpp:293
wxDataViewCtrl * m_tree_ctrl
Definition: lib_tree.h:180
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() override
Definition: lib_tree.cpp:156
void onPreselect(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:428
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:357
LIB_TREE_NODE * GetCurrentTreeNode() const
Definition: lib_tree.cpp:182
void onTreeActivate(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:407
Generic, UI-independent tool event.
Definition: tool_event.h:152
void onQueryText(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:334
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:168
void Regenerate(bool aKeepState)
Regenerate the tree.
Definition: lib_tree.cpp:216
void Unselect()
Unselect currently selected item in wxDataViewCtrl.
Definition: lib_tree.cpp:205
void onTreeSelect(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:400
#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:182
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:421
void RefreshLibTree()
Refreshes the tree (mainly to update highlighting and asterisking)
Definition: lib_tree.cpp:234
void onDebounceTimer(wxTimerEvent &aEvent)
Definition: lib_tree.cpp:351
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
void ExpandLibId(const LIB_ID &aLibId)
Expand and item i the tree widget.
Definition: lib_tree.cpp:210
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:279
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:286
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:318
void CenterLibId(const LIB_ID &aLibId)
Ensure that an item is visible (preferably centered).
Definition: lib_tree.cpp:199
void SelectLibId(const LIB_ID &aLibId)
Select an item in the tree widget.
Definition: lib_tree.cpp:193
bool empty() const
Definition: utf8.h:103
void onContextMenu(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:455
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.