#include "Node.h" #include "Edge.h" #include "GraphCanvas.h" #include #include #include #include #include #define PORT_SIZE 10 // ------------------------------------------------------------------------- PipelineEditor::Node:: Node( GraphCanvas* canvas, cpPlugins::Interface::Object* object ) : QGraphicsItem( NULL ), m_Canvas( canvas ), m_Object( object ), m_UpdatedBounds( false ) { this->setFlag( QGraphicsItem::ItemIsMovable ); this->setFlag( QGraphicsItem::ItemSendsGeometryChanges ); this->setCacheMode( QGraphicsItem::DeviceCoordinateCache ); this->setAcceptHoverEvents( true ); this->setZValue( -1 ); this->setToolTip( this->m_Object->GetName( ) ); } // ------------------------------------------------------------------------- PipelineEditor::Node:: ~Node( ) { } // ------------------------------------------------------------------------- void PipelineEditor::Node:: addEdge( PipelineEditor::Edge* edge ) { this->m_Edges << edge; edge->adjust( ); } // ------------------------------------------------------------------------- QList< PipelineEditor::Edge* > PipelineEditor::Node:: edges( ) const { return( this->m_Edges ); } // ------------------------------------------------------------------------- QRectF PipelineEditor::Node:: boundingRect( ) const { typedef cpPlugins::Interface::ProcessObject _TFilter; if( !this->m_UpdatedBounds ) { // Text bounding box QFontMetricsF fm( this->m_Canvas->font( ) ); this->m_Label = this->m_Object->GetName( ); this->m_Label += "\n"; this->m_Label += this->m_Object->GetClassName( ).c_str( ); // Ports this->m_Bounds = fm.boundingRect( this->m_Label ); const _TFilter* f = dynamic_cast< const _TFilter* >( this->m_Object ); if( f != NULL ) { unsigned int nIn = f->GetNumberOfInputs( ); unsigned int nOut = f->GetNumberOfOutputs( ); qreal n = qreal( ( ( ( ( nIn > nOut )? nIn: nOut ) << 1 ) + 1 ) * PORT_SIZE ); qreal h = this->m_Bounds.height( ); if( n > h ) this->m_Bounds.setHeight( n ); // Let some space for ports this->m_Bounds.setLeft( this->m_Bounds.left( ) - qreal( PORT_SIZE ) ); this->m_Bounds.setTop( this->m_Bounds.top( ) - qreal( PORT_SIZE ) ); this->m_Bounds.setRight( this->m_Bounds.right( ) + qreal( PORT_SIZE ) ); this->m_Bounds.setBottom( this->m_Bounds.bottom( ) + qreal( PORT_SIZE ) ); } // fi this->m_UpdatedBounds = true; } // fi return( this->m_Bounds ); } // ------------------------------------------------------------------------- QPainterPath PipelineEditor::Node:: shape( ) const { QPainterPath path; path.addRect( this->boundingRect( ) ); return( path ); } // ------------------------------------------------------------------------- void PipelineEditor::Node:: paint( QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget ) { typedef cpPlugins::Interface::ProcessObject _TFilter; QRectF rect = this->boundingRect( ); painter->drawRect( rect ); painter->drawText( rect, Qt::AlignCenter, this->m_Label ); // Show ports const _TFilter* f = dynamic_cast< const _TFilter* >( this->m_Object ); if( f != NULL ) { QSizeF port_size( qreal( PORT_SIZE ), qreal( PORT_SIZE ) ); qreal rh = rect.height( ); qreal rt = rect.top( ); qreal rl = rect.left( ); qreal rr = rect.right( ); std::set< std::string > inputs, outputs; f->GetInputsNames( inputs ); f->GetOutputsNames( outputs ); qreal oh = qreal( ( ( inputs.size( ) << 1 ) + 1 ) * PORT_SIZE ); qreal off = qreal( PORT_SIZE ); if( rh > oh ) off += ( rh - oh ) / qreal( 2 ); for( auto it = inputs.begin( ); it != inputs.end( ); ++it ) { painter->drawRect( QRectF( QPointF( rl, rt + off ), port_size ) ); off += qreal( PORT_SIZE < 1 ); } // rof oh = qreal( ( ( outputs.size( ) << 1 ) + 1 ) * PORT_SIZE ); off = qreal( PORT_SIZE ); if( rh > oh ) off += ( rh - oh ) / qreal( 2 ); for( auto it = outputs.begin( ); it != outputs.end( ); ++it ) { painter->drawRect( QRectF( QPointF( rr - qreal( PORT_SIZE ), rt + off ), port_size ) ); off += qreal( PORT_SIZE < 1 ); } // rof } // fi } // ------------------------------------------------------------------------- QVariant PipelineEditor::Node:: itemChange( GraphicsItemChange change, const QVariant& value ) { /* TODO switch( change ) { case QGraphicsItem::ItemPositionHasChanged: foreach( Edge* edge, this->m_Edges ) edge->adjust( ); this->m_Canvas->itemMoved( ); break; default: break; } // hctiws */ return( this->QGraphicsItem::itemChange( change, value ) ); } // ------------------------------------------------------------------------- void PipelineEditor::Node:: mousePressEvent( QGraphicsSceneMouseEvent* event ) { this->update( ); this->QGraphicsItem::mousePressEvent( event ); } // ------------------------------------------------------------------------- void PipelineEditor::Node:: mouseReleaseEvent( QGraphicsSceneMouseEvent* event ) { this->update( ); this->QGraphicsItem::mouseReleaseEvent( event ); } // ------------------------------------------------------------------------- void PipelineEditor::Node:: mouseDoubleClickEvent( QGraphicsSceneMouseEvent* event ) { } // ------------------------------------------------------------------------- void PipelineEditor::Node:: hoverMoveEvent( QGraphicsSceneHoverEvent* event ) { QPointF pos = event->pos( ); } /* TODO private: GraphCanvas* m_Canvas; QList< Edge* > m_Edges; std::string m_Label; }; } // ecapseman #endif // __PIPELINEEDITOR__NODE__H__ */ // eof - $RCSfile$ /* QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) { switch (change) { case ItemPositionHasChanged: foreach (Edge *edge, edgeList) edge->adjust(); graph->itemMoved(); break; default: break; }; return QGraphicsItem::itemChange(change, value); } void Node::mousePressEvent(QGraphicsSceneMouseEvent *event) { update(); QGraphicsItem::mousePressEvent(event); } void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { update(); QGraphicsItem::mouseReleaseEvent(event); } // eof - $RCSfile$ */