]> Creatis software - cpPlugins.git/blob - appli/cpPipelineEditor/GraphCanvas.cxx
3b3041cdcd8afd0364a5d16b3e404d42eccbbaba
[cpPlugins.git] / appli / cpPipelineEditor / GraphCanvas.cxx
1 #include "GraphCanvas.h"
2 #include "Edge.h"
3 #include "Node.h"
4
5 #include <QWheelEvent>
6
7 #include <cpPlugins/Interface/Workspace.h>
8
9 // -------------------------------------------------------------------------
10 PipelineEditor::GraphCanvas::
11 GraphCanvas( QWidget* parent )
12   : QGraphicsView( parent ),
13     m_Workspace( NULL )
14 {
15   QGraphicsScene* scene = new QGraphicsScene( this );
16   scene->setItemIndexMethod( QGraphicsScene::NoIndex );
17   this->setScene( scene );
18 }
19
20 // -------------------------------------------------------------------------
21 PipelineEditor::GraphCanvas::
22 ~GraphCanvas( )
23 {
24 }
25
26 // -------------------------------------------------------------------------
27 cpPlugins::Interface::Workspace* PipelineEditor::GraphCanvas::
28 workspace( )
29 {
30   return( this->m_Workspace );
31 }
32
33 // -------------------------------------------------------------------------
34 const cpPlugins::Interface::Workspace* PipelineEditor::GraphCanvas::
35 workspace( ) const
36 {
37   return( this->m_Workspace );
38 }
39
40 // -------------------------------------------------------------------------
41 void PipelineEditor::GraphCanvas::
42 setWorkspace( cpPlugins::Interface::Workspace* ws )
43 {
44   if( this->m_Workspace == ws )
45     return;
46   this->m_Workspace = ws;
47   QGraphicsScene* scene = this->scene( );
48
49   // Create graph
50   this->m_Graph = TGraph::New( );
51
52   // Add vertices
53   auto vIt = this->m_Workspace->GetGraph( )->BeginVertices( );
54   auto vIt_end = this->m_Workspace->GetGraph( )->EndVertices( );
55   for( ; vIt != vIt_end; ++vIt )
56   {
57     std::string label = vIt->second->GetName( ) + std::string( "\n" );
58     label += vIt->second->GetClassName( );
59
60     Node* node = new Node( this, vIt->second.GetPointer( ) );
61     this->m_Graph->InsertVertex( vIt->first, node );
62     scene->addItem( node );
63
64   } // rof
65
66   /* TODO
67      // Add edges
68      auto rIt = this->m_Workspace->GetGraph( )->BeginEdgesRows( );
69      auto rIt_end = this->m_Workspace->GetGraph( )->EndEdgesRows( );
70      for( ; rIt != rIt_end; ++rIt )
71      {
72      Node* a = this->m_Graph->GetVertex( rIt->first );
73      if( a == NULL )
74      continue;
75      auto cIt = rIt->second.begin( );
76      for( ; cIt != rIt->second.end( ); ++cIt )
77      {
78      Node* b = this->m_Graph->GetVertex( cIt->first );
79      if( b == NULL )
80      continue;
81      Edge* e = new Edge( a, b );
82      this->m_Graph->AddConnection( rIt->first, cIt->first, e );
83      scene->addItem( e );
84
85      } // rof
86
87      } // rof
88   */
89 }
90
91 // -------------------------------------------------------------------------
92 void PipelineEditor::GraphCanvas::
93 draw( )
94 {
95   if( this->m_Workspace == NULL )
96     return;
97 }
98
99 // -------------------------------------------------------------------------
100 void PipelineEditor::GraphCanvas::
101 itemMoved( )
102 {
103 }
104
105 // -------------------------------------------------------------------------
106 void PipelineEditor::GraphCanvas::
107 shuffle( )
108 {
109 }
110
111 // -------------------------------------------------------------------------
112 void PipelineEditor::GraphCanvas::
113 zoomIn( )
114 {
115 }
116
117 // -------------------------------------------------------------------------
118 void PipelineEditor::GraphCanvas::
119 zoomOut( )
120 {
121 }
122
123 // -------------------------------------------------------------------------
124 void PipelineEditor::GraphCanvas::
125 keyPressEvent( QKeyEvent* event )
126 {
127 }
128
129 // -------------------------------------------------------------------------
130 void PipelineEditor::GraphCanvas::
131 timerEvent( QTimerEvent* event )
132 {
133 }
134
135 // -------------------------------------------------------------------------
136 void PipelineEditor::GraphCanvas::
137 wheelEvent( QWheelEvent* event )
138 {
139   this->scaleView(
140     std::pow( double( 2 ), -event->delta( ) / double( 240 ) )
141     );
142 }
143
144 // -------------------------------------------------------------------------
145 void PipelineEditor::GraphCanvas::
146 drawBackground( QPainter* painter, const QRectF& rect )
147 {
148   this->QGraphicsView::drawBackground( painter, rect );
149 }
150
151 // -------------------------------------------------------------------------
152 void PipelineEditor::GraphCanvas::
153 scaleView( qreal scaleFactor )
154 {
155   qreal factor = this->transform( ).
156     scale( scaleFactor, scaleFactor ).
157     mapRect( QRectF( 0, 0, 1, 1 ) ).
158     width( );
159   if( factor < qreal( 0.07 ) || factor > qreal( 100 ) )
160     return;
161   this->scale( scaleFactor, scaleFactor );
162 }
163
164 /*
165   protected:
166     cpPlugins::Interface::Workspace* m_Workspace;
167     TGraph::Pointer m_Graph;
168   };
169 */
170
171 // eof - $RCSfile$
172
173
174 /*
175
176 // -------------------------------------------------------------------------
177 void GraphWidget::
178 draw( )
179 {
180   if( this->m_Workspace == NULL )
181     return;
182 }
183
184 // -------------------------------------------------------------------------
185 void GraphWidget::itemMoved()
186 {
187   if (!timerId)
188     timerId = startTimer(1000 / 25);
189 }
190
191 void GraphWidget::keyPressEvent(QKeyEvent *event)
192 {
193   switch (event->key()) {
194   case Qt::Key_Up:
195     centerNode->moveBy(0, -20);
196     break;
197   case Qt::Key_Down:
198     centerNode->moveBy(0, 20);
199     break;
200   case Qt::Key_Left:
201     centerNode->moveBy(-20, 0);
202     break;
203   case Qt::Key_Right:
204     centerNode->moveBy(20, 0);
205     break;
206   case Qt::Key_Plus:
207     zoomIn();
208     break;
209   case Qt::Key_Minus:
210     zoomOut();
211     break;
212   case Qt::Key_Space:
213   case Qt::Key_Enter:
214     shuffle();
215     break;
216   default:
217     QGraphicsView::keyPressEvent(event);
218   }
219 }
220
221 void GraphWidget::timerEvent(QTimerEvent *event)
222 {
223   Q_UNUSED(event);
224
225   QList<Node *> nodes;
226   foreach (QGraphicsItem *item, scene()->items()) {
227     if (Node *node = qgraphicsitem_cast<Node *>(item))
228       nodes << node;
229   }
230
231   foreach (Node *node, nodes)
232     node->calculateForces();
233
234   bool itemsMoved = false;
235   foreach (Node *node, nodes) {
236     if (node->advance())
237       itemsMoved = true;
238   }
239
240   if (!itemsMoved) {
241     killTimer(timerId);
242     timerId = 0;
243   }
244 }
245
246 void GraphWidget::wheelEvent(QWheelEvent *event)
247 {
248   scaleView(pow((double)2, -event->delta() / 240.0));
249 }
250
251 void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
252 {
253   //Q_UNUSED(rect);
254
255   // Shadow
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);
263
264   // Fill
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);
271
272 #if !defined(Q_OS_SYMBIAN) && !defined(Q_WS_MAEMO_5)
273   // Text
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"));
278
279   QFont font = painter->font();
280   font.setBold(true);
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);
287 #endif
288 }
289
290 void GraphWidget::scaleView(qreal scaleFactor)
291 {
292   qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
293   if (factor < 0.07 || factor > 100)
294     return;
295
296   scale(scaleFactor, scaleFactor);
297 }
298
299 void GraphWidget::shuffle()
300 {
301   foreach (QGraphicsItem *item, scene()->items()) {
302     if (qgraphicsitem_cast<Node *>(item))
303       item->setPos(-150 + qrand() % 300, -150 + qrand() % 300);
304   }
305 }
306
307 void GraphWidget::zoomIn()
308 {
309   scaleView(qreal(1.2));
310 }
311
312 void GraphWidget::zoomOut()
313 {
314   scaleView(1 / qreal(1.2));
315 }
316
317
318 */