Path: blob/main/src/netedit/elements/data/GNEDataSet.cpp
193674 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 GNEDataSet.cpp14/// @author Pablo Alvarez Lopez15/// @date Jan 202016///17// A abstract class for data sets18/****************************************************************************/1920#include <netedit/changes/GNEChange_Attribute.h>21#include <netedit/frames/common/GNEInspectorFrame.h>22#include <netedit/frames/GNEElementTree.h>23#include <netedit/GNEApplicationWindow.h>24#include <netedit/GNENet.h>25#include <netedit/GNEViewParent.h>2627#include "GNEDataSet.h"28#include "GNEDataInterval.h"2930// ===========================================================================31// member method definitions32// ===========================================================================3334// ---------------------------------------------------------------------------35// GNEDataSet::AttributeColors - methods36// ---------------------------------------------------------------------------3738GNEDataSet::AttributeColors::AttributeColors() {39}404142void43GNEDataSet::AttributeColors::updateValues(const std::string& attribute, const double value) {44// check if exist45if (myMinMaxValue.count(attribute) == 0) {46myMinMaxValue[attribute] = std::make_pair(value, value);47} else {48// update min value49if (value < myMinMaxValue.at(attribute).first) {50myMinMaxValue.at(attribute).first = value;51}52// update max value53if (value > myMinMaxValue.at(attribute).second) {54myMinMaxValue.at(attribute).second = value;55}56}57}585960void61GNEDataSet::AttributeColors::updateAllValues(const AttributeColors& attributeColors) {62// iterate over map63for (const auto& attributeColor : attributeColors.myMinMaxValue) {64if (myMinMaxValue.count(attributeColor.first) == 0) {65myMinMaxValue[attributeColor.first] = attributeColor.second;66} else {67// update min value68if (attributeColor.second.first < myMinMaxValue.at(attributeColor.first).first) {69myMinMaxValue.at(attributeColor.first).first = attributeColor.second.first;70}71// update max value72if (attributeColor.second.second > myMinMaxValue.at(attributeColor.first).second) {73myMinMaxValue.at(attributeColor.first).second = attributeColor.second.second;74}75}76}77}787980bool81GNEDataSet::AttributeColors::exist(const std::string& attribute) const {82return (myMinMaxValue.count(attribute) > 0);83}848586double87GNEDataSet::AttributeColors::getMinValue(const std::string& attribute) const {88return myMinMaxValue.at(attribute).first;89}909192double93GNEDataSet::AttributeColors::getMaxValue(const std::string& attribute) const {94return myMinMaxValue.at(attribute).second;95}969798void99GNEDataSet::AttributeColors::clear() {100myMinMaxValue.clear();101}102103// ---------------------------------------------------------------------------104// GNEDataSet - methods105// ---------------------------------------------------------------------------106107GNEDataSet::GNEDataSet(GNENet* net) :108GNEAttributeCarrier(SUMO_TAG_DATASET, net) {109}110111112GNEDataSet::GNEDataSet(const std::string& dataSetID, GNENet* net, FileBucket* fileBucket) :113GNEAttributeCarrier(SUMO_TAG_DATASET, net, fileBucket),114myDataSetID(dataSetID) {115}116117118GNEDataSet::~GNEDataSet() {}119120121GNEHierarchicalElement*122GNEDataSet::getHierarchicalElement() {123return this;124}125126127GNEMoveElement*128GNEDataSet::getMoveElement() const {129return nullptr;130}131132133Parameterised*134GNEDataSet::getParameters() {135return nullptr;136}137138139const Parameterised*140GNEDataSet::getParameters() const {141return nullptr;142}143144145GUIGlObject*146GNEDataSet::getGUIGlObject() {147return nullptr;148}149150151const GUIGlObject*152GNEDataSet::getGUIGlObject() const {153return nullptr;154}155156157FileBucket*158GNEDataSet::getFileBucket() const {159return myFileBucket;160}161162163void164GNEDataSet::updateAttributeColors() {165// first update attribute colors in data interval childrens166for (const auto& interval : myDataIntervalChildren) {167interval.second->updateAttributeColors();168}169// continue with data sets containers170myAllAttributeColors.clear();171mySpecificAttributeColors.clear();172// iterate over all data interval children173for (const auto& interval : myDataIntervalChildren) {174myAllAttributeColors.updateAllValues(interval.second->getAllAttributeColors());175}176// iterate over specificdata interval children177for (const auto& interval : myDataIntervalChildren) {178for (const auto& specificAttributeColor : interval.second->getSpecificAttributeColors()) {179mySpecificAttributeColors[specificAttributeColor.first].updateAllValues(specificAttributeColor.second);180}181}182}183184185const GNEDataSet::AttributeColors&186GNEDataSet::getAllAttributeColors() const {187return myAllAttributeColors;188}189190191const std::map<SumoXMLTag, GNEDataSet::AttributeColors>&192GNEDataSet::getSpecificAttributeColors() const {193return mySpecificAttributeColors;194}195196197void198GNEDataSet::updateGeometry() {199// nothing to update200}201202203Position204GNEDataSet::getPositionInView() const {205return Position(0, 0);206}207208209void210GNEDataSet::writeDataSet(OutputDevice& device) const {211// iterate over intervals212for (const auto& interval : myDataIntervalChildren) {213// open device214device.openTag(SUMO_TAG_INTERVAL);215// write ID216device.writeAttr(SUMO_ATTR_ID, getID());217// write begin218device.writeAttr(SUMO_ATTR_BEGIN, interval.second->getAttribute(SUMO_ATTR_BEGIN));219// write end220device.writeAttr(SUMO_ATTR_END, interval.second->getAttribute(SUMO_ATTR_END));221// iterate over interval generic datas222for (const auto& genericData : interval.second->getGenericDataChildren()) {223// write generic data224genericData->writeGenericData(device);225}226// close device227device.closeTag();228}229}230231232bool233GNEDataSet::checkDrawFromContour() const {234return false;235}236237238bool239GNEDataSet::checkDrawToContour() const {240return false;241}242243244bool245GNEDataSet::checkDrawRelatedContour() const {246return false;247}248249250bool251GNEDataSet::checkDrawOverContour() const {252return false;253}254255256bool257GNEDataSet::checkDrawDeleteContour() const {258return false;259}260261262bool263GNEDataSet::checkDrawDeleteContourSmall() const {264return false;265}266267268bool269GNEDataSet::checkDrawSelectContour() const {270return false;271}272273274bool275GNEDataSet::checkDrawMoveContour() const {276return false;277}278279280void281GNEDataSet::addDataIntervalChild(GNEDataInterval* dataInterval) {282// check that dataInterval wasn't previously inserted283if (myDataIntervalChildren.count(dataInterval->getAttributeDouble(SUMO_ATTR_BEGIN)) == 0) {284// add data interval child285myDataIntervalChildren[dataInterval->getAttributeDouble(SUMO_ATTR_BEGIN)] = dataInterval;286// add reference in attributeCarriers287myNet->getAttributeCarriers()->insertDataInterval(dataInterval, dataInterval);288} else {289throw ProcessError(TL("DataInterval was already inserted"));290}291}292293294void295GNEDataSet::removeDataIntervalChild(GNEDataInterval* dataInterval) {296// check that dataInterval was previously inserted297if (myDataIntervalChildren.count(dataInterval->getAttributeDouble(SUMO_ATTR_BEGIN)) == 1) {298// remove data interval child299myDataIntervalChildren.erase(dataInterval->getAttributeDouble(SUMO_ATTR_BEGIN));300// remove it from inspected elements and GNEElementTree301myNet->getViewNet()->getInspectedElements().uninspectAC(dataInterval);302myNet->getViewParent()->getInspectorFrame()->getHierarchicalElementTree()->removeCurrentEditedAttributeCarrier(dataInterval);303// remove reference from attributeCarriers304myNet->getAttributeCarriers()->deleteDataInterval(dataInterval);305} else {306throw ProcessError(TL("DataInterval wasn't previously inserted"));307}308}309310311bool312GNEDataSet::dataIntervalChildrenExist(GNEDataInterval* dataInterval) const {313for (const auto& interval : myDataIntervalChildren) {314if (interval.second == dataInterval) {315return true;316}317}318return false;319}320321void322GNEDataSet::updateDataIntervalBegin(const double oldBegin) {323// check that dataInterval was previously inserted324if (myDataIntervalChildren.count(oldBegin) == 1) {325// get data interval326GNEDataInterval* dataInterval = myDataIntervalChildren.at(oldBegin);327// insert again using new begin328myDataIntervalChildren[dataInterval->getAttributeDouble(SUMO_ATTR_BEGIN)] = dataInterval;329} else {330throw ProcessError(TL("DataInterval wasn't previously inserted"));331}332}333334335bool336GNEDataSet::checkNewInterval(const double newBegin, const double newEnd) {337return checkNewInterval(myDataIntervalChildren, newBegin, newEnd);338}339340341bool342GNEDataSet::checkNewBeginEnd(const GNEDataInterval* dataInterval, const double newBegin, const double newEnd) {343// make a copy of myDataIntervalChildren without dataInterval, and check checkNewInterval344std::map<const double, GNEDataInterval*> copyOfDataIntervalMap;345for (const auto& element : myDataIntervalChildren) {346if (element.second != dataInterval) {347copyOfDataIntervalMap.insert(element);348}349}350return checkNewInterval(copyOfDataIntervalMap, newBegin, newEnd);351}352353354GNEDataInterval*355GNEDataSet::retrieveInterval(const double begin, const double end) const {356if (myDataIntervalChildren.count(begin) == 0) {357return nullptr;358} else if (myDataIntervalChildren.at(begin)->getAttributeDouble(SUMO_ATTR_END) != end) {359return nullptr;360} else {361return myDataIntervalChildren.at(begin);362}363}364365366const std::map<const double, GNEDataInterval*>&367GNEDataSet::getDataIntervalChildren() const {368return myDataIntervalChildren;369}370371372std::string373GNEDataSet::getAttribute(SumoXMLAttr key) const {374switch (key) {375case SUMO_ATTR_ID:376return myDataSetID;377default:378return getCommonAttribute(key);379}380}381382383double384GNEDataSet::getAttributeDouble(SumoXMLAttr key) const {385return getCommonAttributeDouble(key);386}387388389Position390GNEDataSet::getAttributePosition(SumoXMLAttr key) const {391return getCommonAttributePosition(key);392}393394395PositionVector GNEDataSet::getAttributePositionVector(SumoXMLAttr key) const {396return getCommonAttributePositionVector(key);397}398399400void401GNEDataSet::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {402switch (key) {403case SUMO_ATTR_ID:404GNEChange_Attribute::changeAttribute(this, key, value, undoList);405break;406default:407setCommonAttribute(key, value, undoList);408break;409}410}411412413bool414GNEDataSet::isValid(SumoXMLAttr key, const std::string& value) {415switch (key) {416case SUMO_ATTR_ID:417if (SUMOXMLDefinitions::isValidNetID(value) && (myNet->getAttributeCarriers()->retrieveDataSet(value, false) == nullptr)) {418return true;419} else {420return false;421}422default:423return isCommonAttributeValid(key, value);424}425}426427428std::string429GNEDataSet::getPopUpID() const {430return getTagStr();431}432433434std::string435GNEDataSet::getHierarchyName() const {436return getTagStr() + ": " + myDataSetID;437}438439440void441GNEDataSet::setAttribute(SumoXMLAttr key, const std::string& value) {442switch (key) {443case SUMO_ATTR_ID:444myDataSetID = value;445// update all intervals446for (const auto& interval : myDataIntervalChildren) {447interval.second->updateGenericDataIDs();448}449break;450default:451setCommonAttribute(key, value);452break;453}454// mark interval toolbar for update455if (!isTemplate()) {456myNet->getViewNet()->getIntervalBar().markForUpdate();457}458}459460461bool462GNEDataSet::checkNewInterval(const std::map<const double, GNEDataInterval*>& dataIntervalMap, const double newBegin, const double newEnd) {463if (dataIntervalMap.empty()) {464return true;465} else {466// declare first and last element467const auto itFirstElement = dataIntervalMap.begin();468const auto itLastElement = dataIntervalMap.rbegin();469if (newBegin > newEnd) {470return false;471} else if (dataIntervalMap.count(newBegin) == 1) {472return false;473} else if (newBegin < itFirstElement->first) {474return (newEnd <= itFirstElement->first);475} else if (newBegin > itLastElement->first) {476return (newBegin >= itLastElement->second->getAttributeDouble(SUMO_ATTR_END));477} else {478// iterate over myDataIntervalChildren479for (auto it = itFirstElement; it != dataIntervalMap.end(); it++) {480if (newBegin < it->first) {481// obtain previous edge482auto itPrevious = it;483itPrevious--;484// check overlapping with end485if (itPrevious->second->getAttributeDouble(SUMO_ATTR_END) < newBegin) {486return true;487}488}489}490}491return false;492}493}494495/****************************************************************************/496497498