1#include <Gui/TreeModel/EntityTreeModel.hpp>
5#include <Engine/Scene/Component.hpp>
6#include <Engine/Scene/Entity.hpp>
8#include <Engine/Rendering/RenderObject.hpp>
9#include <Engine/Rendering/RenderObjectManager.hpp>
10#include <Engine/Scene/EntityManager.hpp>
23 EngineTreeItem* entityItem =
new EngineTreeItem;
26 for (
const auto& comp : ent->getComponents() ) {
28 EngineTreeItem* componentItem =
new EngineTreeItem;
29 componentItem->m_entry =
ItemEntry( ent, comp.get() );
30 componentItem->m_parent = entityItem;
31 for (
const auto& roIdx : comp->m_renderObjects ) {
32 EngineTreeItem* roItem =
new EngineTreeItem;
33 roItem->m_entry =
ItemEntry( ent, comp.get(), roIdx );
34 roItem->m_parent = componentItem;
35 roItem->setChecked( roManager->getRenderObject( roIdx )->isVisible() );
36 componentItem->
m_children.emplace_back( roItem );
38 entityItem->
m_children.emplace_back( componentItem );
46 return static_cast<EngineTreeItem*
>(
getItem(
index ) )->m_entry;
53 while ( !stack.
empty() ) {
54 EngineTreeItem* item =
static_cast<EngineTreeItem*
>( stack.
top() );
57 if ( item->m_entry == entry ) {
63 stack.
push( child.get() );
72 CORE_ASSERT( ent.
isValid(),
"Inserting invalid entry" );
73 CORE_ASSERT( !
findEntryIndex( ent ).isValid(),
"Entry already in model" );
78 QModelIndex parentIdx;
85 CORE_ASSERT( parentIdx.isValid(),
"Parent does not exist" );
86 parentItem =
getItem( parentIdx );
89 CORE_ASSERT(
getItem( parentIdx ) == parentItem,
"Model inconsistency" );
91 EngineTreeItem* childItem =
new EngineTreeItem;
92 childItem->m_entry = ent;
93 childItem->m_parent = parentItem;
95 childItem->setChecked( roManager->getRenderObject( ent.
m_roIndex )->isVisible() );
97 bool checked = roManager->getRenderObject( ent.
m_roIndex )->isVisible();
98 auto pent =
static_cast<EngineTreeItem*
>( parentItem )->m_entry.m_component;
99 for (
const auto& roIdx : pent->getRenderObjects() ) {
100 checked = checked || roManager->getRenderObject( roIdx )->isVisible();
103 parentItem->setChecked( checked );
107 bool checked =
false;
108 for (
const auto& roIdx : ent.
m_component->getRenderObjects() ) {
109 checked = checked || roManager->getRenderObject( roIdx )->isVisible();
111 childItem->setChecked( checked );
114 int row = int( parentItem->
m_children.size() );
115 beginInsertRows( parentIdx, row, row );
116 parentItem->
m_children.emplace_back( childItem );
121void ItemModel::removeItem(
const Engine::Scene::ItemEntry& ent ) {
123 CORE_ASSERT( ent.isValid(),
"Removing invalid entry" );
124 CORE_ASSERT( entryIndex.isValid(),
"Entry not in model" );
125 if ( entryIndex.isValid() ) {
126 EngineTreeItem* toRemove =
static_cast<EngineTreeItem*
>(
getItem( entryIndex ) );
127 TreeItem* parentItem = toRemove->m_parent;
129 const auto childPos =
130 std::find_if( childList.begin(), childList.end(), [toRemove](
const auto& ptr ) {
131 return ptr.get() == toRemove;
134 CORE_ASSERT( childPos != childList.end(),
"Child not in parent's list" );
135 int row = toRemove->getIndexInParent();
136 CORE_ASSERT( childPos - childList.begin() == row,
"Iterator consistency error" );
137 beginRemoveRows(
parent( entryIndex ), row, row );
138 parentItem->m_children.erase( childPos );
143void ItemModel::onDataChanged(
const QModelIndex& topLeft,
144 const QModelIndex& bottomRight,
145 const QVector<int>& roles ) {
146 if ( topLeft == bottomRight && roles.size() == 1 && roles.first() == Qt::CheckStateRole ) {
147 auto index = topLeft;
148 if (
index.isValid() ) {
152 if ( ent.isValid() && ent.isRoNode() ) {
153 emit visibilityROChanged( ent.m_roIndex, visible );
160 CORE_ASSERT( m_parent,
"Looking for the root item's index." );
161 for ( uint i = 0; i < m_parent->
m_children.size(); ++i ) {
162 if ( m_parent->
m_children[i].get() ==
this ) {
return i; }
167 CORE_ASSERT(
false,
" Did not find child in parent" );
Scene::EntityManager * getEntityManager() const
Rendering::RenderObjectManager * getRenderObjectManager() const
Manager getters.
void buildModel() override
Internal function to build the tree.
QModelIndex findEntryIndex(const Engine::Scene::ItemEntry &entry) const
const Engine::Scene::ItemEntry & getEntry(const QModelIndex &index) const
const Engine::RadiumEngine * m_engine
Engine instance.
Base class for element of the tree representation.
int getIndexInParent() const
std::vector< std::unique_ptr< TreeItem > > m_children
Children of item in the tree.
TreeItem * getItem(const QModelIndex &index) const
Get the tree item corresponding to the given index.
QModelIndex parent(const QModelIndex &child) const override
std::unique_ptr< TreeItem > m_rootItem
Root of the tree.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
hepler function to manage enum as underlying types in VariableSet
Ra::Core::Utils::Index m_roIndex
RO index of the represented object.
bool isValid() const
Returns true if the item represents any valid object.
bool isComponentNode() const
Returns true if the item represents a component.
Entity * m_entity
The entity represented by the item, or owning the object represented.
bool isEntityNode() const
Returns true if the item represents an entity.
bool isRoNode() const
Returns true if the item represents a render object.