1
#include "FeatureCommands.h"
3
#include "Map/MapDocument.h"
4
#include "Map/MapFeature.h"
5
#include "Map/MapLayer.h"
6
#include "Sync/DirtyList.h"
8
TagCommand::TagCommand(MapFeature* aF, MapLayer* aLayer)
9
: Command(aF), theFeature(aF), FirstRun(true), theLayer(aLayer), oldLayer(0)
13
TagCommand::TagCommand(MapFeature* aF)
14
: Command(aF), theLayer(0), oldLayer(0)
18
TagCommand::~TagCommand(void)
21
oldLayer->decDirtyLevel(commandDirtyLevel);
24
//void TagCommand::undo()
26
// theFeature->clearTags();
27
// for (unsigned int i=0; i<Before.size(); ++i)
28
// theFeature->setTag(Before[i].first,Before[i].second);
31
//void TagCommand::redo()
34
// for (unsigned int i=0; i<theFeature->tagSize(); ++i)
35
// After.push_back(std::make_pair(theFeature->tagKey(i),theFeature->tagValue(i)));
38
// theFeature->clearTags();
39
// for (unsigned int i=0; i<After.size(); ++i)
40
// theFeature->setTag(After[i].first,After[i].second);
45
bool TagCommand::buildDirtyList(DirtyList& theList)
49
if (theFeature->lastUpdated() == MapFeature::NotYetDownloaded)
50
return theList.noop(theFeature);
51
if (theLayer->isUploadable())
52
return theList.update(theFeature);
54
return theList.noop(theFeature);
57
SetTagCommand::SetTagCommand(MapFeature* aF)
58
: TagCommand(aF, 0), theIdx(0), theK(""), theV("")
60
oldLayer = theFeature->layer();
63
SetTagCommand::SetTagCommand(MapFeature* aF, unsigned int idx, const QString& k, const QString& v, MapLayer* aLayer)
64
: TagCommand(aF, aLayer), theIdx(idx), theK(k), theV(v)
66
description = MainWindow::tr("Set Tag '%1=%2' on %3").arg(k).arg(v).arg(aF->description());
67
oldLayer = theFeature->layer();
71
SetTagCommand::SetTagCommand(MapFeature* aF, const QString& k, const QString& v, MapLayer* aLayer)
72
: TagCommand(aF, aLayer), theIdx(-1), theK(k), theV(v)
74
description = MainWindow::tr("Set Tag '%1=%2' on %3").arg(k).arg(v).arg(aF->description());
75
oldLayer = theFeature->layer();
79
void SetTagCommand::undo()
82
if (theFeature->isUploaded()) {
83
theLayer = theLayer->getDocument()->getUploadedLayer();
84
oldLayer = theLayer->getDocument()->getDirtyOrOriginLayer();
86
if (oldV != TAG_UNDEF_VALUE && oldK != KEY_UNDEF_VALUE)
88
if(oldK!=theK) theFeature->clearTag(theK);
89
theFeature->setTag(theIdx,oldK,oldV);
92
theFeature->clearTag(theK);
94
if (theLayer && oldLayer && (theLayer != oldLayer)) {
95
theLayer->remove(theFeature);
96
oldLayer->add(theFeature);
97
decDirtyLevel(oldLayer);
101
void SetTagCommand::redo()
103
oldV = TAG_UNDEF_VALUE;
104
oldK = KEY_UNDEF_VALUE;
107
oldV = theFeature->tagValue(theK, TAG_UNDEF_VALUE);
108
theFeature->setTag(theK,theV);
110
oldV = theFeature->tagValue(theIdx);
111
oldK = theFeature->tagKey(theIdx);
112
if(oldK!=theK) theFeature->clearTag(oldK);
113
theFeature->setTag(theIdx,theK,theV);
116
if (theLayer && oldLayer && (theLayer != oldLayer)) {
117
oldLayer->remove(theFeature);
118
incDirtyLevel(oldLayer);
119
theLayer->add(theFeature);
125
bool SetTagCommand::buildDirtyList(DirtyList& theList)
129
if (theFeature->lastUpdated() == MapFeature::NotYetDownloaded)
130
return theList.noop(theFeature);
131
if (theK.startsWith('_') && (theK.endsWith('_')))
132
return theList.noop(theFeature);
134
return theList.update(theFeature);
137
bool SetTagCommand::toXML(QDomElement& xParent) const
141
QDomElement e = xParent.ownerDocument().createElement("SetTagCommand");
142
xParent.appendChild(e);
144
e.setAttribute("oldkey", oldK);
145
e.setAttribute("xml:id", id());
146
e.setAttribute("feature", theFeature->xmlId());
147
e.setAttribute("idx", QString::number(theIdx));
148
e.setAttribute("key", theK);
149
e.setAttribute("value", theV);
150
e.setAttribute("oldvalue", oldV);
152
e.setAttribute("layer", theLayer->id());
154
e.setAttribute("oldlayer", oldLayer->id());
161
SetTagCommand * SetTagCommand::fromXML(MapDocument * d, QDomElement e)
164
if (!(F = d->getFeature(e.attribute("feature"), false)))
167
SetTagCommand* a = new SetTagCommand(F);
168
a->setId(e.attribute("xml:id"));
169
if (e.hasAttribute("oldkey"))
170
a->oldK = e.attribute("oldkey");
171
if (!(F = d->getFeature(e.attribute("feature"), false)))
174
a->theIdx = e.attribute("idx").toInt();
175
a->theK = e.attribute("key");
176
a->theV = e.attribute("value");
177
if (e.hasAttribute("oldvalue"))
178
a->oldV = e.attribute("oldvalue");
179
if (e.hasAttribute("layer"))
180
a->theLayer = d->getLayer(e.attribute("layer"));
183
if (e.hasAttribute("oldlayer"))
184
a->oldLayer = d->getLayer(e.attribute("oldlayer"));
188
a->description = MainWindow::tr("Set Tag '%1=%2' on %3").arg(a->theK).arg(a->theV).arg(a->theFeature->description());
190
Command::fromXML(d, e, a);
196
/* CLEARTAGSCOMMAND */
198
ClearTagsCommand::ClearTagsCommand(MapFeature* F, MapLayer* aLayer)
199
: TagCommand(F, aLayer)
201
for (unsigned int i=0; i<theFeature->tagSize(); ++i)
202
Before.push_back(std::make_pair(theFeature->tagKey(i),theFeature->tagValue(i)));
203
oldLayer = theFeature->layer();
207
void ClearTagsCommand::undo()
210
if (theFeature->isUploaded()) {
211
theLayer = theLayer->getDocument()->getUploadedLayer();
212
oldLayer = theLayer->getDocument()->getDirtyOrOriginLayer();
214
theFeature->clearTags();
215
for (unsigned int i=0; i<Before.size(); ++i)
216
theFeature->setTag(Before[i].first,Before[i].second);
218
if (theLayer && oldLayer && (theLayer != oldLayer)) {
219
theLayer->remove(theFeature);
220
oldLayer->add(theFeature);
221
decDirtyLevel(oldLayer);
225
void ClearTagsCommand::redo()
227
theFeature->clearTags();
229
if (theLayer && oldLayer && (theLayer != oldLayer)) {
230
oldLayer->remove(theFeature);
231
incDirtyLevel(oldLayer);
232
theLayer->add(theFeature);
237
bool ClearTagsCommand::toXML(QDomElement& xParent) const
241
QDomElement e = xParent.ownerDocument().createElement("ClearTagsCommand");
242
xParent.appendChild(e);
244
e.setAttribute("xml:id", id());
245
e.setAttribute("feature", theFeature->xmlId());
247
e.setAttribute("layer", theLayer->id());
249
e.setAttribute("oldlayer", oldLayer->id());
251
for (unsigned int i=0; i<Before.size(); ++i)
253
QDomElement c = e.ownerDocument().createElement("tag");
256
c.setAttribute("k", Before[i].first);
257
c.setAttribute("v", Before[i].second);
265
ClearTagsCommand * ClearTagsCommand::fromXML(MapDocument * d, QDomElement e)
268
if (!(F = d->getFeature(e.attribute("feature"), false)))
271
ClearTagsCommand* a = new ClearTagsCommand(F);
272
a->setId(e.attribute("xml:id"));
274
if (e.hasAttribute("layer"))
275
a->theLayer = d->getLayer(e.attribute("layer"));
278
if (e.hasAttribute("oldlayer"))
279
a->oldLayer = d->getLayer(e.attribute("oldlayer"));
283
QDomElement c = e.firstChildElement();
285
if (c.tagName() == "tag") {
286
a->Before.push_back(std::make_pair(c.attribute("k"),c.attribute("v")));
288
c = c.nextSiblingElement();
291
Command::fromXML(d, e, a);
296
/* CLEARTAGCOMMAND */
298
ClearTagCommand::ClearTagCommand(MapFeature* F)
299
: TagCommand(F, 0), theIdx(0), theK(""), theV("")
301
oldLayer = theFeature->layer();
304
ClearTagCommand::ClearTagCommand(MapFeature* F, const QString& k, MapLayer* aLayer)
305
: TagCommand(F, aLayer), theIdx(F->findKey(k)), theK(k), theV(F->tagValue(k, ""))
307
description = MainWindow::tr("Clear Tag '%1' on %2").arg(k).arg(F->description());
308
oldLayer = theFeature->layer();
312
void ClearTagCommand::undo()
315
if (theFeature->isUploaded()) {
316
theLayer = theLayer->getDocument()->getUploadedLayer();
317
oldLayer = theLayer->getDocument()->getDirtyOrOriginLayer();
319
theFeature->setTag(theIdx,theK,theV);
320
if (theLayer && oldLayer && (theLayer != oldLayer)) {
321
theLayer->remove(theFeature);
322
oldLayer->add(theFeature);
323
decDirtyLevel(oldLayer);
327
void ClearTagCommand::redo()
329
theFeature->clearTag(theK);
330
if (theLayer && oldLayer && (theLayer != oldLayer)) {
331
oldLayer->remove(theFeature);
332
incDirtyLevel(oldLayer);
333
theLayer->add(theFeature);
338
bool ClearTagCommand::buildDirtyList(DirtyList& theList)
342
if (theFeature->lastUpdated() == MapFeature::NotYetDownloaded)
343
return theList.noop(theFeature);
344
if (theLayer->isUploadable())
345
if (theK.startsWith('_') && (theK.endsWith('_')))
346
return theList.noop(theFeature);
348
return theList.update(theFeature);
350
return theList.noop(theFeature);
353
bool ClearTagCommand::toXML(QDomElement& xParent) const
357
QDomElement e = xParent.ownerDocument().createElement("ClearTagCommand");
358
xParent.appendChild(e);
360
e.setAttribute("xml:id", id());
361
e.setAttribute("feature", theFeature->xmlId());
362
e.setAttribute("idx", QString::number(theIdx));
363
e.setAttribute("key", theK);
364
e.setAttribute("value", theV);
366
e.setAttribute("layer", theLayer->id());
368
e.setAttribute("oldlayer", oldLayer->id());
375
ClearTagCommand * ClearTagCommand::fromXML(MapDocument * d, QDomElement e)
378
if (!(F = d->getFeature(e.attribute("feature"), false)))
381
ClearTagCommand* a = new ClearTagCommand(F);
382
a->setId(e.attribute("xml:id"));
384
a->theIdx = e.attribute("idx").toInt();
385
a->theK = e.attribute("key");
386
a->theV = e.attribute("value");
387
if (e.hasAttribute("layer"))
388
a->theLayer = d->getLayer(e.attribute("layer"));
391
if (e.hasAttribute("oldlayer"))
392
a->oldLayer = d->getLayer(e.attribute("oldlayer"));
396
a->description = MainWindow::tr("Clear Tag '%1' on %2").arg(a->theK).arg(a->theFeature->description());
398
Command::fromXML(d, e, a);