File: lib_tree.h

package info (click to toggle)
kicad 9.0.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 771,448 kB
  • sloc: cpp: 969,355; ansic: 121,001; xml: 66,428; python: 18,387; sh: 1,010; awk: 301; asm: 292; makefile: 228; javascript: 167; perl: 10
file content (288 lines) | stat: -rw-r--r-- 8,850 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2014 Henner Zeller <h.zeller@acm.org>
 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#ifndef LIB_TREE_H
#define LIB_TREE_H

#include <wx/panel.h>
#include <wx/timer.h>
#include <lib_tree_model_adapter.h>
#include <widgets/html_window.h>
#include <widgets/wx_dataviewctrl.h>

class wxTextCtrl;
class wxHtmlLinkEvent;
class wxSearchCtrl;
class wxTimer;
class wxTimerEvent;
class wxPopupWindow;
class STD_BITMAP_BUTTON;
class ACTION_MENU;
class LIB_ID;
class LIB_TABLE;

/**
 * Widget displaying a tree of symbols with optional search text control and description panel.
 */
class LIB_TREE : public wxPanel
{
public:
    ///< Flags to select extra widgets and options
    enum FLAGS
    {
        FLAGS_NONE  = 0x00,
        SEARCH      = 0x01,
        FILTERS     = 0x02,
        DETAILS     = 0x04,
        ALL_WIDGETS = 0x0F,
        MULTISELECT = 0x10
    };

    /**
     * Construct a symbol tree.
     *
     * @param aParent parent window containing this tree widget.
     * @param aRecentSearchesKey a key into a global map storing recent searches (usually "power",
     *                           "symbols", or "footprints", but could be further differentiated).
     * @param aLibTable table containing libraries and items to display.
     * @param aAdapter a LIB_TREE_MODEL_ADAPTER instance to use.
     * @param aFlags selection of sub-widgets to include and other options.
     * @param aDetails if not null, a custom HTML_WINDOW to hold symbol details. If null this
     *                 will be created inside the LIB_TREE.
     */
    LIB_TREE( wxWindow* aParent, const wxString& aRecentSearchesKey, LIB_TABLE* aLibTable,
              wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>& aAdapter, int aFlags = ALL_WIDGETS,
              HTML_WINDOW* aDetails = nullptr );

    ~LIB_TREE() override;

    /**
     * For multi-unit symbols, if the user selects the symbol itself
     * rather than picking an individual unit, 0 will be returned in aUnit.
     * Beware that this is an invalid unit number - this should be replaced
     * with whatever default is desired (usually 1).
     *
     * @param aUnit if not NULL, the selected unit is filled in here.
     * @return the library id of the symbol that has been selected.
     */
    LIB_ID GetSelectedLibId( int* aUnit = nullptr ) const;

    int GetSelectionCount() const
    {
        return m_tree_ctrl->GetSelectedItemsCount();
    }

    /**
     * Retrieve a list of selections for trees that allow multi-selection.
     *
     * @see GetSelectedLibId for details on how aUnit will be filled.
     * @param aSelection will be filled with a list of selected LIB_IDs.
     * @param aUnit is an optional pointer to a list to fill with unit numbers.
     * @return the number of selected items.
     */
    int GetSelectedLibIds( std::vector<LIB_ID>& aSelection,
                           std::vector<int>* aUnit = nullptr ) const;

    /**
     * Retrieve the tree node for the first selected item.
     *
     * @return the tree node for the first selected item.
     */
    LIB_TREE_NODE* GetCurrentTreeNode() const;

    /**
     * Retrieve a list of pointers to selected tree nodes for trees that allow multi-selection.
     * 
     * @param aSelection will be filled with a list of pointers of selected tree nodes.
     * @return the number of selected items.
     */
    int GetSelectedTreeNodes( std::vector<LIB_TREE_NODE*>& aSelection ) const;

    /**
     * Select an item in the tree widget.
     */
    void SelectLibId( const LIB_ID& aLibId );

    /**
     * Ensure that an item is visible (preferably centered).
     */
    void CenterLibId( const LIB_ID& aLibId );

    /**
     * Unselect currently selected item in wxDataViewCtrl.
     */
    void Unselect();

    /**
     * Expand and item i the tree widget.
     */
    void ExpandLibId( const LIB_ID& aLibId );

    void ExpandAll();
    void CollapseAll();

    /**
     * Save/restore search string.
     */
    void SetSearchString( const wxString& aSearchString );
    wxString GetSearchString() const;

    /**
     * Save/restore the sorting mode.
     */
    void SetSortMode( LIB_TREE_MODEL_ADAPTER::SORT_MODE aMode ) { m_adapter->SetSortMode( aMode ); }
    LIB_TREE_MODEL_ADAPTER::SORT_MODE GetSortMode() const { return m_adapter->GetSortMode(); }

    /**
     * Regenerate the tree.
     */
    void Regenerate( bool aKeepState );

    /**
     * Refresh the tree (mainly to update highlighting and asterisking)
     */
    void RefreshLibTree();

    wxWindow* GetFocusTarget();

    wxSizer* GetFiltersSizer() { return m_filtersSizer; }

    /**
     * Focus the search widget if it exists.
     */
    void FocusSearchFieldIfExists();

    void ShowChangedLanguage();

    void BlockPreview( bool aBlock )
    {
        m_previewDisabled = aBlock;
    }

    void ShutdownPreviews();

protected:
    /**
     * Expand or collapse a node, switching it to the opposite state.
     */
    void toggleExpand( const wxDataViewItem& aTreeId );

    /**
     * If a wxDataViewitem is valid, select it and post a selection event.
     */
    void selectIfValid( const wxDataViewItem& aTreeId );

    void centerIfValid( const wxDataViewItem& aTreeId );

    void expandIfValid( const wxDataViewItem& aTreeId );

    /**
     * Post a wxEVT_DATAVIEW_SELECTION_CHANGED to notify the selection handler
     * that a new part has been preselected.
     */
    void postPreselectEvent();

    /**
     * Post #SYMBOL_SELECTED event to notify the selection handler that a part has been selected.
     */
    void postSelectEvent();

    /**
     * Structure storing state of the symbol tree widget.
     */
    struct STATE
    {
        /// List of expanded nodes.
        std::vector<wxDataViewItem> expanded;

        /// Current selection, might be not valid if nothing was selected.
        LIB_ID selection;
    };

    /**
     * Return the symbol tree widget state.
     */
    STATE getState() const;

    /**
     * Restore the symbol tree widget state from an object.
     */
    void setState( const STATE& aState );

    void updateRecentSearchMenu();

    void showPreview( wxDataViewItem aItem );
    void hidePreview();

    void onQueryText( wxCommandEvent& aEvent );
    void onQueryCharHook( wxKeyEvent& aEvent );
    void onQueryMouseMoved( wxMouseEvent& aEvent );

    void onTreeSelect( wxDataViewEvent& aEvent );
    void onTreeActivate( wxDataViewEvent& aEvent );
    void onTreeCharHook( wxKeyEvent& aEvent );

    void onIdle( wxIdleEvent& aEvent );
    void onHoverTimer( wxTimerEvent& aEvent );

    void onDetailsLink( wxHtmlLinkEvent& aEvent );
    void onPreselect( wxCommandEvent& aEvent );
    void onItemContextMenu( wxDataViewEvent& aEvent );
    void onHeaderContextMenu( wxDataViewEvent& aEvent );

    void onDebounceTimer( wxTimerEvent& aEvent );

protected:
    wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER> m_adapter;

    wxSearchCtrl*      m_query_ctrl;
    STD_BITMAP_BUTTON* m_sort_ctrl;
    WX_DATAVIEWCTRL*   m_tree_ctrl;
    HTML_WINDOW*       m_details_ctrl;
    wxTimer*           m_debounceTimer;
    bool               m_inTimerEvent;

    wxString           m_recentSearchesKey;

    wxBoxSizer*        m_filtersSizer;

    bool               m_skipNextRightClick;

    wxPoint            m_hoverPos;
    wxDataViewItem     m_hoverItem;
    wxRect             m_hoverItemRect;
    wxTimer            m_hoverTimer;
    wxDataViewItem     m_previewItem;
    wxRect             m_previewItemRect;
    wxPopupWindow*     m_previewWindow;
    bool               m_previewDisabled;
};

/// Custom event sent when an item is selected in the list.
wxDECLARE_EVENT( EVT_LIBITEM_SELECTED, wxCommandEvent );

/// Custom event sent when an item is chosen (double-clicked).
wxDECLARE_EVENT( EVT_LIBITEM_CHOSEN, wxCommandEvent );

#endif /* LIB_TREE_H */