1 #include "NodeCommands.h"
2 
3 #include "Node.h"
4 #include "Layer.h"
5 #include "DirtyList.h"
6 
MoveNodeCommand()7 MoveNodeCommand::MoveNodeCommand()
8 : Command(0), theLayer(0), oldLayer(0), OldPos(Coord(0, 0)), NewPos(Coord(0, 0))
9 {
10 }
11 
MoveNodeCommand(Node * aPt)12 MoveNodeCommand::MoveNodeCommand(Node* aPt)
13 : Command(aPt), theLayer(0), oldLayer(0), OldPos(Coord(0, 0)), NewPos(Coord(0, 0))
14 {
15     if (!theLayer)
16         theLayer = thePoint->layer();
17     description = QApplication::tr("Move node %1").arg(aPt->description());
18 }
19 
MoveNodeCommand(Node * aPt,const Coord & aPos,Layer * aLayer)20 MoveNodeCommand::MoveNodeCommand(Node* aPt, const Coord& aPos, Layer* aLayer)
21 : Command(aPt), theLayer(aLayer), oldLayer(0), thePoint(aPt), OldPos(aPt->position()), NewPos(aPos)
22 {
23     if (!theLayer)
24         theLayer = thePoint->layer();
25     description = QApplication::tr("Move node %1").arg(aPt->description());
26     redo();
27 }
28 
~MoveNodeCommand(void)29 MoveNodeCommand::~MoveNodeCommand(void)
30 {
31     if (oldLayer)
32         oldLayer->decDirtyLevel(commandDirtyLevel);
33 }
34 
undo()35 void MoveNodeCommand::undo()
36 {
37     Command::undo();
38     thePoint->setPosition(OldPos);
39     if (theLayer && oldLayer && (theLayer != oldLayer)) {
40         theLayer->remove(thePoint);
41         oldLayer->add(thePoint);
42     }
43     decDirtyLevel(oldLayer, thePoint);
44 }
45 
redo()46 void MoveNodeCommand::redo()
47 {
48     oldLayer = thePoint->layer();
49     thePoint->setPosition(NewPos);
50     thePoint->setVirtual(false);
51     if (theLayer && oldLayer && (theLayer != oldLayer)) {
52         oldLayer->remove(thePoint);
53         theLayer->add(thePoint);
54     }
55     incDirtyLevel(oldLayer, thePoint);
56     Command::redo();
57 }
58 
buildDirtyList(DirtyList & theList)59 bool MoveNodeCommand::buildDirtyList(DirtyList &theList)
60 {
61     if (isUndone)
62         return false;
63     if (thePoint->lastUpdated() == Feature::NotYetDownloaded)
64         return theList.noop(thePoint);
65     if (!thePoint->layer())
66         return theList.update(thePoint);
67     if (thePoint->isUploadable())
68         return theList.update(thePoint);
69 
70     return theList.noop(thePoint);
71 }
72 
toXML(QXmlStreamWriter & stream) const73 bool MoveNodeCommand::toXML(QXmlStreamWriter& stream) const
74 {
75     bool OK = true;
76 
77     stream.writeStartElement("MoveTrackPointCommand");
78 
79     stream.writeAttribute("xml:id", id());
80     stream.writeAttribute("trackpoint", thePoint->xmlId());
81     if (theLayer)
82         stream.writeAttribute("layer", theLayer->id());
83     if (oldLayer)
84         stream.writeAttribute("oldlayer", oldLayer->id());
85     OldPos.toXML("oldpos", stream);
86     NewPos.toXML("newpos", stream);
87 
88     Command::toXML(stream);
89     stream.writeEndElement();
90 
91     return OK;
92 }
93 
fromXML(Document * d,QXmlStreamReader & stream)94 MoveNodeCommand * MoveNodeCommand::fromXML(Document * d, QXmlStreamReader& stream)
95 {
96     MoveNodeCommand* a = new MoveNodeCommand();
97     a->setId(stream.attributes().value("xml:id").toString());
98 
99     if (stream.attributes().hasAttribute("layer"))
100         a->theLayer = d->getLayer(stream.attributes().value("layer").toString());
101     else
102         a->theLayer = NULL;
103     if (stream.attributes().hasAttribute("oldlayer"))
104         a->oldLayer = d->getLayer(stream.attributes().value("oldlayer").toString());
105     else
106         a->oldLayer = NULL;
107     if (!a->theLayer)
108         return NULL;
109 
110     a->thePoint = Feature::getNodeOrCreatePlaceHolder(d, a->theLayer, IFeature::FId(IFeature::Point, stream.attributes().value("trackpoint").toString().toLongLong()));
111     a->description = QApplication::tr("Move node %1").arg(a->thePoint->description());
112 
113     stream.readNext();
114     while(!stream.atEnd() && !stream.isEndElement()) {
115         if (stream.name() == "oldpos") {
116             a->OldPos = Coord::fromXML(stream);
117         } else if (stream.name() == "newpos") {
118             a->NewPos = Coord::fromXML(stream);
119         } else if (stream.name() == "Command") {
120             Command::fromXML(d, stream, a);
121         }
122         stream.readNext();
123     }
124 
125     return a;
126 }
127 
128 
129 
130