summaryrefslogtreecommitdiffstats
path: root/polygon.cpp
blob: c785e7b9c235e6933ae533d367e603a4a4fa4b5f (plain)
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
#include "polygon.h"

Polygon::Polygon()
    : Line()
{
    m_polygon.append(QPoint(10, 50));
    m_polygon.append(QPoint(30, 70));
    m_polygon.append(QPoint(50, 50));
}

bool Polygon::isHit(const QPoint &clickPoint)
{
    return m_polygon.containsPoint(clickPoint, Qt::OddEvenFill);
}

void Polygon::draw(QPainter *painter)
{
    if (m_selected)
    {
        QPen penTemp(Qt::DotLine);
        penTemp.setColor(Qt::red);
        painter->setPen(penTemp);

        painter->drawEllipse(m_polygon.point(0), 5, 5);
        painter->drawEllipse(m_polygon.point(1), 5, 5);
        painter->drawEllipse(m_polygon.point(2), 5, 5);
    }
    else
    {
        QPen penNormal(Qt::SolidLine);
        penNormal.setColor(Qt::black);
        painter->setPen(penNormal);
    }

    painter->drawPolygon(m_polygon);
}


void Polygon::move(const QPoint &oldPoint, const QPoint &newPoint)
{
    if (m_selected)
    {
        QPoint offset = newPoint - oldPoint;
        QVector3D vecOld(oldPoint);

        if (vecOld.distanceToPoint(QVector3D(m_polygon.point(0))) < 5)
        {
            m_polygon.replace(0, m_polygon.point(0) + offset);
        }
        else if (vecOld.distanceToPoint(QVector3D(m_polygon.point(1))) < 5)
        {
            m_polygon.replace(1, m_polygon.point(1) + offset);
        }
        else if (vecOld.distanceToPoint(QVector3D(m_polygon.point(2))) < 5)
        {
            m_polygon.replace(2, m_polygon.point(2) + offset);
        }
        else
        {
            m_polygon.replace(0, m_polygon.point(0) + offset);
            m_polygon.replace(1, m_polygon.point(1) + offset);
            m_polygon.replace(2, m_polygon.point(2) + offset);
        }
    }
}

bool Polygon::attributesToDom(QDomDocument *doc, QDomElement &e)
{
    if (e.isNull())
    {
        qDebug() << className() << "::attributesToDom Error: NULL element passed.";
        return false;
    }
    else
    {
        QDomElement tmp;

        foreach(QPoint point, m_polygon.toList())
        {
            tmp = doc->createElement("Point");

            tmp.setAttribute("x", point.rx());
            tmp.setAttribute("y", point.ry());

            e.appendChild(tmp);
        }

        return true;
    }
}

bool Polygon::attributesFromDom(const QDomElement &e)
{
    if (e.isNull())
    {
        qDebug() << className() << "::attributesFromDom Error: NULL element passed.";
        return false;
    }
    else
    {
        QDomElement child = e.firstChildElement("Point");

        m_polygon.clear();

        while(!child.isNull())
        {
            QPoint tmp(child.attribute("x", "0").toInt(), child.attribute("y", "0").toInt());

            m_polygon.append(tmp);

            child = child.nextSiblingElement("Point");
        }

        return true;
    }
}

QString Polygon::className()
{
    return "Polygon";
}