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 The 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
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef LIB_TREE_MODEL_ADAPTER_H
24#define LIB_TREE_MODEL_ADAPTER_H
25
26#include <lib_id.h>
27#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
89
90#include <project.h>
91
92;
93class TOOL_DISPATCHER;
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
112 static constexpr int MAX_COL_WIDTH = 100000;
113
117 static bool IsValidColumnWidth( int aWidth );
118
119public:
125
135
141
146 void SaveSettings();
147
151 void SetFilter( std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) { m_filter = aFilter; }
152
156 std::function<bool( LIB_TREE_NODE& aNode )>* GetFilter() const { return m_filter; }
157
158 void SetSortMode( SORT_MODE aMode ) { m_sort_mode = aMode; }
160
167 void ShowUnits( bool aShow );
168
176 void SetPreselectNode( const LIB_ID& aLibId, int aUnit );
177
187 LIB_TREE_NODE_LIBRARY& DoAddLibrary( const wxString& aNodeName, const wxString& aDesc,
188 const std::vector<LIB_TREE_ITEM*>& aItemList,
189 bool aPinned, bool aPresorted );
190
194 void RemoveGroup( bool aRecentlyUsedGroup, bool aAlreadyPlacedGroup );
195
196 std::vector<wxString> GetAvailableColumns() const { return m_availableColumns; }
197
198 std::vector<wxString> GetShownColumns() const { return m_shownColumns; }
199
200 std::vector<wxString> GetOpenLibs() const;
201 void OpenLibs( const std::vector<wxString>& aLibs );
202
204 void RegisterLazyLoadHandler( std::function<void()>&& aHandler )
205 {
206 m_lazyLoadHandler = aHandler;
207 }
208
213 void SetShownColumns( const std::vector<wxString>& aColumnNames );
214
219 {
220 m_tree.AssignIntrinsicRanks( m_shownColumns );
221
222 for( const std::unique_ptr<LIB_TREE_NODE>& child : m_tree.m_Children )
223 child->AssignIntrinsicRanks( m_shownColumns );
224 }
225
232 void UpdateSearchString( const wxString& aSearch, bool aState );
233
240 void AttachTo( wxDataViewCtrl* aDataViewCtrl );
241
247
256 LIB_ID GetAliasFor( const wxDataViewItem& aSelection ) const;
257
267 int GetUnitFor( const wxDataViewItem& aSelection ) const;
268
277 LIB_TREE_NODE::TYPE GetTypeFor( const wxDataViewItem& aSelection ) const;
278
279 LIB_TREE_NODE* GetTreeNodeFor( const wxDataViewItem& aSelection ) const;
280
281 virtual wxString GenerateInfo( const LIB_ID& aLibId, int aUnit ) { return wxEmptyString; }
282
283 virtual bool HasPreview( const wxDataViewItem& aItem ) { return false; }
284 virtual void ShowPreview( wxWindow* aParent, const wxDataViewItem& aItem ) {}
285 virtual void ShutdownPreview( wxWindow* aParent ) {}
286
288
292 int GetItemCount() const;
293
297 virtual int GetLibrariesCount() const
298 {
299 return (int) m_tree.m_Children.size();
300 }
301
308 wxDataViewItem FindItem( const LIB_ID& aLibId );
309
310 virtual wxDataViewItem GetCurrentDataViewItem();
311
317 unsigned int GetChildren( const wxDataViewItem& aItem, wxDataViewItemArray& aChildren ) const override;
318
319 // Freezing/Thawing. Used when updating the table model so that we don't try and fetch
320 // values during updating. Primarily a problem on OSX which doesn't pay attention to the
321 // wxDataViewCtrl's freeze count when updating the keyWindow.
322 void Freeze() { m_freeze++; }
323 void Thaw() { m_freeze--; }
324 bool IsFrozen() const { return m_freeze; }
325
326 void RefreshTree();
327
328 // Allows subclasses to nominate a context menu handler.
329 virtual TOOL_INTERACTIVE* GetContextMenuTool() { return nullptr; }
330
332 virtual bool CanPinLibraries() const { return true; }
333
334 void PinLibrary( LIB_TREE_NODE* aTreeNode );
335 void UnpinLibrary( LIB_TREE_NODE* aTreeNode );
336
337 void ShowChangedLanguage();
338
347 {
348 public:
349 explicit ResetTreeView( LIB_TREE_MODEL_ADAPTER& aAdapter );
351
352 ResetTreeView( const ResetTreeView& ) = delete;
354
355 private:
357 };
358
359protected:
363 static wxDataViewItem ToItem( const LIB_TREE_NODE* aNode );
364
368 static LIB_TREE_NODE* ToNode( wxDataViewItem aItem );
369
377 LIB_TREE_MODEL_ADAPTER( EDA_BASE_FRAME* aParent, const wxString& aPinnedKey,
378 APP_SETTINGS_BASE::LIB_TREE& aSettingsStruct );
379
380 LIB_TREE_NODE_LIBRARY& DoAddLibraryNode( const wxString& aNodeName, const wxString& aDesc,
381 bool pinned );
382
383 virtual void loadColumnConfig();
384
388 bool HasContainerColumns( const wxDataViewItem& aItem ) const override;
389
393 bool IsContainer( const wxDataViewItem& aItem ) const override;
394
400 wxDataViewItem GetParent( const wxDataViewItem& aItem ) const override;
401
409 void GetValue( wxVariant& aVariant, const wxDataViewItem& aItem, unsigned int aCol ) const override;
410
414 bool SetValue( const wxVariant& aVariant, const wxDataViewItem& aItem, unsigned int aCol ) override
415 {
416 return false;
417 }
418
427 bool GetAttr( const wxDataViewItem& aItem, unsigned int aCol, wxDataViewItemAttr& aAttr ) const override;
428
430
431 void resortTree();
432
433private:
437 const LIB_TREE_NODE* showResults();
438
439 wxDataViewColumn* doAddColumn( const wxString& aHeader, bool aTranslate = true );
440
441protected:
442 void addColumnIfNecessary( const wxString& aHeader );
443
444 void recreateColumns();
446
447protected:
450
452 std::map<unsigned, wxString> m_colIdxMap;
453 std::vector<wxString> m_availableColumns;
454 std::map<wxString, int> m_colWidths;
455 std::vector<wxString> m_shownColumns; // Stored in display order
456 wxDataViewCtrl* m_widget;
457 std::function<void()> m_lazyLoadHandler;
458
459private:
465
466 std::function<bool( LIB_TREE_NODE& aNode )>* m_filter;
467
468 std::vector<wxDataViewColumn*> m_columns;
469 std::map<wxString, wxDataViewColumn*> m_colNameMap;
470};
471
472#endif // LIB_TREE_MODEL_ADAPTER_H
473
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:45
ResetTreeView(const ResetTreeView &)=delete
ResetTreeView(LIB_TREE_MODEL_ADAPTER &aAdapter)
ResetTreeView & operator=(const ResetTreeView &)=delete
LIB_TREE_MODEL_ADAPTER(EDA_BASE_FRAME *aParent, const wxString &aPinnedKey, APP_SETTINGS_BASE::LIB_TREE &aSettingsStruct)
Create the adapter.
std::vector< wxString > GetOpenLibs() const
int GetUnitFor(const wxDataViewItem &aSelection) const
Return the unit for the given item.
static bool IsValidColumnWidth(int aWidth)
LIB_TREE_NODE::TYPE GetTypeFor(const wxDataViewItem &aSelection) const
Return node type for the given item.
void SetSortMode(SORT_MODE aMode)
APP_SETTINGS_BASE::LIB_TREE & m_cfg
bool GetAttr(const wxDataViewItem &aItem, unsigned int aCol, wxDataViewItemAttr &aAttr) const override
Get any formatting for an item.
std::map< wxString, int > m_colWidths
const LIB_TREE_NODE * showResults()
Find and expand successful search results.
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 bool CanPinLibraries() const
Pinning writes to the project, so trees of libraries outside it must opt out.
virtual void ShutdownPreview(wxWindow *aParent)
virtual TOOL_INTERACTIVE * GetContextMenuTool()
virtual wxString GenerateInfo(const LIB_ID &aLibId, int aUnit)
void RegisterLazyLoadHandler(std::function< void()> &&aHandler)
Registers a function to be called whenever new lazy-loaded library content is available.
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 -> LIB_TREE_NODE.
LIB_ID GetAliasFor(const wxDataViewItem &aSelection) const
Return the alias for the given item.
LIB_TREE_NODE_LIBRARY & DoAddLibrary(const wxString &aNodeName, const wxString &aDesc, const std::vector< LIB_TREE_ITEM * > &aItemList, bool aPinned, bool aPresorted)
Add the given list of symbols by alias.
TOOL_DISPATCHER * GetToolDispatcher() const
void AttachTo(wxDataViewCtrl *aDataViewCtrl)
Attach to a wxDataViewCtrl and initialize it.
void SetShownColumns(const std::vector< wxString > &aColumnNames)
Sets which columns are shown in the widget.
std::function< void()> m_lazyLoadHandler
static wxDataViewItem ToItem(const LIB_TREE_NODE *aNode)
Convert LIB_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.
std::vector< wxString > m_shownColumns
void RemoveGroup(bool aRecentlyUsedGroup, bool aAlreadyPlacedGroup)
Remove one of the system groups from the library.
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
std::vector< wxString > GetAvailableColumns() const
static constexpr int MAX_COL_WIDTH
Upper bound for a persisted column width; larger values are treated as corrupt settings.
virtual void ShowPreview(wxWindow *aParent, const wxDataViewItem &aItem)
void SetFilter(std::function< bool(LIB_TREE_NODE &aNode)> *aFilter)
Set the filter.
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.