Eclipse 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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // Storage for "selected" objects
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <algorithm>
27 #include "GUISelectedStorage.h"
30 #include <utils/common/ToString.h>
31 
32 
33 // ===========================================================================
34 // member method definitions
35 // ===========================================================================
36 
37 /* -------------------------------------------------------------------------
38  * for GUISelectedStorage::SingleTypeSelections
39  * ----------------------------------------------------------------------- */
40 
42 
43 
45 
46 
47 bool
49  return mySelected.count(id) > 0;
50 }
51 
52 
53 void
55  mySelected.insert(id);
56 }
57 
58 
59 void
61  mySelected.erase(id);
62 }
63 
64 
65 void
67  mySelected.clear();
68 }
69 
70 
71 void
72 GUISelectedStorage::SingleTypeSelections::save(const std::string& filename) {
73  GUISelectedStorage::save(filename, mySelected);
74 }
75 
76 
77 const std::set<GUIGlID>&
79  return mySelected;
80 }
81 
82 /* -------------------------------------------------------------------------
83  * for GUISelectedStorage
84  * ----------------------------------------------------------------------- */
85 
87 
88 
90 
91 
92 bool
94  switch (type) {
95  case GLO_NETWORK:
96  return false;
97  default:
98  return mySelections[type].isSelected(id);
99  }
100 }
101 
102 bool
104  if (o == nullptr) {
105  return false;
106  } else {
107  return isSelected(o->getType(), o->getGlID());
108  }
109 }
110 
111 void
114  if (!object) {
115  throw ProcessError("Unkown object in GUISelectedStorage::select (id=" + toString(id) + ").");
116  }
117  GUIGlObjectType type = object->getType();
119 
120  mySelections[type].select(id);
121  myAllSelected.insert(id);
122  if (update && myUpdateTarget) {
124  }
125 }
126 
127 
128 void
131  if (!object) {
132  throw ProcessError("Unkown object in GUISelectedStorage::deselect (id=" + toString(id) + ").");
133  }
134  GUIGlObjectType type = object->getType();
136 
137  mySelections[type].deselect(id);
138  myAllSelected.erase(id);
139  if (myUpdateTarget) {
141  }
142 }
143 
144 
145 void
148  if (!object) {
149  throw ProcessError("Unkown object in GUISelectedStorage::toggleSelection (id=" + toString(id) + ").");
150  }
151 
152  bool selected = isSelected(object->getType(), id);
153  if (!selected) {
154  select(id);
155  } else {
156  deselect(id);
157  }
159 }
160 
161 
162 const std::set<GUIGlID>&
164  return myAllSelected;
165 }
166 
167 
168 const std::set<GUIGlID>&
170  return mySelections[type].getSelected();
171 }
172 
173 
174 void
176  for (std::map<GUIGlObjectType, SingleTypeSelections>::iterator it = mySelections.begin(); it != mySelections.end(); it++) {
177  it->second.clear();
178  }
179  myAllSelected.clear();
180  if (myUpdateTarget) {
182  }
183 }
184 
185 
186 std::set<GUIGlID>
187 GUISelectedStorage::loadIDs(const std::string& filename, std::string& msgOut, GUIGlObjectType type, int maxErrors) {
188  std::set<GUIGlID> result;
189  std::ostringstream msg;
190  std::ifstream strm(filename.c_str());
191  int numIgnored = 0;
192  int numMissing = 0;
193  if (!strm.good()) {
194  msgOut = "Could not open '" + filename + "'.\n";
195  return result;
196  }
197  while (strm.good()) {
198  std::string line;
199  strm >> line;
200  if (line.length() == 0) {
201  continue;
202  }
203 
205  if (object) {
206  if (type != GLO_MAX && (object->getType() != type)) {
207  numIgnored++;
208  if (numIgnored + numMissing <= maxErrors) {
209  msg << "Ignoring item '" << line << "' because of invalid type " << toString(object->getType()) << "\n";
210  }
211  } else {
212  result.insert(object->getGlID());
213  }
214  } else {
215  numMissing++;
216  if (numIgnored + numMissing <= maxErrors) {
217  msg << "Item '" + line + "' not found\n";
218  }
219  continue;
220  }
221  }
222  strm.close();
223  if (numIgnored + numMissing > maxErrors) {
224  msg << "...\n" << numIgnored << " objects ignored, " << numMissing << " objects not found\n";
225  }
226  msgOut = msg.str();
227  return result;
228 }
229 
230 
231 std::string
232 GUISelectedStorage::load(const std::string& filename, GUIGlObjectType type) {
233  std::string errors;
234  const std::set<GUIGlID> ids = loadIDs(filename, errors, type);
235  for (std::set<GUIGlID>::const_iterator it = ids.begin(); it != ids.end(); it++) {
236  select(*it, false);
237  }
238  if (myUpdateTarget) {
240  }
241  return errors;
242 }
243 
244 
245 void
246 GUISelectedStorage::save(GUIGlObjectType type, const std::string& filename) {
247  mySelections[type].save(filename);
248 }
249 
250 
251 void
252 GUISelectedStorage::save(const std::string& filename) const {
253  save(filename, myAllSelected);
254 }
255 
256 
257 void
259  myUpdateTarget = updateTarget;
260 }
261 
262 
263 void
265  myUpdateTarget = nullptr;
266 }
267 
268 
269 void
270 GUISelectedStorage::save(const std::string& filename, const std::set<GUIGlID>& ids) {
271  OutputDevice& dev = OutputDevice::getDevice(filename);
272  for (std::set<GUIGlID>::const_iterator i = ids.begin(); i != ids.end(); ++i) {
274  if (object != nullptr) {
275  std::string name = object->getFullName();
276  dev << name << "\n";
278  }
279  }
280  dev.close();
281 }
282 
283 
284 /****************************************************************************/
unsigned int GUIGlID
Definition: GUIGlObject.h:40
GUIGlObjectType
@ GLO_MAX
empty max
@ GLO_NETWORK
The network - empty.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
const std::string & getFullName() const
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
GUIGlID getGlID() const
Returns the numerical id of the object.
void unblockObject(GUIGlID id)
Marks an object as unblocked.
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
GUIGlObject * getObjectBlocking(GUIGlID id)
Returns the object from the container locking it.
void clear()
Clears the list of selected objects.
void select(GUIGlID id)
Adds the object with the given id to 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 from the list of selected objects.
void save(const std::string &filename)
Saves the list of selected objects to a file named as given.
bool isSelected(GUIGlID id)
Returns the information whether the object with the given id is qithin the selection.
virtual void selectionUpdated()=0
called when selection is updated
void save(GUIGlObjectType type, const std::string &filename)
Saves a selection list.
std::map< GUIGlObjectType, SingleTypeSelections > mySelections
map with the selections
std::string load(const std::string &filename, GUIGlObjectType type=GLO_MAX)
Loads a selection list (optionally with restricted type)
std::set< GUIGlID > myAllSelected
List of selected objects.
~GUISelectedStorage()
Destructor.
void clear()
Clears the list of selected objects.
void toggleSelection(GUIGlID id)
Toggles selection of an object.
void select(GUIGlID id, bool update=true)
Adds the object with the given id.
UpdateTarget * myUpdateTarget
The dialog to be updated.
void add2Update(UpdateTarget *updateTarget)
Adds a dialog to be updated.
void remove2Update()
Removes the dialog to be updated.
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.
bool isSelected(GUIGlObjectType type, GUIGlID id)
Returns the information whether the object with the given type and id is selected.
GUISelectedStorage()
Constructor.
void deselect(GUIGlID id)
Deselects the object with the given id.
const std::set< GUIGlID > & getSelected() const
Returns the set of ids of all selected objects.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:60
void close()
Closes the device and removes it from the dictionary.
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.