KiCad PCB EDA Suite
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-2021 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 };
125
130 {
133 NUM_COLS
134 };
135
136
141 void SaveSettings();
142
148 void SetFilter( SYM_FILTER_TYPE aFilter );
149
154
161 void ShowUnits( bool aShow );
162
170 void SetPreselectNode( const LIB_ID& aLibId, int aUnit );
171
180 void DoAddLibrary( const wxString& aNodeName, const wxString& aDesc,
181 const std::vector<LIB_TREE_ITEM*>& aItemList,
182 bool pinned, bool presorted );
183
184 void AddColumn( const wxString& aHeader )
185 {
186 doAddColumn( aHeader, false );
187 }
188
189 std::vector<wxString> GetAvailableColumns() const { return m_availableColumns; }
190
191 std::vector<wxString> GetShownColumns() const { return m_shownColumns; }
192
197 void SetShownColumns( const std::vector<wxString>& aColumnNames );
198
203
210 void UpdateSearchString( const wxString& aSearch, bool aState );
211
218 void AttachTo( wxDataViewCtrl* aDataViewCtrl );
219
225
226 void OnSize( wxSizeEvent& aEvent );
235 LIB_ID GetAliasFor( const wxDataViewItem& aSelection ) const;
236
246 int GetUnitFor( const wxDataViewItem& aSelection ) const;
247
256 LIB_TREE_NODE::TYPE GetTypeFor( const wxDataViewItem& aSelection ) const;
257
258 LIB_TREE_NODE* GetTreeNodeFor( const wxDataViewItem& aSelection ) const;
259
260 virtual wxString GenerateInfo( const LIB_ID& aLibId, int aUnit ) { return wxEmptyString; };
261
265 int GetItemCount() const;
266
270 virtual int GetLibrariesCount() const
271 {
272 return m_tree.m_Children.size();
273 }
274
281 wxDataViewItem FindItem( const LIB_ID& aLibId );
282
288 unsigned int GetChildren( const wxDataViewItem& aItem,
289 wxDataViewItemArray& aChildren ) const override;
290
291 // Freezing/Thawing. Used when updating the table model so that we don't try and fetch
292 // values during updating. Primarily a problem on OSX which doesn't pay attention to the
293 // wxDataViewCtrl's freeze count when updating the keyWindow.
294 void Freeze() { m_freeze++; }
295 void Thaw() { m_freeze--; }
296 bool IsFrozen() const { return m_freeze; }
297
298 void RefreshTree();
299
300 // Allows subclasses to nominate a context menu handler.
301 virtual TOOL_INTERACTIVE* GetContextMenuTool() { return nullptr; }
302
303 void PinLibrary( LIB_TREE_NODE* aTreeNode );
304 void UnpinLibrary( LIB_TREE_NODE* aTreeNode );
305
306protected:
310 static wxDataViewItem ToItem( const LIB_TREE_NODE* aNode );
311
315 static LIB_TREE_NODE* ToNode( wxDataViewItem aItem );
316
320 static unsigned int IntoArray( const LIB_TREE_NODE& aNode, wxDataViewItemArray& aChildren );
321
328 LIB_TREE_MODEL_ADAPTER( EDA_BASE_FRAME* aParent, const wxString& aPinnedKey );
329
330 LIB_TREE_NODE_LIB& DoAddLibraryNode( const wxString& aNodeName, const wxString& aDesc,
331 bool pinned );
332
336 bool HasContainerColumns( const wxDataViewItem& aItem ) const override;
337
341 bool IsContainer( const wxDataViewItem& aItem ) const override;
342
348 wxDataViewItem GetParent( const wxDataViewItem& aItem ) const override;
349
350 unsigned int GetColumnCount() const override { return m_columns.size(); }
351
355 wxString GetColumnType( unsigned int aCol ) const override { return "string"; }
356
364 void GetValue( wxVariant& aVariant,
365 const wxDataViewItem& aItem,
366 unsigned int aCol ) const override;
367
372 bool SetValue( const wxVariant& aVariant,
373 const wxDataViewItem& aItem,
374 unsigned int aCol ) override { return false; }
375
384 bool GetAttr( const wxDataViewItem& aItem,
385 unsigned int aCol,
386 wxDataViewItemAttr& aAttr ) const override;
387
388 virtual bool isSymbolModel() = 0;
389
390 void resortTree();
391
392private:
397 void Find( LIB_TREE_NODE& aNode, std::function<bool( const LIB_TREE_NODE* )> aFunc,
398 LIB_TREE_NODE** aHighScore );
399
404
409
414
415 wxDataViewColumn* doAddColumn( const wxString& aHeader, bool aTranslate = true );
416
417protected:
418 void addColumnIfNecessary( const wxString& aHeader );
419
420 void recreateColumns();
421
423 std::map<unsigned, wxString> m_colIdxMap;
424
425private:
427
433
434 wxDataViewCtrl* m_widget;
435
436 std::vector<wxDataViewColumn*> m_columns;
437 std::map<wxString, wxDataViewColumn*> m_colNameMap;
438 std::map<wxString, int> m_colWidths;
439 std::vector<wxString> m_availableColumns;
440 std::vector<wxString> m_shownColumns; // Stored in display order
441};
442
443#endif // LIB_TREE_MODEL_ADAPTER_H
444
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:101
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.
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 AddColumn(const wxString &aHeader)
void addColumnIfNecessary(const wxString &aHeader)
void PinLibrary(LIB_TREE_NODE *aTreeNode)
void OnSize(wxSizeEvent &aEvent)
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)
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.
void SetFilter(SYM_FILTER_TYPE aFilter)
Set the symbol filter type.
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.
SYM_FILTER_TYPE GetFilter() const
Return the active filter.
void Find(LIB_TREE_NODE &aNode, std::function< bool(const LIB_TREE_NODE *)> aFunc, LIB_TREE_NODE **aHighScore)
Find any results worth highlighting and expand them, according to given criteria The highest-scoring ...
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::map< unsigned, wxString > m_colIdxMap
wxDataViewColumn * doAddColumn(const wxString &aHeader, bool aTranslate=true)
LIB_TREE_MODEL_ADAPTER(EDA_BASE_FRAME *aParent, const wxString &aPinnedKey)
Create the adapter.
LIB_TREE_NODE * ShowPreselect()
Find and expand preselected node.
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.
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)
LIB_TREE_NODE * ShowSingleLibrary()
Find and expand a library if there is only one.
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
static unsigned int IntoArray(const LIB_TREE_NODE &aNode, wxDataViewItemArray &aChildren)
Convert SYM_TREE_NODE's children to wxDataViewItemArray.
LIB_TREE_NODE * ShowResults()
Find and expand successful search results.
std::vector< wxString > GetAvailableColumns() const
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.
SYM_FILTER_TYPE
This enum allows a selective filtering of symbols to list.
@ SYM_FILTER_POWER
list symbols flagged PWR
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.