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