NCL Composer  0.1.5
 All Classes Functions Variables Pages
qnstgraphicsedge.cpp
1 #include "qnstgraphicsedge.h"
2 
3 QnstGraphicsEdge::QnstGraphicsEdge(QnstGraphicsEntity* parent)
4  : QnstGraphicsEntity(parent)
5 {
6  setncgType(Qncg::Edge);
7  setnstType(Qnst::Edge);
8 
9  setSelectable(true);
10  setResizable(false);
11  setMoveable(false);
12 
13  entitya = NULL;
14  entityb = NULL;
15 
16  entityaenabled = true;
17  entitybenabled = true;
18 
19  angle = 0;
20 
21  invalid = false;
22 }
23 
24 QnstGraphicsEdge::~QnstGraphicsEdge()
25 {
26 
27 }
28 
29 bool QnstGraphicsEdge::isInvalid()
30 {
31  return invalid;
32 }
33 
34 void QnstGraphicsEdge::setInvalid(bool invalid)
35 {
36  this->invalid = invalid;
37 }
38 
39 QnstGraphicsEntity* QnstGraphicsEdge::getEntityA() const
40 {
41  return entitya;
42 }
43 
44 void QnstGraphicsEdge::setEntityA(QnstGraphicsEntity* entity)
45 {
46  this->entitya = entity;
47 }
48 
49 QnstGraphicsEntity* QnstGraphicsEdge::getEntityB() const
50 {
51  return entityb;
52 }
53 
54 void QnstGraphicsEdge::setEntityB(QnstGraphicsEntity* entity)
55 {
56  this->entityb = entity;
57 }
58 
59 bool QnstGraphicsEdge::isEntityAEnabled() const
60 {
61  return entityaenabled;
62 }
63 
64 void QnstGraphicsEdge::setEntityAEnabled(bool enable)
65 {
66  this->entityaenabled = enable;
67 }
68 
69 bool QnstGraphicsEdge::isEntityBEnabled() const
70 {
71  return entitybenabled;
72 }
73 
74 void QnstGraphicsEdge::setEntityBEnabled(bool enable)
75 {
76  this->entitybenabled = enable;
77 }
78 
79 qreal QnstGraphicsEdge::getAngle()
80 {
81  return angle;
82 }
83 
84 void QnstGraphicsEdge::setAngle(qreal angle)
85 {
86  this->angle = angle;
87 }
88 
89 qreal QnstGraphicsEdge::getAdjAngle()
90 {
91  return adjustedangle;
92 }
93 
94 void QnstGraphicsEdge::setAdjAngle(qreal adjangle)
95 {
96  this->adjustedangle = adjangle;
97 }
98 
99 void QnstGraphicsEdge::adjust(bool avoidCollision)
100 {
101  if (entitya != NULL && entityb != NULL)
102  {
103  QLineF line = QLineF(QPointF(entitya->getLeft() + entitya->getWidth()/2,
104  entitya->getTop() + entitya->getHeight()/2),
105  QPointF(entityb->getLeft() + entityb->getWidth()/2,
106  entityb->getTop() + entityb->getHeight()/2));
107 
108  if (getEntityA()->getncgType() == Qncg::Interface)
109  {
110  if(getnstGraphicsParent())
111  line.setP1(getnstGraphicsParent()->mapFromItem(getEntityA()->getnstGraphicsParent(), line.p1()));
112  }
113 
114  if (getEntityB()->getncgType() == Qncg::Interface)
115  {
116  if(getnstGraphicsParent())
117  line.setP2(getnstGraphicsParent()->mapFromItem(getEntityB()->getnstGraphicsParent(), line.p2()));
118  }
119 
120  QPointF pointa = line.p1();
121  QPointF pointb = line.p2();
122 
123  aux_adjust(pointa, pointb);
124 
125  entitya->setSelectable(false);
126  entityb->setSelectable(false);
127 
128  qreal index;
129 
130  if (pointa != pointb){
131  index = 1.0;
132 
133  int n = 0;
134 
135  while(entityb->collidesWithItem(this))
136  {
137  index -= 0.01;
138 
139  if (angle == 0)
140  pointb = line.pointAt(index);
141  else
142  pointb = arcPointAt(line , index);
143 
144  aux_adjust(pointa, pointb);
145 
146  if (++n > 100) // avoiding infinity loop
147  {
148  break;
149  }
150  }
151 
152  index = 0;
153 
154  n = 0;
155 
156  while(entitya->collidesWithItem(this))
157  {
158  index += 0.01;
159 
160  if (angle == 0)
161  pointa = line.pointAt(index);
162  else
163  pointa = arcPointAt(line , index);
164 
165  aux_adjust(pointa, pointb);
166 
167  if (++n > 100){ // avoiding infinity loop
168  break;
169  }
170  }
171  }
172 
173  entitya->setSelectable(true);
174  entityb->setSelectable(true);
175 
176  if (scene() != NULL)
177  scene()->update();
178  }
179 }
180 
181 QPointF QnstGraphicsEdge::arcPointAt(QLineF line, qreal at, bool toend)
182 {
183  qreal alfa = getAngle();
184 
185  qreal beta = (180 - alfa)/2 + (360 - line.angle());
186 
187  qreal R = line.length()/(::sin(((alfa/2)*PI)/180)*2);
188 
189  QPointF center_p(line.p2().x() - ::cos((180-beta-alfa)*PI/180)*R,
190  line.p2().y() + ::sin((180-beta-alfa)*PI/180)*R);
191 
192  qreal arc_len = alfa*PI*R/180;
193 
194  qreal new_arc_len = arc_len*at;
195 
196  qreal new_alfa = (180*new_arc_len)/(PI*R);
197 
198  qreal gama = (180-beta-new_alfa);
199 
200  QPointF new_start_p(center_p.x() + ::cos((gama)*PI/180)*R,
201  center_p.y() - ::sin((gama)*PI/180)*R);
202 
203  if (toend)
204  this->adjustedangle = new_alfa;
205  else
206  this->adjustedangle = (180*(arc_len-arc_len*at))/(PI*R);
207 
208  return new_start_p;
209 }
210 
211 void QnstGraphicsEdge::aux_adjust(QPointF pointa, QPointF pointb)
212 {
213  if (pointa.x() <= pointb.x() && pointa.y() <= pointb.y())
214  {
215  setTop(pointa.y()-6);
216  setLeft(pointa.x()-6);
217  setWidth((pointb.x()-6)-(pointa.x()-6) + 12);
218  setHeight((pointb.y()-6)-(pointa.y()-6) + 12);
219  }
220  else if (pointa.x() > pointb.x() && pointa.y() < pointb.y())
221  {
222  setTop(pointa.y()-6);
223  setLeft(pointb.x()-6);
224  setWidth((pointa.x()-6)-(pointb.x()-6) + 12);
225  setHeight((pointb.y()-6)-(pointa.y()-6) + 12);
226  }
227  else if (pointa.x() < pointb.x() && pointa.y() > pointb.y())
228  {
229  setTop(pointb.y()-6);
230  setLeft((pointa.x()-6));
231  setWidth((pointb.x()-6)-(pointa.x()-6) + 12);
232  setHeight((pointa.y()-6)-(pointb.y()-6) + 12);
233  }
234  else if (pointa.x() > pointb.x() && pointa.y() > pointb.y())
235  {
236  setTop(pointb.y()-6);
237  setLeft(pointb.x()-6);
238  setWidth((pointa.x()-6)-(pointb.x()-6) + 12);
239  setHeight((pointa.y()-6)-(pointb.y()-6) + 12);
240  }
241 }
242 
243 void QnstGraphicsEdge::move(QGraphicsSceneMouseEvent* event)
244 {
245  // nothing to do
246 }
247 
248 void QnstGraphicsEdge::resize(QGraphicsSceneMouseEvent* event)
249 {
250  // nothing to do
251 }
252 
253 QnstGraphicsEntityWithEdges::QnstGraphicsEntityWithEdges(QnstGraphicsEntity *parent)
254  : QnstGraphicsEntity(parent)
255 {
256 
257 }
258 
259 QnstGraphicsEntityWithEdges::~QnstGraphicsEntityWithEdges()
260 {
261 
262 }
263 
264 QVector<QnstGraphicsEdge*> QnstGraphicsEntityWithEdges::getnstGraphicsEdges()
265 {
266  return edges;
267 }
268 
269 void QnstGraphicsEntityWithEdges::addnstGraphicsEdge(QnstGraphicsEdge* edge)
270 {
271  if (edge != NULL)
272  edges.append(edge);
273 }
274 
275 void QnstGraphicsEntityWithEdges::removenstGraphicsEdge(QnstGraphicsEdge* edge)
276 {
277  if (edge != NULL)
278  {
279  int index = edges.indexOf(edge);
280 
281  if (index >= 0)
282  edges.remove(index);
283  }
284 }