diff --git a/eeschema/cmp_tree_model.cpp b/eeschema/cmp_tree_model.cpp index 75b2b973cd..3329512164 100644 --- a/eeschema/cmp_tree_model.cpp +++ b/eeschema/cmp_tree_model.cpp @@ -105,6 +105,7 @@ int CMP_TREE_NODE::Compare( CMP_TREE_NODE const& aNode1, CMP_TREE_NODE const& aN CMP_TREE_NODE::CMP_TREE_NODE() : Parent( nullptr ), Type( INVALID ), + InTree( false ), IntrinsicRank( 0 ), Score( kLowestDefaultScore ), Unit( 0 ) @@ -177,6 +178,7 @@ CMP_TREE_NODE_LIB_ID::CMP_TREE_NODE_LIB_ID( CMP_TREE_NODE* aParent, LIB_ALIAS* a CMP_TREE_NODE_UNIT& CMP_TREE_NODE_LIB_ID::AddUnit( int aUnit ) { CMP_TREE_NODE_UNIT* unit = new CMP_TREE_NODE_UNIT( this, aUnit ); + unit->InTree = true; Children.push_back( std::unique_ptr( unit ) ); return *unit; } @@ -243,6 +245,7 @@ CMP_TREE_NODE_LIB::CMP_TREE_NODE_LIB( CMP_TREE_NODE* aParent, wxString const& aN CMP_TREE_NODE_LIB_ID& CMP_TREE_NODE_LIB::AddAlias( LIB_ALIAS* aAlias ) { CMP_TREE_NODE_LIB_ID* alias = new CMP_TREE_NODE_LIB_ID( this, aAlias ); + alias->InTree = true; Children.push_back( std::unique_ptr( alias ) ); return *alias; } @@ -269,6 +272,7 @@ CMP_TREE_NODE_ROOT::CMP_TREE_NODE_ROOT() CMP_TREE_NODE_LIB& CMP_TREE_NODE_ROOT::AddLib( wxString const& aName ) { CMP_TREE_NODE_LIB* lib = new CMP_TREE_NODE_LIB( this, aName ); + lib->InTree = true; Children.push_back( std::unique_ptr( lib ) ); return *lib; } diff --git a/eeschema/cmp_tree_model.h b/eeschema/cmp_tree_model.h index ab062ee4a0..0460f8a1f8 100644 --- a/eeschema/cmp_tree_model.h +++ b/eeschema/cmp_tree_model.h @@ -85,6 +85,7 @@ public: CMP_TREE_NODE* Parent; ///< Parent node or null PTR_VECTOR Children; ///< List of child nodes enum TYPE Type; ///< Node type + bool InTree; ///< Flag indicating whether the node is added to the tree /** * The rank of the item before any search terms are applied. This is diff --git a/eeschema/cmp_tree_model_adapter_base.cpp b/eeschema/cmp_tree_model_adapter_base.cpp index 519a91b6e8..a488126017 100644 --- a/eeschema/cmp_tree_model_adapter_base.cpp +++ b/eeschema/cmp_tree_model_adapter_base.cpp @@ -61,7 +61,7 @@ unsigned int CMP_TREE_MODEL_ADAPTER_BASE::IntoArray( for( auto const& child: aNode.Children ) { - if( child->Score > 0 ) + if( child->Score > 0 && child->InTree ) { aChildren.Add( ToItem( &*child ) ); ++n; @@ -156,9 +156,7 @@ void CMP_TREE_MODEL_ADAPTER_BASE::UpdateSearchString( wxString const& aSearch ) m_tree.UpdateScore( matcher ); } - m_tree.SortNodes(); - Cleared(); - + filterContents(); ShowResults() || ShowPreselect() || ShowSingleLibrary(); } @@ -289,7 +287,7 @@ unsigned int CMP_TREE_MODEL_ADAPTER_BASE::GetChildren( auto node = ( aItem.IsOk() ? ToNode( aItem ) : &m_tree ); if( node->Type != CMP_TREE_NODE::TYPE::LIBID - || ( m_show_units && node->Type == CMP_TREE_NODE::TYPE::UNIT ) ) + || ( m_show_units && node->Type == CMP_TREE_NODE::TYPE::LIBID ) ) return IntoArray( *node, aChildren ); else return 0; @@ -464,3 +462,57 @@ bool CMP_TREE_MODEL_ADAPTER_BASE::ShowSingleLibrary() n->Parent->Parent->Children.size() == 1; } ); } + + +void CMP_TREE_MODEL_ADAPTER_BASE::filterContents() +{ + // Rebuild the tree using only the filtered nodes + for( auto& lib : m_tree.Children ) + { + lib->InTree = false; + + for( auto& alias : lib->Children ) + { + alias->InTree = false; + + for( auto& unit : lib->Children ) + unit->InTree = false; + } + } + + m_tree.SortNodes(); + Cleared(); + + for( auto& lib : m_tree.Children ) + { + if( lib->Score <= 0 ) + continue; + + wxDataViewItem libItem = ToItem( lib.get() ); + + for( auto& alias : lib->Children ) + { + if( !lib->InTree ) + { + lib->InTree = true; + ItemAdded( wxDataViewItem( nullptr ), libItem ); + } + + if( alias->Score > 0 ) + { + alias->InTree = true; + ItemAdded( libItem, ToItem( alias.get() ) ); + wxDataViewItem aliasItem = ToItem( alias.get() ); + + if( !m_show_units ) + continue; + + for( auto& unit : alias->Children ) + { + unit->InTree = true; + ItemAdded( aliasItem, ToItem( unit.get() ) ); + } + } + } + } +} diff --git a/eeschema/cmp_tree_model_adapter_base.h b/eeschema/cmp_tree_model_adapter_base.h index c8294ce929..25a31b85c5 100644 --- a/eeschema/cmp_tree_model_adapter_base.h +++ b/eeschema/cmp_tree_model_adapter_base.h @@ -387,6 +387,11 @@ private: * Find and expand a library if there is only one */ bool ShowSingleLibrary(); + + /** + * Filters the items shown in the view according to the score. + */ + void filterContents(); }; #endif // _CMP_TREE_MODEL_ADAPTER_BASE_H diff --git a/eeschema/lib_manager_adapter.cpp b/eeschema/lib_manager_adapter.cpp index 1a58d32335..23f5d6e8db 100644 --- a/eeschema/lib_manager_adapter.cpp +++ b/eeschema/lib_manager_adapter.cpp @@ -186,16 +186,9 @@ CMP_TREE_NODE::PTR_VECTOR::iterator LIB_MANAGER_ADAPTER::deleteLibrary( } -void LIB_MANAGER_ADAPTER::addAliases( CMP_TREE_NODE_LIB& aLibNode ) -{ -} - - void LIB_MANAGER_ADAPTER::finishUpdate() { m_tree.AssignIntrinsicRanks(); - m_tree.SortNodes(); - Resort(); } diff --git a/eeschema/lib_manager_adapter.h b/eeschema/lib_manager_adapter.h index 9abff01afe..1485773f26 100644 --- a/eeschema/lib_manager_adapter.h +++ b/eeschema/lib_manager_adapter.h @@ -53,8 +53,6 @@ protected: CMP_TREE_NODE::PTR_VECTOR::iterator deleteLibrary( CMP_TREE_NODE::PTR_VECTOR::iterator& aLibNodeIt ); - void addAliases( CMP_TREE_NODE_LIB& aLibNode ); - void finishUpdate(); CMP_TREE_NODE* findLibrary( const wxString& aLibNickName );