SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TraCIServerAPI_GUI.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // APIs for getting/setting GUI values via TraCI
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #ifndef NO_TRACI
34 
35 #include <fx.h>
42 #include <guisim/GUINet.h>
43 #include <guisim/GUIVehicle.h>
44 #include "TraCIServerAPI_GUI.h"
45 
46 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 
51 // ===========================================================================
52 // used namespaces
53 // ===========================================================================
54 using namespace traci;
55 
56 
57 // ===========================================================================
58 // method definitions
59 // ===========================================================================
60 bool
62  tcpip::Storage& outputStorage) throw(TraCIException, std::invalid_argument) {
63  std::string warning = ""; // additional description for response
64  // variable & id
65  int variable = inputStorage.readUnsignedByte();
66  std::string id = inputStorage.readString();
67  // check variable
68  if (variable != ID_LIST && variable != VAR_VIEW_ZOOM && variable != VAR_VIEW_OFFSET
69  && variable != VAR_VIEW_SCHEMA && variable != VAR_VIEW_BOUNDARY) {
70  server.writeStatusCmd(CMD_GET_GUI_VARIABLE, RTYPE_ERR, "Get GUI Variable: unsupported variable specified", outputStorage);
71  return false;
72  }
73  // begin response building
74  tcpip::Storage tempMsg;
75  // response-code, variableID, objectID
77  tempMsg.writeUnsignedByte(variable);
78  tempMsg.writeString(id);
79  // process request
80  if (variable == ID_LIST) {
81  std::vector<std::string> ids = getMainWindow()->getViewIDs();
83  tempMsg.writeStringList(ids);
84  } else {
85  GUISUMOAbstractView* v = getNamedView(id);
86  if (v == 0) {
87  server.writeStatusCmd(CMD_GET_GUI_VARIABLE, RTYPE_ERR, "View '" + id + "' is not known", outputStorage);
88  return false;
89  }
90  switch (variable) {
91  case VAR_VIEW_ZOOM:
93  tempMsg.writeDouble(v->getChanger().getZoom());
94  break;
95  case VAR_VIEW_OFFSET:
97  tempMsg.writeDouble(v->getChanger().getXPos());
98  tempMsg.writeDouble(v->getChanger().getYPos());
99  break;
100  case VAR_VIEW_SCHEMA: {
101  FXComboBox& c = v->getColoringSchemesCombo();
103  tempMsg.writeString((std::string)c.getItem(c.getCurrentItem()).text());
104  break;
105  }
106  case VAR_VIEW_BOUNDARY: {
108  Boundary b = v->getVisibleBoundary();
109  tempMsg.writeDouble(b.xmin());
110  tempMsg.writeDouble(b.ymin());
111  tempMsg.writeDouble(b.xmax());
112  tempMsg.writeDouble(b.ymax());
113  break;
114  }
115  default:
116  break;
117  }
118  }
119  server.writeStatusCmd(CMD_GET_GUI_VARIABLE, RTYPE_OK, warning, outputStorage);
120  server.writeResponseWithLength(outputStorage, tempMsg);
121  return true;
122 }
123 
124 
125 bool
127  tcpip::Storage& outputStorage) throw(TraCIException, std::invalid_argument) {
128  std::string warning = ""; // additional description for response
129  // variable
130  int variable = inputStorage.readUnsignedByte();
131  if (variable != VAR_VIEW_ZOOM && variable != VAR_VIEW_OFFSET && variable != VAR_VIEW_SCHEMA && variable != VAR_VIEW_BOUNDARY
132  && variable != VAR_SCREENSHOT && variable != VAR_TRACK_VEHICLE
133  ) {
134  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "Change GUI State: unsupported variable specified", outputStorage);
135  return false;
136  }
137  // id
138  std::string id = inputStorage.readString();
139  GUISUMOAbstractView* v = getNamedView(id);
140  if (v == 0) {
141  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "View '" + id + "' is not known", outputStorage);
142  return false;
143  }
144  // process
145  int valueDataType = inputStorage.readUnsignedByte();
146  switch (variable) {
147  case VAR_VIEW_ZOOM:
148  if (valueDataType != TYPE_DOUBLE) {
149  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The zoom must be given as a double.", outputStorage);
150  return false;
151  }
152  v->setViewport(inputStorage.readDouble(), v->getChanger().getXPos(), v->getChanger().getYPos());
153  break;
154  case VAR_VIEW_OFFSET: {
155  if (valueDataType != POSITION_2D) {
156  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The view port must be given as a position.", outputStorage);
157  return false;
158  }
159  v->setViewport(v->getChanger().getZoom(), inputStorage.readDouble(), v->getChanger().getYPos());
160  v->setViewport(v->getChanger().getZoom(), v->getChanger().getXPos(), inputStorage.readDouble());
161  }
162  break;
163  case VAR_VIEW_SCHEMA:
164  if (valueDataType != TYPE_STRING) {
165  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The scheme must be specified by a string.", outputStorage);
166  return false;
167  }
168  if (!v->setColorScheme(inputStorage.readString())) {
169  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The scheme is not known.", outputStorage);
170  return false;
171  }
172  break;
173  case VAR_VIEW_BOUNDARY: {
174  if (valueDataType != TYPE_BOUNDINGBOX) {
175  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The boundary must be specified by a bounding box.", outputStorage);
176  return false;
177  }
178  const SUMOReal xmin = inputStorage.readDouble();
179  const SUMOReal ymin = inputStorage.readDouble();
180  const SUMOReal xmax = inputStorage.readDouble();
181  const SUMOReal ymax = inputStorage.readDouble();
182  Boundary b(xmin, ymin, xmax, ymax);
183  v->centerTo(b);
184  break;
185  }
186  case VAR_SCREENSHOT: {
187  if (valueDataType != TYPE_STRING) {
188  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "Making a snapshot requires a file name.", outputStorage);
189  return false;
190  }
191  std::string filename = inputStorage.readString();
192  std::string error = v->makeSnapshot(filename);
193  if (error != "") {
194  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, error, outputStorage);
195  return false;
196  }
197  }
198  break;
199  case VAR_TRACK_VEHICLE: {
200  if (valueDataType != TYPE_STRING) {
201  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "Tracking requires a string vehicle ID.", outputStorage);
202  return false;
203  }
204  std::string id = inputStorage.readString();
205  if (id == "") {
206  v->stopTrack();
207  } else {
209  if (veh == 0) {
210  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "Could not find vehicle '" + id + "'.", outputStorage);
211  return false;
212  }
213  if (!static_cast<GUIVehicle*>(veh)->hasActiveAddVisualisation(v, GUIVehicle::VO_TRACKED)) {
214  v->startTrack(static_cast<GUIVehicle*>(veh)->getGlID());
215  static_cast<GUIVehicle*>(veh)->addActiveAddVisualisation(v, GUIVehicle::VO_TRACKED);
216  }
217  }
218  }
219  default:
220  break;
221  }
222  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_OK, warning, outputStorage);
223  return true;
224 }
225 
226 
229  FXWindow* w = FXApp::instance()->getRootWindow()->getFirst();
230  while (w != 0 && dynamic_cast<GUIMainWindow*>(w) == 0) {
231  w = w->getNext();
232  }
233  if (w == 0) {
234  // main window not found
235  return 0;
236  }
237  return dynamic_cast<GUIMainWindow*>(w);
238 }
239 
240 
241 GUISUMOAbstractView* const
242 TraCIServerAPI_GUI::getNamedView(const std::string& id) {
243  GUIMainWindow* mw = static_cast<GUIMainWindow*>(getMainWindow());
244  if (mw == 0) {
245  return 0;
246  }
247  GUIGlChildWindow* c = static_cast<GUIGlChildWindow*>(mw->getViewByID(id));
248  if (c == 0) {
249  return 0;
250  }
251  return c->getView();
252 }
253 
254 
255 #endif
256 
257 
258 /****************************************************************************/
259