diff --git a/eeschema/libedit.cpp b/eeschema/libedit.cpp index 75e44d92a..807c4117b 100644 --- a/eeschema/libedit.cpp +++ b/eeschema/libedit.cpp @@ -378,10 +378,6 @@ void LIB_EDIT_FRAME::OnRemovePart( wxCommandEvent& aEvent ) if( isCurrentPart( libId ) ) emptyScreen(); - // Keeping a removed item selected may lead to a crash - if( m_treePane->GetCmpTree()->GetSelectedLibId( nullptr ) == libId ) - m_treePane->GetCmpTree()->SelectLibId( LIB_ID( libId.GetLibNickname(), "" ) ); - m_libMgr->RemovePart( libId.GetLibItemName(), libId.GetLibNickname() ); } @@ -389,7 +385,8 @@ void LIB_EDIT_FRAME::OnRemovePart( wxCommandEvent& aEvent ) void LIB_EDIT_FRAME::OnCopyCutPart( wxCommandEvent& aEvent ) { int unit = 0; - LIB_ID partId = m_treePane->GetCmpTree()->GetSelectedLibId( &unit ); + auto cmpTree = m_treePane->GetCmpTree(); + LIB_ID partId = cmpTree->GetSelectedLibId( &unit ); LIB_PART* part = m_libMgr->GetBufferedPart( partId.GetLibItemName(), partId.GetLibNickname() ); if( !part ) @@ -403,10 +400,6 @@ void LIB_EDIT_FRAME::OnCopyCutPart( wxCommandEvent& aEvent ) if( isCurrentPart( libId ) ) emptyScreen(); - // Keeping a removed item selected may lead to a crash - if( m_treePane->GetCmpTree()->GetSelectedLibId( nullptr ) == libId ) - m_treePane->GetCmpTree()->SelectLibId( LIB_ID( libId.GetLibNickname(), "" ) ); - m_libMgr->RemovePart( libId.GetLibItemName(), libId.GetLibNickname() ); } } diff --git a/eeschema/libeditframe.cpp b/eeschema/libeditframe.cpp index c29cb6607..90956c507 100644 --- a/eeschema/libeditframe.cpp +++ b/eeschema/libeditframe.cpp @@ -1635,6 +1635,11 @@ wxString LIB_EDIT_FRAME::getTargetLib() const void LIB_EDIT_FRAME::SyncLibraries( bool aProgress ) { + LIB_ID selected; + + if( m_treePane ) + selected = m_treePane->GetCmpTree()->GetSelectedLibId(); + if( aProgress ) { wxProgressDialog progressDlg( _( "Loading symbol libraries" ), @@ -1650,7 +1655,29 @@ void LIB_EDIT_FRAME::SyncLibraries( bool aProgress ) } if( m_treePane ) + { + // Check if the previously selected item is still valid, + // if not - it has to be unselected to prevent crash + if( selected.IsValid() ) + { + wxDataViewItem found = m_libMgr->GetAdapter()->FindItem( selected ); + + // Try to select the parent library, in case the part is not found + if( !found ) + { + selected.SetLibItemName( "" ); + found = m_libMgr->GetAdapter()->FindItem( selected ); + + if( found ) + m_treePane->GetCmpTree()->SelectLibId( selected ); + } + + if( !found ) + m_treePane->GetCmpTree()->Unselect(); + } + m_treePane->Regenerate(); + } } diff --git a/eeschema/widgets/component_tree.cpp b/eeschema/widgets/component_tree.cpp index bc0cb14a2..2249b9b43 100644 --- a/eeschema/widgets/component_tree.cpp +++ b/eeschema/widgets/component_tree.cpp @@ -146,6 +146,15 @@ void COMPONENT_TREE::SelectLibId( const LIB_ID& aLibId ) } +void COMPONENT_TREE::Unselect() +{ + auto current_selection = m_tree_ctrl->GetSelection(); + + if( current_selection != nullptr ) + m_tree_ctrl->Unselect( current_selection ); +} + + void COMPONENT_TREE::Regenerate() { STATE current; @@ -232,7 +241,7 @@ void COMPONENT_TREE::setState( const STATE& aState ) // command is issued, otherwise it selects a random item (Windows) m_tree_ctrl->Thaw(); - if( aState.selection.IsValid() ) + if( !aState.selection.GetLibItemName().empty() || !aState.selection.GetLibNickname().empty() ) SelectLibId( aState.selection ); } diff --git a/eeschema/widgets/component_tree.h b/eeschema/widgets/component_tree.h index 34b5d7380..15bf9c60f 100644 --- a/eeschema/widgets/component_tree.h +++ b/eeschema/widgets/component_tree.h @@ -78,6 +78,11 @@ public: void SelectLibId( const LIB_ID& aLibId ); /** + * Unselect currently selected item in wxDataViewCtrl + */ + void Unselect(); + + /** * Associates a right click context menu for a specific node type. * @param aType is the node type to have a menu associated. * @param aMenu is the associated menu.