3 #include <QGraphicsScene>
\r
5 #include <QGraphicsSceneContextMenuEvent>
\r
6 #include <QGraphicsSceneDragDropEvent>
\r
7 #include <QGraphicsSceneHelpEvent>
\r
8 #include <QGraphicsSceneHoverEvent>
\r
9 #include <QGraphicsSceneMouseEvent>
\r
10 #include <QGraphicsSceneMoveEvent>
\r
11 #include <QGraphicsSceneResizeEvent>
\r
12 #include <QGraphicsSceneWheelEvent>
\r
13 #include <QInputDialog>
\r
14 #include <QMessageBox>
\r
17 #include "Connection.h"
\r
20 // -------------------------------------------------------------------------
\r
21 #define cpPipelineEditor_Editor_Callback_SWITCH( E, e ) \
\r
22 case QEvent::GraphicsScene##E: \
\r
24 QGraphicsScene##E##Event* evt = \
\r
25 dynamic_cast< QGraphicsScene##E##Event* >( e ); \
\r
27 this->_##E##_cbk( evt ); \
\r
31 // -------------------------------------------------------------------------
\r
32 #define cpPipelineEditor_Editor_Callback_CODE( E ) \
\r
33 void cpPipelineEditor::Editor::_##E##_cbk( QGraphicsScene##E##Event* evt )
\r
35 // -------------------------------------------------------------------------
\r
36 cpPipelineEditor::Editor::
\r
37 Editor( QObject* parent )
\r
38 : Superclass( parent ),
\r
39 m_ActualConnection( NULL ),
\r
44 // -------------------------------------------------------------------------
\r
45 cpPipelineEditor::Editor::
\r
50 // -------------------------------------------------------------------------
\r
51 cpPipelineEditor::Editor::
\r
52 TWorkspace* cpPipelineEditor::Editor::
\r
55 return( this->m_Workspace );
\r
58 // -------------------------------------------------------------------------
\r
59 const cpPipelineEditor::Editor::
\r
60 TWorkspace* cpPipelineEditor::Editor::
\r
63 return( this->m_Workspace );
\r
66 // -------------------------------------------------------------------------
\r
67 void cpPipelineEditor::Editor::
\r
68 setWorkspace( TWorkspace* ws )
\r
70 this->m_Workspace = ws;
\r
71 this->m_Graph = TGraph::New( );
\r
74 auto vIt = this->m_Workspace->GetGraph( )->BeginVertices( );
\r
75 auto vIt_end = this->m_Workspace->GetGraph( )->EndVertices( );
\r
76 for( ; vIt != vIt_end; ++vIt )
\r
78 dynamic_cast< TFilter* >( vIt->second.GetPointer( ) ),
\r
79 QPointF( vIt->second->GetViewX( ), vIt->second->GetViewY( ) )
\r
83 auto rIt = this->m_Workspace->GetGraph( )->BeginEdgesRows( );
\r
84 auto rIt_end = this->m_Workspace->GetGraph( )->EndEdgesRows( );
\r
85 for( ; rIt != rIt_end; ++rIt )
\r
87 Block* orig = this->m_Graph->GetVertex( rIt->first );
\r
88 auto cIt = rIt->second.begin( );
\r
89 for( ; cIt != rIt->second.end( ); ++cIt )
\r
91 Block* dest = this->m_Graph->GetVertex( cIt->first );
\r
92 auto eIt = cIt->second.begin( );
\r
93 for( ; eIt != cIt->second.end( ); ++eIt )
\r
95 OutputPort* op = orig->outputPort( eIt->first.c_str( ) );
\r
96 InputPort* ip = dest->inputPort( eIt->second.c_str( ) );
\r
97 if( op == NULL || ip == NULL )
\r
100 Connection* c = new Connection( 0, this->m_Scene );
\r
103 c->updatePosFromPorts( );
\r
105 this->m_Graph->AddEdge( rIt->first, cIt->first, c );
\r
114 // -------------------------------------------------------------------------
\r
115 std::string cpPipelineEditor::Editor::
\r
116 createFilter( const std::string& filter, const QPointF& pnt )
\r
118 std::string name = filter;
\r
119 while( this->m_Workspace->HasFilter( name ) )
\r
120 name += std::string( "_" );
\r
121 if( this->m_Workspace->CreateFilter( filter, name ) )
\r
123 this->_createBlock( this->m_Workspace->GetFilter( name ), pnt );
\r
130 // -------------------------------------------------------------------------
\r
131 void cpPipelineEditor::Editor::
\r
132 install( QGraphicsScene* s )
\r
134 s->installEventFilter( this );
\r
138 // -------------------------------------------------------------------------
\r
139 QGraphicsItem* cpPipelineEditor::Editor::
\r
140 itemAt( const QPointF& pos )
\r
142 QList< QGraphicsItem* > items =
\r
143 this->m_Scene->items( QRectF( pos - QPointF( 1, 1 ), QSize( 3, 3 ) ) );
\r
145 foreach( QGraphicsItem* item, items )
\r
146 if( item->type( ) > QGraphicsItem::UserType )
\r
151 // -------------------------------------------------------------------------
\r
152 cpPipelineEditor::Block* cpPipelineEditor::Editor::
\r
153 _createBlock( TFilter* f, const QPointF& pnt )
\r
159 Block* b = new Block( f, 0, this->m_Scene );
\r
160 b->setEditor( this );
\r
163 // Mark exposed inputs
\r
164 auto& e_in = this->m_Workspace->GetExposedInputPorts( );
\r
165 std::set< std::string > f_in;
\r
166 f->GetInputsNames( f_in );
\r
167 for( auto iIt = f_in.begin( ); iIt != f_in.end( ); ++iIt )
\r
169 auto eIt = e_in.begin( );
\r
170 auto fIt = e_in.end( );
\r
171 for( ; eIt != e_in.end( ) && fIt == e_in.end( ); ++eIt )
\r
172 if( eIt->second.second == *iIt )
\r
174 if( fIt == e_in.end( ) )
\r
177 auto port = b->inputPort( iIt->c_str( ) );
\r
178 port->setExtendedName( fIt->first.c_str( ) );
\r
179 port->setExtend( true );
\r
183 // Mark exposed outputs
\r
184 auto& e_out = this->m_Workspace->GetExposedOutputPorts( );
\r
185 std::set< std::string > f_out;
\r
186 f->GetOutputsNames( f_out );
\r
187 for( auto iIt = f_out.begin( ); iIt != f_out.end( ); ++iIt )
\r
189 auto eIt = e_out.begin( );
\r
190 auto fIt = e_out.end( );
\r
191 for( ; eIt != e_out.end( ) && fIt == e_out.end( ); ++eIt )
\r
192 if( eIt->second.second == *iIt )
\r
194 if( fIt == e_out.end( ) )
\r
197 auto port = b->outputPort( iIt->c_str( ) );
\r
198 port->setExtendedName( fIt->first.c_str( ) );
\r
199 port->setExtend( true );
\r
203 // Keep a trace of this visual graph
\r
204 this->m_Graph->SetVertex( f->GetName( ), b );
\r
208 // -------------------------------------------------------------------------
\r
209 bool cpPipelineEditor::Editor::
\r
210 eventFilter( QObject* o, QEvent* e )
\r
213 switch( int( e->type( ) ) )
\r
215 cpPipelineEditor_Editor_Callback_SWITCH( ContextMenu, e );
\r
216 cpPipelineEditor_Editor_Callback_SWITCH( DragEnter, e );
\r
217 cpPipelineEditor_Editor_Callback_SWITCH( DragLeave, e );
\r
218 cpPipelineEditor_Editor_Callback_SWITCH( DragMove, e );
\r
219 cpPipelineEditor_Editor_Callback_SWITCH( Drop, e );
\r
220 cpPipelineEditor_Editor_Callback_SWITCH( Help, e );
\r
221 cpPipelineEditor_Editor_Callback_SWITCH( HoverEnter, e );
\r
222 cpPipelineEditor_Editor_Callback_SWITCH( HoverLeave, e );
\r
223 cpPipelineEditor_Editor_Callback_SWITCH( HoverMove, e );
\r
224 cpPipelineEditor_Editor_Callback_SWITCH( MouseDoubleClick, e );
\r
225 cpPipelineEditor_Editor_Callback_SWITCH( MouseMove, e );
\r
226 cpPipelineEditor_Editor_Callback_SWITCH( MousePress, e );
\r
227 cpPipelineEditor_Editor_Callback_SWITCH( MouseRelease, e );
\r
228 cpPipelineEditor_Editor_Callback_SWITCH( Move, e );
\r
229 cpPipelineEditor_Editor_Callback_SWITCH( Resize, e );
\r
230 cpPipelineEditor_Editor_Callback_SWITCH( Wheel, e );
\r
234 return( this->Superclass::eventFilter( o, e ) );
\r
237 // -------------------------------------------------------------------------
\r
238 void cpPipelineEditor::Editor::
\r
239 updateFilter( const std::string& filter_name )
\r
241 emit execFilter( filter_name );
\r
244 // -------------------------------------------------------------------------
\r
245 void cpPipelineEditor::Editor::
\r
247 const std::string& filter_name, const std::string& output_name
\r
250 emit showFilterOutput( filter_name, output_name );
\r
253 // -------------------------------------------------------------------------
\r
254 cpPipelineEditor_Editor_Callback_CODE( ContextMenu )
\r
258 // -------------------------------------------------------------------------
\r
259 cpPipelineEditor_Editor_Callback_CODE( DragEnter )
\r
263 // -------------------------------------------------------------------------
\r
264 cpPipelineEditor_Editor_Callback_CODE( DragLeave )
\r
268 // -------------------------------------------------------------------------
\r
269 cpPipelineEditor_Editor_Callback_CODE( DragMove )
\r
273 // -------------------------------------------------------------------------
\r
274 cpPipelineEditor_Editor_Callback_CODE( Drop )
\r
278 // -------------------------------------------------------------------------
\r
279 cpPipelineEditor_Editor_Callback_CODE( Help )
\r
283 // -------------------------------------------------------------------------
\r
284 cpPipelineEditor_Editor_Callback_CODE( HoverEnter )
\r
288 // -------------------------------------------------------------------------
\r
289 cpPipelineEditor_Editor_Callback_CODE( HoverLeave )
\r
293 // -------------------------------------------------------------------------
\r
294 cpPipelineEditor_Editor_Callback_CODE( HoverMove )
\r
298 // -------------------------------------------------------------------------
\r
299 cpPipelineEditor_Editor_Callback_CODE( MouseDoubleClick )
\r
301 QGraphicsItem* item = this->itemAt( evt->scenePos( ) );
\r
305 switch( evt->button( ) )
\r
307 case Qt::LeftButton:
\r
309 Block* block = dynamic_cast< Block* >( item );
\r
310 Port* port = dynamic_cast< Port* >( item );
\r
311 Connection* conn = dynamic_cast< Connection* >( item );
\r
313 if( block != NULL )
\r
315 QString old_name = block->namePort( );
\r
318 QInputDialog::getText(
\r
319 dynamic_cast< QWidget* >( this->parent( ) ),
\r
320 "Change filter name",
\r
326 if( ok && !new_name.isEmpty( ) && old_name != new_name )
\r
328 ok = this->m_Graph->RenameVertex(
\r
329 old_name.toStdString( ),
\r
330 new_name.toStdString( )
\r
334 block->setNamePort( new_name );
\r
335 this->m_Workspace->RenameFilter(
\r
336 old_name.toStdString( ),
\r
337 new_name.toStdString( )
\r
344 else if( port != NULL )
\r
346 if( evt->modifiers( ) == Qt::ControlModifier )
\r
348 port->setExtend( !( port->isExtended( ) ) );
\r
349 InputPort* in_port = dynamic_cast< InputPort* >( port );
\r
350 OutputPort* out_port = dynamic_cast< OutputPort* >( port );
\r
351 if( port->isExtended( ) )
\r
353 if( in_port != NULL )
\r
355 this->m_Workspace->ExposeInputPort(
\r
356 in_port->extendedName( ).toStdString( ),
\r
357 in_port->block( )->namePort( ).toStdString( ),
\r
358 in_port->name( ).toStdString( )
\r
361 else if( out_port != NULL )
\r
363 this->m_Workspace->ExposeOutputPort(
\r
364 out_port->extendedName( ).toStdString( ),
\r
365 out_port->block( )->namePort( ).toStdString( ),
\r
366 out_port->name( ).toStdString( )
\r
373 if( in_port != NULL )
\r
374 this->m_Workspace->HideInputPort(
\r
375 in_port->extendedName( ).toStdString( )
\r
377 else if( out_port != NULL )
\r
378 this->m_Workspace->HideOutputPort(
\r
379 out_port->extendedName( ).toStdString( )
\r
383 this->m_Scene->update( );
\r
385 else if( evt->modifiers( ) == Qt::NoModifier )
\r
387 if( port->isExtended( ) )
\r
389 QString old_name = port->extendedName( );
\r
392 QInputDialog::getText(
\r
393 dynamic_cast< QWidget* >( this->parent( ) ),
\r
394 "Change filter name",
\r
400 if( ok && !new_name.isEmpty( ) && old_name != new_name )
\r
402 port->setExtendedName( new_name );
\r
403 InputPort* in_port = dynamic_cast< InputPort* >( port );
\r
404 OutputPort* out_port = dynamic_cast< OutputPort* >( port );
\r
405 if( in_port != NULL )
\r
406 this->m_Workspace->
\r
407 RenameExposedInputPort(
\r
408 old_name.toStdString( ),
\r
409 new_name.toStdString( )
\r
411 else if( out_port != NULL )
\r
412 this->m_Workspace->
\r
413 RenameExposedOutputPort(
\r
414 old_name.toStdString( ),
\r
415 new_name.toStdString( )
\r
417 this->m_Scene->update( );
\r
425 else if( conn != NULL )
\r
431 case Qt::RightButton:
\r
435 case Qt::MiddleButton:
\r
445 // -------------------------------------------------------------------------
\r
446 cpPipelineEditor_Editor_Callback_CODE( MouseMove )
\r
448 if( this->m_ActualConnection != NULL )
\r
450 if( this->m_ActualConnection->port1( ) == NULL )
\r
451 this->m_ActualConnection->setPos1( evt->scenePos( ) );
\r
452 else if( this->m_ActualConnection->port2( ) == NULL )
\r
453 this->m_ActualConnection->setPos2( evt->scenePos( ) );
\r
454 this->m_ActualConnection->updatePath( );
\r
459 // -------------------------------------------------------------------------
\r
460 cpPipelineEditor_Editor_Callback_CODE( MousePress )
\r
462 InputPort* in_port =
\r
463 dynamic_cast< InputPort* >( this->itemAt( evt->scenePos( ) ) );
\r
464 OutputPort* out_port =
\r
465 dynamic_cast< OutputPort* >( this->itemAt( evt->scenePos( ) ) );
\r
466 if( in_port == NULL && out_port == NULL )
\r
469 switch( evt->button( ) )
\r
471 case Qt::LeftButton:
\r
473 if( out_port != NULL )
\r
475 if( out_port->block( ) != NULL )
\r
477 // Start new connection
\r
478 this->m_ActualConnection = new Connection( 0, this->m_Scene );
\r
479 this->m_ActualConnection->setPort1( out_port );
\r
480 this->m_ActualConnection->setPos1( out_port->scenePos( ) );
\r
481 this->m_ActualConnection->setPos2( evt->scenePos( ) );
\r
482 this->m_ActualConnection->updatePosFromPorts( );
\r
483 this->m_ActualConnection->updatePath( );
\r
496 // -------------------------------------------------------------------------
\r
497 cpPipelineEditor_Editor_Callback_CODE( MouseRelease )
\r
499 if( this->m_ActualConnection == NULL )
\r
502 switch( evt->button( ) )
\r
504 case Qt::LeftButton:
\r
507 dynamic_cast< InputPort* >( this->itemAt( evt->scenePos( ) ) );
\r
508 if( port2 != NULL )
\r
510 OutputPort* port1 =
\r
511 dynamic_cast< OutputPort* >( this->m_ActualConnection->port1( ) );
\r
512 if( port1 != NULL )
\r
515 port1->block( ) != port2->block( ) &&
\r
516 !port2->hasConnection( ) &&
\r
517 !port1->isConnected( port2 ) &&
\r
518 !port2->isExtended( )
\r
521 this->m_ActualConnection->setPos2( port2->scenePos( ) );
\r
522 this->m_ActualConnection->setPort2( port2 );
\r
523 this->m_ActualConnection->updatePosFromPorts( );
\r
524 this->m_ActualConnection->updatePath( );
\r
526 this->m_Workspace->Connect(
\r
527 port1->block( )->namePort( ).toStdString( ),
\r
528 port2->block( )->namePort( ).toStdString( ),
\r
529 port1->name( ).toStdString( ),
\r
530 port2->name( ).toStdString( )
\r
532 this->m_Graph->AddEdge(
\r
533 port1->block( )->namePort( ).toStdString( ),
\r
534 port2->block( )->namePort( ).toStdString( ),
\r
535 this->m_ActualConnection
\r
539 delete this->m_ActualConnection;
\r
542 delete this->m_ActualConnection;
\r
545 delete this->m_ActualConnection;
\r
546 this->m_ActualConnection = NULL;
\r
554 // -------------------------------------------------------------------------
\r
555 cpPipelineEditor_Editor_Callback_CODE( Move )
\r
559 // -------------------------------------------------------------------------
\r
560 cpPipelineEditor_Editor_Callback_CODE( Resize )
\r
564 // -------------------------------------------------------------------------
\r
565 cpPipelineEditor_Editor_Callback_CODE( Wheel )
\r