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 <pavlina.chris@gmail.com>
5  * Copyright (C) 2014 Henner Zeller <h.zeller@acm.org>
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 
92 class APP_SETTINGS_BASE;
93 class TOOL_INTERACTIVE;
94 class EDA_BASE_FRAME;
95 
96 
97 class LIB_TREE_MODEL_ADAPTER: public wxDataViewModel
98 {
99 public:
105 
110  {
113  };
114 
119  {
120  PART_COL = 0,
123  };
124 
129  void SaveColWidths();
130  void SavePinnedItems();
131 
137  void SetFilter( SYM_FILTER_TYPE aFilter );
138 
142  SYM_FILTER_TYPE GetFilter() const { return m_filter; }
143 
150  void ShowUnits( bool aShow );
151 
159  void SetPreselectNode( const LIB_ID& aLibId, int aUnit );
160 
169  void DoAddLibrary( const wxString& aNodeName, const wxString& aDesc,
170  const std::vector<LIB_TREE_ITEM*>& aItemList, bool presorted );
171 
172 
177 
184  void UpdateSearchString( const wxString& aSearch, bool aState );
185 
192  void AttachTo( wxDataViewCtrl* aDataViewCtrl );
193 
199 
208  LIB_ID GetAliasFor( const wxDataViewItem& aSelection ) const;
209 
219  int GetUnitFor( const wxDataViewItem& aSelection ) const;
220 
229  LIB_TREE_NODE::TYPE GetTypeFor( const wxDataViewItem& aSelection ) const;
230 
231  LIB_TREE_NODE* GetTreeNodeFor( const wxDataViewItem& aSelection ) const;
232 
233  virtual wxString GenerateInfo( const LIB_ID& aLibId, int aUnit ) { return wxEmptyString; };
234 
238  int GetItemCount() const;
239 
243  virtual int GetLibrariesCount() const
244  {
245  return m_tree.m_Children.size();
246  }
247 
254  wxDataViewItem FindItem( const LIB_ID& aLibId );
255 
261  unsigned int GetChildren( const wxDataViewItem& aItem,
262  wxDataViewItemArray& aChildren ) const override;
263 
264  // Freezing/Thawing. Used when updating the table model so that we don't try and fetch
265  // values during updating. Primarily a problem on OSX which doesn't pay attention to the
266  // wxDataViewCtrl's freeze count when updating the keyWindow.
267  void Freeze() { m_freeze++; }
268  void Thaw() { m_freeze--; }
269  bool IsFrozen() const { return m_freeze; }
270 
271  void RefreshTree();
272 
273  // Allows subclasses to nominate a context menu handler.
274  virtual TOOL_INTERACTIVE* GetContextMenuTool() { return nullptr; }
275 
276 protected:
280  static wxDataViewItem ToItem( const LIB_TREE_NODE* aNode );
281 
285  static LIB_TREE_NODE* ToNode( wxDataViewItem aItem );
286 
290  static unsigned int IntoArray( const LIB_TREE_NODE& aNode, wxDataViewItemArray& aChildren );
291 
298  LIB_TREE_MODEL_ADAPTER( EDA_BASE_FRAME* aParent, const wxString& aPinnedKey );
299 
300  LIB_TREE_NODE_LIB& DoAddLibraryNode( const wxString& aNodeName, const wxString& aDesc );
301 
305  bool HasContainerColumns( const wxDataViewItem& aItem ) const override;
306 
310  bool IsContainer( const wxDataViewItem& aItem ) const override;
311 
317  wxDataViewItem GetParent( const wxDataViewItem& aItem ) const override;
318 
319  unsigned int GetColumnCount() const override { return NUM_COLS; }
320 
324  wxString GetColumnType( unsigned int aCol ) const override { return "string"; }
325 
333  void GetValue( wxVariant& aVariant,
334  const wxDataViewItem& aItem,
335  unsigned int aCol ) const override;
336 
341  bool SetValue( const wxVariant& aVariant,
342  const wxDataViewItem& aItem,
343  unsigned int aCol ) override { return false; }
344 
353  bool GetAttr( const wxDataViewItem& aItem,
354  unsigned int aCol,
355  wxDataViewItemAttr& aAttr ) const override;
356 
362  const wxString GetPinningSymbol() const
363  {
364  return wxString::FromUTF8( "☆ " );
365  }
366 
367 private:
372  void FindAndExpand( LIB_TREE_NODE& aNode, std::function<bool( const LIB_TREE_NODE* )> aFunc,
373  LIB_TREE_NODE** aHighScore );
374 
379 
384 
389 
390 protected:
392 
393 private:
395 
400  int m_freeze;
401 
402  wxDataViewColumn* m_col_part;
403  wxDataViewColumn* m_col_desc;
404  wxDataViewCtrl* m_widget;
405 
407  wxArrayString m_pinnedLibs;
408  wxString m_pinnedKey;
409 };
410 
411 #endif // LIB_TREE_MODEL_ADAPTER_H
412 
bool HasContainerColumns(const wxDataViewItem &aItem) const override
Check whether a container has columns too.
void AssignIntrinsicRanks()
Sort the tree and assign ranks after adding libraries.
bool IsContainer(const wxDataViewItem &aItem) const override
Check whether an item can have children.
void SetFilter(SYM_FILTER_TYPE aFilter)
Set the symbol filter type.
virtual int GetLibrariesCount() const
Return the number of libraries loaded in the tree.
LIB_TREE_NODE_LIB & DoAddLibraryNode(const wxString &aNodeName, const wxString &aDesc)
void UpdateSearchString(const wxString &aSearch, bool aState)
Set the search string provided by the user.
unsigned int GetChildren(const wxDataViewItem &aItem, wxDataViewItemArray &aChildren) const override
Populate a list of all the children of an item.
void FindAndExpand(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 ...
int GetUnitFor(const wxDataViewItem &aSelection) const
Return the unit for the given item.
void SetPreselectNode(const LIB_ID &aLibId, int aUnit)
Set the symbol name to be selected if there are no search results.
LIB_TREE_NODE * ShowResults()
Find and expand successful search results.
bool SetValue(const wxVariant &aVariant, const wxDataViewItem &aItem, unsigned int aCol) override
Set the value of an item.
bool GetAttr(const wxDataViewItem &aItem, unsigned int aCol, wxDataViewItemAttr &aAttr) const override
Get any formatting for an item.
LIB_TREE_NODE::TYPE GetTypeFor(const wxDataViewItem &aSelection) const
Return node type for the given item.
LIB_TREE_NODE * ShowPreselect()
Find and expand preselected node.
SYM_FILTER_TYPE GetFilter() const
Return the active filter.
unsigned int GetColumnCount() const override
wxString GetColumnType(unsigned int aCol) const override
Return the type of data stored in the column as indicated by wxVariant::GetType()
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
LIB_TREE_MODEL_ADAPTER(EDA_BASE_FRAME *aParent, const wxString &aPinnedKey)
Create the adapter.
SYM_FILTER_TYPE
This enum allows a selective filtering of symbols to list.
static wxDataViewItem ToItem(const LIB_TREE_NODE *aNode)
Convert #SYM_TREE_NODE -> wxDataViewItem.
LIB_TREE_NODE * GetTreeNodeFor(const wxDataViewItem &aSelection) const
int GetItemCount() const
Return the number of symbols loaded in the tree.
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:99
Node type: root.
wxDataViewItem GetParent(const wxDataViewItem &aItem) const override
Get the parent of an item.
void FinishTreeInitialization()
A final-stage initialization to be called after the window hierarchy has been realized and the window...
Node type: library.
static LIB_TREE_NODE * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> #SYM_TREE_NODE.
static unsigned int IntoArray(const LIB_TREE_NODE &aNode, wxDataViewItemArray &aChildren)
Convert SYM_TREE_NODE's children to wxDataViewItemArray.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
void GetValue(wxVariant &aVariant, const wxDataViewItem &aItem, unsigned int aCol) const override
Get the value of an item.
virtual TOOL_INTERACTIVE * GetContextMenuTool()
TREE_COLS
This enum defines the order of the columns in the tree view.
LIB_ID GetAliasFor(const wxDataViewItem &aSelection) const
Return the alias for the given item.
const wxString GetPinningSymbol() const
void DoAddLibrary(const wxString &aNodeName, const wxString &aDesc, const std::vector< LIB_TREE_ITEM * > &aItemList, bool presorted)
Add the given list of symbols by alias.
void AssignIntrinsicRanks(bool presorted=false)
Store intrinsic ranks on all children of this node.
The base frame for deriving all KiCad main window classes.
wxDataViewItem FindItem(const LIB_ID &aLibId)
Returns tree item corresponding to part.
LIB_TREE_NODE * ShowSingleLibrary()
Find and expand a library if there is only one.
PTR_VECTOR m_Children
void SaveColWidths()
Save the column widths to the config file.
virtual wxString GenerateInfo(const LIB_ID &aLibId, int aUnit)
void AttachTo(wxDataViewCtrl *aDataViewCtrl)
Attach to a wxDataViewCtrl and initialize it.
void ShowUnits(bool aShow)
Whether or not to show units.