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