VR-Forces 4.6.1 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Display Metrics (displayMetrics)

The Display Metrics example demonstrates the following:

The Display Metrics example demonstrates how to add a button to the toolbar which will display a new dialog containing scenario metrics. Metrics are updated by sending specially formatted text messages that are processed by the GUI plugin. Metrics are reset on scenario close or rewind. The formatted text messages are sent by lua scripts that are part of the scenario.

Usage

In the Launcher, click the Plug-ins button to bring up the Plug-ins Selection dialog. Enable the plugin. Then launch VR-Forces.

To view the new behavior:

  1. Start vrfGui.
  2. Start vrfSim.
  3. Load the displayMetrics example scenario.
  4. Run the scenario. The UAV will monitor the warehouse area. When vehicles or people arrive or depart, it will update metrics. The latest metrics can be viewed by clicking Show Metrics on the toolbar.
  5. Click the "Show Metrics" button to display the current metrics.

Note that the metrics do not update in the dialog while open. Close and reopen the metrics dialog to view updated metrics.
Note that the metrics dialog will be blank before any metrics are received.

/*******************************************************************************
** Copyright (c) 2016 MAK Technologies, Inc.
** All rights reserved.
*******************************************************************************/
#include <QPushButton>
#include <QDialog>
#include <map>
namespace makVrv
{
class DtDe;
}
class DtString;
class QGridLayout;
class QVBoxLayout;
class QLabel;
: public QDialog
{
public:
DtMetricsDialog(QWidget* parent = 0);
virtual ~DtMetricsDialog();
virtual void initialize(const std::map<DtString, int>& metrics);
virtual void emptyMetric();
protected:
QVBoxLayout* myLayout;
QGridLayout* myMetricsLayout;
struct LabelAndValue
{
};
std::vector<LabelAndValue> myMetrics;
};
: public QPushButton
{
Q_OBJECT
public:
DtMetricsButton( makVrv::DtDe& de, const QString & text, QWidget* parent = 0);
virtual ~DtMetricsButton();
virtual void processTextReport(const DtVrfObjectMessage* msg);
virtual void clearMetrics();
public slots:
protected:
std::map<DtString, int> myMetrics;
};
/*******************************************************************************
** Copyright (c) 2016 MAK Technologies, Inc.
** All rights reserved.
*******************************************************************************/
//\file displayMetricsPlugin.cxx
//This file defines the generic DT_DLL_VRF_PLUGIN_EXPORT_MACRO. Use that macro when
//you want to export symbols. Also, in your CMakeLists.txt file, make sure to
//define the VRFPLUGIN_EXPORTS define to tell the compiler to define the macro as an
//export
#include <vrvCore/DtDe.h>
#include <QtWidgets/QApplication>
#include <QtWidgets/QDialog>
#include <QtWidgets/QLayout>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QLabel>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QToolBar>
#include <vlpi/entityType.h>
#include <boost/bind.hpp>
// Toolbar for holding new pushbuttons
class DtMetricsToolbar : public makVrv::DtToolbar
{
public:
DtMetricsToolbar (makVrv::DtDe& de);
virtual ~DtMetricsToolbar();
virtual QToolBar* createToolbar(makVrv::DtDe&, QWidget* parent);
};
DtMetricsToolbar::DtMetricsToolbar(makVrv::DtDe& de)
: makVrv::DtToolbar(de, std::string("MetricsToolbar") )
{
}
DtMetricsToolbar::~DtMetricsToolbar()
{
}
{
QToolBar* tb = new QToolBar("Metrics", parent);
tb->setWindowTitle("Metrics Toolbar");
return tb;
}
// Dialog for showing metrics
: QDialog(parent, Qt::Tool)
{
setWindowTitle("Metrics");
// Create the top level layout for the dialog as well as a
// child layout which will contain the metrics themselves.
myLayout = new QVBoxLayout(this);
myMetricsLayout = new QGridLayout;
myLayout->addLayout(myMetricsLayout);
// Create the close button and position to the right of
// dialog using a layout and a spacer.
QHBoxLayout* closeLayout = new QHBoxLayout;
QSpacerItem* closeSpacer = new QSpacerItem(100, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);
closeLayout->addItem(closeSpacer);
myCloseButton = new QPushButton("Close", this);
closeLayout->addWidget(myCloseButton);
myLayout->addLayout(closeLayout);
QSpacerItem* heightSpacer = new QSpacerItem(200, 0, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
myLayout->addItem(heightSpacer);
// Dialog should be hidden when close button is pressed.
connect(myCloseButton, SIGNAL(released()),
this, SLOT(hide()));
}
{
}
void DtMetricsDialog::initialize(const std::map<DtString, int>& metrics)
{
// Clear out existing metrics
std::vector<LabelAndValue>::iterator lvIter = myMetrics.begin();
std::vector<LabelAndValue>::iterator lvEnd = myMetrics.end();
for (; lvIter != lvEnd; ++lvIter)
{
myMetricsLayout->removeWidget(lvIter->lbl);
myMetricsLayout->removeWidget(lvIter->val);
delete lvIter->lbl;
delete lvIter->val;
}
myMetrics.clear();
// Update metrics with new values
if ( metrics.size() == 0 )
std::map<DtString, int>::const_iterator mIter = metrics.begin();
std::map<DtString, int>::const_iterator mEnd = metrics.end();
for (int i = 0; mIter != mEnd; ++mIter, ++i)
{
LabelAndValue lv;
lv.lbl = new QLabel(mIter->first.c_str(), this);
lv.val = new QLabel(QString::number(mIter->second), this);
myMetricsLayout->addWidget(lv.lbl, i, 0);
myMetricsLayout->addWidget(lv.val, i, 1);
myMetrics.push_back(lv);
}
}
{
LabelAndValue lv;
lv.lbl = new QLabel("Awaiting Input", this);
lv.val = new QLabel(0, this);
myMetricsLayout->addWidget(lv.lbl, 0, 0);
myMetricsLayout->addWidget(lv.val, 0, 1);
myMetrics.push_back(lv);
}
// Push button for showing metrics
const QString & text, QWidget* parent /* = 0 */)
: QPushButton(text, parent)
, myDe(de)
{
connect(
this, SIGNAL(clicked()),
this, SLOT(slot_onClicked()) );
// Register connections for signals indicating that the current scenario
// has closed or been rewound. When a scenario is closed, we will clear our
// metrics.
// Create the dialog and keep it hidden for now.
myDialog = new DtMetricsDialog(this);
myDialog->hide();
}
{
};
{
// Update the metrics and show the dialog.
myDialog->show();
}
{
myMetrics.clear();
}
{
// Examine text messages looking for metrics reports.
{
const DtReportMessage *repMsg = static_cast<const DtReportMessage*>(msg);
if (repMsg->report()->type() == DtTextReportType)
{
DtTextReport* textRep = static_cast<DtTextReport*>(repMsg->report());
// Text messages containing a metric update contain three fields
// separated by the token ';'. The first field will be the text
// "[Metric]". The second is the name of the metric. The third is
// the amount by which the metric should be updated (can be positive
// or negative).
std::vector<DtString> tokens;
textRep->text().tokenize(';', tokens);
if (tokens.size() == 3 && tokens[0] == "[Metric]")
{
std::map<DtString, int>::iterator mIter =
myMetrics.find(tokens[1]);
if (mIter == myMetrics.end())
{
myMetrics.insert(std::make_pair(tokens[1], tokens[2].toInt()));
}
else
{
mIter->second += tokens[2].toInt();
}
// Update the dialog
}
}
}
}
class DtMetricsButtonBuilder : public makVrv::DtWidgetBuilder
{
public:
DtMetricsButtonBuilder(makVrv::DtDe& de);
virtual ~DtMetricsButtonBuilder();
virtual QWidget* create(makVrv::DtDe& de, QWidget* parent,
const Qt::WindowFlags& flags = 0);
};
//......................................................................
DtMetricsButtonBuilder::DtMetricsButtonBuilder(makVrv::DtDe& de)
: DtWidgetBuilder()
{
}
DtMetricsButtonBuilder::~DtMetricsButtonBuilder()
{
}
QWidget* DtMetricsButtonBuilder::create( makVrv::DtDe& de, QWidget* parent,
const Qt::WindowFlags& flags)
{
return new DtMetricsButton(de, "Show Metrics", parent);
}
void setupNewToolbar(makVrv::DtVrvApplication& app, makVrv::DtDe& de)
{
makVrv::DtToolbar * tb = new DtMetricsToolbar(de);
DtMetricsButtonBuilder* bwb = new DtMetricsButtonBuilder(de);
"ShowMetrics", bwb);
makVrv::DtToolbarPath::toolbar("MetricsToolbar").widget("ShowMetrics"));
}
{
}
void setupInterface(makVrv::DtVrvApplication& app, makVrv::DtDe& de)
{
//Create the qApplication if it has not already been created
if (!qApp)
{
appProvider.qAppInstance();
}
setupNewToolbar(app, de);
}
{
if(igApp)
{
setupInterface(*igApp, *de);
return true;
}
else
{
return false;
}
}
{
info.pluginName = "Display Metrics";
info.pluginDescription = "Compiles and display metrics received via specially formatted text messages.";
info.pluginVersion = "1.00";
info.pluginCreator = "MAK Technologies";
info.pluginCreatorEmail = "sales@mak.com";
info.pluginContactWebPage = "www.mak.com";
info.pluginContactMailingAddress = "150 Cambridge Park Drive 3rd Floor - Cambridge, MA 02140 USA";
info.pluginContactPhone = "(617) 876 8085";
}

Document ID: Generated on Wed Jul 25 16:57:45 EDT 2018 from SVN revision 190790
Copyright © 2005-2018 VT MÄK. All Rights Reserved (www.mak.com)