]> Creatis software - cpPlugins.git/blob - appli/cpPipelineEditor/QNodesEditorCanvas.cxx
...
[cpPlugins.git] / appli / cpPipelineEditor / QNodesEditorCanvas.cxx
1 #include "QNodesEditorCanvas.h"
2 #include "QNodesEditor.h"
3 #include "QNEBlock.h"
4 #include "QNEConnection.h"
5 #include "QNEPort.h"
6
7 #include <QDragEnterEvent>
8 #include <QWheelEvent>
9 #include <QTreeWidget>
10
11 // -------------------------------------------------------------------------
12 PipelineEditor::QNodesEditorCanvas::
13 QNodesEditorCanvas( QWidget* parent )
14   : QGraphicsView( parent )
15 {
16   QGraphicsScene* scene = new QGraphicsScene( this );
17   this->setScene( scene );
18   this->setRenderHint( QPainter::Antialiasing );
19   this->setAcceptDrops( true );
20
21   this->m_Editor = new QNodesEditor( this );
22   this->m_Editor->install( scene );
23 }
24
25 // -------------------------------------------------------------------------
26 PipelineEditor::QNodesEditorCanvas::
27 ~QNodesEditorCanvas( )
28 {
29 }
30
31 // -------------------------------------------------------------------------
32 PipelineEditor::
33 QNodesEditor* PipelineEditor::QNodesEditorCanvas::
34 editor( )
35 {
36   return( this->m_Editor );
37 }
38
39 // -------------------------------------------------------------------------
40 const PipelineEditor::
41 QNodesEditor* PipelineEditor::QNodesEditorCanvas::
42 editor( ) const
43 {
44   return( this->m_Editor );
45 }
46
47 // -------------------------------------------------------------------------
48 /* TODO
49    void PipelineEditor::QNodesEditorCanvas::
50    keyPressEvent( QKeyEvent* event )
51    {
52    }
53
54    // -------------------------------------------------------------------------
55    void PipelineEditor::QNodesEditorCanvas::
56    timerEvent( QTimerEvent* event )
57    {
58    }
59 */
60
61 // -------------------------------------------------------------------------
62 void PipelineEditor::QNodesEditorCanvas::
63 wheelEvent( QWheelEvent* event )
64 {
65   this->_scaleView(
66     std::pow( double( 2 ), event->delta( ) / double( 240 ) )
67     );
68 }
69
70 // -------------------------------------------------------------------------
71 void PipelineEditor::QNodesEditorCanvas::
72 dragEnterEvent( QDragEnterEvent* event )
73 {
74   const QMimeData* mime = event->mimeData( );
75   if( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) )
76     event->acceptProposedAction( );
77 }
78
79 // -------------------------------------------------------------------------
80 void PipelineEditor::QNodesEditorCanvas::
81 dragLeaveEvent( QDragLeaveEvent* event )
82 {
83 }
84
85 // -------------------------------------------------------------------------
86 void PipelineEditor::QNodesEditorCanvas::
87 dragMoveEvent( QDragMoveEvent* event )
88 {
89 }
90
91 // -------------------------------------------------------------------------
92 void PipelineEditor::QNodesEditorCanvas::
93 dropEvent( QDropEvent* event )
94 {
95   const QMimeData* mime = event->mimeData( );
96   if( !( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) ) )
97     return;
98
99   event->acceptProposedAction( );
100   auto tree = dynamic_cast< QTreeWidget* >( event->source( ) );
101   if( tree == NULL )
102     return;
103
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( ), this->mapToScene( event->pos( ) )
108       );
109 }
110
111 // -------------------------------------------------------------------------
112 void PipelineEditor::QNodesEditorCanvas::
113 _scaleView( qreal scaleFactor )
114 {
115   qreal factor = this->transform( ).
116     scale( scaleFactor, scaleFactor ).
117     mapRect( QRectF( 0, 0, 1, 1 ) ).
118     width( );
119   if( factor < qreal( 0.07 ) || factor > qreal( 100 ) )
120     return;
121   this->scale( scaleFactor, scaleFactor );
122 }
123
124 // -------------------------------------------------------------------------
125   /*
126 void PipelineEditor::QNodesEditorCanvas::
127 _createBlock( TFilter* f )
128 {
129   if( f == NULL )
130     return;
131
132   // Add block
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 );
136
137   // Add input ports
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( ) );
143
144   // Add output ports
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( ) );
150
151   // Keep a trace of this visual graph
152   this->m_Graph->InsertVertex( f->GetName( ), b );
153
154   // Add vertices and keep track of ports
155     std::map< std::string, std::map< std::string, QNEPort* > >
156     in_ports, out_ports;
157     auto vIt = this->m_Workspace->GetGraph( )->BeginVertices( );
158     auto vIt_end = this->m_Workspace->GetGraph( )->EndVertices( );
159     for( ; vIt != vIt_end; ++vIt )
160     {
161     // Add block
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 );
165
166     // Get filter
167     auto f = dynamic_cast< TFilter* >( vIt->second.GetPointer( ) );
168     if( f == NULL )
169     continue;
170
171     // Add input ports
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( ) );
176
177     // Add output ports
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( ) );
182
183     // Keep a trace of this visual graph
184     this->m_Graph->InsertVertex( vIt->first, b );
185
186     } // rof
187 }
188   */
189
190 // eof - $RCSfile$
191
192
193 /*
194
195 // -------------------------------------------------------------------------
196 void GraphWidget::
197 draw( )
198 {
199   if( this->m_Workspace == NULL )
200     return;
201 }
202
203 // -------------------------------------------------------------------------
204 void GraphWidget::itemMoved()
205 {
206   if (!timerId)
207     timerId = startTimer(1000 / 25);
208 }
209
210 void GraphWidget::keyPressEvent(QKeyEvent *event)
211 {
212   switch (event->key()) {
213   case Qt::Key_Up:
214     centerNode->moveBy(0, -20);
215     break;
216   case Qt::Key_Down:
217     centerNode->moveBy(0, 20);
218     break;
219   case Qt::Key_Left:
220     centerNode->moveBy(-20, 0);
221     break;
222   case Qt::Key_Right:
223     centerNode->moveBy(20, 0);
224     break;
225   case Qt::Key_Plus:
226     zoomIn();
227     break;
228   case Qt::Key_Minus:
229     zoomOut();
230     break;
231   case Qt::Key_Space:
232   case Qt::Key_Enter:
233     shuffle();
234     break;
235   default:
236     QGraphicsView::keyPressEvent(event);
237   }
238 }
239
240 void GraphWidget::timerEvent(QTimerEvent *event)
241 {
242   Q_UNUSED(event);
243
244   QList<Node *> nodes;
245   foreach (QGraphicsItem *item, scene()->items()) {
246     if (Node *node = qgraphicsitem_cast<Node *>(item))
247       nodes << node;
248   }
249
250   foreach (Node *node, nodes)
251     node->calculateForces();
252
253   bool itemsMoved = false;
254   foreach (Node *node, nodes) {
255     if (node->advance())
256       itemsMoved = true;
257   }
258
259   if (!itemsMoved) {
260     killTimer(timerId);
261     timerId = 0;
262   }
263 }
264
265 void GraphWidget::wheelEvent(QWheelEvent *event)
266 {
267   scaleView(pow((double)2, -event->delta() / 240.0));
268 }
269
270 void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
271 {
272   //Q_UNUSED(rect);
273
274   // Shadow
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);
282
283   // Fill
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);
290
291 #if !defined(Q_OS_SYMBIAN) && !defined(Q_WS_MAEMO_5)
292   // Text
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"));
297
298   QFont font = painter->font();
299   font.setBold(true);
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);
306 #endif
307 }
308
309 void GraphWidget::scaleView(qreal scaleFactor)
310 {
311   qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
312   if (factor < 0.07 || factor > 100)
313     return;
314
315   scale(scaleFactor, scaleFactor);
316 }
317
318 void GraphWidget::shuffle()
319 {
320   foreach (QGraphicsItem *item, scene()->items()) {
321     if (qgraphicsitem_cast<Node *>(item))
322       item->setPos(-150 + qrand() % 300, -150 + qrand() % 300);
323   }
324 }
325
326 void GraphWidget::zoomIn()
327 {
328   scaleView(qreal(1.2));
329 }
330
331 void GraphWidget::zoomOut()
332 {
333   scaleView(1 / qreal(1.2));
334 }
335
336
337 */