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 ),
39  m_lib_table( aLibTable ),
40  m_adapter( aAdapter ),
41  m_query_ctrl( nullptr ),
42  m_details_ctrl( nullptr ),
43  m_menuActive( false ),
44  m_filtering( 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 
117  Bind( COMPONENT_PRESELECTED, &LIB_TREE::onPreselect, this );
118 
119  // If wxTextCtrl::SetHint() is called before binding wxEVT_TEXT, the event
120  // handler will intermittently fire.
121  if( m_query_ctrl )
122  {
123  m_query_ctrl->SetHint( _( "Filter" ) );
124  m_query_ctrl->SetFocus();
125  m_query_ctrl->SetValue( wxEmptyString ); // SetValue() is required here to kick off
126  // initial sorting and pre-selection.
127  }
128 
129  // There may be a part preselected in the model. Make sure it is displayed.
131 
132  Layout();
133  sizer->Fit( this );
134 
135 #ifdef __WXGTK__
136  // Scrollbars must be always enabled to prevent an infinite event loop
137  // more details: http://trac.wxwidgets.org/ticket/18141
138  if( m_details_ctrl )
139  m_details_ctrl->ShowScrollbars( wxSHOW_SB_ALWAYS, wxSHOW_SB_ALWAYS );
140 #endif /* __WXGTK__ */
141 }
142 
143 
145 {
146  auto sel = m_tree_ctrl->GetSelection();
147 
148  if( !sel )
149  {
150  LIB_ID emptyId;
151 
152  return emptyId;
153  }
154 
155  if( aUnit )
156  *aUnit = m_adapter->GetUnitFor( sel );
157 
158  return m_adapter->GetAliasFor( sel );
159 }
160 
161 
162 void LIB_TREE::SelectLibId( const LIB_ID& aLibId )
163 {
164  selectIfValid( m_adapter->FindItem( aLibId ) );
165 }
166 
167 
168 void LIB_TREE::CenterLibId( const LIB_ID& aLibId )
169 {
170  centerIfValid( m_adapter->FindItem( aLibId ) );
171 }
172 
173 
175 {
176  m_tree_ctrl->UnselectAll();
177 }
178 
179 void LIB_TREE::ExpandLibId( const LIB_ID& aLibId )
180 {
181  expandIfValid( m_adapter->FindItem( aLibId ) );
182 }
183 
184 
186 {
187  STATE current;
188 
189  // Store the state
190  if( !m_filtering )
192 
193  wxString filter = m_query_ctrl->GetValue();
194  m_adapter->UpdateSearchString( filter );
195  m_filtering = !filter.IsEmpty();
197 
198  // Restore the state
199  if( !m_filtering )
201 }
202 
203 
205 {
206  if( m_query_ctrl )
207  m_query_ctrl->SetFocus();
208  else
209  m_tree_ctrl->SetFocus();
210 }
211 
212 
213 void LIB_TREE::toggleExpand( const wxDataViewItem& aTreeId )
214 {
215  if( !aTreeId.IsOk() )
216  return;
217 
218  if( m_tree_ctrl->IsExpanded( aTreeId ) )
219  m_tree_ctrl->Collapse( aTreeId );
220  else
221  m_tree_ctrl->Expand( aTreeId );
222 }
223 
224 
225 void LIB_TREE::selectIfValid( const wxDataViewItem& aTreeId )
226 {
227  if( aTreeId.IsOk() )
228  {
229  m_tree_ctrl->EnsureVisible( aTreeId );
230  m_tree_ctrl->Select( aTreeId );
232  }
233 }
234 
235 
236 void LIB_TREE::centerIfValid( const wxDataViewItem& aTreeId )
237 {
238  if( aTreeId.IsOk() )
239  m_tree_ctrl->EnsureVisible( aTreeId );
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();
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_RETURN:
331  if( type == LIB_TREE_NODE::LIB )
332  {
333  toggleExpand( sel );
334  break;
335  }
336  /* fall through, so the selected component will be treated as the selected one */
337 
338  default:
339  aKeyStroke.Skip(); // Any other key: pass on to search box directly.
340  break;
341  }
342 }
343 
344 
345 void LIB_TREE::onTreeSelect( wxDataViewEvent& aEvent )
346 {
348 }
349 
350 
351 void LIB_TREE::onTreeActivate( wxDataViewEvent& aEvent )
352 {
353  if( !GetSelectedLibId().IsValid() )
354  {
355  // Expand library/part units subtree
356  toggleExpand( m_tree_ctrl->GetSelection() );
357  }
358  else
359  {
360  postSelectEvent();
361  }
362 }
363 
364 
365 void LIB_TREE::onDetailsLink( wxHtmlLinkEvent& aEvent )
366 {
367  const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
368  ::wxLaunchDefaultBrowser( info.GetHref() );
369 }
370 
371 
372 void LIB_TREE::onPreselect( wxCommandEvent& aEvent )
373 {
374  if( m_details_ctrl )
375  {
376  int unit = 0;
377  LIB_ID id = GetSelectedLibId( &unit );
378 
379  if( id.IsValid() )
380  m_details_ctrl->SetPage( m_adapter->GenerateInfo( id, unit ) );
381  else
382  m_details_ctrl->SetPage( wxEmptyString );
383  }
384 
385  aEvent.Skip();
386 }
387 
388 
389 void LIB_TREE::onContextMenu( wxDataViewEvent& aEvent )
390 {
391  auto const sel = m_tree_ctrl->GetSelection();
392  auto type = sel.IsOk() ? m_adapter->GetTypeFor( sel ) : LIB_TREE_NODE::INVALID;
393 
394  if( m_menus[type] )
395  {
396  m_menuActive = true;
397  PopupMenu( m_menus[type].get() );
398  m_menuActive = false;
399  }
400 }
401 
402 
403 wxDEFINE_EVENT( COMPONENT_PRESELECTED, wxCommandEvent );
404 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:225
void toggleExpand(const wxDataViewItem &aTreeId)
Expands or collapses a node, switching it to the opposite state.
Definition: lib_tree.cpp:213
std::vector< std::unique_ptr< wxMenu > > m_menus
Right click context menus for each tree level
Definition: lib_tree.h:186
wxDataViewItem GetPrevItem(wxDataViewCtrl const &aView, wxDataViewItem const &aItem)
Get the previous item in list order.
void onQueryEnter(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:308
STATE getState() const
Returns the component tree widget state.
Definition: lib_tree.cpp:264
wxHtmlWindow * m_details_ctrl
Definition: lib_tree.h:183
void centerIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:236
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:182
LIB_ID selection
Current selection, might be not valid if nothing was selected
Definition: lib_tree.h:153
bool m_filtering
Flag indicating whether the results are filtered using the search query
Definition: lib_tree.h:192
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:372
STATE m_unfilteredState
State of the widget before any filters applied
Definition: lib_tree.h:195
void onQueryCharHook(wxKeyEvent &aEvent)
Definition: lib_tree.cpp:315
wxDEFINE_EVENT(COMPONENT_PRESELECTED, wxCommandEvent)
bool empty() const
Definition: utf8.h:108
bool m_menuActive
Flag indicating whether a right-click context menu is active
Definition: lib_tree.h:189
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
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:144
void onTreeActivate(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:351
void onQueryText(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:298
void Unselect()
Unselect currently selected item in wxDataViewCtrl.
Definition: lib_tree.cpp:174
void onTreeSelect(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:345
wxDataViewItem GetNextItem(wxDataViewCtrl const &aView, wxDataViewItem const &aItem)
Get the next item in list order.
void onDetailsLink(wxHtmlLinkEvent &aEvent)
Definition: lib_tree.cpp:365
void SetFocus() override
Definition: lib_tree.cpp:204
void ExpandLibId(const LIB_ID &aLibId)
Expand and item i the tree widget.
Definition: lib_tree.cpp:179
WIDGETS
Flags to select extra widgets
Definition: lib_tree.h:46
void Regenerate()
Regenerates the tree.
Definition: lib_tree.cpp:185
void expandIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:243
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:250
LIB_TREE_MODEL_ADAPTER::PTR m_adapter
Definition: lib_tree.h:179
BITMAP_DEF GetBitmap(GRAPHIC_PINSHAPE shape)
Definition: pin_shape.cpp:70
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97
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:168
void SelectLibId(const LIB_ID &aLibId)
Select an item in the tree widget.
Definition: lib_tree.cpp:162
wxTextCtrl * m_query_ctrl
Definition: lib_tree.h:181
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:389
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.