Optimize SCH_PLUGIN enumeration for populating the component chooser

This commit is contained in:
Chris Pavlina 2017-02-24 11:09:27 -05:00
parent adba96fd2b
commit 9effcb80e7
8 changed files with 111 additions and 14 deletions

View File

@ -27,6 +27,7 @@
* @file class_library.cpp
*/
#include <algorithm>
#include <fctsys.h>
#include <kiface_i.h>
#include <gr_basic.h>
@ -138,6 +139,16 @@ void PART_LIB::GetAliasNames( wxArrayString& aNames )
}
void PART_LIB::GetAliases( std::vector<LIB_ALIAS*>& aAliases )
{
m_plugin->EnumerateSymbolLib( aAliases, fileName.GetFullPath() );
std::sort( aAliases.begin(), aAliases.end(),
[](LIB_ALIAS *lhs, LIB_ALIAS *rhs) -> bool
{ return lhs->GetName() < rhs->GetName(); });
}
void PART_LIB::GetEntryTypePowerNames( wxArrayString& aNames )
{
wxArrayString aliases;

View File

@ -400,6 +400,13 @@ public:
*/
void GetAliasNames( wxArrayString& aNames );
/**
* Load a vector with all the entries in this library.
*
* @param aAliases - vector to receive the aliases.
*/
void GetAliases( std::vector<LIB_ALIAS*>& aAliases );
/**
* Load a string array with the names of entries of type POWER in this library.
*

View File

@ -151,14 +151,18 @@ void COMPONENT_TREE_SEARCH_CONTAINER::SetTree( TWO_COLUMN_TREE_LIST* aTree )
void COMPONENT_TREE_SEARCH_CONTAINER::AddLibrary( PART_LIB& aLib )
{
wxArrayString all_aliases;
if( m_filter == CMP_FILTER_POWER )
{
wxArrayString all_aliases;
aLib.GetEntryTypePowerNames( all_aliases );
AddAliasList( aLib.GetName(), all_aliases, &aLib );
}
else
aLib.GetAliasNames( all_aliases );
AddAliasList( aLib.GetName(), all_aliases, &aLib );
{
std::vector<LIB_ALIAS*> all_aliases;
aLib.GetAliases( all_aliases );
AddAliasList( aLib.GetName(), all_aliases, &aLib );
}
++m_libraries_added;
}
@ -168,11 +172,9 @@ void COMPONENT_TREE_SEARCH_CONTAINER::AddAliasList( const wxString& aNodeName,
const wxArrayString& aAliasNameList,
PART_LIB* aOptionalLib )
{
TREE_NODE* const lib_node = new TREE_NODE( TREE_NODE::TYPE_LIB, NULL, NULL,
aNodeName, wxEmptyString, wxEmptyString );
m_nodes.push_back( lib_node );
std::vector<LIB_ALIAS*> alias_list;
for( const wxString& aName : aAliasNameList )
for( const wxString& aName: aAliasNameList )
{
LIB_ALIAS* a;
@ -181,9 +183,26 @@ void COMPONENT_TREE_SEARCH_CONTAINER::AddAliasList( const wxString& aNodeName,
else
a = m_libs->FindLibraryAlias( LIB_ID( wxEmptyString, aName ), wxEmptyString );
if( a == NULL )
continue;
if( a )
{
alias_list.push_back( a );
}
}
AddAliasList( aNodeName, alias_list, aOptionalLib );
}
void COMPONENT_TREE_SEARCH_CONTAINER::AddAliasList( const wxString& aNodeName,
const std::vector<LIB_ALIAS*>& aAliasList,
PART_LIB* aOptionalLib )
{
TREE_NODE* const lib_node = new TREE_NODE( TREE_NODE::TYPE_LIB, NULL, NULL,
aNodeName, wxEmptyString, wxEmptyString );
m_nodes.push_back( lib_node );
for( auto a: aAliasList )
{
wxString search_text;
search_text = ( a->GetKeyWords().empty() ) ? wxT(" ") : a->GetKeyWords();
search_text += a->GetDescription();

View File

@ -70,7 +70,7 @@ public:
*/
void AddLibrary( PART_LIB& aLib );
/** Function AddComponentList
/**
* Add the given list of components, given by name, to be searched.
* To be called in the setup phase to fill this container.
*
@ -81,6 +81,17 @@ public:
void AddAliasList( const wxString& aNodeName, const wxArrayString& aAliasNameList,
PART_LIB* aOptionalLib );
/**
* Add the given list of components, given by alias, to be searched.
* To be called in the setup phase to fill this container.
*
* @param aNodeName The parent node name the components will show up as leaf.
* @param aAliasList List of aliases.
* @param aOptionalLib Library to look up the component names (if NULL: global lookup)
*/
void AddAliasList( const wxString& aNodeName, const std::vector<LIB_ALIAS*>& aAliasList,
PART_LIB* aOptionalLib );
/** Function SetPreselectNode
* Set the component name to be selected in absence of any search-result.
*

View File

@ -296,6 +296,26 @@ public:
const wxString& aLibraryPath,
const PROPERTIES* aProperties = NULL );
/**
* Function EnumerateSymbolLib
*
* returns a list of #LIB_PART aliases contained within the library @a aLibraryPath.
*
* @param aAliasList is an array to populate with the #LIB_ALIAS pointers associated with
* the library.
* @param aLibraryPath is a locator for the "library", usually a directory, file,
* or URL containing one or more #LIB_PART objects.
* @param aProperties is an associative array that can be used to tell the plugin anything
* needed about how to perform with respect to @a aLibraryPath. The
* caller continues to own this object (plugin may not delete it), and
* plugins should expect it to be optionally NULL.
*
* @throw IO_ERROR if the library cannot be found, the part library cannot be loaded.
*/
virtual void EnumerateSymbolLib( std::vector<LIB_ALIAS*>& aAliasList,
const wxString& aLibraryPath,
const PROPERTIES* aProperties = NULL );
/**
* Function LoadSymbol
*

View File

@ -2,7 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2016 CERN
* Copyright (C) 2016-2017 KiCad Developers, see change_log.txt for contributors.
* Copyright (C) 2016-2017 KiCad Developers, see AUTHORS.txt for contributors.
*
* @author Wayne Stambaugh <stambaughw@gmail.com>
*
@ -3429,6 +3429,23 @@ void SCH_LEGACY_PLUGIN::EnumerateSymbolLib( wxArrayString& aAliasNameList,
}
void SCH_LEGACY_PLUGIN::EnumerateSymbolLib( std::vector<LIB_ALIAS*>& aAliasList,
const wxString& aLibraryPath,
const PROPERTIES* aProperties )
{
LOCALE_IO toggle; // toggles on, then off, the C locale.
m_props = aProperties;
cacheLib( aLibraryPath );
const LIB_ALIAS_MAP& aliases = m_cache->m_aliases;
for( LIB_ALIAS_MAP::const_iterator it = aliases.begin(); it != aliases.end(); ++it )
aAliasList.push_back( it->second );
}
LIB_ALIAS* SCH_LEGACY_PLUGIN::LoadSymbol( const wxString& aLibraryPath, const wxString& aAliasName,
const PROPERTIES* aProperties )
{

View File

@ -107,6 +107,9 @@ public:
void EnumerateSymbolLib( wxArrayString& aAliasNameList,
const wxString& aLibraryPath,
const PROPERTIES* aProperties = NULL ) override;
void EnumerateSymbolLib( std::vector<LIB_ALIAS*>& aAliasList,
const wxString& aLibraryPath,
const PROPERTIES* aProperties = NULL ) override;
LIB_ALIAS* LoadSymbol( const wxString& aLibraryPath, const wxString& aAliasName,
const PROPERTIES* aProperties = NULL ) override;
void SaveSymbol( const wxString& aLibraryPath, const LIB_PART* aSymbol,

View File

@ -2,7 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2016 CERN
* Copyright (C) 2016-2017 KiCad Developers, see change_log.txt for contributors.
* Copyright (C) 2016-2017 KiCad Developers, see AUTHORS.txt for contributors.
*
* @author Wayne Stambaugh <stambaughw@gmail.com>
*
@ -89,6 +89,15 @@ void SCH_PLUGIN::EnumerateSymbolLib( wxArrayString& aAliasNameList,
}
void SCH_PLUGIN::EnumerateSymbolLib( std::vector<LIB_ALIAS*>& aAliasList,
const wxString& aLibraryPath,
const PROPERTIES* aProperties )
{
// not pure virtual so that plugins only have to implement subset of the SCH_PLUGIN interface.
not_implemented( this, __FUNCTION__ );
}
LIB_ALIAS* SCH_PLUGIN::LoadSymbol( const wxString& aLibraryPath, const wxString& aSymbolName,
const PROPERTIES* aProperties )
{