]> Creatis software - cpPlugins.git/blob - appli/cpPipelineEditor/Edge.cxx
More on graph editor
[cpPlugins.git] / appli / cpPipelineEditor / Edge.cxx
1 #include "Edge.h"
2 #include "Node.h"
3
4 #include <QPainter>
5
6 // -------------------------------------------------------------------------
7 PipelineEditor::Edge::
8 Edge(
9   const Node* nsrc, const Node* ndes,
10   const QRectF* rsrc, const QRectF* rdes
11   )
12   : QGraphicsItem( NULL )
13 {
14   this->setAcceptedMouseButtons( 0 );
15   this->m_SrcNode = nsrc;
16   this->m_DesNode = ndes;
17   this->m_SrcRect = rsrc;
18   this->m_DesRect = rdes;
19   this->adjust( );
20 }
21
22 // -------------------------------------------------------------------------
23 PipelineEditor::Edge::
24 ~Edge( )
25 {
26 }
27
28 // -------------------------------------------------------------------------
29 const QRectF* PipelineEditor::Edge::
30 source( ) const
31 {
32   return( this->m_SrcRect );
33 }
34
35 // -------------------------------------------------------------------------
36 const QRectF* PipelineEditor::Edge::
37 destination( ) const
38 {
39   return( this->m_DesRect );
40 }
41
42 // -------------------------------------------------------------------------
43 void PipelineEditor::Edge::
44 adjust( )
45 {
46
47   /* TODO
48      if( this->m_Src == NULL || this->m_Des == NULL )
49      return;
50
51      QLineF line(
52      this->mapFromParent( this->m_Src->center( ) ),
53      this->mapFromParent( this->m_Des->center( ) )
54      );
55      // TODO: qreal length = line.length( );
56      this->prepareGeometryChange( );
57      if( length > qreal( 20 ) )
58      {
59      QPointF edgeOffset(
60      ( line.dx( ) * qreal( 10 ) ) / length,
61      ( line.dy( ) * qreal( 10 ) ) / length
62      );
63      this->m_SrcPoint = line.p1( ) + edgeOffset;
64      this->m_DesPoint = line.p2( ) - edgeOffset;
65      }
66      else
67      this->m_SrcPoint = this->m_DesPoint = line.p1( );
68   */
69 }
70
71 // -------------------------------------------------------------------------
72 QRectF PipelineEditor::Edge::
73 boundingRect( ) const
74 {
75   if( this->m_SrcRect == NULL || this->m_DesRect == NULL )
76     return( QRectF( ) );
77
78   /*
79     qreal penWidth = 1;
80     qreal extra = ( penWidth + this->m_ArrowSize ) / qreal( 2 );
81   */
82   qreal extra = qreal( 1 );
83   return(
84     QRectF(
85       this->m_SrcRect->center( ),
86       QSizeF(
87         this->m_DesRect->center( ).x( ) - this->m_SrcRect->center( ).x( ),
88         this->m_DesRect->center( ).y( ) - this->m_SrcRect->center( ).y( )
89         )
90       ).normalized( ).adjusted( -extra, -extra, extra, extra )
91     );
92 }
93
94 // -------------------------------------------------------------------------
95 void PipelineEditor::Edge::
96 paint(
97   QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget
98   )
99 {
100   /* TODO
101     if( this->m_Src == NULL || this->m_Des == NULL )
102     return;
103   */
104
105   // TODO: QLineF line( this->m_Src->center( ), this->m_Des->center( ) );
106   QLineF line(
107     this->m_SrcNode->mapToScene( this->m_SrcRect->center( ) ),
108     this->m_DesNode->mapToScene( this->m_DesRect->center( ) )
109     );
110
111   if( qFuzzyCompare( line.length(), qreal( 0 ) ) )
112     return;
113
114   // Draw the line itself
115   painter->setPen(
116     QPen( Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin )
117     );
118   painter->drawLine( line );
119
120   /* TODO
121      // Draw the arrows
122      double angle = ::acos(line.dx() / line.length());
123      if (line.dy() >= 0)
124      angle = TwoPi - angle;
125
126      QPointF destArrowP1 = destPoint + QPointF(
127      sin(angle - Pi / 3) * arrowSize,
128      cos(angle - Pi / 3) * arrowSize
129      );
130      QPointF destArrowP2 = destPoint + QPointF(
131      sin(angle - Pi + Pi / 3) * arrowSize,
132      cos(angle - Pi + Pi / 3) * arrowSize
133      );
134      QPointF center = sourcePoint + destPoint;
135      center /= 2;
136
137      painter->setBrush(Qt::black);
138      painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);
139      painter->drawText( center, "Edge!!!" );
140   */
141 }
142
143 // eof - $RCSfile$