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

« back to all changes in this revision

Viewing changes to Command/FeatureCommands.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 "FeatureCommands.h"
2
 
 
3
 
#include "Map/MapDocument.h"
4
 
#include "Map/MapFeature.h"
5
 
#include "Map/MapLayer.h"
6
 
#include "Sync/DirtyList.h"
7
 
 
8
 
TagCommand::TagCommand(MapFeature* aF, MapLayer* aLayer)
9
 
: Command(aF), theFeature(aF), FirstRun(true), theLayer(aLayer), oldLayer(0)
10
 
{
11
 
}
12
 
 
13
 
TagCommand::TagCommand(MapFeature* aF)
14
 
: Command(aF), theLayer(0), oldLayer(0)
15
 
{
16
 
}
17
 
 
18
 
TagCommand::~TagCommand(void)
19
 
{
20
 
        if (oldLayer)
21
 
                oldLayer->decDirtyLevel(commandDirtyLevel);
22
 
}
23
 
 
24
 
//void TagCommand::undo()
25
 
//{
26
 
//      theFeature->clearTags();
27
 
//      for (unsigned int i=0; i<Before.size(); ++i)
28
 
//              theFeature->setTag(Before[i].first,Before[i].second);
29
 
//}
30
 
//
31
 
//void TagCommand::redo()
32
 
//{
33
 
//      if (FirstRun)
34
 
//              for (unsigned int i=0; i<theFeature->tagSize(); ++i)
35
 
//                      After.push_back(std::make_pair(theFeature->tagKey(i),theFeature->tagValue(i)));
36
 
//      else
37
 
//      {
38
 
//              theFeature->clearTags();
39
 
//              for (unsigned int i=0; i<After.size(); ++i)
40
 
//                      theFeature->setTag(After[i].first,After[i].second);
41
 
//      }
42
 
//      FirstRun = false;
43
 
//}
44
 
//
45
 
bool TagCommand::buildDirtyList(DirtyList& theList)
46
 
{
47
 
        if (isUndone)
48
 
                return false;
49
 
        if (theFeature->lastUpdated() == MapFeature::NotYetDownloaded)
50
 
                return theList.noop(theFeature);
51
 
        if (theLayer->isUploadable())
52
 
                return theList.update(theFeature);
53
 
 
54
 
        return theList.noop(theFeature);
55
 
}
56
 
 
57
 
SetTagCommand::SetTagCommand(MapFeature* aF)
58
 
: TagCommand(aF, 0), theIdx(0), theK(""), theV("")
59
 
{
60
 
        oldLayer = theFeature->layer();
61
 
}
62
 
 
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)
65
 
{
66
 
        description = MainWindow::tr("Set Tag '%1=%2' on %3").arg(k).arg(v).arg(aF->description());
67
 
        oldLayer = theFeature->layer();
68
 
        redo();
69
 
}
70
 
 
71
 
SetTagCommand::SetTagCommand(MapFeature* aF, const QString& k, const QString& v, MapLayer* aLayer)
72
 
: TagCommand(aF, aLayer), theIdx(-1), theK(k), theV(v)
73
 
{
74
 
        description = MainWindow::tr("Set Tag '%1=%2' on %3").arg(k).arg(v).arg(aF->description());
75
 
        oldLayer = theFeature->layer();
76
 
        redo();
77
 
}
78
 
 
79
 
void SetTagCommand::undo()
80
 
{
81
 
        Command::undo();
82
 
        if (theFeature->isUploaded()) {
83
 
                theLayer = theLayer->getDocument()->getUploadedLayer();
84
 
                oldLayer = theLayer->getDocument()->getDirtyOrOriginLayer();
85
 
        }
86
 
        if (oldV != TAG_UNDEF_VALUE && oldK != KEY_UNDEF_VALUE)
87
 
        {
88
 
                if(oldK!=theK) theFeature->clearTag(theK);
89
 
                theFeature->setTag(theIdx,oldK,oldV);
90
 
        }
91
 
        else
92
 
                theFeature->clearTag(theK);
93
 
 
94
 
        if (theLayer && oldLayer && (theLayer != oldLayer)) {
95
 
                theLayer->remove(theFeature);
96
 
                oldLayer->add(theFeature);
97
 
                decDirtyLevel(oldLayer);
98
 
        }
99
 
}
100
 
 
101
 
void SetTagCommand::redo()
102
 
{
103
 
        oldV = TAG_UNDEF_VALUE;
104
 
        oldK = KEY_UNDEF_VALUE;
105
 
        if (theIdx < 0) {
106
 
                oldK = theK;
107
 
                oldV = theFeature->tagValue(theK, TAG_UNDEF_VALUE);
108
 
                theFeature->setTag(theK,theV);
109
 
        } else {
110
 
                oldV = theFeature->tagValue(theIdx);
111
 
                oldK = theFeature->tagKey(theIdx);
112
 
                if(oldK!=theK) theFeature->clearTag(oldK);
113
 
                theFeature->setTag(theIdx,theK,theV);
114
 
        }
115
 
 
116
 
        if (theLayer && oldLayer && (theLayer != oldLayer)) {
117
 
                oldLayer->remove(theFeature);
118
 
                incDirtyLevel(oldLayer);
119
 
                theLayer->add(theFeature);
120
 
 
121
 
        }
122
 
        Command::redo();
123
 
}
124
 
 
125
 
bool SetTagCommand::buildDirtyList(DirtyList& theList)
126
 
{
127
 
        if (isUndone)
128
 
                return false;
129
 
        if (theFeature->lastUpdated() == MapFeature::NotYetDownloaded)
130
 
                return theList.noop(theFeature);
131
 
        if (theK.startsWith('_') && (theK.endsWith('_')))
132
 
                return theList.noop(theFeature);
133
 
        else
134
 
                return theList.update(theFeature);
135
 
}
136
 
 
137
 
bool SetTagCommand::toXML(QDomElement& xParent) const
138
 
{
139
 
        bool OK = true;
140
 
 
141
 
        QDomElement e = xParent.ownerDocument().createElement("SetTagCommand");
142
 
        xParent.appendChild(e);
143
 
 
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);
151
 
        if (theLayer)
152
 
            e.setAttribute("layer", theLayer->id());
153
 
        if (oldLayer)
154
 
            e.setAttribute("oldlayer", oldLayer->id());
155
 
 
156
 
        Command::toXML(e);
157
 
 
158
 
        return OK;
159
 
}
160
 
 
161
 
SetTagCommand * SetTagCommand::fromXML(MapDocument * d, QDomElement e)
162
 
{
163
 
        MapFeature* F;
164
 
        if (!(F = d->getFeature(e.attribute("feature"), false)))
165
 
                return NULL;
166
 
 
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)))
172
 
                return NULL;
173
 
        a->theFeature = F;
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"));
181
 
        else
182
 
                a->theLayer = NULL;
183
 
        if (e.hasAttribute("oldlayer"))
184
 
                a->oldLayer = d->getLayer(e.attribute("oldlayer"));
185
 
        else
186
 
                a->oldLayer = NULL;
187
 
 
188
 
        a->description = MainWindow::tr("Set Tag '%1=%2' on %3").arg(a->theK).arg(a->theV).arg(a->theFeature->description());
189
 
 
190
 
        Command::fromXML(d, e, a);
191
 
 
192
 
        return a;
193
 
}
194
 
 
195
 
 
196
 
/* CLEARTAGSCOMMAND */
197
 
 
198
 
ClearTagsCommand::ClearTagsCommand(MapFeature* F, MapLayer* aLayer)
199
 
: TagCommand(F, aLayer)
200
 
{
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();
204
 
        redo();
205
 
}
206
 
 
207
 
void ClearTagsCommand::undo()
208
 
{
209
 
        Command::undo();
210
 
        if (theFeature->isUploaded()) {
211
 
                theLayer = theLayer->getDocument()->getUploadedLayer();
212
 
                oldLayer = theLayer->getDocument()->getDirtyOrOriginLayer();
213
 
        }
214
 
        theFeature->clearTags();
215
 
        for (unsigned int i=0; i<Before.size(); ++i)
216
 
                theFeature->setTag(Before[i].first,Before[i].second);
217
 
 
218
 
        if (theLayer && oldLayer && (theLayer != oldLayer)) {
219
 
                theLayer->remove(theFeature);
220
 
                oldLayer->add(theFeature);
221
 
                decDirtyLevel(oldLayer);
222
 
        }
223
 
}
224
 
 
225
 
void ClearTagsCommand::redo()
226
 
{
227
 
        theFeature->clearTags();
228
 
 
229
 
        if (theLayer && oldLayer && (theLayer != oldLayer)) {
230
 
                oldLayer->remove(theFeature);
231
 
                incDirtyLevel(oldLayer);
232
 
                theLayer->add(theFeature);
233
 
        }
234
 
        Command::redo();
235
 
}
236
 
 
237
 
bool ClearTagsCommand::toXML(QDomElement& xParent) const
238
 
{
239
 
        bool OK = true;
240
 
 
241
 
        QDomElement e = xParent.ownerDocument().createElement("ClearTagsCommand");
242
 
        xParent.appendChild(e);
243
 
 
244
 
        e.setAttribute("xml:id", id());
245
 
        e.setAttribute("feature", theFeature->xmlId());
246
 
        if (theLayer)
247
 
            e.setAttribute("layer", theLayer->id());
248
 
        if (oldLayer)
249
 
                e.setAttribute("oldlayer", oldLayer->id());
250
 
 
251
 
        for (unsigned int i=0; i<Before.size(); ++i)
252
 
        {
253
 
                QDomElement c = e.ownerDocument().createElement("tag");
254
 
                e.appendChild(c);
255
 
 
256
 
                c.setAttribute("k", Before[i].first);
257
 
                c.setAttribute("v", Before[i].second);
258
 
        }
259
 
 
260
 
        Command::toXML(e);
261
 
 
262
 
        return OK;
263
 
}
264
 
 
265
 
ClearTagsCommand * ClearTagsCommand::fromXML(MapDocument * d, QDomElement e)
266
 
{
267
 
        MapFeature* F;
268
 
        if (!(F = d->getFeature(e.attribute("feature"), false)))
269
 
                return NULL;
270
 
 
271
 
        ClearTagsCommand* a = new ClearTagsCommand(F);
272
 
        a->setId(e.attribute("xml:id"));
273
 
        a->theFeature = F;
274
 
        if (e.hasAttribute("layer"))
275
 
                a->theLayer = d->getLayer(e.attribute("layer"));
276
 
        else
277
 
                a->theLayer = NULL;
278
 
        if (e.hasAttribute("oldlayer"))
279
 
                a->oldLayer = d->getLayer(e.attribute("oldlayer"));
280
 
        else
281
 
                a->oldLayer = NULL;
282
 
 
283
 
        QDomElement c = e.firstChildElement();
284
 
        while(!c.isNull()) {
285
 
                if (c.tagName() == "tag") {
286
 
                        a->Before.push_back(std::make_pair(c.attribute("k"),c.attribute("v")));
287
 
                }
288
 
                c = c.nextSiblingElement();
289
 
        }
290
 
 
291
 
        Command::fromXML(d, e, a);
292
 
 
293
 
        return a;
294
 
}
295
 
 
296
 
/* CLEARTAGCOMMAND */
297
 
 
298
 
ClearTagCommand::ClearTagCommand(MapFeature* F)
299
 
: TagCommand(F, 0), theIdx(0), theK(""), theV("")
300
 
{
301
 
        oldLayer = theFeature->layer();
302
 
}
303
 
 
304
 
ClearTagCommand::ClearTagCommand(MapFeature* F, const QString& k, MapLayer* aLayer)
305
 
: TagCommand(F, aLayer), theIdx(F->findKey(k)), theK(k), theV(F->tagValue(k, ""))
306
 
{
307
 
        description = MainWindow::tr("Clear Tag '%1' on %2").arg(k).arg(F->description());
308
 
        oldLayer = theFeature->layer();
309
 
        redo();
310
 
}
311
 
 
312
 
void ClearTagCommand::undo()
313
 
{
314
 
        Command::undo();
315
 
        if (theFeature->isUploaded()) {
316
 
                theLayer = theLayer->getDocument()->getUploadedLayer();
317
 
                oldLayer = theLayer->getDocument()->getDirtyOrOriginLayer();
318
 
        }
319
 
        theFeature->setTag(theIdx,theK,theV);
320
 
        if (theLayer && oldLayer && (theLayer != oldLayer)) {
321
 
                theLayer->remove(theFeature);
322
 
                oldLayer->add(theFeature);
323
 
                decDirtyLevel(oldLayer);
324
 
        }
325
 
}
326
 
 
327
 
void ClearTagCommand::redo()
328
 
{
329
 
        theFeature->clearTag(theK);
330
 
        if (theLayer && oldLayer && (theLayer != oldLayer)) {
331
 
                oldLayer->remove(theFeature);
332
 
                incDirtyLevel(oldLayer);
333
 
                theLayer->add(theFeature);
334
 
        }
335
 
        Command::redo();
336
 
}
337
 
 
338
 
bool ClearTagCommand::buildDirtyList(DirtyList& theList)
339
 
{
340
 
        if (isUndone)
341
 
                return false;
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);
347
 
                else
348
 
                        return theList.update(theFeature);
349
 
        else
350
 
                return theList.noop(theFeature);
351
 
}
352
 
 
353
 
bool ClearTagCommand::toXML(QDomElement& xParent) const
354
 
{
355
 
        bool OK = true;
356
 
 
357
 
        QDomElement e = xParent.ownerDocument().createElement("ClearTagCommand");
358
 
        xParent.appendChild(e);
359
 
 
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);
365
 
        if (theLayer)
366
 
            e.setAttribute("layer", theLayer->id());
367
 
        if (oldLayer)
368
 
                e.setAttribute("oldlayer", oldLayer->id());
369
 
 
370
 
        Command::toXML(e);
371
 
 
372
 
        return OK;
373
 
}
374
 
 
375
 
ClearTagCommand * ClearTagCommand::fromXML(MapDocument * d, QDomElement e)
376
 
{
377
 
        MapFeature* F;
378
 
        if (!(F = d->getFeature(e.attribute("feature"), false)))
379
 
                return NULL;
380
 
 
381
 
        ClearTagCommand* a = new ClearTagCommand(F);
382
 
        a->setId(e.attribute("xml:id"));
383
 
        a->theFeature = F;
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"));
389
 
        else
390
 
                a->theLayer = NULL;
391
 
        if (e.hasAttribute("oldlayer"))
392
 
                a->oldLayer = d->getLayer(e.attribute("oldlayer"));
393
 
        else
394
 
                a->oldLayer = NULL;
395
 
 
396
 
        a->description = MainWindow::tr("Clear Tag '%1' on %2").arg(a->theK).arg(a->theFeature->description());
397
 
 
398
 
        Command::fromXML(d, e, a);
399
 
 
400
 
        return a;
401
 
}
402
 
 
403