KiCad PCB EDA Suite
cmp_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 _CMP_TREE_MODEL_ADAPTER_H
23 #define _CMP_TREE_MODEL_ADAPTER_H
24 
25 #include <lib_id.h>
26 
27 #include <cmp_tree_model.h>
28 
29 #include <wx/hashmap.h>
30 #include <wx/dataview.h>
31 #include <vector>
32 #include <functional>
33 
34 class SYMBOL_LIB_TABLE;
35 
36 
94 class CMP_TREE_MODEL_ADAPTER: public wxDataViewModel
95 {
96 public:
97 
101  typedef wxObjectDataPtr<CMP_TREE_MODEL_ADAPTER> PTR;
102 
108 
115  static PTR Create( SYMBOL_LIB_TABLE* aLibs );
116 
121  {
124  };
125 
131  void SetFilter( CMP_FILTER_TYPE aFilter );
132 
139  void ShowUnits( bool aShow );
140 
148  void SetPreselectNode( LIB_ID const& aLibId, int aUnit );
149 
156  void AddLibrary( wxString const& aLibNickname );
157 
165  void AddAliasList(
166  wxString const& aNodeName,
167  wxArrayString const& aAliasNameList );
168 
176  void AddAliasList(
177  wxString const& aNodeName,
178  std::vector<LIB_ALIAS*> const& aAliasList );
179 
185  void UpdateSearchString( wxString const& aSearch );
186 
193  void AttachTo( wxDataViewCtrl* aDataViewCtrl );
194 
203  LIB_ID GetAliasFor( wxDataViewItem aSelection ) const;
204 
214  int GetUnitFor( wxDataViewItem aSelection ) const;
215 
219  int GetComponentsCount() const;
220 
221 protected:
222 
227 
231  virtual bool HasContainerColumns( wxDataViewItem const& aItem ) const override;
232 
236  virtual bool IsContainer( wxDataViewItem const& aItem ) const override;
237 
244  virtual wxDataViewItem GetParent( wxDataViewItem const& aItem ) const override;
245 
251  virtual unsigned int GetChildren(
252  wxDataViewItem const& aItem,
253  wxDataViewItemArray& aChildren ) const override;
254 
258  virtual unsigned int GetColumnCount() const override { return 2; }
259 
265  virtual wxString GetColumnType( unsigned int aCol ) const override { return "string"; }
266 
274  virtual void GetValue(
275  wxVariant& aVariant,
276  wxDataViewItem const& aItem,
277  unsigned int aCol ) const override;
278 
283  virtual bool SetValue(
284  wxVariant const& aVariant,
285  wxDataViewItem const& aItem,
286  unsigned int aCol ) override { return false; }
287 
296  virtual bool GetAttr(
297  wxDataViewItem const& aItem,
298  unsigned int aCol,
299  wxDataViewItemAttr& aAttr ) const override;
300 
301 private:
307 
309 
310  wxDataViewColumn* m_col_part;
311  wxDataViewColumn* m_col_desc;
312  wxDataViewCtrl* m_widget;
313 
314  WX_DECLARE_STRING_HASH_MAP( std::vector<int>, WIDTH_CACHE );
315 
316  static WIDTH_CACHE m_width_cache;
317 
326  int ColWidth( CMP_TREE_NODE& aTree, int aCol, wxString const& aHeading );
327 
333  int WidthFor( CMP_TREE_NODE& aNode, int aCol );
334 
339  int WidthFor( wxString const& aHeading, int aCol );
340 
347  bool FindAndExpand(
348  CMP_TREE_NODE& aNode,
349  std::function<bool( CMP_TREE_NODE const* )> aFunc );
350 
354  bool ShowResults();
355 
359  bool ShowPreselect();
360 
364  bool ShowSingleLibrary();
365 };
366 
367 #endif // _CMP_TREE_MODEL_ADAPTER_H
static PTR Create(SYMBOL_LIB_TABLE *aLibs)
Factory function: create a model adapter in a reference-counting container.
LIB_ID GetAliasFor(wxDataViewItem aSelection) const
Return the alias for the given item.
virtual void GetValue(wxVariant &aVariant, wxDataViewItem const &aItem, unsigned int aCol) const override
Get the value of an item.
void AddLibrary(wxString const &aLibNickname)
Add all the components and their aliases in this library.
wxObjectDataPtr< CMP_TREE_MODEL_ADAPTER > PTR
Reference-counting container for a pointer to CMP_TREE_MODEL_ADAPTER.
int WidthFor(CMP_TREE_NODE &aNode, int aCol)
Return the width required to display a single row's aCol text.
virtual bool HasContainerColumns(wxDataViewItem const &aItem) const override
Check whether a container has columns too.
Class LIB_ID.
Definition: lib_id.h:56
bool FindAndExpand(CMP_TREE_NODE &aNode, std::function< bool(CMP_TREE_NODE const *)> aFunc)
Find any results worth highlighting and expand them, according to given criteria (f(CMP_TREE_NODE con...
virtual wxDataViewItem GetParent(wxDataViewItem const &aItem) const override
Get the parent of an item.
static WIDTH_CACHE m_width_cache
void AttachTo(wxDataViewCtrl *aDataViewCtrl)
Attach to a wxDataViewCtrl and initialize it.
virtual bool GetAttr(wxDataViewItem const &aItem, unsigned int aCol, wxDataViewItemAttr &aAttr) const override
Get any formatting for an item.
void UpdateSearchString(wxString const &aSearch)
Set the search string provided by the user.
int GetComponentsCount() const
Return the number of components loaded in the tree.
virtual unsigned int GetColumnCount() const override
Return the number of columns in the model.
CMP_FILTER_TYPE
This enum allows a selective filtering of components to list.
virtual wxString GetColumnType(unsigned int aCol) const override
Return the type of data stored in the column.
int ColWidth(CMP_TREE_NODE &aTree, int aCol, wxString const &aHeading)
Compute the width required for the given column of a node and its children.
virtual unsigned int GetChildren(wxDataViewItem const &aItem, wxDataViewItemArray &aChildren) const override
Populate a list of all the children of an item.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
virtual bool SetValue(wxVariant const &aVariant, wxDataViewItem const &aItem, unsigned int aCol) override
Set the value of an item.
Adapter class in the component selector Model-View-Adapter (mediated MVC) architecture.
void SetFilter(CMP_FILTER_TYPE aFilter)
Set the component filter type.
CMP_TREE_MODEL_ADAPTER(SYMBOL_LIB_TABLE *aLibs)
Constructor; takes a set of libraries to be included in the search.
bool ShowSingleLibrary()
Find and expand a library if there is only one.
void ShowUnits(bool aShow)
Whether or not to show units.
virtual bool IsContainer(wxDataViewItem const &aItem) const override
Check whether an item can have children.
void SetPreselectNode(LIB_ID const &aLibId, int aUnit)
Set the component name to be selected if there are no search results.
int GetUnitFor(wxDataViewItem aSelection) const
Return the unit for the given item.
bool ShowResults()
Find and expand successful search results.
bool ShowPreselect()
Find and expand preselected node.
WX_DECLARE_STRING_HASH_MAP(std::vector< int >, WIDTH_CACHE)
void AddAliasList(wxString const &aNodeName, wxArrayString const &aAliasNameList)
Add the given list of components, by name.
Node type: root.