Path: blob/main/src/netedit/dialogs/elements/GNEDistributionRefDialog.cpp
193716 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file GNEDistributionRefDialog.cpp14/// @author Pablo Alvarez Lopez15/// @date Aug 202516///17// Dialog for edit attribute carriers18/****************************************************************************/1920#include <netedit/changes/GNEChange_DemandElement.h>21#include <netedit/elements/demand/GNERouteRef.h>22#include <netedit/elements/demand/GNEVTypeRef.h>23#include <netedit/GNEApplicationWindow.h>24#include <netedit/GNENet.h>25#include <netedit/GNETagPropertiesDatabase.h>26#include <netedit/GNEUndoList.h>27#include <netedit/GNEViewParent.h>28#include <utils/foxtools/MFXTextFieldIcon.h>29#include <utils/gui/div/GUIDesigns.h>3031#include "GNEDistributionRefDialog.h"3233// ===========================================================================34// Defines35// ===========================================================================3637#define BLACK_COLOR MFXUtils::getFXColor(RGBColor::BLACK)38#define BLUE_COLOR MFXUtils::getFXColor(RGBColor::BLUE)39#define RED_COLOR MFXUtils::getFXColor(RGBColor::RED)4041// ===========================================================================42// FOX callback mapping43// ===========================================================================4445FXDEFMAP(GNEDistributionRefDialog) GNEDistributionRefDialogMap[] = {46FXMAPFUNC(SEL_COMMAND, MID_GNE_DISTRIBUTIONDIALOG_REFERENCE, GNEDistributionRefDialog::onCmdSetReference),47FXMAPFUNC(SEL_COMMAND, MID_GNE_DISTRIBUTIONDIALOG_PROBABILITY, GNEDistributionRefDialog::onCmdSetProbability)48};4950// Object implementation51FXIMPLEMENT(GNEDistributionRefDialog, GNEDialog, GNEDistributionRefDialogMap, ARRAYNUMBER(GNEDistributionRefDialogMap))5253// ===========================================================================54// member method definitions55// ===========================================================================5657GNEDistributionRefDialog::GNEDistributionRefDialog(GNEAttributeCarrier* distributionParent) :58GNEDialog(distributionParent->getNet()->getGNEApplicationWindow(), TLF("Add % reference", distributionParent->getTagStr()),59distributionParent->getTagProperty()->getGUIIcon(), DialogType::DISTRIBUTION_REF, GNEDialog::Buttons::ACCEPT_CANCEL,60OpenType::MODAL, ResizeMode::STATIC),61myDistributionParent(distributionParent) {62auto tooltipMenu = distributionParent->getNet()->getGNEApplicationWindow()->getStaticTooltipMenu();63// create reference elements64FXHorizontalFrame* referenceFrames = new FXHorizontalFrame(myContentFrame, GUIDesignAuxiliarHorizontalFrame);65auto referenceLabel = new FXLabel(referenceFrames, "reference", nullptr, GUIDesignLabelThickedFixed(100));66myReferencesComboBox = new MFXComboBoxIcon(referenceFrames, tooltipMenu, true, GUIDesignComboBoxVisibleItems,67this, MID_GNE_DISTRIBUTIONDIALOG_REFERENCE, GUIDesignComboBox);68// create probability elements69FXHorizontalFrame* probabilityFrame = new FXHorizontalFrame(myContentFrame, GUIDesignAuxiliarHorizontalFrame);70new FXLabel(probabilityFrame, toString(SUMO_ATTR_PROB).c_str(), nullptr, GUIDesignLabelThickedFixed(100));71myProbabilityTextField = new MFXTextFieldIcon(probabilityFrame, tooltipMenu, GUIIcon::EMPTY, this,72MID_GNE_DISTRIBUTIONDIALOG_PROBABILITY, GUIDesignTextFieldRestricted(TEXTFIELD_REAL));73// extract all references74std::set<std::string> referenceIDs;75for (const auto& reference : distributionParent->getHierarchicalElement()->getChildDemandElements()) {76if (reference->getTagProperty()->isDistributionReference()) {77referenceIDs.insert(reference->getParentDemandElements().at(1)->getID());78}79}80// continue depending on distributionParent81if (distributionParent->getTagProperty()->getTag() == SUMO_TAG_VTYPE_DISTRIBUTION) {82referenceLabel->setText(toString(SUMO_TAG_VTYPE).c_str());83const auto& vTypes = distributionParent->getNet()->getAttributeCarriers()->getDemandElements().at(SUMO_TAG_VTYPE);84// obtain candidate IDs85for (const auto& vType : vTypes) {86if (referenceIDs.count(vType.second->getID()) == 0) {87myCandidates[vType.second->getID()] = vType.second;88}89}90// insert it in comboBox91for (const auto& vTypeID : myCandidates) {92myReferencesComboBox->appendIconItem(vTypeID.first.c_str());93}94} else if (distributionParent->getTagProperty()->getTag() == SUMO_TAG_ROUTE_DISTRIBUTION) {95referenceLabel->setText(toString(SUMO_TAG_ROUTE).c_str());96const auto& routes = distributionParent->getNet()->getAttributeCarriers()->getDemandElements().at(SUMO_TAG_ROUTE);97// obtain candidate IDs98for (const auto& route : routes) {99if (referenceIDs.count(route.second->getID()) == 0) {100myCandidates[route.second->getID()] = route.second;101}102}103// insert it in comboBox104for (const auto& routeID : myCandidates) {105myReferencesComboBox->appendIconItem(routeID.first.c_str());106}107}108// set current item (for update probability)109myReferencesComboBox->setCurrentItem(0, TRUE);110// open dialog111openDialog();112}113114115GNEDistributionRefDialog::~GNEDistributionRefDialog() {}116117118void119GNEDistributionRefDialog::runInternalTest(const InternalTestStep::DialogArgument* /*dialogArgument*/) {120// nothing to do121}122123124long125GNEDistributionRefDialog::onCmdAccept(FXObject*, FXSelector, void*) {126if ((myReferencesComboBox->getTextColor() != RED_COLOR) &&127(myProbabilityTextField->getTextColor() != RED_COLOR)) {128// declare referenced element129GNEDemandElement* reference = nullptr;130const double probability = GNEAttributeCarrier::parse<double>(myProbabilityTextField->getText().text());131GNEDemandElement* distribution = myDistributionParent->getNet()->getAttributeCarriers()->retrieveDemandElement(myDistributionParent->getGUIGlObject());132auto undoList = myDistributionParent->getNet()->getUndoList();133// create a routeRef o a vTypeRef134if (distribution->getTagProperty()->getTag() == SUMO_TAG_VTYPE_DISTRIBUTION) {135if (myProbabilityTextField->getTextColor() == BLUE_COLOR) {136reference = new GNEVTypeRef(distribution, myReferencedElement);137} else {138reference = new GNEVTypeRef(distribution, myReferencedElement, probability);139}140} else if (distribution->getTagProperty()->getTag() == SUMO_TAG_ROUTE_DISTRIBUTION) {141if (myProbabilityTextField->getTextColor() == BLUE_COLOR) {142reference = new GNERouteRef(distribution, myReferencedElement);143} else {144reference = new GNERouteRef(distribution, myReferencedElement, probability);145}146}147// continue depending if allow/disallow is enabled148if (myDistributionParent->getNet()->getGNEApplicationWindow()->isUndoRedoAllowed()) {149undoList->begin(myReferencedElement, TLF("add % in '%'", myReferencedElement->getTagStr(), distribution->getID()));150undoList->add(new GNEChange_DemandElement(reference, true), true);151undoList->end();152} else {153myDistributionParent->getNet()->getAttributeCarriers()->insertDemandElement(reference);154distribution->addChildElement(reference);155myReferencedElement->addChildElement(reference);156reference->incRef("GNEDistributionRefDialog");157}158return closeDialogAccepting();159} else {160return closeDialogCanceling();161}162}163164165long166GNEDistributionRefDialog::onCmdSetReference(FXObject*, FXSelector, void*) {167// check if candidateID exist in list of candidates168GNEDemandElement* reference = nullptr;169for (const auto& candidate : myCandidates) {170if (myReferencesComboBox->getText().text() == candidate.first) {171reference = candidate.second;172break;173}174}175// continue depending if selected candidate exist176if (reference == nullptr) {177myReferencesComboBox->setTextColor(RED_COLOR);178} else {179myReferencesComboBox->setTextColor(BLACK_COLOR);180myReferencedElement = reference;181// set default probability182myProbabilityTextField->setText(myReferencedElement->getAttribute(SUMO_ATTR_PROB).c_str(), TRUE);183// check if enable or disable accept button184if ((myReferencesComboBox->getTextColor() != RED_COLOR) &&185(myProbabilityTextField->getTextColor() != RED_COLOR)) {186myAcceptButton->enable();187} else {188myAcceptButton->disable();189}190}191return 1;192}193194195long196GNEDistributionRefDialog::onCmdSetProbability(FXObject*, FXSelector, void*) {197// first check if set default probability198if (myProbabilityTextField->getText().empty()) {199myProbabilityTextField->setText(myReferencedElement->getAttribute(SUMO_ATTR_PROB).c_str(), TRUE);200}201// check if value can be parsed to double202if (GNEAttributeCarrier::canParse<double>(myProbabilityTextField->getText().text())) {203if (myProbabilityTextField->getText().text() == myReferencedElement->getAttribute(SUMO_ATTR_PROB)) {204myProbabilityTextField->setTextColor(BLUE_COLOR);205} else {206myProbabilityTextField->setTextColor(BLACK_COLOR);207}208} else {209myProbabilityTextField->setTextColor(RED_COLOR);210}211// check if enable or disable accept button212if ((myReferencesComboBox->getTextColor() != RED_COLOR) &&213(myProbabilityTextField->getTextColor() != RED_COLOR)) {214myAcceptButton->enable();215} else {216myAcceptButton->disable();217}218return 1;219}220221/****************************************************************************/222223224