MNE-CPP  beta 1.0
arrow.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 //*************************************************************************************************************
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "arrow.h"
42 
43 #include <math.h>
44 
45 #include <QPen>
46 #include <QPainter>
47 
48 
49 //*************************************************************************************************************
50 //=============================================================================================================
51 // CONSTS
52 //=============================================================================================================
53 
54 const qreal Pi = 3.14;
55 
56 
57 //*************************************************************************************************************
58 //=============================================================================================================
59 // USED NAMESPACES
60 //=============================================================================================================
61 
62 using namespace MNEX;
63 
64 
65 //*************************************************************************************************************
66 //=============================================================================================================
67 // DEFINE MEMBER METHODS
68 //=============================================================================================================
69 
70 Arrow::Arrow(PluginItem *startItem, PluginItem *endItem, PluginConnectorConnection::SPtr &connection, QGraphicsItem *parent)
71 : QGraphicsLineItem(parent)
72 , m_StartItem(startItem)
73 , m_EndItem(endItem)
74 , m_pConnection(connection)
75 {
76  m_StartItem = startItem;
77  m_EndItem = endItem;
78  setFlag(QGraphicsItem::ItemIsSelectable, true);
79  m_qColor = Qt::black;
80  setPen(QPen(m_qColor, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
81 }
82 
83 
84 //*************************************************************************************************************
85 
86 QRectF Arrow::boundingRect() const
87 {
88  qreal extra = (pen().width() + 20) / 2.0;
89 
90  return QRectF(line().p1(), QSizeF(line().p2().x() - line().p1().x(),
91  line().p2().y() - line().p1().y()))
92  .normalized()
93  .adjusted(-extra, -extra, extra, extra);
94 }
95 
96 
97 //*************************************************************************************************************
98 
99 QPainterPath Arrow::shape() const
100 {
101  QPainterPath path = QGraphicsLineItem::shape();
102  path.addPolygon(arrowHead);
103  return path;
104 }
105 
106 
107 //*************************************************************************************************************
108 
109 void Arrow::updatePosition()
110 {
111  QLineF line(mapFromItem(m_StartItem, 0, 0), mapFromItem(m_EndItem, 0, 0));
112  setLine(line);
113 }
114 
115 
116 //*************************************************************************************************************
117 
118 void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
119  QWidget *)
120 {
121  if (m_StartItem->collidesWithItem(m_EndItem))
122  return;
123 
124  QPen myPen = pen();
125  myPen.setColor(m_qColor);
126  qreal arrowSize = 10;
127  painter->setPen(myPen);
128  painter->setBrush(m_qColor);
129 
130  painter->setRenderHint(QPainter::Antialiasing);
131 
132 
133  QLineF centerLine(m_StartItem->pos(), m_EndItem->pos());
134  QPolygonF endPolygon = m_EndItem->polygon();
135  QPointF p1 = endPolygon.first() + m_EndItem->pos();
136  QPointF p2;
137  QPointF intersectPoint;
138  QLineF polyLine;
139  for (int i = 1; i < endPolygon.count(); ++i) {
140  p2 = endPolygon.at(i) + m_EndItem->pos();
141  polyLine = QLineF(p1, p2);
142  QLineF::IntersectType intersectType =
143  polyLine.intersect(centerLine, &intersectPoint);
144  if (intersectType == QLineF::BoundedIntersection)
145  break;
146  p1 = p2;
147  }
148 
149  setLine(QLineF(intersectPoint, m_StartItem->pos()));
150 
151  double angle = ::acos(line().dx() / line().length());
152  if (line().dy() >= 0)
153  angle = (Pi * 2) - angle;
154 
155  QPointF arrowP1 = line().p1() + QPointF(sin(angle + Pi * 2 / 5) * arrowSize,
156  cos(angle + Pi * 2 / 5) * arrowSize);
157  QPointF arrowP2 = line().p1() + QPointF(sin(angle + Pi - Pi * 2 / 5) * arrowSize,
158  cos(angle + Pi - Pi * 2 / 5) * arrowSize);
159 
160  arrowHead.clear();
161  arrowHead << line().p1() << arrowP1 << arrowP2;
162  painter->drawLine(line());
163  painter->drawPolygon(arrowHead);
164  if (isSelected()) {
165  painter->setPen(QPen(m_qColor, 1, Qt::DashLine));
166  QLineF qLine = line();
167  qLine.translate(0, 4.0);
168  painter->drawLine(qLine);
169  qLine.translate(0,-8.0);
170  painter->drawLine(qLine);
171  }
172 }
QSharedPointer< PluginConnectorConnection > SPtr
Definition: arrow.h:75
Arrow class declaration.