KiCad PCB EDA Suite
lib_tree.h
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 #ifndef LIB_TREE_H
26 #define LIB_TREE_H
27 
28 #include <wx/panel.h>
29 #include <lib_tree_model_adapter.h>
30 
31 class wxDataViewCtrl;
32 class wxTextCtrl;
33 class wxHtmlWindow;
34 class wxHtmlLinkEvent;
35 class LIB_ID;
36 class LIB_TABLE;
37 
38 
42 class LIB_TREE : public wxPanel
43 {
44 public:
46  enum WIDGETS { NONE = 0x00, SEARCH = 0x01, DETAILS = 0x02, ALL = 0xFF };
47 
58  LIB_TREE( wxWindow* aParent, LIB_TABLE* aLibTable, LIB_TREE_MODEL_ADAPTER::PTR& aAdapter,
59  WIDGETS aWidgets = ALL, wxHtmlWindow *aDetails = nullptr );
60 
70  LIB_ID GetSelectedLibId( int* aUnit = nullptr ) const;
71 
75  void SelectLibId( const LIB_ID& aLibId );
76 
80  void CenterLibId( const LIB_ID& aLibId );
81 
85  void Unselect();
86 
90  void ExpandLibId( const LIB_ID& aLibId );
91 
97  void SetMenu( LIB_TREE_NODE::TYPE aType, std::unique_ptr<wxMenu> aMenu )
98  {
99  m_menus[aType] = std::move( aMenu );
100  }
101 
106  bool IsMenuActive() const
107  {
108  return m_menuActive;
109  }
110 
114  void Regenerate();
115 
116  void SetFocus() override;
117 
118 protected:
122  void toggleExpand( const wxDataViewItem& aTreeId );
123 
127  void selectIfValid( const wxDataViewItem& aTreeId );
128 
129  void centerIfValid( const wxDataViewItem& aTreeId );
130 
131  void expandIfValid( const wxDataViewItem& aTreeId );
132 
137  void postPreselectEvent();
138 
142  void postSelectEvent();
143 
147  struct STATE
148  {
150  std::vector<wxDataViewItem> expanded;
151 
154  };
155 
159  STATE getState() const;
160 
164  void setState( const STATE& aState );
165 
166  void onQueryText( wxCommandEvent& aEvent );
167  void onQueryEnter( wxCommandEvent& aEvent );
168  void onQueryCharHook( wxKeyEvent& aEvent );
169 
170  void onTreeSelect( wxDataViewEvent& aEvent );
171  void onTreeActivate( wxDataViewEvent& aEvent );
172  void onUpdateUI( wxUpdateUIEvent& aEvent );
173 
174  void onDetailsLink( wxHtmlLinkEvent& aEvent );
175  void onPreselect( wxCommandEvent& aEvent );
176  void onContextMenu( wxDataViewEvent& aEvent );
177 
180 
181  wxTextCtrl* m_query_ctrl;
182  wxDataViewCtrl* m_tree_ctrl;
183  wxHtmlWindow* m_details_ctrl;
184 
186  std::vector<std::unique_ptr<wxMenu>> m_menus;
187 
190 
193 
196 };
197 
199 wxDECLARE_EVENT( COMPONENT_PRESELECTED, wxCommandEvent );
200 
202 wxDECLARE_EVENT( COMPONENT_SELECTED, wxCommandEvent );
203 
204 #endif /* LIB_TREE_H */
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
void onUpdateUI(wxUpdateUIEvent &aEvent)
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
LIB_TABLE * m_lib_table
Definition: lib_tree.h:178
bool IsMenuActive() const
Returns the status of right-click context menu.
Definition: lib_tree.h:106
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
void SetMenu(LIB_TREE_NODE::TYPE aType, std::unique_ptr< wxMenu > aMenu)
Associates a right click context menu for a specific node type.
Definition: lib_tree.h:97
bool m_menuActive
Flag indicating whether a right-click context menu is active
Definition: lib_tree.h:189
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
wxDECLARE_EVENT(COMPONENT_PRESELECTED, wxCommandEvent)
Custom event sent when a new component is preselected
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
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
Widget displaying a tree of components with optional search text control and description panel...
Definition: lib_tree.h:42
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.