Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GNEDistributionRefDialog.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/****************************************************************************/
18// Dialog for edit attribute carriers
19/****************************************************************************/
20
25#include <netedit/GNENet.h>
27#include <netedit/GNEUndoList.h>
31
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
50
51// Object implementation
52FXIMPLEMENT(GNEDistributionRefDialog, GNEDialog, GNEDistributionRefDialogMap, ARRAYNUMBER(GNEDistributionRefDialogMap))
53
54// ===========================================================================
55// member method definitions
56// ===========================================================================
57
59 GNEDialog(distributionParent->getNet()->getViewNet()->getViewParent()->getGNEAppWindows(),
60 TLF("Add % reference", distributionParent->getTagStr()), distributionParent->getTagProperty()->getGUIIcon(),
62 myDistributionParent(distributionParent) {
63 auto tooltipMenu = distributionParent->getNet()->getViewNet()->getViewParent()->getGNEAppWindows()->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,
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,
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
117
118
119void
121 // nothing to do
122}
123
124
125long
126GNEDistributionRefDialog::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()->getViewNet()->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()->getViewNet()->getViewParent()->getGNEAppWindows()->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
166long
167GNEDistributionRefDialog::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
196long
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
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/****************************************************************************/
DialogType
#define BLUE_COLOR
#define RED_COLOR
#define BLACK_COLOR
FXDEFMAP(GNEDistributionRefDialog) GNEDistributionRefDialogMap[]
@ MID_GNE_DISTRIBUTIONDIALOG_REFERENCE
change reference
@ MID_GNE_DISTRIBUTIONDIALOG_PROBABILITY
change probability
#define GUIDesignComboBox
Definition GUIDesigns.h:295
#define GUIDesignAuxiliarHorizontalFrame
design for auxiliar (Without borders) horizontal frame used to pack another frames
Definition GUIDesigns.h:430
#define GUIDesignComboBoxVisibleItems
Definition GUIDesigns.h:64
#define GUIDesignTextFieldRestricted(type)
text field extended over Frame with thick frame (int)
Definition GUIDesigns.h:77
#define GUIDesignLabelThickedFixed(width)
label thicked, icon before text, text centered and custom width
Definition GUIDesigns.h:254
@ EMPTY
Definition GUIIcons.h:59
#define TLF(string,...)
Definition MsgHandler.h:306
@ SUMO_TAG_VTYPE
description of a vehicle/person/container type
@ SUMO_TAG_ROUTE_DISTRIBUTION
distribution of a route
@ SUMO_TAG_ROUTE
description of a route
@ SUMO_TAG_VTYPE_DISTRIBUTION
distribution of a vehicle type
@ SUMO_ATTR_PROB
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
const std::string getID() const override
get ID (all Attribute Carriers have one)
static T parse(const std::string &string)
parses a value of type T from string (used for basic types: int, double, bool, etc....
static bool canParse(const std::string &string)
true if a value of type T can be parsed from string
const GNETagProperties * getTagProperty() const
get tagProperty associated with this Attribute Carrier
FXVerticalFrame * myContentFrame
content frame
Definition GNEDialog.h:152
void openDialog(FXWindow *focusableElement=nullptr)
open dialog
FXButton * myAcceptButton
accept button
Definition GNEDialog.h:155
long closeDialogAccepting()
close dialog accepting the changes
OpenType
Open dialog type.
Definition GNEDialog.h:58
GNEDialog(GNEApplicationWindow *applicationWindow, const std::string &name, GUIIcon titleIcon, DialogType type, Buttons buttons, OpenType openType, ResizeMode resizeMode)
basic constructor
Definition GNEDialog.cpp:60
long closeDialogCanceling()
close dialog declining the changes
long onCmdSetReference(FXObject *, FXSelector, void *)
event after choose a reference in the comboBox
long onCmdSetProbability(FXObject *, FXSelector, void *)
event after choose a reference in the probability
void runInternalTest(const InternalTestStep::DialogArgument *dialogArgument)
run internal test
MFXTextFieldIcon * myProbabilityTextField
textField probability
std::map< std::string, GNEDemandElement * > myCandidates
map with candidate IDs;
MFXComboBoxIcon * myReferencesComboBox
comboBox for references
GNEAttributeCarrier * myDistributionParent
FOX needs this.
GNEDistributionRefDialog(GNEAttributeCarrier *distributionParent)
constructor
long onCmdAccept(FXObject *, FXSelector, void *)
event after press accept button
GNEDemandElement * myReferencedElement
current referenced element
void addChildElement(ChildType *element)
add child without updating parent (ONLY used if we're creating elements without undo-redo)
void incRef(const std::string &debugMsg="")
Increase reference.
SumoXMLTag getTag() const
get Tag vinculated with this attribute Property
dialog arguments, used for certain modal dialogs that can not be edited using tab