]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/BaseMPRWidget.cxx
e7f270ce7700de29c530d35d531570b840bb976e
[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 <QTreeWidgetItem>
7 #include <vtkRendererCollection.h>
8
9 // -------------------------------------------------------------------------
10 cpPlugins::Interface::BaseMPRWidget::
11 BaseMPRWidget( QWidget* parent )
12   : QWidget( parent ),
13     m_UI( new Ui::BaseMPRWidget ),
14     m_MainImage( "" )
15 {
16   this->m_UI->setupUi( this );
17
18   // Configure VTK widgets
19   this->m_VTK[ 0 ] = this->m_UI->VTK01;
20   this->m_VTK[ 1 ] = this->m_UI->VTK00;
21   this->m_VTK[ 2 ] = this->m_UI->VTK10;
22   this->m_VTK[ 3 ] = this->m_UI->VTK11;
23
24   this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
25   this->m_MPRObjects->SetRenderWindows(
26     this->m_VTK[ 0 ]->GetRenderWindow( ),
27     this->m_VTK[ 1 ]->GetRenderWindow( ),
28     this->m_VTK[ 2 ]->GetRenderWindow( ),
29     this->m_VTK[ 3 ]->GetRenderWindow( )
30     );
31
32   // Connect slots
33   QObject::connect(
34     this->m_UI->TopSplitter, SIGNAL( splitterMoved( int, int ) ),
35     this, SLOT( _SyncBottom( int, int ) )
36     );
37   QObject::connect(
38     this->m_UI->BottomSplitter, SIGNAL( splitterMoved( int, int ) ),
39     this, SLOT( _SyncTop( int, int ) )
40     );
41 }
42
43 // -------------------------------------------------------------------------
44 cpPlugins::Interface::BaseMPRWidget::
45 ~BaseMPRWidget( )
46 {
47   delete this->m_UI;
48 }
49
50 // -------------------------------------------------------------------------
51 bool cpPlugins::Interface::BaseMPRWidget::
52 AddImage(
53   vtkImageData* image, const std::string& name, const std::string& parent
54   )
55 {
56   if( name == "" )
57     return( false );
58
59   if( parent == "" )
60     this->DeleteAllData( );
61   
62   auto iIt = this->m_Data.find( name );
63   if( iIt == this->m_Data.end( ) )
64   {
65     // Update tree
66     if( parent != "" )
67     {
68       auto pIt = this->m_Data.find( parent );
69       if( pIt != this->m_Data.end( ) )
70         this->m_Tree[ name ] = parent;
71       else
72         return( false );
73     }
74     else
75       this->m_MainImage = name;
76     
77     // Add new data
78     Data nd;
79     nd.Tag = Data::IMAGE;
80     nd.Image = image;
81     this->m_Data[ name ] = nd;
82
83     // Add to tree view
84     this->_UpdateItem( name, parent );
85     return( true );
86   }
87   else
88     return( false );
89 }
90
91 // -------------------------------------------------------------------------
92 bool cpPlugins::Interface::BaseMPRWidget::
93 AddMesh(
94   vtkPolyData* mesh, const std::string& name, const std::string& parent
95   )
96 {
97   if( name == "" || parent == "" )
98     return( false );
99   
100   auto iIt = this->m_Data.find( name );
101   if( iIt == this->m_Data.end( ) )
102   {
103     // Add new data
104     Data nd;
105     nd.Tag = Data::MESH;
106     nd.Mesh.Configure( mesh );
107     this->m_Data[ name ] = nd;
108
109     // Update tree
110     auto pIt = this->m_Data.find( parent );
111     if( pIt != this->m_Data.end( ) )
112       this->m_Tree[ name ] = parent;
113     else
114       return( false );
115     
116     // Add to tree view
117     this->_UpdateItem( name, parent );
118     return( true );
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->_FindItem( 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->ClearAll( );
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   auto iIt = this->m_Data.find( name );
180   if( iIt == this->m_Data.end( ) )
181     return;
182
183   if( iIt->second.Tag == Data::IMAGE )
184   {
185   }
186   else if( iIt->second.Tag == Data::MESH )
187   {
188   } // fi
189 }
190
191 // -------------------------------------------------------------------------
192 void cpPlugins::Interface::BaseMPRWidget::
193 ShowData( const std::string& name )
194 {
195   auto iIt = this->m_Data.find( name );
196   if( iIt == this->m_Data.end( ) )
197     return;
198
199   if( iIt->second.Tag == Data::IMAGE )
200   {
201     this->m_MPRObjects->AddImage( iIt->second.Image );
202   }
203   else if( iIt->second.Tag == Data::MESH )
204   {
205     vtkRenderer* ren =
206       this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
207     if( ren == NULL )
208       return;
209     ren->AddActor( iIt->second.GetMeshActor( ) );
210     this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
211
212   } // fi
213 }
214
215 // -------------------------------------------------------------------------
216 vtkRenderWindowInteractor* cpPlugins::Interface::BaseMPRWidget::
217 GetInteractor( unsigned int i )
218 {
219   if( i < 4 )
220   {
221     if( this->m_VTK[ i ] != NULL )
222       return( this->m_VTK[ i ]->GetInteractor( ) );
223     else
224       return( NULL );
225   }
226   else
227     return( NULL );
228 }
229
230 // -------------------------------------------------------------------------
231 /*
232   bool cpPlugins::Interface::BaseMPRWidget::
233   ShowImage(
234   vtkImageData* image,
235   const std::string& name,
236   const std::string& parent
237   )
238   {
239   // Update tree view
240   QTreeWidgetItem* new_item = this->_UpdateItem( name, parent );
241   if( new_item == NULL )
242   return( false );
243
244   // Associate new data
245   this->m_Images[ name ] = image;
246   this->m_Tree[ name ] = parent;
247
248   // Show image and return
249   this->m_MPRObjects->AddImage( image );
250   return( true );
251   }
252
253   // -------------------------------------------------------------------------
254   bool cpPlugins::Interface::BaseMPRWidget::
255   ShowImage(
256   vtkImageData* image,
257   const std::string& name,
258   const std::string& parent,
259   const double& r, const double& g, const double& b
260   )
261   {
262   // Update tree view
263   QTreeWidgetItem* new_item = this->_UpdateItem( name, parent );
264   if( new_item == NULL )
265   return( false );
266
267   // Associate new data
268   this->m_Images[ name ] = image;
269   this->m_Tree[ name ] = parent;
270
271   // Show image and return
272   this->m_MPRObjects->AddImage( image );
273   return( true );
274   }
275
276   // -------------------------------------------------------------------------
277   bool cpPlugins::Interface::BaseMPRWidget::
278   ShowMesh(
279   vtkPolyData* mesh,
280   const std::string& name,
281   const std::string& parent
282   )
283   {
284   // Update tree view
285   QTreeWidgetItem* new_item = this->_UpdateItem( name, parent );
286   if( new_item == NULL )
287   return( false );
288
289   // Associate new data
290   PolyDataActor* actor = new PolyDataActor( mesh );
291   this->m_Meshes[ name ] = actor;
292   this->m_Tree[ name ] = parent;
293
294   // Show mesh
295   this->_Add3DActor( actor->Actor );
296   return( true );
297   }
298
299   // -------------------------------------------------------------------------
300   bool cpPlugins::Interface::BaseMPRWidget::
301   ShowMesh(
302   vtkPolyData* mesh,
303   const std::string& name,
304   const std::string& parent,
305   const double& r, const double& g, const double& b
306   )
307   {
308   return false;
309   }
310
311   // -------------------------------------------------------------------------
312   void cpPlugins::Interface::BaseMPRWidget::
313   ClearAll( )
314   {
315   this->m_MPRObjects->ClearAll( );
316   this->m_Images.clear( );
317   this->m_Meshes.clear( );
318   }
319 */
320
321 // -------------------------------------------------------------------------
322 std::string cpPlugins::Interface::BaseMPRWidget::
323 GetSelectedData( ) const
324 {
325   QTreeWidgetItem* item = this->m_UI->LoadedData->currentItem( );
326   if( item != NULL )
327     return( item->text( 0 ).toStdString( ) );
328   else
329     return( "" );
330 }
331
332 // -------------------------------------------------------------------------
333 QTreeWidgetItem* cpPlugins::Interface::BaseMPRWidget::
334 _FindItem( const std::string& name ) const
335 {
336   QList< QTreeWidgetItem* > items =
337     this->m_UI->LoadedData->findItems( name.c_str( ), Qt::MatchExactly );
338   if( items.size( ) > 0 )
339     return( items[ 0 ] );
340   else
341     return( NULL );
342 }
343
344 // -------------------------------------------------------------------------
345 QTreeWidgetItem* cpPlugins::Interface::BaseMPRWidget::
346 _UpdateItem( const std::string& name, const std::string& parent )
347 {
348   // Update tree view
349   QTreeWidgetItem* new_item = NULL;
350   if( parent != "" )
351   {
352     QTreeWidgetItem* parent_item = this->_FindItem( parent );
353     if( parent_item != NULL )
354     {
355       QTreeWidgetItem* old_item = this->_FindItem( name );
356       if( old_item == NULL )
357       {
358         new_item =
359           new QTreeWidgetItem( parent_item, QStringList( name.c_str( ) ) );
360         parent_item->setExpanded( true );
361
362       } // fi
363
364     } // fi
365   }
366   else
367   {
368     new_item = new QTreeWidgetItem(
369       ( QTreeWidgetItem* )( NULL ), QStringList( name.c_str( ) )
370       );
371     this->m_UI->LoadedData->addTopLevelItem( new_item );
372
373   } // fi
374   return( new_item );
375 }
376
377 // -------------------------------------------------------------------------
378 /*
379   void cpPlugins::Interface::BaseMPRWidget::
380   _Add3DActor( vtkProp3D* prop )
381   {
382   vtkRenderer* ren =
383   this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
384   if( ren == NULL )
385   return;
386   ren->AddActor( prop );
387   this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
388   }
389 */
390
391 // -------------------------------------------------------------------------
392 void cpPlugins::Interface::BaseMPRWidget::
393 _SyncBottom( int a, int b )
394 {
395   this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
396 }
397
398 // -------------------------------------------------------------------------
399 void cpPlugins::Interface::BaseMPRWidget::
400 _SyncTop( int a, int b )
401 {
402   this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
403 }
404
405 // -------------------------------------------------------------------------
406 cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
407 PolyDataActor( )
408   : Mesh( NULL ),
409     Normals( NULL ),
410     Stripper( NULL ),
411     Mapper( NULL ),
412     Actor( NULL )
413 {
414 }
415
416 // -------------------------------------------------------------------------
417 cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
418 ~PolyDataActor( )
419 {
420   if( this->Actor != NULL )    this->Actor->Delete( );
421   if( this->Mapper != NULL )   this->Mapper->Delete( );
422   if( this->Stripper != NULL ) this->Stripper->Delete( );
423   if( this->Normals != NULL )  this->Normals->Delete( );
424 }
425
426 // -------------------------------------------------------------------------
427 void cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
428 Configure( vtkPolyData* pd )
429 {
430   if( pd ==  NULL )
431     return;
432
433   double range[ 2 ];
434   pd->GetScalarRange( range );
435
436   this->Normals = vtkPolyDataNormals::New( );
437   this->Stripper = vtkStripper::New( );
438   this->Mapper = vtkPolyDataMapper::New( );
439   this->Actor = vtkQuadricLODActor::New( );
440
441   this->Mesh = pd;
442   this->Normals->SetInputData( pd );
443   this->Normals->SetFeatureAngle( 60.0 );
444   this->Stripper->SetInputConnection( this->Normals->GetOutputPort( ) );
445   this->Mapper->SetInputConnection( this->Stripper->GetOutputPort( ) );
446   this->Mapper->UseLookupTableScalarRangeOff( );
447   this->Mapper->SetScalarRange(
448     range[ 0 ], ( ( range[ 1 ] - range[ 0 ] ) * 0.75 ) + range[ 0 ]
449     );
450   this->Actor->SetMapper( this->Mapper );
451   this->Actor->DeferLODConstructionOff( );
452 }
453
454 #endif // cpPlugins_Interface_QT4
455
456 // eof - $RCSfile$