]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/BaseMPRWidget.cxx
Getting ready for interactive initialization.
[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   } // fi
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->_UpdateItem( 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->_UpdateItem( 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->_UpdateItem( 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 _FindItem( 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 _UpdateItem( 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->_FindItem( parent );
346     if( parent_item != NULL )
347     {
348       QTreeWidgetItem* old_item = this->_FindItem( 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 void cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
400 Configure( vtkPolyData* pd )
401 {
402   if( pd ==  NULL )
403     return;
404
405   double range[ 2 ];
406   pd->GetScalarRange( range );
407
408   this->Normals = vtkSmartPointer< vtkPolyDataNormals >::New( );
409   this->Stripper = vtkSmartPointer< vtkStripper >::New( );
410   this->Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
411   this->Actor = vtkSmartPointer< vtkQuadricLODActor >::New( );
412
413   this->Mesh = pd;
414   this->Normals->SetInputData( pd );
415   this->Normals->SetFeatureAngle( 60.0 );
416   this->Stripper->SetInputConnection( this->Normals->GetOutputPort( ) );
417   this->Mapper->SetInputConnection( this->Stripper->GetOutputPort( ) );
418   this->Mapper->UseLookupTableScalarRangeOff( );
419   this->Mapper->SetScalarRange(
420     range[ 0 ], ( ( range[ 1 ] - range[ 0 ] ) * 0.75 ) + range[ 0 ]
421     );
422   this->Actor->SetMapper( this->Mapper );
423   this->Actor->DeferLODConstructionOff( );
424 }
425
426 #endif // cpPlugins_Interface_QT4
427
428 // eof - $RCSfile$