SUMO - Simulation of Urban MObility
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.dlr.de/
12 // Copyright (C) 2001-2017 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 /* -------------------------------------------------------------------------
51  * for GUISelectedStorage::SingleTypeSelections
52  * ----------------------------------------------------------------------- */
53 
55 
56 
58 
59 
60 bool
62  return mySelected.count(id) > 0;
63 }
64 
65 
66 void
68  mySelected.insert(id);
69 }
70 
71 
72 void
74  mySelected.erase(id);
75 }
76 
77 
78 void
80  mySelected.clear();
81 }
82 
83 
84 void
85 GUISelectedStorage::SingleTypeSelections::save(const std::string& filename) {
87 }
88 
89 
90 const std::set<GUIGlID>&
92  return mySelected;
93 }
94 
95 /* -------------------------------------------------------------------------
96  * for GUISelectedStorage
97  * ----------------------------------------------------------------------- */
98 
100 
101 
103 
104 
105 bool
107  switch (type) {
108  case GLO_NETWORK:
109  return false;
110  case GLO_ADDITIONAL:
111  return isSelected(GLO_TRIGGER, id) || isSelected(GLO_DETECTOR, id) || mySelections[GLO_ADDITIONAL].isSelected(id);
112  default:
113  return mySelections[type].isSelected(id);
114  }
115 }
116 
117 
118 void
121  if (!object) {
122  throw ProcessError("Unkown object in GUISelectedStorage::select (id=" + toString(id) + ").");
123  }
124  GUIGlObjectType type = object->getType();
126 
127  mySelections[type].select(id);
128  myAllSelected.insert(id);
129  if (update && myUpdateTarget) {
131  }
132 }
133 
134 
135 void
138  if (!object) {
139  throw ProcessError("Unkown object in GUISelectedStorage::deselect (id=" + toString(id) + ").");
140  }
141  GUIGlObjectType type = object->getType();
143 
144  mySelections[type].deselect(id);
145  myAllSelected.erase(id);
146  if (myUpdateTarget) {
148  }
149 }
150 
151 
152 void
155  if (!object) {
156  throw ProcessError("Unkown object in GUISelectedStorage::toggleSelection (id=" + toString(id) + ").");
157  }
158 
159  bool selected = isSelected(object->getType(), id);
160  if (!selected) {
161  select(id);
162  } else {
163  deselect(id);
164  }
166 }
167 
168 
169 const std::set<GUIGlID>&
171  return myAllSelected;
172 }
173 
174 
175 const std::set<GUIGlID>&
177  return mySelections[type].getSelected();
178 }
179 
180 
181 void
183  for (std::map<GUIGlObjectType, SingleTypeSelections>::iterator it = mySelections.begin(); it != mySelections.end(); it++) {
184  it->second.clear();
185  }
186  myAllSelected.clear();
187  if (myUpdateTarget) {
189  }
190 }
191 
192 
193 std::set<GUIGlID>
194 GUISelectedStorage::loadIDs(const std::string& filename, std::string& msgOut, GUIGlObjectType type, int maxErrors) {
195  std::set<GUIGlID> result;
196  std::ostringstream msg;
197  std::ifstream strm(filename.c_str());
198  int numIgnored = 0;
199  int numMissing = 0;
200  if (!strm.good()) {
201  msgOut = "Could not open '" + filename + "'.\n";
202  return result;
203  }
204  while (strm.good()) {
205  std::string line;
206  strm >> line;
207  if (line.length() == 0) {
208  continue;
209  }
210 
212  if (object) {
213  if (type != GLO_MAX && (object->getType() != type)) {
214  numIgnored++;
215  if (numIgnored + numMissing <= maxErrors) {
216  msg << "Ignoring item '" << line << "' because of invalid type " << toString(object->getType()) << "\n";
217  }
218  } else {
219  result.insert(object->getGlID());
220  }
221  } else {
222  numMissing++;
223  if (numIgnored + numMissing <= maxErrors) {
224  msg << "Item '" + line + "' not found\n";
225  }
226  continue;
227  }
228  }
229  strm.close();
230  if (numIgnored + numMissing > maxErrors) {
231  msg << "...\n" << numIgnored << " objects ignored, " << numMissing << " objects not found\n";
232  }
233  msgOut = msg.str();
234  return result;
235 }
236 
237 
238 std::string
239 GUISelectedStorage::load(const std::string& filename, GUIGlObjectType type) {
240  std::string errors;
241  const std::set<GUIGlID> ids = loadIDs(filename, errors, type);
242  for (std::set<GUIGlID>::const_iterator it = ids.begin(); it != ids.end(); it++) {
243  select(*it, false);
244  }
245  if (myUpdateTarget) {
247  }
248  return errors;
249 }
250 
251 
252 void
253 GUISelectedStorage::save(GUIGlObjectType type, const std::string& filename) {
254  mySelections[type].save(filename);
255 }
256 
257 
258 void
259 GUISelectedStorage::save(const std::string& filename) const {
260  save(filename, myAllSelected);
261 }
262 
263 
264 void
266  myUpdateTarget = updateTarget;
267 }
268 
269 
270 void
272  myUpdateTarget = 0;
273 }
274 
275 
276 void
277 GUISelectedStorage::save(const std::string& filename, const std::set<GUIGlID>& ids) {
278  OutputDevice& dev = OutputDevice::getDevice(filename);
279  for (std::set<GUIGlID>::const_iterator i = ids.begin(); i != ids.end(); ++i) {
281  if (object != 0) {
282  std::string name = object->getFullName();
283  dev << name << "\n";
285  }
286  }
287  dev.close();
288 }
289 
290 /****************************************************************************/
virtual void selectionUpdated()=0
called when selection is updated
void close()
Closes the device and removes it from the dictionary.
~GUISelectedStorage()
Destructor.
a lane speed trigger,
GUIGlObjectType
std::set< GUIGlID > mySelected
The list of selected ids.
void toggleSelection(GUIGlID id)
Toggles selection of an object.
const std::string & getFullName() const
void select(GUIGlID id, bool update=true)
Adds the object with the given id.
void remove2Update()
Removes the dialog to be updated.
std::set< GUIGlID > myAllSelected
List of selected objects.
void clear()
Clears the list of selected objects.
bool isSelected(GUIGlObjectType type, GUIGlID id)
Returns the information whether the object with the given type and id is selected.
GUIGlID getGlID() const
Returns the numerical id of the object.
void save(const std::string &filename)
Saves the list of selected objects to a file named as given.
std::map< GUIGlObjectType, SingleTypeSelections > mySelections
map with the selections
std::set< GUIGlID > loadIDs(const std::string &filename, std::string &msgOut, GUIGlObjectType type=GLO_MAX, int maxErrors=16)
Loads a selection list (optionally with restricted type) and returns the ids of all active objects...
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
const std::set< GUIGlID > & getSelected() const
Returns the set of ids of all selected objects.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
void select(GUIGlID id)
Adds the object with the given id to the list of selected objects.
std::string load(const std::string &filename, GUIGlObjectType type=GLO_MAX)
Loads a selection list (optionally with restricted type)
a detector
unsigned int GUIGlID
Definition: GUIGlObject.h:50
compound additional
void deselect(GUIGlID id)
Deselects the object with the given id from the list of selected objects.
void deselect(GUIGlID id)
Deselects the object with the given id.
void add2Update(UpdateTarget *updateTarget)
Adds a dialog to be updated.
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
void clear()
Clears the list of selected objects.
The network - empty.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool isSelected(GUIGlID id)
Returns the information whether the object with the given id is qithin the selection.
empty max
void unblockObject(GUIGlID id)
Marks an object as unblocked.
GUISelectedStorage()
Constructor.
GUIGlObject * getObjectBlocking(GUIGlID id)
Returns the object from the container locking it.
const std::set< GUIGlID > & getSelected() const
Returns the list of selected ids.
void save(GUIGlObjectType type, const std::string &filename)
Saves a selection list.
UpdateTarget * myUpdateTarget
The dialog to be updated.