]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/BaseMPRWidget.cxx
Merge branch 'master' of ssh://git.creatis.insa-lyon.fr/cpPlugins
[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 {
15   this->m_UI->setupUi( this );
16
17   // Configure VTK widgets
18   this->m_VTK[ 0 ] = this->m_UI->VTK01;
19   this->m_VTK[ 1 ] = this->m_UI->VTK00;
20   this->m_VTK[ 2 ] = this->m_UI->VTK10;
21   this->m_VTK[ 3 ] = this->m_UI->VTK11;
22
23   this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
24   this->m_MPRObjects->SetRenderWindows(
25     this->m_VTK[ 0 ]->GetRenderWindow( ),
26     this->m_VTK[ 1 ]->GetRenderWindow( ),
27     this->m_VTK[ 2 ]->GetRenderWindow( ),
28     this->m_VTK[ 3 ]->GetRenderWindow( )
29     );
30
31   // Connect slots
32   QObject::connect(
33     this->m_UI->TopSplitter, SIGNAL( splitterMoved( int, int ) ),
34     this, SLOT( _SyncBottom( int, int ) )
35     );
36   QObject::connect(
37     this->m_UI->BottomSplitter, SIGNAL( splitterMoved( int, int ) ),
38     this, SLOT( _SyncTop( int, int ) )
39     );
40 }
41
42 // -------------------------------------------------------------------------
43 cpPlugins::Interface::BaseMPRWidget::
44 ~BaseMPRWidget( )
45 {
46   delete this->m_UI;
47
48   // Delete polydata actors
49   std::map< std::string, PolyDataActor* >::iterator mIt =
50     this->m_Meshes.begin( );
51   for( ; mIt != this->m_Meshes.end( ); ++mIt )
52     delete mIt->second;
53   this->m_Meshes.clear( );
54 }
55
56 // -------------------------------------------------------------------------
57 bool cpPlugins::Interface::BaseMPRWidget::
58 ShowImage(
59   vtkImageData* image,
60   const std::string& name,
61   const std::string& parent
62   )
63 {
64   // Update tree view
65   QTreeWidgetItem* new_item = this->_UpdateItem( name, parent );
66   if( new_item == NULL )
67     return( false );
68
69   // Associate new data
70   this->m_Images[ name ] = image;
71   this->m_Tree[ name ] = parent;
72
73   // Show image and return
74   this->m_MPRObjects->AddImage( image );
75   return( true );
76 }
77
78 // -------------------------------------------------------------------------
79 bool cpPlugins::Interface::BaseMPRWidget::
80 ShowImage(
81   vtkImageData* image,
82   const std::string& name,
83   const std::string& parent,
84   const double& r, const double& g, const double& b
85   )
86 {
87 }
88
89 // -------------------------------------------------------------------------
90 bool cpPlugins::Interface::BaseMPRWidget::
91 ShowMesh(
92   vtkPolyData* mesh,
93   const std::string& name,
94   const std::string& parent
95   )
96 {
97   // Update tree view
98   QTreeWidgetItem* new_item = this->_UpdateItem( name, parent );
99   if( new_item == NULL )
100     return( false );
101
102   // Associate new data
103   PolyDataActor* actor = new PolyDataActor( mesh );
104   this->m_Meshes[ name ] = actor;
105   this->m_Tree[ name ] = parent;
106
107   // Show mesh
108   this->_Add3DActor( actor->Actor );
109   return( true );
110 }
111
112 // -------------------------------------------------------------------------
113 bool cpPlugins::Interface::BaseMPRWidget::
114 ShowMesh(
115   vtkPolyData* mesh,
116   const std::string& name,
117   const std::string& parent,
118   const double& r, const double& g, const double& b
119   )
120 {
121 }
122
123 // -------------------------------------------------------------------------
124 void cpPlugins::Interface::BaseMPRWidget::
125 ClearAll( )
126 {
127   /*
128     this->m_MPRObjects->ClearAll( );
129     this->m_Images.clear( );
130     this->m_Meshes.clear( );
131   */
132 }
133
134 // -------------------------------------------------------------------------
135 std::string cpPlugins::Interface::BaseMPRWidget::
136 GetSelectedData( ) const
137 {
138   QTreeWidgetItem* item = this->m_UI->LoadedData->currentItem( );
139   if( item != NULL )
140     return( item->text( 0 ).toStdString( ) );
141   else
142     return( "" );
143 }
144
145 // -------------------------------------------------------------------------
146 QTreeWidgetItem* cpPlugins::Interface::BaseMPRWidget::
147 _FindItem( const std::string& name ) const
148 {
149   QList< QTreeWidgetItem* > items =
150     this->m_UI->LoadedData->findItems( name.c_str( ), Qt::MatchExactly );
151   if( items.size( ) > 0 )
152     return( items[ 0 ] );
153   else
154     return( NULL );
155 }
156
157 // -------------------------------------------------------------------------
158 QTreeWidgetItem* cpPlugins::Interface::BaseMPRWidget::
159 _UpdateItem( const std::string& name, const std::string& parent )
160 {
161   // Update tree view
162   QTreeWidgetItem* new_item = NULL;
163   if( parent != "" )
164   {
165     QTreeWidgetItem* parent_item = this->_FindItem( parent );
166     if( parent_item != NULL )
167     {
168       QTreeWidgetItem* old_item = this->_FindItem( name );
169       if( old_item == NULL )
170       {
171         new_item =
172           new QTreeWidgetItem( parent_item, QStringList( name.c_str( ) ) );
173         parent_item->setExpanded( true );
174
175       } // fi
176
177     } // fi
178   }
179   else
180   {
181     new_item = new QTreeWidgetItem(
182       ( QTreeWidgetItem* )( NULL ), QStringList( name.c_str( ) )
183       );
184     this->m_UI->LoadedData->addTopLevelItem( new_item );
185
186   } // fi
187   return( new_item );
188 }
189
190 // -------------------------------------------------------------------------
191 void cpPlugins::Interface::BaseMPRWidget::
192 _Add3DActor( vtkProp3D* prop )
193 {
194   vtkRenderer* ren =
195     this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
196   if( ren == NULL )
197     return;
198   ren->AddActor( prop );
199   this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
200 }
201
202 // -------------------------------------------------------------------------
203 void cpPlugins::Interface::BaseMPRWidget::
204 _SyncBottom( int a, int b )
205 {
206   this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
207 }
208
209 // -------------------------------------------------------------------------
210 void cpPlugins::Interface::BaseMPRWidget::
211 _SyncTop( int a, int b )
212 {
213   this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
214 }
215
216 // -------------------------------------------------------------------------
217 cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
218 PolyDataActor( vtkPolyData* pd )
219 {
220   if( pd ==  NULL )
221     return;
222
223   double range[ 2 ];
224   pd->GetScalarRange( range );
225
226   this->Normals = vtkSmartPointer< vtkPolyDataNormals >::New( );
227   this->Stripper = vtkSmartPointer< vtkStripper >::New( );
228   this->Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
229   this->Actor = vtkSmartPointer< vtkQuadricLODActor >::New( );
230
231   this->Normals->SetInputData( pd );
232   this->Normals->SetFeatureAngle( 60.0 );
233   this->Stripper->SetInputConnection( this->Normals->GetOutputPort( ) );
234   this->Mapper->SetInputConnection( this->Stripper->GetOutputPort( ) );
235   this->Mapper->UseLookupTableScalarRangeOff( );
236   this->Mapper->SetScalarRange(
237     range[ 0 ], ( ( range[ 1 ] - range[ 0 ] ) * 0.75 ) + range[ 0 ]
238     );
239   this->Actor->SetMapper( this->Mapper );
240   this->Actor->DeferLODConstructionOff( );
241 }
242
243 #endif // cpPlugins_Interface_QT4
244
245 // eof - $RCSfile$