]> Creatis software - cpPlugins.git/blob - appli/cpPipelineEditor/QNEConnection.cxx
More on graph editor
[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 \r
29 #include "QNEPort.h"\r
30 \r
31 #include <QBrush>\r
32 #include <QPen>\r
33 #include <QGraphicsScene>\r
34 \r
35 // -------------------------------------------------------------------------\r
36 PipelineEditor::QNEConnection::\r
37 QNEConnection( QGraphicsItem* parent, QGraphicsScene* scene )\r
38   : Superclass( parent, scene )\r
39 {\r
40   this->setPen( QPen( Qt::black, 2 ) );\r
41   this->setBrush( Qt::NoBrush );\r
42   this->setZValue( -1 );\r
43   this->m_Port1 = 0;\r
44   this->m_Port2 = 0;\r
45 }\r
46 \r
47 // -------------------------------------------------------------------------\r
48 PipelineEditor::QNEConnection::\r
49 ~QNEConnection( )\r
50 {\r
51   if ( this->m_Port1 )\r
52     this->m_Port1->connections( ).remove(\r
53       this->m_Port1->connections( ).indexOf( this )\r
54       );\r
55   if ( this->m_Port2 )\r
56     this->m_Port2->connections( ).remove(\r
57       this->m_Port2->connections( ).indexOf( this )\r
58       );\r
59 }\r
60 \r
61 // -------------------------------------------------------------------------\r
62 void PipelineEditor::QNEConnection::\r
63 setPos1( const QPointF& p )\r
64 {\r
65   this->m_Pos1 = p;\r
66 }\r
67 \r
68 // -------------------------------------------------------------------------\r
69 void PipelineEditor::QNEConnection::\r
70 setPos2( const QPointF& p )\r
71 {\r
72   this->m_Pos2 = p;\r
73 }\r
74 \r
75 // -------------------------------------------------------------------------\r
76 void PipelineEditor::QNEConnection::\r
77 setPort1( QNEPort* p )\r
78 {\r
79   this->m_Port1 = p;\r
80   this->m_Port1->connections( ).append( this );\r
81 }\r
82 \r
83 // -------------------------------------------------------------------------\r
84 void PipelineEditor::QNEConnection::\r
85 setPort2( QNEPort* p )\r
86 {\r
87   this->m_Port2 = p;\r
88   this->m_Port2->connections( ).append( this );\r
89 }\r
90 \r
91 // -------------------------------------------------------------------------\r
92 void PipelineEditor::QNEConnection::\r
93 updatePosFromPorts( )\r
94 {\r
95   this->m_Pos1 = this->m_Port1->scenePos( );\r
96   this->m_Pos2 = this->m_Port2->scenePos( );\r
97 }\r
98 \r
99 // -------------------------------------------------------------------------\r
100 void PipelineEditor::QNEConnection::\r
101 updatePath( )\r
102 {\r
103   QPainterPath p;\r
104 \r
105   p.moveTo( this->m_Pos1 );\r
106 \r
107   qreal dx = this->m_Pos2.x( ) - this->m_Pos1.x( );\r
108   qreal dy = this->m_Pos2.y( ) - this->m_Pos1.y( );\r
109 \r
110   QPointF ctr1( this->m_Pos1.x( ) + dx * 0.25, this->m_Pos1.y( ) + dy * 0.1 );\r
111   QPointF ctr2( this->m_Pos1.x( ) + dx * 0.75, this->m_Pos1.y( ) + dy * 0.9 );\r
112 \r
113   p.cubicTo( ctr1, ctr2, this->m_Pos2 );\r
114 \r
115   this->setPath( p );\r
116 }\r
117 \r
118 // -------------------------------------------------------------------------\r
119 PipelineEditor::QNEPort* PipelineEditor::QNEConnection::\r
120 port1( ) const\r
121 {\r
122   return( this->m_Port1 );\r
123 }\r
124 \r
125 // -------------------------------------------------------------------------\r
126 PipelineEditor::QNEPort* PipelineEditor::QNEConnection::\r
127 port2( ) const\r
128 {\r
129   return( this->m_Port2 );\r
130 }\r
131 \r
132 // -------------------------------------------------------------------------\r
133 void PipelineEditor::QNEConnection::\r
134 save( QDataStream& ds )\r
135 {\r
136   ds << ( quint64 ) this->m_Port1;\r
137   ds << ( quint64 ) this->m_Port2;\r
138 }\r
139 \r
140 // -------------------------------------------------------------------------\r
141 void PipelineEditor::QNEConnection::\r
142 load( QDataStream& ds, const QMap< quint64, QNEPort* >& portMap )\r
143 {\r
144   quint64 ptr1;\r
145   quint64 ptr2;\r
146   ds >> ptr1;\r
147   ds >> ptr2;\r
148 \r
149   this->setPort1( portMap[ ptr1 ] );\r
150   this->setPort2( portMap[ ptr2 ] );\r
151   this->updatePosFromPorts( );\r
152   this->updatePath( );\r
153 }\r
154 \r
155 // eof - $RCSfile$\r