1 #include "QNodesEditorCanvas.h"
2 #include "QNodesEditor.h"
4 #include "QNEConnection.h"
7 #include <QDragEnterEvent>
11 // -------------------------------------------------------------------------
12 PipelineEditor::QNodesEditorCanvas::
13 QNodesEditorCanvas( QWidget* parent )
14 : QGraphicsView( parent ),
17 QGraphicsScene* scene = new QGraphicsScene( this );
18 this->setScene( scene );
19 this->setRenderHint( QPainter::Antialiasing );
20 this->setAcceptDrops( true );
22 this->m_Editor = new QNodesEditor( this );
23 this->m_Editor->install( scene );
26 // -------------------------------------------------------------------------
27 PipelineEditor::QNodesEditorCanvas::
28 ~QNodesEditorCanvas( )
32 // -------------------------------------------------------------------------
33 PipelineEditor::QNodesEditorCanvas::
34 TWorkspace* PipelineEditor::QNodesEditorCanvas::
37 return( this->m_Workspace );
40 // -------------------------------------------------------------------------
41 const PipelineEditor::QNodesEditorCanvas::
42 TWorkspace* PipelineEditor::QNodesEditorCanvas::
45 return( this->m_Workspace );
48 // -------------------------------------------------------------------------
49 void PipelineEditor::QNodesEditorCanvas::
50 setWorkspace( TWorkspace* ws )
52 if( this->m_Workspace == ws )
54 this->m_Workspace = ws;
55 QGraphicsScene* scene = this->scene( );
58 this->m_Graph = TGraph::New( );
60 // Add vertices and keep track of ports
61 std::map< std::string, std::map< std::string, QNEPort* > >
63 auto vIt = this->m_Workspace->GetGraph( )->BeginVertices( );
64 auto vIt_end = this->m_Workspace->GetGraph( )->EndVertices( );
65 for( ; vIt != vIt_end; ++vIt )
67 this->_createBlock( dynamic_cast< TFilter* >( vIt->second.GetPointer( ) ) );
70 QNEBlock* b = new QNEBlock( 0, scene );
71 b->addPort( vIt->second->GetName( ), 0, QNEPort::NamePort );
72 b->addPort( vIt->second->GetClassName( ).c_str( ), 0, QNEPort::TypePort );
79 std::set< std::string > inputs;
80 f->GetInputsNames( inputs );
81 for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt )
82 in_ports[ vIt->first ][ *iIt ] = b->addInputPort( iIt->c_str( ) );
85 std::set< std::string > outputs;
86 f->GetOutputsNames( outputs );
87 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
88 out_ports[ vIt->first ][ *oIt ] = b->addOutputPort( oIt->c_str( ) );
90 // Keep a trace of this visual graph
91 this->m_Graph->InsertVertex( vIt->first, b );
97 auto rIt = this->m_Workspace->GetGraph( )->BeginEdgesRows( );
98 auto rIt_end = this->m_Workspace->GetGraph( )->EndEdgesRows( );
99 for( ; rIt != rIt_end; ++rIt )
101 auto cIt = rIt->second.begin( );
102 for( ; cIt != rIt->second.end( ); ++cIt )
104 auto eIt = cIt->second.begin( );
105 for( ; eIt != cIt->second.end( ); ++eIt )
107 QNEPort* p1 = out_ports[ rIt->first ][ eIt->first ];
108 QNEPort* p2 = in_ports[ cIt->first ][ eIt->second ];
109 if( p1 != NULL && p2 != NULL )
111 QNEConnection* conn = new QNEConnection( 0, scene );
112 conn->setPort1( p1 );
113 conn->setPort2( p2 );
114 this->m_Graph->AddConnection( rIt->first, cIt->first, conn );
126 // -------------------------------------------------------------------------
128 void PipelineEditor::QNodesEditorCanvas::
129 keyPressEvent( QKeyEvent* event )
133 // -------------------------------------------------------------------------
134 void PipelineEditor::QNodesEditorCanvas::
135 timerEvent( QTimerEvent* event )
140 // -------------------------------------------------------------------------
141 void PipelineEditor::QNodesEditorCanvas::
142 wheelEvent( QWheelEvent* event )
145 std::pow( double( 2 ), event->delta( ) / double( 240 ) )
149 // -------------------------------------------------------------------------
150 void PipelineEditor::QNodesEditorCanvas::
151 dragEnterEvent( QDragEnterEvent* event )
153 const QMimeData* mime = event->mimeData( );
154 if( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) )
155 event->acceptProposedAction( );
158 // -------------------------------------------------------------------------
159 void PipelineEditor::QNodesEditorCanvas::
160 dragLeaveEvent( QDragLeaveEvent* event )
164 // -------------------------------------------------------------------------
165 void PipelineEditor::QNodesEditorCanvas::
166 dragMoveEvent( QDragMoveEvent* event )
170 // -------------------------------------------------------------------------
171 void PipelineEditor::QNodesEditorCanvas::
172 dropEvent( QDropEvent* event )
174 if( this->m_Workspace == NULL )
176 const QMimeData* mime = event->mimeData( );
177 if( !( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) ) )
180 event->acceptProposedAction( );
181 auto tree = dynamic_cast< QTreeWidget* >( event->source( ) );
185 QList< QTreeWidgetItem* > items = tree->selectedItems( );
186 for( auto iIt = items.begin( ); iIt != items.end( ); ++iIt )
188 std::string filter = ( *iIt )->text( 0 ).toStdString( );
189 std::string name = filter;
190 if( this->m_Workspace->GetFilter( name ) != NULL )
191 name += std::string( "_" );
192 if( this->m_Workspace->CreateFilter( filter, name ) )
199 // -------------------------------------------------------------------------
200 void PipelineEditor::QNodesEditorCanvas::
201 _scaleView( qreal scaleFactor )
203 qreal factor = this->transform( ).
204 scale( scaleFactor, scaleFactor ).
205 mapRect( QRectF( 0, 0, 1, 1 ) ).
207 if( factor < qreal( 0.07 ) || factor > qreal( 100 ) )
209 this->scale( scaleFactor, scaleFactor );
212 // -------------------------------------------------------------------------
213 void PipelineEditor::QNodesEditorCanvas::
214 _createBlock( TFilter* f )
220 QNEBlock* b = new QNEBlock( 0, this->scene( ) );
221 b->addPort( f->GetName( ), 0, QNEPort::NamePort );
222 b->addPort( f->GetClassName( ).c_str( ), 0, QNEPort::TypePort );
225 std::set< std::string > inputs;
226 f->GetInputsNames( inputs );
227 for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt )
228 b->addInputPort( iIt->c_str( ) );
229 //in_ports[ vIt->first ][ *iIt ] = b->addInputPort( iIt->c_str( ) );
232 std::set< std::string > outputs;
233 f->GetOutputsNames( outputs );
234 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
235 b->addOutputPort( oIt->c_str( ) );
236 // out_ports[ vIt->first ][ *oIt ] = b->addOutputPort( oIt->c_str( ) );
238 // Keep a trace of this visual graph
239 this->m_Graph->InsertVertex( f->GetName( ), b );
241 // Add vertices and keep track of ports
243 std::map< std::string, std::map< std::string, QNEPort* > >
245 auto vIt = this->m_Workspace->GetGraph( )->BeginVertices( );
246 auto vIt_end = this->m_Workspace->GetGraph( )->EndVertices( );
247 for( ; vIt != vIt_end; ++vIt )
250 QNEBlock* b = new QNEBlock( 0, scene );
251 b->addPort( vIt->second->GetName( ), 0, QNEPort::NamePort );
252 b->addPort( vIt->second->GetClassName( ).c_str( ), 0, QNEPort::TypePort );
255 auto f = dynamic_cast< TFilter* >( vIt->second.GetPointer( ) );
260 std::set< std::string > inputs;
261 f->GetInputsNames( inputs );
262 for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt )
263 in_ports[ vIt->first ][ *iIt ] = b->addInputPort( iIt->c_str( ) );
266 std::set< std::string > outputs;
267 f->GetOutputsNames( outputs );
268 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
269 out_ports[ vIt->first ][ *oIt ] = b->addOutputPort( oIt->c_str( ) );
271 // Keep a trace of this visual graph
272 this->m_Graph->InsertVertex( vIt->first, b );
283 // -------------------------------------------------------------------------
287 if( this->m_Workspace == NULL )
291 // -------------------------------------------------------------------------
292 void GraphWidget::itemMoved()
295 timerId = startTimer(1000 / 25);
298 void GraphWidget::keyPressEvent(QKeyEvent *event)
300 switch (event->key()) {
302 centerNode->moveBy(0, -20);
305 centerNode->moveBy(0, 20);
308 centerNode->moveBy(-20, 0);
311 centerNode->moveBy(20, 0);
324 QGraphicsView::keyPressEvent(event);
328 void GraphWidget::timerEvent(QTimerEvent *event)
333 foreach (QGraphicsItem *item, scene()->items()) {
334 if (Node *node = qgraphicsitem_cast<Node *>(item))
338 foreach (Node *node, nodes)
339 node->calculateForces();
341 bool itemsMoved = false;
342 foreach (Node *node, nodes) {
353 void GraphWidget::wheelEvent(QWheelEvent *event)
355 scaleView(pow((double)2, -event->delta() / 240.0));
358 void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
363 QRectF sceneRect = rect;//this->sceneRect();
364 QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
365 QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
366 if (rightShadow.intersects(rect) || rightShadow.contains(rect))
367 painter->fillRect(rightShadow, Qt::darkGray);
368 if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
369 painter->fillRect(bottomShadow, Qt::darkGray);
372 QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
373 gradient.setColorAt(0, Qt::white);
374 gradient.setColorAt(1, Qt::lightGray);
375 painter->fillRect(rect.intersect(sceneRect), gradient);
376 painter->setBrush(Qt::NoBrush);
377 painter->drawRect(sceneRect);
379 #if !defined(Q_OS_SYMBIAN) && !defined(Q_WS_MAEMO_5)
381 QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4,
382 sceneRect.width() - 4, sceneRect.height() - 4);
383 QString message(tr("Click and drag the nodes around, and zoom with the mouse "
384 "wheel or the '+' and '-' keys"));
386 QFont font = painter->font();
388 font.setPointSize(14);
389 painter->setFont(font);
390 painter->setPen(Qt::lightGray);
391 painter->drawText(textRect.translated(2, 2), message);
392 painter->setPen(Qt::black);
393 painter->drawText(textRect, message);
397 void GraphWidget::scaleView(qreal scaleFactor)
399 qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
400 if (factor < 0.07 || factor > 100)
403 scale(scaleFactor, scaleFactor);
406 void GraphWidget::shuffle()
408 foreach (QGraphicsItem *item, scene()->items()) {
409 if (qgraphicsitem_cast<Node *>(item))
410 item->setPos(-150 + qrand() % 300, -150 + qrand() % 300);
414 void GraphWidget::zoomIn()
416 scaleView(qreal(1.2));
419 void GraphWidget::zoomOut()
421 scaleView(1 / qreal(1.2));