KiCad PCB EDA Suite
CMP_TREE_MODEL_ADAPTER Class Reference

Adapter class in the component selector Model-View-Adapter (mediated MVC) architecture. More...

#include <cmp_tree_model_adapter.h>

Inheritance diagram for CMP_TREE_MODEL_ADAPTER:

Public Types

enum  CMP_FILTER_TYPE { CMP_FILTER_NONE, CMP_FILTER_POWER }
 This enum allows a selective filtering of components to list. More...
 
typedef wxObjectDataPtr< CMP_TREE_MODEL_ADAPTERPTR
 Reference-counting container for a pointer to CMP_TREE_MODEL_ADAPTER. More...
 

Public Member Functions

 ~CMP_TREE_MODEL_ADAPTER ()
 Destructor. More...
 
void SetFilter (CMP_FILTER_TYPE aFilter)
 Set the component filter type. More...
 
void ShowUnits (bool aShow)
 Whether or not to show units. More...
 
void SetPreselectNode (wxString const &aName, int aUnit)
 Set the component name to be selected if there are no search results. More...
 
void AddLibrary (PART_LIB &aLib)
 Add all the components and their aliases in this library. More...
 
void AddAliasList (wxString const &aNodeName, wxArrayString const &aAliasNameList, PART_LIB *aOptionalLib=nullptr)
 Add the given list of components, by name. More...
 
void AddAliasList (wxString const &aNodeName, std::vector< LIB_ALIAS * > const &aAliasList, PART_LIB *aOptionalLib=nullptr)
 Add the given list of components by alias. More...
 
void UpdateSearchString (wxString const &aSearch)
 Set the search string provided by the user. More...
 
void AttachTo (wxDataViewCtrl *aDataViewCtrl)
 Attach to a wxDataViewCtrl and initialize it. More...
 
LIB_ALIASGetAliasFor (wxDataViewItem aSelection) const
 Return the alias for the given item. More...
 
int GetUnitFor (wxDataViewItem aSelection) const
 Return the unit for the given item. More...
 
int GetComponentsCount () const
 Return the number of components loaded in the tree. More...
 

Static Public Member Functions

static PTR Create (PART_LIBS *aLibs)
 Factory function: create a model adapter in a reference-counting container. More...
 

Protected Member Functions

 CMP_TREE_MODEL_ADAPTER (PART_LIBS *aLibs)
 Constructor; takes a set of libraries to be included in the search. More...
 
virtual bool HasContainerColumns (wxDataViewItem const &aItem) const override
 Check whether a container has columns too. More...
 
virtual bool IsContainer (wxDataViewItem const &aItem) const override
 Check whether an item can have children. More...
 
virtual wxDataViewItem GetParent (wxDataViewItem const &aItem) const override
 Get the parent of an item. More...
 
virtual unsigned int GetChildren (wxDataViewItem const &aItem, wxDataViewItemArray &aChildren) const override
 Populate a list of all the children of an item. More...
 
virtual unsigned int GetColumnCount () const override
 Return the number of columns in the model. More...
 
virtual wxString GetColumnType (unsigned int aCol) const override
 Return the type of data stored in the column. More...
 
virtual void GetValue (wxVariant &aVariant, wxDataViewItem const &aItem, unsigned int aCol) const override
 Get the value of an item. More...
 
virtual bool SetValue (wxVariant const &aVariant, wxDataViewItem const &aItem, unsigned int aCol) override
 Set the value of an item. More...
 
virtual bool GetAttr (wxDataViewItem const &aItem, unsigned int aCol, wxDataViewItemAttr &aAttr) const override
 Get any formatting for an item. More...
 

Private Member Functions

 WX_DECLARE_STRING_HASH_MAP (std::vector< int >, WIDTH_CACHE)
 
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. More...
 
int WidthFor (CMP_TREE_NODE &aNode, int aCol)
 Return the width required to display a single row's aCol text. More...
 
int WidthFor (wxString const &aHeading, int aCol)
 Return the width required to display a column's heading. More...
 
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 const*) -> bool) More...
 
bool ShowResults ()
 Find and expand successful search results. More...
 
bool ShowPreselect ()
 Find and expand preselected node. More...
 
bool ShowSingleLibrary ()
 Find and expand a library if there is only one. More...
 

Private Attributes

CMP_FILTER_TYPE m_filter
 
bool m_show_units
 
PART_LIBSm_libs
 
wxString m_preselect_name
 
int m_preselect_unit
 
CMP_TREE_NODE_ROOT m_tree
 
wxDataViewColumn * m_col_part
 
wxDataViewColumn * m_col_desc
 
wxDataViewCtrl * m_widget
 

Static Private Attributes

static WIDTH_CACHE m_width_cache
 

Detailed Description

Adapter class in the component selector Model-View-Adapter (mediated MVC) architecture.

The other pieces are in:

This adapter presents the interface specified by wxDataViewModel to the wxDataViewCtrl:

                  +---+                      +------------------+
+---+  Generates  | A |                      |       VIEW       |
| M |  from libs  | D |   wxDataViewModel    |------------------|
| O | <---------- | A | <------------------> |  wxDataViewCtrl  |
| D |             | P |                      |------------------|
| E | <---------> | T | <------------------- |    wxTextCtrl    |
| L | UpdateScore | E | UpdateSearchString() |------------------|
+---+             | R |                      |                  |
                  +---+                      +------------------+

Because this adapter is a wxDataViewModel, it is reference-counted by wxObject. To ensure this interface is used correctly, the constructor is private; CMP_TREE_MODEL_ADAPTER should be created by the static factory method CMP_TREE_MODEL_ADAPTER::Create().

Quick summary of methods used to drive this class:

Quick summary of methods used by the View:

Methods implemented as part of wxDataViewModel:

Definition at line 94 of file cmp_tree_model_adapter.h.

Member Typedef Documentation

Reference-counting container for a pointer to CMP_TREE_MODEL_ADAPTER.

Definition at line 101 of file cmp_tree_model_adapter.h.

Member Enumeration Documentation

This enum allows a selective filtering of components to list.

Enumerator
CMP_FILTER_NONE 

no filtering

CMP_FILTER_POWER 

list components flagged PWR

Definition at line 120 of file cmp_tree_model_adapter.h.

Constructor & Destructor Documentation

CMP_TREE_MODEL_ADAPTER::~CMP_TREE_MODEL_ADAPTER ( )

Destructor.

Do NOT delete this class manually; it is reference-counted by wxObject.

Definition at line 92 of file cmp_tree_model_adapter.cpp.

93 {}
CMP_TREE_MODEL_ADAPTER::CMP_TREE_MODEL_ADAPTER ( PART_LIBS aLibs)
protected

Constructor; takes a set of libraries to be included in the search.

Definition at line 81 of file cmp_tree_model_adapter.cpp.

Referenced by Create().

83  m_show_units( true ),
84  m_libs( aLibs ),
85  m_preselect_unit( 0 ),
86  m_col_part( nullptr ),
87  m_col_desc( nullptr ),
88  m_widget( nullptr )
89 {}

Member Function Documentation

void CMP_TREE_MODEL_ADAPTER::AddAliasList ( wxString const &  aNodeName,
wxArrayString const &  aAliasNameList,
PART_LIB aOptionalLib = nullptr 
)

Add the given list of components, by name.

To be called in the setup phase.

Parameters
aNodeNamethe parent node the components will appear under
aAliasNameListlist of alias names
aOptionalLiblibrary to look up names in (null = global)

Definition at line 134 of file cmp_tree_model_adapter.cpp.

References PART_LIB::FindAlias(), PART_LIBS::FindLibraryAlias(), m_libs, and name.

Referenced by AddLibrary().

138 {
139  std::vector<LIB_ALIAS*> alias_list;
140 
141  for( const wxString& name: aAliasNameList )
142  {
143  LIB_ALIAS* a;
144 
145  if( aOptionalLib )
146  a = aOptionalLib->FindAlias( name );
147  else
148  a = m_libs->FindLibraryAlias( LIB_ID( wxEmptyString, name ), wxEmptyString );
149 
150  if( a )
151  alias_list.push_back( a );
152  }
153 
154  AddAliasList( aNodeName, alias_list, aOptionalLib );
155 }
Part library alias object definition.
Class LIB_ID.
Definition: lib_id.h:56
LIB_ALIAS * FindLibraryAlias(const LIB_ID &aLibId, const wxString &aLibraryName=wxEmptyString)
Function FindLibraryEntry searches all libraries in the list for an entry.
void AddAliasList(wxString const &aNodeName, wxArrayString const &aAliasNameList, PART_LIB *aOptionalLib=nullptr)
Add the given list of components, by name.
const char * name
LIB_ALIAS * FindAlias(const wxString &aName)
Find LIB_ALIAS by aName.
void CMP_TREE_MODEL_ADAPTER::AddAliasList ( wxString const &  aNodeName,
std::vector< LIB_ALIAS * > const &  aAliasList,
PART_LIB aOptionalLib = nullptr 
)

Add the given list of components by alias.

To be called in the setup phase.

Parameters
aNodeNamethe parent node the components will appear under
aAliasListlist of aliases
aOptionalLiblibrary to look up names in (null = global)

Definition at line 158 of file cmp_tree_model_adapter.cpp.

References CMP_TREE_NODE_LIB::AddAlias(), CMP_TREE_NODE_ROOT::AddLib(), CMP_TREE_NODE::AssignIntrinsicRanks(), and m_tree.

162 {
163  auto& lib_node = m_tree.AddLib( aNodeName );
164 
165  for( auto a: aAliasList )
166  {
167  lib_node.AddAlias( a );
168  }
169 
170  lib_node.AssignIntrinsicRanks();
172 }
void AssignIntrinsicRanks()
Store intrinsic ranks on all children of this node.
CMP_TREE_NODE_ALIAS & AddAlias(LIB_ALIAS *aAlias)
Construct a new alias node, add it to this library, and return it.
CMP_TREE_NODE_LIB & AddLib(wxString const &aName)
Construct an empty library node, add it to the root, and return it.
void CMP_TREE_MODEL_ADAPTER::AddLibrary ( PART_LIB aLib)

Add all the components and their aliases in this library.

To be called in the setup phase.

Parameters
aLibreference to a library

Definition at line 115 of file cmp_tree_model_adapter.cpp.

References AddAliasList(), CMP_TREE_NODE::AssignIntrinsicRanks(), CMP_FILTER_POWER, PART_LIB::GetAliases(), PART_LIB::GetEntryTypePowerNames(), PART_LIB::GetName(), m_filter, and m_tree.

116 {
117  if( m_filter == CMP_FILTER_POWER )
118  {
119  wxArrayString all_aliases;
120  aLib.GetEntryTypePowerNames( all_aliases );
121  AddAliasList( aLib.GetName(), all_aliases, &aLib );
122  }
123  else
124  {
125  std::vector<LIB_ALIAS*> all_aliases;
126  aLib.GetAliases( all_aliases );
127  AddAliasList( aLib.GetName(), all_aliases, &aLib );
128  }
129 
131 }
void GetEntryTypePowerNames(wxArrayString &aNames)
Load a string array with the names of entries of type POWER in this library.
void GetAliases(std::vector< LIB_ALIAS * > &aAliases)
Load a vector with all the entries in this library.
void AssignIntrinsicRanks()
Store intrinsic ranks on all children of this node.
void AddAliasList(wxString const &aNodeName, wxArrayString const &aAliasNameList, PART_LIB *aOptionalLib=nullptr)
Add the given list of components, by name.
const wxString GetName() const
Return the file name without path or extension.
void CMP_TREE_MODEL_ADAPTER::AttachTo ( wxDataViewCtrl *  aDataViewCtrl)

Attach to a wxDataViewCtrl and initialize it.

This will set up columns and associate the model via the adapter.

Parameters
aDataViewCtrlthe view component in the dialog

Definition at line 197 of file cmp_tree_model_adapter.cpp.

References ColWidth(), kDataViewIndent, m_col_desc, m_col_part, m_tree, and m_widget.

Referenced by UpdateSearchString().

198 {
199  m_widget = aDataViewCtrl;
200  aDataViewCtrl->Freeze();
201  aDataViewCtrl->SetIndent( kDataViewIndent );
202  aDataViewCtrl->AssociateModel( this );
203  aDataViewCtrl->ClearColumns();
204 
205  wxString part_head = _( "Part" );
206  wxString desc_head = _( "Desc" );
207 
208  m_col_part = aDataViewCtrl->AppendTextColumn( part_head, 0, wxDATAVIEW_CELL_INERT,
209  ColWidth( m_tree, 0, part_head ) );
210  m_col_desc = aDataViewCtrl->AppendTextColumn( desc_head, 1, wxDATAVIEW_CELL_INERT,
211  ColWidth( m_tree, 1, desc_head ) );
212  aDataViewCtrl->Thaw();
213 }
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.
static const int kDataViewIndent
int CMP_TREE_MODEL_ADAPTER::ColWidth ( CMP_TREE_NODE aTree,
int  aCol,
wxString const &  aHeading 
)
private

Compute the width required for the given column of a node and its children.

Parameters
aTree- root node of the tree
aCol- column number
aHeading- heading text, to set the minimum width

Definition at line 340 of file cmp_tree_model_adapter.cpp.

References CMP_TREE_NODE::Children, kDataViewIndent, max, CMP_TREE_NODE::Score, and WidthFor().

Referenced by AttachTo().

341 {
342  const int indent = aCol ? 0 : kDataViewIndent;
343 
344  int min_width = WidthFor( aHeading, aCol );
345  int width = std::max( aTree.Score > 0 ? WidthFor( aTree, aCol ) : 0, min_width );
346 
347  if( aTree.Score > 0 )
348  {
349  for( auto& node: aTree.Children )
350  {
351  width = std::max( width, ColWidth( *node, aCol, aHeading ) + indent );
352  }
353  }
354 
355  return width;
356 }
int WidthFor(CMP_TREE_NODE &aNode, int aCol)
Return the width required to display a single row's aCol text.
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.
#define max(a, b)
Definition: auxiliary.h:86
std::vector< std::unique_ptr< CMP_TREE_NODE > > Children
List of child nodes.
static const int kDataViewIndent
int Score
The score of an item resulting from the search algorithm.
CMP_TREE_MODEL_ADAPTER::PTR CMP_TREE_MODEL_ADAPTER::Create ( PART_LIBS aLibs)
static

Factory function: create a model adapter in a reference-counting container.

Parameters
aLibslibrary set from which parts will be loaded

Definition at line 73 of file cmp_tree_model_adapter.cpp.

References CMP_TREE_MODEL_ADAPTER().

Referenced by LIB_EDIT_FRAME::DeleteOnePart(), LIB_VIEW_FRAME::OnSelectSymbol(), and SCH_BASE_FRAME::SelectComponentFromLibrary().

74 {
75  auto adapter = new CMP_TREE_MODEL_ADAPTER( aLibs );
76  auto container = CMP_TREE_MODEL_ADAPTER::PTR( adapter );
77  return container;
78 }
wxObjectDataPtr< CMP_TREE_MODEL_ADAPTER > PTR
Reference-counting container for a pointer to CMP_TREE_MODEL_ADAPTER.
CMP_TREE_MODEL_ADAPTER(PART_LIBS *aLibs)
Constructor; takes a set of libraries to be included in the search.
bool CMP_TREE_MODEL_ADAPTER::FindAndExpand ( CMP_TREE_NODE aNode,
std::function< bool(CMP_TREE_NODE const *)>  aFunc 
)
private

Find any results worth highlighting and expand them, according to given criteria (f(CMP_TREE_NODE const*) -> bool)

Returns
whether a node was expanded

Definition at line 398 of file cmp_tree_model_adapter.cpp.

References CMP_TREE_NODE::Children, and m_widget.

Referenced by ShowPreselect(), ShowResults(), and ShowSingleLibrary().

401 {
402  for( auto& node: aNode.Children )
403  {
404  if( aFunc( &*node ) )
405  {
406  auto item = wxDataViewItem(
407  const_cast<void*>( static_cast<void const*>( &*node ) ) );
408  m_widget->ExpandAncestors( item );
409  m_widget->EnsureVisible( item );
410  m_widget->Select( item );
411  return true;
412  }
413  else if( FindAndExpand( *node, aFunc ) )
414  {
415  return true;
416  }
417  }
418 
419  return false;
420 }
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...
std::vector< std::unique_ptr< CMP_TREE_NODE > > Children
List of child nodes.
LIB_ALIAS * CMP_TREE_MODEL_ADAPTER::GetAliasFor ( wxDataViewItem  aSelection) const

Return the alias for the given item.

Parameters
aSelectionitem from the wxDataViewCtrl (see wxDataViewCtrl::GetSelection())
Returns
alias, or nullptr if none is selected

Definition at line 216 of file cmp_tree_model_adapter.cpp.

References ToNode().

217 {
218  auto node = ToNode( aSelection );
219  return node ? node->Alias : nullptr;
220 }
static CMP_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
bool CMP_TREE_MODEL_ADAPTER::GetAttr ( wxDataViewItem const &  aItem,
unsigned int  aCol,
wxDataViewItemAttr &  aAttr 
) const
overrideprotectedvirtual

Get any formatting for an item.

Parameters
aItemitem to get formatting for
aColcolumn number of interest
aAttrreceiver for attributes
Returns
true iff the item has non-default attributes

Definition at line 313 of file cmp_tree_model_adapter.cpp.

References CMP_TREE_NODE::ALIAS, and ToNode().

317 {
318  auto node = ToNode( aItem );
319  wxASSERT( node );
320 
321  if( node->Type != CMP_TREE_NODE::ALIAS )
322  {
323  // Currently only aliases are formatted at all
324  return false;
325  }
326 
327  if( node->Alias && !node->Alias->IsRoot() && aCol == 0 )
328  {
329  // Names of non-root aliases are italicized
330  aAttr.SetItalic( true );
331  return true;
332  }
333  else
334  {
335  return false;
336  }
337 }
static CMP_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
unsigned int CMP_TREE_MODEL_ADAPTER::GetChildren ( wxDataViewItem const &  aItem,
wxDataViewItemArray &  aChildren 
) const
overrideprotectedvirtual

Populate a list of all the children of an item.

Returns
number of children

Definition at line 278 of file cmp_tree_model_adapter.cpp.

References IntoArray(), m_show_units, m_tree, and ToNode().

281 {
282  auto node = ( aItem.IsOk() ? ToNode( aItem ) : &m_tree );
283 
284  if( node->Type != CMP_TREE_NODE::TYPE::ALIAS || m_show_units )
285  return IntoArray( *node, aChildren );
286  else
287  return 0;
288 }
static unsigned int IntoArray(CMP_TREE_NODE const &aNode, wxDataViewItemArray &aChildren)
Convert CMP_TREE_NODE's children to wxDataViewItemArray.
static CMP_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
virtual unsigned int CMP_TREE_MODEL_ADAPTER::GetColumnCount ( ) const
inlineoverrideprotectedvirtual

Return the number of columns in the model.

Definition at line 262 of file cmp_tree_model_adapter.h.

262 { return 2; }
virtual wxString CMP_TREE_MODEL_ADAPTER::GetColumnType ( unsigned int  aCol) const
inlineoverrideprotectedvirtual

Return the type of data stored in the column.

Returns
type of data as indicated by wxVariant::GetType()

Definition at line 269 of file cmp_tree_model_adapter.h.

269 { return "string"; }
int CMP_TREE_MODEL_ADAPTER::GetComponentsCount ( ) const

Return the number of components loaded in the tree.

Definition at line 230 of file cmp_tree_model_adapter.cpp.

References CMP_TREE_NODE::Children, and m_tree.

231 {
232  int n = 0;
233 
234  for( auto& lib: m_tree.Children )
235  {
236  for( auto& alias: lib->Children )
237  {
238  (void) alias;
239  ++n;
240  }
241  }
242 
243  return n;
244 }
std::vector< std::unique_ptr< CMP_TREE_NODE > > Children
List of child nodes.
wxDataViewItem CMP_TREE_MODEL_ADAPTER::GetParent ( wxDataViewItem const &  aItem) const
overrideprotectedvirtual

Get the parent of an item.

Parameters
aItemitem to get the parent of
Returns
parent of aItem, or an invalid wxDataViewItem if parent is root

Definition at line 260 of file cmp_tree_model_adapter.cpp.

References ToItem(), and ToNode().

261 {
262  auto node = ToNode( aItem );
263  auto parent = node ? node->Parent : nullptr;
264 
265  // wxDataViewModel has no root node, but rather top-level elements have
266  // an invalid (null) parent.
267  if( !node || !parent || parent->Type == CMP_TREE_NODE::TYPE::ROOT )
268  {
269  return ToItem( nullptr );
270  }
271  else
272  {
273  return ToItem( parent );
274  }
275 }
static wxDataViewItem ToItem(CMP_TREE_NODE const *aNode)
Convert CMP_TREE_NODE -> wxDataViewItem.
static CMP_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
int CMP_TREE_MODEL_ADAPTER::GetUnitFor ( wxDataViewItem  aSelection) const

Return the unit for the given item.

Parameters
aSelectionitem from the wxDataViewCtrl (see wxDataViewCtrl::GetSelection())
Returns
Unit, or zero if the alias itself is selected. Return valid is invalid if GetAliasFor() returns nullptr.

Definition at line 223 of file cmp_tree_model_adapter.cpp.

References ToNode().

224 {
225  auto node = ToNode( aSelection );
226  return node ? node->Unit : 0;
227 }
static CMP_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
void CMP_TREE_MODEL_ADAPTER::GetValue ( wxVariant &  aVariant,
wxDataViewItem const &  aItem,
unsigned int  aCol 
) const
overrideprotectedvirtual

Get the value of an item.

Parameters
aVariantwxVariant to receive the data
aItemitem whose data will be placed into aVariant
aColcolumn number of the data

Definition at line 291 of file cmp_tree_model_adapter.cpp.

References ToNode().

295 {
296  auto node = ToNode( aItem );
297  wxASSERT( node );
298 
299  switch( aCol )
300  {
301  case 0:
302  aVariant = node->Name;
303  break;
304  case 1:
305  aVariant = node->Desc;
306  break;
307  default:
308  wxFAIL_MSG( "Invalid column ID!" );
309  }
310 }
static CMP_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
bool CMP_TREE_MODEL_ADAPTER::HasContainerColumns ( wxDataViewItem const &  aItem) const
overrideprotectedvirtual

Check whether a container has columns too.

Definition at line 247 of file cmp_tree_model_adapter.cpp.

References IsContainer().

248 {
249  return IsContainer( aItem );
250 }
virtual bool IsContainer(wxDataViewItem const &aItem) const override
Check whether an item can have children.
bool CMP_TREE_MODEL_ADAPTER::IsContainer ( wxDataViewItem const &  aItem) const
overrideprotectedvirtual

Check whether an item can have children.

Definition at line 253 of file cmp_tree_model_adapter.cpp.

References ToNode().

Referenced by HasContainerColumns().

254 {
255  auto node = ToNode( aItem );
256  return node ? node->Children.size() : true;
257 }
static CMP_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
void CMP_TREE_MODEL_ADAPTER::SetFilter ( CMP_FILTER_TYPE  aFilter)

Set the component filter type.

Must be set before adding libraries

Parameters
aFilterif CMP_FILTER_POWER, only power parts are loaded

Definition at line 96 of file cmp_tree_model_adapter.cpp.

References m_filter.

97 {
98  m_filter = aFilter;
99 }
void CMP_TREE_MODEL_ADAPTER::SetPreselectNode ( wxString const &  aName,
int  aUnit 
)

Set the component name to be selected if there are no search results.

May be set at any time; updates at the next UpdateSearchString().

Parameters
aNamecomponent name to be selected
aUnitunit to be selected, if > 0 (0 selects the alias itself)

Definition at line 108 of file cmp_tree_model_adapter.cpp.

References m_preselect_name, and m_preselect_unit.

109 {
110  m_preselect_name = aName;
111  m_preselect_unit = aUnit;
112 }
virtual bool CMP_TREE_MODEL_ADAPTER::SetValue ( wxVariant const &  aVariant,
wxDataViewItem const &  aItem,
unsigned int  aCol 
)
inlineoverrideprotectedvirtual

Set the value of an item.

Does nothing - this model doesn't support editing.

Definition at line 287 of file cmp_tree_model_adapter.h.

290  { return false; }
bool CMP_TREE_MODEL_ADAPTER::ShowPreselect ( )
private

Find and expand preselected node.

Definition at line 433 of file cmp_tree_model_adapter.cpp.

References CMP_TREE_NODE::ALIAS, CMP_TREE_NODE::Children, FindAndExpand(), m_preselect_name, m_preselect_unit, m_tree, CMP_TREE_NODE::Name, CMP_TREE_NODE::Parent, CMP_TREE_NODE::Type, CMP_TREE_NODE::UNIT, and CMP_TREE_NODE::Unit.

Referenced by UpdateSearchString().

434 {
435  if( m_preselect_name == wxEmptyString )
436  return false;
437 
438  return FindAndExpand( m_tree,
439  [&]( CMP_TREE_NODE const* n )
440  {
441  if( n->Type == CMP_TREE_NODE::ALIAS && ( n->Children.empty() || !m_preselect_unit ) )
442  return m_preselect_name == n->Name;
443  else if( n->Type == CMP_TREE_NODE::UNIT && m_preselect_unit )
444  return m_preselect_name == n->Parent->Name && m_preselect_unit == n->Unit;
445  else
446  return false;
447  } );
448 }
wxString Name
Actual name of the part.
enum TYPE Type
Node type.
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...
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
CMP_TREE_NODE * Parent
Parent node or null.
int Unit
Actual unit, or zero.
std::vector< std::unique_ptr< CMP_TREE_NODE > > Children
List of child nodes.
bool CMP_TREE_MODEL_ADAPTER::ShowResults ( )
private

Find and expand successful search results.

Definition at line 423 of file cmp_tree_model_adapter.cpp.

References FindAndExpand(), m_tree, CMP_TREE_NODE::Score, and CMP_TREE_NODE::Type.

Referenced by UpdateSearchString().

424 {
425  return FindAndExpand( m_tree,
426  []( CMP_TREE_NODE const* n )
427  {
428  return n->Type == CMP_TREE_NODE::TYPE::ALIAS && n->Score > 1;
429  } );
430 }
enum TYPE Type
Node type.
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...
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
int Score
The score of an item resulting from the search algorithm.
bool CMP_TREE_MODEL_ADAPTER::ShowSingleLibrary ( )
private

Find and expand a library if there is only one.

Definition at line 451 of file cmp_tree_model_adapter.cpp.

References CMP_TREE_NODE::Children, FindAndExpand(), m_tree, CMP_TREE_NODE::Parent, and CMP_TREE_NODE::Type.

Referenced by UpdateSearchString().

452 {
453  return FindAndExpand( m_tree,
454  []( CMP_TREE_NODE const* n )
455  {
456  return n->Type == CMP_TREE_NODE::TYPE::ALIAS &&
457  n->Parent->Parent->Children.size() == 1;
458  } );
459 }
enum TYPE Type
Node type.
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...
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
CMP_TREE_NODE * Parent
Parent node or null.
std::vector< std::unique_ptr< CMP_TREE_NODE > > Children
List of child nodes.
void CMP_TREE_MODEL_ADAPTER::ShowUnits ( bool  aShow)

Whether or not to show units.

May be set at any time; updates at the next UpdateSearchString()

Parameters
aShowif true, units are displayed

Definition at line 102 of file cmp_tree_model_adapter.cpp.

References m_show_units.

103 {
104  m_show_units = aShow;
105 }
void CMP_TREE_MODEL_ADAPTER::UpdateSearchString ( wxString const &  aSearch)

Set the search string provided by the user.

Parameters
aSearchfull, unprocessed search text

Definition at line 175 of file cmp_tree_model_adapter.cpp.

References AttachTo(), m_tree, m_widget, CMP_TREE_NODE::ResetScore(), ShowPreselect(), ShowResults(), ShowSingleLibrary(), CMP_TREE_NODE::SortNodes(), and CMP_TREE_NODE_ROOT::UpdateScore().

176 {
177  m_tree.ResetScore();
178 
179  wxStringTokenizer tokenizer( aSearch );
180 
181  while( tokenizer.HasMoreTokens() )
182  {
183  const wxString term = tokenizer.GetNextToken().Lower();
184  EDA_COMBINED_MATCHER matcher( term );
185 
186  m_tree.UpdateScore( matcher );
187  }
188 
189  m_tree.SortNodes();
190  Cleared();
191  AttachTo( m_widget );
192 
194 }
virtual void UpdateScore(EDA_COMBINED_MATCHER &aMatcher) override
Update the score for this part.
void ResetScore()
Initialize score to kLowestDefaultScore, recursively.
void SortNodes()
Sort child nodes quickly and recursively (IntrinsicRanks must have been set).
void AttachTo(wxDataViewCtrl *aDataViewCtrl)
Attach to a wxDataViewCtrl and initialize it.
bool ShowSingleLibrary()
Find and expand a library if there is only one.
bool ShowResults()
Find and expand successful search results.
bool ShowPreselect()
Find and expand preselected node.
int CMP_TREE_MODEL_ADAPTER::WidthFor ( CMP_TREE_NODE aNode,
int  aCol 
)
private

Return the width required to display a single row's aCol text.

This is cached for efficiency as it's very slow on some platforms (cough macOS)

Definition at line 359 of file cmp_tree_model_adapter.cpp.

References CMP_TREE_NODE::Desc, kDataViewIndent, m_widget, m_width_cache, and CMP_TREE_NODE::Name.

Referenced by ColWidth().

360 {
361  auto result = m_width_cache.find( aNode.Name );
362 
363  if( result != m_width_cache.end() )
364  {
365  return result->second[aCol];
366  }
367  else
368  {
369  int wname = m_widget->GetTextExtent( aNode.Name ).x + kDataViewIndent;
370  int wdesc = m_widget->GetTextExtent( aNode.Desc ).x;
371 
372  auto& val = m_width_cache[aNode.Name];
373  val.push_back( wname );
374  val.push_back( wdesc );
375  return val[aCol];
376  }
377 }
wxString Name
Actual name of the part.
wxString Desc
Description to be displayed.
static WIDTH_CACHE m_width_cache
static const int kDataViewIndent
int CMP_TREE_MODEL_ADAPTER::WidthFor ( wxString const &  aHeading,
int  aCol 
)
private

Return the width required to display a column's heading.

This is cached by column number for the same reason as the width per cell.

Definition at line 380 of file cmp_tree_model_adapter.cpp.

References m_widget.

381 {
382  static std::vector<int> widths;
383 
384  for( int i = (int) widths.size(); i <= aCol; ++i )
385  {
386  widths.push_back( 0 );
387  }
388 
389  if( widths[aCol] == 0 )
390  {
391  widths[aCol] = m_widget->GetTextExtent( aHeading ).x;
392  }
393 
394  return widths[aCol];
395 }
CMP_TREE_MODEL_ADAPTER::WX_DECLARE_STRING_HASH_MAP ( std::vector< int >  ,
WIDTH_CACHE   
)
private

Member Data Documentation

wxDataViewColumn* CMP_TREE_MODEL_ADAPTER::m_col_desc
private

Definition at line 315 of file cmp_tree_model_adapter.h.

Referenced by AttachTo().

wxDataViewColumn* CMP_TREE_MODEL_ADAPTER::m_col_part
private

Definition at line 314 of file cmp_tree_model_adapter.h.

Referenced by AttachTo().

CMP_FILTER_TYPE CMP_TREE_MODEL_ADAPTER::m_filter
private

Definition at line 306 of file cmp_tree_model_adapter.h.

Referenced by AddLibrary(), and SetFilter().

PART_LIBS* CMP_TREE_MODEL_ADAPTER::m_libs
private

Definition at line 308 of file cmp_tree_model_adapter.h.

Referenced by AddAliasList().

wxString CMP_TREE_MODEL_ADAPTER::m_preselect_name
private

Definition at line 309 of file cmp_tree_model_adapter.h.

Referenced by SetPreselectNode(), and ShowPreselect().

int CMP_TREE_MODEL_ADAPTER::m_preselect_unit
private

Definition at line 310 of file cmp_tree_model_adapter.h.

Referenced by SetPreselectNode(), and ShowPreselect().

bool CMP_TREE_MODEL_ADAPTER::m_show_units
private

Definition at line 307 of file cmp_tree_model_adapter.h.

Referenced by GetChildren(), and ShowUnits().

wxDataViewCtrl* CMP_TREE_MODEL_ADAPTER::m_widget
private

Definition at line 316 of file cmp_tree_model_adapter.h.

Referenced by AttachTo(), FindAndExpand(), UpdateSearchString(), and WidthFor().

CMP_TREE_MODEL_ADAPTER::WIDTH_CACHE CMP_TREE_MODEL_ADAPTER::m_width_cache
staticprivate

Definition at line 320 of file cmp_tree_model_adapter.h.

Referenced by WidthFor().


The documentation for this class was generated from the following files: