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, const wxString &aLib) 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 bool 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_Footprint
 
wxString m_MatchName
 
wxString m_SearchText
 
bool m_Normalized
 
std::map< wxString, wxString > m_Fields
 
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 183 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 115 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 111 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 library 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 186 of file lib_tree_model.cpp.

187{
188 m_Type = LIBID;
189 m_Parent = aParent;
190
192 m_LibId.SetLibItemName( aItem->GetName() );
193
194 m_Name = aItem->GetName();
195 m_Desc = aItem->GetDescription();
196 m_Footprint = aItem->GetFootprint();
197
198 aItem->GetChooserFields( m_Fields );
199
200 m_MatchName = aItem->GetName();
201 m_SearchText = aItem->GetSearchText();
202 m_Normalized = false;
203
204 m_IsRoot = aItem->IsRoot();
205
206 if( aItem->GetUnitCount() > 1 )
207 {
208 for( int u = 1; u <= aItem->GetUnitCount(); ++u )
209 AddUnit( aItem, u );
210 }
211}
int SetLibItemName(const UTF8 &aLibItemName)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition: lib_id.cpp:109
int SetLibNickname(const UTF8 &aNickname)
Override the logical library name portion of the LIB_ID to aNickname.
Definition: lib_id.cpp:98
virtual wxString GetLibNickname() const =0
virtual wxString GetFootprint()
For items with footprint fields.
Definition: lib_tree_item.h:68
virtual bool IsRoot() const
For items having aliases, IsRoot() indicates the principal item.
Definition: lib_tree_item.h:63
virtual wxString GetName() const =0
virtual wxString GetDescription()=0
virtual wxString GetSearchText()
Definition: lib_tree_item.h:58
virtual int GetUnitCount() const
For items with units, return the number of units.
Definition: lib_tree_item.h:73
virtual void GetChooserFields(std::map< wxString, wxString > &aColumnMap)
Retrieves a key/value map of the fields on this item that should be exposed to the library browser/ch...
Definition: lib_tree_item.h:56
LIB_TREE_NODE_UNIT & AddUnit(LIB_TREE_ITEM *aItem, int aUnit)
Add a new unit to the component and return it.
enum TYPE m_Type
std::map< wxString, wxString > m_Fields
wxString m_SearchText
wxString m_Footprint
LIB_TREE_NODE * m_Parent
wxString m_MatchName

References AddUnit(), LIB_TREE_ITEM::GetChooserFields(), LIB_TREE_ITEM::GetDescription(), LIB_TREE_ITEM::GetFootprint(), 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_Fields, LIB_TREE_NODE::m_Footprint, 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 214 of file lib_tree_model.cpp.

215{
216 LIB_TREE_NODE_UNIT* unit = new LIB_TREE_NODE_UNIT( this, aItem, aUnit );
217 m_Children.push_back( std::unique_ptr<LIB_TREE_NODE>( unit ) );
218 return *unit;
219}
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( std::unique_ptr<LIB_TREE_NODE>& child: m_Children )
76 sort_buf.push_back( child.get() );
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}
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
int m_IntrinsicRank
The rank of the item before any search terms are applied.

References LIB_TREE_NODE::m_Children, and LIB_TREE_NODE::m_IntrinsicRank.

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

◆ Compare()

bool LIB_TREE_NODE::Compare ( LIB_TREE_NODE const &  aNode1,
LIB_TREE_NODE const &  aNode2 
)
staticinherited

Compare two nodes.

Returns true if aNode1 < aNode2.

Definition at line 103 of file lib_tree_model.cpp.

104{
105 if( aNode1.m_Type != aNode2.m_Type )
106 return aNode1.m_Type < aNode2.m_Type;
107
108 // Recently used sorts at top
109 if( aNode1.m_Name.StartsWith( wxT( "-- " ) ) )
110 {
111 if( aNode2.m_Name.StartsWith( wxT( "-- " ) ) )
112 {
113 return aNode1.m_IntrinsicRank > aNode2.m_IntrinsicRank;
114 }
115 else
116 {
117 return true;
118 }
119 }
120 else if( aNode2.m_Name.StartsWith( wxT( "-- " ) ) )
121 {
122 return false;
123 }
124
125 // Pinned nodes go next
126 if( aNode1.m_Pinned && !aNode2.m_Pinned )
127 return true;
128 else if( aNode2.m_Pinned && !aNode1.m_Pinned )
129 return false;
130
131 if( aNode1.m_IntrinsicRank != aNode2.m_IntrinsicRank )
132 return aNode1.m_IntrinsicRank > aNode2.m_IntrinsicRank;
133
134 return reinterpret_cast<const void*>( &aNode1 ) < reinterpret_cast<const void*>( &aNode2 );
135}

References LIB_TREE_NODE::m_IntrinsicRank, LIB_TREE_NODE::m_Name, LIB_TREE_NODE::m_Pinned, and LIB_TREE_NODE::m_Type.

◆ 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( std::unique_ptr<LIB_TREE_NODE>& child: m_Children )
56 child->ResetScore();
57
59}
static const unsigned kLowestDefaultScore

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 );
96 } );
97
98 for( std::unique_ptr<LIB_TREE_NODE>& node: m_Children )
99 node->SortNodes();
100}

References LIB_TREE_NODE::m_Children.

Referenced by LIB_TREE_MODEL_ADAPTER::resortTree(), and 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 222 of file lib_tree_model.cpp.

223{
225 m_LibId.SetLibItemName( aItem->GetName() );
226
227 m_Name = aItem->GetName();
228 m_Desc = aItem->GetDescription();
229 m_MatchName = aItem->GetName();
230
231 aItem->GetChooserFields( m_Fields );
232
233 m_SearchText = aItem->GetSearchText();
234 m_Normalized = false;
235
236 m_IsRoot = aItem->IsRoot();
237 m_Children.clear();
238
239 for( int u = 1; u <= aItem->GetUnitCount(); ++u )
240 AddUnit( aItem, u );
241}
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:87
virtual LIB_ID GetLibId() const =0

References AddUnit(), LIB_TREE_ITEM::GetChooserFields(), LIB_TREE_ITEM::GetDescription(), LIB_TREE_ITEM::GetLibId(), LIB_ID::GetLibNickname(), LIB_TREE_ITEM::GetName(), 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_Fields, 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_SearchText, LIB_ID::SetLibItemName(), and LIB_ID::SetLibNickname().

◆ UpdateScore()

void LIB_TREE_NODE_LIB_ID::UpdateScore ( EDA_COMBINED_MATCHER aMatcher,
const wxString &  aLib 
)
overridevirtual

Perform the actual search.

Implements LIB_TREE_NODE.

Definition at line 244 of file lib_tree_model.cpp.

245{
246 if( m_Score <= 0 )
247 return; // Leaf nodes without scores are out of the game.
248
249 if( !m_Normalized )
250 {
252 m_SearchText = m_SearchText.Lower();
253 m_Normalized = true;
254 }
255
256 if( !aLib.IsEmpty() && m_Parent->m_MatchName != aLib )
257 {
258 m_Score = 0;
259 return;
260 }
261
262 // Keywords and description we only count if the match string is at
263 // least two characters long. That avoids spurious, low quality
264 // matches. Most abbreviations are at three characters long.
265 int found_pos = EDA_PATTERN_NOT_FOUND;
266 int matchers_fired = 0;
267
268 if( aMatcher.GetPattern() == m_MatchName )
269 {
270 m_Score += 1000; // exact match. High score :)
271 }
272 else if( aMatcher.Find( m_MatchName, matchers_fired, found_pos ) )
273 {
274 // Substring match. The earlier in the string the better.
275 m_Score += matchPosScore( found_pos, 20 ) + 20;
276 }
277 else if( aMatcher.Find( m_Parent->m_MatchName, matchers_fired, found_pos ) )
278 {
279 m_Score += 19; // parent name matches. score += 19
280 }
281 else if( aMatcher.Find( m_SearchText, matchers_fired, found_pos ) )
282 {
283 // If we have a very short search term (like one or two letters),
284 // we don't want to accumulate scores if they just happen to be in
285 // keywords or description as almost any one or two-letter
286 // combination shows up in there.
287 if( aMatcher.GetPattern().length() >= 2 )
288 {
289 // For longer terms, we add scores 1..18 for positional match
290 // (higher in the front, where the keywords are).
291 m_Score += matchPosScore( found_pos, 17 ) + 1;
292 }
293 }
294 else
295 {
296 // No match. That's it for this item.
297 m_Score = 0;
298 }
299
300 // More matchers = better match
301 m_Score += 2 * matchers_fired;
302}
bool Find(const wxString &aTerm, int &aMatchersTriggered, int &aPosition)
const wxString & GetPattern() const
static const int EDA_PATTERN_NOT_FOUND
static int matchPosScore(int aPosition, int aMaximum)
wxString UnescapeString(const wxString &aSource)

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, matchPosScore(), and UnescapeString().

Member Data Documentation

◆ m_Children

◆ m_Desc

◆ m_Fields

std::map<wxString, wxString> LIB_TREE_NODE::m_Fields
inherited
See also
LIB_TREE_ITEMS::GetChooserFields

Definition at line 139 of file lib_tree_model.h.

Referenced by LIB_TREE_MODEL_ADAPTER::GetValue(), SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetValue(), LIB_TREE_NODE_LIB_ID(), and Update().

◆ m_Footprint

wxString LIB_TREE_NODE::m_Footprint
inherited

◆ 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 126 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 136 of file lib_tree_model.h.

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

◆ m_Parent

◆ m_Pinned

◆ m_Score

int LIB_TREE_NODE::m_Score
inherited

◆ m_SearchText

wxString LIB_TREE_NODE::m_SearchText
inherited

Definition at line 135 of file lib_tree_model.h.

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

◆ m_Type

◆ m_Unit


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