SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GUISelectedStorage.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Storage for "selected" objects
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
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 #include <algorithm>
36 #include "GUISelectedStorage.h"
39 #include <utils/common/ToString.h>
40 
41 #ifdef CHECK_MEMORY_LEAKS
42 #include <foreign/nvwa/debug_new.h>
43 #endif // CHECK_MEMORY_LEAKS
44 
45 
46 // ===========================================================================
47 // member method definitions
48 // ===========================================================================
49 /* -------------------------------------------------------------------------
50  * for GUISelectedStorage::SingleTypeSelections
51  * ----------------------------------------------------------------------- */
53 
54 
56 
57 
58 bool
60  return mySelected.count(id) > 0;
61 }
62 
63 
64 void
66  mySelected.insert(id);
67 }
68 
69 
70 void
72  mySelected.erase(id);
73 }
74 
75 
76 void
78  mySelected.clear();
79 }
80 
81 
82 void
83 GUISelectedStorage::SingleTypeSelections::save(const std::string& filename) {
84  GUISelectedStorage::save(filename, mySelected);
85 }
86 
87 
88 const std::set<GUIGlID>&
90  return mySelected;
91 }
92 
93 
94 
95 /* -------------------------------------------------------------------------
96  * for GUISelectedStorage
97  * ----------------------------------------------------------------------- */
99 
100 
102 
103 
104 bool
106  switch (type) {
107  case GLO_NETWORK:
108  return false;
109  case GLO_ADDITIONAL:
110  return isSelected(GLO_TRIGGER, id) || isSelected(GLO_DETECTOR, id);
111  default:
112  return mySelections[type].isSelected(id);
113  }
114 }
115 
116 
117 void
120  if (!object) {
121  throw ProcessError("Unkown object in GUISelectedStorage::select (id=" + toString(id) + ").");
122  }
123  GUIGlObjectType type = object->getType();
125 
126  mySelections[type].select(id);
127  myAllSelected.insert(id);
128  if (update && myUpdateTarget) {
130  }
131 }
132 
133 
134 void
137  if (!object) {
138  throw ProcessError("Unkown object in GUISelectedStorage::deselect (id=" + toString(id) + ").");
139  }
140  GUIGlObjectType type = object->getType();
142 
143  mySelections[type].deselect(id);
144  myAllSelected.erase(id);
145  if (myUpdateTarget) {
147  }
148 }
149 
150 
151 void
154  if (!object) {
155  throw ProcessError("Unkown object in GUISelectedStorage::toggleSelection (id=" + toString(id) + ").");
156  }
157 
158  bool selected = isSelected(object->getType(), id);
159  if (!selected) {
160  select(id);
161  } else {
162  deselect(id);
163  }
165 }
166 
167 
168 const std::set<GUIGlID>&
170  return myAllSelected;
171 }
172 
173 
174 const std::set<GUIGlID>&
176  return mySelections[type].getSelected();
177 }
178 
179 
180 void
182  for (std::map<GUIGlObjectType, SingleTypeSelections>::iterator it = mySelections.begin(); it != mySelections.end(); it++) {
183  it->second.clear();
184  }
185  myAllSelected.clear();
186  if (myUpdateTarget) {
188  }
189 }
190 
191 
192 std::set<GUIGlID>
193 GUISelectedStorage::loadIDs(const std::string& filename, std::string& msgOut, GUIGlObjectType type, int maxErrors) {
194  std::set<GUIGlID> result;
195  std::ostringstream msg;
196  std::ifstream strm(filename.c_str());
197  int numIgnored = 0;
198  int numMissing = 0;
199  if (!strm.good()) {
200  msgOut = "Could not open '" + filename + "'.\n";
201  return result;
202  }
203  while (strm.good()) {
204  std::string line;
205  strm >> line;
206  if (line.length() == 0) {
207  continue;
208  }
209 
211  if (object) {
212  if (type != GLO_MAX && (object->getType() != type)) {
213  numIgnored++;
214  if (numIgnored + numMissing <= maxErrors) {
215  msg << "Ignoring item '" << line << "' because of invalid type " << toString(object->getType()) << "\n";
216  }
217  } else {
218  result.insert(object->getGlID());
219  }
220  } else {
221  numMissing++;
222  if (numIgnored + numMissing <= maxErrors) {
223  msg << "Item '" + line + "' not found\n";
224  }
225  continue;
226  }
227  }
228  strm.close();
229  if (numIgnored + numMissing > maxErrors) {
230  msg << "...\n" << numIgnored << " objects ignored, " << numMissing << " objects not found\n";
231  }
232  msgOut = msg.str();
233  return result;
234 }
235 
236 
237 std::string
238 GUISelectedStorage::load(const std::string& filename, GUIGlObjectType type) {
239  std::string errors;
240  const std::set<GUIGlID> ids = loadIDs(filename, errors, type);
241  for (std::set<GUIGlID>::const_iterator it = ids.begin(); it != ids.end(); it++) {
242  select(*it, false);
243  }
244  if (myUpdateTarget) {
246  }
247  return errors;
248 }
249 
250 
251 void
252 GUISelectedStorage::save(GUIGlObjectType type, const std::string& filename) {
253  mySelections[type].save(filename);
254 }
255 
256 
257 void
258 GUISelectedStorage::save(const std::string& filename) const {
259  save(filename, myAllSelected);
260 }
261 
262 
263 void
265  myUpdateTarget = updateTarget;
266 }
267 
268 
269 void
271  myUpdateTarget = 0;
272 }
273 
274 
275 void
276 GUISelectedStorage::save(const std::string& filename, const std::set<GUIGlID>& ids) {
277  OutputDevice& dev = OutputDevice::getDevice(filename);
278  for (std::set<GUIGlID>::const_iterator i = ids.begin(); i != ids.end(); ++i) {
280  if (object != 0) {
281  std::string name = object->getFullName();
282  dev << name << "\n";
284  }
285  }
286  dev.close();
287 }
288 /****************************************************************************/