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