SUMO - Simulation of Urban MObility
TraCIServerAPI_GUI.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // APIs for getting/setting GUI values via TraCI
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 
00023 
00024 // ===========================================================================
00025 // included modules
00026 // ===========================================================================
00027 #ifdef _MSC_VER
00028 #include <windows_config.h>
00029 #else
00030 #include <config.h>
00031 #endif
00032 
00033 #ifndef NO_TRACI
00034 
00035 #include <fx.h>
00036 #include <utils/gui/windows/GUIMainWindow.h>
00037 #include <utils/gui/windows/GUIGlChildWindow.h>
00038 #include <utils/gui/windows/GUISUMOAbstractView.h>
00039 #include <utils/gui/windows/GUIPerspectiveChanger.h>
00040 #include <utils/foxtools/MFXImageHelper.h>
00041 #include <traci-server/TraCIConstants.h>
00042 #include <guisim/GUINet.h>
00043 #include <guisim/GUIVehicle.h>
00044 #include "TraCIServerAPI_GUI.h"
00045 
00046 #ifdef CHECK_MEMORY_LEAKS
00047 #include <foreign/nvwa/debug_new.h>
00048 #endif // CHECK_MEMORY_LEAKS
00049 
00050 
00051 // ===========================================================================
00052 // used namespaces
00053 // ===========================================================================
00054 using namespace traci;
00055 
00056 
00057 // ===========================================================================
00058 // method definitions
00059 // ===========================================================================
00060 bool
00061 TraCIServerAPI_GUI::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
00062                                tcpip::Storage& outputStorage) throw(TraCIException, std::invalid_argument) {
00063     std::string warning = ""; // additional description for response
00064     // variable & id
00065     int variable = inputStorage.readUnsignedByte();
00066     std::string id = inputStorage.readString();
00067     // check variable
00068     if (variable != ID_LIST && variable != VAR_VIEW_ZOOM && variable != VAR_VIEW_OFFSET
00069             && variable != VAR_VIEW_SCHEMA && variable != VAR_VIEW_BOUNDARY) {
00070         server.writeStatusCmd(CMD_GET_GUI_VARIABLE, RTYPE_ERR, "Get GUI Variable: unsupported variable specified", outputStorage);
00071         return false;
00072     }
00073     // begin response building
00074     tcpip::Storage tempMsg;
00075     //  response-code, variableID, objectID
00076     tempMsg.writeUnsignedByte(RESPONSE_GET_GUI_VARIABLE);
00077     tempMsg.writeUnsignedByte(variable);
00078     tempMsg.writeString(id);
00079     // process request
00080     if (variable == ID_LIST) {
00081         std::vector<std::string> ids = getMainWindow()->getViewIDs();
00082         tempMsg.writeUnsignedByte(TYPE_STRINGLIST);
00083         tempMsg.writeStringList(ids);
00084     } else {
00085         GUISUMOAbstractView* v = getNamedView(id);
00086         if (v == 0) {
00087             server.writeStatusCmd(CMD_GET_GUI_VARIABLE, RTYPE_ERR, "View '" + id + "' is not known", outputStorage);
00088             return false;
00089         }
00090         switch (variable) {
00091             case VAR_VIEW_ZOOM:
00092                 tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00093                 tempMsg.writeDouble(v->getChanger().getZoom());
00094                 break;
00095             case VAR_VIEW_OFFSET:
00096                 tempMsg.writeUnsignedByte(POSITION_2D);
00097                 tempMsg.writeDouble(v->getChanger().getXPos());
00098                 tempMsg.writeDouble(v->getChanger().getYPos());
00099                 break;
00100             case VAR_VIEW_SCHEMA: {
00101                 FXComboBox& c = v->getColoringSchemesCombo();
00102                 tempMsg.writeUnsignedByte(TYPE_STRING);
00103                 tempMsg.writeString((std::string)c.getItem(c.getCurrentItem()).text());
00104                 break;
00105             }
00106             case VAR_VIEW_BOUNDARY: {
00107                 tempMsg.writeUnsignedByte(TYPE_BOUNDINGBOX);
00108                 Boundary b = v->getVisibleBoundary();
00109                 tempMsg.writeDouble(b.xmin());
00110                 tempMsg.writeDouble(b.ymin());
00111                 tempMsg.writeDouble(b.xmax());
00112                 tempMsg.writeDouble(b.ymax());
00113                 break;
00114             }
00115             default:
00116                 break;
00117         }
00118     }
00119     server.writeStatusCmd(CMD_GET_GUI_VARIABLE, RTYPE_OK, warning, outputStorage);
00120     server.writeResponseWithLength(outputStorage, tempMsg);
00121     return true;
00122 }
00123 
00124 
00125 bool
00126 TraCIServerAPI_GUI::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
00127                                tcpip::Storage& outputStorage) throw(TraCIException, std::invalid_argument) {
00128     std::string warning = ""; // additional description for response
00129     // variable
00130     int variable = inputStorage.readUnsignedByte();
00131     if (variable != VAR_VIEW_ZOOM && variable != VAR_VIEW_OFFSET && variable != VAR_VIEW_SCHEMA && variable != VAR_VIEW_BOUNDARY
00132             && variable != VAR_SCREENSHOT && variable != VAR_TRACK_VEHICLE
00133        ) {
00134         server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "Change GUI State: unsupported variable specified", outputStorage);
00135         return false;
00136     }
00137     // id
00138     std::string id = inputStorage.readString();
00139     GUISUMOAbstractView* v = getNamedView(id);
00140     if (v == 0) {
00141         server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "View '" + id + "' is not known", outputStorage);
00142         return false;
00143     }
00144     // process
00145     int valueDataType = inputStorage.readUnsignedByte();
00146     switch (variable) {
00147         case VAR_VIEW_ZOOM:
00148             if (valueDataType != TYPE_DOUBLE) {
00149                 server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The zoom must be given as a double.", outputStorage);
00150                 return false;
00151             }
00152             v->setViewport(inputStorage.readDouble(), v->getChanger().getXPos(), v->getChanger().getYPos());
00153             break;
00154         case VAR_VIEW_OFFSET: {
00155             if (valueDataType != POSITION_2D) {
00156                 server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The view port must be given as a position.", outputStorage);
00157                 return false;
00158             }
00159             v->setViewport(v->getChanger().getZoom(), inputStorage.readDouble(), v->getChanger().getYPos());
00160             v->setViewport(v->getChanger().getZoom(), v->getChanger().getXPos(), inputStorage.readDouble());
00161         }
00162         break;
00163         case VAR_VIEW_SCHEMA:
00164             if (valueDataType != TYPE_STRING) {
00165                 server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The scheme must be specified by a string.", outputStorage);
00166                 return false;
00167             }
00168             if (!v->setColorScheme(inputStorage.readString())) {
00169                 server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The scheme is not known.", outputStorage);
00170                 return false;
00171             }
00172             break;
00173         case VAR_VIEW_BOUNDARY: {
00174             if (valueDataType != TYPE_BOUNDINGBOX) {
00175                 server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The boundary must be specified by a bounding box.", outputStorage);
00176                 return false;
00177             }
00178             const SUMOReal xmin = inputStorage.readDouble();
00179             const SUMOReal ymin = inputStorage.readDouble();
00180             const SUMOReal xmax = inputStorage.readDouble();
00181             const SUMOReal ymax = inputStorage.readDouble();
00182             Boundary b(xmin, ymin, xmax, ymax);
00183             v->centerTo(b);
00184             break;
00185         }
00186         case VAR_SCREENSHOT: {
00187             if (valueDataType != TYPE_STRING) {
00188                 server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "Making a snapshot requires a file name.", outputStorage);
00189                 return false;
00190             }
00191             std::string filename = inputStorage.readString();
00192             std::string error = v->makeSnapshot(filename);
00193             if (error != "") {
00194                 server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, error, outputStorage);
00195                 return false;
00196             }
00197         }
00198         break;
00199         case VAR_TRACK_VEHICLE: {
00200             if (valueDataType != TYPE_STRING) {
00201                 server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "Tracking requires a string vehicle ID.", outputStorage);
00202                 return false;
00203             }
00204             std::string id = inputStorage.readString();
00205             if (id == "") {
00206                 v->stopTrack();
00207             } else {
00208                 SUMOVehicle* veh = MSNet::getInstance()->getVehicleControl().getVehicle(id);
00209                 if (veh == 0) {
00210                     server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "Could not find vehicle '" + id + "'.", outputStorage);
00211                     return false;
00212                 }
00213                 if (!static_cast<GUIVehicle*>(veh)->hasActiveAddVisualisation(v, GUIVehicle::VO_TRACKED)) {
00214                     v->startTrack(static_cast<GUIVehicle*>(veh)->getGlID());
00215                     static_cast<GUIVehicle*>(veh)->addActiveAddVisualisation(v, GUIVehicle::VO_TRACKED);
00216                 }
00217             }
00218         }
00219         default:
00220             break;
00221     }
00222     server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_OK, warning, outputStorage);
00223     return true;
00224 }
00225 
00226 
00227 GUIMainWindow*
00228 TraCIServerAPI_GUI::getMainWindow() {
00229     FXWindow* w = FXApp::instance()->getRootWindow()->getFirst();
00230     while (w != 0 && dynamic_cast<GUIMainWindow*>(w) == 0) {
00231         w = w->getNext();
00232     }
00233     if (w == 0) {
00234         // main window not found
00235         return 0;
00236     }
00237     return dynamic_cast<GUIMainWindow*>(w);
00238 }
00239 
00240 
00241 GUISUMOAbstractView* const
00242 TraCIServerAPI_GUI::getNamedView(const std::string& id) {
00243     GUIMainWindow* mw = static_cast<GUIMainWindow*>(getMainWindow());
00244     if (mw == 0) {
00245         return 0;
00246     }
00247     GUIGlChildWindow* c = static_cast<GUIGlChildWindow*>(mw->getViewByID(id));
00248     if (c == 0) {
00249         return 0;
00250     }
00251     return c->getView();
00252 }
00253 
00254 
00255 #endif
00256 
00257 
00258 /****************************************************************************/
00259 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines