1 #include <cpPlugins/Interface/BaseMPRWidget.h>
3 #ifdef cpPlugins_Interface_QT4
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>
12 // -------------------------------------------------------------------------
13 cpPlugins::Interface::BaseMPRWidget::
14 BaseMPRWidget( QWidget* parent )
16 m_UI( new Ui::BaseMPRWidget ),
19 this->m_UI->setupUi( this );
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;
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( )
37 this->m_UI->TopSplitter, SIGNAL( splitterMoved( int, int ) ),
38 this, SLOT( _SyncBottom( int, int ) )
41 this->m_UI->BottomSplitter, SIGNAL( splitterMoved( int, int ) ),
42 this, SLOT( _SyncTop( int, int ) )
46 // -------------------------------------------------------------------------
47 cpPlugins::Interface::BaseMPRWidget::
53 // -------------------------------------------------------------------------
54 bool cpPlugins::Interface::BaseMPRWidget::
56 cpPlugins::Interface::DataObject* data, const std::string& name,
57 const std::string& parent
63 auto iIt = this->m_Data.find( name );
64 if( iIt == this->m_Data.end( ) )
68 auto pIt = this->m_Data.find( parent );
69 if( pIt == this->m_Data.end( ) )
75 this->m_Data[ name ].SetSourceDataObject( data );
78 this->_UpdateTreeItem( name, parent );
85 // -------------------------------------------------------------------------
86 bool cpPlugins::Interface::BaseMPRWidget::
87 SetMainImage( const std::string& name )
89 auto iIt = this->m_Data.find( name );
90 if( iIt != this->m_Data.end( ) )
92 if( iIt->second.Tag == Data::IMAGE )
94 this->m_MainImage = name;
104 // -------------------------------------------------------------------------
105 void cpPlugins::Interface::BaseMPRWidget::
106 DeleteData( const std::string& name )
108 auto iIt = this->m_Data.find( name );
109 if( iIt != this->m_Data.end( ) )
111 this->m_Data.erase( iIt );
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 );
121 tIt = this->m_Tree.find( name );
122 if( tIt != this->m_Tree.end( ) )
123 this->m_Tree.erase( tIt );
126 auto dIt = to_erase.begin( );
127 for( ; dIt != to_erase.end( ); ++dIt )
128 this->DeleteData( *dIt );
130 // Delete from tree widget
131 QTreeWidgetItem* item = this->_FindItemInTree( name );
133 this->m_UI->LoadedData->removeItemWidget( item, 0 );
135 // Reset main image, just in case
136 if( this->m_Data.size( ) == 0 )
137 this->m_MainImage = "";
142 // -------------------------------------------------------------------------
143 void cpPlugins::Interface::BaseMPRWidget::
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 = "";
153 // -------------------------------------------------------------------------
154 void cpPlugins::Interface::BaseMPRWidget::
156 const std::string& name, const double& r, const double& g, const double& b
160 auto iIt = this->m_Data.find( name );
161 if( iIt == this->m_Data.end( ) )
164 if( iIt->second.Tag == Data::IMAGE )
167 else if( iIt->second.Tag == Data::MESH )
173 // -------------------------------------------------------------------------
174 void cpPlugins::Interface::BaseMPRWidget::
175 ShowData( const std::string& name )
177 auto iIt = this->m_Data.find( name );
178 if( iIt == this->m_Data.end( ) )
181 if( iIt->second.Tag == Data::IMAGE )
183 this->m_MPRObjects->AddImage( iIt->second.Image );
185 else if( iIt->second.Tag == Data::MESH )
189 this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
192 ren->AddActor( iIt->second.GetMeshActor( ) );
193 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
198 // -------------------------------------------------------------------------
199 void cpPlugins::Interface::BaseMPRWidget::
200 HideData( const std::string& name )
204 // -------------------------------------------------------------------------
205 vtkRenderWindowInteractor* cpPlugins::Interface::BaseMPRWidget::
206 GetInteractor( unsigned int i )
210 if( this->m_VTK[ i ] != NULL )
211 return( this->m_VTK[ i ]->GetInteractor( ) );
219 // -------------------------------------------------------------------------
221 bool cpPlugins::Interface::BaseMPRWidget::
224 const std::string& name,
225 const std::string& parent
229 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
230 if( new_item == NULL )
233 // Associate new data
234 this->m_Images[ name ] = image;
235 this->m_Tree[ name ] = parent;
237 // Show image and return
238 this->m_MPRObjects->AddImage( image );
242 // -------------------------------------------------------------------------
243 bool cpPlugins::Interface::BaseMPRWidget::
246 const std::string& name,
247 const std::string& parent,
248 const double& r, const double& g, const double& b
252 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
253 if( new_item == NULL )
256 // Associate new data
257 this->m_Images[ name ] = image;
258 this->m_Tree[ name ] = parent;
260 // Show image and return
261 this->m_MPRObjects->AddImage( image );
265 // -------------------------------------------------------------------------
266 bool cpPlugins::Interface::BaseMPRWidget::
269 const std::string& name,
270 const std::string& parent
274 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
275 if( new_item == NULL )
278 // Associate new data
279 PolyDataActor* actor = new PolyDataActor( mesh );
280 this->m_Meshes[ name ] = actor;
281 this->m_Tree[ name ] = parent;
284 this->_Add3DActor( actor->Actor );
288 // -------------------------------------------------------------------------
289 bool cpPlugins::Interface::BaseMPRWidget::
292 const std::string& name,
293 const std::string& parent,
294 const double& r, const double& g, const double& b
300 // -------------------------------------------------------------------------
301 void cpPlugins::Interface::BaseMPRWidget::
304 this->m_MPRObjects->ClearAll( );
305 this->m_Images.clear( );
306 this->m_Meshes.clear( );
310 // -------------------------------------------------------------------------
311 std::string cpPlugins::Interface::BaseMPRWidget::
312 GetSelectedData( ) const
314 QTreeWidgetItem* item = this->m_UI->LoadedData->currentItem( );
316 return( item->text( 0 ).toStdString( ) );
321 // -------------------------------------------------------------------------
322 QTreeWidgetItem* cpPlugins::Interface::BaseMPRWidget::
323 _FindItemInTree( const std::string& name ) const
325 QList< QTreeWidgetItem* > items =
326 this->m_UI->LoadedData->findItems( name.c_str( ), Qt::MatchExactly );
327 if( items.size( ) > 0 )
328 return( items[ 0 ] );
333 // -------------------------------------------------------------------------
334 QTreeWidgetItem* cpPlugins::Interface::BaseMPRWidget::
335 _UpdateTreeItem( const std::string& name, const std::string& parent )
338 QTreeWidgetItem* new_item = NULL;
341 QTreeWidgetItem* parent_item = this->_FindItemInTree( parent );
342 if( parent_item != NULL )
344 QTreeWidgetItem* old_item = this->_FindItemInTree( name );
345 if( old_item == NULL )
348 new QTreeWidgetItem( parent_item, QStringList( name.c_str( ) ) );
349 parent_item->setExpanded( true );
357 new_item = new QTreeWidgetItem(
358 ( QTreeWidgetItem* )( NULL ), QStringList( name.c_str( ) )
360 this->m_UI->LoadedData->addTopLevelItem( new_item );
366 // -------------------------------------------------------------------------
368 void cpPlugins::Interface::BaseMPRWidget::
369 _Add3DActor( vtkProp3D* prop )
372 this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
375 ren->AddActor( prop );
376 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
380 // -------------------------------------------------------------------------
381 void cpPlugins::Interface::BaseMPRWidget::
382 _SyncBottom( int a, int b )
384 this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
387 // -------------------------------------------------------------------------
388 void cpPlugins::Interface::BaseMPRWidget::
389 _SyncTop( int a, int b )
391 this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
394 // -------------------------------------------------------------------------
395 cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
405 // -------------------------------------------------------------------------
406 cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
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( );
415 // -------------------------------------------------------------------------
416 void cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
417 Configure( vtkPolyData* pd )
423 pd->GetScalarRange( range );
425 this->Normals = vtkPolyDataNormals::New( );
426 this->Stripper = vtkStripper::New( );
427 this->Mapper = vtkPolyDataMapper::New( );
428 this->Actor = vtkQuadricLODActor::New( );
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 ]
439 this->Actor->SetMapper( this->Mapper );
440 this->Actor->DeferLODConstructionOff( );
443 // -------------------------------------------------------------------------
444 cpPlugins::Interface::BaseMPRWidget::Data::
447 this->Tag = Data::IMAGE;
452 // -------------------------------------------------------------------------
453 cpPlugins::Interface::BaseMPRWidget::Data::
458 // -------------------------------------------------------------------------
459 cpPlugins::Interface::BaseMPRWidget::
460 Data& cpPlugins::Interface::BaseMPRWidget::Data::
461 operator=( const Data& data )
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;
472 // -------------------------------------------------------------------------
473 cpPlugins::Interface::
474 DataObject* cpPlugins::Interface::BaseMPRWidget::Data::
475 GetSourceDataObject( )
477 return( this->Source );
480 // -------------------------------------------------------------------------
481 const cpPlugins::Interface::
482 DataObject* cpPlugins::Interface::BaseMPRWidget::Data::
483 GetSourceDataObject( ) const
485 return( this->Source );
488 // -------------------------------------------------------------------------
489 void cpPlugins::Interface::BaseMPRWidget::Data::
490 SetSourceDataObject( cpPlugins::Interface::DataObject* dobj )
492 typedef cpPlugins::Interface::Image _TImage;
493 typedef cpPlugins::Interface::Mesh _TMesh;
496 _TImage* image = dynamic_cast< _TImage* >( dobj );
499 this->Tag = Data::IMAGE;
500 this->Image = image->GetVTK< vtkImageData >( );
504 _TMesh* mesh = dynamic_cast< _TMesh* >( dobj );
508 this->Tag = Data::MESH;
509 this->Mesh.Configure( mesh->GetVTK< vtkPolyData >( ) );
514 // -------------------------------------------------------------------------
515 vtkImageData* cpPlugins::Interface::BaseMPRWidget::Data::
518 if( this->Tag == Data::IMAGE )
519 return( this->Image );
524 // -------------------------------------------------------------------------
525 vtkPolyData* cpPlugins::Interface::BaseMPRWidget::Data::
528 if( this->Tag == Data::MESH )
529 return( this->Mesh.Mesh );
534 // -------------------------------------------------------------------------
535 vtkProp* cpPlugins::Interface::BaseMPRWidget::Data::
538 if( this->Tag == Data::MESH )
539 return( this->Mesh.Actor );
544 #endif // cpPlugins_Interface_QT4