2017-03-06 14:50:48 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Chris Pavlina <pavlina.chris@gmail.com>
|
|
|
|
* Copyright (C) 2014 Henner Zeller <h.zeller@acm.org>
|
2018-01-12 16:59:26 +00:00
|
|
|
* Copyright (C) 2014-2018 KiCad Developers, see AUTHORS.txt for contributors.
|
2017-03-06 14:50:48 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cmp_tree_model.h>
|
|
|
|
|
|
|
|
#include <class_library.h>
|
|
|
|
#include <eda_pattern_match.h>
|
|
|
|
#include <make_unique.h>
|
|
|
|
#include <utility>
|
2018-01-25 23:49:04 +00:00
|
|
|
#include <pgm_base.h>
|
2017-03-06 14:50:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Each node gets this lowest score initially, without any matches applied.
|
|
|
|
// Matches will then increase this score depending on match quality. This way,
|
|
|
|
// an empty search string will result in all components being displayed as they
|
|
|
|
// have the minimum score. However, in that case, we avoid expanding all the
|
|
|
|
// nodes asd the result is very unspecific.
|
|
|
|
static const unsigned kLowestDefaultScore = 1;
|
|
|
|
|
|
|
|
|
|
|
|
// Creates a score depending on the position of a string match. If the position
|
|
|
|
// is 0 (= prefix match), this returns the maximum score. This degrades until
|
|
|
|
// pos == max, which returns a score of 0; Evertyhing else beyond that is just
|
|
|
|
// 0. Only values >= 0 allowed for position and max.
|
|
|
|
//
|
|
|
|
// @param aPosition is the position a string has been found in a substring.
|
|
|
|
// @param aMaximum is the maximum score this function returns.
|
|
|
|
// @return position dependent score.
|
|
|
|
static int matchPosScore(int aPosition, int aMaximum)
|
|
|
|
{
|
|
|
|
return ( aPosition < aMaximum ) ? aMaximum - aPosition : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CMP_TREE_NODE::ResetScore()
|
|
|
|
{
|
|
|
|
for( auto& child: Children )
|
|
|
|
child->ResetScore();
|
|
|
|
|
|
|
|
Score = kLowestDefaultScore;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CMP_TREE_NODE::AssignIntrinsicRanks()
|
|
|
|
{
|
|
|
|
std::vector<CMP_TREE_NODE*> sort_buf;
|
|
|
|
|
|
|
|
for( auto const& node: Children )
|
|
|
|
sort_buf.push_back( &*node );
|
|
|
|
|
|
|
|
std::sort( sort_buf.begin(), sort_buf.end(),
|
|
|
|
[]( CMP_TREE_NODE* a, CMP_TREE_NODE* b ) -> bool
|
|
|
|
{ return a->MatchName > b->MatchName; } );
|
|
|
|
|
|
|
|
for( int i = 0; i < (int) sort_buf.size(); ++i )
|
|
|
|
sort_buf[i]->IntrinsicRank = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CMP_TREE_NODE::SortNodes()
|
|
|
|
{
|
|
|
|
std::sort( Children.begin(), Children.end(),
|
|
|
|
[]( std::unique_ptr<CMP_TREE_NODE> const& a, std::unique_ptr<CMP_TREE_NODE> const& b )
|
|
|
|
{ return Compare( *a, *b ) > 0; } );
|
|
|
|
|
|
|
|
for( auto& node: Children )
|
|
|
|
{
|
|
|
|
node->SortNodes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int CMP_TREE_NODE::Compare( CMP_TREE_NODE const& aNode1, CMP_TREE_NODE const& aNode2 )
|
|
|
|
{
|
|
|
|
if( aNode1.Type != aNode2.Type )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if( aNode1.Score != aNode2.Score )
|
|
|
|
return aNode1.Score - aNode2.Score;
|
|
|
|
|
|
|
|
if( aNode1.Parent != aNode2.Parent )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return aNode1.IntrinsicRank - aNode2.IntrinsicRank;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CMP_TREE_NODE::CMP_TREE_NODE()
|
|
|
|
: Parent( nullptr ),
|
2017-03-12 00:22:42 +00:00
|
|
|
Type( INVALID ),
|
2017-03-06 14:50:48 +00:00
|
|
|
IntrinsicRank( 0 ),
|
|
|
|
Score( kLowestDefaultScore ),
|
2018-01-06 05:02:28 +00:00
|
|
|
Unit( 0 ),
|
|
|
|
IsRoot( false )
|
2017-03-06 14:50:48 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
CMP_TREE_NODE_UNIT::CMP_TREE_NODE_UNIT( CMP_TREE_NODE* aParent, int aUnit )
|
|
|
|
{
|
2018-01-25 23:49:04 +00:00
|
|
|
static void* locale = nullptr;
|
|
|
|
static wxString namePrefix;
|
|
|
|
|
|
|
|
// Fetching translations can take a surprising amount of time when loading libraries,
|
|
|
|
// so only do it when necessary.
|
|
|
|
if( Pgm().GetLocale() != locale )
|
|
|
|
{
|
|
|
|
namePrefix = _( "Unit" );
|
|
|
|
locale = Pgm().GetLocale();
|
|
|
|
}
|
|
|
|
|
2017-03-06 14:50:48 +00:00
|
|
|
Parent = aParent;
|
|
|
|
Type = UNIT;
|
|
|
|
|
|
|
|
Unit = aUnit;
|
2017-09-15 14:17:44 +00:00
|
|
|
LibId = aParent->LibId;
|
2017-03-06 14:50:48 +00:00
|
|
|
|
2018-01-25 23:49:04 +00:00
|
|
|
Name = namePrefix + " " + LIB_PART::SubReference( aUnit, false );
|
2017-03-06 14:50:48 +00:00
|
|
|
Desc = wxEmptyString;
|
|
|
|
MatchName = wxEmptyString;
|
|
|
|
|
|
|
|
IntrinsicRank = -aUnit;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-15 14:17:44 +00:00
|
|
|
CMP_TREE_NODE_LIB_ID::CMP_TREE_NODE_LIB_ID( CMP_TREE_NODE* aParent, LIB_ALIAS* aAlias )
|
2017-03-06 14:50:48 +00:00
|
|
|
{
|
2017-11-06 01:59:51 +00:00
|
|
|
wxASSERT( aParent && aAlias );
|
|
|
|
|
2018-01-10 11:04:34 +00:00
|
|
|
Type = LIBID;
|
|
|
|
Parent = aParent;
|
|
|
|
Update( aAlias );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CMP_TREE_NODE_UNIT& CMP_TREE_NODE_LIB_ID::AddUnit( int aUnit )
|
|
|
|
{
|
|
|
|
CMP_TREE_NODE_UNIT* unit = new CMP_TREE_NODE_UNIT( this, aUnit );
|
|
|
|
Children.push_back( std::unique_ptr<CMP_TREE_NODE>( unit ) );
|
|
|
|
return *unit;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CMP_TREE_NODE_LIB_ID::Update( LIB_ALIAS* aAlias )
|
|
|
|
{
|
2017-03-06 14:50:48 +00:00
|
|
|
Name = aAlias->GetName();
|
|
|
|
Desc = aAlias->GetDescription();
|
2017-09-15 14:17:44 +00:00
|
|
|
|
|
|
|
// Parent node is the library nickname so set the LIB_ID library nickname.
|
|
|
|
IsRoot = aAlias->IsRoot();
|
2017-03-06 14:50:48 +00:00
|
|
|
|
2017-03-10 13:28:09 +00:00
|
|
|
// Pre-normalized strings for fast case-insensitive matching
|
|
|
|
// Search text spaces out keywords and description to penalize description
|
|
|
|
// matches - earlier matches are worth more.
|
|
|
|
MatchName = aAlias->GetName().Lower();
|
2018-01-25 23:49:04 +00:00
|
|
|
SearchText = (aAlias->GetKeyWords() + " " + Desc);
|
2017-03-10 13:28:09 +00:00
|
|
|
|
2017-07-09 13:08:33 +00:00
|
|
|
// Extract default footprint text
|
|
|
|
LIB_PART* part = aAlias->GetPart();
|
|
|
|
|
|
|
|
wxString footprint;
|
|
|
|
|
|
|
|
if( part )
|
|
|
|
{
|
2017-11-06 01:59:51 +00:00
|
|
|
LibId = part->GetLibId();
|
2018-01-12 16:59:26 +00:00
|
|
|
LibId.SetLibItemName( Name );
|
2017-07-09 13:08:33 +00:00
|
|
|
footprint = part->GetFootprintField().GetText();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a footprint is defined for the part,
|
|
|
|
// add it to the serach string
|
|
|
|
if( !footprint.IsEmpty() )
|
|
|
|
{
|
|
|
|
SearchText += " ";
|
2018-01-25 23:49:04 +00:00
|
|
|
SearchText += footprint;
|
2017-07-09 13:08:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-10 11:04:34 +00:00
|
|
|
Children.clear();
|
|
|
|
|
2017-12-04 09:20:05 +00:00
|
|
|
if( part && part->IsMulti() )
|
2017-03-06 14:50:48 +00:00
|
|
|
{
|
2017-11-06 01:59:51 +00:00
|
|
|
for( int u = 1; u <= part->GetUnitCount(); ++u )
|
2017-03-06 14:50:48 +00:00
|
|
|
AddUnit( u );
|
|
|
|
}
|
2018-02-06 22:53:53 +00:00
|
|
|
|
|
|
|
SearchTextNormalized = false;
|
2017-03-06 14:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-15 14:17:44 +00:00
|
|
|
void CMP_TREE_NODE_LIB_ID::UpdateScore( EDA_COMBINED_MATCHER& aMatcher )
|
2017-03-06 14:50:48 +00:00
|
|
|
{
|
|
|
|
if( Score <= 0 )
|
|
|
|
return; // Leaf nodes without scores are out of the game.
|
|
|
|
|
2018-01-25 23:49:04 +00:00
|
|
|
if( !SearchTextNormalized )
|
|
|
|
{
|
|
|
|
SearchText = SearchText.Lower();
|
|
|
|
SearchTextNormalized = true;
|
|
|
|
}
|
|
|
|
|
2017-03-06 14:50:48 +00:00
|
|
|
// 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.
|
|
|
|
int found_pos = EDA_PATTERN_NOT_FOUND;
|
|
|
|
int matchers_fired = 0;
|
|
|
|
|
|
|
|
if( aMatcher.GetPattern() == MatchName )
|
2017-03-10 13:28:09 +00:00
|
|
|
{
|
2017-03-06 14:50:48 +00:00
|
|
|
Score += 1000; // exact match. High score :)
|
2017-03-10 13:28:09 +00:00
|
|
|
}
|
2017-03-06 14:50:48 +00:00
|
|
|
else if( aMatcher.Find( MatchName, matchers_fired, found_pos ) )
|
|
|
|
{
|
|
|
|
// Substring match. The earlier in the string the better.
|
|
|
|
Score += matchPosScore( found_pos, 20 ) + 20;
|
|
|
|
}
|
|
|
|
else if( aMatcher.Find( Parent->MatchName, matchers_fired, found_pos ) )
|
2017-03-10 13:28:09 +00:00
|
|
|
{
|
2017-03-06 14:50:48 +00:00
|
|
|
Score += 19; // parent name matches. score += 19
|
2017-03-10 13:28:09 +00:00
|
|
|
}
|
2017-03-06 14:50:48 +00:00
|
|
|
else if( aMatcher.Find( SearchText, matchers_fired, found_pos ) )
|
|
|
|
{
|
|
|
|
// If we have a very short search term (like one or two letters),
|
|
|
|
// we don't want to accumulate scores if they just happen to be in
|
|
|
|
// keywords or description as almost any one or two-letter
|
|
|
|
// combination shows up in there.
|
|
|
|
if( aMatcher.GetPattern().length() >= 2 )
|
|
|
|
{
|
|
|
|
// For longer terms, we add scores 1..18 for positional match
|
|
|
|
// (higher in the front, where the keywords are).
|
|
|
|
Score += matchPosScore( found_pos, 17 ) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No match. That's it for this item.
|
|
|
|
Score = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// More matchers = better match
|
|
|
|
Score += 2 * matchers_fired;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-06 15:14:01 +00:00
|
|
|
CMP_TREE_NODE_LIB::CMP_TREE_NODE_LIB( CMP_TREE_NODE* aParent,
|
|
|
|
wxString const& aName, wxString const& aDesc )
|
2017-03-06 14:50:48 +00:00
|
|
|
{
|
|
|
|
Type = LIB;
|
|
|
|
Name = aName;
|
|
|
|
MatchName = aName.Lower();
|
2018-02-06 15:14:01 +00:00
|
|
|
Desc = aDesc;
|
2017-03-06 14:50:48 +00:00
|
|
|
Parent = aParent;
|
2017-11-06 13:38:33 +00:00
|
|
|
LibId.SetLibNickname( aName );
|
2017-03-06 14:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-15 14:17:44 +00:00
|
|
|
CMP_TREE_NODE_LIB_ID& CMP_TREE_NODE_LIB::AddAlias( LIB_ALIAS* aAlias )
|
2017-03-06 14:50:48 +00:00
|
|
|
{
|
2017-09-15 14:17:44 +00:00
|
|
|
CMP_TREE_NODE_LIB_ID* alias = new CMP_TREE_NODE_LIB_ID( this, aAlias );
|
2017-03-06 14:50:48 +00:00
|
|
|
Children.push_back( std::unique_ptr<CMP_TREE_NODE>( alias ) );
|
|
|
|
return *alias;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CMP_TREE_NODE_LIB::UpdateScore( EDA_COMBINED_MATCHER& aMatcher )
|
|
|
|
{
|
|
|
|
Score = 0;
|
|
|
|
|
|
|
|
for( auto& child: Children )
|
|
|
|
{
|
|
|
|
child->UpdateScore( aMatcher );
|
|
|
|
Score = std::max( Score, child->Score );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CMP_TREE_NODE_ROOT::CMP_TREE_NODE_ROOT()
|
|
|
|
{
|
|
|
|
Type = ROOT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-06 15:14:01 +00:00
|
|
|
CMP_TREE_NODE_LIB& CMP_TREE_NODE_ROOT::AddLib( wxString const& aName, wxString const& aDesc )
|
2017-03-06 14:50:48 +00:00
|
|
|
{
|
2018-02-06 15:14:01 +00:00
|
|
|
CMP_TREE_NODE_LIB* lib = new CMP_TREE_NODE_LIB( this, aName, aDesc );
|
2017-03-06 14:50:48 +00:00
|
|
|
Children.push_back( std::unique_ptr<CMP_TREE_NODE>( lib ) );
|
|
|
|
return *lib;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CMP_TREE_NODE_ROOT::UpdateScore( EDA_COMBINED_MATCHER& aMatcher )
|
|
|
|
{
|
|
|
|
for( auto& child: Children )
|
|
|
|
child->UpdateScore( aMatcher );
|
|
|
|
}
|
|
|
|
|