1 #include "qnstgraphicsedge.h"
6 setncgType(Qncg::Edge);
7 setnstType(Qnst::Edge);
16 entityaenabled =
true;
17 entitybenabled =
true;
24 QnstGraphicsEdge::~QnstGraphicsEdge()
29 bool QnstGraphicsEdge::isInvalid()
34 void QnstGraphicsEdge::setInvalid(
bool invalid)
36 this->invalid = invalid;
46 this->entitya = entity;
56 this->entityb = entity;
59 bool QnstGraphicsEdge::isEntityAEnabled()
const
61 return entityaenabled;
64 void QnstGraphicsEdge::setEntityAEnabled(
bool enable)
66 this->entityaenabled = enable;
69 bool QnstGraphicsEdge::isEntityBEnabled()
const
71 return entitybenabled;
74 void QnstGraphicsEdge::setEntityBEnabled(
bool enable)
76 this->entitybenabled = enable;
79 qreal QnstGraphicsEdge::getAngle()
84 void QnstGraphicsEdge::setAngle(qreal angle)
89 qreal QnstGraphicsEdge::getAdjAngle()
94 void QnstGraphicsEdge::setAdjAngle(qreal adjangle)
96 this->adjustedangle = adjangle;
99 void QnstGraphicsEdge::adjust(
bool avoidCollision)
101 if (entitya != NULL && entityb != NULL)
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));
108 if (getEntityA()->getncgType() == Qncg::Interface)
110 if(getnstGraphicsParent())
111 line.setP1(getnstGraphicsParent()->mapFromItem(getEntityA()->getnstGraphicsParent(), line.p1()));
114 if (getEntityB()->getncgType() == Qncg::Interface)
116 if(getnstGraphicsParent())
117 line.setP2(getnstGraphicsParent()->mapFromItem(getEntityB()->getnstGraphicsParent(), line.p2()));
120 QPointF pointa = line.p1();
121 QPointF pointb = line.p2();
123 aux_adjust(pointa, pointb);
125 entitya->setSelectable(
false);
126 entityb->setSelectable(
false);
130 if (pointa != pointb){
135 while(entityb->collidesWithItem(
this))
140 pointb = line.pointAt(index);
142 pointb = arcPointAt(line , index);
144 aux_adjust(pointa, pointb);
156 while(entitya->collidesWithItem(
this))
161 pointa = line.pointAt(index);
163 pointa = arcPointAt(line , index);
165 aux_adjust(pointa, pointb);
173 entitya->setSelectable(
true);
174 entityb->setSelectable(
true);
181 QPointF QnstGraphicsEdge::arcPointAt(QLineF line, qreal at,
bool toend)
183 qreal alfa = getAngle();
185 qreal beta = (180 - alfa)/2 + (360 - line.angle());
187 qreal R = line.length()/(::sin(((alfa/2)*PI)/180)*2);
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);
192 qreal arc_len = alfa*PI*R/180;
194 qreal new_arc_len = arc_len*at;
196 qreal new_alfa = (180*new_arc_len)/(PI*R);
198 qreal gama = (180-beta-new_alfa);
200 QPointF new_start_p(center_p.x() + ::cos((gama)*PI/180)*R,
201 center_p.y() - ::sin((gama)*PI/180)*R);
204 this->adjustedangle = new_alfa;
206 this->adjustedangle = (180*(arc_len-arc_len*at))/(PI*R);
211 void QnstGraphicsEdge::aux_adjust(QPointF pointa, QPointF pointb)
213 if (pointa.x() <= pointb.x() && pointa.y() <= pointb.y())
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);
220 else if (pointa.x() > pointb.x() && pointa.y() < pointb.y())
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);
227 else if (pointa.x() < pointb.x() && pointa.y() > pointb.y())
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);
234 else if (pointa.x() > pointb.x() && pointa.y() > pointb.y())
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);
243 void QnstGraphicsEdge::move(QGraphicsSceneMouseEvent* event)
248 void QnstGraphicsEdge::resize(QGraphicsSceneMouseEvent* event)
259 QnstGraphicsEntityWithEdges::~QnstGraphicsEntityWithEdges()
264 QVector<QnstGraphicsEdge*> QnstGraphicsEntityWithEdges::getnstGraphicsEdges()
269 void QnstGraphicsEntityWithEdges::addnstGraphicsEdge(
QnstGraphicsEdge* edge)
275 void QnstGraphicsEntityWithEdges::removenstGraphicsEdge(
QnstGraphicsEdge* edge)
279 int index = edges.indexOf(edge);