From 9effcb80e75c36d29298038c82bcd1b08bafa9df Mon Sep 17 00:00:00 2001 From: Chris Pavlina Date: Fri, 24 Feb 2017 11:09:27 -0500 Subject: [PATCH] Optimize SCH_PLUGIN enumeration for populating the component chooser --- eeschema/class_library.cpp | 11 ++++++ eeschema/class_library.h | 7 ++++ eeschema/component_tree_search_container.cpp | 41 ++++++++++++++------ eeschema/component_tree_search_container.h | 13 ++++++- eeschema/sch_io_mgr.h | 20 ++++++++++ eeschema/sch_legacy_plugin.cpp | 19 ++++++++- eeschema/sch_legacy_plugin.h | 3 ++ eeschema/sch_plugin.cpp | 11 +++++- 8 files changed, 111 insertions(+), 14 deletions(-) diff --git a/eeschema/class_library.cpp b/eeschema/class_library.cpp index 55b17793a1..11a9db6541 100644 --- a/eeschema/class_library.cpp +++ b/eeschema/class_library.cpp @@ -27,6 +27,7 @@ * @file class_library.cpp */ +#include #include #include #include @@ -138,6 +139,16 @@ void PART_LIB::GetAliasNames( wxArrayString& aNames ) } +void PART_LIB::GetAliases( std::vector& 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; diff --git a/eeschema/class_library.h b/eeschema/class_library.h index 1a653b5a33..548ccae053 100644 --- a/eeschema/class_library.h +++ b/eeschema/class_library.h @@ -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& aAliases ); + /** * Load a string array with the names of entries of type POWER in this library. * diff --git a/eeschema/component_tree_search_container.cpp b/eeschema/component_tree_search_container.cpp index 68feef951d..1619bc28b0 100644 --- a/eeschema/component_tree_search_container.cpp +++ b/eeschema/component_tree_search_container.cpp @@ -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 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 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& 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(); diff --git a/eeschema/component_tree_search_container.h b/eeschema/component_tree_search_container.h index 2dbec1ef09..325c71ef09 100644 --- a/eeschema/component_tree_search_container.h +++ b/eeschema/component_tree_search_container.h @@ -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& aAliasList, + PART_LIB* aOptionalLib ); + /** Function SetPreselectNode * Set the component name to be selected in absence of any search-result. * diff --git a/eeschema/sch_io_mgr.h b/eeschema/sch_io_mgr.h index 7f0fe69f05..8102b992b0 100644 --- a/eeschema/sch_io_mgr.h +++ b/eeschema/sch_io_mgr.h @@ -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& aAliasList, + const wxString& aLibraryPath, + const PROPERTIES* aProperties = NULL ); + /** * Function LoadSymbol * diff --git a/eeschema/sch_legacy_plugin.cpp b/eeschema/sch_legacy_plugin.cpp index fbf8df6475..ae2752abd2 100644 --- a/eeschema/sch_legacy_plugin.cpp +++ b/eeschema/sch_legacy_plugin.cpp @@ -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 * @@ -3429,6 +3429,23 @@ void SCH_LEGACY_PLUGIN::EnumerateSymbolLib( wxArrayString& aAliasNameList, } +void SCH_LEGACY_PLUGIN::EnumerateSymbolLib( std::vector& 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 ) { diff --git a/eeschema/sch_legacy_plugin.h b/eeschema/sch_legacy_plugin.h index fc5d3a19a0..8f15376d7b 100644 --- a/eeschema/sch_legacy_plugin.h +++ b/eeschema/sch_legacy_plugin.h @@ -107,6 +107,9 @@ public: void EnumerateSymbolLib( wxArrayString& aAliasNameList, const wxString& aLibraryPath, const PROPERTIES* aProperties = NULL ) override; + void EnumerateSymbolLib( std::vector& 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, diff --git a/eeschema/sch_plugin.cpp b/eeschema/sch_plugin.cpp index 419cf70ab9..7b859ea272 100644 --- a/eeschema/sch_plugin.cpp +++ b/eeschema/sch_plugin.cpp @@ -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 * @@ -89,6 +89,15 @@ void SCH_PLUGIN::EnumerateSymbolLib( wxArrayString& aAliasNameList, } +void SCH_PLUGIN::EnumerateSymbolLib( std::vector& 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 ) {