Score LIB_TREE_NODE_UNITs.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/16106
This commit is contained in:
Jeff Young 2023-11-17 18:13:14 +00:00
parent 78c46e5f4e
commit 2f3c0a608d
2 changed files with 31 additions and 18 deletions

View File

@ -160,6 +160,23 @@ LIB_TREE_NODE_UNIT::LIB_TREE_NODE_UNIT( LIB_TREE_NODE* aParent, LIB_TREE_ITEM* a
}
void LIB_TREE_NODE_UNIT::UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
std::function<bool( LIB_TREE_NODE& aNode )>* aFilter )
{
// aMatcher test results are inherited from parent
if( aMatcher )
m_Score = m_Parent->m_Score;
// aFilter test is subtractive
if( aFilter && !(*aFilter)(*this) )
m_Score = 0;
// show all nodes if no search/filter/etc. criteria are given
if( !aMatcher && aLib.IsEmpty() && ( !aFilter || (*aFilter)(*this) ) )
m_Score = 1;
}
LIB_TREE_NODE_LIB_ITEM::LIB_TREE_NODE_LIB_ITEM( LIB_TREE_NODE* aParent, LIB_TREE_ITEM* aItem )
{
m_Type = LIB_ITEM;
@ -233,6 +250,9 @@ void LIB_TREE_NODE_LIB_ITEM::UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const
// show all nodes if no search/filter/etc. criteria are given
if( !aMatcher && aLib.IsEmpty() && ( !aFilter || (*aFilter)(*this) ) )
m_Score = 1;
for( std::unique_ptr<LIB_TREE_NODE>& child: m_Children )
child->UpdateScore( aMatcher, aLib, aFilter );
}
@ -260,13 +280,10 @@ LIB_TREE_NODE_LIB_ITEM& LIB_TREE_NODE_LIBRARY::AddItem( LIB_TREE_ITEM* aItem )
void LIB_TREE_NODE_LIBRARY::UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
std::function<bool( LIB_TREE_NODE& aNode )>* aFilter )
{
if( m_Children.size() )
for( std::unique_ptr<LIB_TREE_NODE>& child: m_Children )
{
for( std::unique_ptr<LIB_TREE_NODE>& child: m_Children )
{
child->UpdateScore( aMatcher, aLib, aFilter );
m_Score = std::max( m_Score, child->m_Score );
}
child->UpdateScore( aMatcher, aLib, aFilter );
m_Score = std::max( m_Score, child->m_Score );
}
// aLib test is additive

View File

@ -173,12 +173,8 @@ public:
*/
LIB_TREE_NODE_UNIT( LIB_TREE_NODE* aParent, LIB_TREE_ITEM* aItem, int aUnit );
/**
* Do nothing, units just take the parent's score
*/
virtual void UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) override {}
void UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) override;
};
@ -217,8 +213,8 @@ public:
/**
* Perform the actual search.
*/
virtual void UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) override;
void UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) override;
protected:
/**
@ -259,8 +255,8 @@ public:
*/
LIB_TREE_NODE_LIB_ITEM& AddItem( LIB_TREE_ITEM* aItem );
virtual void UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) override;
void UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) override;
};
@ -287,8 +283,8 @@ public:
*/
LIB_TREE_NODE_LIBRARY& AddLib( wxString const& aName, wxString const& aDesc );
virtual void UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) override;
void UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) override;
};