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.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 #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) {
194  std::set<GUIGlID> result;
195  std::ostringstream msg;
196  std::ifstream strm(filename.c_str());
197  if (!strm.good()) {
198  msgOut = "Could not open '" + filename + "'.\n";
199  return result;
200  }
201  while (strm.good()) {
202  std::string line;
203  strm >> line;
204  if (line.length() == 0) {
205  continue;
206  }
207 
209  if (object) {
210  if (type != GLO_MAX && (object->getType() != type)) {
211  msg << "Ignoring item '" << line << "' because of invalid type " << toString(object->getType()) << "\n";
212  } else {
213  result.insert(object->getGlID());
214  }
215  } else {
216  msg << "Item '" + line + "' not found\n";
217  continue;
218  }
219  }
220  strm.close();
221  msgOut = msg.str();
222  return result;
223 }
224 
225 
226 std::string
227 GUISelectedStorage::load(const std::string& filename, GUIGlObjectType type) {
228  std::string errors;
229  const std::set<GUIGlID> ids = loadIDs(filename, errors, type);
230  for (std::set<GUIGlID>::const_iterator it = ids.begin(); it != ids.end(); it++) {
231  select(*it, false);
232  }
233  if (myUpdateTarget) {
235  }
236  return errors;
237 }
238 
239 
240 void
241 GUISelectedStorage::save(GUIGlObjectType type, const std::string& filename) {
242  mySelections[type].save(filename);
243 }
244 
245 
246 void
247 GUISelectedStorage::save(const std::string& filename) const {
248  save(filename, myAllSelected);
249 }
250 
251 
252 void
254  myUpdateTarget = updateTarget;
255 }
256 
257 
258 void
260  myUpdateTarget = 0;
261 }
262 
263 
264 void
265 GUISelectedStorage::save(const std::string& filename, const std::set<GUIGlID>& ids) {
266  OutputDevice& dev = OutputDevice::getDevice(filename);
267  for (std::set<GUIGlID>::const_iterator i = ids.begin(); i != ids.end(); ++i) {
269  if (object != 0) {
270  std::string name = object->getFullName();
271  dev << name << "\n";
273  }
274  }
275  dev.close();
276 }
277 /****************************************************************************/