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  m_details_ctrl = new wxHtmlWindow(
89  this, wxID_ANY, wxDefaultPosition, wxSize( html_sz.x, html_sz.y ),
90  wxHW_SCROLLBAR_AUTO );
91 
92  sizer->Add( m_details_ctrl, 1, wxALL | wxEXPAND, 5 );
93  }
94  else
95  {
96  m_details_ctrl = aDetails;
97  }
98  m_details_ctrl->Bind( wxEVT_HTML_LINK_CLICKED, &COMPONENT_TREE::onDetailsLink, this );
99  }
100 
101  SetSizer( sizer );
102 
103  m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &COMPONENT_TREE::onTreeActivate, this );
104  m_tree_ctrl->Bind( wxEVT_DATAVIEW_SELECTION_CHANGED, &COMPONENT_TREE::onTreeSelect, this );
105  m_tree_ctrl->Bind( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, &COMPONENT_TREE::onContextMenu, this );
106 
107  Bind( COMPONENT_PRESELECTED, &COMPONENT_TREE::onPreselect, this );
108 
109  // If wxTextCtrl::SetHint() is called before binding wxEVT_TEXT, the event
110  // handler will intermittently fire.
111  if( m_query_ctrl )
112  {
113  m_query_ctrl->SetHint( _( "Search" ) );
114  m_query_ctrl->SetFocus();
115  m_query_ctrl->SetValue( wxEmptyString );
116  }
117 
118  // There may be a part preselected in the model. Make sure it is displayed.
120 
121  Layout();
122  sizer->Fit( this );
123 }
124 
125 
127 {
128  auto sel = m_tree_ctrl->GetSelection();
129 
130  if( !sel )
131  {
132  LIB_ID emptyId;
133 
134  return emptyId;
135  }
136 
137  if( aUnit )
138  *aUnit = m_adapter->GetUnitFor( sel );
139 
140  return m_adapter->GetAliasFor( sel );
141 }
142 
143 
144 void COMPONENT_TREE::SelectLibId( const LIB_ID& aLibId )
145 {
146  selectIfValid( m_adapter->FindItem( aLibId ) );
147 }
148 
149 
151 {
152  m_tree_ctrl->UnselectAll();
153 }
154 
155 
157 {
158  STATE current;
159 
160  // Store the state
161  if( !m_filtering )
163  else
164  current = getState();
165 
166  wxString filter = m_query_ctrl->GetValue();
167  m_adapter->UpdateSearchString( filter );
168  m_filtering = !filter.IsEmpty();
170 
171  // Restore the state
172  setState( m_filtering ? current : m_unfilteredState );
173 }
174 
175 
176 void COMPONENT_TREE::toggleExpand( const wxDataViewItem& aTreeId )
177 {
178  if( !aTreeId.IsOk() )
179  return;
180 
181  if( m_tree_ctrl->IsExpanded( aTreeId ) )
182  m_tree_ctrl->Collapse( aTreeId );
183  else
184  m_tree_ctrl->Expand( aTreeId );
185 }
186 
187 
188 void COMPONENT_TREE::selectIfValid( const wxDataViewItem& aTreeId )
189 {
190  if( aTreeId.IsOk() )
191  {
192  m_tree_ctrl->EnsureVisible( aTreeId );
193  m_tree_ctrl->Select( aTreeId );
195  }
196 }
197 
198 
200 {
201  wxCommandEvent event( COMPONENT_PRESELECTED );
202  wxPostEvent( this, event );
203 }
204 
205 
207 {
208  wxCommandEvent event( COMPONENT_SELECTED );
209  wxPostEvent( this, event );
210 }
211 
212 
214 {
215  STATE state;
216  wxDataViewItemArray items;
217  m_adapter->GetChildren( wxDataViewItem( nullptr ), items );
218 
219  for( const auto& item : items )
220  {
221  if( m_tree_ctrl->IsExpanded( item ) )
222  state.expanded.push_back( item );
223  }
224 
225  state.selection = GetSelectedLibId();
226 
227  return state;
228 }
229 
230 
231 void COMPONENT_TREE::setState( const STATE& aState )
232 {
233  m_tree_ctrl->Freeze();
234 
235  for( const auto& item : aState.expanded )
236  m_tree_ctrl->Expand( item );
237 
238  // wxDataViewCtrl cannot be frozen when a selection
239  // command is issued, otherwise it selects a random item (Windows)
240  m_tree_ctrl->Thaw();
241 
242  if( !aState.selection.GetLibItemName().empty() || !aState.selection.GetLibNickname().empty() )
243  SelectLibId( aState.selection );
244 }
245 
246 
247 void COMPONENT_TREE::onQueryText( wxCommandEvent& aEvent )
248 {
249  Regenerate();
250 
251  // Required to avoid interaction with SetHint()
252  // See documentation for wxTextEntry::SetHint
253  aEvent.Skip();
254 }
255 
256 
257 void COMPONENT_TREE::onQueryEnter( wxCommandEvent& aEvent )
258 {
259  if( GetSelectedLibId().IsValid() )
260  postSelectEvent();
261 }
262 
263 
264 void COMPONENT_TREE::onQueryCharHook( wxKeyEvent& aKeyStroke )
265 {
266  auto const sel = m_tree_ctrl->GetSelection();
267  auto type = sel.IsOk() ? m_adapter->GetTypeFor( sel ) : CMP_TREE_NODE::INVALID;
268 
269  switch( aKeyStroke.GetKeyCode() )
270  {
271  case WXK_UP:
273  break;
274 
275  case WXK_DOWN:
277  break;
278 
279  case WXK_RETURN:
280  if( type == CMP_TREE_NODE::LIB )
281  {
282  toggleExpand( sel );
283  break;
284  }
285  /* fall through, so the selected component will be treated as the selected one */
286 
287  default:
288  aKeyStroke.Skip(); // Any other key: pass on to search box directly.
289  break;
290  }
291 }
292 
293 
294 void COMPONENT_TREE::onTreeSelect( wxDataViewEvent& aEvent )
295 {
297 }
298 
299 
300 void COMPONENT_TREE::onTreeActivate( wxDataViewEvent& aEvent )
301 {
302  if( !GetSelectedLibId().IsValid() )
303  {
304  // Expand library/part units subtree
305  toggleExpand( m_tree_ctrl->GetSelection() );
306  }
307  else
308  {
309  postSelectEvent();
310  }
311 }
312 
313 
314 void COMPONENT_TREE::onDetailsLink( wxHtmlLinkEvent& aEvent )
315 {
316  const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
317  ::wxLaunchDefaultBrowser( info.GetHref() );
318 }
319 
320 
321 void COMPONENT_TREE::onPreselect( wxCommandEvent& aEvent )
322 {
323  if( m_details_ctrl )
324  {
325  int unit = 0;
326  LIB_ID id = GetSelectedLibId( &unit );
327 
328  if( id.IsValid() )
329  m_details_ctrl->SetPage( GenerateAliasInfo( m_sym_lib_table, id, unit ) );
330  else
331  m_details_ctrl->SetPage( wxEmptyString );
332  }
333 
334  aEvent.Skip();
335 }
336 
337 
338 void COMPONENT_TREE::onContextMenu( wxDataViewEvent& aEvent )
339 {
340  auto const sel = m_tree_ctrl->GetSelection();
341  auto type = sel.IsOk() ? m_adapter->GetTypeFor( sel ) : CMP_TREE_NODE::INVALID;
342 
343  if( m_menus[type] )
344  {
345  m_menuActive = true;
346  PopupMenu( m_menus[type].get() );
347  m_menuActive = false;
348  }
349 }
350 
351 
352 wxDEFINE_EVENT( COMPONENT_PRESELECTED, wxCommandEvent );
353 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)
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