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 <cmp_tree_model.h>
26 
27 #include <wx/hashmap.h>
28 #include <wx/dataview.h>
29 #include <vector>
30 #include <functional>
31 
32 class LIB_ALIAS;
33 class PART_LIB;
34 class PART_LIBS;
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( PART_LIBS* aLibs );
116 
121  {
124  };
125 
131  void SetFilter( CMP_FILTER_TYPE aFilter );
132 
139  void ShowUnits( bool aShow );
140 
148  void SetPreselectNode( wxString const& aName, int aUnit );
149 
156  void AddLibrary( PART_LIB& aLib );
157 
166  void AddAliasList(
167  wxString const& aNodeName,
168  wxArrayString const& aAliasNameList,
169  PART_LIB* aOptionalLib = nullptr );
170 
179  void AddAliasList(
180  wxString const& aNodeName,
181  std::vector<LIB_ALIAS*> const& aAliasList,
182  PART_LIB* aOptionalLib = nullptr );
183 
189  void UpdateSearchString( wxString const& aSearch );
190 
197  void AttachTo( wxDataViewCtrl* aDataViewCtrl );
198 
207  LIB_ALIAS* GetAliasFor( wxDataViewItem aSelection ) const;
208 
218  int GetUnitFor( wxDataViewItem aSelection ) const;
219 
223  int GetComponentsCount() const;
224 
225 protected:
226 
231 
235  virtual bool HasContainerColumns( wxDataViewItem const& aItem ) const override;
236 
240  virtual bool IsContainer( wxDataViewItem const& aItem ) const override;
241 
248  virtual wxDataViewItem GetParent( wxDataViewItem const& aItem ) const override;
249 
255  virtual unsigned int GetChildren(
256  wxDataViewItem const& aItem,
257  wxDataViewItemArray& aChildren ) const override;
258 
262  virtual unsigned int GetColumnCount() const override { return 2; }
263 
269  virtual wxString GetColumnType( unsigned int aCol ) const override { return "string"; }
270 
278  virtual void GetValue(
279  wxVariant& aVariant,
280  wxDataViewItem const& aItem,
281  unsigned int aCol ) const override;
282 
287  virtual bool SetValue(
288  wxVariant const& aVariant,
289  wxDataViewItem const& aItem,
290  unsigned int aCol ) override { return false; }
291 
300  virtual bool GetAttr(
301  wxDataViewItem const& aItem,
302  unsigned int aCol,
303  wxDataViewItemAttr& aAttr ) const override;
304 
305 private:
311 
313 
314  wxDataViewColumn* m_col_part;
315  wxDataViewColumn* m_col_desc;
316  wxDataViewCtrl* m_widget;
317 
318  WX_DECLARE_STRING_HASH_MAP( std::vector<int>, WIDTH_CACHE );
319 
320  static WIDTH_CACHE m_width_cache;
321 
330  int ColWidth( CMP_TREE_NODE& aTree, int aCol, wxString const& aHeading );
331 
337  int WidthFor( CMP_TREE_NODE& aNode, int aCol );
338 
343  int WidthFor( wxString const& aHeading, int aCol );
344 
351  bool FindAndExpand(
352  CMP_TREE_NODE& aNode,
353  std::function<bool( CMP_TREE_NODE const* )> aFunc );
354 
358  bool ShowResults();
359 
363  bool ShowPreselect();
364 
368  bool ShowSingleLibrary();
369 };
370 
371 #endif // _CMP_TREE_MODEL_ADAPTER_H
void SetPreselectNode(wxString const &aName, int aUnit)
Set the component name to be selected if there are no search results.
Part library alias object definition.
void AddLibrary(PART_LIB &aLib)
Add all the components and their aliases in this library.
virtual void GetValue(wxVariant &aVariant, wxDataViewItem const &aItem, unsigned int aCol) const override
Get the value of an item.
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.
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.
static PTR Create(PART_LIBS *aLibs)
Factory function: create a model adapter in a reference-counting container.
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.
void AddAliasList(wxString const &aNodeName, wxArrayString const &aAliasNameList, PART_LIB *aOptionalLib=nullptr)
Add the given list of components, by name.
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.
CMP_TREE_MODEL_ADAPTER(PART_LIBS *aLibs)
Constructor; takes a set of libraries to be included in the search.
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.
Class PART_LIBS is a collection of PART_LIBs.
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.
LIB_ALIAS * GetAliasFor(wxDataViewItem aSelection) const
Return the alias for the given item.
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.
int GetUnitFor(wxDataViewItem aSelection) const
Return the unit for the given item.
bool ShowResults()
Find and expand successful search results.
Class PART_LIB is used to load, save, search, and otherwise manipulate part library files...
bool ShowPreselect()
Find and expand preselected node.
WX_DECLARE_STRING_HASH_MAP(std::vector< int >, WIDTH_CACHE)
Node type: root.