Hi,
I am trying to create a QTreeWidget to represent the scene graph in a
hierarchical tree view. I used the thread
http://forum.openscenegraph.org/viewtopic.php?t=4486 as a starting point.
The code used to create the tree nodes is:
Code:
void BuildTreeNodeVisitor::CreateSubTree(osg::NodePath list)
{
QTreeWidgetItem* subTree = _tree->topLevelItem(0);
for(osg::NodePath::iterator pitr=list.begin(); pitr!=list.end(); ++pitr)
//iterate over the entire nodePath from the node visitor iterator
{
std::string name = (*pitr)->getName();
bool find =false;
if (!subTree)
//if no item is present at the top of the tree
//set the top most QTreeItem as the first element pointed to by pitr
{
QTreeWidgetItem *added = new QTreeWidgetItem();
//added->setText(0, QString(name.c_str()) );
added->setText(0, QString::fromLocal8Bit("SCENEGRAPH:"));
_tree->addTopLevelItem (added);
subTree = _tree->topLevelItem(0);
}
//loop through tree to check if name exists in tree
for (int i = 0; i < subTree->childCount(); i++)
{
if (subTree->child(i)->text(0)==QString::fromStdString(name))
//name already exists i.e. node already exists, do not add duplicate
{
find = true;
subTree = subTree->child(i);
break;
}
}
if (!find)
//node not found in the node path, create new item and add to tree
{
QTreeWidgetItem *added = new QTreeWidgetItem();
added->setText(0, QString::fromLocal8Bit (name.c_str()));
subTree->addChild (added);
}
}
return;
}
This works fine until the scene graph has more than 1 node with the same name.
If I load two cow.osg nodes, the tree (as expected) shows only one.
To work around I tried to set userData by setting it to false for every newly
loaded node and then setting it to true when the node got added to tree.
However, this has its own problems as the nodes (Geodes) lying underneath the
loaded osg::ref_ptr<Osg::Node> do not have the userData set. Which makes this
rather clumsy:
Code:
void BuildTreeNodeVisitor::CreateSubTree(osg::NodePath list)
{
QTreeWidgetItem* subTree = _tree->topLevelItem(0);
for(osg::NodePath::iterator pitr=list.begin(); pitr!=list.end(); ++pitr)
//iterate over the entire nodePath from the node visitor iterator
{
std::string name = (*pitr)->getName();
bool find = false;
bool addedToTree;
if(dynamic_cast<NodeAddedToTreeUserData*>((*pitr)->getUserData()) == NULL)
{
(*pitr)->setUserData(new NodeAddedToTreeUserData(false));
addedToTree = false;
}
else
{
addedToTree = dynamic_cast<NodeAddedToTreeUserData*>
((*pitr)->getUserData())->addedToTree;
}
int currentChildIndex;
for (currentChildIndex = 0; currentChildIndex < subTree->childCount();
currentChildIndex++)
{
if
(subTree->child(currentChildIndex)->text(0)==QString::fromStdString(name))
//name already exists i.e. node already exists, do not add duplicate
{
if(addedToTree == true)
{
find = true;
subTree = subTree->child(currentChildIndex);
break;
}
}
}
if (!find)
//node not found in the node path, create new item and add to tree
{
QTreeWidgetItem *added = new QTreeWidgetItem();
added->setText(0, QString::fromLocal8Bit (name.c_str()));
subTree->addChild (added);
(*pitr)->setUserData(new NodeAddedToTreeUserData(true));
}
//check for geodes that do not have user data set and if not added to the
tree
if(find && dynamic_cast<NodeAddedToTreeUserData*>((*pitr)->getUserData())
== NULL)
{
QTreeWidgetItem *added = new QTreeWidgetItem();
added->setText(0, QString::fromLocal8Bit (name.c_str()));
subTree->parent()->addChild(added);
(*pitr)->setUserData(new NodeAddedToTreeUserData(true));
}
}
return;
}
I was having trouble figuring out the best way to solve this issue of creating
a QTreeWidget that can represent any scene graph even having multiple nodes
with same names. Any suggestions would be most welcome
Thanks
Sincerely,
Sanat
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=42169#42169
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org