]> Creatis software - cpPlugins.git/blobdiff - appli/cpPipelineEditor/QNEBlock.cxx
More on graph editor
[cpPlugins.git] / appli / cpPipelineEditor / QNEBlock.cxx
diff --git a/appli/cpPipelineEditor/QNEBlock.cxx b/appli/cpPipelineEditor/QNEBlock.cxx
new file mode 100644 (file)
index 0000000..d1e0109
--- /dev/null
@@ -0,0 +1,260 @@
+/* Copyright (c) 2012, STANISLAW ADASZEWSKI\r
+   All rights reserved.\r
+\r
+   Redistribution and use in source and binary forms, with or without\r
+   modification, are permitted provided that the following conditions are met:\r
+   * Redistributions of source code must retain the above copyright\r
+   notice, this list of conditions and the following disclaimer.\r
+   * Redistributions in binary form must reproduce the above copyright\r
+   notice, this list of conditions and the following disclaimer in the\r
+   documentation and/or other materials provided with the distribution.\r
+   * Neither the name of STANISLAW ADASZEWSKI nor the\r
+   names of its contributors may be used to endorse or promote products\r
+   derived from this software without specific prior written permission.\r
+\r
+   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND\r
+   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
+   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
+   DISCLAIMED. IN NO EVENT SHALL STANISLAW ADASZEWSKI BE LIABLE FOR ANY\r
+   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
+   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
+   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
+   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
+   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+*/\r
+\r
+#include "QNEBlock.h"\r
+\r
+#include <QPen>\r
+#include <QGraphicsScene>\r
+#include <QFontMetrics>\r
+#include <QPainter>\r
+#include <QStyleOptionGraphicsItem>\r
+\r
+#include "QNEPort.h"\r
+\r
+// -------------------------------------------------------------------------\r
+PipelineEditor::QNEBlock::\r
+QNEBlock( QGraphicsItem* parent, QGraphicsScene* scene )\r
+  : Superclass( parent, scene ),\r
+    m_HorzMargin( 20 ),\r
+    m_VertMargin( 5 )\r
+{\r
+  QPainterPath p;\r
+  p.addRoundedRect( -50, -15, 100, 30, 5, 5 );\r
+  this->setPath( p );\r
+  this->setPen( QPen( Qt::darkGreen ) );\r
+  this->setBrush( Qt::green );\r
+  this->setFlag( QGraphicsItem::ItemIsMovable );\r
+  this->setFlag( QGraphicsItem::ItemIsSelectable );\r
+  this->m_Width = this->m_HorzMargin;\r
+  this->m_Height = this->m_VertMargin;\r
+}\r
+\r
+// -------------------------------------------------------------------------\r
+PipelineEditor::QNEBlock::\r
+~QNEBlock( )\r
+{\r
+}\r
+\r
+// -------------------------------------------------------------------------\r
+PipelineEditor::QNEPort* PipelineEditor::QNEBlock::\r
+addPort( const QString& name, bool isOutput, int flags, int ptr )\r
+{\r
+  QNEPort* port = new QNEPort( this );\r
+  port->setName( name );\r
+  port->setIsOutput( isOutput );\r
+  port->setNEBlock( this );\r
+  port->setPortFlags( flags );\r
+  port->setPtr( ptr );\r
+\r
+  QFontMetrics fm( this->scene( )->font( ) );\r
+  int w = fm.width( name );\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
+\r
+  QPainterPath p;\r
+  p.addRoundedRect(\r
+    -this->m_Width / 2,\r
+    -this->m_Height / 2,\r
+    this->m_Width,\r
+    this->m_Height, 5, 5\r
+    );\r
+  this->setPath( p );\r
+\r
+  int y = -this->m_Height / 2 + this->m_VertMargin + port->radius( );\r
+  foreach( QGraphicsItem* port_, children( ) )\r
+  {\r
+    if( port_->type( ) != QNEPort::Type )\r
+      continue;\r
+\r
+    QNEPort* port = ( QNEPort* ) port_;\r
+    if( port->isOutput( ) )\r
+      port->setPos( this->m_Width/2 + port->radius( ), y );\r
+    else\r
+      port->setPos( -this->m_Width/2 - port->radius( ), y );\r
+    y += h;\r
+\r
+  } // rof\r
+  return( port );\r
+}\r
+\r
+// -------------------------------------------------------------------------\r
+void PipelineEditor::QNEBlock::\r
+addInputPort( const QString& name )\r
+{\r
+  this->addPort( name, false );\r
+}\r
+\r
+// -------------------------------------------------------------------------\r
+void PipelineEditor::QNEBlock::\r
+addOutputPort( const QString& name )\r
+{\r
+  this->addPort( name, true );\r
+}\r
+\r
+// -------------------------------------------------------------------------\r
+void PipelineEditor::QNEBlock::\r
+addInputPorts( const QStringList& names )\r
+{\r
+  foreach( QString n, names )\r
+    this->addInputPort( n );\r
+}\r
+\r
+// -------------------------------------------------------------------------\r
+void PipelineEditor::QNEBlock::\r
+addOutputPorts( const QStringList& names )\r
+{\r
+  foreach( QString n, names )\r
+    this->addOutputPort( n );\r
+}\r
+\r
+// -------------------------------------------------------------------------\r
+void PipelineEditor::QNEBlock::\r
+save( QDataStream& ds )\r
+{\r
+  ds << pos( );\r
+\r
+  int count( 0 );\r
+\r
+  foreach( QGraphicsItem* port_, children( ) )\r
+  {\r
+    if( port_->type( ) != QNEPort::Type )\r
+      continue;\r
+    count++;\r
+\r
+  } // rof\r
+\r
+  ds << count;\r
+\r
+  foreach( QGraphicsItem* port_, children( ) )\r
+  {\r
+    if( port_->type( ) != QNEPort::Type )\r
+      continue;\r
+\r
+    QNEPort* port = ( QNEPort* ) port_;\r
+    ds << ( quint64 ) port;\r
+    ds << port->portName( );\r
+    ds << port->isOutput( );\r
+    ds << port->portFlags( );\r
+\r
+  } // rof\r
+}\r
+\r
+// -------------------------------------------------------------------------\r
+void PipelineEditor::QNEBlock::\r
+load( QDataStream& ds, QMap<quint64, QNEPort*>& portMap )\r
+{\r
+  QPointF p;\r
+  ds >> p;\r
+  this->setPos( p );\r
+  int count;\r
+  ds >> count;\r
+  for( int i = 0; i < count; i++ )\r
+  {\r
+    QString name;\r
+    bool output;\r
+    int flags;\r
+    quint64 ptr;\r
+\r
+    ds >> ptr;\r
+    ds >> name;\r
+    ds >> output;\r
+    ds >> flags;\r
+    portMap[ptr] = this->addPort( name, output, flags, ptr );\r
+\r
+  } // rof\r
+}\r
+\r
+// -------------------------------------------------------------------------\r
+void PipelineEditor::QNEBlock::\r
+paint(\r
+  QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget\r
+  )\r
+{\r
+  Q_UNUSED( option );\r
+  Q_UNUSED( widget );\r
+\r
+  if( this->isSelected( ) )\r
+  {\r
+    painter->setPen( QPen( Qt::darkYellow ) );\r
+    painter->setBrush( Qt::yellow );\r
+  }\r
+  else\r
+  {\r
+    painter->setPen( QPen( Qt::darkGreen ) );\r
+    painter->setBrush( Qt::green );\r
+\r
+  } // fi\r
+  painter->drawPath( this->path( ) );\r
+}\r
+\r
+// -------------------------------------------------------------------------\r
+PipelineEditor::QNEBlock* PipelineEditor::QNEBlock::\r
+clone( )\r
+{\r
+  QNEBlock* b = new QNEBlock( 0, this->scene( ) );\r
+\r
+  foreach( QGraphicsItem* port_, childItems( ) )\r
+  {\r
+    if( port_->type( ) == QNEPort::Type )\r
+    {\r
+      QNEPort* port = ( QNEPort* ) port_;\r
+      b->addPort(\r
+        port->portName( ),\r
+        port->isOutput( ),\r
+        port->portFlags( ),\r
+        port->ptr( )\r
+        );\r
+\r
+    } // fi\r
+\r
+  } // rof\r
+  return( b );\r
+}\r
+\r
+// -------------------------------------------------------------------------\r
+QVector< PipelineEditor::QNEPort* > PipelineEditor::QNEBlock::\r
+ports( )\r
+{\r
+  QVector< PipelineEditor::QNEPort* > res;\r
+  foreach( QGraphicsItem* port_, childItems( ) )\r
+  {\r
+    if( port_->type( ) == QNEPort::Type )\r
+      res.append( ( QNEPort* ) port_ );\r
+\r
+  } // rof\r
+  return( res );\r
+}\r
+\r
+// -------------------------------------------------------------------------\r
+QVariant PipelineEditor::QNEBlock::\r
+itemChange( GraphicsItemChange change, const QVariant& value )\r
+{\r
+  return( value );\r
+}\r
+\r
+// eof - $RCSfile$\r