VR-Forces 4.0.4 Class Documentation

remoteControlCommands.h

/******************************************************************************
** Copyright (c) 2012 MAK Technologies, Inc.
** All rights reserved.
******************************************************************************/
/******************************************************************************
** $RCSfile: remoteControlCommands.h,v $ $Revision: 1.2 $ $State: Exp $
******************************************************************************/
//\file remoteControlCommands.h

#pragma once

#include <vrvUtil/signalslib.h>
#include <vlpi/entityType.h>

#include <QtCore/QStringList>

namespace makVrv
{
   class DtDe;
}

class QString;
class DtEntityIdentifier;

//These classes will represent commands that can be executed by using the
//DtVrossThreadVrfRemoteControl class.

class DtRemoteControlCommand
{
public:
   virtual bool execute(makVrv::DtDe&, const QString& command) = 0;
   virtual DtRemoteControlCommand* clone() const = 0;

   QString help() const
   {
      return myHelp;
   }

   QString command() const
   {
      return myCommand;
   }

   boost::signal<void (const QString&, DtRemoteControlCommand*)> signal_commandComplete;

protected:
   //Parse out the string.  All tokens are separated by spaces or enclosed in quotes or
   //parens
   virtual QStringList tokenize(const QString&, char delim = ' ') const;

   //Parses a point of the form x,y,z
   bool parsePoint(const QString&, DtVector&) const;

protected:
   DtRemoteControlCommand(const QString& command, const QString& help);
   DtRemoteControlCommand(const DtRemoteControlCommand&);

protected:
   QString myCommand;
   QString myHelp;
};

//This command will create a single point object (entity, waypoint, aggregate) and will notify the user
//when the command has been completed
class DtRemoteControlCreateCommand : public DtRemoteControlCommand
{
public:
   DtRemoteControlCreateCommand();

   virtual ~DtRemoteControlCreateCommand();

   virtual bool execute(makVrv::DtDe&, const QString& command);
   virtual DtRemoteControlCommand* clone() const;

   //Convert string to entity type either by name or by actual type
   DtEntityType entityToCreate(const QString& c, makVrv::DtDe& de) const;

   //Returns the currently selected simulation address
   DtSimulationAddress simulationAddress(makVrv::DtDe&) const;

protected:
   //Called when the object is created, will signal complete with the name and the object id
   void objectCreated(DtString, const DtEntityIdentifier&);
};

//Loads the specified scenario into the simulation

class DtRemoteControlLoadScenarioCommand : public DtRemoteControlCommand
{
public:
   DtRemoteControlLoadScenarioCommand();
   virtual ~DtRemoteControlLoadScenarioCommand();

   virtual bool execute(makVrv::DtDe&, const QString& command);
   virtual DtRemoteControlCommand* clone() const;
};

//Loads a new scenario into the system

class DtRemoteControlNewScenarioCommand : public DtRemoteControlCommand
{
public:
   DtRemoteControlNewScenarioCommand();
   virtual ~DtRemoteControlNewScenarioCommand();

   virtual bool execute(makVrv::DtDe&, const QString& command);
   virtual DtRemoteControlCommand* clone() const;
};

//Rewinds the current scenario

class DtRemoteControlRewindScenarioCommand : public DtRemoteControlCommand
{
public:
   DtRemoteControlRewindScenarioCommand();
   virtual ~DtRemoteControlRewindScenarioCommand();

   virtual bool execute(makVrv::DtDe&, const QString& command);
   virtual DtRemoteControlCommand* clone() const;
};

//Pauses the current scenario

class DtRemoteControlPauseScenarioCommand : public DtRemoteControlCommand
{
public:
   DtRemoteControlPauseScenarioCommand();
   virtual ~DtRemoteControlPauseScenarioCommand();

   virtual bool execute(makVrv::DtDe&, const QString& command);
   virtual DtRemoteControlCommand* clone() const;
};

//Plays the current scenario

class DtRemoteControlPlayScenarioCommand : public DtRemoteControlCommand
{
public:
   DtRemoteControlPlayScenarioCommand();
   virtual ~DtRemoteControlPlayScenarioCommand();

   virtual bool execute(makVrv::DtDe&, const QString& command);
   virtual DtRemoteControlCommand* clone() const;
};

//Saves the current scenario

class DtRemoteControlSaveScenarioCommand : public DtRemoteControlCommand
{
public:
   DtRemoteControlSaveScenarioCommand();
   virtual ~DtRemoteControlSaveScenarioCommand();

   virtual bool execute(makVrv::DtDe&, const QString& command);
   virtual DtRemoteControlCommand* clone() const;
};

remoteControlCommands.cxx

/******************************************************************************
** Copyright (c) 2012 MAK Technologies, Inc.
** All rights reserved.
******************************************************************************/
/******************************************************************************
** $RCSfile: remoteControllCommands.cxx,v $ $Revision: 1.2 $ $State: Exp $
******************************************************************************/
//\file remoteControlCommands.cxx

#include "remoteControlCommands.h"

#include <vrvCore/DtDe.h>
#include <vrvCore/DtDeSharedState.h>
#include <vrfGuiCore/DtGuiThreadVrfRemoteController.h>
#include <vrfGuiCore/DtVrfSimBackEndManager.h>
#include <vrvUtil/DtCoordinateConverter.h>
#include <vrvUtil/DtCoordinateSystem.h>
#include <vrfGuiCoreQt/DtCreateMenu.h>
#include <vrfGuiCoreQt/DtVrfSaveScenarioHandler.h>

#include <QtCore/QString>
#include <QtCore/QObject>

using namespace makVrf;
using namespace makVrv;

DtRemoteControlCommand::DtRemoteControlCommand(const QString& command, const QString& help)
: myCommand(command)
, myHelp(help)
{
}

DtRemoteControlCommand::DtRemoteControlCommand(const DtRemoteControlCommand& orig)
: myCommand(orig.myCommand)
, myHelp(orig.myHelp)
{
}

bool DtRemoteControlCommand::parsePoint(const QString& c, DtVector& v) const
{
   QStringList vals = c.split(',');

   if (vals.size() != 3)
   {
      return false;
   }

   v.setX(atof(vals[0].trimmed().toLatin1().constData()));
   v.setY(atof(vals[1].trimmed().toLatin1().constData()));
   v.setZ(atof(vals[2].trimmed().toLatin1().constData()));

   return true;
}

QStringList DtRemoteControlCommand::tokenize(const QString& c, char delim) const
{
   int iLimit = c.length();
   int i;
   QString token;
   QStringList tokens;
   bool inQuote = false;
   int inParen = 0;

   for (i = 0; i < iLimit; i++)
   {
      if (c[i] == '"')
      {
         if (inParen == 0)
         {
            if (!inQuote)
            {
               inQuote = true;
            }
            else
            {
               if (token.length())
               {
                  tokens += token;
               }
               token = "";
               inQuote = false;
            }
         }
      }
      else if (c[i] == '(')
      {
         if (!inQuote)
         {
            inParen++;
         }
      }
      else if (c[i] == ')')
      {
         if (!inQuote)
         {
            inParen--;

            if (inParen == 0)
            {
               if (token.length())
               {
                  tokens += token;
               }
               token = "";
            }
         }
      }
      else if (c[i] == delim)
      {
         if (!inQuote && (inParen == 0))
         {
            if (token.length())
            {
               tokens += token;
            }
            token = "";
         }
         else
         {
            token += c[i];
         }
      }
      else
      {
         token += c[i];
      }
   }

   if (token.length())
   {
      tokens += token;
   }

   return tokens;
}

//************************************** Create command
DtRemoteControlCreateCommand::DtRemoteControlCreateCommand()
: DtRemoteControlCommand("create", QObject::tr("create <object name|object type> <force number> <x,y,z> [simulation address]"))
{
}

DtRemoteControlCreateCommand::~DtRemoteControlCreateCommand()
{
}

DtEntityType DtRemoteControlCreateCommand::entityToCreate(const QString& c, DtDe& de) const
{
   QStringList vals = c.split(',');
   DtEntityType type;

   if (vals.size() == 7)
   {
      type = DtEntityType(c.toLatin1().constData());
   }
   else
   {
      type = DtCreateMenu::instance(de).nameToType(c);
   }

   return type;
}

DtSimulationAddress DtRemoteControlCreateCommand::simulationAddress(DtDe& de) const
{
   return DtGuiThreadVrfRemoteController::instance(de).backendManager().selectedBackend();
}

bool DtRemoteControlCreateCommand::execute(makVrv::DtDe& de, const QString& command)
{
   QStringList tokens = tokenize(command);
   int iLimit = tokens.size();

   if (iLimit < 4)
   {
      signal_commandComplete(help(), this);
      return false;
   }

   DtEntityType type = entityToCreate(tokens[1], de);

   if (type == DtEntityType())
   {
      QString error = QObject::tr("Cannot create entity of type ") + tokens[1];
      signal_commandComplete(error, this);

      return false;
   }

   int force = atoi(tokens[2].toLatin1().constData());

   DtVector point;

   if (!parsePoint(tokens[3], point))
   {
      QString error = QObject::tr("Location format is incorrect ") + tokens[2];
      signal_commandComplete(error, this);

      return false;
   }

   DtSimulationAddress simAddress;

   if (iLimit == 5)
   {
      simAddress = DtSimulationAddress(tokens[5].toLatin1().constData());
   }
   else
   {
      simAddress = simulationAddress(de);
   }

   //Check to see if the specified backend can simulate.  If not then abort
   if (!DtGuiThreadVrfRemoteController::instance(de).backendManager().backendCanSimulate(simAddress))
   {
      signal_commandComplete(QObject::tr("Backend specified is not ready to simulate: ") + simAddress.string(), this);
      return false;
   }

   DtVector position;

   de.sharedState().coordinateSystem().localToNetPos( point, position );

   //Convert the local position to a geocentric position
   //Create the entity and print out the name and entity id
   //This is where the remote controller is used to create the entity object
   DtGuiThreadVrfRemoteController::instance(de).createEntity(boost::bind(&DtRemoteControlCreateCommand::objectCreated, this, _1, _2),
      type, position, (DtForceType)force, 0.0, "", "", simAddress);

   return true;
}

void DtRemoteControlCreateCommand::objectCreated(DtString s, const DtEntityIdentifier& id)
{
   QString message = QObject::tr("Object ") + s.c_str();

   message += QObject::tr(" with id ") + id.string();
   message += QObject::tr(" has been created.");

   signal_commandComplete(message, this);
}

DtRemoteControlCommand* DtRemoteControlCreateCommand::clone() const
{
   return new DtRemoteControlCreateCommand;
}

//************************ Load Scenario Command

DtRemoteControlLoadScenarioCommand::DtRemoteControlLoadScenarioCommand()
: DtRemoteControlCommand("load", QObject::tr("load <scenario name>"))
{
}

DtRemoteControlLoadScenarioCommand::~DtRemoteControlLoadScenarioCommand()
{
}

bool DtRemoteControlLoadScenarioCommand::execute(makVrv::DtDe& de, const QString& command)
{
   QStringList tokens = tokenize(command);

   if (tokens.size() != 2)
   {
      signal_commandComplete(help(), this);
      return false;
   }

   if (!DtGuiThreadVrfRemoteController::instance(de).loadScenario(tokens[1].toLatin1().constData()))
   {
      signal_commandComplete(QObject::tr("Could not load scenario file ") + tokens[1], this);
      return false;
   }

   signal_commandComplete(QObject::tr("Loading scenario ") + tokens[1], this);

   return true;
}

DtRemoteControlCommand* DtRemoteControlLoadScenarioCommand::clone() const
{
   return new DtRemoteControlLoadScenarioCommand;
}

//************************ New Scenario Command

DtRemoteControlNewScenarioCommand::DtRemoteControlNewScenarioCommand()
: DtRemoteControlCommand("new", QObject::tr("new <terrain database>"))
{
}

DtRemoteControlNewScenarioCommand::~DtRemoteControlNewScenarioCommand()
{
}

bool DtRemoteControlNewScenarioCommand::execute(makVrv::DtDe& de, const QString& command)
{
   QStringList tokens = tokenize(command);

   if (tokens.size() != 2)
   {
      signal_commandComplete(help(), this);
      return false;
   }

   DtGuiThreadVrfRemoteController::instance(de).newScenario(tokens[1].toLatin1().constData());

   signal_commandComplete(QObject::tr("Creating new scenario ") + tokens[1], this);

   return true;
}

DtRemoteControlCommand* DtRemoteControlNewScenarioCommand::clone() const
{
   return new DtRemoteControlNewScenarioCommand;
}

//************************ Save Scenario Command

DtRemoteControlSaveScenarioCommand::DtRemoteControlSaveScenarioCommand()
: DtRemoteControlCommand("save", QObject::tr("save <scenario name>"))
{
}

DtRemoteControlSaveScenarioCommand::~DtRemoteControlSaveScenarioCommand()
{
}

bool DtRemoteControlSaveScenarioCommand::execute(makVrv::DtDe& de, const QString& command)
{
   QStringList tokens = tokenize(command);

   if (tokens.size() != 2)
   {
      signal_commandComplete(help(), this);
      return false;
   }

   //The scenario handler will call down to the cross thread remote controller's save scenario method, however,
   //always use this method to save the current scenario as the handler will fill in all the relevant scenario
   //information
   DtVrfSaveScenarioHandler::instance(de).saveScenario(tokens[1].toLatin1().constData());

   signal_commandComplete(QObject::tr("Saving scenario ") + tokens[1], this);

   return true;
}

DtRemoteControlCommand* DtRemoteControlSaveScenarioCommand::clone() const
{
   return new DtRemoteControlSaveScenarioCommand;
}

//************************ Rewind Scenario Command

DtRemoteControlRewindScenarioCommand::DtRemoteControlRewindScenarioCommand()
: DtRemoteControlCommand("rewind", QObject::tr("rewind"))
{
}

DtRemoteControlRewindScenarioCommand::~DtRemoteControlRewindScenarioCommand()
{
}

bool DtRemoteControlRewindScenarioCommand::execute(makVrv::DtDe& de, const QString& command)
{
   QStringList tokens = tokenize(command);

   if (tokens.size() != 1)
   {
      signal_commandComplete(help(), this);
      return false;
   }

   DtGuiThreadVrfRemoteController::instance(de).rewindScenario();

   signal_commandComplete(QObject::tr("Scenario Rewound"), this);

   return true;
}

DtRemoteControlCommand* DtRemoteControlRewindScenarioCommand::clone() const
{
   return new DtRemoteControlRewindScenarioCommand;
}

//************************ Play Scenario Command

DtRemoteControlPlayScenarioCommand::DtRemoteControlPlayScenarioCommand()
: DtRemoteControlCommand("play", QObject::tr("play"))
{
}

DtRemoteControlPlayScenarioCommand::~DtRemoteControlPlayScenarioCommand()
{
}

bool DtRemoteControlPlayScenarioCommand::execute(makVrv::DtDe& de, const QString& command)
{
   QStringList tokens = tokenize(command);

   if (tokens.size() != 1)
   {
      signal_commandComplete(help(), this);
      return false;
   }

   DtGuiThreadVrfRemoteController::instance(de).runScenario();

   signal_commandComplete(QObject::tr("Scenario Playing"), this);

   return true;
}

DtRemoteControlCommand* DtRemoteControlPlayScenarioCommand::clone() const
{
   return new DtRemoteControlPlayScenarioCommand;
}

//************************ Pause Scenario Command

DtRemoteControlPauseScenarioCommand::DtRemoteControlPauseScenarioCommand()
: DtRemoteControlCommand("pause", QObject::tr("pause"))
{
}

DtRemoteControlPauseScenarioCommand::~DtRemoteControlPauseScenarioCommand()
{
}

bool DtRemoteControlPauseScenarioCommand::execute(makVrv::DtDe& de, const QString& command)
{
   QStringList tokens = tokenize(command);

   if (tokens.size() != 1)
   {
      signal_commandComplete(help(), this);
      return false;
   }

   DtGuiThreadVrfRemoteController::instance(de).pauseScenario();

   signal_commandComplete(QObject::tr("Scenario Paused"), this);

   return true;
}

DtRemoteControlCommand* DtRemoteControlPauseScenarioCommand::clone() const
{
   return new DtRemoteControlPauseScenarioCommand;
}

Document ID: Generated on Fri Jun 29 16:33:32 EDT 2012 from SVN revision 116588
Copyright © 2005-2012 VT MÄK Inc. All Rights Reserved (www.mak.com)