Fix lib tree searches to handle searching on library names.

Fixes https://gitlab.com/kicad/code/kicad/issues/9981
This commit is contained in:
Jeff Young 2021-12-14 15:18:11 +00:00
parent e5d6ec836f
commit 724182abe4
3 changed files with 33 additions and 12 deletions

View File

@ -206,7 +206,7 @@ void LIB_TREE_NODE_LIB_ID::Update( LIB_TREE_ITEM* aItem )
}
void LIB_TREE_NODE_LIB_ID::UpdateScore( EDA_COMBINED_MATCHER& aMatcher )
void LIB_TREE_NODE_LIB_ID::UpdateScore( EDA_COMBINED_MATCHER& aMatcher, const wxString& aLib )
{
if( m_Score <= 0 )
return; // Leaf nodes without scores are out of the game.
@ -218,6 +218,12 @@ void LIB_TREE_NODE_LIB_ID::UpdateScore( EDA_COMBINED_MATCHER& aMatcher )
m_Normalized = true;
}
if( !aLib.IsEmpty() && m_Parent->m_MatchName != aLib )
{
m_Score = 0;
return;
}
// Keywords and description we only count if the match string is at
// least two characters long. That avoids spurious, low quality
// matches. Most abbreviations are at three characters long.
@ -281,7 +287,7 @@ LIB_TREE_NODE_LIB_ID& LIB_TREE_NODE_LIB::AddItem( LIB_TREE_ITEM* aItem )
}
void LIB_TREE_NODE_LIB::UpdateScore( EDA_COMBINED_MATCHER& aMatcher )
void LIB_TREE_NODE_LIB::UpdateScore( EDA_COMBINED_MATCHER& aMatcher, const wxString& aLib )
{
m_Score = 0;
@ -291,13 +297,20 @@ void LIB_TREE_NODE_LIB::UpdateScore( EDA_COMBINED_MATCHER& aMatcher )
{
for( std::unique_ptr<LIB_TREE_NODE>& child: m_Children )
{
child->UpdateScore( aMatcher );
child->UpdateScore( aMatcher, aLib );
m_Score = std::max( m_Score, child->m_Score );
}
}
else
{
// No children; we are a leaf.
if( !aLib.IsEmpty() )
{
m_Score = m_MatchName == aLib ? 1000 : 0;
return;
}
int found_pos = EDA_PATTERN_NOT_FOUND;
int matchers_fired = 0;
@ -331,9 +344,9 @@ LIB_TREE_NODE_LIB& LIB_TREE_NODE_ROOT::AddLib( wxString const& aName, wxString c
}
void LIB_TREE_NODE_ROOT::UpdateScore( EDA_COMBINED_MATCHER& aMatcher )
void LIB_TREE_NODE_ROOT::UpdateScore( EDA_COMBINED_MATCHER& aMatcher, const wxString& aLib )
{
for( std::unique_ptr<LIB_TREE_NODE>& child: m_Children )
child->UpdateScore( aMatcher );
child->UpdateScore( aMatcher, aLib );
}

View File

@ -224,10 +224,18 @@ void LIB_TREE_MODEL_ADAPTER::UpdateSearchString( const wxString& aSearch, bool a
while( tokenizer.HasMoreTokens() )
{
const wxString term = tokenizer.GetNextToken().Lower();
wxString lib;
wxString term = tokenizer.GetNextToken().Lower();
if( term.Contains( ":" ) )
{
lib = term.BeforeFirst( ':' );
term = term.AfterFirst( ':' );
}
EDA_COMBINED_MATCHER matcher( term );
m_tree.UpdateScore( matcher );
m_tree.UpdateScore( matcher, lib );
}
m_tree.SortNodes();

View File

@ -81,7 +81,7 @@ public:
*
* @param aMatcher an EDA_COMBINED_MATCHER initialized with the search term
*/
virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher ) = 0;
virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher, const wxString& aLib ) = 0;
/**
* Initialize score to kLowestDefaultScore, recursively.
@ -170,7 +170,7 @@ public:
/**
* Do nothing, units just take the parent's score
*/
virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher ) override {}
virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher, const wxString& aLib ) override {}
};
@ -209,7 +209,7 @@ public:
/**
* Perform the actual search.
*/
virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher ) override;
virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher, const wxString& aLib ) override;
protected:
/**
@ -250,7 +250,7 @@ public:
*/
LIB_TREE_NODE_LIB_ID& AddItem( LIB_TREE_ITEM* aItem );
virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher ) override;
virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher, const wxString& aLib ) override;
};
@ -277,7 +277,7 @@ public:
*/
LIB_TREE_NODE_LIB& AddLib( wxString const& aName, wxString const& aDesc );
virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher ) override;
virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher, const wxString& aLib ) override;
};