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-2017 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 
27 #include <lib_tree_model.h>
28 
29 #include <wx/hashmap.h>
30 #include <wx/dataview.h>
31 #include <wx/headerctrl.h>
32 #include <vector>
33 #include <functional>
34 
35 
93 class LIB_TREE_MODEL_ADAPTER: public wxDataViewModel
94 {
95 public:
96 
100  typedef wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER> PTR;
101 
107 
112  {
115  };
116 
122  void SetFilter( CMP_FILTER_TYPE aFilter );
123 
127  CMP_FILTER_TYPE GetFilter() const { return m_filter; }
128 
135  void ShowUnits( bool aShow );
136 
142  void UpdateWidth( int aCol );
143 
151  void SetPreselectNode( LIB_ID const& aLibId, int aUnit );
152 
161  void DoAddLibrary( wxString const& aNodeName, wxString const& aDesc,
162  std::vector<LIB_TREE_ITEM*> const& aItemList, bool presorted );
163 
164 
169 
175  void UpdateSearchString( wxString const& aSearch );
176 
183  void AttachTo( wxDataViewCtrl* aDataViewCtrl );
184 
193  LIB_ID GetAliasFor( const wxDataViewItem& aSelection ) const;
194 
204  int GetUnitFor( const wxDataViewItem& aSelection ) const;
205 
214  LIB_TREE_NODE::TYPE GetTypeFor( const wxDataViewItem& aSelection ) const;
215 
216  virtual wxString GenerateInfo( LIB_ID const& aLibId, int aUnit ) { return wxEmptyString; };
217 
221  int GetItemCount() const;
222 
226  virtual int GetLibrariesCount() const
227  {
228  return m_tree.Children.size();
229  }
230 
237  wxDataViewItem FindItem( const LIB_ID& aLibId );
238 
244  unsigned int GetChildren( wxDataViewItem const& aItem,
245  wxDataViewItemArray& aChildren ) const override;
246 
247  // Freezing/Thawing. Used when updating the table model so that we don't try and fetch
248  // values during updating. Primarily a problem on OSX which doesn't pay attention to the
249  // wxDataViewCtrl's freeze count when updating the keyWindow.
250  void Freeze() { m_freeze++; }
251  void Thaw() { m_freeze--; }
252  bool IsFrozen() const { return m_freeze; }
253 
254 protected:
255  static wxDataViewItem ToItem( LIB_TREE_NODE const* aNode );
256  static LIB_TREE_NODE const* ToNode( wxDataViewItem aItem );
257  static unsigned int IntoArray( LIB_TREE_NODE const& aNode, wxDataViewItemArray& aChildren );
258 
260 
265 
269  virtual bool HasContainerColumns( wxDataViewItem const& aItem ) const override;
270 
274  virtual bool IsContainer( wxDataViewItem const& aItem ) const override;
275 
282  virtual wxDataViewItem GetParent( wxDataViewItem const& aItem ) const override;
283 
287  virtual unsigned int GetColumnCount() const override { return 2; }
288 
294  virtual wxString GetColumnType( unsigned int aCol ) const override { return "string"; }
295 
303  virtual void GetValue( wxVariant& aVariant,
304  wxDataViewItem const& aItem,
305  unsigned int aCol ) const override;
306 
311  virtual bool SetValue( wxVariant const& aVariant,
312  wxDataViewItem const& aItem,
313  unsigned int aCol ) override { return false; }
314 
323  virtual bool GetAttr( wxDataViewItem const& aItem,
324  unsigned int aCol,
325  wxDataViewItemAttr& aAttr ) const override;
326 
327 private:
332  int m_freeze;
333 
334  wxDataViewColumn* m_col_part;
335  wxDataViewColumn* m_col_desc;
336  wxDataViewCtrl* m_widget;
337 
346  int ColWidth( LIB_TREE_NODE& aTree, int aCol, wxString const& aHeading );
347 
353  void FindAndExpand( LIB_TREE_NODE& aNode,
354  std::function<bool( LIB_TREE_NODE const* )> aFunc,
355  LIB_TREE_NODE** aHighScore );
356 
361 
366 
371 };
372 
373 #endif // LIB_TREE_MODEL_ADAPTER_H
374 
Adapter class in the component selector Model-View-Adapter (mediated MVC) architecture.
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.
virtual bool SetValue(wxVariant const &aVariant, wxDataViewItem const &aItem, unsigned int aCol) override
Set the value of an item.
void AssignIntrinsicRanks()
Sort the tree and assign ranks after adding libraries.
virtual 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.
PTR_VECTOR Children
List of child nodes.
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.
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)
int ColWidth(LIB_TREE_NODE &aTree, int aCol, wxString const &aHeading)
Compute the width required for the given column of a node and its children.
static wxDataViewItem ToItem(LIB_TREE_NODE const *aNode)
Convert CMP_TREE_NODE -> wxDataViewItem.
static unsigned int IntoArray(LIB_TREE_NODE const &aNode, wxDataViewItemArray &aChildren)
Convert CMP_TREE_NODE's children to wxDataViewItemArray.
virtual 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.
virtual bool HasContainerColumns(wxDataViewItem const &aItem) const override
Check whether a container has columns too.
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 (f(CMP_TREE_NODE con...
Node type: root.
void SetFilter(CMP_FILTER_TYPE aFilter)
Set the component filter type.
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
virtual 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.
void UpdateWidth(int aCol)
Update the column size based on the displayed contents.
virtual wxString GetColumnType(unsigned int aCol) const override
Return the type of data stored in the column.
LIB_ID GetAliasFor(const wxDataViewItem &aSelection) const
Return the alias for the given item.
void AssignIntrinsicRanks(bool presorted=false)
Store intrinsic ranks on all children of this node.
virtual void GetValue(wxVariant &aVariant, wxDataViewItem const &aItem, unsigned int aCol) const override
Get the value of an item.
virtual unsigned int GetColumnCount() const override
Return the number of columns in the model.
wxDataViewItem FindItem(const LIB_ID &aLibId)
Returns tree item corresponding to part.
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > PTR
Reference-counting container for a pointer to CMP_TREE_MODEL_ADAPTER_BASE.
LIB_TREE_NODE * ShowSingleLibrary()
Find and expand a library if there is only one.
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.