KiCad PCB EDA Suite
cmp_tree_model_adapter_base.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_BASE_H
23 #define _CMP_TREE_MODEL_ADAPTER_BASE_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 
92 class CMP_TREE_MODEL_ADAPTER_BASE: public wxDataViewModel
93 {
94 public:
95 
99  typedef wxObjectDataPtr<CMP_TREE_MODEL_ADAPTER_BASE> PTR;
100 
106 
111  {
114  };
115 
121  void SetFilter( CMP_FILTER_TYPE aFilter );
122 
126  CMP_FILTER_TYPE GetFilter() const { return m_filter; }
127 
134  void ShowUnits( bool aShow );
135 
143  void SetPreselectNode( LIB_ID const& aLibId, int aUnit );
144 
151  virtual void AddLibrary( wxString const& aLibNickname ) = 0;
152 
160  void AddLibrariesWithProgress( const std::vector<wxString>& aNicknames,
161  wxWindow* aParent );
162 
170  virtual void AddAliasList(
171  wxString const& aNodeName,
172  wxArrayString const& aAliasNameList ) = 0;
173 
182  void AddAliasList(
183  wxString const& aNodeName,
184  wxString const& aDesc,
185  std::vector<LIB_ALIAS*> const& aAliasList );
186 
192  void UpdateSearchString( wxString const& aSearch );
193 
200  void AttachTo( wxDataViewCtrl* aDataViewCtrl );
201 
210  LIB_ID GetAliasFor( const wxDataViewItem& aSelection ) const;
211 
221  int GetUnitFor( const wxDataViewItem& aSelection ) const;
222 
231  CMP_TREE_NODE::TYPE GetTypeFor( const wxDataViewItem& aSelection ) const;
232 
236  int GetComponentsCount() const;
237 
241  virtual int GetLibrariesCount() const
242  {
243  return m_tree.Children.size();
244  }
245 
252  wxDataViewItem FindItem( const LIB_ID& aLibId );
253 
259  virtual unsigned int GetChildren(
260  wxDataViewItem const& aItem,
261  wxDataViewItemArray& aChildren ) const override;
262 
263  // Freezing/Thawing. Used when updating the table model so that we don't try and fetch
264  // values during updating. Primarily a problem on OSX which doesn't pay attention to the
265  // wxDataViewCtrl's freeze count when updating the keyWindow.
266  void Freeze() { m_freeze++; }
267  void Thaw() { m_freeze--; }
268  bool IsFrozen() const { return m_freeze; }
269 
270 protected:
271  static wxDataViewItem ToItem( CMP_TREE_NODE const* aNode );
272  static CMP_TREE_NODE const* ToNode( wxDataViewItem aItem );
273  static unsigned int IntoArray( CMP_TREE_NODE const& aNode, wxDataViewItemArray& aChildren );
274 
276 
281 
285  virtual bool HasContainerColumns( wxDataViewItem const& aItem ) const override;
286 
290  virtual bool IsContainer( wxDataViewItem const& aItem ) const override;
291 
298  virtual wxDataViewItem GetParent( wxDataViewItem const& aItem ) const override;
299 
303  virtual unsigned int GetColumnCount() const override { return 2; }
304 
310  virtual wxString GetColumnType( unsigned int aCol ) const override { return "string"; }
311 
319  virtual void GetValue(
320  wxVariant& aVariant,
321  wxDataViewItem const& aItem,
322  unsigned int aCol ) const override;
323 
328  virtual bool SetValue(
329  wxVariant const& aVariant,
330  wxDataViewItem const& aItem,
331  unsigned int aCol ) override { return false; }
332 
341  virtual bool GetAttr(
342  wxDataViewItem const& aItem,
343  unsigned int aCol,
344  wxDataViewItemAttr& aAttr ) const override;
345 
346 private:
351  int m_freeze;
352 
353  wxDataViewColumn* m_col_part;
354  wxDataViewColumn* m_col_desc;
355  wxDataViewCtrl* m_widget;
356 
357  WX_DECLARE_STRING_HASH_MAP( std::vector<int>, WIDTH_CACHE );
358 
359  static WIDTH_CACHE m_width_cache;
360 
364  static bool m_show_progress;
365 
374  int ColWidth( CMP_TREE_NODE& aTree, int aCol, wxString const& aHeading );
375 
381  int WidthFor( CMP_TREE_NODE& aNode, int aCol );
382 
387  int WidthFor( wxString const& aHeading, int aCol );
388 
395  bool FindAndExpand(
396  CMP_TREE_NODE& aNode,
397  std::function<bool( CMP_TREE_NODE const* )> aFunc );
398 
402  bool ShowResults();
403 
407  bool ShowPreselect();
408 
412  bool ShowSingleLibrary();
413 };
414 
415 #endif // _CMP_TREE_MODEL_ADAPTER_BASE_H
416 
static unsigned int IntoArray(CMP_TREE_NODE const &aNode, wxDataViewItemArray &aChildren)
Convert CMP_TREE_NODE&#39;s children to wxDataViewItemArray.
CMP_TREE_NODE::TYPE GetTypeFor(const wxDataViewItem &aSelection) const
Return node type for the given item.
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...
Adapter class in the component selector Model-View-Adapter (mediated MVC) architecture.
int GetComponentsCount() const
Return the number of components loaded in the tree.
virtual wxString GetColumnType(unsigned int aCol) const override
Return the type of data stored in the column.
void ShowUnits(bool aShow)
Whether or not to show units.
virtual bool GetAttr(wxDataViewItem const &aItem, unsigned int aCol, wxDataViewItemAttr &aAttr) const override
Get any formatting for an item.
virtual int GetLibrariesCount() const
Return the number of libraries loaded in the tree.
WX_DECLARE_STRING_HASH_MAP(std::vector< int >, WIDTH_CACHE)
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
virtual void GetValue(wxVariant &aVariant, wxDataViewItem const &aItem, unsigned int aCol) const override
Get the value of an item.
static CMP_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
void AddLibrariesWithProgress(const std::vector< wxString > &aNicknames, wxWindow *aParent)
Add all the libraries in a SYMBOL_LIB_TABLE to the model, displaying a progress dialog attached to th...
virtual unsigned int GetColumnCount() const override
Return the number of columns in the model.
static bool m_show_progress
Flag to only show the symbol library table load progress dialog the first time.
int GetUnitFor(const wxDataViewItem &aSelection) const
Return the unit for the given item.
wxObjectDataPtr< CMP_TREE_MODEL_ADAPTER_BASE > PTR
Reference-counting container for a pointer to CMP_TREE_MODEL_ADAPTER_BASE.
bool ShowSingleLibrary()
Find and expand a library if there is only one.
void UpdateSearchString(wxString const &aSearch)
Set the search string provided by the user.
virtual void AddLibrary(wxString const &aLibNickname)=0
Add all the components and their aliases in this library.
int WidthFor(CMP_TREE_NODE &aNode, int aCol)
Return the width required to display a single row&#39;s aCol text.
virtual unsigned int GetChildren(wxDataViewItem const &aItem, wxDataViewItemArray &aChildren) const override
Populate a list of all the children of an item.
LIB_ID GetAliasFor(const wxDataViewItem &aSelection) const
Return the alias for the given item.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
virtual void AddAliasList(wxString const &aNodeName, wxArrayString const &aAliasNameList)=0
Add the given list of components, by name.
virtual bool SetValue(wxVariant const &aVariant, wxDataViewItem const &aItem, unsigned int aCol) override
Set the value of an item.
void SetFilter(CMP_FILTER_TYPE aFilter)
Set the component filter type.
virtual bool HasContainerColumns(wxDataViewItem const &aItem) const override
Check whether a container has columns too.
void SetPreselectNode(LIB_ID const &aLibId, int aUnit)
Set the component name to be selected if there are no search results.
PTR_VECTOR Children
List of child nodes.
void AttachTo(wxDataViewCtrl *aDataViewCtrl)
Attach to a wxDataViewCtrl and initialize it.
bool ShowResults()
Find and expand successful search results.
wxDataViewItem FindItem(const LIB_ID &aLibId)
Returns tree item corresponding to part.
virtual wxDataViewItem GetParent(wxDataViewItem const &aItem) const override
Get the parent of an item.
bool ShowPreselect()
Find and expand preselected node.
static wxDataViewItem ToItem(CMP_TREE_NODE const *aNode)
Convert CMP_TREE_NODE -> wxDataViewItem.
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.
CMP_FILTER_TYPE GetFilter() const
Return the active filter.
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.
Node type: root.