/* Copyright (c) 2012, STANISLAW ADASZEWSKI All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of STANISLAW ADASZEWSKI nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANISLAW ADASZEWSKI BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "QNodesEditor.h" #include #include #include #include #include #include #include #include #include #include #include "QNEPort.h" #include "QNEConnection.h" #include "QNEBlock.h" // ------------------------------------------------------------------------- PipelineEditor::QNodesEditor:: QNodesEditor( QObject* parent ) : Superclass( parent ), m_Conn( NULL ), m_Workspace( NULL ) { } // ------------------------------------------------------------------------- PipelineEditor::QNodesEditor:: ~QNodesEditor( ) { } // ------------------------------------------------------------------------- PipelineEditor::QNodesEditor:: TWorkspace* PipelineEditor::QNodesEditor:: workspace( ) { return( this->m_Workspace ); } // ------------------------------------------------------------------------- const PipelineEditor::QNodesEditor:: TWorkspace* PipelineEditor::QNodesEditor:: workspace( ) const { return( this->m_Workspace ); } // ------------------------------------------------------------------------- void PipelineEditor::QNodesEditor:: setWorkspace( TWorkspace* ws ) { if( this->m_Workspace == ws ) return; this->m_Workspace = ws; this->m_Graph = TGraph::New( ); /* TODO QGraphicsScene* scene = this->scene( ); // Add vertices and keep track of ports std::map< std::string, std::map< std::string, QNEPort* > > in_ports, out_ports; auto vIt = this->m_Workspace->GetGraph( )->BeginVertices( ); auto vIt_end = this->m_Workspace->GetGraph( )->EndVertices( ); for( ; vIt != vIt_end; ++vIt ) { this->_createBlock( dynamic_cast< TFilter* >( vIt->second.GetPointer( ) ) ); } // rof */ // Add edges /* TODO auto rIt = this->m_Workspace->GetGraph( )->BeginEdgesRows( ); auto rIt_end = this->m_Workspace->GetGraph( )->EndEdgesRows( ); for( ; rIt != rIt_end; ++rIt ) { auto cIt = rIt->second.begin( ); for( ; cIt != rIt->second.end( ); ++cIt ) { auto eIt = cIt->second.begin( ); for( ; eIt != cIt->second.end( ); ++eIt ) { QNEPort* p1 = out_ports[ rIt->first ][ eIt->first ]; QNEPort* p2 = in_ports[ cIt->first ][ eIt->second ]; if( p1 != NULL && p2 != NULL ) { QNEConnection* conn = new QNEConnection( 0, scene ); conn->setPort1( p1 ); conn->setPort2( p2 ); this->m_Graph->AddConnection( rIt->first, cIt->first, conn ); } // fi } // rof } // rof } // rof */ } // ------------------------------------------------------------------------- std::string PipelineEditor::QNodesEditor:: createFilter( const std::string& filter, const QPointF& pnt ) { std::string name = filter; while( this->m_Workspace->HasFilter( name ) ) name += std::string( "_" ); if( this->m_Workspace->CreateFilter( filter, name ) ) { this->_CreateBlock( this->m_Workspace->GetFilter( name ), pnt ); return( name ); } else return( "" ); } // ------------------------------------------------------------------------- void PipelineEditor::QNodesEditor:: install( QGraphicsScene* s ) { s->installEventFilter( this ); this->m_Scene = s; } // ------------------------------------------------------------------------- QGraphicsItem* PipelineEditor::QNodesEditor:: itemAt( const QPointF& pos ) { QList< QGraphicsItem* > items = this->m_Scene->items( QRectF( pos - QPointF( 1, 1 ), QSize( 3, 3 ) ) ); foreach( QGraphicsItem* item, items ) if( item->type( ) > QGraphicsItem::UserType ) return( item ); return( NULL ); } #include // ------------------------------------------------------------------------- void PipelineEditor::QNodesEditor:: _CreateBlock( TFilter* f, const QPointF& pnt ) { if( f == NULL ) return; // Add block QNEBlock* b = new QNEBlock( 0, this->m_Scene ); b->setNamePort( f->GetName( ) ); b->setTypePort( f->GetClassName( ).c_str( ) ); // TODO: b->setScenePos( pnt ); // Add input ports std::set< std::string > inputs; f->GetInputsNames( inputs ); for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt ) b->addInputPort( iIt->c_str( ) ); // Add output ports std::set< std::string > outputs; f->GetOutputsNames( outputs ); for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt ) b->addOutputPort( oIt->c_str( ) ); // Keep a trace of this visual graph this->m_Graph->InsertVertex( f->GetName( ), b ); } // ------------------------------------------------------------------------- void PipelineEditor::QNodesEditor:: _DoubleClick( QGraphicsSceneMouseEvent* evt, QGraphicsItem* item ) { switch( evt->button( ) ) { case Qt::LeftButton: { QNEBlock* block = dynamic_cast< QNEBlock* >( item ); QNEPort* port = dynamic_cast< QNEPort* >( item ); QNEConnection* conn = dynamic_cast< QNEConnection* >( item ); if( block != NULL ) { /* TODO auto ports = block->ports( ); std::string name = ""; for( auto pIt = ports.begin( ); pIt != ports.end( ) && name == ""; ++pIt ) if( ( *pIt )->portFlags( ) && QNEPort::NamePort == QNEPort::NamePort ) name = ( *pIt )->portName( ).toStdString( ); if( name == "" ) return; TFilter* filter = this->m_Workspace->GetFilter( name ); if( filter != NULL ) { } // fi */ } else if( port != NULL ) { } else if( conn != NULL ) { } // fi } break; /* TODO: case Qt::RightButton: { } break; case Qt::MiddleButton: { } break; */ default: break; } // hctiws } // ------------------------------------------------------------------------- bool PipelineEditor::QNodesEditor:: eventFilter( QObject* o, QEvent* e ) { // Event type switch( int( e->type( ) ) ) { case QEvent::GraphicsSceneContextMenu: { QGraphicsSceneContextMenuEvent* evt = dynamic_cast< QGraphicsSceneContextMenuEvent* >( e ); if( evt != NULL ) { } // fi } break; case QEvent::GraphicsSceneDragEnter: { QGraphicsSceneDragDropEvent* evt = dynamic_cast< QGraphicsSceneDragDropEvent* >( e ); if( evt != NULL ) { } // fi } break; case QEvent::GraphicsSceneDragLeave: { QGraphicsSceneDragDropEvent* evt = dynamic_cast< QGraphicsSceneDragDropEvent* >( e ); if( evt != NULL ) { } // fi } break; case QEvent::GraphicsSceneDragMove: { QGraphicsSceneDragDropEvent* evt = dynamic_cast< QGraphicsSceneDragDropEvent* >( e ); if( evt != NULL ) { } // fi } break; case QEvent::GraphicsSceneDrop: { QGraphicsSceneDragDropEvent* evt = dynamic_cast< QGraphicsSceneDragDropEvent* >( e ); if( evt != NULL ) { } // fi } break; case QEvent::GraphicsSceneHelp: { QGraphicsSceneHelpEvent* evt = dynamic_cast< QGraphicsSceneHelpEvent* >( e ); if( evt != NULL ) { } // fi } break; case QEvent::GraphicsSceneHoverEnter: { QGraphicsSceneHoverEvent* evt = dynamic_cast< QGraphicsSceneHoverEvent* >( e ); if( evt != NULL ) { } // fi } break; case QEvent::GraphicsSceneHoverLeave: { QGraphicsSceneHoverEvent* evt = dynamic_cast< QGraphicsSceneHoverEvent* >( e ); if( evt != NULL ) { } // fi } break; case QEvent::GraphicsSceneHoverMove: { QGraphicsSceneHoverEvent* evt = dynamic_cast< QGraphicsSceneHoverEvent* >( e ); if( evt != NULL ) { } // fi } break; case QEvent::GraphicsSceneMouseDoubleClick: { QGraphicsSceneMouseEvent* evt = dynamic_cast< QGraphicsSceneMouseEvent* >( e ); if( evt != NULL ) { QGraphicsItem* item = this->itemAt( evt->scenePos( ) ); if( item != NULL ) { this->_DoubleClick( evt, item ); return( true ); } // fi } // fi } break; case QEvent::GraphicsSceneMouseMove: { QGraphicsSceneMouseEvent* evt = dynamic_cast< QGraphicsSceneMouseEvent* >( e ); if( evt != NULL ) { if( this->m_Conn ) { this->m_Conn->setPos2( evt->scenePos( ) ); this->m_Conn->updatePath( ); return( true ); } // fi } // fi } break; case QEvent::GraphicsSceneMousePress: { QGraphicsSceneMouseEvent* evt = dynamic_cast< QGraphicsSceneMouseEvent* >( e ); if( evt != NULL ) { switch( evt->button( ) ) { case Qt::LeftButton: { QNEOutputPort* port = dynamic_cast< QNEOutputPort* >( this->itemAt( evt->scenePos( ) ) ); if( port != NULL ) { this->m_Conn = new QNEConnection( 0, this->m_Scene ); this->m_Conn->setPort1( port ); this->m_Conn->setPos1( port->scenePos( ) ); this->m_Conn->setPos2( evt->scenePos( ) ); this->m_Conn->updatePath( ); return( true ); } // fi } break; default: break; } // hctiws } // fi } break; case QEvent::GraphicsSceneMouseRelease: { QGraphicsSceneMouseEvent* evt = dynamic_cast< QGraphicsSceneMouseEvent* >( e ); if( evt != NULL ) { if( this->m_Conn != NULL && evt->button( ) == Qt::LeftButton ) { QNEInputPort* port2 = dynamic_cast< QNEInputPort* >( this->itemAt( evt->scenePos( ) ) ); if( port2 != NULL ) { QNEOutputPort* port1 = dynamic_cast< QNEOutputPort* >( this->m_Conn->port1( ) ); if( port1 != NULL ) { if( port1->block( ) != port2->block( ) && !port2->hasConnection( ) && !port1->isConnected( port2 ) ) { this->m_Conn->setPos2( port2->scenePos( ) ); this->m_Conn->setPort2( port2 ); this->m_Conn->updatePath( ); this->m_Workspace->Connect( port1->block( )->namePort( )->name( ).toStdString( ), port2->block( )->namePort( )->name( ).toStdString( ), port1->name( ).toStdString( ), port2->name( ).toStdString( ) ); this->m_Graph->AddConnection( port1->block( )->namePort( )->name( ).toStdString( ), port2->block( )->namePort( )->name( ).toStdString( ), this->m_Conn ); this->m_Conn = NULL; return( true ); } // fi } // fi } // fi delete this->m_Conn; this->m_Conn = NULL; return( true ); } // fi } // fi } break; case QEvent::GraphicsSceneMove: { QGraphicsSceneMoveEvent* evt = dynamic_cast< QGraphicsSceneMoveEvent* >( e ); if( evt != NULL ) { } // fi } break; case QEvent::GraphicsSceneResize: { QGraphicsSceneResizeEvent* evt = dynamic_cast< QGraphicsSceneResizeEvent* >( e ); if( evt != NULL ) { } // fi } break; case QEvent::GraphicsSceneWheel: { QGraphicsSceneWheelEvent* evt = dynamic_cast< QGraphicsSceneWheelEvent* >( e ); if( evt != NULL ) { } // fi } break; default: break; } // hctiws // Mouse event /* QGraphicsSceneMouseEvent* me = dynamic_cast< QGraphicsSceneMouseEvent* >( e ); if( me != NULL ) { } // fi */ /* TODO switch( ( int ) e->type( ) ) { case QEvent::GraphicsSceneMouseMove: { if( this->m_Conn ) { this->m_Conn->setPos2( me->scenePos( ) ); this->m_Conn->updatePath( ); return( true ); } break; } case QEvent::GraphicsSceneMouseRelease: { if( this->m_Conn && me->button( ) == Qt::LeftButton ) { QGraphicsItem* item = itemAt( me->scenePos( ) ); if( item && item->type( ) == QNEPort::Type ) { QNEPort* port1 = this->m_Conn->port1( ); QNEPort* port2 = ( QNEPort* ) item; if( port1->block( ) != port2->block( ) && port1->isOutput( ) != port2->isOutput( ) && !port1->isConnected( port2 ) ) { this->m_Conn->setPos2( port2->scenePos( ) ); this->m_Conn->setPort2( port2 ); this->m_Conn->updatePath( ); this->m_Conn = NULL; return( true ); } } delete this->m_Conn; this->m_Conn = NULL; return( true ); } break; } } // hctiws */ return( this->Superclass::eventFilter( o, e ) ); } // eof - $RCSfile$