KiCad PCB EDA Suite
component_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 "component_tree.h"
26 #include <generate_alias_info.h>
27 #include <wxdataviewctrl_helpers.h>
28 
29 #include <wx/artprov.h>
30 #include <wx/sizer.h>
31 #include <wx/statbmp.h>
32 #include <wx/html/htmlwin.h>
33 
34 #include <symbol_lib_table.h>
35 
36 
37 COMPONENT_TREE::COMPONENT_TREE( wxWindow* aParent, SYMBOL_LIB_TABLE* aSymLibTable,
38  CMP_TREE_MODEL_ADAPTER_BASE::PTR& aAdapter, WIDGETS aWidgets, wxHtmlWindow* aDetails )
39  : wxPanel( aParent ),
40  m_sym_lib_table( aSymLibTable ),
41  m_adapter( aAdapter ),
42  m_query_ctrl( nullptr ),
43  m_details_ctrl( nullptr ),
44  m_menuActive( false ),
45  m_filtering( false )
46 {
47  // create space for context menu pointers, INVALID is the max value
49 
50  auto sizer = new wxBoxSizer( wxVERTICAL );
51 
52  // Search text control
53  if( aWidgets & SEARCH )
54  {
55  auto search_sizer = new wxBoxSizer( wxHORIZONTAL );
56 
57  m_query_ctrl = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
58  wxDefaultSize, wxTE_PROCESS_ENTER );
59 
60 // Additional visual cue for GTK, which hides the placeholder text on focus
61 #ifdef __WXGTK__
62  search_sizer->Add( new wxStaticBitmap( this, wxID_ANY,
63  wxArtProvider::GetBitmap( wxART_FIND, wxART_FRAME_ICON ) ),
64  0, wxALIGN_CENTER | wxALL, 5 );
65 #endif
66 
67  search_sizer->Add( m_query_ctrl, 1, wxALL | wxEXPAND, 5 );
68  sizer->Add( search_sizer, 0, wxEXPAND, 5 );
69 
70  m_query_ctrl->Bind( wxEVT_TEXT, &COMPONENT_TREE::onQueryText, this );
71  m_query_ctrl->Bind( wxEVT_TEXT_ENTER, &COMPONENT_TREE::onQueryEnter, this );
72  m_query_ctrl->Bind( wxEVT_CHAR_HOOK, &COMPONENT_TREE::onQueryCharHook, this );
73  }
74 
75  // Component tree
76  m_tree_ctrl =
77  new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxDV_SINGLE );
78  m_adapter->AttachTo( m_tree_ctrl );
79 
80  sizer->Add( m_tree_ctrl, 1, wxALL | wxEXPAND, 5 );
81 
82  // Description panel
83  if( aWidgets & DETAILS )
84  {
85  if( !aDetails )
86  {
87  auto html_sz = ConvertDialogToPixels( wxPoint( 80, 80 ) );
88 
89  m_details_ctrl = new wxHtmlWindow(
90  this, wxID_ANY, wxDefaultPosition, wxSize( html_sz.x, html_sz.y ),
91  wxHW_SCROLLBAR_AUTO );
92 
93  sizer->Add( m_details_ctrl, 1, wxALL | wxEXPAND, 5 );
94  }
95  else
96  {
97  m_details_ctrl = aDetails;
98  }
99 
100  m_details_ctrl->Bind( wxEVT_HTML_LINK_CLICKED, &COMPONENT_TREE::onDetailsLink, this );
101  }
102 
103  SetSizer( sizer );
104 
105  m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &COMPONENT_TREE::onTreeActivate, this );
106  m_tree_ctrl->Bind( wxEVT_DATAVIEW_SELECTION_CHANGED, &COMPONENT_TREE::onTreeSelect, this );
107  m_tree_ctrl->Bind( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, &COMPONENT_TREE::onContextMenu, this );
108 
109  Bind( COMPONENT_PRESELECTED, &COMPONENT_TREE::onPreselect, this );
110 
111  // If wxTextCtrl::SetHint() is called before binding wxEVT_TEXT, the event
112  // handler will intermittently fire.
113  if( m_query_ctrl )
114  {
115  m_query_ctrl->SetHint( _( "Search" ) );
116  m_query_ctrl->SetFocus();
117  m_query_ctrl->SetValue( wxEmptyString );
118  }
119 
120  // There may be a part preselected in the model. Make sure it is displayed.
122 
123  Layout();
124  sizer->Fit( this );
125 
126 #ifdef __WXGTK__
127  // Scrollbars must be always enabled to prevent an infinite event loop
128  // more details: http://trac.wxwidgets.org/ticket/18141
129  if( m_details_ctrl )
130  m_details_ctrl->ShowScrollbars( wxSHOW_SB_ALWAYS, wxSHOW_SB_ALWAYS );
131 #endif /* __WXGTK__ */
132 }
133 
134 
136 {
137  auto sel = m_tree_ctrl->GetSelection();
138 
139  if( !sel )
140  {
141  LIB_ID emptyId;
142 
143  return emptyId;
144  }
145 
146  if( aUnit )
147  *aUnit = m_adapter->GetUnitFor( sel );
148 
149  return m_adapter->GetAliasFor( sel );
150 }
151 
152 
153 void COMPONENT_TREE::SelectLibId( const LIB_ID& aLibId )
154 {
155  selectIfValid( m_adapter->FindItem( aLibId ) );
156 }
157 
158 
160 {
161  m_tree_ctrl->UnselectAll();
162 }
163 
164 
166 {
167  STATE current;
168 
169  // Store the state
170  if( !m_filtering )
172 
173  wxString filter = m_query_ctrl->GetValue();
174  m_adapter->UpdateSearchString( filter );
175  m_filtering = !filter.IsEmpty();
177 
178  // Restore the state
179  if( !m_filtering )
181 }
182 
183 
185 {
186  if( m_query_ctrl )
187  m_query_ctrl->SetFocus();
188  else
189  m_tree_ctrl->SetFocus();
190 }
191 
192 
193 void COMPONENT_TREE::toggleExpand( const wxDataViewItem& aTreeId )
194 {
195  if( !aTreeId.IsOk() )
196  return;
197 
198  if( m_tree_ctrl->IsExpanded( aTreeId ) )
199  m_tree_ctrl->Collapse( aTreeId );
200  else
201  m_tree_ctrl->Expand( aTreeId );
202 }
203 
204 
205 void COMPONENT_TREE::selectIfValid( const wxDataViewItem& aTreeId )
206 {
207  if( aTreeId.IsOk() )
208  {
209  m_tree_ctrl->EnsureVisible( aTreeId );
210  m_tree_ctrl->Select( aTreeId );
212  }
213 }
214 
215 
217 {
218  wxCommandEvent event( COMPONENT_PRESELECTED );
219  wxPostEvent( this, event );
220 }
221 
222 
224 {
225  wxCommandEvent event( COMPONENT_SELECTED );
226  wxPostEvent( this, event );
227 }
228 
229 
231 {
232  STATE state;
233  wxDataViewItemArray items;
234  m_adapter->GetChildren( wxDataViewItem( nullptr ), items );
235 
236  for( const auto& item : items )
237  {
238  if( m_tree_ctrl->IsExpanded( item ) )
239  state.expanded.push_back( item );
240  }
241 
242  state.selection = GetSelectedLibId();
243 
244  return state;
245 }
246 
247 
248 void COMPONENT_TREE::setState( const STATE& aState )
249 {
250  m_tree_ctrl->Freeze();
251 
252  for( const auto& item : aState.expanded )
253  m_tree_ctrl->Expand( item );
254 
255  // wxDataViewCtrl cannot be frozen when a selection
256  // command is issued, otherwise it selects a random item (Windows)
257  m_tree_ctrl->Thaw();
258 
259  if( !aState.selection.GetLibItemName().empty() || !aState.selection.GetLibNickname().empty() )
260  SelectLibId( aState.selection );
261 }
262 
263 
264 void COMPONENT_TREE::onQueryText( wxCommandEvent& aEvent )
265 {
266  Regenerate();
267 
268  // Required to avoid interaction with SetHint()
269  // See documentation for wxTextEntry::SetHint
270  aEvent.Skip();
271 }
272 
273 
274 void COMPONENT_TREE::onQueryEnter( wxCommandEvent& aEvent )
275 {
276  if( GetSelectedLibId().IsValid() )
277  postSelectEvent();
278 }
279 
280 
281 void COMPONENT_TREE::onQueryCharHook( wxKeyEvent& aKeyStroke )
282 {
283  auto const sel = m_tree_ctrl->GetSelection();
284  auto type = sel.IsOk() ? m_adapter->GetTypeFor( sel ) : CMP_TREE_NODE::INVALID;
285 
286  switch( aKeyStroke.GetKeyCode() )
287  {
288  case WXK_UP:
290  break;
291 
292  case WXK_DOWN:
294  break;
295 
296  case WXK_RETURN:
297  if( type == CMP_TREE_NODE::LIB )
298  {
299  toggleExpand( sel );
300  break;
301  }
302  /* fall through, so the selected component will be treated as the selected one */
303 
304  default:
305  aKeyStroke.Skip(); // Any other key: pass on to search box directly.
306  break;
307  }
308 }
309 
310 
311 void COMPONENT_TREE::onTreeSelect( wxDataViewEvent& aEvent )
312 {
314 }
315 
316 
317 void COMPONENT_TREE::onTreeActivate( wxDataViewEvent& aEvent )
318 {
319  if( !GetSelectedLibId().IsValid() )
320  {
321  // Expand library/part units subtree
322  toggleExpand( m_tree_ctrl->GetSelection() );
323  }
324  else
325  {
326  postSelectEvent();
327  }
328 }
329 
330 
331 void COMPONENT_TREE::onDetailsLink( wxHtmlLinkEvent& aEvent )
332 {
333  const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
334  ::wxLaunchDefaultBrowser( info.GetHref() );
335 }
336 
337 
338 void COMPONENT_TREE::onPreselect( wxCommandEvent& aEvent )
339 {
340  if( m_details_ctrl )
341  {
342  int unit = 0;
343  LIB_ID id = GetSelectedLibId( &unit );
344 
345  if( id.IsValid() )
346  m_details_ctrl->SetPage( GenerateAliasInfo( m_sym_lib_table, id, unit ) );
347  else
348  m_details_ctrl->SetPage( wxEmptyString );
349  }
350 
351  aEvent.Skip();
352 }
353 
354 
355 void COMPONENT_TREE::onContextMenu( wxDataViewEvent& aEvent )
356 {
357  auto const sel = m_tree_ctrl->GetSelection();
358  auto type = sel.IsOk() ? m_adapter->GetTypeFor( sel ) : CMP_TREE_NODE::INVALID;
359 
360  if( m_menus[type] )
361  {
362  m_menuActive = true;
363  PopupMenu( m_menus[type].get() );
364  m_menuActive = false;
365  }
366 }
367 
368 
369 wxDEFINE_EVENT( COMPONENT_PRESELECTED, wxCommandEvent );
370 wxDEFINE_EVENT( COMPONENT_SELECTED, wxCommandEvent );
void onQueryCharHook(wxKeyEvent &aEvent)
std::vector< std::unique_ptr< wxMenu > > m_menus
Right click context menus for each tree level
wxDataViewItem GetPrevItem(wxDataViewCtrl const &aView, wxDataViewItem const &aItem)
Get the previous item in list order.
wxDEFINE_EVENT(COMPONENT_PRESELECTED, wxCommandEvent)
void Regenerate()
Regenerates the tree.
void onPreselect(wxCommandEvent &aEvent)
STATE getState() const
Returns the component tree widget state.
void onTreeActivate(wxDataViewEvent &aEvent)
wxHtmlWindow * m_details_ctrl
SYMBOL_LIB_TABLE * m_sym_lib_table
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
std::vector< wxDataViewItem > expanded
List of expanded nodes
bool m_filtering
Flag indicating whether the results are filtered using the search query
bool m_menuActive
Flag indicating whether a right-click context menu is active
void selectIfValid(const wxDataViewItem &aTreeId)
If a wxDataViewitem is valid, select it and post a selection event.
void setState(const STATE &aState)
Restores the component tree widget state from an object.
void postPreselectEvent()
Post a wxEVT_DATAVIEW_SELECTION_CHANGED to notify the selection handler that a new part has been pres...
wxString GenerateAliasInfo(SYMBOL_LIB_TABLE *aSymLibTable, LIB_ID const &aLibId, int aUnit)
Return an HTML page describing a LIB_ID in a SYMBOL_LIB_TABLE.
void onContextMenu(wxDataViewEvent &aEvent)
LIB_ID GetSelectedLibId(int *aUnit=nullptr) const
For multi-unit components, if the user selects the component itself rather than picking an individual...
bool empty() const
Definition: utf8.h:108
const UTF8 & GetLibItemName() const
Definition: lib_id.h:118
wxObjectDataPtr< CMP_TREE_MODEL_ADAPTER_BASE > PTR
Reference-counting container for a pointer to CMP_TREE_MODEL_ADAPTER_BASE.
wxDataViewCtrl * m_tree_ctrl
void onQueryText(wxCommandEvent &aEvent)
STATE m_unfilteredState
State of the widget before any filters applied
wxDataViewItem GetNextItem(wxDataViewCtrl const &aView, wxDataViewItem const &aItem)
Get the next item in list order.
LIB_ID selection
Current selection, might be not valid if nothing was selected
void SelectLibId(const LIB_ID &aLibId)
Select a part in the tree widget.
void Unselect()
Unselect currently selected item in wxDataViewCtrl.
WIDGETS
Flags to select extra widgets
COMPONENT_TREE(wxWindow *aParent, SYMBOL_LIB_TABLE *aSymLibTable, CMP_TREE_MODEL_ADAPTER_BASE::PTR &aAdapter, WIDGETS aWidgets=ALL, wxHtmlWindow *aDetails=nullptr)
Construct a component tree.
void onQueryEnter(wxCommandEvent &aEvent)
void SetFocus() override
BITMAP_DEF GetBitmap(GRAPHIC_PINSHAPE shape)
Definition: pin_shape.cpp:70
CMP_TREE_MODEL_ADAPTER_BASE::PTR m_adapter
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:101
void onDetailsLink(wxHtmlLinkEvent &aEvent)
Structure storing state of the component tree widget.
wxDataViewCtrl helper functions.
void toggleExpand(const wxDataViewItem &aTreeId)
Expands or collapses a node, switching it to the opposite state.
void onTreeSelect(wxDataViewEvent &aEvent)
void postSelectEvent()
Post COMPONENT_SELECTED event to notify the selection handler that a part has been selected...
wxTextCtrl * m_query_ctrl