KiCad PCB EDA Suite
lib_tree.h
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-2021 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 ACTION_MENU;
36 class LIB_ID;
37 class LIB_TABLE;
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,
59  wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>& aAdapter,
60  WIDGETS aWidgets = ALL, wxHtmlWindow *aDetails = nullptr );
61 
62  ~LIB_TREE() override;
63 
73  LIB_ID GetSelectedLibId( int* aUnit = nullptr ) const;
74 
76 
80  void SelectLibId( const LIB_ID& aLibId );
81 
85  void CenterLibId( const LIB_ID& aLibId );
86 
90  void Unselect();
91 
95  void ExpandLibId( const LIB_ID& aLibId );
96 
100  void Regenerate( bool aKeepState );
101 
105  void RefreshLibTree();
106 
107  wxWindow* GetFocusTarget();
108 
109 protected:
113  void toggleExpand( const wxDataViewItem& aTreeId );
114 
118  void selectIfValid( const wxDataViewItem& aTreeId );
119 
120  void centerIfValid( const wxDataViewItem& aTreeId );
121 
122  void expandIfValid( const wxDataViewItem& aTreeId );
123 
128  void postPreselectEvent();
129 
133  void postSelectEvent();
134 
138  struct STATE
139  {
141  std::vector<wxDataViewItem> expanded;
142  std::vector<wxString> pinned;
143 
146  };
147 
151  STATE getState() const;
152 
156  void setState( const STATE& aState );
157 
158  void onQueryText( wxCommandEvent& aEvent );
159  void onQueryEnter( wxCommandEvent& aEvent );
160  void onQueryCharHook( wxKeyEvent& aEvent );
161 
162  void onTreeSelect( wxDataViewEvent& aEvent );
163  void onTreeActivate( wxDataViewEvent& aEvent );
164 
165  void onDetailsLink( wxHtmlLinkEvent& aEvent );
166  void onPreselect( wxCommandEvent& aEvent );
167  void onContextMenu( wxDataViewEvent& aEvent );
168 
169 protected:
171 
172  wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER> m_adapter;
173 
174  wxTextCtrl* m_query_ctrl;
175  wxDataViewCtrl* m_tree_ctrl;
176  wxHtmlWindow* m_details_ctrl;
177 
179 };
180 
182 wxDECLARE_EVENT( COMPONENT_PRESELECTED, wxCommandEvent );
183 
185 wxDECLARE_EVENT( COMPONENT_SELECTED, wxCommandEvent );
186 
187 #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:245
LIB_ID m_last_libid
Definition: lib_tree.h:178
void toggleExpand(const wxDataViewItem &aTreeId)
Expand or collapse a node, switching it to the opposite state.
Definition: lib_tree.cpp:233
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:45
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
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
LIB_ID selection
Definition: lib_tree.h:145
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
LIB_TABLE * m_lib_table
Definition: lib_tree.h:170
std::vector< wxString > pinned
Current selection, might be not valid if nothing was selected.
Definition: lib_tree.h:142
void onQueryCharHook(wxKeyEvent &aEvent)
Definition: lib_tree.cpp:335
LIB_TREE_NODE * GetCurrentTreeNode() const
Definition: lib_tree.cpp:166
void onTreeActivate(wxDataViewEvent &aEvent)
Definition: lib_tree.cpp:385
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
wxDECLARE_EVENT(COMPONENT_PRESELECTED, wxCommandEvent)
Custom event sent when a component is selected.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
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 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
void expandIfValid(const wxDataViewItem &aTreeId)
Definition: lib_tree.cpp:263
std::vector< wxDataViewItem > expanded
< List of expanded nodes
Definition: lib_tree.h:141
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 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
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:177
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.