]> Creatis software - cpPlugins.git/blob - appli/cpPipelineEditor/QNEPort.cxx
...
[cpPlugins.git] / appli / cpPipelineEditor / QNEPort.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 "QNEPort.h"\r
28 #include "QNEConnection.h"\r
29 \r
30 #include <QGraphicsScene>\r
31 #include <QFontMetrics>\r
32 #include <QPen>\r
33 \r
34 // -------------------------------------------------------------------------\r
35 PipelineEditor::QNEPort::\r
36 QNEPort( QGraphicsItem* parent, QGraphicsScene* scene )\r
37   : Superclass( parent, scene ),\r
38     m_Radius( 5 ),\r
39     m_Margin( 2 )\r
40 {\r
41   this->m_Label = new QGraphicsTextItem( this );\r
42 \r
43   QPainterPath p;\r
44   p.addEllipse( \r
45     -this->m_Radius, -this->m_Radius,\r
46     2 * this->m_Radius, 2 * this->m_Radius\r
47     );\r
48 \r
49   this->setPath( p );\r
50   this->setPen( QPen( Qt::darkRed ) );\r
51   this->setBrush( Qt::red );\r
52   this->setFlag( QGraphicsItem::ItemSendsScenePositionChanges );\r
53   this->m_PortFlags = 0;\r
54 }\r
55 \r
56 // -------------------------------------------------------------------------\r
57 PipelineEditor::QNEPort::\r
58 ~QNEPort( )\r
59 {\r
60   foreach( QNEConnection* conn, this->m_Connections )\r
61     delete conn;\r
62 }\r
63 \r
64 // -------------------------------------------------------------------------\r
65 void PipelineEditor::QNEPort::\r
66 setNEBlock( QNEBlock *b )\r
67 {\r
68   this->m_Block = b;\r
69 }\r
70 \r
71 // -------------------------------------------------------------------------\r
72 void PipelineEditor::QNEPort::\r
73 setName( const QString &n )\r
74 {\r
75   this->m_Name = n;\r
76   this->m_Label->setPlainText( n );\r
77 }\r
78 \r
79 // -------------------------------------------------------------------------\r
80 void PipelineEditor::QNEPort::\r
81 setIsOutput( bool o )\r
82 {\r
83   this->m_IsOutput = o;\r
84 \r
85   QFontMetrics fm( this->scene( )->font( ) );\r
86   QRect r = fm.boundingRect( this->m_Name );\r
87 \r
88   int rm = this->m_Radius + this->m_Margin;\r
89   int h = -this->m_Label->boundingRect( ).height( ) / 2;\r
90   if( this->m_IsOutput )\r
91     this->m_Label->setPos(\r
92       -rm - this->m_Label->boundingRect( ).width( ), h\r
93       );\r
94   else\r
95     this->m_Label->setPos( rm, h );\r
96 }\r
97 \r
98 // -------------------------------------------------------------------------\r
99 int PipelineEditor::QNEPort::\r
100 radius( )\r
101 {\r
102   return( this->m_Radius );\r
103 }\r
104 \r
105 // -------------------------------------------------------------------------\r
106 bool PipelineEditor::QNEPort::\r
107 isOutput( )\r
108 {\r
109   return( this->m_IsOutput );\r
110 }\r
111 \r
112 // -------------------------------------------------------------------------\r
113 QVector< PipelineEditor::QNEConnection* >& PipelineEditor::QNEPort::\r
114 connections( )\r
115 {\r
116   return( this->m_Connections );\r
117 }\r
118 \r
119 // -------------------------------------------------------------------------\r
120 void PipelineEditor::QNEPort::\r
121 setPortFlags( int f )\r
122 {\r
123   this->m_PortFlags = f;\r
124 \r
125   if( this->m_PortFlags & Self::TypePort )\r
126   {\r
127     QFont font( this->scene( )->font( ) );\r
128     font.setItalic( true );\r
129     this->m_Label->setFont( font );\r
130     this->setPath( QPainterPath( ) );\r
131   }\r
132   else if( this->m_PortFlags & Self::NamePort )\r
133   {\r
134     QFont font( this->scene( )->font( ) );\r
135     font.setBold( true );\r
136     this->m_Label->setFont( font );\r
137     this->setPath( QPainterPath( ) );\r
138 \r
139   } // fi\r
140 }\r
141 \r
142 // -------------------------------------------------------------------------\r
143 PipelineEditor::QNEBlock* PipelineEditor::QNEPort::\r
144 block( ) const\r
145 {\r
146   return( this->m_Block );\r
147 }\r
148 \r
149 // -------------------------------------------------------------------------\r
150 quint64 PipelineEditor::QNEPort::\r
151 ptr( )\r
152 {\r
153   return( this->m_Ptr );\r
154 }\r
155 \r
156 // -------------------------------------------------------------------------\r
157 void PipelineEditor::QNEPort::\r
158 setPtr( quint64 p )\r
159 {\r
160   this->m_Ptr = p;\r
161 }\r
162 \r
163 // -------------------------------------------------------------------------\r
164 bool PipelineEditor::QNEPort::\r
165 isConnected( QNEPort* other )\r
166 {\r
167   foreach( QNEConnection* conn, this->m_Connections )\r
168     if( conn->port1( ) == other || conn->port2( ) == other )\r
169       return( true );\r
170   return( false );\r
171 }\r
172 \r
173 // -------------------------------------------------------------------------\r
174 QVariant PipelineEditor::QNEPort::\r
175 itemChange( GraphicsItemChange change, const QVariant& value )\r
176 {\r
177   if( change == ItemScenePositionHasChanged )\r
178   {\r
179     foreach( QNEConnection* conn, this->m_Connections )\r
180     {\r
181       conn->updatePosFromPorts( );\r
182       conn->updatePath( );\r
183 \r
184     } // rof\r
185 \r
186   } // fi\r
187   return( value );\r
188 }\r
189 \r
190 // eof - $RCSfile$\r