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