NCL Composer  0.1.5
 All Classes Functions Variables Pages
qnstviewlink.cpp
1 #include "qnstviewlink.h"
2 
3 QnstViewLink::QnstViewLink(QnstGraphicsEntity* parent)
4  : QnstGraphicsEntity(parent)
5 {
6  setSelectable(false);
7  setResizable(false);
8  setMoveable(false);
9 }
10 
11 QnstViewLink::~QnstViewLink()
12 {
13 
14 }
15 
16 QLineF QnstViewLink::getLine() const
17 {
18  return line;
19 }
20 
21 void QnstViewLink::setLine(QLineF line)
22 {
23  this->line = line;
24 
25  adjust();
26 }
27 
28 void QnstViewLink::adjust(bool avoidCollision)
29 {
30  QPointF pointa = line.p1();
31  QPointF pointb = line.p2();
32 
33  if (pointa.x() <= pointb.x() && pointa.y() <= pointb.y())
34  {
35  setTop(pointa.y()-6);
36  setLeft(pointa.x()-6);
37  setWidth((pointb.x()-6)-(pointa.x()-6) + 12);
38  setHeight((pointb.y()-6)-(pointa.y()-6) + 12);
39  }
40  else if (pointa.x() > pointb.x() && pointa.y() < pointb.y())
41  {
42  setTop(pointa.y()-6);
43  setLeft(pointb.x()-6);
44  setWidth((pointa.x()-6)-(pointb.x()-6) + 12);
45  setHeight((pointb.y()-6)-(pointa.y()-6) + 12);
46  }
47  else if (pointa.x() < pointb.x() && pointa.y() > pointb.y())
48  {
49  setTop(pointb.y()-6);
50  setLeft((pointa.x()-6));
51  setWidth((pointb.x()-6)-(pointa.x()-6) + 12);
52  setHeight((pointa.y()-6)-(pointb.y()-6) + 12);
53  }
54  else if (pointa.x() > pointb.x() && pointa.y() > pointb.y())
55  {
56  setTop(pointb.y()-6);
57  setLeft(pointb.x()-6);
58  setWidth((pointa.x()-6)-(pointb.x()-6) + 12);
59  setHeight((pointa.y()-6)-(pointb.y()-6) + 12);
60  }
61 
62  if (scene() != NULL)
63  scene()->update();
64 }
65 
66 void QnstViewLink::draw(QPainter* painter)
67 {
68  painter->setRenderHint(QPainter::Antialiasing, true);
69 
70  QPointF p1;
71 
72  QPointF pointa = line.p1();
73  QPointF pointb = line.p2();
74 
75  if (pointa.x() <= pointb.x() && pointa.y() <= pointb.y())
76  {
77  painter->setPen(QPen(QBrush(QColor("#000000")), 1, Qt::DashLine));
78 
79  painter->drawLine(4+6,4+6, 4+6+getWidth()-12-2, 4+6+getHeight()-12-2);
80 
81  painter->setBrush(QBrush(QColor("#000000")));
82  painter->setPen(Qt::NoPen);
83 
84  painter->drawEllipse(4,4,12,12);
85 
86  p1 = QPointF(4+6+getWidth()-12, 4+6+getHeight()-12);
87 
88  }
89  else if (pointa.x() > pointb.x() && pointa.y() < pointb.y())
90  {
91  painter->setPen(QPen(QBrush(QColor("#000000")), 1, Qt::DashLine));
92 
93  painter->drawLine(4+6+getWidth()-12,4+6, 4+6, 4+6+getHeight()-12);
94 
95  painter->setBrush(QBrush(QColor("#000000")));
96  painter->setPen(Qt::NoPen);
97 
98  painter->drawEllipse(4+getWidth()-12,4,12,12);
99 
100  p1 = QPointF(4+6, 4+6+getHeight()-12);
101 
102  }
103  else if (pointa.x() < pointb.x() && pointa.y() > pointb.y())
104  {
105  painter->setPen(QPen(QBrush(QColor("#000000")), 1, Qt::DashLine));
106 
107  painter->drawLine(4+6, 4+6+getHeight()-12, 4+6+getWidth()-12, 4+6);
108 
109  painter->setBrush(QBrush(QColor("#000000")));
110  painter->setPen(Qt::NoPen);
111 
112  painter->drawEllipse(4, 4+getHeight()-12, 12, 12);
113 
114  p1 = QPointF(4+6+getWidth()-12, 4+6);
115 
116  }
117  else if (pointa.x() > pointb.x() && pointa.y() > pointb.y())
118  {
119  painter->setPen(QPen(QBrush(QColor("#000000")), 1, Qt::DashLine));
120 
121  painter->drawLine(4+6+getWidth()-12, 4+6+getHeight()-12, 4+6, 4+6);
122 
123  painter->setBrush(QBrush(QColor("#000000")));
124  painter->setPen(Qt::NoPen);
125 
126  painter->drawEllipse(4+getWidth()-12, 4+getHeight()-12, 12, 12);
127 
128  p1 = QPointF(4+6, 4+6);
129  }
130 
131  double angle = ::acos(line.dx() / line.length());
132 
133  if (line.dy() >= 0)
134  {
135  angle = (PI * 2) - angle;
136  }
137 
138  QPointF p2 = p1 - QPointF(sin(angle + PI / 3) * 12, cos(angle + PI / 3) * 12);
139  QPointF p3 = p1 - QPointF(sin(angle + PI - PI / 3) * 12, cos(angle + PI - PI / 3) * 12);
140 
141  QVector<QPointF> polygon;
142 
143  polygon.append(p1);
144  polygon.append(p2);
145  polygon.append(p3);
146 
147  painter->drawPolygon(QPolygonF(polygon));
148 }
149 
150 void QnstViewLink::delineate(QPainterPath* painter) const
151 {
152  // nothing to do
153 }
154 
155 void QnstViewLink::move(QGraphicsSceneMouseEvent* event)
156 {
157  // nothing to do
158 }
159 
160 void QnstViewLink::resize(QGraphicsSceneMouseEvent* event)
161 {
162  // nothing to do
163 }