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 
42 // ===========================================================================
43 // member method definitions
44 // ===========================================================================
45 
46 /* -------------------------------------------------------------------------
47  * for GUISelectedStorage::SingleTypeSelections
48  * ----------------------------------------------------------------------- */
49 
51 
52 
54 
55 
56 bool
58  return mySelected.count(id) > 0;
59 }
60 
61 
62 void
64  mySelected.insert(id);
65 }
66 
67 
68 void
70  mySelected.erase(id);
71 }
72 
73 
74 void
76  mySelected.clear();
77 }
78 
79 
80 void
81 GUISelectedStorage::SingleTypeSelections::save(const std::string& filename) {
83 }
84 
85 
86 const std::set<GUIGlID>&
88  return mySelected;
89 }
90 
91 /* -------------------------------------------------------------------------
92  * for GUISelectedStorage
93  * ----------------------------------------------------------------------- */
94 
96 
97 
99 
100 
101 bool
103  switch (type) {
104  case GLO_NETWORK:
105  return false;
106  case GLO_ADDITIONAL:
107  return isSelected(GLO_TRIGGER, id) || isSelected(GLO_DETECTOR, id) || mySelections[GLO_ADDITIONAL].isSelected(id);
108  default:
109  return mySelections[type].isSelected(id);
110  }
111 }
112 
113 
114 void
117  if (!object) {
118  throw ProcessError("Unkown object in GUISelectedStorage::select (id=" + toString(id) + ").");
119  }
120  GUIGlObjectType type = object->getType();
122 
123  mySelections[type].select(id);
124  myAllSelected.insert(id);
125  if (update && myUpdateTarget) {
127  }
128 }
129 
130 
131 void
134  if (!object) {
135  throw ProcessError("Unkown object in GUISelectedStorage::deselect (id=" + toString(id) + ").");
136  }
137  GUIGlObjectType type = object->getType();
139 
140  mySelections[type].deselect(id);
141  myAllSelected.erase(id);
142  if (myUpdateTarget) {
144  }
145 }
146 
147 
148 void
151  if (!object) {
152  throw ProcessError("Unkown object in GUISelectedStorage::toggleSelection (id=" + toString(id) + ").");
153  }
154 
155  bool selected = isSelected(object->getType(), id);
156  if (!selected) {
157  select(id);
158  } else {
159  deselect(id);
160  }
162 }
163 
164 
165 const std::set<GUIGlID>&
167  return myAllSelected;
168 }
169 
170 
171 const std::set<GUIGlID>&
173  return mySelections[type].getSelected();
174 }
175 
176 
177 void
179  for (std::map<GUIGlObjectType, SingleTypeSelections>::iterator it = mySelections.begin(); it != mySelections.end(); it++) {
180  it->second.clear();
181  }
182  myAllSelected.clear();
183  if (myUpdateTarget) {
185  }
186 }
187 
188 
189 std::set<GUIGlID>
190 GUISelectedStorage::loadIDs(const std::string& filename, std::string& msgOut, GUIGlObjectType type, int maxErrors) {
191  std::set<GUIGlID> result;
192  std::ostringstream msg;
193  std::ifstream strm(filename.c_str());
194  int numIgnored = 0;
195  int numMissing = 0;
196  if (!strm.good()) {
197  msgOut = "Could not open '" + filename + "'.\n";
198  return result;
199  }
200  while (strm.good()) {
201  std::string line;
202  strm >> line;
203  if (line.length() == 0) {
204  continue;
205  }
206 
208  if (object) {
209  if (type != GLO_MAX && (object->getType() != type)) {
210  numIgnored++;
211  if (numIgnored + numMissing <= maxErrors) {
212  msg << "Ignoring item '" << line << "' because of invalid type " << toString(object->getType()) << "\n";
213  }
214  } else {
215  result.insert(object->getGlID());
216  }
217  } else {
218  numMissing++;
219  if (numIgnored + numMissing <= maxErrors) {
220  msg << "Item '" + line + "' not found\n";
221  }
222  continue;
223  }
224  }
225  strm.close();
226  if (numIgnored + numMissing > maxErrors) {
227  msg << "...\n" << numIgnored << " objects ignored, " << numMissing << " objects not found\n";
228  }
229  msgOut = msg.str();
230  return result;
231 }
232 
233 
234 std::string
235 GUISelectedStorage::load(const std::string& filename, GUIGlObjectType type) {
236  std::string errors;
237  const std::set<GUIGlID> ids = loadIDs(filename, errors, type);
238  for (std::set<GUIGlID>::const_iterator it = ids.begin(); it != ids.end(); it++) {
239  select(*it, false);
240  }
241  if (myUpdateTarget) {
243  }
244  return errors;
245 }
246 
247 
248 void
249 GUISelectedStorage::save(GUIGlObjectType type, const std::string& filename) {
250  mySelections[type].save(filename);
251 }
252 
253 
254 void
255 GUISelectedStorage::save(const std::string& filename) const {
256  save(filename, myAllSelected);
257 }
258 
259 
260 void
262  myUpdateTarget = updateTarget;
263 }
264 
265 
266 void
268  myUpdateTarget = 0;
269 }
270 
271 
272 void
273 GUISelectedStorage::save(const std::string& filename, const std::set<GUIGlID>& ids) {
274  OutputDevice& dev = OutputDevice::getDevice(filename);
275  for (std::set<GUIGlID>::const_iterator i = ids.begin(); i != ids.end(); ++i) {
277  if (object != 0) {
278  std::string name = object->getFullName();
279  dev << name << "\n";
281  }
282  }
283  dev.close();
284 }
285 
286 /****************************************************************************/
virtual void selectionUpdated()=0
called when selection is updated
void close()
Closes the device and removes it from the dictionary.
const std::set< GUIGlID > & getSelected() const
Returns the set of ids of all selected objects.
~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.
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.
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.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
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.
const std::set< GUIGlID > & getSelected() const
Returns the list of selected ids.
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.
GUIGlID getGlID() const
Returns the numerical id of the object.
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.
const std::string & getFullName() const
GUISelectedStorage()
Constructor.
GUIGlObject * getObjectBlocking(GUIGlID id)
Returns the object from the container locking it.
void save(GUIGlObjectType type, const std::string &filename)
Saves a selection list.
UpdateTarget * myUpdateTarget
The dialog to be updated.