1#include <Core/CoreMacros.hpp>
2#include <Engine/Rendering/RenderObject.hpp>
3#include <Engine/Rendering/RenderObjectManager.hpp>
4#include <Engine/Scene/Component.hpp>
5#include <Engine/Scene/Entity.hpp>
6#include <Engine/Scene/EntityManager.hpp>
7#include <Gui/TreeModel/EntityTreeModel.hpp>
25 EngineTreeItem* entityItem =
new EngineTreeItem;
28 for (
const auto& comp : ent->getComponents() ) {
30 EngineTreeItem* componentItem =
new EngineTreeItem;
31 componentItem->m_entry =
ItemEntry( ent, comp.get() );
32 componentItem->m_parent = entityItem;
33 for (
const auto& roIdx : comp->m_renderObjects ) {
34 EngineTreeItem* roItem =
new EngineTreeItem;
35 roItem->m_entry =
ItemEntry( ent, comp.get(), roIdx );
36 roItem->m_parent = componentItem;
37 roItem->setChecked( roManager->getRenderObject( roIdx )->isVisible() );
38 componentItem->
m_children.emplace_back( roItem );
40 entityItem->
m_children.emplace_back( componentItem );
48 return static_cast<EngineTreeItem*
>(
getItem(
index ) )->m_entry;
55 while ( !stack.
empty() ) {
56 EngineTreeItem* item =
static_cast<EngineTreeItem*
>( stack.
top() );
59 if ( item->m_entry == entry ) {
65 stack.
push( child.get() );
74 CORE_ASSERT( ent.
isValid(),
"Inserting invalid entry" );
75 CORE_ASSERT( !
findEntryIndex( ent ).isValid(),
"Entry already in model" );
80 QModelIndex parentIdx;
87 CORE_ASSERT( parentIdx.isValid(),
"Parent does not exist" );
88 parentItem =
getItem( parentIdx );
91 CORE_ASSERT(
getItem( parentIdx ) == parentItem,
"Model inconsistency" );
93 EngineTreeItem* childItem =
new EngineTreeItem;
94 childItem->m_entry = ent;
95 childItem->m_parent = parentItem;
97 childItem->setChecked( roManager->getRenderObject( ent.
m_roIndex )->isVisible() );
99 bool checked = roManager->getRenderObject( ent.
m_roIndex )->isVisible();
100 auto pent =
static_cast<EngineTreeItem*
>( parentItem )->m_entry.m_component;
101 for (
const auto& roIdx : pent->getRenderObjects() ) {
102 checked = checked || roManager->getRenderObject( roIdx )->isVisible();
105 parentItem->setChecked( checked );
109 bool checked =
false;
110 for (
const auto& roIdx : ent.
m_component->getRenderObjects() ) {
111 checked = checked || roManager->getRenderObject( roIdx )->isVisible();
113 childItem->setChecked( checked );
116 int row = int( parentItem->
m_children.size() );
117 beginInsertRows( parentIdx, row, row );
118 parentItem->
m_children.emplace_back( childItem );
123void ItemModel::removeItem(
const Engine::Scene::ItemEntry& ent ) {
125 CORE_ASSERT( ent.isValid(),
"Removing invalid entry" );
126 CORE_ASSERT( entryIndex.isValid(),
"Entry not in model" );
127 if ( entryIndex.isValid() ) {
128 EngineTreeItem* toRemove =
static_cast<EngineTreeItem*
>(
getItem( entryIndex ) );
129 TreeItem* parentItem = toRemove->m_parent;
131 const auto childPos =
132 std::find_if( childList.begin(), childList.end(), [toRemove](
const auto& ptr ) {
133 return ptr.get() == toRemove;
136 CORE_ASSERT( childPos != childList.end(),
"Child not in parent's list" );
137 int row = toRemove->getIndexInParent();
138 CORE_ASSERT( childPos - childList.begin() == row,
"Iterator consistency error" );
139 beginRemoveRows(
parent( entryIndex ), row, row );
140 parentItem->m_children.erase( childPos );
145void ItemModel::onDataChanged(
const QModelIndex& topLeft,
146 const QModelIndex& bottomRight,
147 const QVector<int>& roles ) {
148 if ( topLeft == bottomRight && roles.size() == 1 && roles.first() == Qt::CheckStateRole ) {
149 auto index = topLeft;
150 if (
index.isValid() ) {
154 if ( ent.isValid() && ent.isRoNode() ) {
155 emit visibilityROChanged( ent.m_roIndex, visible );
162 CORE_ASSERT( m_parent,
"Looking for the root item's index." );
163 for ( uint i = 0; i < m_parent->
m_children.size(); ++i ) {
164 if ( m_parent->
m_children[i].get() ==
this ) {
return i; }
169 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.