summaryrefslogtreecommitdiffstats
path: root/line.cpp
blob: 2d198742b09021290f83809a23d78ea718e0fc6b (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "line.h"

Line::Line()
    : QLine()
{
    m_selected = true;
}

bool Line::isHit(const QPoint &clickPoint)
{
    QVector3D a(p2() - p1());
    QVector3D r1(p1());

    QVector3D rq(clickPoint);

    float dist = rq.distanceToLine(r1, a.normalized());

    return  dist < 5;
}

void Line::setSelected(bool selected)
{
    m_selected = selected;
}

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

        painter->drawEllipse(p1(), 5, 5);
        painter->drawEllipse(p2(), 5, 5);
    }
    else
    {
        QPen penNormal(Qt::SolidLine);
        penNormal.setColor(Qt::black);
        painter->setPen(penNormal);
    }

    painter->drawLine(p1(), p2());

}

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

        if (vecOld.distanceToPoint(QVector3D(p1())) < 5)
        {
            setP1(p1() + offset);
        }
        else if (vecOld.distanceToPoint(QVector3D(p2())) < 5)
        {
            setP2(p2() + offset);
        }
        else
        {
            setP1(p1() + offset);
            setP2(p2() + offset);
        }
    }
}

QDomElement Line::toDomElement(QDomDocument *doc)
{
    QDomElement e = doc->createElement(className());
    attributesToDom(doc, e);
    return e;
}

bool Line::attributesToDom(QDomDocument *doc, QDomElement &e)
{
    if (e.isNull())
    {
        qDebug() << className() << "::attributesToDom Error: NULL element passed.";
        return false;
    }
    else
    {
        e.setAttribute("x1", p1().rx());
        e.setAttribute("y1", p1().ry());
        e.setAttribute("x2", p2().rx());
        e.setAttribute("y2", p2().ry());

        return true;
    }
}

bool Line::fromDomElement(const QDomElement &e)
{
    if (e.isNull())
    {
        qDebug() << className() << "::fromQDomElement Error: NULL element passed.";
        return false;
    }

    if (className() != e.tagName())
    {
        qDebug() << className() << "::fromQDomElement Error: Invalid element type: " <<
                 e.tagName() << " instead of " << className();
        return false;
    }

    return attributesFromDom(e);
}

bool Line::attributesFromDom(const QDomElement &e)
{
    if (e.isNull())
    {
        qDebug() << className() << "::attributesFromDom Error: NULL element passed.";
        return false;
    }
    else
    {
        QPoint newP1(e.attribute("x1", "0").toInt(), e.attribute("y1", "0").toInt());
        QPoint newP2(e.attribute("x2", "0").toInt(), e.attribute("y2", "0").toInt());

        setPoints(newP1, newP2);

        return true;
    }
}

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