Path: blob/main/src/netedit/frames/GNEViewObjectSelector.cpp
169678 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 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 GNEViewObjectSelector.cpp14/// @author Pablo Alvarez Lopez15/// @date Mar 202216///17// NetworkElement selector module18/****************************************************************************/1920#include <netedit/GNENet.h>21#include <netedit/GNEViewNet.h>22#include <netedit/GNETagProperties.h>23#include <utils/common/MsgHandler.h>24#include <utils/gui/div/GUIDesigns.h>25#include <utils/gui/windows/GUIAppEnum.h>2627#include "GNEViewObjectSelector.h"28#include "GNEFrame.h"2930// ===========================================================================31// FOX callback mapping32// ===========================================================================3334FXDEFMAP(GNEViewObjectSelector) SelectorParentNetworkElementsMap[] = {35FXMAPFUNC(SEL_COMMAND, MID_GNE_USESELECTED, GNEViewObjectSelector::onCmdUseSelectedElements),36FXMAPFUNC(SEL_COMMAND, MID_GNE_CLEARSELECTION, GNEViewObjectSelector::onCmdClearSelection),37};3839// Object implementation40FXIMPLEMENT(GNEViewObjectSelector, MFXGroupBoxModule, SelectorParentNetworkElementsMap, ARRAYNUMBER(SelectorParentNetworkElementsMap))4142// ---------------------------------------------------------------------------43// GNEViewObjectSelector - methods44// ---------------------------------------------------------------------------4546GNEViewObjectSelector::GNEViewObjectSelector(GNEFrame* frameParent) :47MFXGroupBoxModule(frameParent, TL("NetworkElements")),48myFrameParent(frameParent) {49// Create buttons50myClearSelection = GUIDesigns::buildFXButton(getCollapsableFrame(), TL("Clear selection"), "", "", nullptr, this, MID_GNE_CLEARSELECTION, GUIDesignButton);51myUseSelected = GUIDesigns::buildFXButton(getCollapsableFrame(), TL("Use selected"), "", "", nullptr, this, MID_GNE_USESELECTED, GUIDesignButton);52// list label53new FXLabel(getCollapsableFrame(), TL("Selected elements"), 0, GUIDesignLabelThick(JUSTIFY_NORMAL));54// Create list55myList = new FXList(getCollapsableFrame(), this, MID_GNE_SELECT, GUIDesignListFixedHeight);56// create information label57myLabel = new FXLabel(getCollapsableFrame(), "", 0, GUIDesignLabelFrameInformation);58// Hide List59hide();60}616263GNEViewObjectSelector::~GNEViewObjectSelector() {}646566SumoXMLTag67GNEViewObjectSelector::getTag() const {68return myTag;69}707172bool73GNEViewObjectSelector::isNetworkElementSelected(const GNEAttributeCarrier* AC) const {74return std::find(mySelectedACs.begin(), mySelectedACs.end(), AC) != mySelectedACs.end();75}767778void79GNEViewObjectSelector::showNetworkElementsSelector(const SumoXMLTag newTag, const SumoXMLAttr attribute) {80myTag = newTag;81myAttribute = attribute;82// update info83myLabel->setText((TL("-This additional requires to\n select at least\n one element") + std::string("\n") +84TLF("-Click over % to select", toString(newTag)) + std::string("\n") +85TL("-ESC to clear elements")).c_str());86myUseSelected->setText(TLF("Use selected %s", toString(newTag)).c_str());87// update groupBox elements88setText(TLF("% selector", toString(newTag)));89// clear items90myList->clearItems();91mySelectedACs.clear();92show();93}949596void97GNEViewObjectSelector::hideNetworkElementsSelector() {98hide();99}100101102bool103GNEViewObjectSelector::toggleSelectedElement(const GNEAttributeCarrier* AC) {104if (shown() && AC) {105// Obtain Id's of list106for (int i = 0; i < myList->getNumItems(); i++) {107if (myList->getItem(i)->getText().text() == AC->getID()) {108// unselect element109myList->removeItem(i);110// remove it in list111mySelectedACs.erase(mySelectedACs.begin() + i);112// update viewNet113myFrameParent->getViewNet()->update();114return true;115}116}117// select element118myList->appendItem(AC->getID().c_str(), AC->getACIcon());119mySelectedACs.push_back(AC);120// update viewNet121myFrameParent->getViewNet()->update();122return true;123} else {124// nothing to toogle125return false;126}127}128129130bool131GNEViewObjectSelector::toggleSelectedLane(const GNELane* lane) {132if (shown() && lane) {133if (myTag == SUMO_TAG_EDGE) {134return toggleSelectedElement(lane->getParentEdge());135} else {136return toggleSelectedElement(lane);137}138} else {139// nothing to toogle140return false;141}142}143144145bool146GNEViewObjectSelector::fillSumoBaseObject(CommonXMLStructure::SumoBaseObject* baseObject) const {147if (shown()) {148if (myList->getNumItems() == 0) {149WRITE_WARNING(TLF("List of % cannot be empty", toString(myTag)));150return false;151} else {152std::vector<std::string> selectedIDs;153selectedIDs.reserve(mySelectedACs.size());154// Obtain Id's of list155for (const auto AC : mySelectedACs) {156selectedIDs.push_back(AC->getID());157}158baseObject->addStringListAttribute(myAttribute, selectedIDs);159return true;160}161} else {162// nothing to fill163return true;164}165}166167168void169GNEViewObjectSelector::clearSelection() {170// clear list of egdge ids171myList->clearItems();172mySelectedACs.clear();173// update viewNet174myFrameParent->getViewNet()->update();175}176177178long179GNEViewObjectSelector::onCmdUseSelectedElements(FXObject*, FXSelector, void*) {180// clear list of egdge ids181myList->clearItems();182mySelectedACs.clear();183// get all selected ACs184const auto selectedACs = myFrameParent->getViewNet()->getNet()->getAttributeCarriers()->getSelectedAttributeCarriers(false);185for (const auto AC : selectedACs) {186if (AC->getTagProperty()->getTag() == myTag) {187if (AC->isAttributeCarrierSelected()) {188myList->appendItem(AC->getID().c_str(), AC->getACIcon());189mySelectedACs.push_back(AC);190}191}192}193// Update Frame194update();195return 1;196}197198199long200GNEViewObjectSelector::onCmdClearSelection(FXObject*, FXSelector, void*) {201clearSelection();202return 1;203}204205206GNEViewObjectSelector::GNEViewObjectSelector() :207myFrameParent(nullptr) {208}209210/****************************************************************************/211212213