KiCad PCB EDA Suite
lib_tree.cpp
Go to the documentation of this file.
1 /* -*- c++ -*-
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 <wxdataviewctrl_helpers.h>
27 #include <wx/artprov.h>
28 #include <wx/sizer.h>
29 #include <wx/statbmp.h>
30 #include <wx/html/htmlwin.h>
31 #include <tool/tool_interactive.h>
32 #include <tool/tool_manager.h>
33 
34 LIB_TREE::LIB_TREE( wxWindow* aParent, LIB_TABLE* aLibTable, LIB_TREE_MODEL_ADAPTER::PTR& aAdapter,
35  WIDGETS aWidgets, wxHtmlWindow* aDetails )
36  : wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
37  wxWANTS_CHARS | wxTAB_TRAVERSAL | wxNO_BORDER ),
38  m_lib_table( aLibTable ),
39  m_adapter( aAdapter ),
40  m_query_ctrl( nullptr ),
41  m_details_ctrl( nullptr )
42 {
43  auto sizer = new wxBoxSizer( wxVERTICAL );
44 
45  // Search text control
46  if( aWidgets & SEARCH )
47  {
48  auto search_sizer = new wxBoxSizer( wxHORIZONTAL );
49 
50  m_query_ctrl = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
51  wxDefaultSize, wxTE_PROCESS_ENTER );
52 
53 // Additional visual cue for GTK, which hides the placeholder text on focus
54 #ifdef __WXGTK__
55  auto bitmap = new wxStaticBitmap( this, wxID_ANY, wxArtProvider::GetBitmap( wxART_FIND, wxART_FRAME_ICON ) );
56 
57  if( aWidgets & DETAILS )
58  search_sizer->Add( bitmap, 0, wxALIGN_CENTER | wxALL, 5 );
59  else
60  search_sizer->Add( bitmap, 0, wxALIGN_CENTER | wxRIGHT, 5 );
61 #endif
62 
63  if( aWidgets & DETAILS )
64  search_sizer->Add( m_query_ctrl, 1, wxLEFT | wxTOP | wxRIGHT | wxEXPAND, 5 );
65  else
66  search_sizer->Add( m_query_ctrl, 1, wxEXPAND, 5 );
67  sizer->Add( search_sizer, 0, wxEXPAND, 5 );
68 
69  m_query_ctrl->Bind( wxEVT_TEXT, &LIB_TREE::onQueryText, this );
70  m_query_ctrl->Bind( wxEVT_TEXT_ENTER, &LIB_TREE::onQueryEnter, this );
71  m_query_ctrl->Bind( wxEVT_CHAR_HOOK, &LIB_TREE::onQueryCharHook, this );
72  }
73 
74  // Tree control
75  m_tree_ctrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
76  wxDV_SINGLE | wxDV_NO_HEADER );
77  m_adapter->AttachTo( m_tree_ctrl );
78 
79  if( aWidgets & DETAILS )
80  sizer->Add( m_tree_ctrl, 5, wxLEFT | wxTOP | wxRIGHT | wxEXPAND, 5 );
81  else
82  sizer->Add( m_tree_ctrl, 5, wxRIGHT | wxBOTTOM | wxEXPAND, 1 );
83 
84  // Description panel
85  if( aWidgets & DETAILS )
86  {
87  if( !aDetails )
88  {
89  auto html_sz = ConvertDialogToPixels( wxPoint( 80, 80 ) );
90 
91  m_details_ctrl = new wxHtmlWindow(
92  this, wxID_ANY, wxDefaultPosition, wxSize( html_sz.x, html_sz.y ),
93  wxHW_SCROLLBAR_AUTO );
94 
95  sizer->Add( m_details_ctrl, 2, wxALL | wxEXPAND, 5 );
96  }
97  else
98  {
99  m_details_ctrl = aDetails;
100  }
101 
102  m_details_ctrl->Bind( wxEVT_HTML_LINK_CLICKED, &LIB_TREE::onDetailsLink, this );
103  }
104 
105  SetSizer( sizer );
106 
107  m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &LIB_TREE::onTreeActivate, this );
108  m_tree_ctrl->Bind( wxEVT_DATAVIEW_SELECTION_CHANGED, &LIB_TREE::onTreeSelect, this );
109  m_tree_ctrl->Bind( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, &LIB_TREE::onContextMenu, this );
110  m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_EXPANDED, &LIB_TREE::onExpandCollapse, this );
111  m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_COLLAPSED, &LIB_TREE::onExpandCollapse, this );
112 
113  Bind( COMPONENT_PRESELECTED, &LIB_TREE::onPreselect, this );
114 
115  // If wxTextCtrl::SetHint() is called before binding wxEVT_TEXT, the event
116  // handler will intermittently fire.
117  if( m_query_ctrl )
118  {
119  m_query_ctrl->SetHint( _( "Filter" ) );
120  m_query_ctrl->SetFocus();
121  m_query_ctrl->SetValue( wxEmptyString ); // SetValue() is required here to kick off
122  // initial sorting and pre-selection.
123  }
124 
125  // There may be a part preselected in the model. Make sure it is displayed.
127  m_adapter->UpdateWidth( 0 );
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  auto sel = m_tree_ctrl->GetSelection();
144 
145  if( !sel )
146  {
147  LIB_ID emptyId;
148 
149  return emptyId;
150  }
151 
152  if( aUnit )
153  *aUnit = m_adapter->GetUnitFor( sel );
154 
155  return m_adapter->GetAliasFor( sel );
156 }
157 
158 
159 void LIB_TREE::SelectLibId( const LIB_ID& aLibId )
160 {
161  selectIfValid( m_adapter->FindItem( aLibId ) );
162 }
163 
164 
165 void LIB_TREE::CenterLibId( const LIB_ID& aLibId )
166 {
167  centerIfValid( m_adapter->FindItem( aLibId ) );
168 }
169 
170 
172 {
173  m_tree_ctrl->UnselectAll();
174 }
175 
176 void LIB_TREE::ExpandLibId( const LIB_ID& aLibId )
177 {
178  expandIfValid( m_adapter->FindItem( aLibId ) );
179 }
180 
181 
182 void LIB_TREE::Regenerate( bool aKeepState )
183 {
184  STATE current;
185 
186  // Store the state
187  if( aKeepState )
189 
190  wxString filter = m_query_ctrl->GetValue();
191  m_adapter->UpdateSearchString( filter );
193 
194  // Restore the state
195  if( aKeepState )
197 }
198 
199 
201 {
202  if( m_query_ctrl )
203  m_query_ctrl->SetFocus();
204  else
205  m_tree_ctrl->SetFocus();
206 }
207 
208 
209 void LIB_TREE::toggleExpand( const wxDataViewItem& aTreeId )
210 {
211  if( !aTreeId.IsOk() )
212  return;
213 
214  if( m_tree_ctrl->IsExpanded( aTreeId ) )
215  m_tree_ctrl->Collapse( aTreeId );
216  else
217  m_tree_ctrl->Expand( aTreeId );
218 }
219 
220 
221 void LIB_TREE::selectIfValid( const wxDataViewItem& aTreeId )
222 {
223  if( aTreeId.IsOk() )
224  {
225  m_tree_ctrl->EnsureVisible( aTreeId );
226  m_adapter->UpdateWidth( 0 );
227  m_tree_ctrl->Select( aTreeId );
229  }
230 }
231 
232 
233 void LIB_TREE::centerIfValid( const wxDataViewItem& aTreeId )
234 {
235  if( aTreeId.IsOk() )
236  {
237  m_tree_ctrl->EnsureVisible( aTreeId );
238  m_adapter->UpdateWidth( 0 );
239  }
240 }
241 
242 
243 void LIB_TREE::expandIfValid( const wxDataViewItem& aTreeId )
244 {
245  if( aTreeId.IsOk() && !m_tree_ctrl->IsExpanded( aTreeId ) )
246  m_tree_ctrl->Expand( aTreeId );
247 }
248 
249 
251 {
252  wxCommandEvent event( COMPONENT_PRESELECTED );
253  wxPostEvent( this, event );
254 }
255 
256 
258 {
259  wxCommandEvent event( COMPONENT_SELECTED );
260  wxPostEvent( this, event );
261 }
262 
263 
265 {
266  STATE state;
267  wxDataViewItemArray items;
268  m_adapter->GetChildren( wxDataViewItem( nullptr ), items );
269 
270  for( const auto& item : items )
271  {
272  if( m_tree_ctrl->IsExpanded( item ) )
273  state.expanded.push_back( item );
274  }
275 
276  state.selection = GetSelectedLibId();
277 
278  return state;
279 }
280 
281 
282 void LIB_TREE::setState( const STATE& aState )
283 {
284  m_tree_ctrl->Freeze();
285 
286  for( const auto& item : aState.expanded )
287  m_tree_ctrl->Expand( item );
288 
289  // wxDataViewCtrl cannot be frozen when a selection
290  // command is issued, otherwise it selects a random item (Windows)
291  m_tree_ctrl->Thaw();
292 
293  if( !aState.selection.GetLibItemName().empty() || !aState.selection.GetLibNickname().empty() )
294  SelectLibId( aState.selection );
295 }
296 
297 
298 void LIB_TREE::onQueryText( wxCommandEvent& aEvent )
299 {
300  Regenerate( false );
301 
302  // Required to avoid interaction with SetHint()
303  // See documentation for wxTextEntry::SetHint
304  aEvent.Skip();
305 }
306 
307 
308 void LIB_TREE::onQueryEnter( wxCommandEvent& aEvent )
309 {
310  if( GetSelectedLibId().IsValid() )
311  postSelectEvent();
312 }
313 
314 
315 void LIB_TREE::onQueryCharHook( wxKeyEvent& aKeyStroke )
316 {
317  auto const sel = m_tree_ctrl->GetSelection();
318  auto type = sel.IsOk() ? m_adapter->GetTypeFor( sel ) : LIB_TREE_NODE::INVALID;
319 
320  switch( aKeyStroke.GetKeyCode() )
321  {
322  case WXK_UP:
324  break;
325 
326  case WXK_DOWN:
328  break;
329 
330  case WXK_ADD:
331  if( type == LIB_TREE_NODE::LIB )
332  m_tree_ctrl->Expand( sel );
333 
334  break;
335 
336  case WXK_SUBTRACT:
337  if( type == LIB_TREE_NODE::LIB )
338  m_tree_ctrl->Collapse( sel );
339 
340  break;
341 
342  case WXK_RETURN:
343  if( type == LIB_TREE_NODE::LIB )
344  {
345  toggleExpand( sel );
346  break;
347  }
348  /* fall through, so the selected component will be treated as the selected one */
349 
350  default:
351  aKeyStroke.Skip(); // Any other key: pass on to search box directly.
352  break;
353  }
354 }
355 
356 
357 void LIB_TREE::onTreeSelect( wxDataViewEvent& aEvent )
358 {
360 }
361 
362 
363 void LIB_TREE::onExpandCollapse( wxDataViewEvent& aEvent )
364 {
365  m_adapter->UpdateWidth( 0 );
366 }
367 
368 
369 void LIB_TREE::onTreeActivate( wxDataViewEvent& aEvent )
370 {
371  if( !GetSelectedLibId().IsValid() )
372  {
373  // Expand library/part units subtree
374  toggleExpand( m_tree_ctrl->GetSelection() );
375  }
376  else
377  {
378  postSelectEvent();
379  }
380 }
381 
382 
383 void LIB_TREE::onDetailsLink( wxHtmlLinkEvent& aEvent )
384 {
385  const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
386  ::wxLaunchDefaultBrowser( info.GetHref() );
387 }
388 
389 
390 void LIB_TREE::onPreselect( wxCommandEvent& aEvent )
391 {
392  if( m_details_ctrl )
393  {
394  int unit = 0;
395  LIB_ID id = GetSelectedLibId( &unit );
396 
397  if( id.IsValid() )
398  m_details_ctrl->SetPage( m_adapter->GenerateInfo( id, unit ) );
399  else
400  m_details_ctrl->SetPage( wxEmptyString );
401  }
402 
403  aEvent.Skip();
404 }
405 
406 
407 void LIB_TREE::onContextMenu( wxDataViewEvent& aEvent )
408 {
409  TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool();
410 
411  if( tool )
412  {
413  tool->Activate();
415  tool->GetToolMenu().ShowContextMenu();
416 
418  tool->GetManager()->DispatchContextMenu( evt );
419  }
420 }
421 
422 
423 wxDEFINE_EVENT( COMPONENT_PRESELECTED, wxCommandEvent );
424 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:221
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:209
STATE getState() const
Returns the component tree widget state.
Definition: lib_tree.cpp:264
wxDataViewItem GetPrevItem(wxDataViewCtrl const &aView, wxDataViewItem const &aItem)
Get the previous item in list order.
void onQueryEnter(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:308
wxHtmlWindow * m_details_ctrl
Definition: lib_tree.h:164
void centerIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:233
void postSelectEvent()
Post COMPONENT_SELECTED event to notify the selection handler that a part has been selected.
Definition: lib_tree.cpp:257
wxDataViewCtrl * m_tree_ctrl
Definition: lib_tree.h:163
TOOL_MENU & GetToolMenu()
LIB_ID selection
Current selection, might be not valid if nothing was selected
Definition: lib_tree.h:134
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
void onPreselect(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:390
TOOL_MANAGER * GetManager() const
Function GetManager() Returns the instance of TOOL_MANAGER that takes care of the tool.
Definition: tool_base.h:143
STATE m_unfilteredState
State of the widget before any filters applied
Definition: lib_tree.h:167
void onQueryCharHook(wxKeyEvent &aEvent)
Definition: lib_tree.cpp:315
wxDEFINE_EVENT(COMPONENT_PRESELECTED, wxCommandEvent)
void onTreeActivate(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:369
Class TOOL_EVENT.
Definition: tool_event.h:168
void onQueryText(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:298
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:141
void Regenerate(bool aKeepState)
Regenerates the tree.
Definition: lib_tree.cpp:182
void Unselect()
Unselect currently selected item in wxDataViewCtrl.
Definition: lib_tree.cpp:171
void onTreeSelect(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:357
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97
wxDataViewItem GetNextItem(wxDataViewCtrl const &aView, wxDataViewItem const &aItem)
Get the next item in list order.
void onDetailsLink(wxHtmlLinkEvent &aEvent)
Definition: lib_tree.cpp:383
void SetFocus() override
Definition: lib_tree.cpp:200
#define _(s)
void ExpandLibId(const LIB_ID &aLibId)
Expand and item i the tree widget.
Definition: lib_tree.cpp:176
WIDGETS
Flags to select extra widgets
Definition: lib_tree.h:46
void onExpandCollapse(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:363
void VetoContextMenuMouseWarp()
Disables mouse warping after the current context menu is closed.
Definition: tool_manager.h:372
void expandIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:243
std::vector< wxDataViewItem > expanded
List of expanded nodes
Definition: lib_tree.h:131
void Activate()
Function Activate() Runs the tool.
Structure storing state of the component tree widget.
Definition: lib_tree.h:128
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:250
LIB_TREE_MODEL_ADAPTER::PTR m_adapter
Definition: lib_tree.h:160
BITMAP_DEF GetBitmap(GRAPHIC_PINSHAPE shape)
Definition: pin_shape.cpp:70
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:282
void CenterLibId(const LIB_ID &aLibId)
Ensure that an item is visible (preferrably centered).
Definition: lib_tree.cpp:165
void SelectLibId(const LIB_ID &aLibId)
Select an item in the tree widget.
Definition: lib_tree.cpp:159
bool empty() const
Definition: utf8.h:108
wxTextCtrl * m_query_ctrl
Definition: lib_tree.h:162
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:34
void onContextMenu(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:407
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.