SUMO - Simulation of Urban MObility
GUISelectedStorage.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Storage for "selected" objects
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <algorithm>
35 #include "GUISelectedStorage.h"
38 #include <utils/common/ToString.h>
39 
40 
41 // ===========================================================================
42 // member method definitions
43 // ===========================================================================
44 
45 /* -------------------------------------------------------------------------
46  * for GUISelectedStorage::SingleTypeSelections
47  * ----------------------------------------------------------------------- */
48 
50 
51 
53 
54 
55 bool
57  return mySelected.count(id) > 0;
58 }
59 
60 
61 void
63  mySelected.insert(id);
64 }
65 
66 
67 void
69  mySelected.erase(id);
70 }
71 
72 
73 void
75  mySelected.clear();
76 }
77 
78 
79 void
80 GUISelectedStorage::SingleTypeSelections::save(const std::string& filename) {
82 }
83 
84 
85 const std::set<GUIGlID>&
87  return mySelected;
88 }
89 
90 /* -------------------------------------------------------------------------
91  * for GUISelectedStorage
92  * ----------------------------------------------------------------------- */
93 
95 
96 
98 
99 
100 bool
102  switch (type) {
103  case GLO_NETWORK:
104  return false;
105  case GLO_ADDITIONAL:
106  return isSelected(GLO_TRIGGER, id) || isSelected(GLO_DETECTOR, id) || mySelections[GLO_ADDITIONAL].isSelected(id);
107  default:
108  return mySelections[type].isSelected(id);
109  }
110 }
111 
112 
113 void
116  if (!object) {
117  throw ProcessError("Unkown object in GUISelectedStorage::select (id=" + toString(id) + ").");
118  }
119  GUIGlObjectType type = object->getType();
121 
122  mySelections[type].select(id);
123  myAllSelected.insert(id);
124  if (update && myUpdateTarget) {
126  }
127 }
128 
129 
130 void
133  if (!object) {
134  throw ProcessError("Unkown object in GUISelectedStorage::deselect (id=" + toString(id) + ").");
135  }
136  GUIGlObjectType type = object->getType();
138 
139  mySelections[type].deselect(id);
140  myAllSelected.erase(id);
141  if (myUpdateTarget) {
143  }
144 }
145 
146 
147 void
150  if (!object) {
151  throw ProcessError("Unkown object in GUISelectedStorage::toggleSelection (id=" + toString(id) + ").");
152  }
153 
154  bool selected = isSelected(object->getType(), id);
155  if (!selected) {
156  select(id);
157  } else {
158  deselect(id);
159  }
161 }
162 
163 
164 const std::set<GUIGlID>&
166  return myAllSelected;
167 }
168 
169 
170 const std::set<GUIGlID>&
172  return mySelections[type].getSelected();
173 }
174 
175 
176 void
178  for (std::map<GUIGlObjectType, SingleTypeSelections>::iterator it = mySelections.begin(); it != mySelections.end(); it++) {
179  it->second.clear();
180  }
181  myAllSelected.clear();
182  if (myUpdateTarget) {
184  }
185 }
186 
187 
188 std::set<GUIGlID>
189 GUISelectedStorage::loadIDs(const std::string& filename, std::string& msgOut, GUIGlObjectType type, int maxErrors) {
190  std::set<GUIGlID> result;
191  std::ostringstream msg;
192  std::ifstream strm(filename.c_str());
193  int numIgnored = 0;
194  int numMissing = 0;
195  if (!strm.good()) {
196  msgOut = "Could not open '" + filename + "'.\n";
197  return result;
198  }
199  while (strm.good()) {
200  std::string line;
201  strm >> line;
202  if (line.length() == 0) {
203  continue;
204  }
205 
207  if (object) {
208  if (type != GLO_MAX && (object->getType() != type)) {
209  numIgnored++;
210  if (numIgnored + numMissing <= maxErrors) {
211  msg << "Ignoring item '" << line << "' because of invalid type " << toString(object->getType()) << "\n";
212  }
213  } else {
214  result.insert(object->getGlID());
215  }
216  } else {
217  numMissing++;
218  if (numIgnored + numMissing <= maxErrors) {
219  msg << "Item '" + line + "' not found\n";
220  }
221  continue;
222  }
223  }
224  strm.close();
225  if (numIgnored + numMissing > maxErrors) {
226  msg << "...\n" << numIgnored << " objects ignored, " << numMissing << " objects not found\n";
227  }
228  msgOut = msg.str();
229  return result;
230 }
231 
232 
233 std::string
234 GUISelectedStorage::load(const std::string& filename, GUIGlObjectType type) {
235  std::string errors;
236  const std::set<GUIGlID> ids = loadIDs(filename, errors, type);
237  for (std::set<GUIGlID>::const_iterator it = ids.begin(); it != ids.end(); it++) {
238  select(*it, false);
239  }
240  if (myUpdateTarget) {
242  }
243  return errors;
244 }
245 
246 
247 void
248 GUISelectedStorage::save(GUIGlObjectType type, const std::string& filename) {
249  mySelections[type].save(filename);
250 }
251 
252 
253 void
254 GUISelectedStorage::save(const std::string& filename) const {
255  save(filename, myAllSelected);
256 }
257 
258 
259 void
261  myUpdateTarget = updateTarget;
262 }
263 
264 
265 void
267  myUpdateTarget = 0;
268 }
269 
270 
271 void
272 GUISelectedStorage::save(const std::string& filename, const std::set<GUIGlID>& ids) {
273  OutputDevice& dev = OutputDevice::getDevice(filename);
274  for (std::set<GUIGlID>::const_iterator i = ids.begin(); i != ids.end(); ++i) {
276  if (object != 0) {
277  std::string name = object->getFullName();
278  dev << name << "\n";
280  }
281  }
282  dev.close();
283 }
284 
285 /****************************************************************************/
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: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:49
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:70
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.