KiCad PCB EDA Suite
Loading...
Searching...
No Matches
lib_tree_model_adapter.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) 2017 Chris Pavlina <[email protected]>
5 * Copyright (C) 2014 Henner Zeller <[email protected]>
6 * Copyright (C) 2014-2023 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#ifndef LIB_TREE_MODEL_ADAPTER_H
23#define LIB_TREE_MODEL_ADAPTER_H
24
25#include <lib_id.h>
26#include <lib_tree_model.h>
27#include <wx/hashmap.h>
28#include <wx/dataview.h>
29#include <wx/headerctrl.h>
30#include <vector>
31#include <functional>
32#include <set>
33#include <map>
34
95class EDA_BASE_FRAME;
96
97
98class LIB_TREE_MODEL_ADAPTER: public wxDataViewModel
99{
100public:
105 static const wxString GetPinningSymbol()
106 {
107 return wxString::FromUTF8( "☆ " );
108 }
109
110public:
116
121 {
124 NUM_COLS
125 };
126
128 {
131 };
132
137 void SaveSettings();
138
144 void SetFilter( std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) { m_filter = aFilter; }
145
149 std::function<bool( LIB_TREE_NODE& aNode )>* GetFilter() const { return m_filter; }
150
151 void SetSortMode( SORT_MODE aMode ) { m_sort_mode = aMode; }
153
160 void ShowUnits( bool aShow );
161
169 void SetPreselectNode( const LIB_ID& aLibId, int aUnit );
170
179 void DoAddLibrary( const wxString& aNodeName, const wxString& aDesc,
180 const std::vector<LIB_TREE_ITEM*>& aItemList,
181 bool pinned, bool presorted );
182
183 std::vector<wxString> GetAvailableColumns() const { return m_availableColumns; }
184
185 std::vector<wxString> GetShownColumns() const { return m_shownColumns; }
186
191 void SetShownColumns( const std::vector<wxString>& aColumnNames );
192
197
204 void UpdateSearchString( const wxString& aSearch, bool aState );
205
212 void AttachTo( wxDataViewCtrl* aDataViewCtrl );
213
219
228 LIB_ID GetAliasFor( const wxDataViewItem& aSelection ) const;
229
239 int GetUnitFor( const wxDataViewItem& aSelection ) const;
240
249 LIB_TREE_NODE::TYPE GetTypeFor( const wxDataViewItem& aSelection ) const;
250
251 LIB_TREE_NODE* GetTreeNodeFor( const wxDataViewItem& aSelection ) const;
252
253 virtual wxString GenerateInfo( const LIB_ID& aLibId, int aUnit ) { return wxEmptyString; }
254
255 virtual bool HasPreview( const wxDataViewItem& aItem ) { return false; }
256 virtual void ShowPreview( wxWindow* aParent, const wxDataViewItem& aItem ) {}
257
261 int GetItemCount() const;
262
266 virtual int GetLibrariesCount() const
267 {
268 return m_tree.m_Children.size();
269 }
270
277 wxDataViewItem FindItem( const LIB_ID& aLibId );
278
279 virtual wxDataViewItem GetCurrentDataViewItem();
280
286 unsigned int GetChildren( const wxDataViewItem& aItem,
287 wxDataViewItemArray& aChildren ) const override;
288
289 // Freezing/Thawing. Used when updating the table model so that we don't try and fetch
290 // values during updating. Primarily a problem on OSX which doesn't pay attention to the
291 // wxDataViewCtrl's freeze count when updating the keyWindow.
292 void Freeze() { m_freeze++; }
293 void Thaw() { m_freeze--; }
294 bool IsFrozen() const { return m_freeze; }
295
296 void RefreshTree();
297
298 // Allows subclasses to nominate a context menu handler.
299 virtual TOOL_INTERACTIVE* GetContextMenuTool() { return nullptr; }
300
301 void PinLibrary( LIB_TREE_NODE* aTreeNode );
302 void UnpinLibrary( LIB_TREE_NODE* aTreeNode );
303
305 {
307 }
308
309protected:
313 static wxDataViewItem ToItem( const LIB_TREE_NODE* aNode );
314
318 static LIB_TREE_NODE* ToNode( wxDataViewItem aItem );
319
326 LIB_TREE_MODEL_ADAPTER( EDA_BASE_FRAME* aParent, const wxString& aPinnedKey );
327
328 LIB_TREE_NODE_LIB& DoAddLibraryNode( const wxString& aNodeName, const wxString& aDesc,
329 bool pinned );
330
334 bool HasContainerColumns( const wxDataViewItem& aItem ) const override;
335
339 bool IsContainer( const wxDataViewItem& aItem ) const override;
340
346 wxDataViewItem GetParent( const wxDataViewItem& aItem ) const override;
347
348 unsigned int GetColumnCount() const override { return m_columns.size(); }
349
353 wxString GetColumnType( unsigned int aCol ) const override { return "string"; }
354
362 void GetValue( wxVariant& aVariant,
363 const wxDataViewItem& aItem,
364 unsigned int aCol ) const override;
365
370 bool SetValue( const wxVariant& aVariant,
371 const wxDataViewItem& aItem,
372 unsigned int aCol ) override { return false; }
373
382 bool GetAttr( const wxDataViewItem& aItem,
383 unsigned int aCol,
384 wxDataViewItemAttr& aAttr ) const override;
385
386 virtual bool isSymbolModel() = 0;
387
388 void resortTree();
389
390private:
394 const LIB_TREE_NODE* ShowResults();
395
396 wxDataViewColumn* doAddColumn( const wxString& aHeader, bool aTranslate = true );
397
398protected:
399 void addColumnIfNecessary( const wxString& aHeader );
400
401 void recreateColumns();
402
404 std::map<unsigned, wxString> m_colIdxMap;
405 std::vector<wxString> m_availableColumns;
406
407private:
409
415
416 wxDataViewCtrl* m_widget;
417
418 std::function<bool( LIB_TREE_NODE& aNode )>* m_filter;
419
420 std::vector<wxDataViewColumn*> m_columns;
421 std::map<wxString, wxDataViewColumn*> m_colNameMap;
422 std::map<wxString, int> m_colWidths;
423 std::vector<wxString> m_shownColumns; // Stored in display order
424};
425
426#endif // LIB_TREE_MODEL_ADAPTER_H
427
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:92
The base frame for deriving all KiCad main window classes.
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
wxString GetColumnType(unsigned int aCol) const override
Return the type of data stored in the column as indicated by wxVariant::GetType()
int GetUnitFor(const wxDataViewItem &aSelection) const
Return the unit for the given item.
LIB_TREE_NODE::TYPE GetTypeFor(const wxDataViewItem &aSelection) const
Return node type for the given item.
void SetSortMode(SORT_MODE aMode)
bool GetAttr(const wxDataViewItem &aItem, unsigned int aCol, wxDataViewItemAttr &aAttr) const override
Get any formatting for an item.
std::map< wxString, int > m_colWidths
void FinishTreeInitialization()
A final-stage initialization to be called after the window hierarchy has been realized and the window...
void addColumnIfNecessary(const wxString &aHeader)
void PinLibrary(LIB_TREE_NODE *aTreeNode)
virtual TOOL_INTERACTIVE * GetContextMenuTool()
LIB_TREE_NODE_LIB & DoAddLibraryNode(const wxString &aNodeName, const wxString &aDesc, bool pinned)
virtual wxString GenerateInfo(const LIB_ID &aLibId, int aUnit)
virtual wxDataViewItem GetCurrentDataViewItem()
void SetPreselectNode(const LIB_ID &aLibId, int aUnit)
Set the symbol name to be selected if there are no search results.
static LIB_TREE_NODE * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> #SYM_TREE_NODE.
LIB_ID GetAliasFor(const wxDataViewItem &aSelection) const
Return the alias for the given item.
void AttachTo(wxDataViewCtrl *aDataViewCtrl)
Attach to a wxDataViewCtrl and initialize it.
void DoAddLibrary(const wxString &aNodeName, const wxString &aDesc, const std::vector< LIB_TREE_ITEM * > &aItemList, bool pinned, bool presorted)
Add the given list of symbols by alias.
void SetShownColumns(const std::vector< wxString > &aColumnNames)
Sets which columns are shown in the widget.
static wxDataViewItem ToItem(const LIB_TREE_NODE *aNode)
Convert #SYM_TREE_NODE -> wxDataViewItem.
virtual bool isSymbolModel()=0
bool IsContainer(const wxDataViewItem &aItem) const override
Check whether an item can have children.
static const wxString GetPinningSymbol()
void AssignIntrinsicRanks()
Sort the tree and assign ranks after adding libraries.
void ShowUnits(bool aShow)
Whether or not to show units.
LIB_TREE_NODE * GetTreeNodeFor(const wxDataViewItem &aSelection) const
unsigned int GetChildren(const wxDataViewItem &aItem, wxDataViewItemArray &aChildren) const override
Populate a list of all the children of an item.
void SaveSettings()
Save the column widths to the config file.
TREE_COLS
This enum defines the order of the default columns in the tree view.
@ NUM_COLS
The number of default tree columns.
@ NAME_COL
Library or library item name column.
@ DESC_COL
Library or library description column.
std::function< bool(LIB_TREE_NODE &aNode)> * GetFilter() const
Return the active filter.
std::map< unsigned, wxString > m_colIdxMap
wxDataViewColumn * doAddColumn(const wxString &aHeader, bool aTranslate=true)
virtual bool HasPreview(const wxDataViewItem &aItem)
std::vector< wxDataViewColumn * > m_columns
virtual int GetLibrariesCount() const
Return the number of libraries loaded in the tree.
std::vector< wxString > GetShownColumns() const
int GetItemCount() const
Return the number of symbols loaded in the tree.
const LIB_TREE_NODE * ShowResults()
Find and expand successful search results.
std::vector< wxString > m_shownColumns
void GetValue(wxVariant &aVariant, const wxDataViewItem &aItem, unsigned int aCol) const override
Get the value of an item.
void UnpinLibrary(LIB_TREE_NODE *aTreeNode)
std::map< wxString, wxDataViewColumn * > m_colNameMap
bool HasContainerColumns(const wxDataViewItem &aItem) const override
Check whether a container has columns too.
bool SetValue(const wxVariant &aVariant, const wxDataViewItem &aItem, unsigned int aCol) override
Set the value of an item.
wxDataViewItem GetParent(const wxDataViewItem &aItem) const override
Get the parent of an item.
std::vector< wxString > m_availableColumns
unsigned int GetColumnCount() const override
std::vector< wxString > GetAvailableColumns() const
virtual void ShowPreview(wxWindow *aParent, const wxDataViewItem &aItem)
void SetFilter(std::function< bool(LIB_TREE_NODE &aNode)> *aFilter)
Set the symbol filter type.
wxDataViewItem FindItem(const LIB_ID &aLibId)
Returns tree item corresponding to part.
void UpdateSearchString(const wxString &aSearch, bool aState)
Set the search string provided by the user.
std::function< bool(LIB_TREE_NODE &aNode)> * m_filter
Node type: library.
Node type: root.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
PTR_VECTOR m_Children
void AssignIntrinsicRanks(bool presorted=false)
Store intrinsic ranks on all children of this node.