Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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-2025 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>
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
48bool
50 return mySelected.count(id) > 0;
51}
52
53
54void
58
59
60void
64
65
66void
70
71
72void
74 GUISelectedStorage::save(filename, mySelected);
75}
76
77
78const std::unordered_set<GUIGlID>&
82
83/* -------------------------------------------------------------------------
84 * for GUISelectedStorage
85 * ----------------------------------------------------------------------- */
86
88
89
91
92
93bool
95 switch (type) {
96 case GLO_NETWORK:
97 return false;
98 default:
99 return mySelections[type].isSelected(id);
100 }
101}
102
103bool
105 if (o == nullptr) {
106 return false;
107 } else {
108 return isSelected(o->getType(), o->getGlID());
109 }
110}
111
112void
115 if (!object) {
116 throw ProcessError("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
129void
132 if (!object) {
133 throw ProcessError("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
146void
148 mySelections[type].deselect(id);
149 myAllSelected.erase(id);
150}
151
152
153void
156 if (!object) {
157 throw ProcessError("Unknown object in GUISelectedStorage::toggleSelection (id=" + toString(id) + ").");
158 }
159
160 bool selected = isSelected(object->getType(), id);
161 if (!selected) {
162 select(id);
163 } else {
164 deselect(id);
165 }
167}
168
169
170const std::unordered_set<GUIGlID>&
174
175
176const std::unordered_set<GUIGlID>&
178 return mySelections[type].getSelected();
179}
180
181
182void
184 for (auto& selection : mySelections) {
185 selection.second.clear();
186 }
187 myAllSelected.clear();
188 if (myUpdateTarget) {
190 }
191}
192
193
194void
200
201
202std::set<GUIGlID>
203GUISelectedStorage::loadIDs(std::istream& strm, std::string& msgOut, GUIGlObjectType type, std::ostream* dynamicNotFound, int maxErrors) {
204 std::set<GUIGlID> result;
205 std::ostringstream msg;
206 int numIgnored = 0;
207 int numMissing = 0;
208 while (strm.good()) {
209 std::string line;
210 strm >> line;
211 if (line.length() == 0) {
212 continue;
213 }
214 if (StringUtils::startsWith(line, "node:")) {
215 line = StringUtils::replace(line, "node:", "junction:");
216 }
217
219 if (object) {
220 if (type != GLO_MAX && (object->getType() != type)) {
221 numIgnored++;
222 if (numIgnored + numMissing <= maxErrors) {
223 msg << TLF("Ignoring item '%' because of invalid type %\n", line, toString(object->getType()));
224 }
225 } else {
226 result.insert(object->getGlID());
227 }
228 } else {
229 numMissing++;
230 if (dynamicNotFound != nullptr && (
231 StringUtils::startsWith(line, "vehicle:") ||
232 StringUtils::startsWith(line, "person:") ||
233 StringUtils::startsWith(line, "container:"))) {
234 (*dynamicNotFound) << line << "\n";
235 } else {
236 if (numIgnored + numMissing <= maxErrors) {
237 msg << TLF("Item '%' not found\n", line);
238 }
239 }
240 continue;
241 }
242 }
243 if (numIgnored + numMissing > maxErrors) {
244 msg << "...\n" << TLF("% objects ignored, % objects not found\n", numIgnored, numMissing);
245 }
246 msgOut = msg.str();
247 return result;
248}
249
250
251std::string
252GUISelectedStorage::load(const std::string& filename, GUIGlObjectType type, std::ostream* dynamicNotFound) {
253 std::ifstream strm(filename.c_str());
254 if (!strm.good()) {
255 return TLF("Could not open '%'.\n", filename);
256 }
257 std::string errors = load(strm, type, dynamicNotFound);
258 strm.close();
259 return errors;
260}
261
262
263std::string
264GUISelectedStorage::load(std::istream& strm, GUIGlObjectType type, std::ostream* dynamicNotFound) {
265 std::string errors;
266 const std::set<GUIGlID> ids = loadIDs(strm, errors, type, dynamicNotFound);
267 for (const auto glID : ids) {
268 select(glID, false);
269 }
270 if (myUpdateTarget) {
272 }
273 return errors;
274}
275
276
277void
278GUISelectedStorage::save(GUIGlObjectType type, const std::string& filename) {
279 mySelections[type].save(filename);
280}
281
282
283void
284GUISelectedStorage::save(const std::string& filename) const {
285 save(filename, myAllSelected);
286}
287
288
289void
291 myUpdateTarget = updateTarget;
292}
293
294
295void
299
300
301void
302GUISelectedStorage::save(const std::string& filename, const std::unordered_set<GUIGlID>& ids) {
303 OutputDevice& dev = OutputDevice::getDevice(filename);
304 for (const auto glID : ids) {
306 if (object != nullptr) {
307 std::string name = object->getFullName();
308 dev << name << "\n";
310 }
311 }
312 dev.close();
313}
314
315
316/****************************************************************************/
unsigned int GUIGlID
Definition GUIGlObject.h:44
GUIGlObjectType
@ GLO_MAX
empty max
@ GLO_NETWORK
The network - empty.
#define TLF(string,...)
Definition MsgHandler.h:306
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
const std::string & getFullName() const
Definition GUIGlObject.h:95
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.
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::unordered_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
void notifyChanged()
inform the update target of earlier changes
~GUISelectedStorage()
Destructor.
void clear()
Clears the list of selected objects.
std::set< GUIGlID > loadIDs(std::istream &strm, std::string &msgOut, GUIGlObjectType type=GLO_MAX, std::ostream *dynamicNotFound=nullptr, int maxErrors=16)
Loads a selection list (optionally with restricted type) and returns the ids of all active 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.
bool isSelected(GUIGlObjectType type, GUIGlID id)
Returns the information whether the object with the given type and id is selected.
GUISelectedStorage()
Constructor.
std::unordered_set< GUIGlID > myAllSelected
List of selected objects.
void deselect(GUIGlID id)
Deselects the object with the given id.
const std::unordered_set< GUIGlID > & getSelected() const
Returns the set of ids of all selected objects.
std::string load(const std::string &filename, GUIGlObjectType type=GLO_MAX, std::ostream *dynamicNotFound=nullptr)
Loads a selection list (optionally with restricted type)
Static storage of an output device and its base (abstract) implementation.
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.