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 /****************************************************************************/
16 // Storage for "selected" objects
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <algorithm>
28 #include "GUISelectedStorage.h"
31 #include <utils/common/ToString.h>
32 
33 
34 // ===========================================================================
35 // member method definitions
36 // ===========================================================================
37 
38 /* -------------------------------------------------------------------------
39  * for GUISelectedStorage::SingleTypeSelections
40  * ----------------------------------------------------------------------- */
41 
43 
44 
46 
47 
48 bool
50  return mySelected.count(id) > 0;
51 }
52 
53 
54 void
56  mySelected.insert(id);
57 }
58 
59 
60 void
62  mySelected.erase(id);
63 }
64 
65 
66 void
68  mySelected.clear();
69 }
70 
71 
72 void
73 GUISelectedStorage::SingleTypeSelections::save(const std::string& filename) {
74  GUISelectedStorage::save(filename, mySelected);
75 }
76 
77 
78 const std::set<GUIGlID>&
80  return mySelected;
81 }
82 
83 /* -------------------------------------------------------------------------
84  * for GUISelectedStorage
85  * ----------------------------------------------------------------------- */
86 
88 
89 
91 
92 
93 bool
95  switch (type) {
96  case GLO_NETWORK:
97  return false;
98  default:
99  return mySelections[type].isSelected(id);
100  }
101 }
102 
103 bool
105  if (o == nullptr) {
106  return false;
107  } else {
108  return isSelected(o->getType(), o->getGlID());
109  }
110 }
111 
112 void
115  if (!object) {
116  throw ProcessError("Unkown object in GUISelectedStorage::select (id=" + toString(id) + ").");
117  }
118  GUIGlObjectType type = object->getType();
120 
121  mySelections[type].select(id);
122  myAllSelected.insert(id);
123  if (update && myUpdateTarget) {
125  }
126 }
127 
128 
129 void
132  if (!object) {
133  throw ProcessError("Unkown object in GUISelectedStorage::deselect (id=" + toString(id) + ").");
134  }
135  GUIGlObjectType type = object->getType();
137 
138  mySelections[type].deselect(id);
139  myAllSelected.erase(id);
140  if (myUpdateTarget) {
142  }
143 }
144 
145 
146 void
149  if (!object) {
150  throw ProcessError("Unkown object in GUISelectedStorage::toggleSelection (id=" + toString(id) + ").");
151  }
152 
153  bool selected = isSelected(object->getType(), id);
154  if (!selected) {
155  select(id);
156  } else {
157  deselect(id);
158  }
160 }
161 
162 
163 const std::set<GUIGlID>&
165  return myAllSelected;
166 }
167 
168 
169 const std::set<GUIGlID>&
171  return mySelections[type].getSelected();
172 }
173 
174 
175 void
177  for (std::map<GUIGlObjectType, SingleTypeSelections>::iterator it = mySelections.begin(); it != mySelections.end(); it++) {
178  it->second.clear();
179  }
180  myAllSelected.clear();
181  if (myUpdateTarget) {
183  }
184 }
185 
186 
187 std::set<GUIGlID>
188 GUISelectedStorage::loadIDs(const std::string& filename, std::string& msgOut, GUIGlObjectType type, int maxErrors) {
189  std::set<GUIGlID> result;
190  std::ostringstream msg;
191  std::ifstream strm(filename.c_str());
192  int numIgnored = 0;
193  int numMissing = 0;
194  if (!strm.good()) {
195  msgOut = "Could not open '" + filename + "'.\n";
196  return result;
197  }
198  while (strm.good()) {
199  std::string line;
200  strm >> line;
201  if (line.length() == 0) {
202  continue;
203  }
204 
206  if (object) {
207  if (type != GLO_MAX && (object->getType() != type)) {
208  numIgnored++;
209  if (numIgnored + numMissing <= maxErrors) {
210  msg << "Ignoring item '" << line << "' because of invalid type " << toString(object->getType()) << "\n";
211  }
212  } else {
213  result.insert(object->getGlID());
214  }
215  } else {
216  numMissing++;
217  if (numIgnored + numMissing <= maxErrors) {
218  msg << "Item '" + line + "' not found\n";
219  }
220  continue;
221  }
222  }
223  strm.close();
224  if (numIgnored + numMissing > maxErrors) {
225  msg << "...\n" << numIgnored << " objects ignored, " << numMissing << " objects not found\n";
226  }
227  msgOut = msg.str();
228  return result;
229 }
230 
231 
232 std::string
233 GUISelectedStorage::load(const std::string& filename, GUIGlObjectType type) {
234  std::string errors;
235  const std::set<GUIGlID> ids = loadIDs(filename, errors, type);
236  for (std::set<GUIGlID>::const_iterator it = ids.begin(); it != ids.end(); it++) {
237  select(*it, false);
238  }
239  if (myUpdateTarget) {
241  }
242  return errors;
243 }
244 
245 
246 void
247 GUISelectedStorage::save(GUIGlObjectType type, const std::string& filename) {
248  mySelections[type].save(filename);
249 }
250 
251 
252 void
253 GUISelectedStorage::save(const std::string& filename) const {
254  save(filename, myAllSelected);
255 }
256 
257 
258 void
260  myUpdateTarget = updateTarget;
261 }
262 
263 
264 void
266  myUpdateTarget = nullptr;
267 }
268 
269 
270 void
271 GUISelectedStorage::save(const std::string& filename, const std::set<GUIGlID>& ids) {
272  OutputDevice& dev = OutputDevice::getDevice(filename);
273  for (std::set<GUIGlID>::const_iterator i = ids.begin(); i != ids.end(); ++i) {
275  if (object != nullptr) {
276  std::string name = object->getFullName();
277  dev << name << "\n";
279  }
280  }
281  dev.close();
282 }
283 
284 /****************************************************************************/
GUIGlObject::getType
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
Definition: GUIGlObject.cpp:180
GLO_MAX
@ GLO_MAX
empty max
Definition: GUIGlObjectTypes.h:165
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:55
GUISelectedStorage::SingleTypeSelections::SingleTypeSelections
SingleTypeSelections()
Constructor.
Definition: GUISelectedStorage.cpp:42
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:63
GUISelectedStorage::save
void save(GUIGlObjectType type, const std::string &filename)
Saves a selection list.
Definition: GUISelectedStorage.cpp:247
GUISelectedStorage::SingleTypeSelections::clear
void clear()
Clears the list of selected objects.
Definition: GUISelectedStorage.cpp:67
GUISelectedStorage::SingleTypeSelections::save
void save(const std::string &filename)
Saves the list of selected objects to a file named as given.
Definition: GUISelectedStorage.cpp:73
GUISelectedStorage::mySelections
std::map< GUIGlObjectType, SingleTypeSelections > mySelections
map with the selections
Definition: GUISelectedStorage.h:273
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:188
GUIGlObject::getFullName
const std::string & getFullName() const
Definition: GUIGlObject.cpp:137
OutputDevice::close
void close()
Closes the device and removes it from the dictionary.
Definition: OutputDevice.cpp:207
GUISelectedStorage::load
std::string load(const std::string &filename, GUIGlObjectType type=GLO_MAX)
Loads a selection list (optionally with restricted type)
Definition: GUISelectedStorage.cpp:233
GUISelectedStorage::SingleTypeSelections::~SingleTypeSelections
~SingleTypeSelections()
Destructor.
Definition: GUISelectedStorage.cpp:45
GUIGlObjectStorage.h
GUIGlObjectType
GUIGlObjectType
Definition: GUIGlObjectTypes.h:39
GUISelectedStorage::select
void select(GUIGlID id, bool update=true)
Adds the object with the given id.
Definition: GUISelectedStorage.cpp:113
GUIGlObject::getGlID
GUIGlID getGlID() const
Returns the numerical id of the object.
Definition: GUIGlObject.cpp:149
update
GUISelectedStorage::remove2Update
void remove2Update()
Removes the dialog to be updated.
Definition: GUISelectedStorage.cpp:265
GUISelectedStorage::myAllSelected
std::set< GUIGlID > myAllSelected
List of selected objects.
Definition: GUISelectedStorage.h:276
OutputDevice.h
ProcessError
Definition: UtilExceptions.h:39
GUIDialog_GLChosenEditor.h
GUIGlObjectStorage::getObjectBlocking
GUIGlObject * getObjectBlocking(GUIGlID id)
Returns the object from the container locking it.
Definition: GUIGlObjectStorage.cpp:62
GUISelectedStorage::SingleTypeSelections::getSelected
const std::set< GUIGlID > & getSelected() const
Returns the list of selected ids.
Definition: GUISelectedStorage.cpp:79
GUIGlObject
Definition: GUIGlObject.h:65
GUISelectedStorage::isSelected
bool isSelected(const GUIGlObject *o)
Definition: GUISelectedStorage.cpp:104
GUISelectedStorage::clear
void clear()
Clears the list of selected objects.
Definition: GUISelectedStorage.cpp:176
GUIGlObjectStorage::unblockObject
void unblockObject(GUIGlID id)
Marks an object as unblocked.
Definition: GUIGlObjectStorage.cpp:119
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
OutputDevice::getDevice
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
Definition: OutputDevice.cpp:54
GUISelectedStorage::SingleTypeSelections::isSelected
bool isSelected(GUIGlID id)
Returns the information whether the object with the given id is qithin the selection.
Definition: GUISelectedStorage.cpp:49
GUIGlID
unsigned int GUIGlID
Definition: GUIGlObject.h:42
GUISelectedStorage::GUISelectedStorage
GUISelectedStorage()
Constructor.
Definition: GUISelectedStorage.cpp:87
GUIGlObjectStorage::gIDStorage
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
Definition: GUIGlObjectStorage.h:140
config.h
GUISelectedStorage::myUpdateTarget
UpdateTarget * myUpdateTarget
The dialog to be updated.
Definition: GUISelectedStorage.h:279
GUISelectedStorage::UpdateTarget
Definition: GUISelectedStorage.h:73
GUISelectedStorage::getSelected
const std::set< GUIGlID > & getSelected() const
Returns the set of ids of all selected objects.
Definition: GUISelectedStorage.cpp:164
GUISelectedStorage::toggleSelection
void toggleSelection(GUIGlID id)
Toggles selection of an object.
Definition: GUISelectedStorage.cpp:147
GUISelectedStorage::~GUISelectedStorage
~GUISelectedStorage()
Destructor.
Definition: GUISelectedStorage.cpp:90
GUISelectedStorage::SingleTypeSelections::deselect
void deselect(GUIGlID id)
Deselects the object with the given id from the list of selected objects.
Definition: GUISelectedStorage.cpp:61
GLO_NETWORK
@ GLO_NETWORK
The network - empty.
Definition: GUIGlObjectTypes.h:41
GUISelectedStorage::deselect
void deselect(GUIGlID id)
Deselects the object with the given id.
Definition: GUISelectedStorage.cpp:130
GUISelectedStorage::add2Update
void add2Update(UpdateTarget *updateTarget)
Adds a dialog to be updated.
Definition: GUISelectedStorage.cpp:259
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:94