KiCad PCB EDA Suite
Loading...
Searching...
No Matches
lib_tree_model.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2017 Chris Pavlina <[email protected]>
5 * Copyright (C) 2014 Henner Zeller <[email protected]>
6 * Copyright (C) 2023 CERN
7 * Copyright (C) 2014-2023 KiCad Developers, see AUTHORS.txt for contributors.
8 *
9 * This program is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include <lib_tree_model.h>
24
25#include <algorithm>
26#include <eda_pattern_match.h>
27#include <lib_tree_item.h>
28#include <pgm_base.h>
29#include <string_utils.h>
30
31
32
34{
35 for( std::unique_ptr<LIB_TREE_NODE>& child: m_Children )
36 child->ResetScore();
37
38 m_Score = 0;
39}
40
41
43{
44 std::vector<LIB_TREE_NODE*> sort_buf;
45
46 if( presorted )
47 {
48 int max = m_Children.size() - 1;
49
50 for( int i = 0; i <= max; ++i )
51 m_Children[i]->m_IntrinsicRank = max - i;
52 }
53 else
54 {
55 for( std::unique_ptr<LIB_TREE_NODE>& child: m_Children )
56 sort_buf.push_back( child.get() );
57
58 std::sort( sort_buf.begin(), sort_buf.end(),
59 []( LIB_TREE_NODE* a, LIB_TREE_NODE* b ) -> bool
60 {
61 return StrNumCmp( a->m_Name, b->m_Name, true ) > 0;
62 } );
63
64 for( int i = 0; i < (int) sort_buf.size(); ++i )
65 sort_buf[i]->m_IntrinsicRank = i;
66 }
67}
68
69
70void LIB_TREE_NODE::SortNodes( bool aUseScores )
71{
72 std::sort( m_Children.begin(), m_Children.end(),
73 [&]( std::unique_ptr<LIB_TREE_NODE>& a, std::unique_ptr<LIB_TREE_NODE>& b )
74 {
75 return Compare( *a, *b, aUseScores );
76 } );
77
78 for( std::unique_ptr<LIB_TREE_NODE>& node: m_Children )
79 node->SortNodes( aUseScores );
80}
81
82
83bool LIB_TREE_NODE::Compare( LIB_TREE_NODE const& aNode1, LIB_TREE_NODE const& aNode2,
84 bool aUseScores )
85{
86 if( aNode1.m_Type != aNode2.m_Type )
87 return aNode1.m_Type < aNode2.m_Type;
88
89 // Recently used sorts at top
90 if( aNode1.m_Name.StartsWith( wxT( "-- " ) ) )
91 {
92 if( aNode2.m_Name.StartsWith( wxT( "-- " ) ) )
93 {
94 // Make sure -- Recently Used is always at the top
95 if( aNode1.m_Name.StartsWith( wxT( "-- Recently Used" ) ) )
96 return true;
97 else if( aNode2.m_Name.StartsWith( wxT( "-- Recently Used" ) ) )
98 return false;
99
100 return aNode1.m_IntrinsicRank > aNode2.m_IntrinsicRank;
101 }
102 else
103 {
104 return true;
105 }
106 }
107 else if( aNode2.m_Name.StartsWith( wxT( "-- " ) ) )
108 {
109 return false;
110 }
111
112 // Pinned nodes go next
113 if( aNode1.m_Pinned && !aNode2.m_Pinned )
114 return true;
115 else if( aNode2.m_Pinned && !aNode1.m_Pinned )
116 return false;
117
118 if( aUseScores && aNode1.m_Score != aNode2.m_Score )
119 return aNode1.m_Score > aNode2.m_Score;
120
121 if( aNode1.m_IntrinsicRank != aNode2.m_IntrinsicRank )
122 return aNode1.m_IntrinsicRank > aNode2.m_IntrinsicRank;
123
124 return reinterpret_cast<const void*>( &aNode1 ) < reinterpret_cast<const void*>( &aNode2 );
125}
126
127
129 : m_Parent( nullptr ),
130 m_Type( INVALID ),
131 m_IntrinsicRank( 0 ),
132 m_Score( 0 ),
133 m_Pinned( false ),
134 m_PinCount( 0 ),
135 m_Unit( 0 ),
136 m_IsRoot( false )
137{}
138
139
141{
142 static void* locale = nullptr;
143 static wxString namePrefix;
144
145 // Fetching translations can take a surprising amount of time when loading libraries,
146 // so only do it when necessary.
147 if( Pgm().GetLocale() != locale )
148 {
149 namePrefix = _( "Unit" );
150 locale = Pgm().GetLocale();
151 }
152
153 m_Parent = aParent;
154 m_Type = UNIT;
155
156 m_Unit = aUnit;
157 m_LibId = aParent->m_LibId;
158
159 m_Name = namePrefix + " " + aItem->GetUnitReference( aUnit );
160
161 if( aItem->HasUnitDisplayName( aUnit ) )
162 m_Desc = aItem->GetUnitDisplayName( aUnit );
163 else
164 m_Desc = wxEmptyString;
165
166 m_IntrinsicRank = -aUnit;
167}
168
169
170void LIB_TREE_NODE_UNIT::UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
171 std::function<bool( LIB_TREE_NODE& aNode )>* aFilter )
172{
173 // aMatcher test results are inherited from parent
174 if( aMatcher )
176
177 // aFilter test is subtractive
178 if( aFilter && !(*aFilter)(*this) )
179 m_Score = 0;
180
181 // show all nodes if no search/filter/etc. criteria are given
182 if( !aMatcher && aLib.IsEmpty() && ( !aFilter || (*aFilter)(*this) ) )
183 m_Score = 1;
184}
185
186
188{
189 m_Type = ITEM;
190 m_Parent = aParent;
191
193 m_LibId.SetLibItemName( aItem->GetName() );
194
195 m_Name = aItem->GetName();
196 m_Desc = aItem->GetDescription();
197 m_Footprint = aItem->GetFootprint();
198 m_PinCount = aItem->GetPinCount();
199
200 aItem->GetChooserFields( m_Fields );
201
202 m_SearchTerms = aItem->GetSearchTerms();
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}
212
213
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}
220
221
223{
225 m_LibId.SetLibItemName( aItem->GetName() );
226
227 m_Name = aItem->GetName();
228 m_Desc = aItem->GetDescription();
229
230 aItem->GetChooserFields( m_Fields );
231
232 m_SearchTerms = aItem->GetSearchTerms();
233
234 m_IsRoot = aItem->IsRoot();
235 m_Children.clear();
236
237 for( int u = 1; u <= aItem->GetUnitCount(); ++u )
238 AddUnit( aItem, u );
239}
240
241
242void LIB_TREE_NODE_ITEM::UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
243 std::function<bool( LIB_TREE_NODE& aNode )>* aFilter )
244{
245 // aMatcher test is additive, but if we don't match the given term at all, it nulls out
246 if( aMatcher )
247 {
248 int currentScore = aMatcher->ScoreTerms( m_SearchTerms );
249
250 // This is a hack: the second phase of search in the adapter will look for a tokenized
251 // LIB_ID and send the lib part down here. While we generally want to prune ourselves
252 // out here (by setting score to -1) the first time we fail to match a search term,
253 // we want to give the same search term a second chance if it has been split from a library
254 // name.
255 if( ( m_Score >= 0 || !aLib.IsEmpty() ) && currentScore > 0 )
256 m_Score += currentScore;
257 else
258 m_Score = -1; // Item has failed to match this term, rule it out
259 }
260
261 // aFilter test is subtractive
262 if( aFilter && !(*aFilter)(*this) )
263 m_Score = 0;
264
265 // show all nodes if no search/filter/etc. criteria are given
266 if( !aMatcher && aLib.IsEmpty() && ( !aFilter || (*aFilter)(*this) ) )
267 m_Score = 1;
268
269 for( std::unique_ptr<LIB_TREE_NODE>& child: m_Children )
270 child->UpdateScore( aMatcher, aLib, aFilter );
271}
272
273
275 wxString const& aDesc )
276{
277 m_Type = LIBRARY;
278 m_Name = aName;
279 m_Desc = aDesc;
280 m_Parent = aParent;
281 m_LibId.SetLibNickname( aName );
282
283 m_SearchTerms.emplace_back( SEARCH_TERM( aName, 8 ) );
284}
285
286
288{
289 LIB_TREE_NODE_ITEM* item = new LIB_TREE_NODE_ITEM( this, aItem );
290 m_Children.push_back( std::unique_ptr<LIB_TREE_NODE>( item ) );
291 return *item;
292}
293
294
295void LIB_TREE_NODE_LIBRARY::UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
296 std::function<bool( LIB_TREE_NODE& aNode )>* aFilter )
297{
298 int newScore = 0;
299
300 for( std::unique_ptr<LIB_TREE_NODE>& child: m_Children )
301 {
302 child->UpdateScore( aMatcher, aLib, aFilter );
303 newScore = std::max( newScore, child->m_Score );
304 }
305
306 // Each time UpdateScore is called for a library, child (item) scores may go up or down.
307 // If the all go down to zero, we need to make sure to drop the library from the list.
308 if( newScore > 0 )
309 m_Score = std::max( m_Score, newScore );
310 else
311 m_Score = 0;
312
313 // aLib test is additive, but only when we've already accumulated some score from children
314 if( !aLib.IsEmpty()
315 && m_Name.Lower().Matches( aLib )
316 && ( m_Score > 0 || m_Children.empty() ) )
317 {
318 m_Score += 1;
319 }
320
321 // aMatcher test is additive
322 if( aMatcher )
323 m_Score += aMatcher->ScoreTerms( m_SearchTerms );
324
325 // show all nodes if no search/filter/etc. criteria are given
326 if( m_Children.empty() && !aMatcher && aLib.IsEmpty() && ( !aFilter || (*aFilter)(*this) ) )
327 m_Score = 1;
328}
329
330
332{
333 m_Type = ROOT;
334}
335
336
337LIB_TREE_NODE_LIBRARY& LIB_TREE_NODE_ROOT::AddLib( wxString const& aName, wxString const& aDesc )
338{
339 LIB_TREE_NODE_LIBRARY* lib = new LIB_TREE_NODE_LIBRARY( this, aName, aDesc );
340 m_Children.push_back( std::unique_ptr<LIB_TREE_NODE>( lib ) );
341 return *lib;
342}
343
344
345void LIB_TREE_NODE_ROOT::UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
346 std::function<bool( LIB_TREE_NODE& aNode )>* aFilter )
347{
348 for( std::unique_ptr<LIB_TREE_NODE>& child: m_Children )
349 child->UpdateScore( aMatcher, aLib, aFilter );
350}
351
int ScoreTerms(std::vector< SEARCH_TERM > &aWeightedTerms)
int SetLibItemName(const UTF8 &aLibItemName)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition: lib_id.cpp:110
int SetLibNickname(const UTF8 &aLibNickname)
Override the logical library name portion of the LIB_ID to aLibNickname.
Definition: lib_id.cpp:99
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:87
A mix-in to provide polymorphism between items stored in libraries (symbols, aliases and footprints).
Definition: lib_tree_item.h:41
virtual wxString GetUnitReference(int aUnit)
For items with units, return an identifier for unit x.
Definition: lib_tree_item.h:84
virtual wxString GetLibNickname() const =0
virtual std::vector< SEARCH_TERM > GetSearchTerms()
Definition: lib_tree_item.h:59
virtual bool HasUnitDisplayName(int aUnit)
For items with units, return true if a display name is set for x.
Definition: lib_tree_item.h:94
virtual wxString GetFootprint()
For items with footprint fields.
Definition: lib_tree_item.h:69
virtual bool IsRoot() const
For items having aliases, IsRoot() indicates the principal item.
Definition: lib_tree_item.h:64
virtual wxString GetName() const =0
virtual wxString GetDescription()=0
virtual LIB_ID GetLibId() const =0
virtual int GetUnitCount() const
For items with units, return the number of units.
Definition: lib_tree_item.h:79
virtual int GetPinCount()
The pin count for symbols or the unique pad count for footprints.
Definition: lib_tree_item.h:74
virtual wxString GetUnitDisplayName(int aUnit)
For items with units, return a display name for unit x.
Definition: lib_tree_item.h:89
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:57
Node type: LIB_ID.
void UpdateScore(EDA_COMBINED_MATCHER *aMatcher, const wxString &aLib, std::function< bool(LIB_TREE_NODE &aNode)> *aFilter) override
Perform the actual search.
LIB_TREE_NODE_ITEM(LIB_TREE_NODE_ITEM const &_)=delete
The addresses of CMP_TREE_NODEs are used as unique IDs for the wxDataViewModel, so don't let them be ...
LIB_TREE_NODE_UNIT & AddUnit(LIB_TREE_ITEM *aItem, int aUnit)
Add a new unit to the component and return it.
void Update(LIB_TREE_ITEM *aItem)
Update the node using data from a LIB_ALIAS object.
Node type: library.
LIB_TREE_NODE_LIBRARY(LIB_TREE_NODE_LIBRARY const &_)=delete
The addresses of CMP_TREE_NODEs are used as unique IDs for the wxDataViewModel, so don't let them be ...
LIB_TREE_NODE_ITEM & AddItem(LIB_TREE_ITEM *aItem)
Construct a new alias node, add it to this library, and return it.
void UpdateScore(EDA_COMBINED_MATCHER *aMatcher, const wxString &aLib, std::function< bool(LIB_TREE_NODE &aNode)> *aFilter) override
Update the score for this part.
LIB_TREE_NODE_LIBRARY & AddLib(wxString const &aName, wxString const &aDesc)
Construct an empty library node, add it to the root, and return it.
void UpdateScore(EDA_COMBINED_MATCHER *aMatcher, const wxString &aLib, std::function< bool(LIB_TREE_NODE &aNode)> *aFilter) override
Update the score for this part.
LIB_TREE_NODE_ROOT()
Construct the root node.
Node type: unit of component.
LIB_TREE_NODE_UNIT(LIB_TREE_NODE_UNIT const &_)=delete
The addresses of CMP_TREE_NODEs are used as unique IDs for the wxDataViewModel, so don't let them be ...
void UpdateScore(EDA_COMBINED_MATCHER *aMatcher, const wxString &aLib, std::function< bool(LIB_TREE_NODE &aNode)> *aFilter) override
Update the score for this part.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
void SortNodes(bool aUseScores)
Sort child nodes quickly and recursively (IntrinsicRanks must have been set).
static bool Compare(LIB_TREE_NODE const &aNode1, LIB_TREE_NODE const &aNode2, bool aUseScores)
Compare two nodes.
enum TYPE m_Type
std::vector< SEARCH_TERM > m_SearchTerms
std::map< wxString, wxString > m_Fields
List of weighted search terms.
PTR_VECTOR m_Children
wxString m_Footprint
LIB_TREE_NODE * m_Parent
virtual void ResetScore()
Initialize scores recursively.
int m_IntrinsicRank
The rank of the item before any search terms are applied.
void AssignIntrinsicRanks(bool presorted=false)
Store intrinsic ranks on all children of this node.
#define _(s)
Abstract pattern-matching tool and implementations.
@ INVALID
Definition: kiface_ids.h:32
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:119