]> Creatis software - cpPlugins.git/blob - appli/cpPipelineEditor/QNEConnection.cxx
...
[cpPlugins.git] / appli / cpPipelineEditor / QNEConnection.cxx
1 /* Copyright (c) 2012, STANISLAW ADASZEWSKI\r
2    All rights reserved.\r
3 \r
4    Redistribution and use in source and binary forms, with or without\r
5    modification, are permitted provided that the following conditions are met:\r
6    * Redistributions of source code must retain the above copyright\r
7    notice, this list of conditions and the following disclaimer.\r
8    * Redistributions in binary form must reproduce the above copyright\r
9    notice, this list of conditions and the following disclaimer in the\r
10    documentation and/or other materials provided with the distribution.\r
11    * Neither the name of STANISLAW ADASZEWSKI nor the\r
12    names of its contributors may be used to endorse or promote products\r
13    derived from this software without specific prior written permission.\r
14 \r
15    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND\r
16    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
17    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
18    DISCLAIMED. IN NO EVENT SHALL STANISLAW ADASZEWSKI BE LIABLE FOR ANY\r
19    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
20    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
21    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
22    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
23    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
24    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
25 */\r
26 \r
27 #include "QNEConnection.h"\r
28 #include "QNEPort.h"\r
29 \r
30 #include <QBrush>\r
31 #include <QPen>\r
32 #include <QGraphicsScene>\r
33 \r
34 // -------------------------------------------------------------------------\r
35 PipelineEditor::QNEConnection::\r
36 QNEConnection( QGraphicsItem* parent, QGraphicsScene* scene )\r
37   : Superclass( parent, scene )\r
38 {\r
39   this->setPen( QPen( Qt::black, 2 ) );\r
40   this->setBrush( Qt::NoBrush );\r
41   this->setZValue( -1 );\r
42   this->m_Port1 = NULL;\r
43   this->m_Port2 = NULL;\r
44 }\r
45 \r
46 // -------------------------------------------------------------------------\r
47 PipelineEditor::QNEConnection::\r
48 ~QNEConnection( )\r
49 {\r
50   if( this->m_Port1 != NULL )\r
51     this->m_Port1->connections( ).\r
52       remove( this->m_Port1->connections( ).indexOf( this ) );\r
53 \r
54   if( this->m_Port2 != NULL )\r
55     this->m_Port2->setConnection( NULL );\r
56 }\r
57 \r
58 // -------------------------------------------------------------------------\r
59 void PipelineEditor::QNEConnection::\r
60 setPos1( const QPointF& p )\r
61 {\r
62   this->m_Pos1 = p;\r
63 }\r
64 \r
65 // -------------------------------------------------------------------------\r
66 void PipelineEditor::QNEConnection::\r
67 setPos2( const QPointF& p )\r
68 {\r
69   this->m_Pos2 = p;\r
70 }\r
71 \r
72 // -------------------------------------------------------------------------\r
73 void PipelineEditor::QNEConnection::\r
74 setPort1( QNEOutputPort* p )\r
75 {\r
76   if( p != NULL )\r
77   {\r
78     p->connections( ).append( this );\r
79     this->m_Port1 = p;\r
80 \r
81   } // fi\r
82 }\r
83 \r
84 // -------------------------------------------------------------------------\r
85 void PipelineEditor::QNEConnection::\r
86 setPort2( QNEInputPort* p )\r
87 {\r
88   if( p != NULL )\r
89   {\r
90     if( p->connection( ) == NULL )\r
91     {\r
92       p->setConnection( this );\r
93       this->m_Port2 = p;\r
94 \r
95     } // fi\r
96 \r
97   } // fi\r
98 }\r
99 \r
100 // -------------------------------------------------------------------------\r
101 void PipelineEditor::QNEConnection::\r
102 updatePosFromPorts( )\r
103 {\r
104   this->m_Pos1 = this->m_Port1->scenePos( );\r
105   this->m_Pos2 = this->m_Port2->scenePos( );\r
106 }\r
107 \r
108 // -------------------------------------------------------------------------\r
109 void PipelineEditor::QNEConnection::\r
110 updatePath( )\r
111 {\r
112   QPainterPath p;\r
113   p.moveTo( this->m_Pos1 );\r
114 \r
115   qreal dx = this->m_Pos2.x( ) - this->m_Pos1.x( );\r
116   qreal dy = this->m_Pos2.y( ) - this->m_Pos1.y( );\r
117   QPointF c1( this->m_Pos1.x( ) + dx * 0.25, this->m_Pos1.y( ) + dy * 0.1 );\r
118   QPointF c2( this->m_Pos1.x( ) + dx * 0.75, this->m_Pos1.y( ) + dy * 0.9 );\r
119   p.cubicTo( c1, c2, this->m_Pos2 );\r
120 \r
121   this->setPath( p );\r
122 }\r
123 \r
124 // -------------------------------------------------------------------------\r
125 PipelineEditor::QNEOutputPort* PipelineEditor::QNEConnection::\r
126 port1( ) const\r
127 {\r
128   return( this->m_Port1 );\r
129 }\r
130 \r
131 // -------------------------------------------------------------------------\r
132 PipelineEditor::QNEInputPort* PipelineEditor::QNEConnection::\r
133 port2( ) const\r
134 {\r
135   return( this->m_Port2 );\r
136 }\r
137 \r
138 // eof - $RCSfile$\r