#include #include #include #include #include #include #include #include #include #include #include #include #include #include // ------------------------------------------------------------------------- cpBaseQtApplication::Block:: Block( TFilter* filter, const QString& name, QGraphicsItem* parent, QGraphicsScene* scene ) : Superclass( parent, scene ), m_HorzMargin( 20 ), m_VertMargin( 5 ), m_NamePort( NULL ), m_Filter( filter ), m_Editor( NULL ) { QPainterPath p; p.addRoundedRect( -50, -15, 100, 30, 5, 5 ); this->setPath( p ); this->setPen( QPen( Qt::darkGreen ) ); this->setBrush( Qt::green ); this->setFlag( QGraphicsItem::ItemIsMovable ); this->setFlag( QGraphicsItem::ItemIsSelectable ); this->m_Width = this->m_HorzMargin; this->m_Height = this->m_VertMargin; // Configure names this->setNamePort( name ); this->_setTypeInfo( ( std::string( this->m_Filter->GetClassCategory( ) ) + std::string( "::" ) + std::string( this->m_Filter->GetClassName( ) ) ).c_str( ) ); // Add input ports auto inputs = this->m_Filter->GetInputsNames( ); for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt ) this->addInputPort( iIt->c_str( ) ); // Add output ports auto outputs = this->m_Filter->GetOutputsNames( ); for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt ) this->addOutputPort( oIt->c_str( ) ); } // ------------------------------------------------------------------------- cpBaseQtApplication::Block:: ~Block( ) { } // ------------------------------------------------------------------------- cpBaseQtApplication::Block:: TFilter* cpBaseQtApplication::Block:: filter( ) { return( this->m_Filter ); } // ------------------------------------------------------------------------- const cpBaseQtApplication::Block:: TFilter* cpBaseQtApplication::Block:: filter( ) const { return( this->m_Filter ); } // ------------------------------------------------------------------------- cpBaseQtApplication::Editor* cpBaseQtApplication::Block:: editor( ) { return( this->m_Editor ); } // ------------------------------------------------------------------------- const cpBaseQtApplication::Editor* cpBaseQtApplication::Block:: editor( ) const { return( this->m_Editor ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::Block:: setEditor( cpBaseQtApplication::Editor* editor ) { this->m_Editor = editor; } // ------------------------------------------------------------------------- void cpBaseQtApplication::Block:: setNamePort( const QString& txt ) { if( this->m_NamePort == NULL ) this->m_NamePort = new NamePort( this ); this->m_NamePort->setName( txt ); this->_configPort( this->m_NamePort ); } // ------------------------------------------------------------------------- cpBaseQtApplication::InputPort* cpBaseQtApplication::Block:: addInputPort( const QString& txt ) { InputPort* ip = new InputPort( this ); ip->setExtendedName( "" ); ip->setName( txt ); this->m_InputPorts[ txt.toStdString( ) ] = ip; this->_configPort( ip ); return( ip ); } // ------------------------------------------------------------------------- cpBaseQtApplication::OutputPort* cpBaseQtApplication::Block:: addOutputPort( const QString& txt ) { OutputPort* op = new OutputPort( this ); op->setExtendedName( "" ); op->setName( txt ); this->m_OutputPorts[ txt.toStdString( ) ] = op; this->_configPort( op ); return( op ); } // ------------------------------------------------------------------------- cpBaseQtApplication::InputPort* cpBaseQtApplication::Block:: inputPort( const QString& txt ) { auto i = this->m_InputPorts.find( txt.toStdString( ) ); if( i != this->m_InputPorts.end( ) ) return( i->second ); else return( NULL ); } // ------------------------------------------------------------------------- cpBaseQtApplication::OutputPort* cpBaseQtApplication::Block:: outputPort( const QString& txt ) { auto o = this->m_OutputPorts.find( txt.toStdString( ) ); if( o != this->m_OutputPorts.end( ) ) return( o->second ); else return( NULL ); } // ------------------------------------------------------------------------- QString cpBaseQtApplication::Block:: namePort( ) const { return( this->m_NamePort->name( ) ); } // ------------------------------------------------------------------------- const cpBaseQtApplication::InputPort* cpBaseQtApplication::Block:: inputPort( const QString& txt ) const { auto i = this->m_InputPorts.find( txt.toStdString( ) ); if( i != this->m_InputPorts.end( ) ) return( i->second ); else return( NULL ); } // ------------------------------------------------------------------------- const cpBaseQtApplication::OutputPort* cpBaseQtApplication::Block:: outputPort( const QString& txt ) const { auto o = this->m_OutputPorts.find( txt.toStdString( ) ); if( o != this->m_OutputPorts.end( ) ) return( o->second ); else return( NULL ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::Block:: paint( QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget ) { Q_UNUSED( option ); Q_UNUSED( widget ); if( this->isSelected( ) ) { painter->setPen( QPen( Qt::darkYellow ) ); painter->setBrush( Qt::yellow ); } else { painter->setPen( QPen( Qt::darkGreen ) ); painter->setBrush( Qt::green ); } // fi painter->drawPath( this->path( ) ); } // ------------------------------------------------------------------------- QVariant cpBaseQtApplication::Block:: itemChange( GraphicsItemChange change, const QVariant& value ) { return( value ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::Block:: _setTypeInfo( const QString& txt ) { this->setToolTip( txt ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::Block:: _configPort( Port* port ) { port->setBlock( this ); QFontMetrics fm( this->scene( )->font( ) ); this->m_Width = 0; foreach( QGraphicsItem* i, this->children( ) ) { Port* p = dynamic_cast< Port* >( i ); if( p == NULL ) continue; int w = fm.width( p->name( ) ) + ( 4 * p->radius( ) ); if( w > this->m_Width - this->m_HorzMargin ) this->m_Width = w + this->m_HorzMargin; } // rof int h = fm.height( ); this->m_Height = this->m_InputPorts.size( ) + this->m_OutputPorts.size( ); this->m_Height += 3; this->m_Height *= h; QPainterPath pth; pth.addRoundedRect( -this->m_Width / 2, -this->m_Height / 2, this->m_Width, this->m_Height, 5, 5 ); this->setPath( pth ); int y = -this->m_Height / 2 + this->m_VertMargin + port->radius( ); foreach( QGraphicsItem* i, this->children( ) ) { Port* p = dynamic_cast< Port* >( i ); if( p == NULL ) continue; if( dynamic_cast< NamePort* >( i ) != NULL ) { i->setPos( -this->m_Width / 2 + port->radius( ), y ); y += h; } else if( dynamic_cast< InputPort* >( i ) != NULL ) i->setPos( -this->m_Width / 2 - 2 * port->radius( ), y ); else if( dynamic_cast< OutputPort* >( i ) != NULL ) i->setPos( this->m_Width / 2, y ); y += h; } // rof } // ------------------------------------------------------------------------- void cpBaseQtApplication::Block:: mouseReleaseEvent( QGraphicsSceneMouseEvent* evt ) { if( this->m_Filter.IsNotNull( ) ) this->m_Filter->SetViewCoords( this->scenePos( ).x( ), this->scenePos( ).y( ) ); this->Superclass::mouseReleaseEvent( evt ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::Block:: contextMenuEvent( QGraphicsSceneContextMenuEvent* evt ) { QMenu menu; QAction* configureAction = menu.addAction( "Configure" ); QAction* updateAction = menu.addAction( "Update" ); auto widget = dynamic_cast< cpPlugins::BaseObjects::Widget* >( this->m_Filter.GetPointer( ) ); QAction* enableAction = NULL; if( widget != NULL ) enableAction = menu.addAction( ( widget->GetEnabled( ) )? "Disable": "Enable" ); QAction* selectedAction = menu.exec( evt->screenPos( ) ); if( selectedAction == configureAction ) { auto dlg = this->m_Filter->CreateQDialog( ); if( dlg != NULL ) dlg->exec( ); } else if( selectedAction == enableAction ) { if( widget != NULL ) widget->SetEnabled( !( widget->GetEnabled( ) ) ); } else if( selectedAction == updateAction ) this->m_Editor->updateFilter( this->namePort( ).toStdString( ) ); } // eof - $RCSfile$