Eclipse SUMO - Simulation of Urban MObility
GNEChange_Attribute.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2001-2024 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 /****************************************************************************/
18 // A network change in which something is changed (for undo/redo)
19 /****************************************************************************/
20 #include <config.h>
21 
22 #include <netedit/GNENet.h>
23 #include <netedit/GNEUndoList.h>
25 
26 #include "GNEChange_Attribute.h"
27 
28 // ===========================================================================
29 // FOX-declarations
30 // ===========================================================================
31 FXIMPLEMENT_ABSTRACT(GNEChange_Attribute, GNEChange, nullptr, 0)
32 
33 // ===========================================================================
34 // member method definitions
35 // ===========================================================================
36 
37 void
38 GNEChange_Attribute::changeAttribute(GNEAttributeCarrier* AC, SumoXMLAttr key, const std::string& value, GNEUndoList* undoList, const bool force) {
39  // create change
40  auto change = new GNEChange_Attribute(AC, key, value);
41  // set force
42  change->myForceChange = force;
43  // check if process change
44  if (change->trueChange()) {
45  undoList->begin(AC, TLF("change '%' attribute in % '%' to '%'", toString(key), AC->getTagStr(), AC->getID(), value));
46  undoList->add(change, true);
47  undoList->end();
48  } else {
49  delete change;
50  }
51 }
52 
53 
54 void
55 GNEChange_Attribute::changeAttribute(GNEAttributeCarrier* AC, SumoXMLAttr key, const std::string& value, const std::string& originalValue, GNEUndoList* undoList, const bool force) {
56  // create change
57  auto change = new GNEChange_Attribute(AC, key, value, originalValue);
58  // set force
59  change->myForceChange = force;
60  // check if process change
61  if (change->trueChange()) {
62  undoList->begin(AC, TLF("change '%' attribute in % '%' to '%'", toString(key), AC->getTagStr(), AC->getID(), value));
63  undoList->add(change, true);
64  undoList->end();
65  } else {
66  delete change;
67  }
68 }
69 
70 
72  // decrease reference
73  myAC->decRef("GNEChange_Attribute " + toString(myKey));
74  // remove if is unreferenced
75  if (myAC->unreferenced()) {
76  // show extra information for tests
77  WRITE_DEBUG("Deleting unreferenced " + myAC->getTagStr() + " in GNEChange_Attribute");
78  // delete AC
79  delete myAC;
80  }
81 }
82 
83 
84 void
86  // show extra information for tests
87  WRITE_DEBUG("Restoring previous attribute"/* + toString(myKey)*/);
88  // set original value
90  // certain attributes needs extra operations
91  if (myKey != GNE_ATTR_SELECTED) {
92  // check if updated attribute requires a update geometry
95  }
96  // if is a dataelement, update attribute colors
99  } else if (myAC->getTagProperty().getTag() == SUMO_TAG_DATASET) {
101  }
102  // check if networkElements, additional or shapes has to be saved (only if key isn't GNE_ATTR_SELECTED)
105  } else if (myAC->getTagProperty().isAdditionalElement()) {
107  } else if (myAC->getTagProperty().isDemandElement()) {
109  } else if (myAC->getTagProperty().isDataElement()) {
111  } else if (myAC->getTagProperty().isMeanData()) {
113  }
114  }
115 }
116 
117 
118 void
120  // show extra information for tests
121  WRITE_DEBUG("Setting new attribute"/* + toString(myKey)*/);
122  // set new value
124  // certain attributes needs extra operations
125  if (myKey != GNE_ATTR_SELECTED) {
126  // check if updated attribute requires a update geometry
128  myAC->updateGeometry();
129  }
130  // if is a dataelement, update attribute colors
131  if (myAC->getTagProperty().isGenericData()) {
133  } else if (myAC->getTagProperty().getTag() == SUMO_TAG_DATASET) {
135  }
136  // check if networkElements, additional or shapes has to be saved (only if key isn't GNE_ATTR_SELECTED)
139  } else if (myAC->getTagProperty().isAdditionalElement()) {
141  } else if (myAC->getTagProperty().isDemandElement()) {
143  } else if (myAC->getTagProperty().isDataElement()) {
145  } else if (myAC->getTagProperty().isMeanData()) {
147  }
148  }
149 }
150 
151 
152 std::string
154  return (TL("Undo change ") + myAC->getTagStr() + " attribute");
155 }
156 
157 
158 std::string
160  return (TL("Redo change ") + myAC->getTagStr() + " attribute");
161 }
162 
163 
165  GNEChange(ac->getTagProperty().getSupermode(), true, false),
166  myAC(ac),
167  myKey(key),
168  myForceChange(false),
169  myOrigValue(ac->getAttribute(key)),
170  myNewValue(value) {
171  myAC->incRef("GNEChange_Attribute " + toString(myKey));
172 }
173 
174 
175 GNEChange_Attribute::GNEChange_Attribute(GNEAttributeCarrier* ac, SumoXMLAttr key, const std::string& value, const std::string& origValue) :
176  GNEChange(ac->getTagProperty().getSupermode(), true, false),
177  myAC(ac),
178  myKey(key),
179  myForceChange(false),
180  myOrigValue(origValue),
181  myNewValue(value) {
182  myAC->incRef("GNEChange_Attribute " + toString(myKey));
183 }
184 
185 
186 bool
188  // check if we're editing the value of an attribute or changing a disjoint attribute
189  if (myForceChange) {
190  return true;
191  } else {
192  return (myOrigValue != myNewValue);
193  }
194 }
195 
196 /****************************************************************************/
#define WRITE_DEBUG(msg)
Definition: MsgHandler.h:306
#define TL(string)
Definition: MsgHandler.h:315
#define TLF(string,...)
Definition: MsgHandler.h:317
@ SUMO_TAG_DATASET
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ GNE_ATTR_SELECTED
element is selected
@ GNE_ATTR_DATASET
data set of a generic data
@ SUMO_ATTR_ID
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
const std::string getID() const
get ID (all Attribute Carriers have one)
const std::string & getTagStr() const
get tag assigned to this object in string format
const GNETagProperties & getTagProperty() const
get tagProperty associated with this Attribute Carrier
GNENet * getNet() const
get pointer to net
virtual void updateGeometry()=0
update pre-computed geometry information
virtual void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)=0
virtual std::string getAttribute(SumoXMLAttr key) const =0
bool requireUpdateGeometry() const
return true if attribute requires a update geometry in setAttribute(...)
the function-object for an editing operation (abstract base)
const SumoXMLAttr myKey
The attribute name (or the original attribute if we're editing a disjoint attribute)
bool myForceChange
flag used to force set attributes
void undo()
undo action
bool trueChange()
wether original and new value differ
std::string undoName() const
get undo Name
const std::string myNewValue
the new value
GNEChange_Attribute(GNEAttributeCarrier *ac, const SumoXMLAttr key, const std::string &value)
constructor
static void changeAttribute(GNEAttributeCarrier *AC, SumoXMLAttr key, const std::string &value, GNEUndoList *undoList, const bool force=false)
change attribute
GNEAttributeCarrier * myAC
the net to which all operations shall be applied
~GNEChange_Attribute()
Destructor.
const std::string myOrigValue
the original value
std::string redoName() const
get Redo name
the function-object for an editing operation (abstract base)
Definition: GNEChange.h:56
void updateAttributeColors()
update attribute colors deprecated
Definition: GNEDataSet.cpp:143
GNEDataSet * retrieveDataSet(const std::string &id, bool hardFail=true) const
Returns the named data set.
void requireSaveNetwork()
inform that network has to be saved
void requireSaveMeanDatas()
inform that mean data elements has to be saved
void requireSaveAdditionals()
inform that additionals has to be saved
void requireSaveDataElements()
inform that data elements has to be saved
void requireSaveDemandElements()
inform that demand elements has to be saved
GNENetHelper::SavingStatus * getSavingStatus() const
get saving status
Definition: GNENet.cpp:127
GNENetHelper::AttributeCarriers * getAttributeCarriers() const
get all attribute carriers used in this net
Definition: GNENet.cpp:121
void decRef(const std::string &debugMsg="")
Decrease reference.
void incRef(const std::string &debugMsg="")
Increase reference.
bool unreferenced()
check if object ins't referenced
bool isMeanData() const
return true if tag correspond to a mean data element
bool isGenericData() const
data elements
const GNEAttributeProperties & getAttributeProperties(SumoXMLAttr attr) const
get attribute (throw error if doesn't exist)
bool isNetworkElement() const
element sets
bool isDataElement() const
return true if tag correspond to a data element
SumoXMLTag getTag() const
get Tag vinculated with this attribute Property
bool isDemandElement() const
return true if tag correspond to a demand element
bool isAdditionalElement() const
return true if tag correspond to an additional element (note: this include TAZ, shapes and wires)
bool hasAttribute(SumoXMLAttr attr) const
check if current TagProperties owns the attribute "attr"
void end()
End undo command sub-group. If the sub-group is still empty, it will be deleted; otherwise,...
void begin(GUIIcon icon, const std::string &description)
Begin undo command sub-group with current supermode. This begins a new group of commands that are tre...
void add(GNEChange *command, bool doit=false, bool merge=true)
Add new command, executing it if desired. The new command will be merged with the previous command if...
Definition: json.hpp:4471