Component Tree: restore expanded nodes after search query is cancelled

This commit is contained in:
Maciej Suminski 2017-11-21 14:54:30 +01:00
parent 4a942a9810
commit e25a1feb25
4 changed files with 84 additions and 25 deletions

View File

@ -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,

View File

@ -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
*/

View File

@ -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 );

View File

@ -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<wxDataViewItem> m_expanded;
wxDataViewItem m_selection;
};
///> Custom event sent when a new component is preselected