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-2019 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) {
75  GUISelectedStorage::save(filename, mySelected);
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 bool
106  if (o == nullptr) {
107  return false;
108  } else {
109  return isSelected(o->getType(), o->getGlID());
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 = nullptr;
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 != nullptr) {
277  std::string name = object->getFullName();
278  dev << name << "\n";
280  }
281  }
282  dev.close();
283 }
284 
285 /****************************************************************************/
GUIGlObject::getType
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
Definition: GUIGlObject.cpp:181
GLO_MAX
empty max
Definition: GUIGlObjectTypes.h:166
GUIGlObject.h
ToString.h
GUISelectedStorage::SingleTypeSelections::select
void select(GUIGlID id)
Adds the object with the given id to the list of selected objects.
Definition: GUISelectedStorage.cpp:56
GUISelectedStorage::SingleTypeSelections::SingleTypeSelections
SingleTypeSelections()
Constructor.
Definition: GUISelectedStorage.cpp:43
GUISelectedStorage::UpdateTarget::selectionUpdated
virtual void selectionUpdated()=0
called when selection is updated
GUISelectedStorage.h
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
GUISelectedStorage::save
void save(GUIGlObjectType type, const std::string &filename)
Saves a selection list.
Definition: GUISelectedStorage.cpp:248
GUISelectedStorage::SingleTypeSelections::clear
void clear()
Clears the list of selected objects.
Definition: GUISelectedStorage.cpp:68
GUISelectedStorage::SingleTypeSelections::save
void save(const std::string &filename)
Saves the list of selected objects to a file named as given.
Definition: GUISelectedStorage.cpp:74
GUISelectedStorage::mySelections
std::map< GUIGlObjectType, SingleTypeSelections > mySelections
map with the selections
Definition: GUISelectedStorage.h:274
GUISelectedStorage::loadIDs
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.
Definition: GUISelectedStorage.cpp:189
GUIGlObject::getFullName
const std::string & getFullName() const
Definition: GUIGlObject.cpp:138
OutputDevice::close
void close()
Closes the device and removes it from the dictionary.
Definition: OutputDevice.cpp:208
GUISelectedStorage::load
std::string load(const std::string &filename, GUIGlObjectType type=GLO_MAX)
Loads a selection list (optionally with restricted type)
Definition: GUISelectedStorage.cpp:234
GUISelectedStorage::SingleTypeSelections::~SingleTypeSelections
~SingleTypeSelections()
Destructor.
Definition: GUISelectedStorage.cpp:46
GUIGlObjectStorage.h
GUIGlObjectType
GUIGlObjectType
Definition: GUIGlObjectTypes.h:40
GUISelectedStorage::select
void select(GUIGlID id, bool update=true)
Adds the object with the given id.
Definition: GUISelectedStorage.cpp:114
GUIGlObject::getGlID
GUIGlID getGlID() const
Returns the numerical id of the object.
Definition: GUIGlObject.cpp:150
update
GUISelectedStorage::remove2Update
void remove2Update()
Removes the dialog to be updated.
Definition: GUISelectedStorage.cpp:266
GUISelectedStorage::myAllSelected
std::set< GUIGlID > myAllSelected
List of selected objects.
Definition: GUISelectedStorage.h:277
OutputDevice.h
ProcessError
Definition: UtilExceptions.h:40
GUIDialog_GLChosenEditor.h
GUIGlObjectStorage::getObjectBlocking
GUIGlObject * getObjectBlocking(GUIGlID id)
Returns the object from the container locking it.
Definition: GUIGlObjectStorage.cpp:63
GUISelectedStorage::SingleTypeSelections::getSelected
const std::set< GUIGlID > & getSelected() const
Returns the list of selected ids.
Definition: GUISelectedStorage.cpp:80
GUIGlObject
Definition: GUIGlObject.h:66
GUISelectedStorage::isSelected
bool isSelected(const GUIGlObject *o)
Definition: GUISelectedStorage.cpp:105
GUISelectedStorage::clear
void clear()
Clears the list of selected objects.
Definition: GUISelectedStorage.cpp:177
GUIGlObjectStorage::unblockObject
void unblockObject(GUIGlID id)
Marks an object as unblocked.
Definition: GUIGlObjectStorage.cpp:120
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
OutputDevice::getDevice
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
Definition: OutputDevice.cpp:55
GUISelectedStorage::SingleTypeSelections::isSelected
bool isSelected(GUIGlID id)
Returns the information whether the object with the given id is qithin the selection.
Definition: GUISelectedStorage.cpp:50
GUIGlID
unsigned int GUIGlID
Definition: GUIGlObject.h:43
GUISelectedStorage::GUISelectedStorage
GUISelectedStorage()
Constructor.
Definition: GUISelectedStorage.cpp:88
GUIGlObjectStorage::gIDStorage
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
Definition: GUIGlObjectStorage.h:141
config.h
GUISelectedStorage::myUpdateTarget
UpdateTarget * myUpdateTarget
The dialog to be updated.
Definition: GUISelectedStorage.h:280
GUISelectedStorage::UpdateTarget
Definition: GUISelectedStorage.h:74
GUISelectedStorage::getSelected
const std::set< GUIGlID > & getSelected() const
Returns the set of ids of all selected objects.
Definition: GUISelectedStorage.cpp:165
GUISelectedStorage::toggleSelection
void toggleSelection(GUIGlID id)
Toggles selection of an object.
Definition: GUISelectedStorage.cpp:148
GUISelectedStorage::~GUISelectedStorage
~GUISelectedStorage()
Destructor.
Definition: GUISelectedStorage.cpp:91
GUISelectedStorage::SingleTypeSelections::deselect
void deselect(GUIGlID id)
Deselects the object with the given id from the list of selected objects.
Definition: GUISelectedStorage.cpp:62
GLO_NETWORK
The network - empty.
Definition: GUIGlObjectTypes.h:42
GUISelectedStorage::deselect
void deselect(GUIGlID id)
Deselects the object with the given id.
Definition: GUISelectedStorage.cpp:131
GUISelectedStorage::add2Update
void add2Update(UpdateTarget *updateTarget)
Adds a dialog to be updated.
Definition: GUISelectedStorage.cpp:260
GUISelectedStorage::isSelected
bool isSelected(GUIGlObjectType type, GUIGlID id)
Returns the information whether the object with the given type and id is selected.
Definition: GUISelectedStorage.cpp:95