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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Storage for "selected" objects
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <algorithm>
29 #include "GUISelectedStorage.h"
32 #include <utils/common/ToString.h>
33 
34 
35 // ===========================================================================
36 // member method definitions
37 // ===========================================================================
38 
39 /* -------------------------------------------------------------------------
40  * for GUISelectedStorage::SingleTypeSelections
41  * ----------------------------------------------------------------------- */
42 
44 
45 
47 
48 
49 bool
51  return mySelected.count(id) > 0;
52 }
53 
54 
55 void
57  mySelected.insert(id);
58 }
59 
60 
61 void
63  mySelected.erase(id);
64 }
65 
66 
67 void
69  mySelected.clear();
70 }
71 
72 
73 void
74 GUISelectedStorage::SingleTypeSelections::save(const std::string& filename) {
76 }
77 
78 
79 const std::set<GUIGlID>&
81  return mySelected;
82 }
83 
84 /* -------------------------------------------------------------------------
85  * for GUISelectedStorage
86  * ----------------------------------------------------------------------- */
87 
89 
90 
92 
93 
94 bool
96  switch (type) {
97  case GLO_NETWORK:
98  return false;
99  default:
100  return mySelections[type].isSelected(id);
101  }
102 }
103 
104 
105 void
108  if (!object) {
109  throw ProcessError("Unkown object in GUISelectedStorage::select (id=" + toString(id) + ").");
110  }
111  GUIGlObjectType type = object->getType();
113 
114  mySelections[type].select(id);
115  myAllSelected.insert(id);
116  if (update && myUpdateTarget) {
118  }
119 }
120 
121 
122 void
125  if (!object) {
126  throw ProcessError("Unkown object in GUISelectedStorage::deselect (id=" + toString(id) + ").");
127  }
128  GUIGlObjectType type = object->getType();
130 
131  mySelections[type].deselect(id);
132  myAllSelected.erase(id);
133  if (myUpdateTarget) {
135  }
136 }
137 
138 
139 void
142  if (!object) {
143  throw ProcessError("Unkown object in GUISelectedStorage::toggleSelection (id=" + toString(id) + ").");
144  }
145 
146  bool selected = isSelected(object->getType(), id);
147  if (!selected) {
148  select(id);
149  } else {
150  deselect(id);
151  }
153 }
154 
155 
156 const std::set<GUIGlID>&
158  return myAllSelected;
159 }
160 
161 
162 const std::set<GUIGlID>&
164  return mySelections[type].getSelected();
165 }
166 
167 
168 void
170  for (std::map<GUIGlObjectType, SingleTypeSelections>::iterator it = mySelections.begin(); it != mySelections.end(); it++) {
171  it->second.clear();
172  }
173  myAllSelected.clear();
174  if (myUpdateTarget) {
176  }
177 }
178 
179 
180 std::set<GUIGlID>
181 GUISelectedStorage::loadIDs(const std::string& filename, std::string& msgOut, GUIGlObjectType type, int maxErrors) {
182  std::set<GUIGlID> result;
183  std::ostringstream msg;
184  std::ifstream strm(filename.c_str());
185  int numIgnored = 0;
186  int numMissing = 0;
187  if (!strm.good()) {
188  msgOut = "Could not open '" + filename + "'.\n";
189  return result;
190  }
191  while (strm.good()) {
192  std::string line;
193  strm >> line;
194  if (line.length() == 0) {
195  continue;
196  }
197 
199  if (object) {
200  if (type != GLO_MAX && (object->getType() != type)) {
201  numIgnored++;
202  if (numIgnored + numMissing <= maxErrors) {
203  msg << "Ignoring item '" << line << "' because of invalid type " << toString(object->getType()) << "\n";
204  }
205  } else {
206  result.insert(object->getGlID());
207  }
208  } else {
209  numMissing++;
210  if (numIgnored + numMissing <= maxErrors) {
211  msg << "Item '" + line + "' not found\n";
212  }
213  continue;
214  }
215  }
216  strm.close();
217  if (numIgnored + numMissing > maxErrors) {
218  msg << "...\n" << numIgnored << " objects ignored, " << numMissing << " objects not found\n";
219  }
220  msgOut = msg.str();
221  return result;
222 }
223 
224 
225 std::string
226 GUISelectedStorage::load(const std::string& filename, GUIGlObjectType type) {
227  std::string errors;
228  const std::set<GUIGlID> ids = loadIDs(filename, errors, type);
229  for (std::set<GUIGlID>::const_iterator it = ids.begin(); it != ids.end(); it++) {
230  select(*it, false);
231  }
232  if (myUpdateTarget) {
234  }
235  return errors;
236 }
237 
238 
239 void
240 GUISelectedStorage::save(GUIGlObjectType type, const std::string& filename) {
241  mySelections[type].save(filename);
242 }
243 
244 
245 void
246 GUISelectedStorage::save(const std::string& filename) const {
247  save(filename, myAllSelected);
248 }
249 
250 
251 void
253  myUpdateTarget = updateTarget;
254 }
255 
256 
257 void
259  myUpdateTarget = 0;
260 }
261 
262 
263 void
264 GUISelectedStorage::save(const std::string& filename, const std::set<GUIGlID>& ids) {
265  OutputDevice& dev = OutputDevice::getDevice(filename);
266  for (std::set<GUIGlID>::const_iterator i = ids.begin(); i != ids.end(); ++i) {
268  if (object != 0) {
269  std::string name = object->getFullName();
270  dev << name << "\n";
272  }
273  }
274  dev.close();
275 }
276 
277 /****************************************************************************/
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.
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:49
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)
unsigned int GUIGlID
Definition: GUIGlObject.h:43
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:64
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.