File: object_imp_factory.cc

package info (click to toggle)
kig 4%3A25.08.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,716 kB
  • sloc: cpp: 41,465; xml: 851; python: 486; perl: 23; sh: 17; makefile: 3
file content (442 lines) | stat: -rw-r--r-- 17,701 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// SPDX-FileCopyrightText: 2002 Dominique Devriese <devriese@kde.org>

// SPDX-License-Identifier: GPL-2.0-or-later

#include "object_imp_factory.h"

#include "bogus_imp.h"
#include "circle_imp.h"
#include "conic_imp.h"
#include "cubic_imp.h"
#include "line_imp.h"
#include "locus_imp.h"
#include "object_imp.h"
#include "other_imp.h"
#include "point_imp.h"
#include "text_imp.h"

#include "../misc/coordinate.h"

#include <qdom.h>

const ObjectImpFactory *ObjectImpFactory::instance()
{
    static const ObjectImpFactory t;
    return &t;
}

ObjectImpFactory::ObjectImpFactory()
{
}

ObjectImpFactory::~ObjectImpFactory()
{
}

static void addXYElements(const Coordinate &c, QDomElement &parent, QDomDocument &doc)
{
    QDomElement xe = doc.createElement(QStringLiteral("x"));
    xe.appendChild(doc.createTextNode(QString::number(c.x)));
    parent.appendChild(xe);
    QDomElement ye = doc.createElement(QStringLiteral("y"));
    ye.appendChild(doc.createTextNode(QString::number(c.y)));
    parent.appendChild(ye);
}

static void addDoubleElement(const char *name, double d, QDomElement &parent, QDomDocument &doc)
{
    QDomElement e = doc.createElement(name);
    e.appendChild(doc.createTextNode(QString::number(d)));
    parent.appendChild(e);
}

static void addCoordinateElement(const char *name, const Coordinate &d, QDomElement &p, QDomDocument &doc)
{
    QDomElement e = doc.createElement(name);
    addXYElements(d, e, doc);
    p.appendChild(e);
}

QString ObjectImpFactory::serialize(const ObjectImp &d, QDomElement &parent, QDomDocument &doc) const
{
    if (d.inherits(IntImp::stype())) {
        parent.appendChild(doc.createTextNode(QString::number(static_cast<const IntImp &>(d).data())));
        return QStringLiteral("int");
    } else if (d.inherits(DoubleImp::stype())) {
        parent.appendChild(doc.createTextNode(QString::number(static_cast<const DoubleImp &>(d).data())));
        return QStringLiteral("double");
    } else if (d.inherits(StringImp::stype())) {
        parent.appendChild(doc.createTextNode(static_cast<const StringImp &>(d).data()));
        return QStringLiteral("string");
    } else if (d.inherits(TestResultImp::stype())) {
        assert(false);
        parent.appendChild(doc.createTextNode(static_cast<const TestResultImp &>(d).data()));
        return QStringLiteral("testresult");
    } else if (d.inherits(HierarchyImp::stype())) {
        static_cast<const HierarchyImp &>(d).data().serialize(parent, doc);
        return QStringLiteral("hierarchy");
    } else if (d.inherits(TransformationImp::stype())) {
        const Transformation &trans = static_cast<const TransformationImp &>(d).data();

        QDomElement matrixe = doc.createElement(QStringLiteral("matrix"));
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                QDomElement elel = doc.createElement(QStringLiteral("element"));
                elel.setAttribute(QStringLiteral("row"), QString::number(i));
                elel.setAttribute(QStringLiteral("column"), QString::number(j));
                elel.appendChild(doc.createTextNode(QString::number(trans.data(i, j))));
                matrixe.appendChild(elel);
            };
        }
        parent.appendChild(matrixe);

        QDomElement homothetye = doc.createElement(QStringLiteral("homothetic"));
        const char *ishomothety = trans.isHomothetic() ? "true" : "false";
        homothetye.appendChild(doc.createTextNode(ishomothety));
        parent.appendChild(homothetye);

        return QStringLiteral("transformation");
    } else if (d.inherits(AbstractLineImp::stype())) {
        LineData l = static_cast<const AbstractLineImp &>(d).data();
        addCoordinateElement("a", l.a, parent, doc);
        addCoordinateElement("b", l.b, parent, doc);
        if (d.inherits(SegmentImp::stype()))
            return QStringLiteral("segment");
        else if (d.inherits(RayImp::stype()))
            return QStringLiteral("ray");
        else
            return QStringLiteral("line");
    } else if (d.inherits(PointImp::stype())) {
        addXYElements(static_cast<const PointImp &>(d).coordinate(), parent, doc);
        return QStringLiteral("point");
    } else if (d.inherits(TextImp::stype())) {
        QString text = static_cast<const TextImp &>(d).text();
        parent.appendChild(doc.createTextNode(text));
        return QStringLiteral("text");
    } else if (d.inherits(AngleImp::stype())) {
        addDoubleElement("size", static_cast<const AngleImp &>(d).size(), parent, doc);
        return QStringLiteral("angle");
    } else if (d.inherits(ArcImp::stype())) {
        const ArcImp &a = static_cast<const ArcImp &>(d);
        addCoordinateElement("center", a.center(), parent, doc);
        addDoubleElement("radius", a.radius(), parent, doc);
        addDoubleElement("startangle", a.startAngle(), parent, doc);
        addDoubleElement("angle", a.angle(), parent, doc);
        return QStringLiteral("arc");
    } else if (d.inherits(VectorImp::stype())) {
        Coordinate dir = static_cast<const VectorImp &>(d).dir();
        addXYElements(dir, parent, doc);
        return QStringLiteral("vector");
    } else if (d.inherits(LocusImp::stype())) {
        const LocusImp &locus = static_cast<const LocusImp &>(d);

        // serialize the curve..
        QDomElement curve = doc.createElement(QStringLiteral("curve"));
        const CurveImp &curveimp = *locus.curve();
        QString type = serialize(curveimp, curve, doc);
        curve.setAttribute(QStringLiteral("type"), type);
        parent.appendChild(curve);

        // serialize the hierarchy..
        QDomElement hier = doc.createElement(QStringLiteral("calculation"));
        locus.hierarchy().serialize(hier, doc);
        parent.appendChild(hier);

        return QStringLiteral("locus");
    } else if (d.inherits(CircleImp::stype())) {
        const CircleImp &c = static_cast<const CircleImp &>(d);
        addCoordinateElement("center", c.center(), parent, doc);
        addDoubleElement("radius", c.radius(), parent, doc);
        return QStringLiteral("circle");
    } else if (d.inherits(ConicImp::stype())) {
        const ConicPolarData data = static_cast<const ConicImp &>(d).polarData();
        addCoordinateElement("focus1", data.focus1, parent, doc);
        addDoubleElement("pdimen", data.pdimen, parent, doc);
        addDoubleElement("ecostheta0", data.ecostheta0, parent, doc);
        addDoubleElement("esintheta0", data.esintheta0, parent, doc);
        return QStringLiteral("conic");
    } else if (d.inherits(CubicImp::stype())) {
        const CubicCartesianData data = static_cast<const CubicImp &>(d).data();
        QDomElement coeffs = doc.createElement(QStringLiteral("coefficients"));
        addDoubleElement("a000", data.coeffs[0], coeffs, doc);
        addDoubleElement("a001", data.coeffs[1], coeffs, doc);
        addDoubleElement("a002", data.coeffs[2], coeffs, doc);
        addDoubleElement("a011", data.coeffs[3], coeffs, doc);
        addDoubleElement("a012", data.coeffs[4], coeffs, doc);
        addDoubleElement("a022", data.coeffs[5], coeffs, doc);
        addDoubleElement("a111", data.coeffs[6], coeffs, doc);
        addDoubleElement("a112", data.coeffs[7], coeffs, doc);
        addDoubleElement("a122", data.coeffs[8], coeffs, doc);
        addDoubleElement("a222", data.coeffs[9], coeffs, doc);
        parent.appendChild(coeffs);
        return QStringLiteral("cubic");
    }
    assert(false);
    return QString();
}

static Coordinate readXYElements(const QDomElement &e, bool &ok)
{
    double x, y;
    ok = true;
    QDomElement xe = e.firstChild().toElement();
    if (xe.isNull() || xe.tagName() != QLatin1String("x")) {
        ok = false;
        return Coordinate();
    } else
        x = xe.text().toDouble(&ok);

    QDomElement ye = xe.nextSibling().toElement();
    if (ye.isNull() || ye.tagName() != QLatin1String("y")) {
        ok = false;
        return Coordinate();
    } else
        y = ye.text().toDouble(&ok);

    return Coordinate(x, y);
}

static Coordinate readCoordinateElement(const QDomNode &n, bool &ok, const char *tagname)
{
    QDomElement e = n.toElement();
    if (e.isNull() || e.tagName() != tagname) {
        ok = false;
        Coordinate ret;
        return ret;
    }
    return readXYElements(e, ok);
}

static double readDoubleElement(const QDomNode &n, bool &ok, const char *tagname)
{
    QDomElement e = n.toElement();
    if (e.isNull() || e.tagName() != tagname) {
        ok = false;
        return 0.;
    };
    return e.text().toDouble(&ok);
}

ObjectImp *ObjectImpFactory::deserialize(const QString &type, const QDomElement &parent, QString &error) const
{
#define KIG_GENERIC_PARSE_ERROR                                                                                                                                \
    {                                                                                                                                                          \
        error = i18n("An error was encountered at line %1 in file %2.", __LINE__, __FILE__);                                                                   \
        return nullptr;                                                                                                                                        \
    }

    bool ok = true;
    if (type == QLatin1String("int")) {
        int ret = parent.text().toInt(&ok);
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;
        return new IntImp(ret);
    } else if (type == QLatin1String("double")) {
        double ret = parent.text().toDouble(&ok);
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;
        return new DoubleImp(ret);
    } else if (type == QLatin1String("string")) {
        return new StringImp(parent.text());
    } else if (type == QLatin1String("testresult")) {
        // should never get here, at least for new save files!
        return new TestResultImp(true, parent.text());
    } else if (type == QLatin1String("hierarchy")) {
        ObjectHierarchy *hier = ObjectHierarchy::buildSafeObjectHierarchy(parent, error);
        if (!hier)
            return nullptr;
        HierarchyImp *imp = new HierarchyImp(*hier);
        delete hier;
        return imp;
    } else if (type == QLatin1String("transformation")) {
        double data[3][3];
        bool homothetic = false;
        for (QDomElement childe = parent.firstChild().toElement(); !childe.isNull(); childe = childe.nextSibling().toElement()) {
            if (childe.tagName() == QLatin1String("matrix")) {
                for (QDomElement elel = childe.firstChild().toElement(); !elel.isNull(); elel = elel.nextSibling().toElement()) {
                    if (elel.tagName() != QLatin1String("element"))
                        KIG_GENERIC_PARSE_ERROR;
                    bool ok = true;
                    int row = elel.attribute(QStringLiteral("row")).toInt(&ok);
                    if (!ok)
                        KIG_GENERIC_PARSE_ERROR;
                    int column = elel.attribute(QStringLiteral("column")).toInt(&ok);
                    if (!ok)
                        KIG_GENERIC_PARSE_ERROR;
                    data[row][column] = elel.text().toDouble(&ok);
                    if (!ok)
                        KIG_GENERIC_PARSE_ERROR;
                };
            } else if (childe.tagName() == QLatin1String("homothetic")) {
                homothetic = childe.text() == QLatin1String("true");
            } else
                continue;
        };
        Transformation trans(data, homothetic);
        return new TransformationImp(trans);
    } else if (type == QLatin1String("point")) {
        Coordinate ret = readXYElements(parent, ok);
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;
        return new PointImp(ret);
    } else if (type == QLatin1String("line") || type == QLatin1String("segment") || type == QLatin1String("ray")) {
        QDomNode n = parent.firstChild();
        Coordinate a = readCoordinateElement(n, ok, "a");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;
        n = n.nextSibling();
        Coordinate b = readCoordinateElement(n, ok, "b");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;
        if (type == QLatin1String("line"))
            return new LineImp(a, b);
        else if (type == QLatin1String("segment"))
            return new SegmentImp(a, b);
        else
            return new RayImp(a, b);
    } else if (type == QLatin1String("angle")) {
        double size = readDoubleElement(parent.firstChild(), ok, "size");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;

        // TODO figure out how to know if this should be marked as a right angle
        return new AngleImp(Coordinate(), 0, size, false);
    } else if (type == QLatin1String("arc")) {
        QDomNode n = parent.firstChild();
        Coordinate center = readCoordinateElement(n, ok, "center");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;
        n = n.nextSibling();
        double radius = readDoubleElement(n, ok, "radius");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;
        n = n.nextSibling();
        double startangle = readDoubleElement(n, ok, "startangle");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;
        n = n.nextSibling();
        double angle = readDoubleElement(n, ok, "angle");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;
        return new ArcImp(center, radius, startangle, angle);
    } else if (type == QLatin1String("vector")) {
        Coordinate dir = readXYElements(parent, ok);
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;
        return new VectorImp(Coordinate(), dir);
    } else if (type == QLatin1String("locus")) {
        QDomElement curvee = parent.firstChild().toElement();
        if (curvee.isNull() || curvee.tagName() != QLatin1String("curve"))
            KIG_GENERIC_PARSE_ERROR;
        QString type = curvee.attribute(QStringLiteral("type"));
        ObjectImp *oi = deserialize(type, curvee, error);
        if (!oi || !oi->inherits(CurveImp::stype()))
            KIG_GENERIC_PARSE_ERROR;
        // CurveImp* curvei = static_cast<CurveImp*>( oi );

        QDomElement hiere = curvee.nextSibling().toElement();
        if (hiere.isNull() || hiere.tagName() != QLatin1String("calculation"))
            KIG_GENERIC_PARSE_ERROR;
        assert(false); // TODO
        //    return new LocusImp( curvei, hier );
    } else if (type == QLatin1String("circle")) {
        QDomNode n = parent.firstChild();
        Coordinate center = readCoordinateElement(n, ok, "center");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;

        n = n.nextSibling();
        double radius = readDoubleElement(n, ok, "radius");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;

        return new CircleImp(center, radius);
    } else if (type == QLatin1String("conic")) {
        QDomNode n = parent.firstChild();
        Coordinate focus1 = readCoordinateElement(n, ok, "focus1");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;

        n = n.nextSibling();
        double pdimen = readDoubleElement(n, ok, "pdimen");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;

        n = n.nextSibling();
        double ecostheta0 = readDoubleElement(n, ok, "ecostheta0");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;

        n = n.nextSibling();
        double esintheta0 = readDoubleElement(n, ok, "esintheta0");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;

        return new ConicImpPolar(ConicPolarData(focus1, pdimen, ecostheta0, esintheta0));
    } else if (type == QLatin1String("cubic")) {
        QDomElement coeffse = parent.firstChild().toElement();
        if (coeffse.isNull() || coeffse.tagName() != QLatin1String("coefficients"))
            KIG_GENERIC_PARSE_ERROR;

        QDomNode n = coeffse.firstChild();
        double a000 = readDoubleElement(n, ok, "a000");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;

        n = n.nextSibling();
        double a001 = readDoubleElement(n, ok, "a001");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;

        n = n.nextSibling();
        double a002 = readDoubleElement(n, ok, "a002");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;

        n = n.nextSibling();
        double a011 = readDoubleElement(n, ok, "a011");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;

        n = n.nextSibling();
        double a012 = readDoubleElement(n, ok, "a012");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;

        n = n.nextSibling();
        double a022 = readDoubleElement(n, ok, "a022");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;

        n = n.nextSibling();
        double a111 = readDoubleElement(n, ok, "a111");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;

        n = n.nextSibling();
        double a112 = readDoubleElement(n, ok, "a112");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;

        n = n.nextSibling();
        double a122 = readDoubleElement(n, ok, "a112");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;

        n = n.nextSibling();
        double a222 = readDoubleElement(n, ok, "a222");
        if (!ok)
            KIG_GENERIC_PARSE_ERROR;

        return new CubicImp(CubicCartesianData(a000, a001, a002, a011, a012, a022, a111, a112, a122, a222));
    }

    error = i18n(
        "This Kig file uses an object of type \"%1\", "
        "which this Kig version does not support."
        "Perhaps you have compiled Kig without support "
        "for this object type,"
        "or perhaps you are using an older Kig version.",
        type);
    return nullptr;
}