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

PathItem::PathItem(Path *path)
{
    _path = path;
    readPos();
}

void PathItem::readPos()
{
    QPainterPath painterPath;

    foreach (Edge *e, _path->edges()) {
        e->updateControls();

        if (e == _path->edges().first())
            painterPath.moveTo (toScreen(e->tail()));

        if (e->bend() != 0 || !e->basicBendMode()) {
            painterPath.cubicTo(toScreen(e->cp1()),
                toScreen(e->cp2()),
                toScreen(e->head()));
        } else {
            painterPath.lineTo(toScreen(e->head()));
        }
    }

    setPainterPath(painterPath);
}

void PathItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    Style *st = _path->edges().first()->style();
    QPen pen = st->pen();
    QBrush brush = st->brush();
    QColor c = brush.color();
    brush.setColor(QColor(c.red(),c.green(),c.blue(),200));
    painter->setPen(pen);
    painter->setBrush(brush);
    painter->drawPath(painterPath());
}

Path *PathItem::path() const
{
    return _path;
}

QPainterPath PathItem::painterPath() const
{
    return _painterPath;
}

void PathItem::setPainterPath(const QPainterPath &painterPath)
{
    prepareGeometryChange();

    _painterPath = painterPath;
    float r = GLOBAL_SCALEF * 0.1;
    _boundingRect = _painterPath.boundingRect().adjusted(-r,-r,r,r);

    update();
}

QRectF PathItem::boundingRect() const
{
    return _boundingRect;
}