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 this->m_Graph = TGraph::New( );
58 QGraphicsScene* scene = this->scene( );
59 // Add vertices and keep track of ports
60 std::map< std::string, std::map< std::string, QNEPort* > >
62 auto vIt = this->m_Workspace->GetGraph( )->BeginVertices( );
63 auto vIt_end = this->m_Workspace->GetGraph( )->EndVertices( );
64 for( ; vIt != vIt_end; ++vIt )
66 this->_createBlock( dynamic_cast< TFilter* >( vIt->second.GetPointer( ) ) );
73 auto rIt = this->m_Workspace->GetGraph( )->BeginEdgesRows( );
74 auto rIt_end = this->m_Workspace->GetGraph( )->EndEdgesRows( );
75 for( ; rIt != rIt_end; ++rIt )
77 auto cIt = rIt->second.begin( );
78 for( ; cIt != rIt->second.end( ); ++cIt )
80 auto eIt = cIt->second.begin( );
81 for( ; eIt != cIt->second.end( ); ++eIt )
83 QNEPort* p1 = out_ports[ rIt->first ][ eIt->first ];
84 QNEPort* p2 = in_ports[ cIt->first ][ eIt->second ];
85 if( p1 != NULL && p2 != NULL )
87 QNEConnection* conn = new QNEConnection( 0, scene );
90 this->m_Graph->AddConnection( rIt->first, cIt->first, conn );
102 // -------------------------------------------------------------------------
104 void PipelineEditor::QNodesEditorCanvas::
105 keyPressEvent( QKeyEvent* event )
109 // -------------------------------------------------------------------------
110 void PipelineEditor::QNodesEditorCanvas::
111 timerEvent( QTimerEvent* event )
116 // -------------------------------------------------------------------------
117 void PipelineEditor::QNodesEditorCanvas::
118 wheelEvent( QWheelEvent* event )
121 std::pow( double( 2 ), event->delta( ) / double( 240 ) )
125 // -------------------------------------------------------------------------
126 void PipelineEditor::QNodesEditorCanvas::
127 dragEnterEvent( QDragEnterEvent* event )
129 const QMimeData* mime = event->mimeData( );
130 if( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) )
131 event->acceptProposedAction( );
134 // -------------------------------------------------------------------------
135 void PipelineEditor::QNodesEditorCanvas::
136 dragLeaveEvent( QDragLeaveEvent* event )
140 // -------------------------------------------------------------------------
141 void PipelineEditor::QNodesEditorCanvas::
142 dragMoveEvent( QDragMoveEvent* event )
146 // -------------------------------------------------------------------------
147 void PipelineEditor::QNodesEditorCanvas::
148 dropEvent( QDropEvent* event )
150 std::cout << event << " " << this->m_Workspace << std::endl;
151 if( this->m_Workspace == NULL )
153 const QMimeData* mime = event->mimeData( );
154 if( !( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) ) )
157 event->acceptProposedAction( );
158 auto tree = dynamic_cast< QTreeWidget* >( event->source( ) );
162 QList< QTreeWidgetItem* > items = tree->selectedItems( );
163 for( auto iIt = items.begin( ); iIt != items.end( ); ++iIt )
165 std::string filter = ( *iIt )->text( 0 ).toStdString( );
166 std::string name = "filtro"; //filter;
167 if( this->m_Workspace->GetFilter( name ) != NULL )
168 name += std::string( "_" );
169 std::cout << name << std::endl;
170 if( this->m_Workspace->CreateFilter( filter, name ) )
172 auto vIt = this->m_Workspace->GetGraph( )->BeginVertices( );
173 auto vIt_end = this->m_Workspace->GetGraph( )->EndVertices( );
174 for( ; vIt != vIt_end; ++vIt )
176 std::cout << "NAME: " << vIt->first << " " << vIt->second.GetPointer( ) << std::endl;
177 vIt->second->Print( std::cout );
181 std::cout << "ok" << std::endl;
182 this->_createBlock( this->m_Workspace->GetFilter( name ) );
185 std::cout << "no" << std::endl;
190 // -------------------------------------------------------------------------
191 void PipelineEditor::QNodesEditorCanvas::
192 _scaleView( qreal scaleFactor )
194 qreal factor = this->transform( ).
195 scale( scaleFactor, scaleFactor ).
196 mapRect( QRectF( 0, 0, 1, 1 ) ).
198 if( factor < qreal( 0.07 ) || factor > qreal( 100 ) )
200 this->scale( scaleFactor, scaleFactor );
203 // -------------------------------------------------------------------------
204 void PipelineEditor::QNodesEditorCanvas::
205 _createBlock( TFilter* f )
207 std::cout << "ptr: " << f << std::endl;
213 QNEBlock* b = new QNEBlock( 0, this->scene( ) );
214 b->addPort( f->GetName( ), 0, QNEPort::NamePort );
215 b->addPort( f->GetClassName( ).c_str( ), 0, QNEPort::TypePort );
218 std::set< std::string > inputs;
219 f->GetInputsNames( inputs );
220 for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt )
221 b->addInputPort( iIt->c_str( ) );
222 //in_ports[ vIt->first ][ *iIt ] = b->addInputPort( iIt->c_str( ) );
225 std::set< std::string > outputs;
226 f->GetOutputsNames( outputs );
227 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
228 b->addOutputPort( oIt->c_str( ) );
229 // out_ports[ vIt->first ][ *oIt ] = b->addOutputPort( oIt->c_str( ) );
231 // Keep a trace of this visual graph
232 this->m_Graph->InsertVertex( f->GetName( ), b );
234 // Add vertices and keep track of ports
236 std::map< std::string, std::map< std::string, QNEPort* > >
238 auto vIt = this->m_Workspace->GetGraph( )->BeginVertices( );
239 auto vIt_end = this->m_Workspace->GetGraph( )->EndVertices( );
240 for( ; vIt != vIt_end; ++vIt )
243 QNEBlock* b = new QNEBlock( 0, scene );
244 b->addPort( vIt->second->GetName( ), 0, QNEPort::NamePort );
245 b->addPort( vIt->second->GetClassName( ).c_str( ), 0, QNEPort::TypePort );
248 auto f = dynamic_cast< TFilter* >( vIt->second.GetPointer( ) );
253 std::set< std::string > inputs;
254 f->GetInputsNames( inputs );
255 for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt )
256 in_ports[ vIt->first ][ *iIt ] = b->addInputPort( iIt->c_str( ) );
259 std::set< std::string > outputs;
260 f->GetOutputsNames( outputs );
261 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
262 out_ports[ vIt->first ][ *oIt ] = b->addOutputPort( oIt->c_str( ) );
264 // Keep a trace of this visual graph
265 this->m_Graph->InsertVertex( vIt->first, b );
276 // -------------------------------------------------------------------------
280 if( this->m_Workspace == NULL )
284 // -------------------------------------------------------------------------
285 void GraphWidget::itemMoved()
288 timerId = startTimer(1000 / 25);
291 void GraphWidget::keyPressEvent(QKeyEvent *event)
293 switch (event->key()) {
295 centerNode->moveBy(0, -20);
298 centerNode->moveBy(0, 20);
301 centerNode->moveBy(-20, 0);
304 centerNode->moveBy(20, 0);
317 QGraphicsView::keyPressEvent(event);
321 void GraphWidget::timerEvent(QTimerEvent *event)
326 foreach (QGraphicsItem *item, scene()->items()) {
327 if (Node *node = qgraphicsitem_cast<Node *>(item))
331 foreach (Node *node, nodes)
332 node->calculateForces();
334 bool itemsMoved = false;
335 foreach (Node *node, nodes) {
346 void GraphWidget::wheelEvent(QWheelEvent *event)
348 scaleView(pow((double)2, -event->delta() / 240.0));
351 void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
356 QRectF sceneRect = rect;//this->sceneRect();
357 QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
358 QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
359 if (rightShadow.intersects(rect) || rightShadow.contains(rect))
360 painter->fillRect(rightShadow, Qt::darkGray);
361 if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
362 painter->fillRect(bottomShadow, Qt::darkGray);
365 QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
366 gradient.setColorAt(0, Qt::white);
367 gradient.setColorAt(1, Qt::lightGray);
368 painter->fillRect(rect.intersect(sceneRect), gradient);
369 painter->setBrush(Qt::NoBrush);
370 painter->drawRect(sceneRect);
372 #if !defined(Q_OS_SYMBIAN) && !defined(Q_WS_MAEMO_5)
374 QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4,
375 sceneRect.width() - 4, sceneRect.height() - 4);
376 QString message(tr("Click and drag the nodes around, and zoom with the mouse "
377 "wheel or the '+' and '-' keys"));
379 QFont font = painter->font();
381 font.setPointSize(14);
382 painter->setFont(font);
383 painter->setPen(Qt::lightGray);
384 painter->drawText(textRect.translated(2, 2), message);
385 painter->setPen(Qt::black);
386 painter->drawText(textRect, message);
390 void GraphWidget::scaleView(qreal scaleFactor)
392 qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
393 if (factor < 0.07 || factor > 100)
396 scale(scaleFactor, scaleFactor);
399 void GraphWidget::shuffle()
401 foreach (QGraphicsItem *item, scene()->items()) {
402 if (qgraphicsitem_cast<Node *>(item))
403 item->setPos(-150 + qrand() % 300, -150 + qrand() % 300);
407 void GraphWidget::zoomIn()
409 scaleView(qreal(1.2));
412 void GraphWidget::zoomOut()
414 scaleView(1 / qreal(1.2));