KiCad PCB EDA Suite
CMP_TREE_NODE_ALIAS Class Reference

Node type: alias. More...

#include <cmp_tree_model.h>

Inheritance diagram for CMP_TREE_NODE_ALIAS:
CMP_TREE_NODE

Public Types

enum  TYPE {
  ROOT, LIB, ALIAS, UNIT,
  INVALID
}
 

Public Member Functions

 CMP_TREE_NODE_ALIAS (CMP_TREE_NODE_ALIAS const &_)=delete
 The addresses of CMP_TREE_NODEs are used as unique IDs for the wxDataViewModel, so don't let them be copied around. More...
 
void operator= (CMP_TREE_NODE_ALIAS const &_)=delete
 
 CMP_TREE_NODE_ALIAS (CMP_TREE_NODE *aParent, LIB_ALIAS *aAlias)
 Construct an alias node. More...
 
virtual void UpdateScore (EDA_COMBINED_MATCHER &aMatcher) override
 Perform the actual search. More...
 
void ResetScore ()
 Initialize score to kLowestDefaultScore, recursively. More...
 
void AssignIntrinsicRanks ()
 Store intrinsic ranks on all children of this node. More...
 
void SortNodes ()
 Sort child nodes quickly and recursively (IntrinsicRanks must have been set). More...
 

Static Public Member Functions

static int Compare (CMP_TREE_NODE const &aNode1, CMP_TREE_NODE const &aNode2)
 Compare two nodes. More...
 

Public Attributes

CMP_TREE_NODEParent
 Parent node or null. More...
 
std::vector< std::unique_ptr< CMP_TREE_NODE > > Children
 List of child nodes. More...
 
enum TYPE Type
 Node type. More...
 
int IntrinsicRank
 The rank of the item before any search terms are applied. More...
 
int Score
 The score of an item resulting from the search algorithm. More...
 
wxString Name
 Actual name of the part. More...
 
wxString Desc
 Description to be displayed. More...
 
wxString MatchName
 Normalized name for matching. More...
 
wxString SearchText
 Descriptive text to search. More...
 
LIB_ALIASAlias
 Actual LIB_ALIAS*, or null. More...
 
int Unit
 Actual unit, or zero. More...
 

Protected Member Functions

CMP_TREE_NODE_UNITAddUnit (int aUnit)
 Add a new unit to the component and return it. More...
 

Detailed Description

Node type: alias.

Definition at line 178 of file cmp_tree_model.h.

Member Enumeration Documentation

enum CMP_TREE_NODE::TYPE
inherited
Enumerator
ROOT 
LIB 
ALIAS 
UNIT 
INVALID 

Definition at line 78 of file cmp_tree_model.h.

Constructor & Destructor Documentation

CMP_TREE_NODE_ALIAS::CMP_TREE_NODE_ALIAS ( CMP_TREE_NODE_ALIAS const &  _)
delete

The addresses of CMP_TREE_NODEs are used as unique IDs for the wxDataViewModel, so don't let them be copied around.

CMP_TREE_NODE_ALIAS::CMP_TREE_NODE_ALIAS ( CMP_TREE_NODE aParent,
LIB_ALIAS aAlias 
)

Construct an alias node.

All fields will be populated from the LIB_ALIAS, including children (unit nodes will be generated automatically).

Parameters
aParentparent node, should be a CMP_TREE_NODE_LIB
aAliasLIB_ALIAS to populate the node

Definition at line 131 of file cmp_tree_model.cpp.

References AddUnit(), CMP_TREE_NODE::ALIAS, CMP_TREE_NODE::Alias, CMP_TREE_NODE::Desc, LIB_ALIAS::GetDescription(), LIB_PART::GetFootprintField(), LIB_ALIAS::GetKeyWords(), LIB_ALIAS::GetName(), LIB_ALIAS::GetPart(), EDA_TEXT::GetText(), LIB_PART::GetUnitCount(), LIB_PART::IsMulti(), CMP_TREE_NODE::MatchName, CMP_TREE_NODE::Name, CMP_TREE_NODE::Parent, CMP_TREE_NODE::SearchText, and CMP_TREE_NODE::Type.

132 {
133  Parent = aParent;
134  Type = ALIAS;
135  Name = aAlias->GetName();
136  Desc = aAlias->GetDescription();
137  Alias = aAlias;
138 
139  // Pre-normalized strings for fast case-insensitive matching
140  // Search text spaces out keywords and description to penalize description
141  // matches - earlier matches are worth more.
142  MatchName = aAlias->GetName().Lower();
143  SearchText = (aAlias->GetKeyWords() + " " + Desc).Lower();
144 
145  // Extract default footprint text
146  LIB_PART* part = aAlias->GetPart();
147 
148  wxString footprint;
149 
150  if( part )
151  {
152  footprint = part->GetFootprintField().GetText();
153  }
154 
155  // If a footprint is defined for the part,
156  // add it to the serach string
157  if( !footprint.IsEmpty() )
158  {
159  SearchText += " ";
160  SearchText += footprint.Lower();
161  }
162 
163  if( aAlias->GetPart()->IsMulti() )
164  {
165  for( int u = 1; u <= aAlias->GetPart()->GetUnitCount(); ++u )
166  {
167  AddUnit( u );
168  }
169  }
170 }
LIB_FIELD & GetFootprintField()
Return reference to the footprint field.
wxString Name
Actual name of the part.
wxString Desc
Description to be displayed.
CMP_TREE_NODE_UNIT & AddUnit(int aUnit)
Add a new unit to the component and return it.
bool IsMulti() const
Function IsMulti.
enum TYPE Type
Node type.
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:130
wxString GetDescription() const
Class LIB_PART defines a library part object.
wxString MatchName
Normalized name for matching.
CMP_TREE_NODE * Parent
Parent node or null.
LIB_PART * GetPart() const
Function GetPart gets the shared LIB_PART.
int GetUnitCount() const
wxString SearchText
Descriptive text to search.
const wxString & GetName() const
LIB_ALIAS * Alias
Actual LIB_ALIAS*, or null.
wxString GetKeyWords() const

Member Function Documentation

CMP_TREE_NODE_UNIT & CMP_TREE_NODE_ALIAS::AddUnit ( int  aUnit)
protected

Add a new unit to the component and return it.

This should not be used directly, as the constructor adds all units.

Definition at line 173 of file cmp_tree_model.cpp.

References CMP_TREE_NODE::Children.

Referenced by CMP_TREE_NODE_ALIAS().

174 {
175  CMP_TREE_NODE_UNIT* unit = new CMP_TREE_NODE_UNIT( this, aUnit );
176  Children.push_back( std::unique_ptr<CMP_TREE_NODE>( unit ) );
177  return *unit;
178 }
std::vector< std::unique_ptr< CMP_TREE_NODE > > Children
List of child nodes.
Node type: unit of component.
void CMP_TREE_NODE::AssignIntrinsicRanks ( )
inherited

Store intrinsic ranks on all children of this node.

See IntrinsicRank member doc for more information.

Definition at line 61 of file cmp_tree_model.cpp.

References CMP_TREE_NODE::Children, CMP_TREE_NODE::IntrinsicRank, and CMP_TREE_NODE::MatchName.

Referenced by CMP_TREE_MODEL_ADAPTER::AddAliasList(), and CMP_TREE_MODEL_ADAPTER::AddLibrary().

62 {
63  std::vector<CMP_TREE_NODE*> sort_buf;
64 
65  for( auto const& node: Children )
66  sort_buf.push_back( &*node );
67 
68  std::sort( sort_buf.begin(), sort_buf.end(),
69  []( CMP_TREE_NODE* a, CMP_TREE_NODE* b ) -> bool
70  { return a->MatchName > b->MatchName; } );
71 
72  for( int i = 0; i < (int) sort_buf.size(); ++i )
73  sort_buf[i]->IntrinsicRank = i;
74 }
wxString MatchName
Normalized name for matching.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
int IntrinsicRank
The rank of the item before any search terms are applied.
std::vector< std::unique_ptr< CMP_TREE_NODE > > Children
List of child nodes.
int CMP_TREE_NODE::Compare ( CMP_TREE_NODE const &  aNode1,
CMP_TREE_NODE const &  aNode2 
)
staticinherited

Compare two nodes.

Returns negative if aNode1 < aNode2, zero if aNode1 == aNode2, or positive if aNode1 > aNode2.

Definition at line 90 of file cmp_tree_model.cpp.

References CMP_TREE_NODE::IntrinsicRank, CMP_TREE_NODE::Parent, CMP_TREE_NODE::Score, and CMP_TREE_NODE::Type.

Referenced by CMP_TREE_NODE::SortNodes().

91 {
92  if( aNode1.Type != aNode2.Type )
93  return 0;
94 
95  if( aNode1.Score != aNode2.Score )
96  return aNode1.Score - aNode2.Score;
97 
98  if( aNode1.Parent != aNode2.Parent )
99  return 0;
100 
101  return aNode1.IntrinsicRank - aNode2.IntrinsicRank;
102 }
void CMP_TREE_NODE_ALIAS::operator= ( CMP_TREE_NODE_ALIAS const &  _)
delete
void CMP_TREE_NODE::ResetScore ( )
inherited

Initialize score to kLowestDefaultScore, recursively.

Definition at line 52 of file cmp_tree_model.cpp.

References CMP_TREE_NODE::Children, kLowestDefaultScore, and CMP_TREE_NODE::Score.

Referenced by CMP_TREE_MODEL_ADAPTER::UpdateSearchString().

53 {
54  for( auto& child: Children )
55  child->ResetScore();
56 
58 }
static const unsigned kLowestDefaultScore
std::vector< std::unique_ptr< CMP_TREE_NODE > > Children
List of child nodes.
int Score
The score of an item resulting from the search algorithm.
void CMP_TREE_NODE::SortNodes ( )
inherited

Sort child nodes quickly and recursively (IntrinsicRanks must have been set).

Definition at line 77 of file cmp_tree_model.cpp.

References CMP_TREE_NODE::Children, and CMP_TREE_NODE::Compare().

Referenced by CMP_TREE_MODEL_ADAPTER::UpdateSearchString().

78 {
79  std::sort( Children.begin(), Children.end(),
80  []( std::unique_ptr<CMP_TREE_NODE> const& a, std::unique_ptr<CMP_TREE_NODE> const& b )
81  { return Compare( *a, *b ) > 0; } );
82 
83  for( auto& node: Children )
84  {
85  node->SortNodes();
86  }
87 }
static int Compare(CMP_TREE_NODE const &aNode1, CMP_TREE_NODE const &aNode2)
Compare two nodes.
std::vector< std::unique_ptr< CMP_TREE_NODE > > Children
List of child nodes.
void CMP_TREE_NODE_ALIAS::UpdateScore ( EDA_COMBINED_MATCHER aMatcher)
overridevirtual

Perform the actual search.

Implements CMP_TREE_NODE.

Definition at line 181 of file cmp_tree_model.cpp.

References EDA_PATTERN_NOT_FOUND, EDA_COMBINED_MATCHER::Find(), EDA_COMBINED_MATCHER::GetPattern(), CMP_TREE_NODE::MatchName, matchPosScore(), CMP_TREE_NODE::Parent, CMP_TREE_NODE::Score, and CMP_TREE_NODE::SearchText.

182 {
183  if( Score <= 0 )
184  return; // Leaf nodes without scores are out of the game.
185 
186  // Keywords and description we only count if the match string is at
187  // least two characters long. That avoids spurious, low quality
188  // matches. Most abbreviations are at three characters long.
189  int found_pos = EDA_PATTERN_NOT_FOUND;
190  int matchers_fired = 0;
191 
192  if( aMatcher.GetPattern() == MatchName )
193  {
194  Score += 1000; // exact match. High score :)
195  }
196  else if( aMatcher.Find( MatchName, matchers_fired, found_pos ) )
197  {
198  // Substring match. The earlier in the string the better.
199  Score += matchPosScore( found_pos, 20 ) + 20;
200  }
201  else if( aMatcher.Find( Parent->MatchName, matchers_fired, found_pos ) )
202  {
203  Score += 19; // parent name matches. score += 19
204  }
205  else if( aMatcher.Find( SearchText, matchers_fired, found_pos ) )
206  {
207  // If we have a very short search term (like one or two letters),
208  // we don't want to accumulate scores if they just happen to be in
209  // keywords or description as almost any one or two-letter
210  // combination shows up in there.
211  if( aMatcher.GetPattern().length() >= 2 )
212  {
213  // For longer terms, we add scores 1..18 for positional match
214  // (higher in the front, where the keywords are).
215  Score += matchPosScore( found_pos, 17 ) + 1;
216  }
217  }
218  else
219  {
220  // No match. That's it for this item.
221  Score = 0;
222  }
223 
224  // More matchers = better match
225  Score += 2 * matchers_fired;
226 }
wxString const & GetPattern() const
bool Find(const wxString &aTerm, int &aMatchersTriggered, int &aPosition)
wxString MatchName
Normalized name for matching.
static const int EDA_PATTERN_NOT_FOUND
static int matchPosScore(int aPosition, int aMaximum)
CMP_TREE_NODE * Parent
Parent node or null.
wxString SearchText
Descriptive text to search.
int Score
The score of an item resulting from the search algorithm.

Member Data Documentation

LIB_ALIAS* CMP_TREE_NODE::Alias
inherited

Actual LIB_ALIAS*, or null.

Definition at line 101 of file cmp_tree_model.h.

Referenced by CMP_TREE_NODE_ALIAS(), and CMP_TREE_NODE_UNIT::CMP_TREE_NODE_UNIT().

wxString CMP_TREE_NODE::Desc
inherited

Description to be displayed.

Definition at line 97 of file cmp_tree_model.h.

Referenced by CMP_TREE_NODE_ALIAS(), CMP_TREE_NODE_UNIT::CMP_TREE_NODE_UNIT(), and CMP_TREE_MODEL_ADAPTER::WidthFor().

int CMP_TREE_NODE::IntrinsicRank
inherited

The rank of the item before any search terms are applied.

This is a fairly expensive sort (involving string compares) so it helps to store the result of that sort.

Definition at line 91 of file cmp_tree_model.h.

Referenced by CMP_TREE_NODE::AssignIntrinsicRanks(), CMP_TREE_NODE_UNIT::CMP_TREE_NODE_UNIT(), and CMP_TREE_NODE::Compare().

wxString CMP_TREE_NODE::MatchName
inherited
int CMP_TREE_NODE::Score
inherited
wxString CMP_TREE_NODE::SearchText
inherited

Descriptive text to search.

Definition at line 99 of file cmp_tree_model.h.

Referenced by CMP_TREE_NODE_ALIAS(), and UpdateScore().

int CMP_TREE_NODE::Unit
inherited

Actual unit, or zero.

Definition at line 102 of file cmp_tree_model.h.

Referenced by CMP_TREE_NODE_UNIT::CMP_TREE_NODE_UNIT(), and CMP_TREE_MODEL_ADAPTER::ShowPreselect().


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