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_NODEm_Parent
 
PTR_VECTOR m_Children
 
enum TYPE m_Type
 
int m_IntrinsicRank
 The rank of the item before any search terms are applied. More...
 
int m_Score
 
bool m_Pinned
 
wxString m_Name
 
wxString m_Desc
 
wxString m_MatchName
 
wxString m_SearchText
 
bool m_Normalized
 
LIB_ID m_LibId
 
int m_Unit
 
bool m_IsRoot
 

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 182 of file lib_tree_model.h.

Member Typedef Documentation

◆ PTR_VECTOR

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

Definition at line 82 of file lib_tree_model.h.

Member Enumeration Documentation

◆ TYPE

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

Definition at line 78 of file lib_tree_model.h.

Constructor & Destructor Documentation

◆ LIB_TREE_NODE_LIB_ID() [1/2]

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() [2/2]

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 157 of file lib_tree_model.cpp.

158 {
159  m_Type = LIBID;
160  m_Parent = aParent;
161 
163  m_LibId.SetLibItemName( aItem->GetName () );
164 
165  m_Name = aItem->GetName();
166  m_Desc = aItem->GetDescription();
167 
168  m_MatchName = aItem->GetName();
169  m_SearchText = aItem->GetSearchText();
170  m_Normalized = false;
171 
172  m_IsRoot = aItem->IsRoot();
173 
174  if( aItem->GetUnitCount() > 1 )
175  {
176  for( int u = 1; u <= aItem->GetUnitCount(); ++u )
177  AddUnit( aItem, u );
178  }
179 }
virtual int GetUnitCount() const
For items with units, return the number of units.
Definition: lib_tree_item.h:63
virtual bool IsRoot() const
For items having aliases, IsRoot() indicates the principal item.
Definition: lib_tree_item.h:58
virtual wxString GetSearchText()
Definition: lib_tree_item.h:53
LIB_TREE_NODE * m_Parent
virtual wxString GetDescription()=0
LIB_TREE_NODE_UNIT & AddUnit(LIB_TREE_ITEM *aItem, int aUnit)
Add a new unit to the component and return it.
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
virtual wxString GetName() const =0
int SetLibNickname(const UTF8 &aNickname)
Override the logical library name portion of the LIB_ID to aNickname.
Definition: lib_id.cpp:193
virtual wxString GetLibNickname() const =0
enum TYPE m_Type
wxString m_Name
wxString m_Desc
wxString m_SearchText
wxString m_MatchName

References AddUnit(), 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::LIBID, LIB_TREE_NODE::m_Desc, LIB_TREE_NODE::m_IsRoot, LIB_TREE_NODE::m_LibId, LIB_TREE_NODE::m_MatchName, LIB_TREE_NODE::m_Name, LIB_TREE_NODE::m_Normalized, LIB_TREE_NODE::m_Parent, LIB_TREE_NODE::m_SearchText, LIB_TREE_NODE::m_Type, LIB_ID::SetLibItemName(), and LIB_ID::SetLibNickname().

Member Function Documentation

◆ AddUnit()

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 182 of file lib_tree_model.cpp.

183 {
184  LIB_TREE_NODE_UNIT* unit = new LIB_TREE_NODE_UNIT( this, aItem, aUnit );
185  m_Children.push_back( std::unique_ptr<LIB_TREE_NODE>( unit ) );
186  return *unit;
187 }
Node type: unit of component.
PTR_VECTOR m_Children

References LIB_TREE_NODE::m_Children.

Referenced by LIB_TREE_NODE_LIB_ID(), and Update().

◆ AssignIntrinsicRanks()

void LIB_TREE_NODE::AssignIntrinsicRanks ( bool  presorted = false)
inherited

Store intrinsic ranks on all children of this node.

See m_IntrinsicRank member doc for more information.

Definition at line 62 of file lib_tree_model.cpp.

63 {
64  std::vector<LIB_TREE_NODE*> sort_buf;
65 
66  if( presorted )
67  {
68  int max = m_Children.size() - 1;
69 
70  for( int i = 0; i <= max; ++i )
71  m_Children[i]->m_IntrinsicRank = max - i;
72  }
73  else
74  {
75  for( auto const& node: m_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  {
81  return StrNumCmp( a->m_Name, b->m_Name, true ) > 0;
82  } );
83 
84  for( int i = 0; i < (int) sort_buf.size(); ++i )
85  sort_buf[i]->m_IntrinsicRank = i;
86  }
87 }
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
Definition: string.cpp:409
int m_IntrinsicRank
The rank of the item before any search terms are applied.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
wxString m_Name
PTR_VECTOR m_Children

References LIB_TREE_NODE::m_Children, LIB_TREE_NODE::m_IntrinsicRank, LIB_TREE_NODE::m_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(), FP_TREE_SYNCHRONIZING_ADAPTER::updateLibrary(), and SYMBOL_TREE_SYNCHRONIZING_ADAPTER::updateLibrary().

◆ Compare()

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 103 of file lib_tree_model.cpp.

104 {
105  if( aNode1.m_Type != aNode2.m_Type )
106  return 0;
107 
108  if( aNode1.m_Score != aNode2.m_Score )
109  return aNode1.m_Score - aNode2.m_Score;
110 
111  if( aNode1.m_Parent != aNode2.m_Parent )
112  return 0;
113 
114  return aNode1.m_IntrinsicRank - aNode2.m_IntrinsicRank;
115 }

References LIB_TREE_NODE::m_IntrinsicRank, LIB_TREE_NODE::m_Parent, LIB_TREE_NODE::m_Score, and LIB_TREE_NODE::m_Type.

Referenced by LIB_TREE_NODE::SortNodes().

◆ operator=()

void LIB_TREE_NODE_LIB_ID::operator= ( LIB_TREE_NODE_LIB_ID const &  _)
delete

◆ ResetScore()

void LIB_TREE_NODE::ResetScore ( )
inherited

Initialize score to kLowestDefaultScore, recursively.

Definition at line 53 of file lib_tree_model.cpp.

54 {
55  for( auto& child: m_Children )
56  child->ResetScore();
57 
59 }
static const unsigned kLowestDefaultScore
PTR_VECTOR m_Children

References kLowestDefaultScore, LIB_TREE_NODE::m_Children, and LIB_TREE_NODE::m_Score.

Referenced by LIB_TREE_MODEL_ADAPTER::UpdateSearchString().

◆ SortNodes()

void LIB_TREE_NODE::SortNodes ( )
inherited

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

Definition at line 90 of file lib_tree_model.cpp.

91 {
92  std::sort( m_Children.begin(), m_Children.end(),
93  []( std::unique_ptr<LIB_TREE_NODE>& a, std::unique_ptr<LIB_TREE_NODE>& b )
94  {
95  return Compare( *a, *b ) > 0;
96  } );
97 
98  for( std::unique_ptr<LIB_TREE_NODE>& node: m_Children )
99  node->SortNodes();
100 }
static int Compare(LIB_TREE_NODE const &aNode1, LIB_TREE_NODE const &aNode2)
Compare two nodes.
PTR_VECTOR m_Children

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

Referenced by LIB_TREE_MODEL_ADAPTER::UpdateSearchString().

◆ Update()

void LIB_TREE_NODE_LIB_ID::Update ( LIB_TREE_ITEM aItem)

Update the node using data from a LIB_ALIAS object.

Definition at line 190 of file lib_tree_model.cpp.

191 {
192  // Update is called when the names match, so just update the other fields.
193 
195 
196  m_Desc = aItem->GetDescription();
197 
198  m_SearchText = aItem->GetSearchText();
199  m_Normalized = false;
200 
201  m_IsRoot = aItem->IsRoot();
202  m_Children.clear();
203 
204  for( int u = 1; u <= aItem->GetUnitCount(); ++u )
205  AddUnit( aItem, u );
206 }
virtual int GetUnitCount() const
For items with units, return the number of units.
Definition: lib_tree_item.h:63
virtual bool IsRoot() const
For items having aliases, IsRoot() indicates the principal item.
Definition: lib_tree_item.h:58
virtual wxString GetSearchText()
Definition: lib_tree_item.h:53
virtual wxString GetDescription()=0
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97
LIB_TREE_NODE_UNIT & AddUnit(LIB_TREE_ITEM *aItem, int aUnit)
Add a new unit to the component and return it.
int SetLibNickname(const UTF8 &aNickname)
Override the logical library name portion of the LIB_ID to aNickname.
Definition: lib_id.cpp:193
virtual LIB_ID GetLibId() const =0
PTR_VECTOR m_Children
wxString m_Desc
wxString m_SearchText

References AddUnit(), 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::m_Children, LIB_TREE_NODE::m_Desc, LIB_TREE_NODE::m_IsRoot, LIB_TREE_NODE::m_LibId, LIB_TREE_NODE::m_Normalized, LIB_TREE_NODE::m_SearchText, and LIB_ID::SetLibNickname().

◆ UpdateScore()

void LIB_TREE_NODE_LIB_ID::UpdateScore ( EDA_COMBINED_MATCHER aMatcher)
overridevirtual

Perform the actual search.

Implements LIB_TREE_NODE.

Definition at line 209 of file lib_tree_model.cpp.

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

References EDA_PATTERN_NOT_FOUND, EDA_COMBINED_MATCHER::Find(), EDA_COMBINED_MATCHER::GetPattern(), LIB_TREE_NODE::m_MatchName, LIB_TREE_NODE::m_Normalized, LIB_TREE_NODE::m_Parent, LIB_TREE_NODE::m_Score, LIB_TREE_NODE::m_SearchText, and matchPosScore().

Member Data Documentation

◆ m_Children

◆ m_Desc

◆ m_IntrinsicRank

int LIB_TREE_NODE::m_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 93 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().

◆ m_IsRoot

bool LIB_TREE_NODE::m_IsRoot
inherited

◆ m_LibId

◆ m_MatchName

◆ m_Name

◆ m_Normalized

bool LIB_TREE_NODE::m_Normalized
inherited

Definition at line 102 of file lib_tree_model.h.

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

◆ m_Parent

◆ m_Pinned

◆ m_Score

◆ m_SearchText

wxString LIB_TREE_NODE::m_SearchText
inherited

Definition at line 101 of file lib_tree_model.h.

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

◆ m_Type

◆ m_Unit

int LIB_TREE_NODE::m_Unit
inherited

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