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-2019 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( CMP_FILTER_TYPE aFilter );
138 
142  CMP_FILTER_TYPE GetFilter() const { return m_filter; }
143 
150  void ShowUnits( bool aShow );
151 
159  void SetPreselectNode( LIB_ID const& aLibId, int aUnit );
160 
169  void DoAddLibrary( wxString const& aNodeName, wxString const& aDesc,
170  std::vector<LIB_TREE_ITEM*> const& aItemList, bool presorted );
171 
172 
177 
183  void UpdateSearchString( wxString const& aSearch );
184 
191  void AttachTo( wxDataViewCtrl* aDataViewCtrl );
192 
201  LIB_ID GetAliasFor( const wxDataViewItem& aSelection ) const;
202 
212  int GetUnitFor( const wxDataViewItem& aSelection ) const;
213 
222  LIB_TREE_NODE::TYPE GetTypeFor( const wxDataViewItem& aSelection ) const;
223 
224  LIB_TREE_NODE* GetTreeNodeFor( const wxDataViewItem& aSelection ) const;
225 
226  virtual wxString GenerateInfo( LIB_ID const& aLibId, int aUnit ) { return wxEmptyString; };
227 
231  int GetItemCount() const;
232 
236  virtual int GetLibrariesCount() const
237  {
238  return m_tree.m_Children.size();
239  }
240 
247  wxDataViewItem FindItem( const LIB_ID& aLibId );
248 
254  unsigned int GetChildren( wxDataViewItem const& aItem,
255  wxDataViewItemArray& aChildren ) const override;
256 
257  // Freezing/Thawing. Used when updating the table model so that we don't try and fetch
258  // values during updating. Primarily a problem on OSX which doesn't pay attention to the
259  // wxDataViewCtrl's freeze count when updating the keyWindow.
260  void Freeze() { m_freeze++; }
261  void Thaw() { m_freeze--; }
262  bool IsFrozen() const { return m_freeze; }
263 
264  void RefreshTree();
265 
266  // Allows subclasses to nominate a context menu handler.
267  virtual TOOL_INTERACTIVE* GetContextMenuTool() { return nullptr; }
268 
269 protected:
270  static wxDataViewItem ToItem( LIB_TREE_NODE const* aNode );
271  static LIB_TREE_NODE* ToNode( wxDataViewItem aItem );
272  static unsigned int IntoArray( LIB_TREE_NODE const& aNode, wxDataViewItemArray& aChildren );
273 
275 
281  LIB_TREE_MODEL_ADAPTER( EDA_BASE_FRAME* aParent, wxString aPinnedKey );
282 
283  LIB_TREE_NODE_LIB& DoAddLibraryNode( wxString const& aNodeName, wxString const& aDesc );
284 
288  bool HasContainerColumns( wxDataViewItem const& aItem ) const override;
289 
293  bool IsContainer( wxDataViewItem const& aItem ) const override;
294 
300  wxDataViewItem GetParent( wxDataViewItem const& aItem ) const override;
301 
302  unsigned int GetColumnCount() const override { return NUM_COLS; }
303 
307  wxString GetColumnType( unsigned int aCol ) const override { return "string"; }
308 
316  void GetValue( wxVariant& aVariant,
317  wxDataViewItem const& aItem,
318  unsigned int aCol ) const override;
319 
324  bool SetValue( wxVariant const& aVariant,
325  wxDataViewItem const& aItem,
326  unsigned int aCol ) override { return false; }
327 
336  bool GetAttr( wxDataViewItem const& aItem,
337  unsigned int aCol,
338  wxDataViewItemAttr& aAttr ) const override;
339 
345  const wxString GetPinningSymbol() const
346  {
347  return wxString::FromUTF8( "☆ " );
348  }
349 
350 private:
352 
357  int m_freeze;
358 
359  wxDataViewColumn* m_col_part;
360  wxDataViewColumn* m_col_desc;
361  wxDataViewCtrl* m_widget;
362 
364  wxArrayString m_pinnedLibs;
365  wxString m_pinnedKey;
366 
371  void FindAndExpand( LIB_TREE_NODE& aNode, std::function<bool( LIB_TREE_NODE const* )> aFunc,
372  LIB_TREE_NODE** aHighScore );
373 
378 
383 
388 };
389 
390 #endif // LIB_TREE_MODEL_ADAPTER_H
391 
void DoAddLibrary(wxString const &aNodeName, wxString const &aDesc, std::vector< LIB_TREE_ITEM * > const &aItemList, bool presorted)
Add the given list of components by alias.
void AssignIntrinsicRanks()
Sort the tree and assign ranks after adding libraries.
bool IsContainer(wxDataViewItem const &aItem) const override
Check whether an item can have children.
CMP_FILTER_TYPE
This enum allows a selective filtering of components to list.
virtual int GetLibrariesCount() const
Return the number of libraries loaded in the tree.
int GetUnitFor(const wxDataViewItem &aSelection) const
Return the unit for the given item.
void SetPreselectNode(LIB_ID const &aLibId, int aUnit)
Set the component name to be selected if there are no search results.
void UpdateSearchString(wxString const &aSearch)
Set the search string provided by the user.
LIB_TREE_NODE * ShowResults()
Find and expand successful search results.
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.
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
virtual wxString GenerateInfo(LIB_ID const &aLibId, int aUnit)
static wxDataViewItem ToItem(LIB_TREE_NODE const *aNode)
Convert CMP_TREE_NODE -> wxDataViewItem.
LIB_TREE_NODE * GetTreeNodeFor(const wxDataViewItem &aSelection) const
static unsigned int IntoArray(LIB_TREE_NODE const &aNode, wxDataViewItemArray &aChildren)
Convert CMP_TREE_NODE's children to wxDataViewItemArray.
bool GetAttr(wxDataViewItem const &aItem, unsigned int aCol, wxDataViewItemAttr &aAttr) const override
Get any formatting for an item.
int GetItemCount() const
Return the number of components loaded in the tree.
bool HasContainerColumns(wxDataViewItem const &aItem) const override
Check whether a container has columns too.
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:99
void FindAndExpand(LIB_TREE_NODE &aNode, std::function< bool(LIB_TREE_NODE const *)> aFunc, LIB_TREE_NODE **aHighScore)
Find any results worth highlighting and expand them, according to given criteria The highest-scoring ...
Node type: root.
Node type: library.
void SetFilter(CMP_FILTER_TYPE aFilter)
Set the component filter type.
static LIB_TREE_NODE * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
wxDataViewItem GetParent(wxDataViewItem const &aItem) const override
Get the parent of an item.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
LIB_TREE_NODE_LIB & DoAddLibraryNode(wxString const &aNodeName, wxString const &aDesc)
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 AssignIntrinsicRanks(bool presorted=false)
Store intrinsic ranks on all children of this node.
void GetValue(wxVariant &aVariant, wxDataViewItem const &aItem, unsigned int aCol) const override
Get the value of an item.
bool SetValue(wxVariant const &aVariant, wxDataViewItem const &aItem, unsigned int aCol) override
Set the value of an item.
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.
unsigned int GetChildren(wxDataViewItem const &aItem, wxDataViewItemArray &aChildren) const override
Populate a list of all the children of an item.
void AttachTo(wxDataViewCtrl *aDataViewCtrl)
Attach to a wxDataViewCtrl and initialize it.
CMP_FILTER_TYPE GetFilter() const
Return the active filter.
void ShowUnits(bool aShow)
Whether or not to show units.
LIB_TREE_MODEL_ADAPTER(EDA_BASE_FRAME *aParent, wxString aPinnedKey)
Creates the adapter.