Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/gui/div/GUISelectedStorage.h
169684 views
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
/****************************************************************************/
14
/// @file GUISelectedStorage.h
15
/// @author Daniel Krajzewicz
16
/// @author Jakob Erdmann
17
/// @author Michael Behrisch
18
/// @date Jun 2004
19
///
20
// Storage for "selected" objects
21
/****************************************************************************/
22
#pragma once
23
#include <config.h>
24
25
#include <unordered_set>
26
#include <string>
27
#include <map>
28
#include <fstream>
29
#include <utils/foxtools/fxheader.h>
30
#include <utils/common/UtilExceptions.h>
31
#include <utils/gui/globjects/GUIGlObject.h>
32
33
34
// ===========================================================================
35
// class declarations
36
// ===========================================================================
37
class OutputDevice;
38
39
40
// ===========================================================================
41
// class definitions
42
// ===========================================================================
43
/**
44
* @class GUISelectedStorage
45
* @brief Storage for "selected" objects
46
*
47
* Object selection is done by storing the "gl-ids" of selectable objects
48
* (GUIGlObjects) within internal containers of this class. Each id is stored
49
* twice - in a global container and within one of the type-aware containers.
50
*
51
* This class allows adding and removing objects (their ids) from the lists
52
* of selected objects, saving the lists into a file, and obtaining the lists
53
* of selected objects (both all and type-aware).
54
*
55
* Most of the adding/removing methods do not require a GUIGlObjectType as
56
* parameter, but an integer. This is done to perform the action on objects with
57
* a yet unknown type - in this case, the type is obtained internally.
58
*
59
* Besides this, the class forces an active view of selected items
60
* to refresh its contents if an item is added/removed. For this, an
61
* FXWindow has to make itself visible to GUISelectedStorage.
62
*
63
* @see GUIGlObject
64
* @see GUIGlObjectType
65
* @see GUIDialog_GLChosenEditor
66
*/
67
class GUISelectedStorage {
68
69
public:
70
/// @class update target
71
class UpdateTarget {
72
73
public:
74
/// @brief virtual destructor
75
virtual ~UpdateTarget() {};
76
77
/// @brief called when selection is updated
78
virtual void selectionUpdated() = 0;
79
};
80
81
public:
82
/// @brief Constructor
83
GUISelectedStorage();
84
85
/// @brief Destructor
86
~GUISelectedStorage();
87
88
/** @brief Returns the information whether the object with the given type and id is selected
89
*
90
* If the type is ==-1, it is determined, first. If it could not be obtained,
91
* or if the type is not covered by any selection container, a ProcessError is thrown.
92
*
93
* Otherwise, the container holding objects of the determined type is
94
* asked whether the given id is stored using SingleTypeSelections::isSelected().
95
*
96
* @param[in] type The type of the object (GUIGlObjectType or -1)
97
* @param[in] id The id of the object
98
* @return Whether the object is selected
99
* @exception ProcessError If the object is not known or the type is not covered by a sub-container
100
* @see GUIGlObject
101
* @see GUIGlObjectType
102
* @see SingleTypeSelections::isSelected
103
*/
104
bool isSelected(GUIGlObjectType type, GUIGlID id);
105
106
bool isSelected(const GUIGlObject* o);
107
108
/** @brief Adds the object with the given id
109
*
110
* The id of the object is added to the sub-container that is
111
* responsible for objects of the determined type using SingleTypeSelections::select
112
* and to the global list of chosen items if it is not already there.
113
*
114
* The optionally listening window is informed about the change.
115
*
116
* @param[in] id The id of the object
117
* @exception ProcessError If the object is not known or the type is not covered by a sub-container
118
* @see GUIGlObject
119
* @see GUIGlObjectType
120
* @see SingleTypeSelections::select
121
* @see GUIDialog_GLChosenEditor
122
*/
123
void select(GUIGlID id, bool update = true);
124
125
/** @brief Deselects the object with the given id
126
*
127
* The id of the object is removed from the sub-container that is
128
* responsible for objects of the determined type using SingleTypeSelections::deselect
129
* and from the global list of chosen items if it is there.
130
*
131
* The optionally listening UpdateTarget is informed about the change.
132
*
133
* @param[in] id The id of the object
134
* @exception ProcessError If the object is not known or the type is not covered by a sub-container
135
* @see GUIGlObject
136
* @see GUIGlObjectType
137
* @see SingleTypeSelections::deselect
138
* @see GUIDialog_GLChosenEditor
139
*/
140
void deselect(GUIGlID id);
141
142
void deselect(GUIGlObjectType type, GUIGlID id);
143
144
/** @brief Toggles selection of an object
145
*
146
* If the object can not be obtained a ProcessError is thrown.
147
*
148
* Otherwise, it is determined whether the object is already selected or not.
149
* If so, it is deselected by calling "deselect", otherwise it is selected
150
* via "select".
151
*
152
* @param[in] id The id of the object
153
* @exception ProcessError If the object is not known or the type is not covered by a sub-container
154
* @see GUIGlObject
155
* @see deselect
156
* @see select
157
*/
158
void toggleSelection(GUIGlID id);
159
160
/// @brief Returns the set of ids of all selected objects
161
const std::unordered_set<GUIGlID>& getSelected() const;
162
163
/** @brief Returns the set of ids of all selected objects' of a certain type
164
*
165
* @param[in] type The type of the object
166
* @return A set containing the ids of all selected objects of the given type
167
* @see SingleTypeSelections::getSelected
168
*/
169
const std::unordered_set<GUIGlID>& getSelected(GUIGlObjectType type);
170
171
/** @brief Clears the list of selected objects
172
*
173
* Clears the global container and all sub-containers via SingleTypeSelections::clear.
174
*
175
* The optionally listening UpdateTarget is informed about the change.
176
*/
177
void clear();
178
179
/// @brief inform the update target of earlier changes
180
void notifyChanged();
181
182
/** @brief Loads a selection list (optionally with restricted type)
183
*
184
* @param[in] filename The name of the file to load the list of selected objects from
185
* @param[in] type The type of the objects to load if changed from default
186
* @return error messages if errors occurred or the empty string
187
*/
188
std::string load(const std::string& filename, GUIGlObjectType type = GLO_MAX, std::ostream* dynamicNotFound = nullptr);
189
std::string load(std::istream& strm, GUIGlObjectType type = GLO_MAX, std::ostream* dynamicNotFound = nullptr);
190
191
/** @brief Loads a selection list (optionally with restricted type) and
192
* returns the ids of all active objects
193
*
194
* @param[in] filename The name of the file to load the list of selected objects from
195
* @param[out] msg Any error messages while loading or the empty string
196
* @param[in] type The type of the objects to load if changed from default
197
* @param[in] maxErrors The maximum Number of errors to return
198
* @return the set of loaded ids
199
*/
200
std::set<GUIGlID> loadIDs(std::istream& strm, std::string& msgOut, GUIGlObjectType type = GLO_MAX, std::ostream* dynamicNotFound = nullptr, int maxErrors = 16);
201
202
/** @brief Saves a selection list
203
*
204
* @param[in] type The type of the objects to save
205
* @param[in] filename The name of the file to save the list of selected objects into
206
*/
207
void save(GUIGlObjectType type, const std::string& filename);
208
209
/** @brief Saves the combined selection of all types
210
*
211
* @param[in] filename The name of the file to save the list of selected objects into
212
*/
213
void save(const std::string& filename) const;
214
215
/** @brief Adds a dialog to be updated
216
* @param[in] updateTarget the callback for selection changes
217
*/
218
void add2Update(UpdateTarget* updateTarget);
219
220
/// @brief @brief Removes the dialog to be updated
221
void remove2Update();
222
223
/**
224
* @class SingleTypeSelections
225
* @brief A container for ids of selected objects of a certain type.
226
*/
227
class SingleTypeSelections {
228
229
public:
230
/// @brief Constructor
231
SingleTypeSelections();
232
233
/// @brief Destructor
234
~SingleTypeSelections();
235
236
/** @brief Returns the information whether the object with the given id is qithin the selection
237
* @param[in] id The id of the object
238
* @return Whether the object is selected
239
*/
240
bool isSelected(GUIGlID id);
241
242
/** @brief Adds the object with the given id to the list of selected objects
243
* @param[in] id The id of the object
244
*/
245
void select(GUIGlID id);
246
247
/** @brief Deselects the object with the given id from the list of selected objects
248
* @param[in] id The id of the object
249
*/
250
void deselect(GUIGlID id);
251
252
/// @brief Clears the list of selected objects
253
void clear();
254
255
/** @brief Saves the list of selected objects to a file named as given
256
* @param[in] filename The name of the file to save the list into
257
*/
258
void save(const std::string& filename);
259
260
/** @brief Returns the list of selected ids
261
* @return A list containing the ids of all selected objects
262
*/
263
const std::unordered_set<GUIGlID>& getSelected() const;
264
265
private:
266
/// @brief The list of selected ids
267
std::unordered_set<GUIGlID> mySelected;
268
};
269
270
/// @brief set SingleTypeSelections as friend class
271
friend class SingleTypeSelections;
272
273
274
private:
275
/// @brief map with the selections
276
std::map<GUIGlObjectType, SingleTypeSelections> mySelections;
277
278
/// @brief List of selected objects
279
std::unordered_set<GUIGlID> myAllSelected;
280
281
/// @brief The dialog to be updated
282
UpdateTarget* myUpdateTarget;
283
284
/// @brief saves items from the given set
285
static void save(const std::string& filename, const std::unordered_set<GUIGlID>& ids);
286
};
287
288