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.dev/sumo
3 // Copyright (C) 2001-2024 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>
28 #include <utils/common/ToString.h>
29 
30 #include "GUISelectedStorage.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(TLF("Unknown 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(TLF("Unknown 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(TLF("Unknown 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 void
189  if (myUpdateTarget) {
191  }
192 }
193 
194 
195 std::set<GUIGlID>
196 GUISelectedStorage::loadIDs(const std::string& filename, std::string& msgOut, GUIGlObjectType type, int maxErrors) {
197  std::set<GUIGlID> result;
198  std::ostringstream msg;
199  std::ifstream strm(filename.c_str());
200  int numIgnored = 0;
201  int numMissing = 0;
202  if (!strm.good()) {
203  msgOut = "Could not open '" + filename + "'.\n";
204  return result;
205  }
206  while (strm.good()) {
207  std::string line;
208  strm >> line;
209  if (line.length() == 0) {
210  continue;
211  }
212  if (StringUtils::startsWith(line, "node:")) {
213  line = StringUtils::replace(line, "node:", "junction:");
214  }
215 
217  if (object) {
218  if (type != GLO_MAX && (object->getType() != type)) {
219  numIgnored++;
220  if (numIgnored + numMissing <= maxErrors) {
221  msg << "Ignoring item '" << line << "' because of invalid type " << toString(object->getType()) << "\n";
222  }
223  } else {
224  result.insert(object->getGlID());
225  }
226  } else {
227  numMissing++;
228  if (numIgnored + numMissing <= maxErrors) {
229  msg << "Item '" + line + "' not found\n";
230  }
231  continue;
232  }
233  }
234  strm.close();
235  if (numIgnored + numMissing > maxErrors) {
236  msg << "...\n" << numIgnored << " objects ignored, " << numMissing << " objects not found\n";
237  }
238  msgOut = msg.str();
239  return result;
240 }
241 
242 
243 std::string
244 GUISelectedStorage::load(const std::string& filename, GUIGlObjectType type) {
245  std::string errors;
246  const std::set<GUIGlID> ids = loadIDs(filename, errors, type);
247  for (std::set<GUIGlID>::const_iterator it = ids.begin(); it != ids.end(); it++) {
248  select(*it, false);
249  }
250  if (myUpdateTarget) {
252  }
253  return errors;
254 }
255 
256 
257 void
258 GUISelectedStorage::save(GUIGlObjectType type, const std::string& filename) {
259  mySelections[type].save(filename);
260 }
261 
262 
263 void
264 GUISelectedStorage::save(const std::string& filename) const {
265  save(filename, myAllSelected);
266 }
267 
268 
269 void
271  myUpdateTarget = updateTarget;
272 }
273 
274 
275 void
277  myUpdateTarget = nullptr;
278 }
279 
280 
281 void
282 GUISelectedStorage::save(const std::string& filename, const std::set<GUIGlID>& ids) {
283  OutputDevice& dev = OutputDevice::getDevice(filename);
284  for (std::set<GUIGlID>::const_iterator i = ids.begin(); i != ids.end(); ++i) {
286  if (object != nullptr) {
287  std::string name = object->getFullName();
288  dev << name << "\n";
290  }
291  }
292  dev.close();
293 }
294 
295 
296 /****************************************************************************/
unsigned int GUIGlID
Definition: GUIGlObject.h:43
GUIGlObjectType
@ GLO_MAX
empty max
@ GLO_NETWORK
The network - empty.
#define TLF(string,...)
Definition: MsgHandler.h:317
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
const std::string & getFullName() const
Definition: GUIGlObject.h:94
GUIGlObjectType getType() const
Returns the type of the object as coded in GUIGlObjectType.
Definition: GUIGlObject.h:156
GUIGlID getGlID() const
Returns the numerical id of the object.
Definition: GUIGlObject.h:104
void unblockObject(GUIGlID id)
Marks an object as unblocked.
GUIGlObject * getObjectBlocking(GUIGlID id) const
Returns the object from the container locking it.
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
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)
void notifyChanged()
inform the update target of earlier changes
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:61
void close()
Closes the device and removes it from the dictionary.
static OutputDevice & getDevice(const std::string &name, bool usePrefix=true)
Returns the described OutputDevice.
static std::string replace(std::string str, const std::string &what, const std::string &by)
Replaces all occurrences of the second string by the third string within the first string.
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.