A better algorithm for centering library items in the tree.

It still doesn't center as wxWidgets' API is poorly suited for that.
But it does now at least keep it off the very top or bottom.
This commit is contained in:
Jeff Young 2021-10-02 10:40:12 +01:00
parent 94552ed838
commit 671e9f2dba
2 changed files with 50 additions and 3 deletions

View File

@ -368,11 +368,17 @@ unsigned int LIB_TREE_MODEL_ADAPTER::GetChildren( const wxDataViewItem& aItem,
{ {
const LIB_TREE_NODE* node = ( aItem.IsOk() ? ToNode( aItem ) : &m_tree ); const LIB_TREE_NODE* node = ( aItem.IsOk() ? ToNode( aItem ) : &m_tree );
if( node->m_Type != LIB_TREE_NODE::TYPE::LIBID if( node->m_Type == LIB_TREE_NODE::TYPE::ROOT
|| node->m_Type == LIB_TREE_NODE::LIB
|| ( m_show_units && node->m_Type == LIB_TREE_NODE::TYPE::LIBID ) ) || ( m_show_units && node->m_Type == LIB_TREE_NODE::TYPE::LIBID ) )
{
return IntoArray( *node, aChildren ); return IntoArray( *node, aChildren );
}
else else
{
return 0; return 0;
}
} }

View File

@ -271,8 +271,49 @@ void LIB_TREE::selectIfValid( const wxDataViewItem& aTreeId )
void LIB_TREE::centerIfValid( const wxDataViewItem& aTreeId ) void LIB_TREE::centerIfValid( const wxDataViewItem& aTreeId )
{ {
/*
* This doesn't actually center because the wxWidgets API is poorly suited to that (and
* it might be too noisy as well).
*
* It does try to keep the given item a bit off the top or bottom of the window.
*/
if( aTreeId.IsOk() ) if( aTreeId.IsOk() )
{
LIB_TREE_NODE* node = m_adapter->GetTreeNodeFor( aTreeId );
LIB_TREE_NODE* parent = node->m_Parent;
LIB_TREE_NODE* grandParent = parent ? parent->m_Parent : nullptr;
if( parent )
{
wxDataViewItemArray siblings;
m_adapter->GetChildren( wxDataViewItem( parent ), siblings );
int idx = siblings.Index( aTreeId );
if( idx + 5 < (int) siblings.GetCount() )
{
m_tree_ctrl->EnsureVisible( siblings.at( idx + 5 ) );
}
else if( grandParent )
{
wxDataViewItemArray parentsSiblings;
m_adapter->GetChildren( wxDataViewItem( grandParent ), parentsSiblings );
int p_idx = parentsSiblings.Index( wxDataViewItem( parent ) );
if( p_idx + 1 < (int) parentsSiblings.GetCount() )
m_tree_ctrl->EnsureVisible( parentsSiblings.at( p_idx + 1 ) );
}
if( idx - 5 >= 0 )
m_tree_ctrl->EnsureVisible( siblings.at( idx - 5 ) );
else
m_tree_ctrl->EnsureVisible( wxDataViewItem( parent ) );
}
m_tree_ctrl->EnsureVisible( aTreeId ); m_tree_ctrl->EnsureVisible( aTreeId );
}
} }
@ -303,7 +344,7 @@ LIB_TREE::STATE LIB_TREE::getState() const
wxDataViewItemArray items; wxDataViewItemArray items;
m_adapter->GetChildren( wxDataViewItem( nullptr ), items ); m_adapter->GetChildren( wxDataViewItem( nullptr ), items );
for( const auto& item : items ) for( const wxDataViewItem& item : items )
{ {
if( m_tree_ctrl->IsExpanded( item ) ) if( m_tree_ctrl->IsExpanded( item ) )
state.expanded.push_back( item ); state.expanded.push_back( item );
@ -319,7 +360,7 @@ void LIB_TREE::setState( const STATE& aState )
{ {
m_tree_ctrl->Freeze(); m_tree_ctrl->Freeze();
for( const auto& item : aState.expanded ) for( const wxDataViewItem& item : aState.expanded )
m_tree_ctrl->Expand( item ); m_tree_ctrl->Expand( item );
// wxDataViewCtrl cannot be frozen when a selection // wxDataViewCtrl cannot be frozen when a selection