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