]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/SimpleMPRWidget.cxx
Pipeline editor added.
[cpPlugins.git] / lib / cpPlugins / Interface / SimpleMPRWidget.cxx
1 #include <cpPlugins/Interface/SimpleMPRWidget.h>
2
3 #ifdef cpPlugins_Interface_QT4
4
5 #include <cpPlugins/Interface/ui_SimpleMPRWidget.h>
6 #include <cpPlugins/Interface/DataObject.h>
7 #include <cpPlugins/Interface/Image.h>
8 #include <cpPlugins/Interface/Mesh.h>
9 // TODO: #include <QTreeWidgetItem>
10 #include <vtkRendererCollection.h>
11
12 double cpPlugins::Interface::SimpleMPRWidget::
13 cm_Colors[ 8 ][ 3 ] =
14 {
15   { 1.0, 0.0, 0.0 },
16   { 0.0, 1.0, 0.0 },
17   { 0.0, 0.0, 1.0 },
18   { 0.0, 1.0, 1.0 },
19   { 1.0, 0.0, 1.0 },
20   { 1.0, 1.0, 0.0 },
21   { 1.0, 0.5, 0.0 },
22   { 1.0, 0.0, 0.5 }
23 };
24
25 // -------------------------------------------------------------------------
26 cpPlugins::Interface::SimpleMPRWidget::
27 SimpleMPRWidget( QWidget* parent )
28   : QWidget( parent ),
29     m_UI( new Ui::SimpleMPRWidget ),
30     m_MainImage( "" )
31 {
32   this->m_UI->setupUi( this );
33
34   // Configure VTK widgets
35   this->m_VTK[ 0 ] = this->m_UI->VTK01;
36   this->m_VTK[ 1 ] = this->m_UI->VTK00;
37   this->m_VTK[ 2 ] = this->m_UI->VTK10;
38   this->m_VTK[ 3 ] = this->m_UI->VTK11;
39
40   this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
41   this->m_MPRObjects->SetRenderWindows(
42     this->m_VTK[ 0 ]->GetRenderWindow( ),
43     this->m_VTK[ 1 ]->GetRenderWindow( ),
44     this->m_VTK[ 2 ]->GetRenderWindow( ),
45     this->m_VTK[ 3 ]->GetRenderWindow( )
46     );
47
48   // Connect slots
49   QObject::connect(
50     this->m_UI->TopSplitter, SIGNAL( splitterMoved( int, int ) ),
51     this, SLOT( _SyncBottom( int, int ) )
52     );
53   QObject::connect(
54     this->m_UI->BottomSplitter, SIGNAL( splitterMoved( int, int ) ),
55     this, SLOT( _SyncTop( int, int ) )
56     );
57 }
58
59 // -------------------------------------------------------------------------
60 cpPlugins::Interface::SimpleMPRWidget::
61 ~SimpleMPRWidget( )
62 {
63   delete this->m_UI;
64 }
65
66 // -------------------------------------------------------------------------
67 bool cpPlugins::Interface::SimpleMPRWidget::
68 AddData(
69   cpPlugins::Interface::DataObject* data, const std::string& name,
70   const std::string& parent
71   )
72 {
73   if( name == "" )
74     return( false );
75
76   auto iIt = this->m_Data.find( name );
77   if( iIt == this->m_Data.end( ) )
78   {
79     if( parent != "" )
80     {
81       auto pIt = this->m_Data.find( parent );
82       if( pIt == this->m_Data.end( ) )
83         return( false );
84
85     } // fi
86
87     // Add new data
88     this->m_Data[ name ].SetSourceDataObject( data );
89
90     // Add to tree view
91     // TODO: this->_UpdateTreeItem( name, parent );
92     return( true );
93   }
94   else
95     return( false );
96 }
97
98 // -------------------------------------------------------------------------
99 const std::string& cpPlugins::Interface::SimpleMPRWidget::
100 GetMainImage( ) const
101 {
102   return( this->m_MainImage );
103 }
104
105 // -------------------------------------------------------------------------
106 bool cpPlugins::Interface::SimpleMPRWidget::
107 SetMainImage( const std::string& name )
108 {
109   auto iIt = this->m_Data.find( name );
110   if( iIt != this->m_Data.end( ) )
111   {
112     if( iIt->second.Tag == Data::IMAGE )
113     {
114       this->m_MainImage = name;
115       return( true );
116     }
117     else
118       return( false );
119   }
120   else
121     return( false );
122 }
123
124 // -------------------------------------------------------------------------
125 void cpPlugins::Interface::SimpleMPRWidget::
126 DeleteData( const std::string& name )
127 {
128   auto iIt = this->m_Data.find( name );
129   if( iIt != this->m_Data.end( ) )
130   {
131     this->m_Data.erase( iIt );
132
133     // Get children
134     std::vector< std::string > to_erase;
135     auto tIt = this->m_Tree.begin( );
136     for( ; tIt != this->m_Tree.end( ); ++tIt )
137       if( tIt->second == name )
138         to_erase.push_back( tIt->first );
139
140     // Delete from tree
141     tIt = this->m_Tree.find( name );
142     if( tIt != this->m_Tree.end( ) )
143       this->m_Tree.erase( tIt );
144
145     // Recursive erase
146     auto dIt = to_erase.begin( );
147     for( ; dIt != to_erase.end( ); ++dIt )
148       this->DeleteData( *dIt );
149
150     // Delete from tree widget
151     /* TODO
152        QTreeWidgetItem* item = this->_FindItemInTree( name );
153        if( item != NULL )
154        this->m_UI->LoadedData->removeItemWidget( item, 0 );
155     */
156
157     // Reset main image, just in case
158     if( this->m_Data.size( ) == 0 )
159       this->m_MainImage = "";
160
161   } // fi
162 }
163
164 // -------------------------------------------------------------------------
165 void cpPlugins::Interface::SimpleMPRWidget::
166 DeleteAllData( )
167 {
168   this->m_MPRObjects->Clear( );
169   this->m_Data.clear( );
170   this->m_Tree.clear( );
171   // TODO: this->m_UI->LoadedData->clear( );
172   this->m_MainImage = "";
173 }
174
175 // -------------------------------------------------------------------------
176 void cpPlugins::Interface::SimpleMPRWidget::
177 SetDataColor(
178   const std::string& name, const double& r, const double& g, const double& b
179   )
180 {
181   /*
182     auto iIt = this->m_Data.find( name );
183     if( iIt == this->m_Data.end( ) )
184     return;
185
186     if( iIt->second.Tag == Data::IMAGE )
187     {
188     }
189     else if( iIt->second.Tag == Data::MESH )
190     {
191     } // fi
192   */
193 }
194
195 // -------------------------------------------------------------------------
196 void cpPlugins::Interface::SimpleMPRWidget::
197 ShowData( const std::string& name )
198 {
199   auto iIt = this->m_Data.find( name );
200   if( iIt == this->m_Data.end( ) )
201     return;
202
203   if( iIt->second.Tag == Data::IMAGE )
204   {
205     if( name == this->m_MainImage )
206       this->m_MPRObjects->SetInputImage( iIt->second.Image );
207     else
208     {
209       unsigned int i = ( this->m_MPRObjects->GetNumberOfImages( ) - 1 ) % 8;
210       this->m_MPRObjects->AddBinaryImage(
211         iIt->second.Image,
212         Self::cm_Colors[ i ][ 0 ],
213         Self::cm_Colors[ i ][ 1 ],
214         Self::cm_Colors[ i ][ 2 ]
215         );
216     }
217     this->m_MPRObjects->Show( );
218   }
219   else if( iIt->second.Tag == Data::MESH )
220   {
221     /* TODO
222        vtkRenderer* ren =
223        this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
224        if( ren == NULL )
225        return;
226        ren->AddActor( iIt->second.GetMeshActor( ) );
227        this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
228     */
229   } // fi
230 }
231
232 // -------------------------------------------------------------------------
233 void cpPlugins::Interface::SimpleMPRWidget::
234 HideData( const std::string& name )
235 {
236 }
237
238 // -------------------------------------------------------------------------
239 vtkRenderWindowInteractor* cpPlugins::Interface::SimpleMPRWidget::
240 GetInteractor( unsigned int i )
241 {
242   if( i < 4 )
243   {
244     if( this->m_VTK[ i ] != NULL )
245       return( this->m_VTK[ i ]->GetInteractor( ) );
246     else
247       return( NULL );
248   }
249   else
250     return( NULL );
251 }
252
253 // -------------------------------------------------------------------------
254 /*
255   bool cpPlugins::Interface::SimpleMPRWidget::
256   ShowImage(
257   vtkImageData* image,
258   const std::string& name,
259   const std::string& parent
260   )
261   {
262   // Update tree view
263   QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
264   if( new_item == NULL )
265   return( false );
266
267   // Associate new data
268   this->m_Images[ name ] = image;
269   this->m_Tree[ name ] = parent;
270
271   // Show image and return
272   this->m_MPRObjects->AddImage( image );
273   return( true );
274   }
275
276   // -------------------------------------------------------------------------
277   bool cpPlugins::Interface::SimpleMPRWidget::
278   ShowImage(
279   vtkImageData* image,
280   const std::string& name,
281   const std::string& parent,
282   const double& r, const double& g, const double& b
283   )
284   {
285   // Update tree view
286   QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
287   if( new_item == NULL )
288   return( false );
289
290   // Associate new data
291   this->m_Images[ name ] = image;
292   this->m_Tree[ name ] = parent;
293
294   // Show image and return
295   this->m_MPRObjects->AddImage( image );
296   return( true );
297   }
298
299   // -------------------------------------------------------------------------
300   bool cpPlugins::Interface::SimpleMPRWidget::
301   ShowMesh(
302   vtkPolyData* mesh,
303   const std::string& name,
304   const std::string& parent
305   )
306   {
307   // Update tree view
308   QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
309   if( new_item == NULL )
310   return( false );
311
312   // Associate new data
313   PolyDataActor* actor = new PolyDataActor( mesh );
314   this->m_Meshes[ name ] = actor;
315   this->m_Tree[ name ] = parent;
316
317   // Show mesh
318   this->_Add3DActor( actor->Actor );
319   return( true );
320   }
321
322   // -------------------------------------------------------------------------
323   bool cpPlugins::Interface::SimpleMPRWidget::
324   ShowMesh(
325   vtkPolyData* mesh,
326   const std::string& name,
327   const std::string& parent,
328   const double& r, const double& g, const double& b
329   )
330   {
331   return false;
332   }
333
334   // -------------------------------------------------------------------------
335   void cpPlugins::Interface::SimpleMPRWidget::
336   ClearAll( )
337   {
338   this->m_MPRObjects->ClearAll( );
339   this->m_Images.clear( );
340   this->m_Meshes.clear( );
341   }
342 */
343
344 // -------------------------------------------------------------------------
345 std::string cpPlugins::Interface::SimpleMPRWidget::
346 GetSelectedData( ) const
347 {
348   /* TODO
349      QTreeWidgetItem* item = this->m_UI->LoadedData->currentItem( );
350      if( item != NULL )
351      return( item->text( 0 ).toStdString( ) );
352      else
353   */
354   return( "" );
355 }
356
357 // -------------------------------------------------------------------------
358 /* TODO
359 QTreeWidgetItem* cpPlugins::Interface::SimpleMPRWidget::
360 _FindItemInTree( const std::string& name ) const
361 {
362   QList< QTreeWidgetItem* > items =
363     this->m_UI->LoadedData->findItems(
364       name.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
365       );
366   if( items.size( ) > 0 )
367     return( items[ 0 ] );
368   else
369     return( NULL );
370 }
371
372 // -------------------------------------------------------------------------
373 QTreeWidgetItem* cpPlugins::Interface::SimpleMPRWidget::
374 _UpdateTreeItem( const std::string& name, const std::string& parent )
375 {
376   // Update tree view
377   QTreeWidgetItem* new_item = NULL;
378   if( parent != "" )
379   {
380     QTreeWidgetItem* parent_item = this->_FindItemInTree( parent );
381     if( parent_item != NULL )
382     {
383       QTreeWidgetItem* old_item = this->_FindItemInTree( name );
384       if( old_item == NULL )
385       {
386         new_item =
387           new QTreeWidgetItem( parent_item, QStringList( name.c_str( ) ) );
388         parent_item->setExpanded( true );
389
390       } // fi
391
392     } // fi
393   }
394   else
395   {
396     new_item = new QTreeWidgetItem(
397       ( QTreeWidgetItem* )( NULL ), QStringList( name.c_str( ) )
398       );
399     this->m_UI->LoadedData->addTopLevelItem( new_item );
400
401   } // fi
402   return( new_item );
403 }
404 */
405
406 // -------------------------------------------------------------------------
407 /*
408   void cpPlugins::Interface::SimpleMPRWidget::
409   _Add3DActor( vtkProp3D* prop )
410   {
411   vtkRenderer* ren =
412   this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
413   if( ren == NULL )
414   return;
415   ren->AddActor( prop );
416   this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
417   }
418 */
419
420 // -------------------------------------------------------------------------
421 void cpPlugins::Interface::SimpleMPRWidget::
422 _SyncBottom( int a, int b )
423 {
424   this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
425 }
426
427 // -------------------------------------------------------------------------
428 void cpPlugins::Interface::SimpleMPRWidget::
429 _SyncTop( int a, int b )
430 {
431   this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
432 }
433
434 // -------------------------------------------------------------------------
435 cpPlugins::Interface::SimpleMPRWidget::PolyDataActor::
436 PolyDataActor( )
437   : Mesh( NULL ),
438     Normals( NULL ),
439     Stripper( NULL ),
440     Mapper( NULL ),
441     Actor( NULL )
442 {
443 }
444
445 // -------------------------------------------------------------------------
446 cpPlugins::Interface::SimpleMPRWidget::PolyDataActor::
447 ~PolyDataActor( )
448 {
449   if( this->Actor != NULL )    this->Actor->Delete( );
450   if( this->Mapper != NULL )   this->Mapper->Delete( );
451   if( this->Stripper != NULL ) this->Stripper->Delete( );
452   if( this->Normals != NULL )  this->Normals->Delete( );
453 }
454
455 // -------------------------------------------------------------------------
456 void cpPlugins::Interface::SimpleMPRWidget::PolyDataActor::
457 Configure( vtkPolyData* pd )
458 {
459   if( pd ==  NULL )
460     return;
461
462   double range[ 2 ];
463   pd->GetScalarRange( range );
464
465   this->Normals = vtkPolyDataNormals::New( );
466   this->Stripper = vtkStripper::New( );
467   this->Mapper = vtkPolyDataMapper::New( );
468   this->Actor = vtkQuadricLODActor::New( );
469
470   this->Mesh = pd;
471   this->Normals->SetInputData( pd );
472   this->Normals->SetFeatureAngle( 60.0 );
473   this->Stripper->SetInputConnection( this->Normals->GetOutputPort( ) );
474   this->Mapper->SetInputConnection( this->Stripper->GetOutputPort( ) );
475   this->Mapper->UseLookupTableScalarRangeOff( );
476   this->Mapper->SetScalarRange(
477     range[ 0 ], ( ( range[ 1 ] - range[ 0 ] ) * 0.75 ) + range[ 0 ]
478     );
479   this->Actor->SetMapper( this->Mapper );
480   this->Actor->DeferLODConstructionOff( );
481 }
482
483 // -------------------------------------------------------------------------
484 cpPlugins::Interface::SimpleMPRWidget::Data::
485 Data( )
486 {
487   this->Tag = Data::IMAGE;
488   this->Source = NULL;
489   this->Image = NULL;
490 }
491
492 // -------------------------------------------------------------------------
493 cpPlugins::Interface::SimpleMPRWidget::Data::
494 ~Data( )
495 {
496 }
497
498 // -------------------------------------------------------------------------
499 cpPlugins::Interface::SimpleMPRWidget::
500 Data& cpPlugins::Interface::SimpleMPRWidget::Data::
501 operator=( const Data& data )
502 {
503   this->Tag = data.Tag;
504   this->Source = data.Source;
505   if( this->Tag == Data::IMAGE )
506     this->Image = data.Image;
507   else if( this->Tag == Data::MESH )
508     this->Mesh = data.Mesh;
509   return( *this );
510 }
511
512 // -------------------------------------------------------------------------
513 cpPlugins::Interface::
514 DataObject* cpPlugins::Interface::SimpleMPRWidget::Data::
515 GetSourceDataObject( )
516 {
517   return( this->Source );
518 }
519
520 // -------------------------------------------------------------------------
521 const cpPlugins::Interface::
522 DataObject* cpPlugins::Interface::SimpleMPRWidget::Data::
523 GetSourceDataObject( ) const
524 {
525   return( this->Source );
526 }
527
528 // -------------------------------------------------------------------------
529 void cpPlugins::Interface::SimpleMPRWidget::Data::
530 SetSourceDataObject( cpPlugins::Interface::DataObject* dobj )
531 {
532   typedef cpPlugins::Interface::Image _TImage;
533   typedef cpPlugins::Interface::Mesh  _TMesh;
534
535   this->Source = dobj;
536   _TImage* image = dynamic_cast< _TImage* >( dobj );
537   if( image != NULL )
538   {
539     this->Tag = Data::IMAGE;
540     this->Image = image->GetVTK< vtkImageData >( );
541   }
542   else
543   {
544     _TMesh* mesh = dynamic_cast< _TMesh* >( dobj );
545     if( mesh == NULL )
546       return;
547
548     this->Tag = Data::MESH;
549     this->Mesh.Configure( mesh->GetVTK< vtkPolyData >( ) );
550
551   } // fi
552 }
553
554 // -------------------------------------------------------------------------
555 vtkImageData* cpPlugins::Interface::SimpleMPRWidget::Data::
556 GetImage( )
557 {
558   if( this->Tag == Data::IMAGE )
559     return( this->Image );
560   else
561     return( NULL );
562 }
563
564 // -------------------------------------------------------------------------
565 vtkPolyData* cpPlugins::Interface::SimpleMPRWidget::Data::
566 GetMesh( )
567 {
568   if( this->Tag == Data::MESH )
569     return( this->Mesh.Mesh );
570   else
571     return( NULL );
572 }
573
574 // -------------------------------------------------------------------------
575 vtkProp* cpPlugins::Interface::SimpleMPRWidget::Data::
576 GetMeshActor( )
577 {
578   if( this->Tag == Data::MESH )
579     return( this->Mesh.Actor );
580   else
581     return( NULL );
582 }
583
584 #endif // cpPlugins_Interface_QT4
585
586 // eof - $RCSfile$