Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/netedit/changes/GNEChange_TLS.cpp
169678 views
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
/****************************************************************************/
14
/// @file GNEChange_TLS.cpp
15
/// @author Jakob Erdmann
16
/// @date July 2011
17
///
18
// A network change in which a traffic light is created or deleted
19
/****************************************************************************/
20
21
#include <netedit/GNENet.h>
22
#include <netedit/GNEViewNet.h>
23
#include <netedit/GNEViewParent.h>
24
#include <netedit/GNEApplicationWindow.h>
25
#include <utils/options/OptionsCont.h>
26
#include <netbuild/NBOwnTLDef.h>
27
28
#include "GNEChange_TLS.h"
29
30
// ===========================================================================
31
// FOX-declarations
32
// ===========================================================================
33
34
FXIMPLEMENT_ABSTRACT(GNEChange_TLS, GNEChange, nullptr, 0)
35
36
// ===========================================================================
37
// member method definitions
38
// ===========================================================================
39
40
GNEChange_TLS::GNEChange_TLS(GNEJunction* junction, NBTrafficLightDefinition* tlDef, bool forward, bool forceInsert, const std::string tlID) :
41
GNEChange(Supermode::NETWORK, forward, false),
42
myJunction(junction),
43
myTlDef(tlDef),
44
myForceInsert(forceInsert) {
45
myJunction->incRef("GNEChange_TLS");
46
if (myTlDef == nullptr) {
47
// check forward
48
if (!forward) {
49
throw ProcessError("If myTlDef is null, forward cannot be false");
50
}
51
// potential memory leak if this change is never executed
52
TrafficLightType type = SUMOXMLDefinitions::TrafficLightTypes.get(OptionsCont::getOptions().getString("tls.default-type"));
53
if (myJunction->getNBNode()->isTLControlled()) {
54
// copy existing type
55
type = (*myJunction->getNBNode()->getControllingTLS().begin())->getType();
56
}
57
myTlDef = new NBOwnTLDef(tlID == "" ? myJunction->getMicrosimID() : tlID, 0, type);
58
}
59
}
60
61
62
GNEChange_TLS::GNEChange_TLS(GNEJunction* junction, NBTrafficLightDefinition* tlDef, bool forward, TrafficLightType type,
63
bool forceInsert, const std::string tlID) :
64
GNEChange(Supermode::NETWORK, forward, false),
65
myJunction(junction),
66
myTlDef(tlDef),
67
myForceInsert(forceInsert) {
68
myJunction->incRef("GNEChange_TLS");
69
if (myTlDef == nullptr) {
70
// check forward
71
if (!forward) {
72
throw ProcessError("If myTlDef is null, forward cannot be false");
73
}
74
if (myJunction->getNBNode()->isTLControlled()) {
75
// copy existing type
76
type = (*myJunction->getNBNode()->getControllingTLS().begin())->getType();
77
}
78
myTlDef = new NBOwnTLDef(tlID == "" ? myJunction->getMicrosimID() : tlID, 0, type);
79
}
80
}
81
82
83
GNEChange_TLS::GNEChange_TLS(GNEJunction* junction, NBTrafficLightDefinition* tlDef, const std::string& newID) :
84
GNEChange(Supermode::NETWORK, true, false),
85
myJunction(junction),
86
myTlDef(tlDef),
87
myForceInsert(false),
88
myOldID(tlDef->getID()),
89
myNewID(newID) {
90
myJunction->incRef("GNEChange_TLS");
91
}
92
93
94
GNEChange_TLS::~GNEChange_TLS() {
95
// only continue we have undo-redo mode enabled
96
if (myJunction->getNet()->getViewNet()->getViewParent()->getGNEAppWindows()->isUndoRedoAllowed()) {
97
myJunction->decRef("GNEChange_TLS");
98
if (myJunction->unreferenced()) {
99
delete myJunction;
100
}
101
}
102
}
103
104
105
void
106
GNEChange_TLS::undo() {
107
if (myForward) {
108
if (myNewID.empty()) {
109
// remove traffic light from junction
110
myJunction->removeTrafficLight(myTlDef);
111
} else {
112
// set old ID
113
myJunction->getNet()->getTLLogicCont().rename(myTlDef, myOldID);
114
}
115
} else {
116
if (myNewID.empty()) {
117
// add traffic light to junction
118
myJunction->addTrafficLight(myTlDef, myForceInsert);
119
} else {
120
// set new ID
121
myJunction->getNet()->getTLLogicCont().rename(myTlDef, myNewID);
122
}
123
}
124
// enable save networkElements
125
myJunction->getNet()->getSavingStatus()->requireSaveNetwork();
126
}
127
128
129
void
130
GNEChange_TLS::redo() {
131
if (myForward) {
132
if (myNewID.empty()) {
133
// add traffic light to junction
134
myJunction->addTrafficLight(myTlDef, myForceInsert);
135
} else {
136
// set new ID
137
myJunction->getNet()->getTLLogicCont().rename(myTlDef, myNewID);
138
}
139
} else {
140
if (myNewID.empty()) {
141
// remove traffic light from junction
142
myJunction->removeTrafficLight(myTlDef);
143
} else {
144
// set old ID
145
myJunction->getNet()->getTLLogicCont().rename(myTlDef, myOldID);
146
}
147
}
148
// enable save networkElements
149
myJunction->getNet()->getSavingStatus()->requireSaveNetwork();
150
}
151
152
153
std::string
154
GNEChange_TLS::undoName() const {
155
if (myForward) {
156
return (TL("Undo create TLS '") + myJunction->getID() + "'");
157
} else {
158
return (TL("Undo delete TLS '") + myJunction->getID() + "'");
159
}
160
}
161
162
163
std::string
164
GNEChange_TLS::redoName() const {
165
if (myForward) {
166
return (TL("Redo create TLS '") + myJunction->getID() + "'");
167
} else {
168
return (TL("Redo delete TLS '") + myJunction->getID() + "'");
169
}
170
}
171
172