summaryrefslogtreecommitdiff
path: root/src/gui/edgeitem.cpp
blob: 04ee7b6007178bd135e8773c988856f39c3acefb (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
#include "tikzit.h"
#include "edgeitem.h"

#include <QPainterPath>
#include <QPen>

EdgeItem::EdgeItem(Edge *edge)
{
    _edge = edge;
    setFlag(QGraphicsItem::ItemIsSelectable);

    QPen pen(Qt::black);
    pen.setWidth(2);
    setPen(pen);
    _cp1Item = new QGraphicsEllipseItem(this);
    _cp1Item->setParentItem(this);
    _cp1Item->setRect(GLOBAL_SCALEF * (-0.05), GLOBAL_SCALEF * (-0.05),
                      GLOBAL_SCALEF * 0.1, GLOBAL_SCALEF * 0.1);
    _cp1Item->setVisible(false);

    _cp2Item = new QGraphicsEllipseItem(this);
    _cp2Item->setParentItem(this);
    _cp2Item->setRect(GLOBAL_SCALEF * (-0.05), GLOBAL_SCALEF * (-0.05),
                      GLOBAL_SCALEF * 0.1, GLOBAL_SCALEF * 0.1);
    _cp2Item->setVisible(false);

    readPos();
}

void EdgeItem::readPos()
{
    //_edge->setAttributesFromData();
    _edge->updateControls();
    QPainterPath path;

    path.moveTo (toScreen(_edge->tail()));
    path.cubicTo(toScreen(_edge->cp1()),
                 toScreen(_edge->cp2()),
                 toScreen(_edge->head()));
    setPath(path);

    _cp1Item->setPos(toScreen(_edge->cp1()));
    _cp2Item->setPos(toScreen(_edge->cp2()));
}

void EdgeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    //QGraphicsPathItem::paint(painter, option, widget);
    painter->setPen(pen());
    painter->setBrush(Qt::NoBrush);
    painter->drawPath(path());

    if (isSelected()) {
        QColor draw;
        QColor draw1;
        QColor fill;

        if (_edge->basicBendMode()) {
            draw = Qt::blue;
            draw1 = QColor(100,100,255,100);
            fill = QColor(200,200,255,50);
        } else {
            draw = Qt::darkGreen;
            draw1 = QColor(0, 150, 0, 50);
            fill = QColor(200,255,200,150);
        }

        painter->setPen(QPen(draw1));

        float r = GLOBAL_SCALEF * _edge->cpDist();
        painter->drawEllipse(toScreen(_edge->source()->point()), r, r);
        painter->drawEllipse(toScreen(_edge->target()->point()), r, r);

        painter->setPen(QPen(draw));
        painter->setBrush(QBrush(fill));

        painter->drawLine(toScreen(_edge->tail()), toScreen(_edge->cp1()));
        painter->drawLine(toScreen(_edge->head()), toScreen(_edge->cp2()));

        //painter->drawEllipse(toScreen(_edge->cp1()), r, r);
        //painter->drawEllipse(toScreen(_edge->cp2()), r, r);

        _cp1Item->setPen(QPen(draw));
        _cp1Item->setBrush(QBrush(fill));
        _cp1Item->setVisible(true);

        _cp2Item->setPen(QPen(draw));
        _cp2Item->setBrush(QBrush(fill));
        _cp2Item->setVisible(true);

        r = GLOBAL_SCALEF * 0.05;
        painter->setPen(QPen(Qt::black));
        painter->setBrush(QBrush(QColor(255,255,255,200)));
        painter->drawEllipse(toScreen(_edge->mid()), r, r);
    } else {
        _cp1Item->setVisible(false);
        _cp2Item->setVisible(false);
    }
}

QRectF EdgeItem::boundingRect() const
{
    float r = GLOBAL_SCALEF * (_edge->cpDist() + 0.2);
    return shape().boundingRect().adjusted(-r,-r,r,r);
}

QPainterPath EdgeItem::shape() const
{
    // get the shape of the edge, and expand a bit to make selection easier
    QPainterPath oldShape = QGraphicsPathItem::shape();
    QPainterPathStroker stroker;
    stroker.setWidth(5);
    stroker.setJoinStyle(Qt::MiterJoin);
    QPainterPath newShape = (stroker.createStroke(oldShape) + oldShape).simplified();
    return newShape;
}

Edge *EdgeItem::edge() const
{
    return _edge;
}

QGraphicsEllipseItem *EdgeItem::cp1Item() const
{
    return _cp1Item;
}

QGraphicsEllipseItem *EdgeItem::cp2Item() const
{
    return _cp2Item;
}