~ubuntu-branches/ubuntu/saucy/merkaartor/saucy

« back to all changes in this revision

Viewing changes to Command/RelationCommands.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bernd Zeimetz
  • Date: 2009-09-13 00:52:12 UTC
  • mto: (1.2.7 upstream) (0.1.3 upstream) (3.1.7 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: james.westby@ubuntu.com-20090913005212-pjecal8zxm07x0fj
ImportĀ upstreamĀ versionĀ 0.14+svnfixes~20090912

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "RelationCommands.h"
2
 
 
3
 
#include "Map/Relation.h"
4
 
#include "Map/MapFeature.h"
5
 
#include "Map/MapLayer.h"
6
 
#include "Sync/DirtyList.h"
7
 
 
8
 
RelationAddFeatureCommand::RelationAddFeatureCommand(Relation* R)
9
 
: Command(R), theLayer(0), oldLayer(0), theRelation(R), Role(""), theMapFeature(0), Position(0)
10
 
{
11
 
}
12
 
 
13
 
RelationAddFeatureCommand::RelationAddFeatureCommand(Relation* R, const QString& aRole, MapFeature* W, MapLayer* aLayer)
14
 
: Command(R), theLayer(aLayer), oldLayer(0), theRelation(R), Role(aRole), theMapFeature(W), Position(theRelation->size())
15
 
{
16
 
        redo();
17
 
}
18
 
 
19
 
RelationAddFeatureCommand::RelationAddFeatureCommand(Relation* R, const QString& aRole, MapFeature* W, unsigned int aPos, MapLayer* aLayer)
20
 
: Command(R), theLayer(aLayer), oldLayer(0), theRelation(R), Role(aRole), theMapFeature(W), Position(aPos)
21
 
{
22
 
        redo();
23
 
}
24
 
 
25
 
RelationAddFeatureCommand::~RelationAddFeatureCommand(void)
26
 
{
27
 
        if (oldLayer)
28
 
                oldLayer->decDirtyLevel(commandDirtyLevel);
29
 
}
30
 
 
31
 
void RelationAddFeatureCommand::undo()
32
 
{
33
 
        Command::undo();
34
 
        theRelation->remove(Position);
35
 
        if (theLayer && oldLayer && (theLayer != oldLayer)) {
36
 
                theLayer->remove(theRelation);
37
 
                oldLayer->add(theRelation);
38
 
                decDirtyLevel(oldLayer);
39
 
        }
40
 
}
41
 
 
42
 
void RelationAddFeatureCommand::redo()
43
 
{
44
 
        theRelation->add(Role, theMapFeature, Position);
45
 
        oldLayer = theRelation->layer();
46
 
        if (theLayer && oldLayer && (theLayer != oldLayer)) {
47
 
                oldLayer->remove(theRelation);
48
 
                incDirtyLevel(oldLayer);
49
 
                theLayer->add(theRelation);
50
 
        }
51
 
        Command::redo();
52
 
}
53
 
 
54
 
bool RelationAddFeatureCommand::buildDirtyList(DirtyList& theList)
55
 
{
56
 
        if (isUndone)
57
 
                return false;
58
 
        if (theRelation->lastUpdated() == MapFeature::NotYetDownloaded)
59
 
                return theList.noop(theRelation);
60
 
        if (!theRelation->layer())
61
 
                return theList.update(theRelation);
62
 
        if (theRelation->layer()->isUploadable())
63
 
                return theList.update(theRelation);
64
 
 
65
 
        return theList.noop(theRelation);
66
 
}
67
 
 
68
 
bool RelationAddFeatureCommand::toXML(QDomElement& xParent) const
69
 
{
70
 
        bool OK = true;
71
 
 
72
 
        QDomElement e = xParent.ownerDocument().createElement("RelationAddFeatureCommand");
73
 
        xParent.appendChild(e);
74
 
 
75
 
        e.setAttribute("xml:id", id());
76
 
        e.setAttribute("relation", theRelation->xmlId());
77
 
        e.setAttribute("role", Role);
78
 
        e.setAttribute("feature", theMapFeature->xmlId());
79
 
        e.setAttribute("pos", QString::number(Position));
80
 
        if (theLayer)
81
 
                e.setAttribute("layer", theLayer->id());
82
 
        if (oldLayer)
83
 
                e.setAttribute("oldlayer", oldLayer->id());
84
 
 
85
 
        Command::toXML(e);
86
 
 
87
 
        return OK;
88
 
}
89
 
 
90
 
RelationAddFeatureCommand * RelationAddFeatureCommand::fromXML(MapDocument * d, QDomElement e)
91
 
{
92
 
        RelationAddFeatureCommand* a = new RelationAddFeatureCommand();
93
 
        a->setId(e.attribute("xml:id"));
94
 
        if (e.hasAttribute("layer"))
95
 
                a->theLayer = d->getLayer(e.attribute("layer"));
96
 
        else
97
 
                a->theLayer = d->getDirtyOrOriginLayer();
98
 
        if (e.hasAttribute("oldlayer"))
99
 
                a->oldLayer = d->getLayer(e.attribute("oldlayer"));
100
 
        else
101
 
                a->oldLayer = NULL;
102
 
        a->theRelation = MapFeature::getRelationOrCreatePlaceHolder(d, a->theLayer, e.attribute("relation"));
103
 
        MapFeature* F;
104
 
        if (e.attribute("featureclass") == "TrackPoint") {
105
 
                F = (MapFeature*) MapFeature::getTrackPointOrCreatePlaceHolder(d, a->theLayer, e.attribute("trackpoint"));
106
 
        } else 
107
 
        if (e.attribute("featureclass") == "Road") {
108
 
                F = (MapFeature*) MapFeature::getWayOrCreatePlaceHolder(d, a->theLayer, e.attribute("road"));
109
 
        } else 
110
 
        if (e.attribute("featureclass") == "Relation") {
111
 
                F = (MapFeature*) MapFeature::getRelationOrCreatePlaceHolder(d, a->theLayer, e.attribute("road"));
112
 
        } else {
113
 
                if (!(F = d->getFeature(e.attribute("feature"), false)))
114
 
                        return NULL;
115
 
        }
116
 
        a->Role = e.attribute("role");
117
 
        a->theMapFeature = F;
118
 
        a->Position = e.attribute("pos").toUInt();
119
 
 
120
 
        Command::fromXML(d, e, a);
121
 
 
122
 
        return a;
123
 
}
124
 
 
125
 
/* ROADREMOVEMapFeatureCOMMAND */
126
 
 
127
 
RelationRemoveFeatureCommand::RelationRemoveFeatureCommand(Relation* R)
128
 
: Command(R), theLayer(0), oldLayer(0), Idx(0), theRelation(R), theMapFeature(0)
129
 
{
130
 
}
131
 
 
132
 
RelationRemoveFeatureCommand::RelationRemoveFeatureCommand(Relation* R, MapFeature* W, MapLayer* aLayer)
133
 
: Command(R), theLayer(aLayer), oldLayer(0), Idx(R->find(W)), theRelation(R), theMapFeature(W)
134
 
{
135
 
        Role = R->getRole(Idx);
136
 
        redo();
137
 
}
138
 
 
139
 
RelationRemoveFeatureCommand::RelationRemoveFeatureCommand(Relation* R, unsigned int anIdx, MapLayer* aLayer)
140
 
: Command(R), theLayer(aLayer), oldLayer(0), Idx(anIdx), theRelation(R), Role(R->getRole(anIdx)), theMapFeature(R->get(anIdx))
141
 
{
142
 
        redo();
143
 
}
144
 
 
145
 
RelationRemoveFeatureCommand::~RelationRemoveFeatureCommand(void)
146
 
{
147
 
        oldLayer->decDirtyLevel(commandDirtyLevel);
148
 
}
149
 
 
150
 
 
151
 
void RelationRemoveFeatureCommand::undo()
152
 
{
153
 
        Command::undo();
154
 
        theRelation->add(Role,theMapFeature,Idx);
155
 
        if (theLayer && oldLayer && (theLayer != oldLayer)) {
156
 
                theLayer->remove(theRelation);
157
 
                oldLayer->add(theRelation);
158
 
                decDirtyLevel(oldLayer);
159
 
        }
160
 
}
161
 
 
162
 
void RelationRemoveFeatureCommand::redo()
163
 
{
164
 
        theRelation->remove(Idx);
165
 
        oldLayer = theRelation->layer();
166
 
        if (theLayer && oldLayer && (theLayer != oldLayer)) {
167
 
                oldLayer->remove(theRelation);
168
 
                incDirtyLevel(oldLayer);
169
 
                theLayer->add(theRelation);
170
 
        }
171
 
        Command::redo();
172
 
}
173
 
 
174
 
bool RelationRemoveFeatureCommand::buildDirtyList(DirtyList& theList)
175
 
{
176
 
        if (isUndone)
177
 
                return false;
178
 
        if (theRelation->lastUpdated() == MapFeature::NotYetDownloaded)
179
 
                return theList.noop(theRelation);
180
 
        if (!theRelation->layer())
181
 
                return theList.update(theRelation);
182
 
        if (theRelation->layer()->isUploadable())
183
 
                return theList.update(theRelation);
184
 
 
185
 
        return theList.noop(theRelation);
186
 
}
187
 
 
188
 
bool RelationRemoveFeatureCommand::toXML(QDomElement& xParent) const
189
 
{
190
 
        bool OK = true;
191
 
 
192
 
        QDomElement e = xParent.ownerDocument().createElement("RelationRemoveFeatureCommand");
193
 
        xParent.appendChild(e);
194
 
 
195
 
        e.setAttribute("xml:id", id());
196
 
        e.setAttribute("relation", theRelation->xmlId());
197
 
        e.setAttribute("feature", theMapFeature->xmlId());
198
 
        e.setAttribute("featureclass", theMapFeature->getClass());
199
 
        e.setAttribute("index", QString::number(Idx));
200
 
        e.setAttribute("role", Role);
201
 
        if (theLayer)
202
 
                e.setAttribute("layer", theLayer->id());
203
 
        if (oldLayer)
204
 
                e.setAttribute("oldlayer", oldLayer->id());
205
 
 
206
 
        Command::toXML(e);
207
 
 
208
 
        return OK;
209
 
}
210
 
 
211
 
RelationRemoveFeatureCommand * RelationRemoveFeatureCommand::fromXML(MapDocument * d, QDomElement e)
212
 
{
213
 
        RelationRemoveFeatureCommand* a = new RelationRemoveFeatureCommand();
214
 
 
215
 
        a->setId(e.attribute("xml:id"));
216
 
        if (e.hasAttribute("layer"))
217
 
                a->theLayer = d->getLayer(e.attribute("layer"));
218
 
        else
219
 
                a->theLayer = d->getDirtyOrOriginLayer();
220
 
        if (e.hasAttribute("oldlayer"))
221
 
                a->oldLayer = d->getLayer(e.attribute("oldlayer"));
222
 
        else
223
 
                a->oldLayer = NULL;
224
 
        a->theRelation = MapFeature::getRelationOrCreatePlaceHolder(d, a->theLayer, e.attribute("relation"));
225
 
        MapFeature* F = NULL;
226
 
        if (e.attribute("featureclass") == "TrackPoint") {
227
 
                F = (MapFeature*) MapFeature::getTrackPointOrCreatePlaceHolder(d, a->theLayer, e.attribute("feature"));
228
 
        } else 
229
 
        if (e.attribute("featureclass") == "Road") {
230
 
                F = (MapFeature*) MapFeature::getWayOrCreatePlaceHolder(d, a->theLayer, e.attribute("feature"));
231
 
        } else 
232
 
        if (e.attribute("featureclass") == "Relation") {
233
 
                F = (MapFeature*) MapFeature::getRelationOrCreatePlaceHolder(d, a->theLayer, e.attribute("feature"));
234
 
        }
235
 
        a->theMapFeature = F;
236
 
        a->Idx = e.attribute("index").toInt();
237
 
        a->Role = e.attribute("role");
238
 
 
239
 
        Command::fromXML(d, e, a);
240
 
 
241
 
        return a;
242
 
}
243
 
 
244
 
 
245
 
 
246