1 #include "GraphCanvas.h"
7 #include <cpPlugins/Interface/Workspace.h>
9 // -------------------------------------------------------------------------
10 PipelineEditor::GraphCanvas::
11 GraphCanvas( QWidget* parent )
12 : QGraphicsView( parent ),
15 QGraphicsScene* scene = new QGraphicsScene( this );
16 scene->setItemIndexMethod( QGraphicsScene::NoIndex );
17 this->setScene( scene );
20 // -------------------------------------------------------------------------
21 PipelineEditor::GraphCanvas::
26 // -------------------------------------------------------------------------
27 cpPlugins::Interface::Workspace* PipelineEditor::GraphCanvas::
30 return( this->m_Workspace );
33 // -------------------------------------------------------------------------
34 const cpPlugins::Interface::Workspace* PipelineEditor::GraphCanvas::
37 return( this->m_Workspace );
40 // -------------------------------------------------------------------------
41 void PipelineEditor::GraphCanvas::
42 setWorkspace( cpPlugins::Interface::Workspace* ws )
44 if( this->m_Workspace == ws )
46 this->m_Workspace = ws;
47 QGraphicsScene* scene = this->scene( );
50 this->m_Graph = TGraph::New( );
53 auto vIt = this->m_Workspace->GetGraph( )->BeginVertices( );
54 auto vIt_end = this->m_Workspace->GetGraph( )->EndVertices( );
55 for( ; vIt != vIt_end; ++vIt )
57 std::string label = vIt->second->GetName( ) + std::string( "\n" );
58 label += vIt->second->GetClassName( );
60 Node* node = new Node( this, vIt->second.GetPointer( ) );
61 this->m_Graph->InsertVertex( vIt->first, node );
62 scene->addItem( node );
68 auto rIt = this->m_Workspace->GetGraph( )->BeginEdgesRows( );
69 auto rIt_end = this->m_Workspace->GetGraph( )->EndEdgesRows( );
70 for( ; rIt != rIt_end; ++rIt )
72 Node* a = this->m_Graph->GetVertex( rIt->first );
75 auto cIt = rIt->second.begin( );
76 for( ; cIt != rIt->second.end( ); ++cIt )
78 Node* b = this->m_Graph->GetVertex( cIt->first );
81 Edge* e = new Edge( a, b );
82 this->m_Graph->AddConnection( rIt->first, cIt->first, e );
91 // -------------------------------------------------------------------------
92 void PipelineEditor::GraphCanvas::
95 if( this->m_Workspace == NULL )
99 // -------------------------------------------------------------------------
100 void PipelineEditor::GraphCanvas::
105 // -------------------------------------------------------------------------
106 void PipelineEditor::GraphCanvas::
111 // -------------------------------------------------------------------------
112 void PipelineEditor::GraphCanvas::
117 // -------------------------------------------------------------------------
118 void PipelineEditor::GraphCanvas::
123 // -------------------------------------------------------------------------
124 void PipelineEditor::GraphCanvas::
125 keyPressEvent( QKeyEvent* event )
129 // -------------------------------------------------------------------------
130 void PipelineEditor::GraphCanvas::
131 timerEvent( QTimerEvent* event )
135 // -------------------------------------------------------------------------
136 void PipelineEditor::GraphCanvas::
137 wheelEvent( QWheelEvent* event )
140 std::pow( double( 2 ), -event->delta( ) / double( 240 ) )
144 // -------------------------------------------------------------------------
145 void PipelineEditor::GraphCanvas::
146 drawBackground( QPainter* painter, const QRectF& rect )
148 this->QGraphicsView::drawBackground( painter, rect );
151 // -------------------------------------------------------------------------
152 void PipelineEditor::GraphCanvas::
153 scaleView( qreal scaleFactor )
155 qreal factor = this->transform( ).
156 scale( scaleFactor, scaleFactor ).
157 mapRect( QRectF( 0, 0, 1, 1 ) ).
159 if( factor < qreal( 0.07 ) || factor > qreal( 100 ) )
161 this->scale( scaleFactor, scaleFactor );
166 cpPlugins::Interface::Workspace* m_Workspace;
167 TGraph::Pointer m_Graph;
176 // -------------------------------------------------------------------------
180 if( this->m_Workspace == NULL )
184 // -------------------------------------------------------------------------
185 void GraphWidget::itemMoved()
188 timerId = startTimer(1000 / 25);
191 void GraphWidget::keyPressEvent(QKeyEvent *event)
193 switch (event->key()) {
195 centerNode->moveBy(0, -20);
198 centerNode->moveBy(0, 20);
201 centerNode->moveBy(-20, 0);
204 centerNode->moveBy(20, 0);
217 QGraphicsView::keyPressEvent(event);
221 void GraphWidget::timerEvent(QTimerEvent *event)
226 foreach (QGraphicsItem *item, scene()->items()) {
227 if (Node *node = qgraphicsitem_cast<Node *>(item))
231 foreach (Node *node, nodes)
232 node->calculateForces();
234 bool itemsMoved = false;
235 foreach (Node *node, nodes) {
246 void GraphWidget::wheelEvent(QWheelEvent *event)
248 scaleView(pow((double)2, -event->delta() / 240.0));
251 void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
256 QRectF sceneRect = rect;//this->sceneRect();
257 QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
258 QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
259 if (rightShadow.intersects(rect) || rightShadow.contains(rect))
260 painter->fillRect(rightShadow, Qt::darkGray);
261 if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
262 painter->fillRect(bottomShadow, Qt::darkGray);
265 QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
266 gradient.setColorAt(0, Qt::white);
267 gradient.setColorAt(1, Qt::lightGray);
268 painter->fillRect(rect.intersect(sceneRect), gradient);
269 painter->setBrush(Qt::NoBrush);
270 painter->drawRect(sceneRect);
272 #if !defined(Q_OS_SYMBIAN) && !defined(Q_WS_MAEMO_5)
274 QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4,
275 sceneRect.width() - 4, sceneRect.height() - 4);
276 QString message(tr("Click and drag the nodes around, and zoom with the mouse "
277 "wheel or the '+' and '-' keys"));
279 QFont font = painter->font();
281 font.setPointSize(14);
282 painter->setFont(font);
283 painter->setPen(Qt::lightGray);
284 painter->drawText(textRect.translated(2, 2), message);
285 painter->setPen(Qt::black);
286 painter->drawText(textRect, message);
290 void GraphWidget::scaleView(qreal scaleFactor)
292 qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
293 if (factor < 0.07 || factor > 100)
296 scale(scaleFactor, scaleFactor);
299 void GraphWidget::shuffle()
301 foreach (QGraphicsItem *item, scene()->items()) {
302 if (qgraphicsitem_cast<Node *>(item))
303 item->setPos(-150 + qrand() % 300, -150 + qrand() % 300);
307 void GraphWidget::zoomIn()
309 scaleView(qreal(1.2));
312 void GraphWidget::zoomOut()
314 scaleView(1 / qreal(1.2));