#include <netedit/GNENet.h>
#include <netedit/GNEViewNet.h>
#include <netedit/GNEViewParent.h>
#include <netedit/GNEApplicationWindow.h>
#include <utils/options/OptionsCont.h>
#include <netbuild/NBOwnTLDef.h>
#include "GNEChange_TLS.h"
FXIMPLEMENT_ABSTRACT(GNEChange_TLS, GNEChange, nullptr, 0)
GNEChange_TLS::GNEChange_TLS(GNEJunction* junction, NBTrafficLightDefinition* tlDef, bool forward, bool forceInsert, const std::string tlID) :
GNEChange(Supermode::NETWORK, forward, false),
myJunction(junction),
myTlDef(tlDef),
myForceInsert(forceInsert) {
myJunction->incRef("GNEChange_TLS");
if (myTlDef == nullptr) {
if (!forward) {
throw ProcessError("If myTlDef is null, forward cannot be false");
}
TrafficLightType type = SUMOXMLDefinitions::TrafficLightTypes.get(OptionsCont::getOptions().getString("tls.default-type"));
if (myJunction->getNBNode()->isTLControlled()) {
type = (*myJunction->getNBNode()->getControllingTLS().begin())->getType();
}
myTlDef = new NBOwnTLDef(tlID == "" ? myJunction->getMicrosimID() : tlID, 0, type);
}
}
GNEChange_TLS::GNEChange_TLS(GNEJunction* junction, NBTrafficLightDefinition* tlDef, bool forward, TrafficLightType type,
bool forceInsert, const std::string tlID) :
GNEChange(Supermode::NETWORK, forward, false),
myJunction(junction),
myTlDef(tlDef),
myForceInsert(forceInsert) {
myJunction->incRef("GNEChange_TLS");
if (myTlDef == nullptr) {
if (!forward) {
throw ProcessError("If myTlDef is null, forward cannot be false");
}
if (myJunction->getNBNode()->isTLControlled()) {
type = (*myJunction->getNBNode()->getControllingTLS().begin())->getType();
}
myTlDef = new NBOwnTLDef(tlID == "" ? myJunction->getMicrosimID() : tlID, 0, type);
}
}
GNEChange_TLS::GNEChange_TLS(GNEJunction* junction, NBTrafficLightDefinition* tlDef, const std::string& newID) :
GNEChange(Supermode::NETWORK, true, false),
myJunction(junction),
myTlDef(tlDef),
myForceInsert(false),
myOldID(tlDef->getID()),
myNewID(newID) {
myJunction->incRef("GNEChange_TLS");
}
GNEChange_TLS::~GNEChange_TLS() {
if (myJunction->getNet()->getViewNet()->getViewParent()->getGNEAppWindows()->isUndoRedoAllowed()) {
myJunction->decRef("GNEChange_TLS");
if (myJunction->unreferenced()) {
delete myJunction;
}
}
}
void
GNEChange_TLS::undo() {
if (myForward) {
if (myNewID.empty()) {
myJunction->removeTrafficLight(myTlDef);
} else {
myJunction->getNet()->getTLLogicCont().rename(myTlDef, myOldID);
}
} else {
if (myNewID.empty()) {
myJunction->addTrafficLight(myTlDef, myForceInsert);
} else {
myJunction->getNet()->getTLLogicCont().rename(myTlDef, myNewID);
}
}
myJunction->getNet()->getSavingStatus()->requireSaveNetwork();
}
void
GNEChange_TLS::redo() {
if (myForward) {
if (myNewID.empty()) {
myJunction->addTrafficLight(myTlDef, myForceInsert);
} else {
myJunction->getNet()->getTLLogicCont().rename(myTlDef, myNewID);
}
} else {
if (myNewID.empty()) {
myJunction->removeTrafficLight(myTlDef);
} else {
myJunction->getNet()->getTLLogicCont().rename(myTlDef, myOldID);
}
}
myJunction->getNet()->getSavingStatus()->requireSaveNetwork();
}
std::string
GNEChange_TLS::undoName() const {
if (myForward) {
return (TL("Undo create TLS '") + myJunction->getID() + "'");
} else {
return (TL("Undo delete TLS '") + myJunction->getID() + "'");
}
}
std::string
GNEChange_TLS::redoName() const {
if (myForward) {
return (TL("Redo create TLS '") + myJunction->getID() + "'");
} else {
return (TL("Redo delete TLS '") + myJunction->getID() + "'");
}
}