summaryrefslogtreecommitdiff
path: root/src/gui/nodeitem.cpp
blob: b0b7ea6f4a4899392036193486b70a37d17c5bbd (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
136
137
138
139
140
#include "tikzit.h"
#include "nodeitem.h"
#include "tikzscene.h"
#include <cmath>

#include <QPen>
#include <QApplication>
#include <QBrush>
#include <QDebug>
#include <QFont>
#include <QFontMetrics>
#include <QPainterPathStroker>

NodeItem::NodeItem(Node *node)
{
    _node = node;
    setFlag(QGraphicsItem::ItemIsSelectable);
    //setFlag(QGraphicsItem::ItemIsMovable);
    //setFlag(QGraphicsItem::ItemSendsGeometryChanges);
    readPos();
	updateBounds();
}

void NodeItem::readPos()
{
    setPos(toScreen(_node->point()));
}

void NodeItem::writePos()
{
    _node->setPoint(fromScreen(pos()));
}

QRectF NodeItem::labelRect() const {
    QString label = _node->label();
    //QFont f("Courier", 9);
    QFontMetrics fm(Tikzit::LABEL_FONT);

    QRectF rect = fm.boundingRect(label);
    //rect.adjust(-2,-2,2,2);
    rect.moveCenter(QPointF(0,0));
    return rect;
}

void NodeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    if (_node->style()->isNone()) {
        QColor c(180,180,200);
        painter->setPen(QPen(c));
        painter->setBrush(QBrush(c));
        painter->drawEllipse(QPointF(0,0), 1,1);

        QPen pen(QColor(180,180,220));
        QVector<qreal> p;
        p << 2.0 << 2.0;
        pen.setDashPattern(p);
        painter->setPen(pen);
        painter->setBrush(Qt::NoBrush);
        painter->drawPath(shape());
    } else {
        QPen pen(_node->style()->strokeColor());
        pen.setWidth(_node->style()->strokeThickness());
        painter->setPen(pen);
        painter->setBrush(QBrush(_node->style()->fillColor()));
        painter->drawPath(shape());
    }

    if (_node->label() != "") {
        QRectF rect = labelRect();
        QPen pen(QColor(200,0,0,120));
        QVector<qreal> d;
        d << 2.0 << 2.0;
        pen.setDashPattern(d);
        painter->setPen(pen);
        painter->setBrush(QBrush(QColor(255,255,100,120)));
        painter->drawRect(rect);

        painter->setPen(QPen(Qt::black));
        painter->setFont(Tikzit::LABEL_FONT);
        painter->drawText(rect, Qt::AlignCenter, _node->label());
    }

    if (isSelected()) {
        QPainterPath sh = shape();
        QPainterPathStroker stroker;
        stroker.setWidth(4);
        QPainterPath outline = (stroker.createStroke(sh) + sh).simplified();
        painter->setPen(Qt::NoPen);
        painter->setBrush(QBrush(QColor(150,200,255,100)));
        painter->drawPath(outline);
    }

}

QPainterPath NodeItem::shape() const
{
    QPainterPath path;
    path.addEllipse(QPointF(0,0), GLOBAL_SCALEF * 0.2, GLOBAL_SCALEF * 0.2);
    return path;
}

// TODO: nodeitem should sync boundingRect()-relevant stuff (label etc) explicitly,
// to allow prepareGeometryChange()
QRectF NodeItem::boundingRect() const
{
	return _boundingRect;
}

void NodeItem::updateBounds()
{
	prepareGeometryChange();
	QString label = _node->label();
	if (label != "") {
		QFontMetrics fm(Tikzit::LABEL_FONT);
		QRectF labelRect = fm.boundingRect(label);
		labelRect.moveCenter(QPointF(0, 0));
		_boundingRect = labelRect.united(shape().boundingRect()).adjusted(-4, -4, 4, 4);
	} else {
		_boundingRect = shape().boundingRect().adjusted(-4, -4, 4, 4);
	}
}

Node *NodeItem::node() const
{
    return _node;
}

//QVariant NodeItem::itemChange(GraphicsItemChange change, const QVariant &value)
//{
//    if (change == ItemPositionChange) {
//        QPointF newPos = value.toPointF();
//        int gridSize = GLOBAL_SCALE / 8;
//        QPointF gridPos(round(newPos.x()/gridSize)*gridSize, round(newPos.y()/gridSize)*gridSize);
//        _node->setPoint(fromScreen(gridPos));
//
//        return gridPos;
//    } else {
//        return QGraphicsItem::itemChange(change, value);
//    }
//}