KiCad PCB EDA Suite
LIB_TREE_NODE_LIB_ID Class Reference

Node type: LIB_ID. More...

#include <lib_tree_model.h>

Inheritance diagram for LIB_TREE_NODE_LIB_ID:
LIB_TREE_NODE

Public Types

enum  TYPE {
  ROOT, LIB, LIBID, UNIT,
  INVALID
}
 
typedef std::vector< std::unique_ptr< LIB_TREE_NODE > > PTR_VECTOR
 

Public Member Functions

 LIB_TREE_NODE_LIB_ID (LIB_TREE_NODE_LIB_ID 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= (LIB_TREE_NODE_LIB_ID const &_)=delete
 
 LIB_TREE_NODE_LIB_ID (LIB_TREE_NODE *aParent, LIB_TREE_ITEM *aItem)
 Construct a LIB_ID node. More...
 
void Update (LIB_TREE_ITEM *aItem)
 Update the node using data from a LIB_ALIAS object. More...
 
virtual void UpdateScore (EDA_COMBINED_MATCHER &aMatcher) override
 Perform the actual search. More...
 
void ResetScore ()
 Initialize score to kLowestDefaultScore, recursively. More...
 
void AssignIntrinsicRanks (bool presorted=false)
 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 (LIB_TREE_NODE const &aNode1, LIB_TREE_NODE const &aNode2)
 Compare two nodes. More...
 

Public Attributes

LIB_TREE_NODEParent
 Parent node or null. More...
 
PTR_VECTOR 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...
 
bool Normalized
 Support for lazy normalization. More...
 
LIB_ID LibId
 LIB_ID determined by the parent library nickname and alias name. More...
 
int Unit
 Actual unit, or zero. More...
 
bool IsRoot
 Indicates if the symbol is a root symbol instead of an alias. More...
 

Protected Member Functions

LIB_TREE_NODE_UNITAddUnit (LIB_TREE_ITEM *aItem, int aUnit)
 Add a new unit to the component and return it. More...
 

Detailed Description

Node type: LIB_ID.

Definition at line 181 of file lib_tree_model.h.

Member Typedef Documentation

typedef std::vector<std::unique_ptr<LIB_TREE_NODE> > LIB_TREE_NODE::PTR_VECTOR
inherited

Definition at line 81 of file lib_tree_model.h.

Member Enumeration Documentation

enum LIB_TREE_NODE::TYPE
inherited
Enumerator
ROOT 
LIB 
LIBID 
UNIT 
INVALID 

Definition at line 77 of file lib_tree_model.h.

Constructor & Destructor Documentation

LIB_TREE_NODE_LIB_ID::LIB_TREE_NODE_LIB_ID ( LIB_TREE_NODE_LIB_ID const &  _)
delete

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

LIB_TREE_NODE_LIB_ID::LIB_TREE_NODE_LIB_ID ( LIB_TREE_NODE aParent,
LIB_TREE_ITEM aItem 
)

Construct a LIB_ID node.

All fields will be populated from the LIB_ALIAS, including children (unit nodes will be generated automatically). This does not keep the pointer to the LIB_ALIAS object because at any time, a LIB_ALIAS can be remove from a libray which will result in an invalid pointer. The alias must be resolved at the time of use. Anything else is a bug.

Parameters
aParentparent node, should be a CMP_TREE_NODE_LIB
aItemLIB_COMPONENT to populate the node.

Definition at line 154 of file lib_tree_model.cpp.

References LIB_TREE_NODE::Desc, LIB_TREE_ITEM::GetDescription(), LIB_TREE_ITEM::GetLibNickname(), LIB_TREE_ITEM::GetName(), LIB_TREE_ITEM::GetSearchText(), LIB_TREE_ITEM::GetUnitCount(), LIB_TREE_ITEM::IsRoot(), LIB_TREE_NODE::IsRoot, LIB_TREE_NODE::LIBID, LIB_TREE_NODE::LibId, LIB_TREE_NODE::MatchName, LIB_TREE_NODE::Name, LIB_TREE_NODE::Normalized, LIB_TREE_NODE::Parent, LIB_TREE_NODE::SearchText, LIB_ID::SetLibItemName(), LIB_ID::SetLibNickname(), and LIB_TREE_NODE::Type.

155 {
156  Type = LIBID;
157  Parent = aParent;
158 
159  LibId.SetLibNickname( aItem->GetLibNickname() );
160  LibId.SetLibItemName( aItem->GetName () );
161 
162  Name = aItem->GetName();
163  Desc = aItem->GetDescription();
164 
165  MatchName = aItem->GetName();
166  SearchText = aItem->GetSearchText();
167  Normalized = false;
168 
169  IsRoot = aItem->IsRoot();
170 
171  if( aItem->GetUnitCount() > 1 )
172  {
173  for( int u = 1; u <= aItem->GetUnitCount(); ++u )
174  AddUnit( aItem, u );
175  }
176 }
enum TYPE Type
Node type.
bool Normalized
Support for lazy normalization.
LIB_TREE_NODE * Parent
Parent node or null.
virtual wxString GetSearchText()
Definition: lib_tree_item.h:49
wxString MatchName
Normalized name for matching.
LIB_ID LibId
LIB_ID determined by the parent library nickname and alias name.
LIB_TREE_NODE_UNIT & AddUnit(LIB_TREE_ITEM *aItem, int aUnit)
Add a new unit to the component and return it.
virtual const wxString & GetDescription()=0
wxString Desc
Description to be displayed.
int SetLibItemName(const UTF8 &aLibItemName, bool aTestForRev=true)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition: lib_id.cpp:206
wxString SearchText
Descriptive text to search.
int SetLibNickname(const UTF8 &aNickname)
Override the logical library name portion of the LIB_ID to aNickname.
Definition: lib_id.cpp:193
virtual int GetUnitCount()
For items with units, return the number of units.
Definition: lib_tree_item.h:59
virtual const wxString & GetName() const =0
wxString Name
Actual name of the part.
bool IsRoot
Indicates if the symbol is a root symbol instead of an alias.
virtual wxString GetLibNickname() const =0
virtual bool IsRoot() const
For items having aliases, IsRoot() indicates the principal item.
Definition: lib_tree_item.h:54

Member Function Documentation

LIB_TREE_NODE_UNIT & LIB_TREE_NODE_LIB_ID::AddUnit ( LIB_TREE_ITEM aItem,
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 179 of file lib_tree_model.cpp.

References LIB_TREE_NODE::Children.

180 {
181  LIB_TREE_NODE_UNIT* unit = new LIB_TREE_NODE_UNIT( this, aItem, aUnit );
182  Children.push_back( std::unique_ptr<LIB_TREE_NODE>( unit ) );
183  return *unit;
184 }
PTR_VECTOR Children
List of child nodes.
Node type: unit of component.
void LIB_TREE_NODE::AssignIntrinsicRanks ( bool  presorted = false)
inherited

Store intrinsic ranks on all children of this node.

See IntrinsicRank member doc for more information.

Definition at line 62 of file lib_tree_model.cpp.

References LIB_TREE_NODE::Children, i, LIB_TREE_NODE::IntrinsicRank, max, LIB_TREE_NODE::Name, and StrNumCmp().

Referenced by FP_TREE_MODEL_ADAPTER::AddLibraries(), SYMBOL_TREE_MODEL_ADAPTER::AddLibraries(), LIB_TREE_MODEL_ADAPTER::AssignIntrinsicRanks(), LIB_TREE_MODEL_ADAPTER::DoAddLibrary(), FP_TREE_SYNCHRONIZING_ADAPTER::Sync(), SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Sync(), SYMBOL_TREE_SYNCHRONIZING_ADAPTER::updateLibrary(), and FP_TREE_SYNCHRONIZING_ADAPTER::updateLibrary().

63 {
64  std::vector<LIB_TREE_NODE*> sort_buf;
65 
66  if( presorted )
67  {
68  int max = Children.size() - 1;
69 
70  for( int i = 0; i <= max; ++i )
71  Children[i]->IntrinsicRank = max - i;
72  }
73  else
74  {
75  for( auto const& node: Children )
76  sort_buf.push_back( &*node );
77 
78  std::sort( sort_buf.begin(), sort_buf.end(),
79  []( LIB_TREE_NODE* a, LIB_TREE_NODE* b ) -> bool
80  { return StrNumCmp( a->Name, b->Name, INT_MAX, true ) > 0; } );
81 
82  for( int i = 0; i < (int) sort_buf.size(); ++i )
83  sort_buf[i]->IntrinsicRank = i;
84  }
85 }
PTR_VECTOR Children
List of child nodes.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
wxString Name
Actual name of the part.
#define max(a, b)
Definition: auxiliary.h:86
size_t i
Definition: json11.cpp:597
int IntrinsicRank
The rank of the item before any search terms are applied.
int StrNumCmp(const wxString &aString1, const wxString &aString2, int aLength, bool aIgnoreCase)
Function StrNumCmp is a routine compatible with qsort() to sort by alphabetical order.
Definition: string.cpp:315
int LIB_TREE_NODE::Compare ( LIB_TREE_NODE const &  aNode1,
LIB_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 101 of file lib_tree_model.cpp.

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

Referenced by LIB_TREE_NODE::SortNodes().

102 {
103  if( aNode1.Type != aNode2.Type )
104  return 0;
105 
106  if( aNode1.Score != aNode2.Score )
107  return aNode1.Score - aNode2.Score;
108 
109  if( aNode1.Parent != aNode2.Parent )
110  return 0;
111 
112  return aNode1.IntrinsicRank - aNode2.IntrinsicRank;
113 }
void LIB_TREE_NODE_LIB_ID::operator= ( LIB_TREE_NODE_LIB_ID const &  _)
delete
void LIB_TREE_NODE::ResetScore ( )
inherited

Initialize score to kLowestDefaultScore, recursively.

Definition at line 53 of file lib_tree_model.cpp.

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

Referenced by LIB_TREE_MODEL_ADAPTER::UpdateSearchString().

54 {
55  for( auto& child: Children )
56  child->ResetScore();
57 
59 }
PTR_VECTOR Children
List of child nodes.
int Score
The score of an item resulting from the search algorithm.
static const unsigned kLowestDefaultScore
void LIB_TREE_NODE::SortNodes ( )
inherited

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

Definition at line 88 of file lib_tree_model.cpp.

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

Referenced by LIB_TREE_MODEL_ADAPTER::UpdateSearchString().

89 {
90  std::sort( Children.begin(), Children.end(),
91  []( std::unique_ptr<LIB_TREE_NODE> const& a, std::unique_ptr<LIB_TREE_NODE> const& b )
92  { return Compare( *a, *b ) > 0; } );
93 
94  for( auto& node: Children )
95  {
96  node->SortNodes();
97  }
98 }
PTR_VECTOR Children
List of child nodes.
static int Compare(LIB_TREE_NODE const &aNode1, LIB_TREE_NODE const &aNode2)
Compare two nodes.
void LIB_TREE_NODE_LIB_ID::Update ( LIB_TREE_ITEM aItem)

Update the node using data from a LIB_ALIAS object.

Definition at line 187 of file lib_tree_model.cpp.

References LIB_TREE_NODE::Children, LIB_TREE_NODE::Desc, LIB_TREE_ITEM::GetDescription(), LIB_TREE_ITEM::GetLibId(), LIB_ID::GetLibNickname(), LIB_TREE_ITEM::GetSearchText(), LIB_TREE_ITEM::GetUnitCount(), LIB_TREE_ITEM::IsRoot(), LIB_TREE_NODE::IsRoot, LIB_TREE_NODE::LibId, LIB_TREE_NODE::Normalized, LIB_TREE_NODE::SearchText, and LIB_ID::SetLibNickname().

188 {
189  // Update is called when the names match, so just update the other fields.
190 
192 
193  Desc = aItem->GetDescription();
194 
195  SearchText = aItem->GetSearchText();
196  Normalized = false;
197 
198  IsRoot = aItem->IsRoot();
199  Children.clear();
200 
201  for( int u = 1; u <= aItem->GetUnitCount(); ++u )
202  AddUnit( aItem, u );
203 }
PTR_VECTOR Children
List of child nodes.
bool Normalized
Support for lazy normalization.
virtual wxString GetSearchText()
Definition: lib_tree_item.h:49
LIB_ID LibId
LIB_ID determined by the parent library nickname and alias name.
LIB_TREE_NODE_UNIT & AddUnit(LIB_TREE_ITEM *aItem, int aUnit)
Add a new unit to the component and return it.
virtual const wxString & GetDescription()=0
wxString Desc
Description to be displayed.
wxString SearchText
Descriptive text to search.
int SetLibNickname(const UTF8 &aNickname)
Override the logical library name portion of the LIB_ID to aNickname.
Definition: lib_id.cpp:193
virtual int GetUnitCount()
For items with units, return the number of units.
Definition: lib_tree_item.h:59
bool IsRoot
Indicates if the symbol is a root symbol instead of an alias.
virtual LIB_ID GetLibId() const =0
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97
virtual bool IsRoot() const
For items having aliases, IsRoot() indicates the principal item.
Definition: lib_tree_item.h:54
void LIB_TREE_NODE_LIB_ID::UpdateScore ( EDA_COMBINED_MATCHER aMatcher)
overridevirtual

Perform the actual search.

Implements LIB_TREE_NODE.

Definition at line 206 of file lib_tree_model.cpp.

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

207 {
208  if( Score <= 0 )
209  return; // Leaf nodes without scores are out of the game.
210 
211  if( !Normalized )
212  {
213  MatchName = MatchName.Lower();
214  SearchText = SearchText.Lower();
215  Normalized = true;
216  }
217 
218  // Keywords and description we only count if the match string is at
219  // least two characters long. That avoids spurious, low quality
220  // matches. Most abbreviations are at three characters long.
221  int found_pos = EDA_PATTERN_NOT_FOUND;
222  int matchers_fired = 0;
223 
224  if( aMatcher.GetPattern() == MatchName )
225  {
226  Score += 1000; // exact match. High score :)
227  }
228  else if( aMatcher.Find( MatchName, matchers_fired, found_pos ) )
229  {
230  // Substring match. The earlier in the string the better.
231  Score += matchPosScore( found_pos, 20 ) + 20;
232  }
233  else if( aMatcher.Find( Parent->MatchName, matchers_fired, found_pos ) )
234  {
235  Score += 19; // parent name matches. score += 19
236  }
237  else if( aMatcher.Find( SearchText, matchers_fired, found_pos ) )
238  {
239  // If we have a very short search term (like one or two letters),
240  // we don't want to accumulate scores if they just happen to be in
241  // keywords or description as almost any one or two-letter
242  // combination shows up in there.
243  if( aMatcher.GetPattern().length() >= 2 )
244  {
245  // For longer terms, we add scores 1..18 for positional match
246  // (higher in the front, where the keywords are).
247  Score += matchPosScore( found_pos, 17 ) + 1;
248  }
249  }
250  else
251  {
252  // No match. That's it for this item.
253  Score = 0;
254  }
255 
256  // More matchers = better match
257  Score += 2 * matchers_fired;
258 }
wxString const & GetPattern() const
static int matchPosScore(int aPosition, int aMaximum)
bool Normalized
Support for lazy normalization.
LIB_TREE_NODE * Parent
Parent node or null.
int Score
The score of an item resulting from the search algorithm.
wxString MatchName
Normalized name for matching.
bool Find(const wxString &aTerm, int &aMatchersTriggered, int &aPosition)
static const int EDA_PATTERN_NOT_FOUND
wxString SearchText
Descriptive text to search.

Member Data Documentation

wxString LIB_TREE_NODE::Desc
inherited

Description to be displayed.

Definition at line 98 of file lib_tree_model.h.

Referenced by LIB_TREE_NODE_LIB::LIB_TREE_NODE_LIB(), LIB_TREE_NODE_LIB_ID(), LIB_TREE_NODE_UNIT::LIB_TREE_NODE_UNIT(), and Update().

int LIB_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 92 of file lib_tree_model.h.

Referenced by LIB_TREE_NODE::AssignIntrinsicRanks(), LIB_TREE_NODE::Compare(), and LIB_TREE_NODE_UNIT::LIB_TREE_NODE_UNIT().

bool LIB_TREE_NODE::IsRoot
inherited

Indicates if the symbol is a root symbol instead of an alias.

Definition at line 106 of file lib_tree_model.h.

Referenced by LIB_TREE_NODE_LIB_ID(), and Update().

LIB_ID LIB_TREE_NODE::LibId
inherited

LIB_ID determined by the parent library nickname and alias name.

Definition at line 104 of file lib_tree_model.h.

Referenced by LIB_TREE_NODE_LIB::LIB_TREE_NODE_LIB(), LIB_TREE_NODE_LIB_ID(), LIB_TREE_NODE_UNIT::LIB_TREE_NODE_UNIT(), LIB_TREE_MODEL_ADAPTER::ShowPreselect(), and Update().

wxString LIB_TREE_NODE::MatchName
inherited
bool LIB_TREE_NODE::Normalized
inherited

Support for lazy normalization.

Definition at line 101 of file lib_tree_model.h.

Referenced by LIB_TREE_NODE_LIB_ID(), Update(), and UpdateScore().

int LIB_TREE_NODE::Score
inherited

The score of an item resulting from the search algorithm.

Definition at line 95 of file lib_tree_model.h.

Referenced by LIB_TREE_NODE::Compare(), LIB_TREE_NODE::ResetScore(), LIB_TREE_MODEL_ADAPTER::ShowResults(), UpdateScore(), and LIB_TREE_NODE_LIB::UpdateScore().

wxString LIB_TREE_NODE::SearchText
inherited

Descriptive text to search.

Definition at line 100 of file lib_tree_model.h.

Referenced by LIB_TREE_NODE_LIB_ID(), Update(), and UpdateScore().

int LIB_TREE_NODE::Unit
inherited

Actual unit, or zero.

Definition at line 105 of file lib_tree_model.h.

Referenced by LIB_TREE_NODE_UNIT::LIB_TREE_NODE_UNIT(), and LIB_TREE_MODEL_ADAPTER::ShowPreselect().


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