diff --git a/eeschema/cmp_tree_model_adapter_base.cpp b/eeschema/cmp_tree_model_adapter_base.cpp index 2b05df1bce..9c3515bb17 100644 --- a/eeschema/cmp_tree_model_adapter_base.cpp +++ b/eeschema/cmp_tree_model_adapter_base.cpp @@ -228,6 +228,30 @@ int CMP_TREE_MODEL_ADAPTER_BASE::GetComponentsCount() const } +wxDataViewItem CMP_TREE_MODEL_ADAPTER_BASE::FindItem( const LIB_ID& aLibId ) +{ + for( auto& lib: m_tree.Children ) + { + if( lib->Name != aLibId.GetLibNickname() ) + continue; + + // if part name is not specified, return the library node + if( aLibId.GetLibItemName() == "" ) + return ToItem( lib.get() ); + + for( auto& alias: lib->Children ) + { + if( alias->Name == aLibId.GetLibItemName() ) + return ToItem( alias.get() ); + } + + break; // could not find the part in the requested library + } + + return wxDataViewItem(); +} + + bool CMP_TREE_MODEL_ADAPTER_BASE::HasContainerColumns( wxDataViewItem const& aItem ) const { return IsContainer( aItem ); diff --git a/eeschema/cmp_tree_model_adapter_base.h b/eeschema/cmp_tree_model_adapter_base.h index 556a58b499..c8294ce929 100644 --- a/eeschema/cmp_tree_model_adapter_base.h +++ b/eeschema/cmp_tree_model_adapter_base.h @@ -233,6 +233,14 @@ public: */ int GetComponentsCount() const; + /** + * Returns tree item corresponding to part. + * + * @param aLibId specifies the part and library name to be searched for. + * @return Tree data item representing the part. Might be invalid if nothings was found. + */ + wxDataViewItem FindItem( const LIB_ID& aLibId ); + protected: static wxDataViewItem ToItem( CMP_TREE_NODE const* aNode ); static CMP_TREE_NODE const* ToNode( wxDataViewItem aItem ); diff --git a/eeschema/libedit.cpp b/eeschema/libedit.cpp index b5e1ac6f47..605061207d 100644 --- a/eeschema/libedit.cpp +++ b/eeschema/libedit.cpp @@ -424,6 +424,7 @@ void LIB_EDIT_FRAME::OnPasteDuplicatePart( wxCommandEvent& aEvent ) LIB_PART newPart( *srcPart ); fixDuplicateAliases( &newPart, lib ); m_libMgr->UpdatePart( &newPart, lib ); + m_treePane->GetCmpTree()->SelectLibId( LIB_ID( lib, newPart.GetName() ) ); } diff --git a/eeschema/libeditframe.cpp b/eeschema/libeditframe.cpp index b5628f1e2a..637341cfa4 100644 --- a/eeschema/libeditframe.cpp +++ b/eeschema/libeditframe.cpp @@ -1012,6 +1012,10 @@ void LIB_EDIT_FRAME::SetCurPart( LIB_PART* aPart ) m_my_part = aPart; } + // select the current component in the tree widget + if( aPart ) + m_treePane->GetCmpTree()->SelectLibId( aPart->GetLibId() ); + wxString partName = aPart ? aPart->GetName() : wxString(); m_libMgr->SetCurrentPart( partName ); diff --git a/eeschema/widgets/component_tree.cpp b/eeschema/widgets/component_tree.cpp index c240bdb2ac..a10e509416 100644 --- a/eeschema/widgets/component_tree.cpp +++ b/eeschema/widgets/component_tree.cpp @@ -131,6 +131,12 @@ LIB_ID COMPONENT_TREE::GetSelectedLibId( int* aUnit ) const } +void COMPONENT_TREE::SelectLibId( const LIB_ID& aLibId ) +{ + selectIfValid( m_adapter->FindItem( aLibId ) ); +} + + void COMPONENT_TREE::selectIfValid( const wxDataViewItem& aTreeId ) { if( aTreeId.IsOk() ) diff --git a/eeschema/widgets/component_tree.h b/eeschema/widgets/component_tree.h index d2777b6743..3cf0afb1e1 100644 --- a/eeschema/widgets/component_tree.h +++ b/eeschema/widgets/component_tree.h @@ -60,6 +60,13 @@ public: */ LIB_ID GetSelectedLibId( int* aUnit = nullptr ) const; + /** + * Select a part in the tree widget. + * + * @param aLibId is the identifier of part to be selected. + */ + void SelectLibId( const LIB_ID& aLibId ); + /** * Associates a right click context menu for a specific node type. * @param aType is the node type to have a menu associated.