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) 2023 CERN
7 * Copyright (C) 2014-2024 KiCad Developers, see AUTHORS.txt for contributors.
8 *
9 * This program is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#ifndef LIB_TREE_MODEL_ADAPTER_H
24#define LIB_TREE_MODEL_ADAPTER_H
25
26#include <eda_base_frame.h>
27#include <lib_id.h>
28#include <lib_tree_model.h>
29#include <wx/hashmap.h>
30#include <wx/dataview.h>
31#include <wx/headerctrl.h>
32#include <vector>
33#include <functional>
34#include <set>
35#include <map>
36
95#include <project.h>
96
99class EDA_BASE_FRAME;
100
101
102class LIB_TREE_MODEL_ADAPTER: public wxDataViewModel
103{
104public:
109 static const wxString GetPinningSymbol()
110 {
111 return wxString::FromUTF8( "☆ " );
112 }
113
114public:
120
125 {
128 NUM_COLS
129 };
130
132 {
135 };
136
141 void SaveSettings();
142
148 void SetFilter( std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) { m_filter = aFilter; }
149
153 std::function<bool( LIB_TREE_NODE& aNode )>* GetFilter() const { return m_filter; }
154
155 void SetSortMode( SORT_MODE aMode ) { m_sort_mode = aMode; }
157
164 void ShowUnits( bool aShow );
165
173 void SetPreselectNode( const LIB_ID& aLibId, int aUnit );
174
183 void DoAddLibrary( const wxString& aNodeName, const wxString& aDesc,
184 const std::vector<LIB_TREE_ITEM*>& aItemList,
185 bool pinned, bool presorted );
186
192 void DoRemoveLibrary( const wxString& aNodeName );
193
194 std::vector<wxString> GetAvailableColumns() const { return m_availableColumns; }
195
196 std::vector<wxString> GetShownColumns() const { return m_shownColumns; }
197
198 std::vector<wxString> GetOpenLibs() const;
199 void OpenLibs( const std::vector<wxString>& aLibs );
200
205 void SetShownColumns( const std::vector<wxString>& aColumnNames );
206
211
218 void UpdateSearchString( const wxString& aSearch, bool aState );
219
226 void AttachTo( wxDataViewCtrl* aDataViewCtrl );
227
233
242 LIB_ID GetAliasFor( const wxDataViewItem& aSelection ) const;
243
253 int GetUnitFor( const wxDataViewItem& aSelection ) const;
254
263 LIB_TREE_NODE::TYPE GetTypeFor( const wxDataViewItem& aSelection ) const;
264
265 LIB_TREE_NODE* GetTreeNodeFor( const wxDataViewItem& aSelection ) const;
266
267 virtual wxString GenerateInfo( const LIB_ID& aLibId, int aUnit ) { return wxEmptyString; }
268
269 virtual bool HasPreview( const wxDataViewItem& aItem ) { return false; }
270 virtual void ShowPreview( wxWindow* aParent, const wxDataViewItem& aItem ) {}
271
273
277 int GetItemCount() const;
278
282 virtual int GetLibrariesCount() const
283 {
284 return m_tree.m_Children.size();
285 }
286
293 wxDataViewItem FindItem( const LIB_ID& aLibId );
294
295 virtual wxDataViewItem GetCurrentDataViewItem();
296
302 unsigned int GetChildren( const wxDataViewItem& aItem,
303 wxDataViewItemArray& aChildren ) const override;
304
305 // Freezing/Thawing. Used when updating the table model so that we don't try and fetch
306 // values during updating. Primarily a problem on OSX which doesn't pay attention to the
307 // wxDataViewCtrl's freeze count when updating the keyWindow.
308 void Freeze() { m_freeze++; }
309 void Thaw() { m_freeze--; }
310 bool IsFrozen() const { return m_freeze; }
311
312 void RefreshTree();
313
314 // Allows subclasses to nominate a context menu handler.
315 virtual TOOL_INTERACTIVE* GetContextMenuTool() { return nullptr; }
316
317 void PinLibrary( LIB_TREE_NODE* aTreeNode );
318 void UnpinLibrary( LIB_TREE_NODE* aTreeNode );
319
321 {
323 }
324
325protected:
329 static wxDataViewItem ToItem( const LIB_TREE_NODE* aNode );
330
334 static LIB_TREE_NODE* ToNode( wxDataViewItem aItem );
335
343 LIB_TREE_MODEL_ADAPTER( EDA_BASE_FRAME* aParent, const wxString& aPinnedKey,
344 APP_SETTINGS_BASE* aCfg );
345
346 LIB_TREE_NODE_LIBRARY& DoAddLibraryNode( const wxString& aNodeName, const wxString& aDesc,
347 bool pinned );
348
352 bool HasContainerColumns( const wxDataViewItem& aItem ) const override;
353
357 bool IsContainer( const wxDataViewItem& aItem ) const override;
358
364 wxDataViewItem GetParent( const wxDataViewItem& aItem ) const override;
365
366 unsigned int GetColumnCount() const override { return m_columns.size(); }
367
371 wxString GetColumnType( unsigned int aCol ) const override { return "string"; }
372
380 void GetValue( wxVariant& aVariant,
381 const wxDataViewItem& aItem,
382 unsigned int aCol ) const override;
383
388 bool SetValue( const wxVariant& aVariant,
389 const wxDataViewItem& aItem,
390 unsigned int aCol ) override { return false; }
391
400 bool GetAttr( const wxDataViewItem& aItem,
401 unsigned int aCol,
402 wxDataViewItemAttr& aAttr ) const override;
403
405
406 void resortTree();
407
408private:
412 const LIB_TREE_NODE* ShowResults();
413
414 wxDataViewColumn* doAddColumn( const wxString& aHeader, bool aTranslate = true );
415
416protected:
417 void addColumnIfNecessary( const wxString& aHeader );
418
419 void recreateColumns();
420
422 std::map<unsigned, wxString> m_colIdxMap;
423 std::vector<wxString> m_availableColumns;
424
425 wxDataViewCtrl* m_widget;
426
427private:
430
436
437 std::function<bool( LIB_TREE_NODE& aNode )>* m_filter;
438
439 std::vector<wxDataViewColumn*> m_columns;
440 std::map<wxString, wxDataViewColumn*> m_colNameMap;
441 std::map<wxString, int> m_colWidths;
442 std::vector<wxString> m_shownColumns; // Stored in display order
443};
444
445#endif // LIB_TREE_MODEL_ADAPTER_H
446
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()
std::vector< wxString > GetOpenLibs() const
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 PROJECT::LIB_TYPE_T getLibType()=0
virtual TOOL_INTERACTIVE * GetContextMenuTool()
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.
TOOL_DISPATCHER * GetToolDispatcher() const
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.
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)
void DoRemoveLibrary(const wxString &aNodeName)
Remove the library by name.
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.
LIB_TREE_NODE_LIBRARY & DoAddLibraryNode(const wxString &aNodeName, const wxString &aDesc, bool pinned)
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
void OpenLibs(const std::vector< wxString > &aLibs)
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.
LIB_TYPE_T
Definition: project.h:189
TOOL_DISPATCHER * GetToolDispatcher() const
Definition: tools_holder.h:57
Base window classes and related definitions.