-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQGVEdge.cpp
164 lines (135 loc) · 4.41 KB
/
QGVEdge.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/***************************************************************
QGVCore
Copyright (c) 2014, Bergont Nicolas, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
***************************************************************/
#include <QGVEdge.h>
#include <QGVCore.h>
#include <QGVScene.h>
#include <QGVGraphPrivate.h>
#include <QGVEdgePrivate.h>
#include <QDebug>
#include <QPainter>
QGVEdge::QGVEdge(QGVEdgePrivate *edge, QGVScene *scene) : _edge(edge), _scene(scene)
{
setFlag(QGraphicsItem::ItemIsSelectable, true);
}
QGVEdge::~QGVEdge()
{
_scene->removeItem(this);
delete _edge;
}
QString QGVEdge::label() const
{
return getAttribute("xlabel");
}
QRectF QGVEdge::boundingRect() const
{
return _path.boundingRect() | _head_arrow.boundingRect() | _tail_arrow.boundingRect() | _label_rect;
}
QPainterPath QGVEdge::shape() const
{
QPainterPathStroker ps;
ps.setCapStyle(_pen.capStyle());
ps.setWidth(_pen.widthF() + 10);
ps.setJoinStyle(_pen.joinStyle());
ps.setMiterLimit(_pen.miterLimit());
return ps.createStroke(_path);
}
void QGVEdge::setLabel(const QString &label)
{
setAttribute("xlabel", label);
}
void QGVEdge::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
painter->save();
if(isSelected())
{
QPen tpen(_pen);
tpen.setColor(_pen.color().darker(120));
tpen.setStyle(Qt::DotLine);
painter->setPen(tpen);
}
else
painter->setPen(_pen);
painter->drawPath(_path);
/*
QRectF pp = _path.controlPointRect();
if(pp.width() < pp.height())
{
painter->save();
painter->translate(_label_rect.topLeft());
painter->rotate(90);
painter->drawText(QRectF(QPointF(0, -_label_rect.width()), _label_rect.size()), Qt::AlignCenter, _label);
painter->restore();
}
else
*/
painter->drawText(_label_rect, Qt::AlignCenter, _label);
painter->setBrush(QBrush(_pen.color(), Qt::SolidPattern));
painter->drawPolygon(_head_arrow);
painter->drawPolygon(_tail_arrow);
painter->restore();
}
void QGVEdge::setAttribute(const QString &name, const QString &value)
{
agsafeset(_edge->edge(), name.toLocal8Bit().data(), value.toLocal8Bit().data(), "");
}
QString QGVEdge::getAttribute(const QString &name) const
{
char* value = agget(_edge->edge(), name.toLocal8Bit().data());
if(value)
return value;
return QString();
}
void QGVEdge::updateLayout()
{
prepareGeometryChange();
qreal gheight = QGVCore::graphHeight(_scene->_graph->graph());
const splines* spl = ED_spl(_edge->edge());
_path = QGVCore::toPath(spl, gheight);
//Edge arrows
if((spl->list != 0) && (spl->list->size%3 == 1))
{
if(spl->list->sflag)
{
_tail_arrow = toArrow(QLineF(QGVCore::toPoint(spl->list->list[0], gheight), QGVCore::toPoint(spl->list->sp, gheight)));
}
if(spl->list->eflag)
{
_head_arrow = toArrow(QLineF(QGVCore::toPoint(spl->list->list[spl->list->size-1], gheight), QGVCore::toPoint(spl->list->ep, gheight)));
}
}
_pen.setWidth(1);
_pen.setColor(QGVCore::toColor(getAttribute("color")));
_pen.setStyle(QGVCore::toPenStyle(getAttribute("style")));
//Edge label
textlabel_t *xlabel = ED_xlabel(_edge->edge());
if(xlabel)
{
_label = xlabel->text;
_label_rect.setSize(QSize(xlabel->dimen.x, xlabel->dimen.y));
_label_rect.moveCenter(QGVCore::toPoint(xlabel->pos, QGVCore::graphHeight(_scene->_graph->graph())));
}
setToolTip(getAttribute("tooltip"));
}
QPolygonF QGVEdge::toArrow(const QLineF &line) const
{
QLineF n = line.normalVector();
QPointF o(n.dx() / 3.0, n.dy() / 3.0);
//Only support normal arrow type
QPolygonF polygon;
polygon.append(line.p1() + o);
polygon.append(line.p2());
polygon.append(line.p1() - o);
return polygon;
}