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