NCL Composer  0.1.5
 All Classes Functions Variables Pages
qnstgraphicsreference.cpp
1 #include "qnstgraphicsreference.h"
2 
3 QnstGraphicsReference::QnstGraphicsReference(QnstGraphicsEntity* parent)
4  : QnstGraphicsEdge(parent)
5 {
6  setnstType(Qnst::Reference);
7 }
8 
9 QnstGraphicsReference::~QnstGraphicsReference()
10 {
11 
12 }
13 
14 void QnstGraphicsReference::draw(QPainter* painter)
15 {
16  if (getEntityA() != NULL && getEntityB() != NULL)
17  {
18  painter->setRenderHint(QPainter::Antialiasing, true);
19 
20  QLineF line = QLineF(QPointF(getEntityA()->getLeft() + getEntityA()->getWidth()/2,
21  getEntityA()->getTop() + getEntityA()->getHeight()/2),
22  QPointF(getEntityB()->getLeft() + getEntityB()->getWidth()/2,
23  getEntityB()->getTop() + getEntityB()->getHeight()/2));
24 
25  if (getEntityA()->getncgType() == Qncg::Interface)
26  {
27  line.setP1(getnstGraphicsParent()->mapFromItem(getEntityA()->getnstGraphicsParent(), line.p1()));
28  }
29 
30  if (getEntityB()->getncgType() == Qncg::Interface)
31  {
32  line.setP2(getnstGraphicsParent()->mapFromItem(getEntityB()->getnstGraphicsParent(), line.p2()));
33  }
34 
35  QPointF p1;
36 
37  QPointF pointa = line.p1();
38  QPointF pointb = line.p2();
39 
40  if (pointa.x() <= pointb.x() && pointa.y() <= pointb.y())
41  {
42  painter->setPen(QPen(QBrush(QColor("#000000")), 1, Qt::DashLine));
43 
44  painter->drawLine(4+6,4+6, 4+6+getWidth()-12, 4+6+getHeight()-12);
45 
46  painter->setBrush(QBrush(QColor("#000000")));
47  painter->setPen(Qt::NoPen);
48 
49  p1 = QPointF(4+6+getWidth()-12, 4+6+getHeight()-12);
50 
51  }
52  else if (pointa.x() > pointb.x() && pointa.y() < pointb.y())
53  {
54  painter->setPen(QPen(QBrush(QColor("#000000")), 1, Qt::DashLine));
55 
56  painter->drawLine(4+6+getWidth()-12,4+6, 4+6, 4+6+getHeight()-12);
57 
58  painter->setBrush(QBrush(QColor("#000000")));
59  painter->setPen(Qt::NoPen);
60 
61  p1 = QPointF(4+6, 4+6+getHeight()-12);
62 
63  }
64  else if (pointa.x() < pointb.x() && pointa.y() > pointb.y())
65  {
66  painter->setPen(QPen(QBrush(QColor("#000000")), 1, Qt::DashLine));
67 
68  painter->drawLine(4+6, 4+6+getHeight()-12, 4+6+getWidth()-12, 4+6);
69 
70  painter->setBrush(QBrush(QColor("#000000")));
71  painter->setPen(Qt::NoPen);
72 
73  p1 = QPointF(4+6+getWidth()-12, 4+6);
74 
75  }
76  else if (pointa.x() > pointb.x() && pointa.y() > pointb.y())
77  {
78  painter->setPen(QPen(QBrush(QColor("#000000")), 1, Qt::DashLine));
79 
80  painter->drawLine(4+6+getWidth()-12, 4+6+getHeight()-12, 4+6, 4+6);
81 
82  painter->setBrush(QBrush(QColor("#000000")));
83  painter->setPen(Qt::NoPen);
84 
85  p1 = QPointF(4+6, 4+6);
86  }
87 
88  double angle = ::acos(line.dx() / line.length());
89 
90  if (line.dy() >= 0)
91  {
92  angle = (PI * 2) - angle;
93  }
94 
95  QPointF p2 = p1 - QPointF(sin(angle + PI / 3) * 12, cos(angle + PI / 3) * 12);
96  QPointF p3 = p1 - QPointF(sin(angle + PI - PI / 3) * 12, cos(angle + PI - PI / 3) * 12);
97 
98  QVector<QPointF> polygon;
99 
100  polygon.append(p1);
101  polygon.append(p2);
102  polygon.append(p3);
103 
104  painter->drawPolygon(QPolygonF(polygon));
105  }
106 }
107 
108 void QnstGraphicsReference::delineate(QPainterPath* painter) const
109 {
110  if (getEntityA() != NULL && getEntityB() != NULL)
111  {
112  QLineF line = QLineF(QPointF(getEntityA()->getLeft() + getEntityA()->getWidth()/2,
113  getEntityA()->getTop() + getEntityA()->getHeight()/2),
114  QPointF(getEntityB()->getLeft() + getEntityB()->getWidth()/2,
115  getEntityB()->getTop() + getEntityB()->getHeight()/2));
116 
117  if (getEntityA()->getncgType() == Qncg::Interface)
118  {
119  if(getnstGraphicsParent())
120  line.setP1(getnstGraphicsParent()->mapFromItem(getEntityA()->getnstGraphicsParent(), line.p1()));
121  }
122 
123  if (getEntityB()->getncgType() == Qncg::Interface)
124  {
125  if(getnstGraphicsParent())
126  line.setP2(getnstGraphicsParent()->mapFromItem(getEntityB()->getnstGraphicsParent(), line.p2()));
127  }
128 
129  QPointF p1;
130 
131  QPointF pointa = line.p1();
132  QPointF pointb = line.p2();
133 
134  if (pointa.x() <= pointb.x() && pointa.y() <= pointb.y())
135  {
136  painter->addEllipse(4,4,3,3);
137 
138  p1 = QPointF(4+6+getWidth()-12, 4+6+getHeight()-12);
139 
140  }
141  else if (pointa.x() > pointb.x() && pointa.y() < pointb.y())
142  {
143  painter->addEllipse(4+getWidth()-3,4,3,3);
144 
145  p1 = QPointF(4+6, 4+6+getHeight()-12);
146 
147  }
148  else if (pointa.x() < pointb.x() && pointa.y() > pointb.y())
149  {
150  painter->addEllipse(4, 4+getHeight()-3, 3, 3);
151 
152  p1 = QPointF(4+6+getWidth()-12, 4+6);
153 
154  }
155  else if (pointa.x() > pointb.x() && pointa.y() > pointb.y())
156  {
157  painter->addEllipse(4+getWidth()-3, 4+getHeight()-3, 3, 3);
158 
159  p1 = QPointF(4+6, 4+6);
160  }
161 
162  double angle = ::acos(line.dx() / line.length());
163 
164  if (line.dy() >= 0)
165  {
166  angle = (PI * 2) - angle;
167  }
168 
169  QPointF p2 = p1 - QPointF(sin(angle + PI / 3) * 12, cos(angle + PI / 3) * 12);
170  QPointF p3 = p1 - QPointF(sin(angle + PI - PI / 3) * 12, cos(angle + PI - PI / 3) * 12);
171 
172  QVector<QPointF> polygon;
173 
174  polygon.append(p1);
175  polygon.append(p2);
176  polygon.append(p3);
177 
178  painter->addPolygon(QPolygonF(polygon));
179  }
180 }