]> 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   // Update tree view
88   QTreeWidgetItem* new_item = this->_UpdateItem( name, parent );
89   if( new_item == NULL )
90     return( false );
91
92   // Associate new data
93   this->m_Images[ name ] = image;
94   this->m_Tree[ name ] = parent;
95
96   // Show image and return
97   this->m_MPRObjects->AddImage( image );
98   return( true );
99 }
100
101 // -------------------------------------------------------------------------
102 bool cpPlugins::Interface::BaseMPRWidget::
103 ShowMesh(
104   vtkPolyData* mesh,
105   const std::string& name,
106   const std::string& parent
107   )
108 {
109   // Update tree view
110   QTreeWidgetItem* new_item = this->_UpdateItem( name, parent );
111   if( new_item == NULL )
112     return( false );
113
114   // Associate new data
115   PolyDataActor* actor = new PolyDataActor( mesh );
116   this->m_Meshes[ name ] = actor;
117   this->m_Tree[ name ] = parent;
118
119   // Show mesh
120   this->_Add3DActor( actor->Actor );
121   return( true );
122 }
123
124 // -------------------------------------------------------------------------
125 bool cpPlugins::Interface::BaseMPRWidget::
126 ShowMesh(
127   vtkPolyData* mesh,
128   const std::string& name,
129   const std::string& parent,
130   const double& r, const double& g, const double& b
131   )
132 {
133   return false;
134 }
135
136 // -------------------------------------------------------------------------
137 void cpPlugins::Interface::BaseMPRWidget::
138 ClearAll( )
139 {
140   /*
141     this->m_MPRObjects->ClearAll( );
142     this->m_Images.clear( );
143     this->m_Meshes.clear( );
144   */
145 }
146
147 // -------------------------------------------------------------------------
148 std::string cpPlugins::Interface::BaseMPRWidget::
149 GetSelectedData( ) const
150 {
151   QTreeWidgetItem* item = this->m_UI->LoadedData->currentItem( );
152   if( item != NULL )
153     return( item->text( 0 ).toStdString( ) );
154   else
155     return( "" );
156 }
157
158 // -------------------------------------------------------------------------
159 QTreeWidgetItem* cpPlugins::Interface::BaseMPRWidget::
160 _FindItem( const std::string& name ) const
161 {
162   QList< QTreeWidgetItem* > items =
163     this->m_UI->LoadedData->findItems( name.c_str( ), Qt::MatchExactly );
164   if( items.size( ) > 0 )
165     return( items[ 0 ] );
166   else
167     return( NULL );
168 }
169
170 // -------------------------------------------------------------------------
171 QTreeWidgetItem* cpPlugins::Interface::BaseMPRWidget::
172 _UpdateItem( const std::string& name, const std::string& parent )
173 {
174   // Update tree view
175   QTreeWidgetItem* new_item = NULL;
176   if( parent != "" )
177   {
178     QTreeWidgetItem* parent_item = this->_FindItem( parent );
179     if( parent_item != NULL )
180     {
181       QTreeWidgetItem* old_item = this->_FindItem( name );
182       if( old_item == NULL )
183       {
184         new_item =
185           new QTreeWidgetItem( parent_item, QStringList( name.c_str( ) ) );
186         parent_item->setExpanded( true );
187
188       } // fi
189
190     } // fi
191   }
192   else
193   {
194     new_item = new QTreeWidgetItem(
195       ( QTreeWidgetItem* )( NULL ), QStringList( name.c_str( ) )
196       );
197     this->m_UI->LoadedData->addTopLevelItem( new_item );
198
199   } // fi
200   return( new_item );
201 }
202
203 // -------------------------------------------------------------------------
204 void cpPlugins::Interface::BaseMPRWidget::
205 _Add3DActor( vtkProp3D* prop )
206 {
207   vtkRenderer* ren =
208     this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
209   if( ren == NULL )
210     return;
211   ren->AddActor( prop );
212   this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
213 }
214
215 // -------------------------------------------------------------------------
216 void cpPlugins::Interface::BaseMPRWidget::
217 _SyncBottom( int a, int b )
218 {
219   this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
220 }
221
222 // -------------------------------------------------------------------------
223 void cpPlugins::Interface::BaseMPRWidget::
224 _SyncTop( int a, int b )
225 {
226   this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
227 }
228
229 // -------------------------------------------------------------------------
230 cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
231 PolyDataActor( vtkPolyData* pd )
232 {
233   if( pd ==  NULL )
234     return;
235
236   double range[ 2 ];
237   pd->GetScalarRange( range );
238
239   this->Normals = vtkSmartPointer< vtkPolyDataNormals >::New( );
240   this->Stripper = vtkSmartPointer< vtkStripper >::New( );
241   this->Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
242   this->Actor = vtkSmartPointer< vtkQuadricLODActor >::New( );
243
244   this->Normals->SetInputData( pd );
245   this->Normals->SetFeatureAngle( 60.0 );
246   this->Stripper->SetInputConnection( this->Normals->GetOutputPort( ) );
247   this->Mapper->SetInputConnection( this->Stripper->GetOutputPort( ) );
248   this->Mapper->UseLookupTableScalarRangeOff( );
249   this->Mapper->SetScalarRange(
250     range[ 0 ], ( ( range[ 1 ] - range[ 0 ] ) * 0.75 ) + range[ 0 ]
251     );
252   this->Actor->SetMapper( this->Mapper );
253   this->Actor->DeferLODConstructionOff( );
254 }
255
256 #endif // cpPlugins_Interface_QT4
257
258 // eof - $RCSfile$