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