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
97class EDA_BASE_FRAME;
98
99
100class LIB_TREE_MODEL_ADAPTER: public wxDataViewModel
101{
102public:
107 static const wxString GetPinningSymbol()
108 {
109 return wxString::FromUTF8( "☆ " );
110 }
111
112public:
118
123 {
126 NUM_COLS
127 };
128
130 {
133 };
134
139 void SaveSettings();
140
146 void SetFilter( std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) { m_filter = aFilter; }
147
151 std::function<bool( LIB_TREE_NODE& aNode )>* GetFilter() const { return m_filter; }
152
153 void SetSortMode( SORT_MODE aMode ) { m_sort_mode = aMode; }
155
162 void ShowUnits( bool aShow );
163
171 void SetPreselectNode( const LIB_ID& aLibId, int aUnit );
172
181 void DoAddLibrary( const wxString& aNodeName, const wxString& aDesc,
182 const std::vector<LIB_TREE_ITEM*>& aItemList,
183 bool pinned, bool presorted );
184
185 std::vector<wxString> GetAvailableColumns() const { return m_availableColumns; }
186
187 std::vector<wxString> GetShownColumns() const { return m_shownColumns; }
188
189 std::vector<wxString> GetOpenLibs() const;
190 void OpenLibs( const std::vector<wxString>& aLibs );
191
196 void SetShownColumns( const std::vector<wxString>& aColumnNames );
197
202
209 void UpdateSearchString( const wxString& aSearch, bool aState );
210
217 void AttachTo( wxDataViewCtrl* aDataViewCtrl );
218
224
233 LIB_ID GetAliasFor( const wxDataViewItem& aSelection ) const;
234
244 int GetUnitFor( const wxDataViewItem& aSelection ) const;
245
254 LIB_TREE_NODE::TYPE GetTypeFor( const wxDataViewItem& aSelection ) const;
255
256 LIB_TREE_NODE* GetTreeNodeFor( const wxDataViewItem& aSelection ) const;
257
258 virtual wxString GenerateInfo( const LIB_ID& aLibId, int aUnit ) { return wxEmptyString; }
259
260 virtual bool HasPreview( const wxDataViewItem& aItem ) { return false; }
261 virtual void ShowPreview( wxWindow* aParent, const wxDataViewItem& aItem ) {}
262
264
268 int GetItemCount() const;
269
273 virtual int GetLibrariesCount() const
274 {
275 return m_tree.m_Children.size();
276 }
277
284 wxDataViewItem FindItem( const LIB_ID& aLibId );
285
286 virtual wxDataViewItem GetCurrentDataViewItem();
287
293 unsigned int GetChildren( const wxDataViewItem& aItem,
294 wxDataViewItemArray& aChildren ) const override;
295
296 // Freezing/Thawing. Used when updating the table model so that we don't try and fetch
297 // values during updating. Primarily a problem on OSX which doesn't pay attention to the
298 // wxDataViewCtrl's freeze count when updating the keyWindow.
299 void Freeze() { m_freeze++; }
300 void Thaw() { m_freeze--; }
301 bool IsFrozen() const { return m_freeze; }
302
303 void RefreshTree();
304
305 // Allows subclasses to nominate a context menu handler.
306 virtual TOOL_INTERACTIVE* GetContextMenuTool() { return nullptr; }
307
308 void PinLibrary( LIB_TREE_NODE* aTreeNode );
309 void UnpinLibrary( LIB_TREE_NODE* aTreeNode );
310
312 {
314 }
315
316protected:
320 static wxDataViewItem ToItem( const LIB_TREE_NODE* aNode );
321
325 static LIB_TREE_NODE* ToNode( wxDataViewItem aItem );
326
333 LIB_TREE_MODEL_ADAPTER( EDA_BASE_FRAME* aParent, const wxString& aPinnedKey );
334
335 LIB_TREE_NODE_LIBRARY& DoAddLibraryNode( const wxString& aNodeName, const wxString& aDesc,
336 bool pinned );
337
341 bool HasContainerColumns( const wxDataViewItem& aItem ) const override;
342
346 bool IsContainer( const wxDataViewItem& aItem ) const override;
347
353 wxDataViewItem GetParent( const wxDataViewItem& aItem ) const override;
354
355 unsigned int GetColumnCount() const override { return m_columns.size(); }
356
360 wxString GetColumnType( unsigned int aCol ) const override { return "string"; }
361
369 void GetValue( wxVariant& aVariant,
370 const wxDataViewItem& aItem,
371 unsigned int aCol ) const override;
372
377 bool SetValue( const wxVariant& aVariant,
378 const wxDataViewItem& aItem,
379 unsigned int aCol ) override { return false; }
380
389 bool GetAttr( const wxDataViewItem& aItem,
390 unsigned int aCol,
391 wxDataViewItemAttr& aAttr ) const override;
392
393 virtual bool isSymbolModel() = 0;
394
395 void resortTree();
396
397private:
401 const LIB_TREE_NODE* ShowResults();
402
403 wxDataViewColumn* doAddColumn( const wxString& aHeader, bool aTranslate = true );
404
405protected:
406 void addColumnIfNecessary( const wxString& aHeader );
407
408 void recreateColumns();
409
411 std::map<unsigned, wxString> m_colIdxMap;
412 std::vector<wxString> m_availableColumns;
413
414 wxDataViewCtrl* m_widget;
415
416private:
418
424
425 std::function<bool( LIB_TREE_NODE& aNode )>* m_filter;
426
427 std::vector<wxDataViewColumn*> m_columns;
428 std::map<wxString, wxDataViewColumn*> m_colNameMap;
429 std::map<wxString, int> m_colWidths;
430 std::vector<wxString> m_shownColumns; // Stored in display order
431};
432
433#endif // LIB_TREE_MODEL_ADAPTER_H
434
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 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.
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.
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.
TOOL_DISPATCHER * GetToolDispatcher() const
Definition: tools_holder.h:57
Base window classes and related definitions.