From e6b704f4c47b3b359d4ef3532be38162e7be1afb Mon Sep 17 00:00:00 2001 From: sguionni Date: Wed, 11 Dec 2024 00:11:52 +0100 Subject: [PATCH] Simplify scene tree --- lib/ui/qt/include/ui/qt/dock_widget/scene.hpp | 4 +- lib/ui/qt/src/ui/qt/dock_widget/scene.cpp | 122 ++++++++---------- 2 files changed, 57 insertions(+), 69 deletions(-) diff --git a/lib/ui/qt/include/ui/qt/dock_widget/scene.hpp b/lib/ui/qt/include/ui/qt/dock_widget/scene.hpp index 507d76fe6..271d86e69 100644 --- a/lib/ui/qt/include/ui/qt/dock_widget/scene.hpp +++ b/lib/ui/qt/include/ui/qt/dock_widget/scene.hpp @@ -91,9 +91,7 @@ namespace VTX::UI::QT::DockWidget QPointer _tree; std::map _loadFuncs; - void _addTopLevelData( const TreeItemData &, const LoadFunc & ); - void _addChildLevelData( QTreeWidgetItem * const, const TreeItemData & ); - + void _addTreeItem( const TreeItemData &, std::variant ); void _resetTreeItem( QTreeWidgetItem * const ); }; diff --git a/lib/ui/qt/src/ui/qt/dock_widget/scene.cpp b/lib/ui/qt/src/ui/qt/dock_widget/scene.cpp index 52619c6fd..7edc12e53 100644 --- a/lib/ui/qt/src/ui/qt/dock_widget/scene.cpp +++ b/lib/ui/qt/src/ui/qt/dock_widget/scene.cpp @@ -92,62 +92,56 @@ namespace VTX::UI::QT::DockWidget auto & system = App::ECS_REGISTRY().getComponent( 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(); + _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(); + + 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; } - ) + } ); } }; @@ -155,17 +149,24 @@ namespace VTX::UI::QT::DockWidget _layout->addWidget( _tree.get() ); } - void Scene::_addTopLevelData( const TreeItemData & p_data, const LoadFunc & p_loadFunc ) + void Scene::_addTreeItem( + const TreeItemData & p_data, + std::variant p_parent + ) { - QTreeWidgetItem * item = new QTreeWidgetItem(); - - auto * p = item->parent(); + QTreeWidgetItem * parent = nullptr; + if ( std::holds_alternative( p_parent ) ) + { + parent = std::get( 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 ); @@ -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( p_parent ) ); + _tree->addTopLevelItem( item ); } }