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-2019 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 "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 
34 
35 LIB_TREE::LIB_TREE( wxWindow* aParent, LIB_TABLE* aLibTable, LIB_TREE_MODEL_ADAPTER::PTR& aAdapter,
36  WIDGETS aWidgets, wxHtmlWindow* aDetails )
37  : wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
38  wxWANTS_CHARS | wxTAB_TRAVERSAL | wxNO_BORDER ),
39  m_lib_table( aLibTable ),
40  m_adapter( aAdapter ),
41  m_query_ctrl( nullptr ),
42  m_details_ctrl( nullptr )
43 {
44  auto sizer = new wxBoxSizer( wxVERTICAL );
45 
46  // Search text control
47  if( aWidgets & SEARCH )
48  {
49  auto search_sizer = new wxBoxSizer( wxHORIZONTAL );
50 
51  m_query_ctrl = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
52  wxDefaultSize, wxTE_PROCESS_ENTER );
53 
54 // Additional visual cue for GTK, which hides the placeholder text on focus
55 #ifdef __WXGTK__
56  auto bitmap = new wxStaticBitmap( this, wxID_ANY, wxArtProvider::GetBitmap( wxART_FIND, wxART_FRAME_ICON ) );
57 
58  if( aWidgets & DETAILS )
59  search_sizer->Add( bitmap, 0, wxALIGN_CENTER | wxALL, 5 );
60  else
61  search_sizer->Add( bitmap, 0, wxALIGN_CENTER | wxRIGHT, 5 );
62 #endif
63 
64  if( aWidgets & DETAILS )
65  search_sizer->Add( m_query_ctrl, 1, wxLEFT | wxTOP | wxRIGHT | wxEXPAND, 5 );
66  else
67  search_sizer->Add( m_query_ctrl, 1, wxEXPAND, 5 );
68  sizer->Add( search_sizer, 0, wxEXPAND, 5 );
69 
70  m_query_ctrl->Bind( wxEVT_TEXT, &LIB_TREE::onQueryText, this );
71  m_query_ctrl->Bind( wxEVT_TEXT_ENTER, &LIB_TREE::onQueryEnter, this );
72  m_query_ctrl->Bind( wxEVT_CHAR_HOOK, &LIB_TREE::onQueryCharHook, this );
73  }
74 
75  // Tree control
76  m_tree_ctrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
77  wxDV_SINGLE );
78  m_adapter->AttachTo( m_tree_ctrl );
79 
80  if( aWidgets & DETAILS )
81  sizer->Add( m_tree_ctrl, 5, wxLEFT | wxTOP | wxRIGHT | wxEXPAND, 5 );
82  else
83  sizer->Add( m_tree_ctrl, 5, wxRIGHT | wxBOTTOM | wxEXPAND, 1 );
84 
85  // Description panel
86  if( aWidgets & DETAILS )
87  {
88  if( !aDetails )
89  {
90  auto html_sz = ConvertDialogToPixels( wxPoint( 80, 80 ) );
91 
92  m_details_ctrl = new wxHtmlWindow(
93  this, wxID_ANY, wxDefaultPosition, wxSize( html_sz.x, html_sz.y ),
94  wxHW_SCROLLBAR_AUTO );
95 
96  sizer->Add( m_details_ctrl, 2, wxALL | wxEXPAND, 5 );
97  }
98  else
99  {
100  m_details_ctrl = aDetails;
101  }
102 
103  m_details_ctrl->Bind( wxEVT_HTML_LINK_CLICKED, &LIB_TREE::onDetailsLink, this );
104  }
105 
106  SetSizer( sizer );
107 
108  m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &LIB_TREE::onTreeActivate, this );
109  m_tree_ctrl->Bind( wxEVT_DATAVIEW_SELECTION_CHANGED, &LIB_TREE::onTreeSelect, this );
110  m_tree_ctrl->Bind( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, &LIB_TREE::onContextMenu, this );
111 
112  Bind( COMPONENT_PRESELECTED, &LIB_TREE::onPreselect, this );
113 
114  // If wxTextCtrl::SetHint() is called before binding wxEVT_TEXT, the event
115  // handler will intermittently fire.
116  if( m_query_ctrl )
117  {
118  m_query_ctrl->SetHint( _( "Filter" ) );
119  m_query_ctrl->SetFocus();
120  m_query_ctrl->SetValue( wxEmptyString );
121 
122  // Force an update of the adapter with the empty text to ensure preselect is done
123  Regenerate( false );
124  }
125 
126  // There may be a part preselected in the model. Make sure it is displayed.
128 
129  Layout();
130  sizer->Fit( this );
131 
132 #ifdef __WXGTK__
133  // Scrollbars must be always enabled to prevent an infinite event loop
134  // more details: http://trac.wxwidgets.org/ticket/18141
135  if( m_details_ctrl )
136  m_details_ctrl->ShowScrollbars( wxSHOW_SB_ALWAYS, wxSHOW_SB_ALWAYS );
137 #endif /* __WXGTK__ */
138 }
139 
140 
142 {
143  // Save the column widths to the config file
144  m_adapter->SaveColWidths();
145 
146  m_adapter->SavePinnedItems();
147 }
148 
149 
151 {
152  auto sel = m_tree_ctrl->GetSelection();
153 
154  if( !sel )
155  {
156  LIB_ID emptyId;
157 
158  return emptyId;
159  }
160 
161  if( aUnit )
162  *aUnit = m_adapter->GetUnitFor( sel );
163 
164  return m_adapter->GetAliasFor( sel );
165 }
166 
167 
169 {
170  auto sel = m_tree_ctrl->GetSelection();
171 
172  if( !sel )
173  return nullptr;
174 
175  return m_adapter->GetTreeNodeFor( sel );
176 }
177 
178 
179 void LIB_TREE::SelectLibId( const LIB_ID& aLibId )
180 {
181  selectIfValid( m_adapter->FindItem( aLibId ) );
182 }
183 
184 
185 void LIB_TREE::CenterLibId( const LIB_ID& aLibId )
186 {
187  centerIfValid( m_adapter->FindItem( aLibId ) );
188 }
189 
190 
192 {
193  m_tree_ctrl->UnselectAll();
194 }
195 
196 void LIB_TREE::ExpandLibId( const LIB_ID& aLibId )
197 {
198  expandIfValid( m_adapter->FindItem( aLibId ) );
199 }
200 
201 
202 void LIB_TREE::Regenerate( bool aKeepState )
203 {
204  STATE current;
205 
206  // Store the state
207  if( aKeepState )
208  current = getState();
209 
210  wxString filter = m_query_ctrl->GetValue();
211  m_adapter->UpdateSearchString( filter );
213 
214  // Restore the state
215  if( aKeepState )
216  setState( current );
217 }
218 
219 
221 {
222  m_adapter->RefreshTree();
223 }
224 
225 
227 {
228  if( m_query_ctrl )
229  m_query_ctrl->SetFocus();
230  else
231  m_tree_ctrl->SetFocus();
232 }
233 
234 
235 void LIB_TREE::toggleExpand( const wxDataViewItem& aTreeId )
236 {
237  if( !aTreeId.IsOk() )
238  return;
239 
240  if( m_tree_ctrl->IsExpanded( aTreeId ) )
241  m_tree_ctrl->Collapse( aTreeId );
242  else
243  m_tree_ctrl->Expand( aTreeId );
244 }
245 
246 
247 void LIB_TREE::selectIfValid( const wxDataViewItem& aTreeId )
248 {
249  if( aTreeId.IsOk() )
250  {
251  m_tree_ctrl->EnsureVisible( aTreeId );
252  m_tree_ctrl->Select( aTreeId );
254  }
255 }
256 
257 
258 void LIB_TREE::centerIfValid( const wxDataViewItem& aTreeId )
259 {
260  if( aTreeId.IsOk() )
261  m_tree_ctrl->EnsureVisible( aTreeId );
262 }
263 
264 
265 void LIB_TREE::expandIfValid( const wxDataViewItem& aTreeId )
266 {
267  if( aTreeId.IsOk() && !m_tree_ctrl->IsExpanded( aTreeId ) )
268  m_tree_ctrl->Expand( aTreeId );
269 }
270 
271 
273 {
274  wxCommandEvent event( COMPONENT_PRESELECTED );
275  wxPostEvent( this, event );
276 }
277 
278 
280 {
281  wxCommandEvent event( COMPONENT_SELECTED );
282  wxPostEvent( this, event );
283 }
284 
285 
287 {
288  STATE state;
289  wxDataViewItemArray items;
290  m_adapter->GetChildren( wxDataViewItem( nullptr ), items );
291 
292  for( const auto& item : items )
293  {
294  if( m_tree_ctrl->IsExpanded( item ) )
295  state.expanded.push_back( item );
296  }
297 
298  state.selection = GetSelectedLibId();
299 
300  return state;
301 }
302 
303 
304 void LIB_TREE::setState( const STATE& aState )
305 {
306  m_tree_ctrl->Freeze();
307 
308  for( const auto& item : aState.expanded )
309  m_tree_ctrl->Expand( item );
310 
311  // wxDataViewCtrl cannot be frozen when a selection
312  // command is issued, otherwise it selects a random item (Windows)
313  m_tree_ctrl->Thaw();
314 
315  if( !aState.selection.GetLibItemName().empty() || !aState.selection.GetLibNickname().empty() )
316  SelectLibId( aState.selection );
317 }
318 
319 
320 void LIB_TREE::onQueryText( wxCommandEvent& aEvent )
321 {
322  Regenerate( false );
323 
324  // Required to avoid interaction with SetHint()
325  // See documentation for wxTextEntry::SetHint
326  aEvent.Skip();
327 }
328 
329 
330 void LIB_TREE::onQueryEnter( wxCommandEvent& aEvent )
331 {
332  if( GetSelectedLibId().IsValid() )
333  postSelectEvent();
334 }
335 
336 
337 void LIB_TREE::onQueryCharHook( wxKeyEvent& aKeyStroke )
338 {
339  auto const sel = m_tree_ctrl->GetSelection();
340  auto type = sel.IsOk() ? m_adapter->GetTypeFor( sel ) : LIB_TREE_NODE::INVALID;
341 
342  switch( aKeyStroke.GetKeyCode() )
343  {
344  case WXK_UP:
346  break;
347 
348  case WXK_DOWN:
350  break;
351 
352  case WXK_ADD:
353  if( type == LIB_TREE_NODE::LIB )
354  m_tree_ctrl->Expand( sel );
355 
356  break;
357 
358  case WXK_SUBTRACT:
359  if( type == LIB_TREE_NODE::LIB )
360  m_tree_ctrl->Collapse( sel );
361 
362  break;
363 
364  case WXK_RETURN:
365  if( type == LIB_TREE_NODE::LIB )
366  {
367  toggleExpand( sel );
368  break;
369  }
370  // Intentionally fall through, so the selected component will be treated as the selected one
372 
373  default:
374  aKeyStroke.Skip(); // Any other key: pass on to search box directly.
375  break;
376  }
377 }
378 
379 
380 void LIB_TREE::onTreeSelect( wxDataViewEvent& aEvent )
381 {
383 }
384 
385 
386 void LIB_TREE::onTreeActivate( wxDataViewEvent& aEvent )
387 {
388  if( !GetSelectedLibId().IsValid() )
389  {
390  // Expand library/part units subtree
391  toggleExpand( m_tree_ctrl->GetSelection() );
392  }
393  else
394  {
395  postSelectEvent();
396  }
397 }
398 
399 
400 void LIB_TREE::onDetailsLink( wxHtmlLinkEvent& aEvent )
401 {
402  const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
403  ::wxLaunchDefaultBrowser( info.GetHref() );
404 }
405 
406 
407 void LIB_TREE::onPreselect( wxCommandEvent& aEvent )
408 {
409  if( m_details_ctrl )
410  {
411  int unit = 0;
412  LIB_ID id = GetSelectedLibId( &unit );
413 
414  if( id.IsValid() )
415  m_details_ctrl->SetPage( m_adapter->GenerateInfo( id, unit ) );
416  else
417  m_details_ctrl->SetPage( wxEmptyString );
418  }
419 
420  aEvent.Skip();
421 }
422 
423 
424 void LIB_TREE::onContextMenu( wxDataViewEvent& aEvent )
425 {
426  TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool();
427 
428  if( tool )
429  {
430  tool->Activate();
432  tool->GetToolMenu().ShowContextMenu();
433 
435  tool->GetManager()->DispatchContextMenu( evt );
436  }
437 }
438 
439 
440 wxDEFINE_EVENT( COMPONENT_PRESELECTED, wxCommandEvent );
441 wxDEFINE_EVENT( COMPONENT_SELECTED, wxCommandEvent );
void selectIfValid(const wxDataViewItem &aTreeId)
If a wxDataViewitem is valid, select it and post a selection event.
Definition: lib_tree.cpp:247
void DispatchContextMenu(const TOOL_EVENT &aEvent)
Function DispatchContextMenu() Handles context menu related events.
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
void toggleExpand(const wxDataViewItem &aTreeId)
Expands or collapses a node, switching it to the opposite state.
Definition: lib_tree.cpp:235
STATE getState() const
Returns the component tree widget state.
Definition: lib_tree.cpp:286
wxDataViewItem GetPrevItem(wxDataViewCtrl const &aView, wxDataViewItem const &aItem)
Get the previous item in list order.
void onQueryEnter(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:330
wxHtmlWindow * m_details_ctrl
Definition: lib_tree.h:174
void centerIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:258
void postSelectEvent()
Post COMPONENT_SELECTED event to notify the selection handler that a part has been selected.
Definition: lib_tree.cpp:279
wxDataViewCtrl * m_tree_ctrl
Definition: lib_tree.h:173
TOOL_MENU & GetToolMenu()
LIB_ID selection
Current selection, might be not valid if nothing was selected
Definition: lib_tree.h:144
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:88
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:141
void onPreselect(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:407
This file contains miscellaneous commonly used macros and functions.
TOOL_MANAGER * GetManager() const
Function GetManager() Returns the instance of TOOL_MANAGER that takes care of the tool.
Definition: tool_base.h:143
void onQueryCharHook(wxKeyEvent &aEvent)
Definition: lib_tree.cpp:337
wxDEFINE_EVENT(COMPONENT_PRESELECTED, wxCommandEvent)
LIB_TREE_NODE * GetCurrentTreeNode() const
Definition: lib_tree.cpp:168
void onTreeActivate(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:386
TOOL_EVENT.
Definition: tool_event.h:171
void onQueryText(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:320
LIB_ID GetSelectedLibId(int *aUnit=nullptr) const
For multi-unit components, if the user selects the component itself rather than picking an individual...
Definition: lib_tree.cpp:150
void Regenerate(bool aKeepState)
Regenerates the tree.
Definition: lib_tree.cpp:202
void Unselect()
Unselect currently selected item in wxDataViewCtrl.
Definition: lib_tree.cpp:191
void onTreeSelect(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:380
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97
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:400
void SetFocus() override
Definition: lib_tree.cpp:226
void RefreshLibTree()
Refreshes the tree (mainly to update highlighting and asterisking)
Definition: lib_tree.cpp:220
void ExpandLibId(const LIB_ID &aLibId)
Expand and item i the tree widget.
Definition: lib_tree.cpp:196
WIDGETS
Flags to select extra widgets
Definition: lib_tree.h:46
#define _(s)
Definition: 3d_actions.cpp:33
void VetoContextMenuMouseWarp()
Disables mouse warping after the current context menu is closed.
Definition: tool_manager.h:413
void expandIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:265
std::vector< wxDataViewItem > expanded
List of expanded nodes
Definition: lib_tree.h:140
void Activate()
Function Activate() Runs the tool.
Structure storing state of the component tree widget.
Definition: lib_tree.h:137
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > PTR
Reference-counting container for a pointer to CMP_TREE_MODEL_ADAPTER_BASE.
void postPreselectEvent()
Post a wxEVT_DATAVIEW_SELECTION_CHANGED to notify the selection handler that a new part has been pres...
Definition: lib_tree.cpp:272
LIB_TREE_MODEL_ADAPTER::PTR m_adapter
Definition: lib_tree.h:170
void ShowContextMenu(SELECTION &aSelection)
Function ShowContextMenu.
Definition: tool_menu.cpp:59
wxDataViewCtrl helper functions.
void setState(const STATE &aState)
Restores the component tree widget state from an object.
Definition: lib_tree.cpp:304
void CenterLibId(const LIB_ID &aLibId)
Ensure that an item is visible (preferrably centered).
Definition: lib_tree.cpp:185
void SelectLibId(const LIB_ID &aLibId)
Select an item in the tree widget.
Definition: lib_tree.cpp:179
bool empty() const
Definition: utf8.h:108
wxTextCtrl * m_query_ctrl
Definition: lib_tree.h:172
LIB_TREE(wxWindow *aParent, LIB_TABLE *aLibTable, LIB_TREE_MODEL_ADAPTER::PTR &aAdapter, WIDGETS aWidgets=ALL, wxHtmlWindow *aDetails=nullptr)
Construct a component tree.
Definition: lib_tree.cpp:35
void onContextMenu(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:424
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.