Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/netedit/frames/GNEViewObjectSelector.cpp
169678 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 GNEViewObjectSelector.cpp
15
/// @author Pablo Alvarez Lopez
16
/// @date Mar 2022
17
///
18
// NetworkElement selector module
19
/****************************************************************************/
20
21
#include <netedit/GNENet.h>
22
#include <netedit/GNEViewNet.h>
23
#include <netedit/GNETagProperties.h>
24
#include <utils/common/MsgHandler.h>
25
#include <utils/gui/div/GUIDesigns.h>
26
#include <utils/gui/windows/GUIAppEnum.h>
27
28
#include "GNEViewObjectSelector.h"
29
#include "GNEFrame.h"
30
31
// ===========================================================================
32
// FOX callback mapping
33
// ===========================================================================
34
35
FXDEFMAP(GNEViewObjectSelector) SelectorParentNetworkElementsMap[] = {
36
FXMAPFUNC(SEL_COMMAND, MID_GNE_USESELECTED, GNEViewObjectSelector::onCmdUseSelectedElements),
37
FXMAPFUNC(SEL_COMMAND, MID_GNE_CLEARSELECTION, GNEViewObjectSelector::onCmdClearSelection),
38
};
39
40
// Object implementation
41
FXIMPLEMENT(GNEViewObjectSelector, MFXGroupBoxModule, SelectorParentNetworkElementsMap, ARRAYNUMBER(SelectorParentNetworkElementsMap))
42
43
// ---------------------------------------------------------------------------
44
// GNEViewObjectSelector - methods
45
// ---------------------------------------------------------------------------
46
47
GNEViewObjectSelector::GNEViewObjectSelector(GNEFrame* frameParent) :
48
MFXGroupBoxModule(frameParent, TL("NetworkElements")),
49
myFrameParent(frameParent) {
50
// Create buttons
51
myClearSelection = GUIDesigns::buildFXButton(getCollapsableFrame(), TL("Clear selection"), "", "", nullptr, this, MID_GNE_CLEARSELECTION, GUIDesignButton);
52
myUseSelected = GUIDesigns::buildFXButton(getCollapsableFrame(), TL("Use selected"), "", "", nullptr, this, MID_GNE_USESELECTED, GUIDesignButton);
53
// list label
54
new FXLabel(getCollapsableFrame(), TL("Selected elements"), 0, GUIDesignLabelThick(JUSTIFY_NORMAL));
55
// Create list
56
myList = new FXList(getCollapsableFrame(), this, MID_GNE_SELECT, GUIDesignListFixedHeight);
57
// create information label
58
myLabel = new FXLabel(getCollapsableFrame(), "", 0, GUIDesignLabelFrameInformation);
59
// Hide List
60
hide();
61
}
62
63
64
GNEViewObjectSelector::~GNEViewObjectSelector() {}
65
66
67
SumoXMLTag
68
GNEViewObjectSelector::getTag() const {
69
return myTag;
70
}
71
72
73
bool
74
GNEViewObjectSelector::isNetworkElementSelected(const GNEAttributeCarrier* AC) const {
75
return std::find(mySelectedACs.begin(), mySelectedACs.end(), AC) != mySelectedACs.end();
76
}
77
78
79
void
80
GNEViewObjectSelector::showNetworkElementsSelector(const SumoXMLTag newTag, const SumoXMLAttr attribute) {
81
myTag = newTag;
82
myAttribute = attribute;
83
// update info
84
myLabel->setText((TL("-This additional requires to\n select at least\n one element") + std::string("\n") +
85
TLF("-Click over % to select", toString(newTag)) + std::string("\n") +
86
TL("-ESC to clear elements")).c_str());
87
myUseSelected->setText(TLF("Use selected %s", toString(newTag)).c_str());
88
// update groupBox elements
89
setText(TLF("% selector", toString(newTag)));
90
// clear items
91
myList->clearItems();
92
mySelectedACs.clear();
93
show();
94
}
95
96
97
void
98
GNEViewObjectSelector::hideNetworkElementsSelector() {
99
hide();
100
}
101
102
103
bool
104
GNEViewObjectSelector::toggleSelectedElement(const GNEAttributeCarrier* AC) {
105
if (shown() && AC) {
106
// Obtain Id's of list
107
for (int i = 0; i < myList->getNumItems(); i++) {
108
if (myList->getItem(i)->getText().text() == AC->getID()) {
109
// unselect element
110
myList->removeItem(i);
111
// remove it in list
112
mySelectedACs.erase(mySelectedACs.begin() + i);
113
// update viewNet
114
myFrameParent->getViewNet()->update();
115
return true;
116
}
117
}
118
// select element
119
myList->appendItem(AC->getID().c_str(), AC->getACIcon());
120
mySelectedACs.push_back(AC);
121
// update viewNet
122
myFrameParent->getViewNet()->update();
123
return true;
124
} else {
125
// nothing to toogle
126
return false;
127
}
128
}
129
130
131
bool
132
GNEViewObjectSelector::toggleSelectedLane(const GNELane* lane) {
133
if (shown() && lane) {
134
if (myTag == SUMO_TAG_EDGE) {
135
return toggleSelectedElement(lane->getParentEdge());
136
} else {
137
return toggleSelectedElement(lane);
138
}
139
} else {
140
// nothing to toogle
141
return false;
142
}
143
}
144
145
146
bool
147
GNEViewObjectSelector::fillSumoBaseObject(CommonXMLStructure::SumoBaseObject* baseObject) const {
148
if (shown()) {
149
if (myList->getNumItems() == 0) {
150
WRITE_WARNING(TLF("List of % cannot be empty", toString(myTag)));
151
return false;
152
} else {
153
std::vector<std::string> selectedIDs;
154
selectedIDs.reserve(mySelectedACs.size());
155
// Obtain Id's of list
156
for (const auto AC : mySelectedACs) {
157
selectedIDs.push_back(AC->getID());
158
}
159
baseObject->addStringListAttribute(myAttribute, selectedIDs);
160
return true;
161
}
162
} else {
163
// nothing to fill
164
return true;
165
}
166
}
167
168
169
void
170
GNEViewObjectSelector::clearSelection() {
171
// clear list of egdge ids
172
myList->clearItems();
173
mySelectedACs.clear();
174
// update viewNet
175
myFrameParent->getViewNet()->update();
176
}
177
178
179
long
180
GNEViewObjectSelector::onCmdUseSelectedElements(FXObject*, FXSelector, void*) {
181
// clear list of egdge ids
182
myList->clearItems();
183
mySelectedACs.clear();
184
// get all selected ACs
185
const auto selectedACs = myFrameParent->getViewNet()->getNet()->getAttributeCarriers()->getSelectedAttributeCarriers(false);
186
for (const auto AC : selectedACs) {
187
if (AC->getTagProperty()->getTag() == myTag) {
188
if (AC->isAttributeCarrierSelected()) {
189
myList->appendItem(AC->getID().c_str(), AC->getACIcon());
190
mySelectedACs.push_back(AC);
191
}
192
}
193
}
194
// Update Frame
195
update();
196
return 1;
197
}
198
199
200
long
201
GNEViewObjectSelector::onCmdClearSelection(FXObject*, FXSelector, void*) {
202
clearSelection();
203
return 1;
204
}
205
206
207
GNEViewObjectSelector::GNEViewObjectSelector() :
208
myFrameParent(nullptr) {
209
}
210
211
/****************************************************************************/
212
213