diff --git a/eeschema/cmp_tree_model_adapter_base.cpp b/eeschema/cmp_tree_model_adapter_base.cpp index 1d1f01622f..29b78c5fa0 100644 --- a/eeschema/cmp_tree_model_adapter_base.cpp +++ b/eeschema/cmp_tree_model_adapter_base.cpp @@ -251,6 +251,20 @@ wxDataViewItem CMP_TREE_MODEL_ADAPTER_BASE::FindItem( const LIB_ID& aLibId ) } +unsigned int CMP_TREE_MODEL_ADAPTER_BASE::GetChildren( + wxDataViewItem const& aItem, + wxDataViewItemArray& aChildren ) const +{ + 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::LIBID ) ) + return IntoArray( *node, aChildren ); + else + return 0; +} + + bool CMP_TREE_MODEL_ADAPTER_BASE::HasContainerColumns( wxDataViewItem const& aItem ) const { return IsContainer( aItem ); @@ -282,20 +296,6 @@ wxDataViewItem CMP_TREE_MODEL_ADAPTER_BASE::GetParent( wxDataViewItem const& aIt } -unsigned int CMP_TREE_MODEL_ADAPTER_BASE::GetChildren( - wxDataViewItem const& aItem, - wxDataViewItemArray& aChildren ) const -{ - 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::LIBID ) ) - return IntoArray( *node, aChildren ); - else - return 0; -} - - void CMP_TREE_MODEL_ADAPTER_BASE::GetValue( wxVariant& aVariant, wxDataViewItem const& aItem, diff --git a/eeschema/cmp_tree_model_adapter_base.h b/eeschema/cmp_tree_model_adapter_base.h index 25a31b85c5..66d2b9285f 100644 --- a/eeschema/cmp_tree_model_adapter_base.h +++ b/eeschema/cmp_tree_model_adapter_base.h @@ -241,6 +241,15 @@ public: */ wxDataViewItem FindItem( const LIB_ID& aLibId ); + /** + * Populate a list of all the children of an item + * + * @return number of children + */ + virtual unsigned int GetChildren( + wxDataViewItem const& aItem, + wxDataViewItemArray& aChildren ) const override; + protected: static wxDataViewItem ToItem( CMP_TREE_NODE const* aNode ); static CMP_TREE_NODE const* ToNode( wxDataViewItem aItem ); @@ -271,15 +280,6 @@ protected: */ virtual wxDataViewItem GetParent( wxDataViewItem const& aItem ) const override; - /** - * Populate a list of all the children of an item - * - * @return number of children - */ - virtual unsigned int GetChildren( - wxDataViewItem const& aItem, - wxDataViewItemArray& aChildren ) const override; - /** * Return the number of columns in the model */ diff --git a/eeschema/widgets/component_tree.cpp b/eeschema/widgets/component_tree.cpp index a10e509416..8b6724d077 100644 --- a/eeschema/widgets/component_tree.cpp +++ b/eeschema/widgets/component_tree.cpp @@ -41,7 +41,8 @@ COMPONENT_TREE::COMPONENT_TREE( wxWindow* aParent, SYMBOL_LIB_TABLE* aSymLibTabl m_sym_lib_table( aSymLibTable ), m_adapter( aAdapter ), m_query_ctrl( nullptr ), - m_details_ctrl( nullptr ) + m_details_ctrl( nullptr ), + m_filtering( false ) { // create space for context menu pointers, INVALID is the max value m_menus.resize( CMP_TREE_NODE::TYPE::INVALID + 1 ); @@ -164,9 +165,24 @@ void COMPONENT_TREE::postSelectEvent() void COMPONENT_TREE::onQueryText( wxCommandEvent& aEvent ) { + // Store the state + if( !m_filtering ) + { + m_selection = m_tree_ctrl->GetSelection(); + saveExpandFlag(); + } + m_adapter->UpdateSearchString( m_query_ctrl->GetLineText( 0 ) ); + m_filtering = !m_query_ctrl->IsEmpty(); postPreselectEvent(); + // Restore the state + if( !m_filtering ) + { + selectIfValid( m_selection ); + restoreExpandFlag(); + } + // Required to avoid interaction with SetHint() // See documentation for wxTextEntry::SetHint aEvent.Skip(); @@ -260,5 +276,32 @@ void COMPONENT_TREE::onContextMenu( wxDataViewEvent& aEvent ) } +void COMPONENT_TREE::saveExpandFlag() +{ + wxDataViewItemArray items; + m_adapter->GetChildren( wxDataViewItem( nullptr ), items ); + m_expanded.clear(); + + for( const auto& item : items ) + { + if( m_tree_ctrl->IsExpanded( item ) ) + m_expanded.push_back( item ); + } +} + + +void COMPONENT_TREE::restoreExpandFlag() +{ + m_tree_ctrl->Freeze(); + + for( const auto& item : m_expanded ) + { + m_tree_ctrl->Expand( item ); + } + + m_tree_ctrl->Thaw(); +} + + wxDEFINE_EVENT( COMPONENT_PRESELECTED, wxCommandEvent ); wxDEFINE_EVENT( COMPONENT_SELECTED, wxCommandEvent ); diff --git a/eeschema/widgets/component_tree.h b/eeschema/widgets/component_tree.h index 3cf0afb1e1..0917e4f951 100644 --- a/eeschema/widgets/component_tree.h +++ b/eeschema/widgets/component_tree.h @@ -114,8 +114,18 @@ protected: void onPreselect( wxCommandEvent& aEvent ); void onContextMenu( wxDataViewEvent& aEvent ); + /** + * Store the list of expanded nodes in the tree widget. + */ + void saveExpandFlag(); + + /** + * Restore the expanded nodes in the tree widget. + */ + void restoreExpandFlag(); + SYMBOL_LIB_TABLE* m_sym_lib_table; - CMP_TREE_MODEL_ADAPTER::PTR m_adapter; + CMP_TREE_MODEL_ADAPTER_BASE::PTR m_adapter; wxTextCtrl* m_query_ctrl; wxDataViewCtrl* m_tree_ctrl; @@ -126,6 +136,12 @@ protected: ///> Flag indicating whether a right-click context menu is active bool m_menuActive; + + bool m_filtering; + + ///> List of expanded nodes + std::vector m_expanded; + wxDataViewItem m_selection; }; ///> Custom event sent when a new component is preselected