From 2c372f652bce67b347cd91737916504a054604a3 Mon Sep 17 00:00:00 2001 From: Leonardo Florez-Valencia Date: Wed, 16 Dec 2015 18:43:20 -0500 Subject: [PATCH] ... --- appli/cpPipelineEditor/CMakeLists.txt | 10 +- appli/cpPipelineEditor/GraphCanvas.cxx | 318 ------------- appli/cpPipelineEditor/GraphCanvas.h | 66 --- appli/cpPipelineEditor/QNEBlock.cxx | 8 +- appli/cpPipelineEditor/QNEBlock.h | 4 +- appli/cpPipelineEditor/QNodesEditor.cxx | 27 +- appli/cpPipelineEditor/QNodesEditor.h | 7 +- appli/cpPipelineEditor/QNodesEditorCanvas.cxx | 425 ++++++++++++++++++ appli/cpPipelineEditor/QNodesEditorCanvas.h | 62 +++ appli/cpPipelineEditor/cpPipelineEditor.cxx | 132 ++++++ appli/cpPipelineEditor/cpPipelineEditor.h | 5 + appli/cpPipelineEditor/cpPipelineEditor.ui | 149 +++++- appli/cpPipelineEditor/main.cxx | 4 +- 13 files changed, 801 insertions(+), 416 deletions(-) delete mode 100644 appli/cpPipelineEditor/GraphCanvas.cxx delete mode 100644 appli/cpPipelineEditor/GraphCanvas.h create mode 100644 appli/cpPipelineEditor/QNodesEditorCanvas.cxx create mode 100644 appli/cpPipelineEditor/QNodesEditorCanvas.h diff --git a/appli/cpPipelineEditor/CMakeLists.txt b/appli/cpPipelineEditor/CMakeLists.txt index b978bcd..14874c5 100644 --- a/appli/cpPipelineEditor/CMakeLists.txt +++ b/appli/cpPipelineEditor/CMakeLists.txt @@ -14,11 +14,12 @@ IF(USE_QT4) QNEConnection.cxx QNEPort.cxx QNodesEditor.cxx - QNEMainWindow.cxx + QNodesEditorCanvas.cxx + #QNEMainWindow.cxx #Edge.cxx #GraphCanvas.cxx #Node.cxx - #cpPipelineEditor.cxx + cpPipelineEditor.cxx ) SET( App_SOURCES @@ -27,9 +28,10 @@ IF(USE_QT4) SET( App_QT_HEADERS QNodesEditor.h - QNEMainWindow.h + QNodesEditorCanvas.h + #QNEMainWindow.h #GraphCanvas.h - #cpPipelineEditor.h + cpPipelineEditor.h ) SET( App_HEADERS diff --git a/appli/cpPipelineEditor/GraphCanvas.cxx b/appli/cpPipelineEditor/GraphCanvas.cxx deleted file mode 100644 index 3b3041c..0000000 --- a/appli/cpPipelineEditor/GraphCanvas.cxx +++ /dev/null @@ -1,318 +0,0 @@ -#include "GraphCanvas.h" -#include "Edge.h" -#include "Node.h" - -#include - -#include - -// ------------------------------------------------------------------------- -PipelineEditor::GraphCanvas:: -GraphCanvas( QWidget* parent ) - : QGraphicsView( parent ), - m_Workspace( NULL ) -{ - QGraphicsScene* scene = new QGraphicsScene( this ); - scene->setItemIndexMethod( QGraphicsScene::NoIndex ); - this->setScene( scene ); -} - -// ------------------------------------------------------------------------- -PipelineEditor::GraphCanvas:: -~GraphCanvas( ) -{ -} - -// ------------------------------------------------------------------------- -cpPlugins::Interface::Workspace* PipelineEditor::GraphCanvas:: -workspace( ) -{ - return( this->m_Workspace ); -} - -// ------------------------------------------------------------------------- -const cpPlugins::Interface::Workspace* PipelineEditor::GraphCanvas:: -workspace( ) const -{ - return( this->m_Workspace ); -} - -// ------------------------------------------------------------------------- -void PipelineEditor::GraphCanvas:: -setWorkspace( cpPlugins::Interface::Workspace* ws ) -{ - if( this->m_Workspace == ws ) - return; - this->m_Workspace = ws; - QGraphicsScene* scene = this->scene( ); - - // Create graph - this->m_Graph = TGraph::New( ); - - // Add vertices - auto vIt = this->m_Workspace->GetGraph( )->BeginVertices( ); - auto vIt_end = this->m_Workspace->GetGraph( )->EndVertices( ); - for( ; vIt != vIt_end; ++vIt ) - { - std::string label = vIt->second->GetName( ) + std::string( "\n" ); - label += vIt->second->GetClassName( ); - - Node* node = new Node( this, vIt->second.GetPointer( ) ); - this->m_Graph->InsertVertex( vIt->first, node ); - scene->addItem( node ); - - } // rof - - /* TODO - // Add edges - auto rIt = this->m_Workspace->GetGraph( )->BeginEdgesRows( ); - auto rIt_end = this->m_Workspace->GetGraph( )->EndEdgesRows( ); - for( ; rIt != rIt_end; ++rIt ) - { - Node* a = this->m_Graph->GetVertex( rIt->first ); - if( a == NULL ) - continue; - auto cIt = rIt->second.begin( ); - for( ; cIt != rIt->second.end( ); ++cIt ) - { - Node* b = this->m_Graph->GetVertex( cIt->first ); - if( b == NULL ) - continue; - Edge* e = new Edge( a, b ); - this->m_Graph->AddConnection( rIt->first, cIt->first, e ); - scene->addItem( e ); - - } // rof - - } // rof - */ -} - -// ------------------------------------------------------------------------- -void PipelineEditor::GraphCanvas:: -draw( ) -{ - if( this->m_Workspace == NULL ) - return; -} - -// ------------------------------------------------------------------------- -void PipelineEditor::GraphCanvas:: -itemMoved( ) -{ -} - -// ------------------------------------------------------------------------- -void PipelineEditor::GraphCanvas:: -shuffle( ) -{ -} - -// ------------------------------------------------------------------------- -void PipelineEditor::GraphCanvas:: -zoomIn( ) -{ -} - -// ------------------------------------------------------------------------- -void PipelineEditor::GraphCanvas:: -zoomOut( ) -{ -} - -// ------------------------------------------------------------------------- -void PipelineEditor::GraphCanvas:: -keyPressEvent( QKeyEvent* event ) -{ -} - -// ------------------------------------------------------------------------- -void PipelineEditor::GraphCanvas:: -timerEvent( QTimerEvent* event ) -{ -} - -// ------------------------------------------------------------------------- -void PipelineEditor::GraphCanvas:: -wheelEvent( QWheelEvent* event ) -{ - this->scaleView( - std::pow( double( 2 ), -event->delta( ) / double( 240 ) ) - ); -} - -// ------------------------------------------------------------------------- -void PipelineEditor::GraphCanvas:: -drawBackground( QPainter* painter, const QRectF& rect ) -{ - this->QGraphicsView::drawBackground( painter, rect ); -} - -// ------------------------------------------------------------------------- -void PipelineEditor::GraphCanvas:: -scaleView( qreal scaleFactor ) -{ - qreal factor = this->transform( ). - scale( scaleFactor, scaleFactor ). - mapRect( QRectF( 0, 0, 1, 1 ) ). - width( ); - if( factor < qreal( 0.07 ) || factor > qreal( 100 ) ) - return; - this->scale( scaleFactor, scaleFactor ); -} - -/* - protected: - cpPlugins::Interface::Workspace* m_Workspace; - TGraph::Pointer m_Graph; - }; -*/ - -// eof - $RCSfile$ - - -/* - -// ------------------------------------------------------------------------- -void GraphWidget:: -draw( ) -{ - if( this->m_Workspace == NULL ) - return; -} - -// ------------------------------------------------------------------------- -void GraphWidget::itemMoved() -{ - if (!timerId) - timerId = startTimer(1000 / 25); -} - -void GraphWidget::keyPressEvent(QKeyEvent *event) -{ - switch (event->key()) { - case Qt::Key_Up: - centerNode->moveBy(0, -20); - break; - case Qt::Key_Down: - centerNode->moveBy(0, 20); - break; - case Qt::Key_Left: - centerNode->moveBy(-20, 0); - break; - case Qt::Key_Right: - centerNode->moveBy(20, 0); - break; - case Qt::Key_Plus: - zoomIn(); - break; - case Qt::Key_Minus: - zoomOut(); - break; - case Qt::Key_Space: - case Qt::Key_Enter: - shuffle(); - break; - default: - QGraphicsView::keyPressEvent(event); - } -} - -void GraphWidget::timerEvent(QTimerEvent *event) -{ - Q_UNUSED(event); - - QList nodes; - foreach (QGraphicsItem *item, scene()->items()) { - if (Node *node = qgraphicsitem_cast(item)) - nodes << node; - } - - foreach (Node *node, nodes) - node->calculateForces(); - - bool itemsMoved = false; - foreach (Node *node, nodes) { - if (node->advance()) - itemsMoved = true; - } - - if (!itemsMoved) { - killTimer(timerId); - timerId = 0; - } -} - -void GraphWidget::wheelEvent(QWheelEvent *event) -{ - scaleView(pow((double)2, -event->delta() / 240.0)); -} - -void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect) -{ - //Q_UNUSED(rect); - - // Shadow - QRectF sceneRect = rect;//this->sceneRect(); - QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height()); - QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5); - if (rightShadow.intersects(rect) || rightShadow.contains(rect)) - painter->fillRect(rightShadow, Qt::darkGray); - if (bottomShadow.intersects(rect) || bottomShadow.contains(rect)) - painter->fillRect(bottomShadow, Qt::darkGray); - - // Fill - QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight()); - gradient.setColorAt(0, Qt::white); - gradient.setColorAt(1, Qt::lightGray); - painter->fillRect(rect.intersect(sceneRect), gradient); - painter->setBrush(Qt::NoBrush); - painter->drawRect(sceneRect); - -#if !defined(Q_OS_SYMBIAN) && !defined(Q_WS_MAEMO_5) - // Text - QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4, - sceneRect.width() - 4, sceneRect.height() - 4); - QString message(tr("Click and drag the nodes around, and zoom with the mouse " - "wheel or the '+' and '-' keys")); - - QFont font = painter->font(); - font.setBold(true); - font.setPointSize(14); - painter->setFont(font); - painter->setPen(Qt::lightGray); - painter->drawText(textRect.translated(2, 2), message); - painter->setPen(Qt::black); - painter->drawText(textRect, message); -#endif -} - -void GraphWidget::scaleView(qreal scaleFactor) -{ - qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width(); - if (factor < 0.07 || factor > 100) - return; - - scale(scaleFactor, scaleFactor); -} - -void GraphWidget::shuffle() -{ - foreach (QGraphicsItem *item, scene()->items()) { - if (qgraphicsitem_cast(item)) - item->setPos(-150 + qrand() % 300, -150 + qrand() % 300); - } -} - -void GraphWidget::zoomIn() -{ - scaleView(qreal(1.2)); -} - -void GraphWidget::zoomOut() -{ - scaleView(1 / qreal(1.2)); -} - - -*/ diff --git a/appli/cpPipelineEditor/GraphCanvas.h b/appli/cpPipelineEditor/GraphCanvas.h deleted file mode 100644 index 3a4a9d0..0000000 --- a/appli/cpPipelineEditor/GraphCanvas.h +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef __PIPELINEEDITOR__GRAPHCANVAS__H__ -#define __PIPELINEEDITOR__GRAPHCANVAS__H__ - -#include -#include - -// Some forward declarations -namespace cpPlugins -{ - namespace Interface - { - class Workspace; - } -} - -namespace PipelineEditor -{ - // Some other forward declarations - class Node; - class Edge; - - /** - */ - class GraphCanvas - : public QGraphicsView - { - Q_OBJECT; - - public: - typedef - cpExtensions::DataStructures::Graph< Node*, Edge*, std::string > TGraph; - - public: - GraphCanvas( QWidget* parent = 0 ); - virtual ~GraphCanvas( ); - - cpPlugins::Interface::Workspace* workspace( ); - const cpPlugins::Interface::Workspace* workspace( ) const; - void setWorkspace( cpPlugins::Interface::Workspace* ws ); - - void draw( ); - - void itemMoved( ); - - public slots: - void shuffle( ); - void zoomIn( ); - void zoomOut( ); - - protected: - void keyPressEvent( QKeyEvent* event ); - void timerEvent( QTimerEvent* event ); - void wheelEvent( QWheelEvent* event ); - void drawBackground( QPainter* painter, const QRectF& rect ); - void scaleView( qreal scaleFactor ); - - protected: - cpPlugins::Interface::Workspace* m_Workspace; - TGraph::Pointer m_Graph; - }; - -} // ecapseman - -#endif // __PIPELINEEDITOR__GRAPHCANVAS__H__ - -// eof - $RCSfile$ diff --git a/appli/cpPipelineEditor/QNEBlock.cxx b/appli/cpPipelineEditor/QNEBlock.cxx index d1e0109..7ccb166 100644 --- a/appli/cpPipelineEditor/QNEBlock.cxx +++ b/appli/cpPipelineEditor/QNEBlock.cxx @@ -103,17 +103,17 @@ addPort( const QString& name, bool isOutput, int flags, int ptr ) } // ------------------------------------------------------------------------- -void PipelineEditor::QNEBlock:: +PipelineEditor::QNEPort* PipelineEditor::QNEBlock:: addInputPort( const QString& name ) { - this->addPort( name, false ); + return( this->addPort( name, false ) ); } // ------------------------------------------------------------------------- -void PipelineEditor::QNEBlock:: +PipelineEditor::QNEPort* PipelineEditor::QNEBlock:: addOutputPort( const QString& name ) { - this->addPort( name, true ); + return( this->addPort( name, true ) ); } // ------------------------------------------------------------------------- diff --git a/appli/cpPipelineEditor/QNEBlock.h b/appli/cpPipelineEditor/QNEBlock.h index 516e871..1f3d651 100644 --- a/appli/cpPipelineEditor/QNEBlock.h +++ b/appli/cpPipelineEditor/QNEBlock.h @@ -48,8 +48,8 @@ namespace PipelineEditor QNEPort* addPort( const QString& name, bool isOutput, int flags = 0, int ptr = 0 ); - void addInputPort( const QString& name ); - void addOutputPort( const QString& name ); + QNEPort* addInputPort( const QString& name ); + QNEPort* addOutputPort( const QString& name ); void addInputPorts( const QStringList& names ); void addOutputPorts( const QStringList& names ); void save( QDataStream& ds ); diff --git a/appli/cpPipelineEditor/QNodesEditor.cxx b/appli/cpPipelineEditor/QNodesEditor.cxx index 9669eea..91a5914 100644 --- a/appli/cpPipelineEditor/QNodesEditor.cxx +++ b/appli/cpPipelineEditor/QNodesEditor.cxx @@ -77,19 +77,19 @@ eventFilter( QObject* o, QEvent* e ) switch ( ( int ) e->type( ) ) { - case QEvent::GraphicsThis->M_SceneMousePress: + case QEvent::GraphicsSceneMousePress: { switch ( ( int ) me->button( ) ) { case Qt::LeftButton: { - QGraphicsItem* item = this->itemAt( me->this->m_ScenePos( ) ); + QGraphicsItem* item = this->itemAt( me->scenePos( ) ); if( item && item->type( ) == QNEPort::Type ) { this->m_Conn = new QNEConnection( 0, this->m_Scene ); this->m_Conn->setPort1( ( QNEPort* ) item ); - this->m_Conn->setPos1( item->this->m_ScenePos( ) ); - this->m_Conn->setPos2( me->this->m_ScenePos( ) ); + this->m_Conn->setPos1( item->scenePos( ) ); + this->m_Conn->setPos2( me->scenePos( ) ); this->m_Conn->updatePath( ); return( true ); @@ -105,7 +105,7 @@ eventFilter( QObject* o, QEvent* e ) } case Qt::RightButton: { - QGraphicsItem* item = itemAt( me->this->m_ScenePos( ) ); + QGraphicsItem* item = itemAt( me->scenePos( ) ); if( item && ( item->type( ) == QNEConnection::Type || item->type( ) == QNEBlock::Type ) ) delete item; // if( selBlock == ( QNEBlock* ) item ) @@ -114,29 +114,29 @@ eventFilter( QObject* o, QEvent* e ) } } } - case QEvent::GraphicsThis->M_SceneMouseMove: + case QEvent::GraphicsSceneMouseMove: { if( this->m_Conn ) { - this->m_Conn->setPos2( me->this->m_ScenePos( ) ); + this->m_Conn->setPos2( me->scenePos( ) ); this->m_Conn->updatePath( ); return( true ); } break; } - case QEvent::GraphicsThis->M_SceneMouseRelease: + case QEvent::GraphicsSceneMouseRelease: { if( this->m_Conn && me->button( ) == Qt::LeftButton ) { - QGraphicsItem* item = itemAt( me->this->m_ScenePos( ) ); + 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->isThis->M_Connected( port2 ) ) + if( port1->block( ) != port2->block( ) && port1->isOutput( ) != port2->isOutput( ) && !port1->isConnected( port2 ) ) { - this->m_Conn->setPos2( port2->this->m_ScenePos( ) ); + this->m_Conn->setPos2( port2->scenePos( ) ); this->m_Conn->setPort2( port2 ); this->m_Conn->updatePath( ); this->m_Conn = NULL; @@ -189,9 +189,10 @@ load( QDataStream& ds ) { QNEBlock* block = new QNEBlock( 0, this->m_Scene ); block->load( ds, portMap ); - } else if( type == QNEConnection::Type ) + } + else if( type == QNEConnection::Type ) { - QNEConnection* this->m_Conn = new QNEConnection( 0, this->m_Scene ); + this->m_Conn = new QNEConnection( 0, this->m_Scene ); this->m_Conn->load( ds, portMap ); } } diff --git a/appli/cpPipelineEditor/QNodesEditor.h b/appli/cpPipelineEditor/QNodesEditor.h index 3e90b50..87c5a20 100644 --- a/appli/cpPipelineEditor/QNodesEditor.h +++ b/appli/cpPipelineEditor/QNodesEditor.h @@ -28,12 +28,13 @@ #include +class QGraphicsScene; +class QGraphicsItem; +class QPointF; + namespace PipelineEditor { - class QGraphicsScene; class QNEConnection; - class QGraphicsItem; - class QPointF; class QNEBlock; /** diff --git a/appli/cpPipelineEditor/QNodesEditorCanvas.cxx b/appli/cpPipelineEditor/QNodesEditorCanvas.cxx new file mode 100644 index 0000000..6d970ad --- /dev/null +++ b/appli/cpPipelineEditor/QNodesEditorCanvas.cxx @@ -0,0 +1,425 @@ +#include "QNodesEditorCanvas.h" +#include "QNodesEditor.h" +#include "QNEBlock.h" +#include "QNEConnection.h" +#include "QNEPort.h" + +#include +#include +#include + +// ------------------------------------------------------------------------- +PipelineEditor::QNodesEditorCanvas:: +QNodesEditorCanvas( QWidget* parent ) + : QGraphicsView( parent ), + m_Workspace( NULL ) +{ + QGraphicsScene* scene = new QGraphicsScene( this ); + this->setScene( scene ); + this->setRenderHint( QPainter::Antialiasing ); + this->setAcceptDrops( true ); + + this->m_Editor = new QNodesEditor( this ); + this->m_Editor->install( scene ); +} + +// ------------------------------------------------------------------------- +PipelineEditor::QNodesEditorCanvas:: +~QNodesEditorCanvas( ) +{ +} + +// ------------------------------------------------------------------------- +PipelineEditor::QNodesEditorCanvas:: +TWorkspace* PipelineEditor::QNodesEditorCanvas:: +workspace( ) +{ + return( this->m_Workspace ); +} + +// ------------------------------------------------------------------------- +const PipelineEditor::QNodesEditorCanvas:: +TWorkspace* PipelineEditor::QNodesEditorCanvas:: +workspace( ) const +{ + return( this->m_Workspace ); +} + +// ------------------------------------------------------------------------- +void PipelineEditor::QNodesEditorCanvas:: +setWorkspace( TWorkspace* ws ) +{ + if( this->m_Workspace == ws ) + return; + this->m_Workspace = ws; + QGraphicsScene* scene = this->scene( ); + + // Create graph + this->m_Graph = TGraph::New( ); + + // 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( ) ) ); +#error ACA VOY + // Add block + QNEBlock* b = new QNEBlock( 0, scene ); + b->addPort( vIt->second->GetName( ), 0, QNEPort::NamePort ); + b->addPort( vIt->second->GetClassName( ).c_str( ), 0, QNEPort::TypePort ); + + // Get filter + if( f == NULL ) + continue; + + // Add input ports + std::set< std::string > inputs; + f->GetInputsNames( inputs ); + for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt ) + in_ports[ vIt->first ][ *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 ) + out_ports[ vIt->first ][ *oIt ] = b->addOutputPort( oIt->c_str( ) ); + + // Keep a trace of this visual graph + this->m_Graph->InsertVertex( vIt->first, b ); + + } // 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 + */ +} + +// ------------------------------------------------------------------------- +/* TODO + void PipelineEditor::QNodesEditorCanvas:: + keyPressEvent( QKeyEvent* event ) + { + } + + // ------------------------------------------------------------------------- + void PipelineEditor::QNodesEditorCanvas:: + timerEvent( QTimerEvent* event ) + { + } +*/ + +// ------------------------------------------------------------------------- +void PipelineEditor::QNodesEditorCanvas:: +wheelEvent( QWheelEvent* event ) +{ + this->_scaleView( + std::pow( double( 2 ), event->delta( ) / double( 240 ) ) + ); +} + +// ------------------------------------------------------------------------- +void PipelineEditor::QNodesEditorCanvas:: +dragEnterEvent( QDragEnterEvent* event ) +{ + const QMimeData* mime = event->mimeData( ); + if( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) ) + event->acceptProposedAction( ); +} + +// ------------------------------------------------------------------------- +void PipelineEditor::QNodesEditorCanvas:: +dragLeaveEvent( QDragLeaveEvent* event ) +{ +} + +// ------------------------------------------------------------------------- +void PipelineEditor::QNodesEditorCanvas:: +dragMoveEvent( QDragMoveEvent* event ) +{ +} + +// ------------------------------------------------------------------------- +void PipelineEditor::QNodesEditorCanvas:: +dropEvent( QDropEvent* event ) +{ + if( this->m_Workspace == NULL ) + return; + const QMimeData* mime = event->mimeData( ); + if( !( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) ) ) + return; + + event->acceptProposedAction( ); + auto tree = dynamic_cast< QTreeWidget* >( event->source( ) ); + if( tree == NULL ) + return; + + QList< QTreeWidgetItem* > items = tree->selectedItems( ); + for( auto iIt = items.begin( ); iIt != items.end( ); ++iIt ) + { + std::string filter = ( *iIt )->text( 0 ).toStdString( ); + std::string name = filter; + if( this->m_Workspace->GetFilter( name ) != NULL ) + name += std::string( "_" ); + if( this->m_Workspace->CreateFilter( filter, name ) ) + { + } // fi + + } // rof +} + +// ------------------------------------------------------------------------- +void PipelineEditor::QNodesEditorCanvas:: +_scaleView( qreal scaleFactor ) +{ + qreal factor = this->transform( ). + scale( scaleFactor, scaleFactor ). + mapRect( QRectF( 0, 0, 1, 1 ) ). + width( ); + if( factor < qreal( 0.07 ) || factor > qreal( 100 ) ) + return; + this->scale( scaleFactor, scaleFactor ); +} + +// ------------------------------------------------------------------------- +void PipelineEditor::QNodesEditorCanvas:: +_createBlock( TFilter* f ) +{ + if( f == NULL ) + return; + + // Add block + QNEBlock* b = new QNEBlock( 0, this->scene( ) ); + b->addPort( f->GetName( ), 0, QNEPort::NamePort ); + b->addPort( f->GetClassName( ).c_str( ), 0, QNEPort::TypePort ); + + // 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( ) ); + //in_ports[ vIt->first ][ *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( ) ); + // out_ports[ vIt->first ][ *oIt ] = b->addOutputPort( oIt->c_str( ) ); + + // Keep a trace of this visual graph + this->m_Graph->InsertVertex( f->GetName( ), b ); + + // 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 ) + { + // Add block + QNEBlock* b = new QNEBlock( 0, scene ); + b->addPort( vIt->second->GetName( ), 0, QNEPort::NamePort ); + b->addPort( vIt->second->GetClassName( ).c_str( ), 0, QNEPort::TypePort ); + + // Get filter + auto f = dynamic_cast< TFilter* >( vIt->second.GetPointer( ) ); + if( f == NULL ) + continue; + + // Add input ports + std::set< std::string > inputs; + f->GetInputsNames( inputs ); + for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt ) + in_ports[ vIt->first ][ *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 ) + out_ports[ vIt->first ][ *oIt ] = b->addOutputPort( oIt->c_str( ) ); + + // Keep a trace of this visual graph + this->m_Graph->InsertVertex( vIt->first, b ); + + } // rof + */ +} + +// eof - $RCSfile$ + + +/* + +// ------------------------------------------------------------------------- +void GraphWidget:: +draw( ) +{ + if( this->m_Workspace == NULL ) + return; +} + +// ------------------------------------------------------------------------- +void GraphWidget::itemMoved() +{ + if (!timerId) + timerId = startTimer(1000 / 25); +} + +void GraphWidget::keyPressEvent(QKeyEvent *event) +{ + switch (event->key()) { + case Qt::Key_Up: + centerNode->moveBy(0, -20); + break; + case Qt::Key_Down: + centerNode->moveBy(0, 20); + break; + case Qt::Key_Left: + centerNode->moveBy(-20, 0); + break; + case Qt::Key_Right: + centerNode->moveBy(20, 0); + break; + case Qt::Key_Plus: + zoomIn(); + break; + case Qt::Key_Minus: + zoomOut(); + break; + case Qt::Key_Space: + case Qt::Key_Enter: + shuffle(); + break; + default: + QGraphicsView::keyPressEvent(event); + } +} + +void GraphWidget::timerEvent(QTimerEvent *event) +{ + Q_UNUSED(event); + + QList nodes; + foreach (QGraphicsItem *item, scene()->items()) { + if (Node *node = qgraphicsitem_cast(item)) + nodes << node; + } + + foreach (Node *node, nodes) + node->calculateForces(); + + bool itemsMoved = false; + foreach (Node *node, nodes) { + if (node->advance()) + itemsMoved = true; + } + + if (!itemsMoved) { + killTimer(timerId); + timerId = 0; + } +} + +void GraphWidget::wheelEvent(QWheelEvent *event) +{ + scaleView(pow((double)2, -event->delta() / 240.0)); +} + +void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect) +{ + //Q_UNUSED(rect); + + // Shadow + QRectF sceneRect = rect;//this->sceneRect(); + QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height()); + QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5); + if (rightShadow.intersects(rect) || rightShadow.contains(rect)) + painter->fillRect(rightShadow, Qt::darkGray); + if (bottomShadow.intersects(rect) || bottomShadow.contains(rect)) + painter->fillRect(bottomShadow, Qt::darkGray); + + // Fill + QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight()); + gradient.setColorAt(0, Qt::white); + gradient.setColorAt(1, Qt::lightGray); + painter->fillRect(rect.intersect(sceneRect), gradient); + painter->setBrush(Qt::NoBrush); + painter->drawRect(sceneRect); + +#if !defined(Q_OS_SYMBIAN) && !defined(Q_WS_MAEMO_5) + // Text + QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4, + sceneRect.width() - 4, sceneRect.height() - 4); + QString message(tr("Click and drag the nodes around, and zoom with the mouse " + "wheel or the '+' and '-' keys")); + + QFont font = painter->font(); + font.setBold(true); + font.setPointSize(14); + painter->setFont(font); + painter->setPen(Qt::lightGray); + painter->drawText(textRect.translated(2, 2), message); + painter->setPen(Qt::black); + painter->drawText(textRect, message); +#endif +} + +void GraphWidget::scaleView(qreal scaleFactor) +{ + qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width(); + if (factor < 0.07 || factor > 100) + return; + + scale(scaleFactor, scaleFactor); +} + +void GraphWidget::shuffle() +{ + foreach (QGraphicsItem *item, scene()->items()) { + if (qgraphicsitem_cast(item)) + item->setPos(-150 + qrand() % 300, -150 + qrand() % 300); + } +} + +void GraphWidget::zoomIn() +{ + scaleView(qreal(1.2)); +} + +void GraphWidget::zoomOut() +{ + scaleView(1 / qreal(1.2)); +} + + +*/ diff --git a/appli/cpPipelineEditor/QNodesEditorCanvas.h b/appli/cpPipelineEditor/QNodesEditorCanvas.h new file mode 100644 index 0000000..03380d1 --- /dev/null +++ b/appli/cpPipelineEditor/QNodesEditorCanvas.h @@ -0,0 +1,62 @@ +#ifndef __PIPELINEEDITOR__QNODESEDITORCANVAS__H__ +#define __PIPELINEEDITOR__QNODESEDITORCANVAS__H__ + +#include +#include +#include + +namespace PipelineEditor +{ + // Some other forward declarations + class QNEBlock; + class QNEConnection; + class QNodesEditor; + + /** + */ + class QNodesEditorCanvas + : public QGraphicsView + { + Q_OBJECT; + + public: + typedef cpPlugins::Interface::Workspace TWorkspace; + typedef TWorkspace::TFilter TFilter; + typedef + cpExtensions::DataStructures:: + Graph< QNEBlock*, QNEConnection*, std::string > TGraph; + + public: + QNodesEditorCanvas( QWidget* parent = 0 ); + virtual ~QNodesEditorCanvas( ); + + TWorkspace* workspace( ); + const TWorkspace* workspace( ) const; + void setWorkspace( TWorkspace* ws ); + + protected: + /* TODO + void keyPressEvent( QKeyEvent* event ); + void timerEvent( QTimerEvent* event ); + */ + void wheelEvent( QWheelEvent* event ); + + void dragEnterEvent( QDragEnterEvent* event ); + void dragLeaveEvent( QDragLeaveEvent* event ); + void dragMoveEvent( QDragMoveEvent* event ); + void dropEvent( QDropEvent* event ); + + void _scaleView( qreal scaleFactor ); + void _createBlock( TFilter* f ); + + protected: + TWorkspace* m_Workspace; + QNodesEditor* m_Editor; + TGraph::Pointer m_Graph; + }; + +} // ecapseman + +#endif // __PIPELINEEDITOR__QNODESEDITORCANVAS__H__ + +// eof - $RCSfile$ diff --git a/appli/cpPipelineEditor/cpPipelineEditor.cxx b/appli/cpPipelineEditor/cpPipelineEditor.cxx index 87fd6c8..9abdd01 100644 --- a/appli/cpPipelineEditor/cpPipelineEditor.cxx +++ b/appli/cpPipelineEditor/cpPipelineEditor.cxx @@ -12,6 +12,13 @@ this, SLOT( _Action##ACTION( ) ) \ ) +// ------------------------------------------------------------------------- +#define cpPipelineEditor_ConnectButton( BUTTON ) \ + QObject::connect( \ + this->m_UI->Button##BUTTON, SIGNAL( clicked( ) ), \ + this, SLOT( _Button##BUTTON( ) ) \ + ) + // ------------------------------------------------------------------------- cpPipelineEditor:: cpPipelineEditor( QWidget* parent ) @@ -22,6 +29,8 @@ cpPipelineEditor( QWidget* parent ) this->m_UI->setupUi( this ); // Connect actions to slots + cpPipelineEditor_ConnectButton( LoadPluginsFile ); + cpPipelineEditor_ConnectButton( LoadPluginsPath ); cpPipelineEditor_ConnectAction( OpenWorkspace ); } @@ -34,6 +43,129 @@ cpPipelineEditor:: delete this->m_Workspace; } +// ------------------------------------------------------------------------- +void cpPipelineEditor:: +_UpdateLoadedPlugins( ) +{ + typedef cpPlugins::Interface::Workspace::TStringContainer _TStrings; + + if( this->m_Workspace == NULL ) + return; + + _TStrings categories; + this->m_Workspace->GetLoadedPluginCategories( categories ); + for( auto cIt = categories.begin( ); cIt != categories.end( ); ++cIt ) + { + // Create or get category + QList< QTreeWidgetItem* > cat_items = + this->m_UI->LoadedPlugins->findItems( + cIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive + ); + QTreeWidgetItem* cat = NULL; + if( cat_items.size( ) == 0 ) + { + cat = new QTreeWidgetItem( + ( QTreeWidgetItem* )( NULL ), QStringList( cIt->c_str( ) ) + ); + this->m_UI->LoadedPlugins->addTopLevelItem( cat ); + } + else + cat = cat_items[ 0 ]; + + // Add filters + _TStrings filters = this->m_Workspace->GetLoadedPluginFilters( *cIt ); + for( auto fIt = filters.begin( ); fIt != filters.end( ); ++fIt ) + { + // Find filter + QList< QTreeWidgetItem* > filter_items = + this->m_UI->LoadedPlugins->findItems( + fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive + ); + auto fiIt = filter_items.begin( ); + auto found_fiIt = filter_items.end( ); + for( ; fiIt != filter_items.end( ); ++fiIt ) + if( ( *fiIt )->parent( ) == cat ) + found_fiIt = fiIt; + + // Add filter + if( found_fiIt == filter_items.end( ) ) + QTreeWidgetItem* filter = new QTreeWidgetItem( + cat, QStringList( fIt->c_str( ) ) + ); + + } // rof + + } // rof +} + +// ------------------------------------------------------------------------- +void cpPipelineEditor:: +_ButtonLoadPluginsFile( ) +{ + QFileDialog dlg( this ); + dlg.setFileMode( QFileDialog::ExistingFiles ); + dlg.setDirectory( "." ); +#ifdef _WIN32 + dlg.setNameFilter( "Plugins file (*.dll);;All files (*)" ); + dlg.setDefaultSuffix( "dll" ); +#else // _WIN32 + dlg.setNameFilter( "Plugins file (*.so);;All files (*)" ); + dlg.setDefaultSuffix( "so" ); +#endif // _WIN32 + if( !( dlg.exec( ) ) ) + return; + + // Create a new workspace, if not ready + if( this->m_Workspace == NULL ) + this->m_Workspace = new cpPlugins::Interface::Workspace( ); + + // Read + QStringList names = dlg.selectedFiles( ); + std::stringstream err_str; + for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt ) + if( !( this->m_Workspace->LoadPlugins( qIt->toStdString( ) ) ) ) + err_str << qIt->toStdString( ) << std::endl; + + // Show an error message + std::string err = err_str.str( ); + if( err.size( ) > 0 ) + QMessageBox::critical( + this, + "Error loading plugins", + err.c_str( ) + ); + + // Update view + this->_UpdateLoadedPlugins( ); +} + +// ------------------------------------------------------------------------- +void cpPipelineEditor:: +_ButtonLoadPluginsPath( ) +{ + QFileDialog dlg( this ); + dlg.setFileMode( QFileDialog::DirectoryOnly ); + dlg.setDirectory( "." ); + if( !( dlg.exec( ) ) ) + return; + + // Create a new workspace, if not ready + if( this->m_Workspace == NULL ) + this->m_Workspace = new cpPlugins::Interface::Workspace( ); + + // Read + std::string dir = dlg.selectedFiles( ).begin( )->toStdString( ); + if( !( this->m_Workspace->LoadPluginsPath( dir, false ) ) ) + QMessageBox::critical( + this, + "Error loading plugins directory", + dir.c_str( ) + ); + + // Update view + this->_UpdateLoadedPlugins( ); +} + // ------------------------------------------------------------------------- void cpPipelineEditor:: _ActionOpenWorkspace( ) diff --git a/appli/cpPipelineEditor/cpPipelineEditor.h b/appli/cpPipelineEditor/cpPipelineEditor.h index 2970093..5180dd1 100644 --- a/appli/cpPipelineEditor/cpPipelineEditor.h +++ b/appli/cpPipelineEditor/cpPipelineEditor.h @@ -33,7 +33,12 @@ public: explicit cpPipelineEditor( QWidget* parent = 0 ); virtual ~cpPipelineEditor( ); +protected: + void _UpdateLoadedPlugins( ); + protected slots: + void _ButtonLoadPluginsFile( ); + void _ButtonLoadPluginsPath( ); void _ActionOpenWorkspace( ); private: diff --git a/appli/cpPipelineEditor/cpPipelineEditor.ui b/appli/cpPipelineEditor/cpPipelineEditor.ui index f35e636..5fa8eb0 100644 --- a/appli/cpPipelineEditor/cpPipelineEditor.ui +++ b/appli/cpPipelineEditor/cpPipelineEditor.ui @@ -10,13 +10,130 @@ 600 + + + 800 + 600 + + MainWindow - + + + Qt::Horizontal + + + + + 251 + 331 + + + + QFrame::StyledPanel + + + QFrame::Raised + + + + + + + 202 + 67 + + + + + 202 + 67 + + + + Plugins + + + + + + + 85 + 31 + + + + + 85 + 31 + + + + File + + + + + + + + 85 + 31 + + + + + 85 + 31 + + + + Path + + + + + + + + + + + 229 + 0 + + + + true + + + QAbstractItemView::DragOnly + + + + Loaded plugins + + + + + + + + + + 500 + 0 + + + + true + + + @@ -41,6 +158,8 @@ + + @@ -55,15 +174,37 @@ Save + + + E&xit + + - PipelineEditor::GraphCanvas + PipelineEditor::QNodesEditorCanvas QWidget -
GraphCanvas.h
+
QNodesEditorCanvas.h
1
- + + + ActionExit + triggered() + cpPipelineEditor + close() + + + -1 + -1 + + + 399 + 299 + + + + diff --git a/appli/cpPipelineEditor/main.cxx b/appli/cpPipelineEditor/main.cxx index 531d13e..7154469 100644 --- a/appli/cpPipelineEditor/main.cxx +++ b/appli/cpPipelineEditor/main.cxx @@ -1,4 +1,4 @@ -#include "QNEMainWindow.h" +#include "cpPipelineEditor.h" #include #include @@ -6,7 +6,7 @@ int main( int argc, char* argv[] ) { QApplication a( argc, argv ); - QNEMainWindow w; + cpPipelineEditor w; w.show( ); return( a.exec( ) ); -- 2.45.1