]> Creatis software - cpPlugins.git/blobdiff - appli/cpPipelineEditor/QNEBlock.cxx
More on graph editor
[cpPlugins.git] / appli / cpPipelineEditor / QNEBlock.cxx
index 071effc1077bf48a23f420f4514a9987938b043d..67f4a9bd477d873a924b5e357298233797170128 100644 (file)
 #include <QStyleOptionGraphicsItem>\r
 \r
 #include "QNEPort.h"\r
+#include "QNEConnection.h"\r
 \r
 // -------------------------------------------------------------------------\r
 PipelineEditor::QNEBlock::\r
-QNEBlock( QGraphicsItem* parent, QGraphicsScene* scene )\r
+QNEBlock( TFilter* filter, QGraphicsItem* parent, QGraphicsScene* scene )\r
   : Superclass( parent, scene ),\r
     m_HorzMargin( 20 ),\r
     m_VertMargin( 5 ),\r
     m_NamePort( NULL ),\r
-    m_TypePort( NULL )\r
+    m_TypePort( NULL ),\r
+    m_Filter( filter )\r
 {\r
   QPainterPath p;\r
   p.addRoundedRect( -50, -15, 100, 30, 5, 5 );\r
@@ -54,6 +56,22 @@ QNEBlock( QGraphicsItem* parent, QGraphicsScene* scene )
 \r
   this->m_Width = this->m_HorzMargin;\r
   this->m_Height = this->m_VertMargin;\r
+\r
+  // Configure names\r
+  this->setNamePort( this->m_Filter->GetName( ) );\r
+  this->_setTypePort( this->m_Filter->GetClassName( ) );\r
+\r
+  // Add input ports\r
+  std::set< std::string > inputs;\r
+  this->m_Filter->GetInputsNames( inputs );\r
+  for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt )\r
+    this->addInputPort( iIt->c_str( ) );\r
+\r
+  // Add output ports\r
+  std::set< std::string > outputs;\r
+  this->m_Filter->GetOutputsNames( outputs );\r
+  for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )\r
+    this->addOutputPort( oIt->c_str( ) );\r
 }\r
 \r
 // -------------------------------------------------------------------------\r
@@ -69,79 +87,147 @@ setNamePort( const QString& txt )
   if( this->m_NamePort == NULL )\r
     this->m_NamePort = new QNENamePort( this );\r
   this->m_NamePort->setName( txt );\r
+  this->m_Filter->SetName( txt.toStdString( ) );\r
   this->_configPort( this->m_NamePort );\r
 }\r
 \r
 // -------------------------------------------------------------------------\r
-void PipelineEditor::QNEBlock::\r
-setTypePort( const QString& txt )\r
-{\r
-  if( this->m_TypePort == NULL )\r
-    this->m_TypePort = new QNETypePort( this );\r
-  this->m_TypePort->setName( txt );\r
-  this->_configPort( this->m_TypePort );\r
-}\r
-\r
-// -------------------------------------------------------------------------\r
-void PipelineEditor::QNEBlock::\r
+PipelineEditor::QNEInputPort* PipelineEditor::QNEBlock::\r
 addInputPort( const QString& txt )\r
 {\r
   QNEInputPort* ip = new QNEInputPort( this );\r
+  ip->setExtendedName(\r
+    (\r
+      txt.toStdString( ) +\r
+      std::string( "@" ) +\r
+      this->namePort( ).toStdString( )\r
+      ).c_str( )\r
+    );\r
   ip->setName( txt );\r
-  this->m_InputPorts.push_back( ip );\r
+  this->m_InputPorts[ txt.toStdString( ) ] = ip;\r
   this->_configPort( ip );\r
+  return( ip );\r
 }\r
 \r
 // -------------------------------------------------------------------------\r
-void PipelineEditor::QNEBlock::\r
+PipelineEditor::QNEOutputPort* PipelineEditor::QNEBlock::\r
 addOutputPort( const QString& txt )\r
 {\r
   QNEOutputPort* op = new QNEOutputPort( this );\r
+  op->setExtendedName(\r
+    (\r
+      txt.toStdString( ) +\r
+      std::string( "@" ) +\r
+      this->namePort( ).toStdString( )\r
+      ).c_str( )\r
+    );\r
   op->setName( txt );\r
-  this->m_OutputPorts.push_back( op );\r
+  this->m_OutputPorts[ txt.toStdString( ) ] = op;\r
   this->_configPort( op );\r
+  return( op );\r
 }\r
 \r
 // -------------------------------------------------------------------------\r
-QVector< PipelineEditor::QNEPort* > PipelineEditor::QNEBlock::\r
-ports( )\r
+bool PipelineEditor::QNEBlock::\r
+extendInputPort( const QString& txt, QNEConnection* conn )\r
 {\r
-  QVector< QNEPort* > res;\r
-  foreach( QGraphicsItem* i, this->childItems( ) )\r
+  auto p = this->m_InputPorts.find( txt.toStdString( ) );\r
+  auto i = this->m_ExtInputPorts.find( txt.toStdString( ) );\r
+  if( p != this->m_InputPorts.end( ) && i == this->m_ExtInputPorts.end( ) )\r
   {\r
-    QNEPort* p = dynamic_cast< QNEPort* >( i );\r
-    if( p != NULL )\r
-      res.append( p );\r
-\r
-  } // rof\r
-  return( res );\r
+    this->m_ExtInputPorts[ txt.toStdString( ) ] = conn;\r
+    return( true );\r
+  }\r
+  else\r
+    return( false );\r
 }\r
 \r
 // -------------------------------------------------------------------------\r
-PipelineEditor::QNEBlock* PipelineEditor::QNEBlock::\r
-clone( )\r
+bool PipelineEditor::QNEBlock::\r
+extendOutputPort( const QString& txt, QNEConnection* conn )\r
 {\r
-  QNEBlock* b = new QNEBlock( 0, this->scene( ) );\r
-  foreach( QGraphicsItem* i, this->childItems( ) )\r
+  auto p = this->m_OutputPorts.find( txt.toStdString( ) );\r
+  auto i = this->m_ExtOutputPorts.find( txt.toStdString( ) );\r
+  if( p != this->m_OutputPorts.end( ) && i == this->m_ExtOutputPorts.end( ) )\r
   {\r
-    QNENamePort* np = dynamic_cast< QNENamePort* >( i );\r
-    if( np != NULL )\r
-      b->setNamePort( np->name( ) );\r
+    this->m_ExtOutputPorts[ txt.toStdString( ) ] = conn;\r
+    return( true );\r
+  }\r
+  else\r
+    return( false );\r
+}\r
+\r
+// -------------------------------------------------------------------------\r
+PipelineEditor::QNEInputPort* PipelineEditor::QNEBlock::\r
+inputPort( const QString& txt )\r
+{\r
+  auto i = this->m_InputPorts.find( txt.toStdString( ) );\r
+  if( i != this->m_InputPorts.end( ) )\r
+    return( i->second );\r
+  else\r
+    return( NULL );\r
+}\r
+\r
+// -------------------------------------------------------------------------\r
+PipelineEditor::QNEOutputPort* PipelineEditor::QNEBlock::\r
+outputPort( const QString& txt )\r
+{\r
+  auto o = this->m_OutputPorts.find( txt.toStdString( ) );\r
+  if( o != this->m_OutputPorts.end( ) )\r
+    return( o->second );\r
+  else\r
+    return( NULL );\r
+}\r
+\r
+// -------------------------------------------------------------------------\r
+QString PipelineEditor::QNEBlock::\r
+namePort( ) const\r
+{\r
+  return( this->m_NamePort->name( ) );\r
+}\r
 \r
-    QNETypePort* tp = dynamic_cast< QNETypePort* >( i );\r
-    if( tp != NULL )\r
-      b->setTypePort( tp->name( ) );\r
+// -------------------------------------------------------------------------\r
+const PipelineEditor::QNEInputPort* PipelineEditor::QNEBlock::\r
+inputPort( const QString& txt ) const\r
+{\r
+  auto i = this->m_InputPorts.find( txt.toStdString( ) );\r
+  if( i != this->m_InputPorts.end( ) )\r
+    return( i->second );\r
+  else\r
+    return( NULL );\r
+}\r
 \r
-    QNEInputPort* ip = dynamic_cast< QNEInputPort* >( i );\r
-    if( ip != NULL )\r
-      b->addInputPort( ip->name( ) );\r
+// -------------------------------------------------------------------------\r
+const PipelineEditor::QNEOutputPort* PipelineEditor::QNEBlock::\r
+outputPort( const QString& txt ) const\r
+{\r
+  auto o = this->m_OutputPorts.find( txt.toStdString( ) );\r
+  if( o != this->m_OutputPorts.end( ) )\r
+    return( o->second );\r
+  else\r
+    return( NULL );\r
+}\r
 \r
-    QNEOutputPort* op = dynamic_cast< QNEOutputPort* >( i );\r
-    if( op != NULL )\r
-      b->addOutputPort( op->name( ) );\r
+// -------------------------------------------------------------------------\r
+const PipelineEditor::QNEConnection* PipelineEditor::QNEBlock::\r
+extendedInputPort( const QString& txt ) const\r
+{\r
+  auto i = this->m_ExtInputPorts.find( txt.toStdString( ) );\r
+  if( i != this->m_ExtInputPorts.end( ) )\r
+    return( i->second );\r
+  else\r
+    return( NULL );\r
+}\r
 \r
-  } // rof\r
-  return( b );\r
+// -------------------------------------------------------------------------\r
+const PipelineEditor::QNEConnection* PipelineEditor::QNEBlock::\r
+extendedOutputPort( const QString& txt ) const\r
+{\r
+  auto i = this->m_ExtOutputPorts.find( txt.toStdString( ) );\r
+  if( i != this->m_ExtOutputPorts.end( ) )\r
+    return( i->second );\r
+  else\r
+    return( NULL );\r
 }\r
 \r
 // -------------------------------------------------------------------------\r
@@ -174,6 +260,16 @@ itemChange( GraphicsItemChange change, const QVariant& value )
   return( value );\r
 }\r
 \r
+// -------------------------------------------------------------------------\r
+void PipelineEditor::QNEBlock::\r
+_setTypePort( const QString& txt )\r
+{\r
+  if( this->m_TypePort == NULL )\r
+    this->m_TypePort = new QNETypePort( this );\r
+  this->m_TypePort->setName( txt );\r
+  this->_configPort( this->m_TypePort );\r
+}\r
+\r
 // -------------------------------------------------------------------------\r
 void PipelineEditor::QNEBlock::\r
 _configPort( QNEPort* port )\r
@@ -181,11 +277,13 @@ _configPort( QNEPort* port )
   port->setBlock( this );\r
 \r
   QFontMetrics fm( this->scene( )->font( ) );\r
-  int w = fm.width( port->name( ) );\r
+  int w = fm.width( port->name( ) ) + ( 4 * port->radius( ) );\r
   int h = fm.height( );\r
   if( w > this->m_Width - this->m_HorzMargin )\r
     this->m_Width = w + this->m_HorzMargin;\r
-  this->m_Height += h;\r
+  this->m_Height = this->m_InputPorts.size( ) + this->m_OutputPorts.size( );\r
+  this->m_Height += 4;\r
+  this->m_Height *= h;\r
 \r
   QPainterPath pth;\r
   pth.addRoundedRect(\r
@@ -203,13 +301,42 @@ _configPort( QNEPort* port )
     if( p == NULL )\r
       continue;\r
 \r
-    if( dynamic_cast< QNEOutputPort* >( p ) != NULL )\r
-      p->setPos( this->m_Width / 2 + port->radius( ), y );\r
-    else\r
-      p->setPos( -this->m_Width / 2 - port->radius( ), y );\r
+    if( dynamic_cast< QNENamePort* >( i ) != NULL )\r
+      i->setPos( -this->m_Width / 2 + port->radius( ), y );\r
+    else if( dynamic_cast< QNETypePort* >( i ) != NULL )\r
+    {\r
+      i->setPos( -this->m_Width / 2 + port->radius( ), y );\r
+      y += h;\r
+    }\r
+    else if( dynamic_cast< QNEInputPort* >( i ) != NULL )\r
+      i->setPos( -this->m_Width / 2 - 2 * port->radius( ), y );\r
+    else if( dynamic_cast< QNEOutputPort* >( i ) != NULL )\r
+      i->setPos( this->m_Width / 2, y );\r
+\r
+    /* TODO\r
+       QNEPort* p = dynamic_cast< QNEPort* >( i );\r
+       if( p == NULL )\r
+       continue;\r
+\r
+       if( dynamic_cast< QNEOutputPort* >( p ) != NULL )\r
+       p->setPos( this->m_Width / 2 + port->radius( ), y );\r
+       else\r
+       p->setPos( -this->m_Width / 2 - port->radius( ), y );\r
+    */\r
     y += h;\r
 \r
   } // rof\r
 }\r
 \r
+// -------------------------------------------------------------------------\r
+void PipelineEditor::QNEBlock::\r
+mouseReleaseEvent( QGraphicsSceneMouseEvent* evt )\r
+{\r
+  if( this->m_Filter.IsNotNull( ) )\r
+    this->m_Filter->SetViewCoords(\r
+      this->scenePos( ).x( ), this->scenePos( ).y( )\r
+      );\r
+  this->Superclass::mouseReleaseEvent( evt );\r
+}\r
+\r
 // eof - $RCSfile$\r