Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/netedit/dialogs/elements/GNEDistributionRefDialog.cpp
193716 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2026 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 GNEDistributionRefDialog.cpp
15
/// @author Pablo Alvarez Lopez
16
/// @date Aug 2025
17
///
18
// Dialog for edit attribute carriers
19
/****************************************************************************/
20
21
#include <netedit/changes/GNEChange_DemandElement.h>
22
#include <netedit/elements/demand/GNERouteRef.h>
23
#include <netedit/elements/demand/GNEVTypeRef.h>
24
#include <netedit/GNEApplicationWindow.h>
25
#include <netedit/GNENet.h>
26
#include <netedit/GNETagPropertiesDatabase.h>
27
#include <netedit/GNEUndoList.h>
28
#include <netedit/GNEViewParent.h>
29
#include <utils/foxtools/MFXTextFieldIcon.h>
30
#include <utils/gui/div/GUIDesigns.h>
31
32
#include "GNEDistributionRefDialog.h"
33
34
// ===========================================================================
35
// Defines
36
// ===========================================================================
37
38
#define BLACK_COLOR MFXUtils::getFXColor(RGBColor::BLACK)
39
#define BLUE_COLOR MFXUtils::getFXColor(RGBColor::BLUE)
40
#define RED_COLOR MFXUtils::getFXColor(RGBColor::RED)
41
42
// ===========================================================================
43
// FOX callback mapping
44
// ===========================================================================
45
46
FXDEFMAP(GNEDistributionRefDialog) GNEDistributionRefDialogMap[] = {
47
FXMAPFUNC(SEL_COMMAND, MID_GNE_DISTRIBUTIONDIALOG_REFERENCE, GNEDistributionRefDialog::onCmdSetReference),
48
FXMAPFUNC(SEL_COMMAND, MID_GNE_DISTRIBUTIONDIALOG_PROBABILITY, GNEDistributionRefDialog::onCmdSetProbability)
49
};
50
51
// Object implementation
52
FXIMPLEMENT(GNEDistributionRefDialog, GNEDialog, GNEDistributionRefDialogMap, ARRAYNUMBER(GNEDistributionRefDialogMap))
53
54
// ===========================================================================
55
// member method definitions
56
// ===========================================================================
57
58
GNEDistributionRefDialog::GNEDistributionRefDialog(GNEAttributeCarrier* distributionParent) :
59
GNEDialog(distributionParent->getNet()->getGNEApplicationWindow(), TLF("Add % reference", distributionParent->getTagStr()),
60
distributionParent->getTagProperty()->getGUIIcon(), DialogType::DISTRIBUTION_REF, GNEDialog::Buttons::ACCEPT_CANCEL,
61
OpenType::MODAL, ResizeMode::STATIC),
62
myDistributionParent(distributionParent) {
63
auto tooltipMenu = distributionParent->getNet()->getGNEApplicationWindow()->getStaticTooltipMenu();
64
// create reference elements
65
FXHorizontalFrame* referenceFrames = new FXHorizontalFrame(myContentFrame, GUIDesignAuxiliarHorizontalFrame);
66
auto referenceLabel = new FXLabel(referenceFrames, "reference", nullptr, GUIDesignLabelThickedFixed(100));
67
myReferencesComboBox = new MFXComboBoxIcon(referenceFrames, tooltipMenu, true, GUIDesignComboBoxVisibleItems,
68
this, MID_GNE_DISTRIBUTIONDIALOG_REFERENCE, GUIDesignComboBox);
69
// create probability elements
70
FXHorizontalFrame* probabilityFrame = new FXHorizontalFrame(myContentFrame, GUIDesignAuxiliarHorizontalFrame);
71
new FXLabel(probabilityFrame, toString(SUMO_ATTR_PROB).c_str(), nullptr, GUIDesignLabelThickedFixed(100));
72
myProbabilityTextField = new MFXTextFieldIcon(probabilityFrame, tooltipMenu, GUIIcon::EMPTY, this,
73
MID_GNE_DISTRIBUTIONDIALOG_PROBABILITY, GUIDesignTextFieldRestricted(TEXTFIELD_REAL));
74
// extract all references
75
std::set<std::string> referenceIDs;
76
for (const auto& reference : distributionParent->getHierarchicalElement()->getChildDemandElements()) {
77
if (reference->getTagProperty()->isDistributionReference()) {
78
referenceIDs.insert(reference->getParentDemandElements().at(1)->getID());
79
}
80
}
81
// continue depending on distributionParent
82
if (distributionParent->getTagProperty()->getTag() == SUMO_TAG_VTYPE_DISTRIBUTION) {
83
referenceLabel->setText(toString(SUMO_TAG_VTYPE).c_str());
84
const auto& vTypes = distributionParent->getNet()->getAttributeCarriers()->getDemandElements().at(SUMO_TAG_VTYPE);
85
// obtain candidate IDs
86
for (const auto& vType : vTypes) {
87
if (referenceIDs.count(vType.second->getID()) == 0) {
88
myCandidates[vType.second->getID()] = vType.second;
89
}
90
}
91
// insert it in comboBox
92
for (const auto& vTypeID : myCandidates) {
93
myReferencesComboBox->appendIconItem(vTypeID.first.c_str());
94
}
95
} else if (distributionParent->getTagProperty()->getTag() == SUMO_TAG_ROUTE_DISTRIBUTION) {
96
referenceLabel->setText(toString(SUMO_TAG_ROUTE).c_str());
97
const auto& routes = distributionParent->getNet()->getAttributeCarriers()->getDemandElements().at(SUMO_TAG_ROUTE);
98
// obtain candidate IDs
99
for (const auto& route : routes) {
100
if (referenceIDs.count(route.second->getID()) == 0) {
101
myCandidates[route.second->getID()] = route.second;
102
}
103
}
104
// insert it in comboBox
105
for (const auto& routeID : myCandidates) {
106
myReferencesComboBox->appendIconItem(routeID.first.c_str());
107
}
108
}
109
// set current item (for update probability)
110
myReferencesComboBox->setCurrentItem(0, TRUE);
111
// open dialog
112
openDialog();
113
}
114
115
116
GNEDistributionRefDialog::~GNEDistributionRefDialog() {}
117
118
119
void
120
GNEDistributionRefDialog::runInternalTest(const InternalTestStep::DialogArgument* /*dialogArgument*/) {
121
// nothing to do
122
}
123
124
125
long
126
GNEDistributionRefDialog::onCmdAccept(FXObject*, FXSelector, void*) {
127
if ((myReferencesComboBox->getTextColor() != RED_COLOR) &&
128
(myProbabilityTextField->getTextColor() != RED_COLOR)) {
129
// declare referenced element
130
GNEDemandElement* reference = nullptr;
131
const double probability = GNEAttributeCarrier::parse<double>(myProbabilityTextField->getText().text());
132
GNEDemandElement* distribution = myDistributionParent->getNet()->getAttributeCarriers()->retrieveDemandElement(myDistributionParent->getGUIGlObject());
133
auto undoList = myDistributionParent->getNet()->getUndoList();
134
// create a routeRef o a vTypeRef
135
if (distribution->getTagProperty()->getTag() == SUMO_TAG_VTYPE_DISTRIBUTION) {
136
if (myProbabilityTextField->getTextColor() == BLUE_COLOR) {
137
reference = new GNEVTypeRef(distribution, myReferencedElement);
138
} else {
139
reference = new GNEVTypeRef(distribution, myReferencedElement, probability);
140
}
141
} else if (distribution->getTagProperty()->getTag() == SUMO_TAG_ROUTE_DISTRIBUTION) {
142
if (myProbabilityTextField->getTextColor() == BLUE_COLOR) {
143
reference = new GNERouteRef(distribution, myReferencedElement);
144
} else {
145
reference = new GNERouteRef(distribution, myReferencedElement, probability);
146
}
147
}
148
// continue depending if allow/disallow is enabled
149
if (myDistributionParent->getNet()->getGNEApplicationWindow()->isUndoRedoAllowed()) {
150
undoList->begin(myReferencedElement, TLF("add % in '%'", myReferencedElement->getTagStr(), distribution->getID()));
151
undoList->add(new GNEChange_DemandElement(reference, true), true);
152
undoList->end();
153
} else {
154
myDistributionParent->getNet()->getAttributeCarriers()->insertDemandElement(reference);
155
distribution->addChildElement(reference);
156
myReferencedElement->addChildElement(reference);
157
reference->incRef("GNEDistributionRefDialog");
158
}
159
return closeDialogAccepting();
160
} else {
161
return closeDialogCanceling();
162
}
163
}
164
165
166
long
167
GNEDistributionRefDialog::onCmdSetReference(FXObject*, FXSelector, void*) {
168
// check if candidateID exist in list of candidates
169
GNEDemandElement* reference = nullptr;
170
for (const auto& candidate : myCandidates) {
171
if (myReferencesComboBox->getText().text() == candidate.first) {
172
reference = candidate.second;
173
break;
174
}
175
}
176
// continue depending if selected candidate exist
177
if (reference == nullptr) {
178
myReferencesComboBox->setTextColor(RED_COLOR);
179
} else {
180
myReferencesComboBox->setTextColor(BLACK_COLOR);
181
myReferencedElement = reference;
182
// set default probability
183
myProbabilityTextField->setText(myReferencedElement->getAttribute(SUMO_ATTR_PROB).c_str(), TRUE);
184
// check if enable or disable accept button
185
if ((myReferencesComboBox->getTextColor() != RED_COLOR) &&
186
(myProbabilityTextField->getTextColor() != RED_COLOR)) {
187
myAcceptButton->enable();
188
} else {
189
myAcceptButton->disable();
190
}
191
}
192
return 1;
193
}
194
195
196
long
197
GNEDistributionRefDialog::onCmdSetProbability(FXObject*, FXSelector, void*) {
198
// first check if set default probability
199
if (myProbabilityTextField->getText().empty()) {
200
myProbabilityTextField->setText(myReferencedElement->getAttribute(SUMO_ATTR_PROB).c_str(), TRUE);
201
}
202
// check if value can be parsed to double
203
if (GNEAttributeCarrier::canParse<double>(myProbabilityTextField->getText().text())) {
204
if (myProbabilityTextField->getText().text() == myReferencedElement->getAttribute(SUMO_ATTR_PROB)) {
205
myProbabilityTextField->setTextColor(BLUE_COLOR);
206
} else {
207
myProbabilityTextField->setTextColor(BLACK_COLOR);
208
}
209
} else {
210
myProbabilityTextField->setTextColor(RED_COLOR);
211
}
212
// check if enable or disable accept button
213
if ((myReferencesComboBox->getTextColor() != RED_COLOR) &&
214
(myProbabilityTextField->getTextColor() != RED_COLOR)) {
215
myAcceptButton->enable();
216
} else {
217
myAcceptButton->disable();
218
}
219
return 1;
220
}
221
222
/****************************************************************************/
223
224