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