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