-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoarrow.cpp
95 lines (89 loc) · 2.78 KB
/
autoarrow.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
#include <QPainterPath>
#include <QGraphicsScene>
#include "autoarrow.h"
#include "magpoint.h"
#include"itemscene.h"
AutoArrow::AutoArrow(MagPoint* start,MagPoint* end,QList<ArrowPoint*> pathp) : QGraphicsPathItem()
{
startp = start;
endp = end;
this->pathp = pathp;
for(auto p : pathp)
{
p->setParentItem(this);
}
setFlag(QGraphicsItem::ItemIsSelectable, true);
setZValue(-1000);
}
AutoArrow::~AutoArrow()
{
for(auto p : pathp)
{
if(p)
{
if(scene())
scene()->removeItem(p);
delete p;
}
p = nullptr;
}
if(startp)
startp->removeArrow(this);
startp = nullptr;
if(endp)
endp->removeArrow(this);
endp = nullptr;
if(scene())
scene()->removeItem(this);
}
QPainterPath AutoArrow::shape() const
{
QPainterPath path = this->path();
path.addPath(this->path());
path.addPolygon(arrowHead);
return path;
}
void AutoArrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QPainterPath path;
path.moveTo(mapFromScene(startp->scenePos()));
QPointF lastP = startp->scenePos();
QLineF l;
QPointF lp=mapFromScene(startp->scenePos());
for(auto p : pathp)
{
if(lp.x()!=p->pos().x()&&lp.y()!=p->pos().y())
{
//path.lineTo(QPointF(lp.x(),p->pos().y()));
((ItemScene*)scene())->bfs(lp,p->pos(),path);
//((ItemScene*)scene())->Check(lastP,p->scenePos(),path);
}
path.lineTo(p->pos());
lastP = p->scenePos();
lp = p->pos();
}
if(lp.x()!=mapFromScene(endp->scenePos()).x()&&lp.y()!=mapFromScene(endp->scenePos()).y())
{
//((ItemScene*)scene())->Check(lastP,endp->scenePos(),path);
((ItemScene*)scene())->bfs(lp,mapFromScene(endp->scenePos()),path);
//path.lineTo(QPointF(lp.x(),mapFromScene(endp->scenePos()).y()));
}
l = QLineF(mapFromScene(lastP),mapFromScene(endp->scenePos()));
path.lineTo(mapFromScene(endp->scenePos()));
QPen pen;
pen.setColor(myColor); // 设置颜色
pen.setWidth(3);//设置宽度
painter->setPen(pen);
painter->setRenderHint(QPainter::Antialiasing);
painter->drawPath(path);
setPath(path);
double angle = std::atan2(l.dy(), -l.dx());// 计算线条与水平线之间的角度
//下面开始绘制线条
QPointF arrowP1 = l.p2() + QPointF(sin(angle + M_PI / 3) * arrowSize,
cos(angle + M_PI / 3) * arrowSize);
QPointF arrowP2 = l.p2() + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
cos(angle + M_PI - M_PI / 3) * arrowSize);
arrowHead.clear();
arrowHead << l.p2() << arrowP1 << arrowP2;
painter->drawPolygon(arrowHead);
}