Skip to content

Commit

Permalink
Simplify scene tree
Browse files Browse the repository at this point in the history
  • Loading branch information
sguionni committed Dec 10, 2024
1 parent e04fc9a commit e6b704f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 69 deletions.
4 changes: 1 addition & 3 deletions lib/ui/qt/include/ui/qt/dock_widget/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ namespace VTX::UI::QT::DockWidget
QPointer<QTreeWidget> _tree;
std::map<QTreeWidgetItem *, LoadFunc> _loadFuncs;

void _addTopLevelData( const TreeItemData &, const LoadFunc & );
void _addChildLevelData( QTreeWidgetItem * const, const TreeItemData & );

void _addTreeItem( const TreeItemData &, std::variant<const LoadFunc, QTreeWidgetItem * const> );
void _resetTreeItem( QTreeWidgetItem * const );
};

Expand Down
122 changes: 56 additions & 66 deletions lib/ui/qt/src/ui/qt/dock_widget/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,80 +92,81 @@ namespace VTX::UI::QT::DockWidget
auto & system = App::ECS_REGISTRY().getComponent<App::Component::Chemistry::System>( p_itemComponent );

// Add with concept.
_addTopLevelData(
TreeItemData { p_itemComponent.getName(),
WidgetData( p_itemComponent.getPersistentSceneID() ),
system.getChains().size() },
LoadFunc(
[ this, &system ]( const uint p_level, QTreeWidgetItem * const p_item )
{
WidgetData parentWidgetData = p_item->data( 0, Qt::UserRole ).value<WidgetData>();
_addTreeItem(
{ p_itemComponent.getName(),
WidgetData( p_itemComponent.getPersistentSceneID() ),
system.getChains().size() },

switch ( p_level )
{
case 0: // Load chains.
[ this, &system ]( const uint p_level, QTreeWidgetItem * const p_item )
{
WidgetData parentWidgetData = p_item->data( 0, Qt::UserRole ).value<WidgetData>();

switch ( p_level )
{
case 0: // Load chains.
{
WidgetData index = 0;
for ( auto & chain : system.getChains() )
{
WidgetData index = 0;
for ( auto & chain : system.getChains() )
{
_addChildLevelData(
p_item, { chain->getName(), index++, chain->getResidueCount() }
);
}
_addTreeItem( { chain->getName(), index++, chain->getResidueCount() }, p_item );
}
break;
}
break;

case 1: // Load residues.
case 1: // Load residues.
{
auto * chain = system.getChain( parentWidgetData );
assert( chain );
for ( size_t index = chain->getIndexFirstResidue(); index <= chain->getIndexLastResidue();
++index )
{
auto * chain = system.getChain( parentWidgetData );
assert( chain );
for ( size_t index = chain->getIndexFirstResidue();
index <= chain->getIndexLastResidue();
++index )
{
auto * residue = system.getResidue( index );
_addChildLevelData(
p_item, { residue->getName(), index, residue->getAtomCount() }
);
}
auto * residue = system.getResidue( index );
_addTreeItem( { residue->getName(), index, residue->getAtomCount() }, p_item );
}
break;
}
break;

case 2: // Load atoms.
case 2: // Load atoms.
{
auto * residue = system.getResidue( parentWidgetData );
assert( residue );
for ( size_t index = residue->getIndexFirstAtom(); index <= residue->getIndexLastAtom();
++index )
{
auto * residue = system.getResidue( parentWidgetData );
assert( residue );
for ( size_t index = residue->getIndexFirstAtom(); index <= residue->getIndexLastAtom();
++index )
{
auto * atom = system.getAtom( atom_index_t( index ) );
_addChildLevelData( p_item, { atom->getName(), index, 0 } );
}
auto * atom = system.getAtom( atom_index_t( index ) );
_addTreeItem( { atom->getName(), index, 0 }, p_item );
}
break;
}
break;

default: assert( true ); break;
}
default: assert( true ); break;
}
)
}
);
}
};

_layout->addWidget( _tree.get() );
}

void Scene::_addTopLevelData( const TreeItemData & p_data, const LoadFunc & p_loadFunc )
void Scene::_addTreeItem(
const TreeItemData & p_data,
std::variant<const LoadFunc, QTreeWidgetItem * const> p_parent
)
{
QTreeWidgetItem * item = new QTreeWidgetItem();

auto * p = item->parent();
QTreeWidgetItem * parent = nullptr;
if ( std::holds_alternative<QTreeWidgetItem * const>( p_parent ) )
{
parent = std::get<QTreeWidgetItem * const>( p_parent );
}

Qt::ItemFlags flags = Qt::ItemFlag::ItemIsSelectable | Qt::ItemFlag::ItemIsUserCheckable;
QTreeWidgetItem * item = new QTreeWidgetItem( parent );
Qt::ItemFlags flags = Qt::ItemFlag::ItemIsSelectable | Qt::ItemFlag::ItemIsUserCheckable;

item->setFlags( flags );
item->setData( 0, Qt::UserRole, QVariant::fromValue( p_data.data ) );
item->setText( 0, QString::fromUtf8( p_data.name ) );
// item->setText( 0, QString::fromUtf8( p_data.name ) );
item->setText( 0, QString::fromStdString( p_data.name.data() ).append( " (%1)" ).arg( p_data.childrenCount ) );
// item->setIcon( 0, *VTX::Style::IconConst::get().getModelSymbol( model.getTypeId() ) );
item->setCheckState( 0, Qt::Checked );

Expand All @@ -174,22 +175,11 @@ namespace VTX::UI::QT::DockWidget
_resetTreeItem( item );
}

assert( not _loadFuncs.contains( item ) );
_loadFuncs.emplace( item, p_loadFunc );

_tree->addTopLevelItem( item );
}

void Scene::_addChildLevelData( QTreeWidgetItem * const p_item, const TreeItemData & p_data )
{
QTreeWidgetItem * child = new QTreeWidgetItem( p_item );
child->setText( 0, QString::fromStdString( p_data.name.data() ).append( " (%1)" ).arg( p_data.childrenCount ) );

child->setData( 0, Qt::UserRole, QVariant::fromValue( p_data.data ) );

if ( p_data.childrenCount != 0 )
if ( not parent )
{
_resetTreeItem( child );
assert( not _loadFuncs.contains( item ) );
_loadFuncs.emplace( item, std::get<const LoadFunc>( p_parent ) );
_tree->addTopLevelItem( item );
}
}

Expand Down

0 comments on commit e6b704f

Please sign in to comment.