]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/BaseMPRWidget.cxx
052e374f51c879794a222e1cf336e59eecca1a66
[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 double cpPlugins::Interface::BaseMPRWidget::
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::BaseMPRWidget::
27 BaseMPRWidget( QWidget* parent )
28   : QWidget( parent ),
29     m_UI( new Ui::BaseMPRWidget ),
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::BaseMPRWidget::
61 ~BaseMPRWidget( )
62 {
63   delete this->m_UI;
64 }
65
66 // -------------------------------------------------------------------------
67 bool cpPlugins::Interface::BaseMPRWidget::
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     this->_UpdateTreeItem( name, parent );
92     return( true );
93   }
94   else
95     return( false );
96 }
97
98 // -------------------------------------------------------------------------
99 const std::string& cpPlugins::Interface::BaseMPRWidget::
100 GetMainImage( ) const
101 {
102   return( this->m_MainImage );
103 }
104
105 // -------------------------------------------------------------------------
106 bool cpPlugins::Interface::BaseMPRWidget::
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::BaseMPRWidget::
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     QTreeWidgetItem* item = this->_FindItemInTree( name );
152     if( item != NULL )
153       this->m_UI->LoadedData->removeItemWidget( item, 0 );
154
155     // Reset main image, just in case
156     if( this->m_Data.size( ) == 0 )
157       this->m_MainImage = "";
158
159   } // fi
160 }
161
162 // -------------------------------------------------------------------------
163 void cpPlugins::Interface::BaseMPRWidget::
164 DeleteAllData( )
165 {
166   this->m_MPRObjects->Clear( );
167   this->m_Data.clear( );
168   this->m_Tree.clear( );
169   this->m_UI->LoadedData->clear( );
170   this->m_MainImage = "";
171 }
172
173 // -------------------------------------------------------------------------
174 void cpPlugins::Interface::BaseMPRWidget::
175 SetDataColor(
176   const std::string& name, const double& r, const double& g, const double& b
177   )
178 {
179   /*
180     auto iIt = this->m_Data.find( name );
181     if( iIt == this->m_Data.end( ) )
182     return;
183
184     if( iIt->second.Tag == Data::IMAGE )
185     {
186     }
187     else if( iIt->second.Tag == Data::MESH )
188     {
189     } // fi
190   */
191 }
192
193 // -------------------------------------------------------------------------
194 void cpPlugins::Interface::BaseMPRWidget::
195 ShowData( const std::string& name )
196 {
197   auto iIt = this->m_Data.find( name );
198   if( iIt == this->m_Data.end( ) )
199     return;
200
201   if( iIt->second.Tag == Data::IMAGE )
202   {
203     if( name == this->m_MainImage )
204       this->m_MPRObjects->SetInputImage( iIt->second.Image );
205     else
206     {
207       unsigned int i = ( this->m_MPRObjects->GetNumberOfImages( ) - 1 ) % 8;
208       this->m_MPRObjects->AddBinaryImage(
209         iIt->second.Image,
210         Self::cm_Colors[ i ][ 0 ],
211         Self::cm_Colors[ i ][ 1 ],
212         Self::cm_Colors[ i ][ 2 ]
213         );
214     }
215     this->m_MPRObjects->Show( );
216   }
217   else if( iIt->second.Tag == Data::MESH )
218   {
219     /* TODO
220        vtkRenderer* ren =
221        this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
222        if( ren == NULL )
223        return;
224        ren->AddActor( iIt->second.GetMeshActor( ) );
225        this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
226     */
227   } // fi
228 }
229
230 // -------------------------------------------------------------------------
231 void cpPlugins::Interface::BaseMPRWidget::
232 HideData( const std::string& name )
233 {
234 }
235
236 // -------------------------------------------------------------------------
237 vtkRenderWindowInteractor* cpPlugins::Interface::BaseMPRWidget::
238 GetInteractor( unsigned int i )
239 {
240   if( i < 4 )
241   {
242     if( this->m_VTK[ i ] != NULL )
243       return( this->m_VTK[ i ]->GetInteractor( ) );
244     else
245       return( NULL );
246   }
247   else
248     return( NULL );
249 }
250
251 // -------------------------------------------------------------------------
252 /*
253   bool cpPlugins::Interface::BaseMPRWidget::
254   ShowImage(
255   vtkImageData* image,
256   const std::string& name,
257   const std::string& parent
258   )
259   {
260   // Update tree view
261   QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
262   if( new_item == NULL )
263   return( false );
264
265   // Associate new data
266   this->m_Images[ name ] = image;
267   this->m_Tree[ name ] = parent;
268
269   // Show image and return
270   this->m_MPRObjects->AddImage( image );
271   return( true );
272   }
273
274   // -------------------------------------------------------------------------
275   bool cpPlugins::Interface::BaseMPRWidget::
276   ShowImage(
277   vtkImageData* image,
278   const std::string& name,
279   const std::string& parent,
280   const double& r, const double& g, const double& b
281   )
282   {
283   // Update tree view
284   QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
285   if( new_item == NULL )
286   return( false );
287
288   // Associate new data
289   this->m_Images[ name ] = image;
290   this->m_Tree[ name ] = parent;
291
292   // Show image and return
293   this->m_MPRObjects->AddImage( image );
294   return( true );
295   }
296
297   // -------------------------------------------------------------------------
298   bool cpPlugins::Interface::BaseMPRWidget::
299   ShowMesh(
300   vtkPolyData* mesh,
301   const std::string& name,
302   const std::string& parent
303   )
304   {
305   // Update tree view
306   QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
307   if( new_item == NULL )
308   return( false );
309
310   // Associate new data
311   PolyDataActor* actor = new PolyDataActor( mesh );
312   this->m_Meshes[ name ] = actor;
313   this->m_Tree[ name ] = parent;
314
315   // Show mesh
316   this->_Add3DActor( actor->Actor );
317   return( true );
318   }
319
320   // -------------------------------------------------------------------------
321   bool cpPlugins::Interface::BaseMPRWidget::
322   ShowMesh(
323   vtkPolyData* mesh,
324   const std::string& name,
325   const std::string& parent,
326   const double& r, const double& g, const double& b
327   )
328   {
329   return false;
330   }
331
332   // -------------------------------------------------------------------------
333   void cpPlugins::Interface::BaseMPRWidget::
334   ClearAll( )
335   {
336   this->m_MPRObjects->ClearAll( );
337   this->m_Images.clear( );
338   this->m_Meshes.clear( );
339   }
340 */
341
342 // -------------------------------------------------------------------------
343 std::string cpPlugins::Interface::BaseMPRWidget::
344 GetSelectedData( ) const
345 {
346   QTreeWidgetItem* item = this->m_UI->LoadedData->currentItem( );
347   if( item != NULL )
348     return( item->text( 0 ).toStdString( ) );
349   else
350     return( "" );
351 }
352
353 // -------------------------------------------------------------------------
354 QTreeWidgetItem* cpPlugins::Interface::BaseMPRWidget::
355 _FindItemInTree( const std::string& name ) const
356 {
357   QList< QTreeWidgetItem* > items =
358     this->m_UI->LoadedData->findItems(
359       name.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
360       );
361   if( items.size( ) > 0 )
362     return( items[ 0 ] );
363   else
364     return( NULL );
365 }
366
367 // -------------------------------------------------------------------------
368 QTreeWidgetItem* cpPlugins::Interface::BaseMPRWidget::
369 _UpdateTreeItem( const std::string& name, const std::string& parent )
370 {
371   // Update tree view
372   QTreeWidgetItem* new_item = NULL;
373   if( parent != "" )
374   {
375     QTreeWidgetItem* parent_item = this->_FindItemInTree( parent );
376     if( parent_item != NULL )
377     {
378       QTreeWidgetItem* old_item = this->_FindItemInTree( name );
379       if( old_item == NULL )
380       {
381         new_item =
382           new QTreeWidgetItem( parent_item, QStringList( name.c_str( ) ) );
383         parent_item->setExpanded( true );
384
385       } // fi
386
387     } // fi
388   }
389   else
390   {
391     new_item = new QTreeWidgetItem(
392       ( QTreeWidgetItem* )( NULL ), QStringList( name.c_str( ) )
393       );
394     this->m_UI->LoadedData->addTopLevelItem( new_item );
395
396   } // fi
397   return( new_item );
398 }
399
400 // -------------------------------------------------------------------------
401 /*
402   void cpPlugins::Interface::BaseMPRWidget::
403   _Add3DActor( vtkProp3D* prop )
404   {
405   vtkRenderer* ren =
406   this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
407   if( ren == NULL )
408   return;
409   ren->AddActor( prop );
410   this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
411   }
412 */
413
414 // -------------------------------------------------------------------------
415 void cpPlugins::Interface::BaseMPRWidget::
416 _SyncBottom( int a, int b )
417 {
418   this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
419 }
420
421 // -------------------------------------------------------------------------
422 void cpPlugins::Interface::BaseMPRWidget::
423 _SyncTop( int a, int b )
424 {
425   this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
426 }
427
428 // -------------------------------------------------------------------------
429 cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
430 PolyDataActor( )
431   : Mesh( NULL ),
432     Normals( NULL ),
433     Stripper( NULL ),
434     Mapper( NULL ),
435     Actor( NULL )
436 {
437 }
438
439 // -------------------------------------------------------------------------
440 cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
441 ~PolyDataActor( )
442 {
443   if( this->Actor != NULL )    this->Actor->Delete( );
444   if( this->Mapper != NULL )   this->Mapper->Delete( );
445   if( this->Stripper != NULL ) this->Stripper->Delete( );
446   if( this->Normals != NULL )  this->Normals->Delete( );
447 }
448
449 // -------------------------------------------------------------------------
450 void cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
451 Configure( vtkPolyData* pd )
452 {
453   if( pd ==  NULL )
454     return;
455
456   double range[ 2 ];
457   pd->GetScalarRange( range );
458
459   this->Normals = vtkPolyDataNormals::New( );
460   this->Stripper = vtkStripper::New( );
461   this->Mapper = vtkPolyDataMapper::New( );
462   this->Actor = vtkQuadricLODActor::New( );
463
464   this->Mesh = pd;
465   this->Normals->SetInputData( pd );
466   this->Normals->SetFeatureAngle( 60.0 );
467   this->Stripper->SetInputConnection( this->Normals->GetOutputPort( ) );
468   this->Mapper->SetInputConnection( this->Stripper->GetOutputPort( ) );
469   this->Mapper->UseLookupTableScalarRangeOff( );
470   this->Mapper->SetScalarRange(
471     range[ 0 ], ( ( range[ 1 ] - range[ 0 ] ) * 0.75 ) + range[ 0 ]
472     );
473   this->Actor->SetMapper( this->Mapper );
474   this->Actor->DeferLODConstructionOff( );
475 }
476
477 // -------------------------------------------------------------------------
478 cpPlugins::Interface::BaseMPRWidget::Data::
479 Data( )
480 {
481   this->Tag = Data::IMAGE;
482   this->Source = NULL;
483   this->Image = NULL;
484 }
485
486 // -------------------------------------------------------------------------
487 cpPlugins::Interface::BaseMPRWidget::Data::
488 ~Data( )
489 {
490 }
491
492 // -------------------------------------------------------------------------
493 cpPlugins::Interface::BaseMPRWidget::
494 Data& cpPlugins::Interface::BaseMPRWidget::Data::
495 operator=( const Data& data )
496 {
497   this->Tag = data.Tag;
498   this->Source = data.Source;
499   if( this->Tag == Data::IMAGE )
500     this->Image = data.Image;
501   else if( this->Tag == Data::MESH )
502     this->Mesh = data.Mesh;
503   return( *this );
504 }
505
506 // -------------------------------------------------------------------------
507 cpPlugins::Interface::
508 DataObject* cpPlugins::Interface::BaseMPRWidget::Data::
509 GetSourceDataObject( )
510 {
511   return( this->Source );
512 }
513
514 // -------------------------------------------------------------------------
515 const cpPlugins::Interface::
516 DataObject* cpPlugins::Interface::BaseMPRWidget::Data::
517 GetSourceDataObject( ) const
518 {
519   return( this->Source );
520 }
521
522 // -------------------------------------------------------------------------
523 void cpPlugins::Interface::BaseMPRWidget::Data::
524 SetSourceDataObject( cpPlugins::Interface::DataObject* dobj )
525 {
526   typedef cpPlugins::Interface::Image _TImage;
527   typedef cpPlugins::Interface::Mesh  _TMesh;
528
529   this->Source = dobj;
530   _TImage* image = dynamic_cast< _TImage* >( dobj );
531   if( image != NULL )
532   {
533     this->Tag = Data::IMAGE;
534     this->Image = image->GetVTK< vtkImageData >( );
535   }
536   else
537   {
538     _TMesh* mesh = dynamic_cast< _TMesh* >( dobj );
539     if( mesh == NULL )
540       return;
541
542     this->Tag = Data::MESH;
543     this->Mesh.Configure( mesh->GetVTK< vtkPolyData >( ) );
544
545   } // fi
546 }
547
548 // -------------------------------------------------------------------------
549 vtkImageData* cpPlugins::Interface::BaseMPRWidget::Data::
550 GetImage( )
551 {
552   if( this->Tag == Data::IMAGE )
553     return( this->Image );
554   else
555     return( NULL );
556 }
557
558 // -------------------------------------------------------------------------
559 vtkPolyData* cpPlugins::Interface::BaseMPRWidget::Data::
560 GetMesh( )
561 {
562   if( this->Tag == Data::MESH )
563     return( this->Mesh.Mesh );
564   else
565     return( NULL );
566 }
567
568 // -------------------------------------------------------------------------
569 vtkProp* cpPlugins::Interface::BaseMPRWidget::Data::
570 GetMeshActor( )
571 {
572   if( this->Tag == Data::MESH )
573     return( this->Mesh.Actor );
574   else
575     return( NULL );
576 }
577
578 #endif // cpPlugins_Interface_QT4
579
580 // eof - $RCSfile$