KiCad PCB EDA Suite
lib_tree.cpp
Go to the documentation of this file.
1 /*
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-2020 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 <widgets/lib_tree.h>
26 #include <macros.h>
27 #include <wxdataviewctrl_helpers.h>
28 #include <wx/artprov.h>
29 #include <wx/sizer.h>
30 #include <wx/html/htmlwin.h>
31 #include <tool/tool_interactive.h>
32 #include <tool/tool_manager.h>
33 
34 
35 LIB_TREE::LIB_TREE( wxWindow* aParent, LIB_TABLE* aLibTable,
36  wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>& 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 {
45  auto sizer = new wxBoxSizer( wxVERTICAL );
46 
47  // Search text control
48  if( aWidgets & SEARCH )
49  {
50  auto search_sizer = new wxBoxSizer( wxHORIZONTAL );
51 
52  m_query_ctrl = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
53  wxDefaultSize, wxTE_PROCESS_ENTER );
54 
55 // Additional visual cue for GTK, which hides the placeholder text on focus
56 #ifdef __WXGTK__
57  auto bitmap = new wxStaticBitmap( this, wxID_ANY, wxArtProvider::GetBitmap( wxART_FIND, wxART_FRAME_ICON ) );
58 
59  search_sizer->Add( bitmap, 0, wxALIGN_CENTER | wxRIGHT, 5 );
60 #endif
61  search_sizer->Add( m_query_ctrl, 1, wxEXPAND, 5 );
62 
63  sizer->Add( search_sizer, 0, wxEXPAND, 5 );
64 
65  m_query_ctrl->Bind( wxEVT_TEXT, &LIB_TREE::onQueryText, this );
66  m_query_ctrl->Bind( wxEVT_TEXT_ENTER, &LIB_TREE::onQueryEnter, this );
67  m_query_ctrl->Bind( wxEVT_CHAR_HOOK, &LIB_TREE::onQueryCharHook, this );
68  }
69 
70  // Tree control
71  m_tree_ctrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
72  wxDV_SINGLE );
73  m_adapter->AttachTo( m_tree_ctrl );
74 
75  if( aWidgets & DETAILS )
76  sizer->AddSpacer( 5 );
77 
78  sizer->Add( m_tree_ctrl, 5, wxRIGHT | wxBOTTOM | wxEXPAND, 1 );
79 
80  // Description panel
81  if( aWidgets & DETAILS )
82  {
83  if( !aDetails )
84  {
85  wxPoint html_size = ConvertDialogToPixels( wxPoint( 80, 80 ) );
86 
87  m_details_ctrl = new wxHtmlWindow( this, wxID_ANY, wxDefaultPosition,
88  wxSize( html_size.x, html_size.y ),
89  wxHW_SCROLLBAR_AUTO );
90 
91  sizer->Add( m_details_ctrl, 2, wxTOP | wxEXPAND, 5 );
92  }
93  else
94  {
95  m_details_ctrl = aDetails;
96  }
97 
98  m_details_ctrl->Bind( wxEVT_HTML_LINK_CLICKED, &LIB_TREE::onDetailsLink, this );
99  }
100 
101  SetSizer( sizer );
102 
103  m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &LIB_TREE::onTreeActivate, this );
104  m_tree_ctrl->Bind( wxEVT_DATAVIEW_SELECTION_CHANGED, &LIB_TREE::onTreeSelect, this );
105  m_tree_ctrl->Bind( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, &LIB_TREE::onContextMenu, this );
106 
107  Bind( COMPONENT_PRESELECTED, &LIB_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( _( "Filter" ) );
114  m_query_ctrl->SetFocus();
115  m_query_ctrl->SetValue( wxEmptyString );
116 
117  // Force an update of the adapter with the empty text to ensure preselect is done
118  Regenerate( false );
119  }
120  else
121  {
122  // There may be a part preselected in the model. Make sure it is displayed.
123  // Regenerate does this in the other branch
125  }
126 
127  Layout();
128  sizer->Fit( this );
129 
130 #ifdef __WXGTK__
131  // Scrollbars must be always enabled to prevent an infinite event loop
132  // more details: http://trac.wxwidgets.org/ticket/18141
133  if( m_details_ctrl )
134  m_details_ctrl->ShowScrollbars( wxSHOW_SB_ALWAYS, wxSHOW_SB_ALWAYS );
135 #endif /* __WXGTK__ */
136 }
137 
138 
140 {
141  // Save the column widths to the config file
142  m_adapter->SaveColWidths();
143 
144  m_adapter->SavePinnedItems();
145 }
146 
147 
149 {
150  auto sel = m_tree_ctrl->GetSelection();
151 
152  if( !sel )
153  {
154  LIB_ID emptyId;
155 
156  return emptyId;
157  }
158 
159  if( aUnit )
160  *aUnit = m_adapter->GetUnitFor( sel );
161 
162  return m_adapter->GetAliasFor( sel );
163 }
164 
165 
167 {
168  auto sel = m_tree_ctrl->GetSelection();
169 
170  if( !sel )
171  return nullptr;
172 
173  return m_adapter->GetTreeNodeFor( sel );
174 }
175 
176 
177 void LIB_TREE::SelectLibId( const LIB_ID& aLibId )
178 {
179  selectIfValid( m_adapter->FindItem( aLibId ) );
180 }
181 
182 
183 void LIB_TREE::CenterLibId( const LIB_ID& aLibId )
184 {
185  centerIfValid( m_adapter->FindItem( aLibId ) );
186 }
187 
188 
190 {
191  m_tree_ctrl->UnselectAll();
192 }
193 
194 void LIB_TREE::ExpandLibId( const LIB_ID& aLibId )
195 {
196  expandIfValid( m_adapter->FindItem( aLibId ) );
197 }
198 
199 
200 void LIB_TREE::Regenerate( bool aKeepState )
201 {
202  STATE current;
203 
204  // Store the state
205  if( aKeepState )
206  current = getState();
207 
208  wxString filter = m_query_ctrl->GetValue();
209  m_adapter->UpdateSearchString( filter, aKeepState );
211 
212  // Restore the state
213  if( aKeepState )
214  setState( current );
215 }
216 
217 
219 {
220  m_adapter->RefreshTree();
221 }
222 
223 
225 {
226  if( m_query_ctrl )
227  return m_query_ctrl;
228  else
229  return m_tree_ctrl;
230 }
231 
232 
233 void LIB_TREE::toggleExpand( const wxDataViewItem& aTreeId )
234 {
235  if( !aTreeId.IsOk() )
236  return;
237 
238  if( m_tree_ctrl->IsExpanded( aTreeId ) )
239  m_tree_ctrl->Collapse( aTreeId );
240  else
241  m_tree_ctrl->Expand( aTreeId );
242 }
243 
244 
245 void LIB_TREE::selectIfValid( const wxDataViewItem& aTreeId )
246 {
247  if( aTreeId.IsOk() )
248  {
249  m_tree_ctrl->EnsureVisible( aTreeId );
250  m_tree_ctrl->Select( aTreeId );
252  }
253 }
254 
255 
256 void LIB_TREE::centerIfValid( const wxDataViewItem& aTreeId )
257 {
258  if( aTreeId.IsOk() )
259  m_tree_ctrl->EnsureVisible( aTreeId );
260 }
261 
262 
263 void LIB_TREE::expandIfValid( const wxDataViewItem& aTreeId )
264 {
265  if( aTreeId.IsOk() && !m_tree_ctrl->IsExpanded( aTreeId ) )
266  m_tree_ctrl->Expand( aTreeId );
267 }
268 
269 
271 {
272  wxCommandEvent event( COMPONENT_PRESELECTED );
273  wxPostEvent( this, event );
274 }
275 
276 
278 {
279  wxCommandEvent event( COMPONENT_SELECTED );
280  wxPostEvent( this, event );
281 }
282 
283 
285 {
286  STATE state;
287  wxDataViewItemArray items;
288  m_adapter->GetChildren( wxDataViewItem( nullptr ), items );
289 
290  for( const auto& item : items )
291  {
292  if( m_tree_ctrl->IsExpanded( item ) )
293  state.expanded.push_back( item );
294  }
295 
296  state.selection = GetSelectedLibId();
297 
298  return state;
299 }
300 
301 
302 void LIB_TREE::setState( const STATE& aState )
303 {
304  m_tree_ctrl->Freeze();
305 
306  for( const auto& item : aState.expanded )
307  m_tree_ctrl->Expand( item );
308 
309  // wxDataViewCtrl cannot be frozen when a selection
310  // command is issued, otherwise it selects a random item (Windows)
311  m_tree_ctrl->Thaw();
312 
313  if( !aState.selection.GetLibItemName().empty() || !aState.selection.GetLibNickname().empty() )
314  SelectLibId( aState.selection );
315 }
316 
317 
318 void LIB_TREE::onQueryText( wxCommandEvent& aEvent )
319 {
320  Regenerate( false );
321 
322  // Required to avoid interaction with SetHint()
323  // See documentation for wxTextEntry::SetHint
324  aEvent.Skip();
325 }
326 
327 
328 void LIB_TREE::onQueryEnter( wxCommandEvent& aEvent )
329 {
330  if( GetSelectedLibId().IsValid() )
331  postSelectEvent();
332 }
333 
334 
335 void LIB_TREE::onQueryCharHook( wxKeyEvent& aKeyStroke )
336 {
337  auto const sel = m_tree_ctrl->GetSelection();
338  auto type = sel.IsOk() ? m_adapter->GetTypeFor( sel ) : LIB_TREE_NODE::INVALID;
339 
340  switch( aKeyStroke.GetKeyCode() )
341  {
342  case WXK_UP:
344  break;
345 
346  case WXK_DOWN:
348  break;
349 
350  case WXK_ADD:
351  if( type == LIB_TREE_NODE::LIB )
352  m_tree_ctrl->Expand( sel );
353 
354  break;
355 
356  case WXK_SUBTRACT:
357  if( type == LIB_TREE_NODE::LIB )
358  m_tree_ctrl->Collapse( sel );
359 
360  break;
361 
362  case WXK_RETURN:
363  if( type == LIB_TREE_NODE::LIB )
364  {
365  toggleExpand( sel );
366  break;
367  }
368  // Intentionally fall through, so the selected component will be treated as the selected one
370 
371  default:
372  aKeyStroke.Skip(); // Any other key: pass on to search box directly.
373  break;
374  }
375 }
376 
377 
378 void LIB_TREE::onTreeSelect( wxDataViewEvent& aEvent )
379 {
380  if( !m_tree_ctrl->IsFrozen() )
382 }
383 
384 
385 void LIB_TREE::onTreeActivate( wxDataViewEvent& aEvent )
386 {
387  if( !GetSelectedLibId().IsValid() )
388  {
389  // Expand library/part units subtree
390  toggleExpand( m_tree_ctrl->GetSelection() );
391  }
392  else
393  {
394  postSelectEvent();
395  }
396 }
397 
398 
399 void LIB_TREE::onDetailsLink( wxHtmlLinkEvent& aEvent )
400 {
401  const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
402  ::wxLaunchDefaultBrowser( info.GetHref() );
403 }
404 
405 
406 void LIB_TREE::onPreselect( wxCommandEvent& aEvent )
407 {
408  if( m_details_ctrl )
409  {
410  int unit = 0;
411  LIB_ID id = GetSelectedLibId( &unit );
412 
413  wxString htmlColor = GetBackgroundColour().GetAsString( wxC2S_HTML_SYNTAX );
414  wxString textColor = GetForegroundColour().GetAsString( wxC2S_HTML_SYNTAX );
415  wxString linkColor = wxSystemSettings::GetColour( wxSYS_COLOUR_HOTLIGHT )
416  .GetAsString( wxC2S_HTML_SYNTAX );
417 
418  wxString html = wxString::Format( wxT( "<html><body bgcolor='%s' text='%s' link='%s'>" ),
419  htmlColor, textColor, linkColor );
420 
421  if( id.IsValid() )
422  html.Append( m_adapter->GenerateInfo( id, unit ) );
423 
424  html.Append( wxT( "</body></html>" ) );
425 
426  m_details_ctrl->SetPage( html );
427  }
428 
429  aEvent.Skip();
430 }
431 
432 
433 void LIB_TREE::onContextMenu( wxDataViewEvent& aEvent )
434 {
435  TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool();
436 
437  if( tool )
438  {
439  tool->Activate();
441  tool->GetToolMenu().ShowContextMenu();
442 
444  tool->GetManager()->DispatchContextMenu( evt );
445  }
446 }
447 
448 
449 wxDEFINE_EVENT( COMPONENT_PRESELECTED, wxCommandEvent );
450 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:245
void DispatchContextMenu(const TOOL_EVENT &aEvent)
Handle context menu related events.
const UTF8 & GetLibItemName() const
Definition: lib_id.h:106
void toggleExpand(const wxDataViewItem &aTreeId)
Expand or collapse a node, switching it to the opposite state.
Definition: lib_tree.cpp:233
LIB_TREE(wxWindow *aParent, LIB_TABLE *aLibTable, wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > &aAdapter, WIDGETS aWidgets=ALL, wxHtmlWindow *aDetails=nullptr)
Construct a component tree.
Definition: lib_tree.cpp:35
STATE getState() const
Return the component tree widget state.
Definition: lib_tree.cpp:284
wxDataViewItem GetPrevItem(wxDataViewCtrl const &aView, wxDataViewItem const &aItem)
Get the previous item in list order.
void onQueryEnter(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:328
wxWindow * GetFocusTarget()
Definition: lib_tree.cpp:224
wxHtmlWindow * m_details_ctrl
Definition: lib_tree.h:176
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > m_adapter
Definition: lib_tree.h:172
void centerIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:256
void postSelectEvent()
Post COMPONENT_SELECTED event to notify the selection handler that a part has been selected.
Definition: lib_tree.cpp:277
wxDataViewCtrl * m_tree_ctrl
Definition: lib_tree.h:175
TOOL_MENU & GetToolMenu()
LIB_ID selection
Definition: lib_tree.h:145
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:83
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
~LIB_TREE() override
Definition: lib_tree.cpp:139
void onPreselect(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:406
This file contains miscellaneous commonly used macros and functions.
TOOL_MANAGER * GetManager() const
Return the instance of TOOL_MANAGER that takes care of the tool.
Definition: tool_base.h:144
void onQueryCharHook(wxKeyEvent &aEvent)
Definition: lib_tree.cpp:335
wxDEFINE_EVENT(COMPONENT_PRESELECTED, wxCommandEvent)
LIB_TREE_NODE * GetCurrentTreeNode() const
Definition: lib_tree.cpp:166
void onTreeActivate(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:385
Generic, UI-independent tool event.
Definition: tool_event.h:173
void onQueryText(wxCommandEvent &aEvent)
Definition: lib_tree.cpp:318
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:148
void Regenerate(bool aKeepState)
Regenerate the tree.
Definition: lib_tree.cpp:200
void Unselect()
Unselect currently selected item in wxDataViewCtrl.
Definition: lib_tree.cpp:189
void onTreeSelect(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:378
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:92
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
wxDataViewItem GetNextItem(wxDataViewCtrl const &aView, wxDataViewItem const &aItem)
Get the next item in list order.
void onDetailsLink(wxHtmlLinkEvent &aEvent)
Definition: lib_tree.cpp:399
void RefreshLibTree()
Refreshes the tree (mainly to update highlighting and asterisking)
Definition: lib_tree.cpp:218
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
void ExpandLibId(const LIB_ID &aLibId)
Expand and item i the tree widget.
Definition: lib_tree.cpp:194
WIDGETS
< Flags to select extra widgets
Definition: lib_tree.h:46
#define _(s)
Definition: 3d_actions.cpp:33
void VetoContextMenuMouseWarp()
Disable mouse warping after the current context menu is closed.
Definition: tool_manager.h:428
void expandIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:263
std::vector< wxDataViewItem > expanded
< List of expanded nodes
Definition: lib_tree.h:141
void Activate()
Run the tool.
Structure storing state of the component tree widget.
Definition: lib_tree.h:138
void postPreselectEvent()
Post a wxEVT_DATAVIEW_SELECTION_CHANGED to notify the selection handler that a new part has been pres...
Definition: lib_tree.cpp:270
void ShowContextMenu(SELECTION &aSelection)
Helper function to set and immediately show a CONDITIONAL_MENU in concert with the given SELECTION.
Definition: tool_menu.cpp:59
wxDataViewCtrl helper functions.
void setState(const STATE &aState)
Restore the component tree widget state from an object.
Definition: lib_tree.cpp:302
void CenterLibId(const LIB_ID &aLibId)
Ensure that an item is visible (preferably centered).
Definition: lib_tree.cpp:183
void SelectLibId(const LIB_ID &aLibId)
Select an item in the tree widget.
Definition: lib_tree.cpp:177
bool empty() const
Definition: utf8.h:103
wxTextCtrl * m_query_ctrl
Definition: lib_tree.h:174
void onContextMenu(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:433
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.