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  // variable & id
64  int variable = inputStorage.readUnsignedByte();
65  std::string id = inputStorage.readString();
66  // check variable
67  if (variable != ID_LIST && variable != VAR_VIEW_ZOOM && variable != VAR_VIEW_OFFSET
68  && variable != VAR_VIEW_SCHEMA && variable != VAR_VIEW_BOUNDARY) {
69  server.writeStatusCmd(CMD_GET_GUI_VARIABLE, RTYPE_ERR, "Get GUI Variable: unsupported variable specified", outputStorage);
70  return false;
71  }
72  // begin response building
73  tcpip::Storage tempMsg;
74  // response-code, variableID, objectID
76  tempMsg.writeUnsignedByte(variable);
77  tempMsg.writeString(id);
78  // process request
79  if (variable == ID_LIST) {
80  std::vector<std::string> ids = getMainWindow()->getViewIDs();
82  tempMsg.writeStringList(ids);
83  } else {
84  GUISUMOAbstractView* v = getNamedView(id);
85  if (v == 0) {
86  server.writeStatusCmd(CMD_GET_GUI_VARIABLE, RTYPE_ERR, "View '" + id + "' is not known", outputStorage);
87  return false;
88  }
89  switch (variable) {
90  case VAR_VIEW_ZOOM:
92  tempMsg.writeDouble(v->getChanger().getZoom());
93  break;
94  case VAR_VIEW_OFFSET:
96  tempMsg.writeDouble(v->getChanger().getXPos());
97  tempMsg.writeDouble(v->getChanger().getYPos());
98  break;
99  case VAR_VIEW_SCHEMA: {
100  FXComboBox& c = v->getColoringSchemesCombo();
102  tempMsg.writeString((std::string)c.getItem(c.getCurrentItem()).text());
103  break;
104  }
105  case VAR_VIEW_BOUNDARY: {
107  Boundary b = v->getVisibleBoundary();
108  tempMsg.writeDouble(b.xmin());
109  tempMsg.writeDouble(b.ymin());
110  tempMsg.writeDouble(b.xmax());
111  tempMsg.writeDouble(b.ymax());
112  break;
113  }
114  default:
115  break;
116  }
117  }
118  server.writeStatusCmd(CMD_GET_GUI_VARIABLE, RTYPE_OK, "", outputStorage);
119  server.writeResponseWithLength(outputStorage, tempMsg);
120  return true;
121 }
122 
123 
124 bool
126  tcpip::Storage& outputStorage) throw(TraCIException, std::invalid_argument) {
127  std::string warning = ""; // additional description for response
128  // variable
129  int variable = inputStorage.readUnsignedByte();
130  if (variable != VAR_VIEW_ZOOM && variable != VAR_VIEW_OFFSET && variable != VAR_VIEW_SCHEMA && variable != VAR_VIEW_BOUNDARY
131  && variable != VAR_SCREENSHOT && variable != VAR_TRACK_VEHICLE
132  ) {
133  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "Change GUI State: unsupported variable specified", outputStorage);
134  return false;
135  }
136  // id
137  std::string id = inputStorage.readString();
138  GUISUMOAbstractView* v = getNamedView(id);
139  if (v == 0) {
140  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "View '" + id + "' is not known", outputStorage);
141  return false;
142  }
143  // process
144  int valueDataType = inputStorage.readUnsignedByte();
145  switch (variable) {
146  case VAR_VIEW_ZOOM:
147  if (valueDataType != TYPE_DOUBLE) {
148  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The zoom must be given as a double.", outputStorage);
149  return false;
150  }
151  v->setViewport(inputStorage.readDouble(), v->getChanger().getXPos(), v->getChanger().getYPos());
152  break;
153  case VAR_VIEW_OFFSET: {
154  if (valueDataType != POSITION_2D) {
155  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The view port must be given as a position.", outputStorage);
156  return false;
157  }
158  v->setViewport(v->getChanger().getZoom(), inputStorage.readDouble(), v->getChanger().getYPos());
159  v->setViewport(v->getChanger().getZoom(), v->getChanger().getXPos(), inputStorage.readDouble());
160  }
161  break;
162  case VAR_VIEW_SCHEMA:
163  if (valueDataType != TYPE_STRING) {
164  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The scheme must be specified by a string.", outputStorage);
165  return false;
166  }
167  if (!v->setColorScheme(inputStorage.readString())) {
168  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The scheme is not known.", outputStorage);
169  return false;
170  }
171  break;
172  case VAR_VIEW_BOUNDARY: {
173  if (valueDataType != TYPE_BOUNDINGBOX) {
174  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The boundary must be specified by a bounding box.", outputStorage);
175  return false;
176  }
177  const SUMOReal xmin = inputStorage.readDouble();
178  const SUMOReal ymin = inputStorage.readDouble();
179  const SUMOReal xmax = inputStorage.readDouble();
180  const SUMOReal ymax = inputStorage.readDouble();
181  Boundary b(xmin, ymin, xmax, ymax);
182  v->centerTo(b);
183  break;
184  }
185  case VAR_SCREENSHOT: {
186  if (valueDataType != TYPE_STRING) {
187  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "Making a snapshot requires a file name.", outputStorage);
188  return false;
189  }
190  std::string filename = inputStorage.readString();
191  std::string error = v->makeSnapshot(filename);
192  if (error != "") {
193  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, error, outputStorage);
194  return false;
195  }
196  }
197  break;
198  case VAR_TRACK_VEHICLE: {
199  if (valueDataType != TYPE_STRING) {
200  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "Tracking requires a string vehicle ID.", outputStorage);
201  return false;
202  }
203  std::string id = inputStorage.readString();
204  if (id == "") {
205  v->stopTrack();
206  } else {
208  if (veh == 0) {
209  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "Could not find vehicle '" + id + "'.", outputStorage);
210  return false;
211  }
212  if (!static_cast<GUIVehicle*>(veh)->hasActiveAddVisualisation(v, GUIVehicle::VO_TRACKED)) {
213  v->startTrack(static_cast<GUIVehicle*>(veh)->getGlID());
214  static_cast<GUIVehicle*>(veh)->addActiveAddVisualisation(v, GUIVehicle::VO_TRACKED);
215  }
216  }
217  }
218  default:
219  break;
220  }
221  server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_OK, warning, outputStorage);
222  return true;
223 }
224 
225 
228  FXWindow* w = FXApp::instance()->getRootWindow()->getFirst();
229  while (w != 0 && dynamic_cast<GUIMainWindow*>(w) == 0) {
230  w = w->getNext();
231  }
232  if (w == 0) {
233  // main window not found
234  return 0;
235  }
236  return dynamic_cast<GUIMainWindow*>(w);
237 }
238 
239 
241 TraCIServerAPI_GUI::getNamedView(const std::string& id) {
242  GUIMainWindow* mw = static_cast<GUIMainWindow*>(getMainWindow());
243  if (mw == 0) {
244  return 0;
245  }
246  GUIGlChildWindow* c = static_cast<GUIGlChildWindow*>(mw->getViewByID(id));
247  if (c == 0) {
248  return 0;
249  }
250  return c->getView();
251 }
252 
253 
254 #endif
255 
256 
257 /****************************************************************************/
258