VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Entity List (entityListClass)

The DtEntityList class is a Qt-based dialog/ widget class that displays all existing entities as well as their current task and, if available, their destination.


Header file:

/*******************************************************************************
** Copyright (c) 2011 MAK Technologies, Inc.
** All rights reserved.
*******************************************************************************/
//\file DtEntityList.h
//\brief Contains makVrf::makVrfEntityListExample::DtEntityList class.
#pragma once
#include "entityListUi.hpp"
namespace makVrv
{
class DtDe;
}
namespace makVrfEntityListExample
{
//A widget class that displays all existing entities
//as well as their current task and, if available, their destination.
class DtEntityList : public makVrv::DtPage
{
public:
DtEntityList( makVrv::DtDe& de, QWidget* parent = 0, Qt::WindowFlags flags = 0);
virtual ~DtEntityList() override;
//Get the icon of the page.
virtual QIcon icon() override;
//Get the title of the page.
virtual QString title() override;
//Get the class name of the page.
virtual const std::string& pageClassName() const override;
//Called when the page is displayed.
virtual void activate() override;
//Called when the page is hidden.
virtual void deactivate() override;
static std::string thePageClassName();
//Goes through the entire list of entities and populates the tree
virtual void populateEntityList();
protected:
//Sets up the contents of the widget onto the supplied parent
void setupUi(QWidget* p);
//Clears out widget
virtual void clear();
protected:
virtual void slot_onCurrentTaskChanged(DtUUID entityName);
//Using the known task, fill in the destination line in the entity list
void fillDestinationLine(DtSimTask* task,QTreeWidgetItem* displayEntityItem);
public:
Ui_DtEntityListUi myUi;
//This maps keeps track of each line in the target list
struct LineToStateView
{
boost::signals2::scoped_connection connection;
};
std::map<makVrv::DtUniqueID, LineToStateView> myLineToStateViewMap;
};
//Create a "creator" helper class using the template.
}

Implementation:

/*******************************************************************************
** Copyright (c) 2011 MAK Technologies, Inc.
** All rights reserved.
*******************************************************************************/
//\file DtEntityList.cxx
//\brief Contains makVrf3DEntityListExample::DtEntityList class.
#include "DtEntityList.h"
#include <vlpi/appearance.h>
#include <QtGui/QPixmap>
using namespace makVrf;
namespace makVrfEntityListExample
{
DtEntityList::DtEntityList( makVrv::DtDe& de, QWidget* parent, Qt ::WindowFlags flags)
: DtPage(de, parent, flags)
{
setObjectName(tr("Entity List"));
setupUi(this);
myUi.myEntityTree->setRootIsDecorated(false);
//The first thing to do is to populate my entity list with all existing entities.
populateEntityList();
//Set up connections
//From now on, all new entities are called through these slots.
myConnections += DtVrfGuiSimObjectManager::instance(myDe).signal_objectAdded.connect(
boost::bind(&DtEntityList::slot_onObjectAdded, this, std::placeholders::_1));
myConnections += DtVrfGuiSimObjectManager::instance(myDe).signal_objectDeleted.connect(
boost::bind(&DtEntityList::slot_onObjectAboutToBeRemoved, this, std::placeholders::_1));
myConnections += DtVrfCurrentTaskManager::instance(de).signal_currentTaskChanged.connect(
boost::bind(&DtEntityList::slot_onCurrentTaskChanged, this, std::placeholders::_1));
// Go through all the currently known objects and add them
DtVrfGuiSimObjectManager::SimObjects::const_iterator iter = DtVrfGuiSimObjectManager::instance(myDe).simObjects().begin();
DtVrfGuiSimObjectManager::SimObjects::const_iterator end = DtVrfGuiSimObjectManager::instance(myDe).simObjects().end();
while (iter != end)
{
slot_onObjectAdded(iter->second);
++iter;
}
}
DtEntityList::~DtEntityList()
{
}
QIcon DtEntityList::icon()
{
return QIcon();
}
makVrv::DtQIconConfigurations DtEntityList::qIconConfigurations() const
{
return result;
}
QString DtEntityList::title()
{
return tr("Entity List");
}
const std::string& DtEntityList::pageClassName() const
{
static std::string theClassName = "DtEntityList";
return theClassName;
}
void DtEntityList::activate()
{
}
void DtEntityList::deactivate()
{
}
std::string DtEntityList::thePageClassName()
{
return "DtEntityList";
}
void DtEntityList::setupUi(QWidget* p)
{
myUi.setupUi(this);
}
void DtEntityList::clear()
{
myUi.myEntityTree->clear();
}
void DtEntityList::populateEntityList()
{
//The list from the DtVrfStateViewCollectionManager will contain
//all the entities that are currently on the network. Initially,
//this function populates our list, but it gets updated by using the
//callback functions below
for(auto& object : DtVrfGuiSimObjectManager::instance(myDe).simObjects())
{
slot_onObjectAdded(object.second);
}
}
void DtEntityList::slot_onObjectAdded(DtSimObjectReference state)
{
{
return;
}
if(myLineToStateViewMap.find(state->id()) != myLineToStateViewMap.end())
{
return; //This object has already been added.
}
QTreeWidgetItem* displayEntityItem = new QTreeWidgetItem(myUi.myEntityTree);
//Subscribe to object updates so we can grab the name of an entity when it updates.
//Note that it is possible that at this point the entity name is blank which is why
//we need to wait for an update.
myLineToStateViewMap[state->id()].connection = state->signal_modified.connect(
boost::bind(&DtEntityList::slot_onObjectUpdated, this, std::placeholders::_1));
//Force the initial update. Note that it is possible that this initial update is incomplete
myLineToStateViewMap[state->id()].item = displayEntityItem;
slot_onObjectUpdated(state);
}
void DtEntityList::slot_onObjectAboutToBeRemoved(DtSimObjectReference state)
{
std::map<makVrv::DtUniqueID, LineToStateView>::iterator iter = myLineToStateViewMap.find(state->id());
if (iter == myLineToStateViewMap.end())
{
return;
}
iter->second.connection.disconnect();
//Remove deleted entity from gui.
QTreeWidgetItem* displayEntityItem = iter->second.item;
if(displayEntityItem)
{
myUi.myEntityTree->takeTopLevelItem(
myUi.myEntityTree->indexOfTopLevelItem(displayEntityItem));
}
myLineToStateViewMap.erase(iter);
}
void DtEntityList::slot_onObjectUpdated(DtSimObjectReference entityState)
{
std::map<makVrv::DtUniqueID, LineToStateView>::iterator iter = myLineToStateViewMap.find(entityState->id());
if(iter != myLineToStateViewMap.end())
{
QTreeWidgetItem* displayEntityItem = iter->second.item;
if(displayEntityItem->text(0).isEmpty())
{
DtVrfCurrentTaskManager::instance(myDe).requestCurrentTasksFor(entityState->id(),
boost::bind(&DtEntityList::slot_onCurrentTaskChanged, this, std::placeholders::_1));
//In order to get the task of an object we need to subscribe to the task manager
//Note that we do this here instead of when the object is added because the
//DtVrfCurrentTaskManager will not work until after an update has
//identified the object name.
DtVrfCurrentTaskManager& manager = DtVrfCurrentTaskManager::instance(DtPage::myDe);
manager.subscribeForTaskInformation(entityState->id());
}
if(DtString(displayEntityItem->text(0).toLocal8Bit().constData()) != entityState->objectName())
{
displayEntityItem->setText(0, QString::fromLocal8Bit(entityState->objectName().c_str()));
}
//Get the icon for this item. If the serial numbers are different, then set the icon
bool destroyed = false;
makVrf::DtEntityMapperKey key(entityState->forceType(), entityState->entityType());
destroyed = entityState->isDestroyed();
QPixmap pMap = makVrf::DtVrfGuiSymbolMapper::instance(DtPage::myDe).getIcon(
key, destroyed, 48, false, entityState);
if (pMap.cacheKey() != displayEntityItem->icon(0).cacheKey())
{
displayEntityItem->setIcon(0, pMap);
}
}
}
void DtEntityList::slot_onCurrentTaskChanged(DtUUID entityName)
{
if(!entityName.isValid()) return;
DtSimObjectReference entityState = DtVrfGuiSimObjectManager::instance(myDe).lookup(entityName);
if (!entityState.isValid())
{
return;
}
std::map<makVrv::DtUniqueID, LineToStateView>::iterator iter = myLineToStateViewMap.find(entityState->id());
if(iter != myLineToStateViewMap.end())
{
QTreeWidgetItem* displayEntityItem = iter->second.item;
//Use the task manager to grab the current task and display it.
DtVrfCurrentTaskManager& manager = DtVrfCurrentTaskManager::instance(DtPage::myDe);
const DtVrfCurrentTaskManager::EntityTaskInformation* allTasks = manager.tasksFor(entityState->id());
if (allTasks)
{
if(allTasks->size() == 0)
{
//No task
displayEntityItem->setText(1,"");
displayEntityItem->setText(2,"");
}
else
{
DtSimTaskMessageType taskType = ((*allTasks)[0]).task->type();
displayEntityItem->setText(1,QString::fromUtf8(taskType.type().c_str()));
fillDestinationLine(((*allTasks)[0]).task.get(),displayEntityItem);
}
}
}
}
void DtEntityList::fillDestinationLine(DtSimTask* task,QTreeWidgetItem* displayEntityItem)
{
DtSimTaskMessageType taskType = task->type();
displayEntityItem->setText(2, "");
{
DtScriptedTaskTask* stt = dynamic_cast<DtScriptedTaskTask*>(task);
if (stt)
{
DtRwVector* rwv = dynamic_cast<DtRwVector*>(stt->variables().findVariableBinding("destination"));
if (rwv)
{
displayEntityItem->setText(2, QString::fromUtf8(formatSourceToDisplay(*rwv).c_str()));
}
}
}
{
DtUUID controlPoint(DtUUID::nullUUID());
DtVector aimingPoint;
//For location destinations, display the ending point and the name of the waypoint.
DtMoveToTask* locTask = dynamic_cast<DtMoveToTask*>(task);
if (locTask)
{
controlPoint = locTask->controlPoint();
aimingPoint = locTask->aimingPoint();
}
else
{
DtScriptedTaskTask* stt = dynamic_cast<DtScriptedTaskTask*>(task);
if (stt)
{
DtRwUUID* cp = dynamic_cast<DtRwUUID*>(stt->variables().findVariableBinding("destination"));
if (cp)
{
controlPoint = *cp;
}
}
}
if (controlPoint.isValid())
{
DtSimObjectReference sv = DtVrfGuiSimObjectManager::instance(myDe).findStateView(controlPoint);
if (sv.isValid())
{
aimingPoint = sv->worldPosition();
}
}
if (aimingPoint != DtVector::zero())
{
//Found the named waypoint
QString destinationText =
QString::fromUtf8(formatSourceToDisplay(aimingPoint)) + " (" + QString::fromUtf8(controlPoint.markingText()) + ")";
displayEntityItem->setText(2,destinationText);
}
}
}
}

Document ID: Generated on Thu Oct 23 22:29:17 EDT 2025 from SVN revision 280951
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)