1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file GNEVariableSpeedSignStep.cpp
11 /// @author Pablo Alvarez Lopez
12 /// @date Apr 2017
13 /// @version $Id$
14 ///
15 //
16 /****************************************************************************/
17
18 // ===========================================================================
19 // included modules
20 // ===========================================================================
21 #include <config.h>
22
23 #include <netedit/dialogs/GNEVariableSpeedSignDialog.h>
24 #include <netedit/changes/GNEChange_Attribute.h>
25 #include <netedit/GNEUndoList.h>
26 #include <netedit/GNEViewNet.h>
27
28 #include "GNEVariableSpeedSignStep.h"
29
30
31 // ===========================================================================
32 // member method definitions
33 // ===========================================================================
34
GNEVariableSpeedSignStep(GNEVariableSpeedSignDialog * variableSpeedSignDialog)35 GNEVariableSpeedSignStep::GNEVariableSpeedSignStep(GNEVariableSpeedSignDialog* variableSpeedSignDialog) :
36 GNEAdditional(variableSpeedSignDialog->getEditedAdditional(), variableSpeedSignDialog->getEditedAdditional()->getViewNet(), GLO_VSS, SUMO_TAG_STEP, "", false,
37 {}, {}, {}, {variableSpeedSignDialog->getEditedAdditional()}, {}, {}, {}, {}, {}, {}) {
38 // fill VSS Step with default values
39 setDefaultValues();
40 // set time Attribute manually
41 if (getAdditionalParents().at(0)->getAdditionalChilds().size() > 0) {
42 myTime = parse<double>(getAdditionalParents().at(0)->getAdditionalChilds().back()->getAttribute(SUMO_ATTR_TIME)) + 1;
43 } else {
44 myTime = 0;
45 }
46 }
47
48
GNEVariableSpeedSignStep(GNEAdditional * variableSpeedSignParent,double time,double speed)49 GNEVariableSpeedSignStep::GNEVariableSpeedSignStep(GNEAdditional* variableSpeedSignParent, double time, double speed) :
50 GNEAdditional(variableSpeedSignParent, variableSpeedSignParent->getViewNet(), GLO_VSS, SUMO_TAG_STEP, "", false,
51 {}, {}, {}, {variableSpeedSignParent}, {}, {}, {}, {}, {}, {}),
52 myTime(time),
53 mySpeed(speed) {
54 }
55
56
~GNEVariableSpeedSignStep()57 GNEVariableSpeedSignStep::~GNEVariableSpeedSignStep() {}
58
59
60 double
getTime() const61 GNEVariableSpeedSignStep::getTime() const {
62 return myTime;
63 }
64
65
66 void
moveGeometry(const Position &)67 GNEVariableSpeedSignStep::moveGeometry(const Position&) {
68 // This additional cannot be moved
69 }
70
71
72 void
commitGeometryMoving(GNEUndoList *)73 GNEVariableSpeedSignStep::commitGeometryMoving(GNEUndoList*) {
74 // This additional cannot be moved
75 }
76
77
78 void
updateGeometry(bool)79 GNEVariableSpeedSignStep::updateGeometry(bool /*updateGrid*/) {
80 // Currently this additional doesn't own a Geometry
81 }
82
83
84 Position
getPositionInView() const85 GNEVariableSpeedSignStep::getPositionInView() const {
86 return getAdditionalParents().at(0)->getPositionInView();
87 }
88
89
90 std::string
getParentName() const91 GNEVariableSpeedSignStep::getParentName() const {
92 return getAdditionalParents().at(0)->getID();
93 }
94
95
96 void
drawGL(const GUIVisualizationSettings &) const97 GNEVariableSpeedSignStep::drawGL(const GUIVisualizationSettings&) const {
98 // Currently This additional isn't drawn
99 }
100
101
102 std::string
getAttribute(SumoXMLAttr key) const103 GNEVariableSpeedSignStep::getAttribute(SumoXMLAttr key) const {
104 switch (key) {
105 case SUMO_ATTR_ID:
106 return getAdditionalID();
107 case SUMO_ATTR_TIME:
108 return toString(myTime);
109 case SUMO_ATTR_SPEED:
110 return toString(mySpeed);
111 case GNE_ATTR_PARENT:
112 return getAdditionalParents().at(0)->getID();
113 case GNE_ATTR_GENERIC:
114 return getGenericParametersStr();
115 default:
116 throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
117 }
118 }
119
120
121 void
setAttribute(SumoXMLAttr key,const std::string & value,GNEUndoList * undoList)122 GNEVariableSpeedSignStep::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
123 if (value == getAttribute(key)) {
124 return; //avoid needless changes, later logic relies on the fact that attributes have changed
125 }
126 switch (key) {
127 case SUMO_ATTR_ID:
128 case SUMO_ATTR_TIME:
129 case SUMO_ATTR_SPEED:
130 case GNE_ATTR_GENERIC:
131 undoList->p_add(new GNEChange_Attribute(this, myViewNet->getNet(), key, value));
132 break;
133 default:
134 throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
135 }
136 }
137
138
139 bool
isValid(SumoXMLAttr key,const std::string & value)140 GNEVariableSpeedSignStep::isValid(SumoXMLAttr key, const std::string& value) {
141 switch (key) {
142 case SUMO_ATTR_ID:
143 return isValidAdditionalID(value);
144 case SUMO_ATTR_TIME:
145 if (canParse<double>(value)) {
146 // Check that
147 double newTime = parse<double>(value);
148 // Only allowed positiv times
149 if (newTime < 0) {
150 return false;
151 }
152 // check that there isn't duplicate times
153 int counter = 0;
154 for (auto i : getAdditionalParents().at(0)->getAdditionalChilds()) {
155 if (parse<double>(i->getAttribute(SUMO_ATTR_TIME)) == newTime) {
156 counter++;
157 }
158 }
159 return (counter <= 1);
160 } else {
161 return false;
162 }
163 case SUMO_ATTR_SPEED:
164 return canParse<double>(value);
165 case GNE_ATTR_GENERIC:
166 return isGenericParametersValid(value);
167 default:
168 throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
169 }
170 }
171
172
173 std::string
getPopUpID() const174 GNEVariableSpeedSignStep::getPopUpID() const {
175 return getTagStr();
176 }
177
178
179 std::string
getHierarchyName() const180 GNEVariableSpeedSignStep::getHierarchyName() const {
181 return getTagStr() + ": " + getAttribute(SUMO_ATTR_TIME);
182 }
183
184 // ===========================================================================
185 // private
186 // ===========================================================================
187
188 void
setAttribute(SumoXMLAttr key,const std::string & value)189 GNEVariableSpeedSignStep::setAttribute(SumoXMLAttr key, const std::string& value) {
190 switch (key) {
191 case SUMO_ATTR_ID:
192 changeAdditionalID(value);
193 break;
194 case SUMO_ATTR_TIME:
195 myTime = parse<double>(value);
196 break;
197 case SUMO_ATTR_SPEED:
198 mySpeed = parse<double>(value);
199 break;
200 case GNE_ATTR_GENERIC:
201 setGenericParametersStr(value);
202 break;
203 default:
204 throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
205 }
206 // check if updated attribute requieres update geometry
207 if (myTagProperty.hasAttribute(key) && myTagProperty.getAttributeProperties(key).requiereUpdateGeometry()) {
208 updateGeometry(true);
209 }
210 }
211
212
213 /****************************************************************************/
214