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 )
16 QGraphicsScene* scene = new QGraphicsScene( this );
17 this->setScene( scene );
18 this->setRenderHint( QPainter::Antialiasing );
19 this->setAcceptDrops( true );
21 this->m_Editor = new QNodesEditor( this );
22 this->m_Editor->install( scene );
25 // -------------------------------------------------------------------------
26 PipelineEditor::QNodesEditorCanvas::
27 ~QNodesEditorCanvas( )
31 // -------------------------------------------------------------------------
33 QNodesEditor* PipelineEditor::QNodesEditorCanvas::
36 return( this->m_Editor );
39 // -------------------------------------------------------------------------
40 const PipelineEditor::
41 QNodesEditor* PipelineEditor::QNodesEditorCanvas::
44 return( this->m_Editor );
47 // -------------------------------------------------------------------------
49 void PipelineEditor::QNodesEditorCanvas::
50 keyPressEvent( QKeyEvent* event )
54 // -------------------------------------------------------------------------
55 void PipelineEditor::QNodesEditorCanvas::
56 timerEvent( QTimerEvent* event )
61 // -------------------------------------------------------------------------
62 void PipelineEditor::QNodesEditorCanvas::
63 wheelEvent( QWheelEvent* event )
66 std::pow( double( 2 ), event->delta( ) / double( 240 ) )
70 // -------------------------------------------------------------------------
71 void PipelineEditor::QNodesEditorCanvas::
72 dragEnterEvent( QDragEnterEvent* event )
74 const QMimeData* mime = event->mimeData( );
75 if( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) )
76 event->acceptProposedAction( );
79 // -------------------------------------------------------------------------
80 void PipelineEditor::QNodesEditorCanvas::
81 dragLeaveEvent( QDragLeaveEvent* event )
85 // -------------------------------------------------------------------------
86 void PipelineEditor::QNodesEditorCanvas::
87 dragMoveEvent( QDragMoveEvent* event )
91 // -------------------------------------------------------------------------
92 void PipelineEditor::QNodesEditorCanvas::
93 dropEvent( QDropEvent* event )
95 const QMimeData* mime = event->mimeData( );
96 if( !( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) ) )
99 event->acceptProposedAction( );
100 auto tree = dynamic_cast< QTreeWidget* >( event->source( ) );
104 QList< QTreeWidgetItem* > items = tree->selectedItems( );
105 for( auto iIt = items.begin( ); iIt != items.end( ); ++iIt )
106 this->m_Editor->createFilter(
107 ( *iIt )->text( 0 ).toStdString( ), event->pos( )
111 // -------------------------------------------------------------------------
112 void PipelineEditor::QNodesEditorCanvas::
113 _scaleView( qreal scaleFactor )
115 qreal factor = this->transform( ).
116 scale( scaleFactor, scaleFactor ).
117 mapRect( QRectF( 0, 0, 1, 1 ) ).
119 if( factor < qreal( 0.07 ) || factor > qreal( 100 ) )
121 this->scale( scaleFactor, scaleFactor );
124 // -------------------------------------------------------------------------
126 void PipelineEditor::QNodesEditorCanvas::
127 _createBlock( TFilter* f )
133 QNEBlock* b = new QNEBlock( 0, this->scene( ) );
134 b->addPort( f->GetName( ), 0, QNEPort::NamePort );
135 b->addPort( f->GetClassName( ).c_str( ), 0, QNEPort::TypePort );
138 std::set< std::string > inputs;
139 f->GetInputsNames( inputs );
140 for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt )
141 b->addInputPort( iIt->c_str( ) );
142 //in_ports[ vIt->first ][ *iIt ] = b->addInputPort( iIt->c_str( ) );
145 std::set< std::string > outputs;
146 f->GetOutputsNames( outputs );
147 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
148 b->addOutputPort( oIt->c_str( ) );
149 // out_ports[ vIt->first ][ *oIt ] = b->addOutputPort( oIt->c_str( ) );
151 // Keep a trace of this visual graph
152 this->m_Graph->InsertVertex( f->GetName( ), b );
154 // Add vertices and keep track of ports
155 std::map< std::string, std::map< std::string, QNEPort* > >
157 auto vIt = this->m_Workspace->GetGraph( )->BeginVertices( );
158 auto vIt_end = this->m_Workspace->GetGraph( )->EndVertices( );
159 for( ; vIt != vIt_end; ++vIt )
162 QNEBlock* b = new QNEBlock( 0, scene );
163 b->addPort( vIt->second->GetName( ), 0, QNEPort::NamePort );
164 b->addPort( vIt->second->GetClassName( ).c_str( ), 0, QNEPort::TypePort );
167 auto f = dynamic_cast< TFilter* >( vIt->second.GetPointer( ) );
172 std::set< std::string > inputs;
173 f->GetInputsNames( inputs );
174 for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt )
175 in_ports[ vIt->first ][ *iIt ] = b->addInputPort( iIt->c_str( ) );
178 std::set< std::string > outputs;
179 f->GetOutputsNames( outputs );
180 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
181 out_ports[ vIt->first ][ *oIt ] = b->addOutputPort( oIt->c_str( ) );
183 // Keep a trace of this visual graph
184 this->m_Graph->InsertVertex( vIt->first, b );
195 // -------------------------------------------------------------------------
199 if( this->m_Workspace == NULL )
203 // -------------------------------------------------------------------------
204 void GraphWidget::itemMoved()
207 timerId = startTimer(1000 / 25);
210 void GraphWidget::keyPressEvent(QKeyEvent *event)
212 switch (event->key()) {
214 centerNode->moveBy(0, -20);
217 centerNode->moveBy(0, 20);
220 centerNode->moveBy(-20, 0);
223 centerNode->moveBy(20, 0);
236 QGraphicsView::keyPressEvent(event);
240 void GraphWidget::timerEvent(QTimerEvent *event)
245 foreach (QGraphicsItem *item, scene()->items()) {
246 if (Node *node = qgraphicsitem_cast<Node *>(item))
250 foreach (Node *node, nodes)
251 node->calculateForces();
253 bool itemsMoved = false;
254 foreach (Node *node, nodes) {
265 void GraphWidget::wheelEvent(QWheelEvent *event)
267 scaleView(pow((double)2, -event->delta() / 240.0));
270 void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
275 QRectF sceneRect = rect;//this->sceneRect();
276 QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
277 QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
278 if (rightShadow.intersects(rect) || rightShadow.contains(rect))
279 painter->fillRect(rightShadow, Qt::darkGray);
280 if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
281 painter->fillRect(bottomShadow, Qt::darkGray);
284 QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
285 gradient.setColorAt(0, Qt::white);
286 gradient.setColorAt(1, Qt::lightGray);
287 painter->fillRect(rect.intersect(sceneRect), gradient);
288 painter->setBrush(Qt::NoBrush);
289 painter->drawRect(sceneRect);
291 #if !defined(Q_OS_SYMBIAN) && !defined(Q_WS_MAEMO_5)
293 QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4,
294 sceneRect.width() - 4, sceneRect.height() - 4);
295 QString message(tr("Click and drag the nodes around, and zoom with the mouse "
296 "wheel or the '+' and '-' keys"));
298 QFont font = painter->font();
300 font.setPointSize(14);
301 painter->setFont(font);
302 painter->setPen(Qt::lightGray);
303 painter->drawText(textRect.translated(2, 2), message);
304 painter->setPen(Qt::black);
305 painter->drawText(textRect, message);
309 void GraphWidget::scaleView(qreal scaleFactor)
311 qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
312 if (factor < 0.07 || factor > 100)
315 scale(scaleFactor, scaleFactor);
318 void GraphWidget::shuffle()
320 foreach (QGraphicsItem *item, scene()->items()) {
321 if (qgraphicsitem_cast<Node *>(item))
322 item->setPos(-150 + qrand() % 300, -150 + qrand() % 300);
326 void GraphWidget::zoomIn()
328 scaleView(qreal(1.2));
331 void GraphWidget::zoomOut()
333 scaleView(1 / qreal(1.2));