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