]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/QT/RendererWidget.cxx
Start contour tracer widget debugging: change interactor style.
[cpPlugins.git] / lib / cpExtensions / QT / RendererWidget.cxx
1 #include <cpExtensions/QT/RendererWidget.h>
2
3 #ifdef cpExtensions_QT4
4
5 #include <vtkAxesActor.h>
6 #include <vtkCamera.h>
7 #include <vtkInteractorStyle.h>
8 #include <vtkOrientationMarkerWidget.h>
9 #include <vtkRenderer.h>
10 #include <vtkRenderWindow.h>
11
12 // -------------------------------------------------------------------------
13 cpExtensions::QT::RendererWidget::
14 RendererWidget( QWidget* parent, Qt::WindowFlags f )
15   : Superclass( parent, f )
16 {
17   this->m_Renderer = vtkSmartPointer< vtkRenderer >::New( );
18   this->GetRenderWindow( )->AddRenderer( this->m_Renderer );
19
20   vtkAxesActor* axes = vtkAxesActor::New( );
21   axes->AxisLabelsOff( );
22   this->m_Marker = vtkSmartPointer< vtkOrientationMarkerWidget >::New( );
23   this->m_Marker->SetOutlineColor( 1, 1, 1 );
24   this->m_Marker->SetOrientationMarker( axes );
25   this->m_Marker->SetInteractor( this->GetRenderWindow( )->GetInteractor( ) );
26   this->m_Marker->EnabledOn( );
27   this->m_Marker->InteractiveOff( );
28   axes->Delete( );
29   this->SetQuadrant( 0 );
30 }
31
32 // -------------------------------------------------------------------------
33 cpExtensions::QT::RendererWidget::
34 ~RendererWidget( )
35 {
36 }
37
38 // -------------------------------------------------------------------------
39 int cpExtensions::QT::RendererWidget::
40 GetQuadrant( ) const
41 {
42   return( this->m_Quadrant );
43 }
44
45 // -------------------------------------------------------------------------
46 void cpExtensions::QT::RendererWidget::
47 SetQuadrant( int q )
48 {
49   this->m_Quadrant = ( q - 1 ) % 4;
50   if( this->m_Quadrant == 0 )
51     this->m_Marker->SetViewport( 0.85, 0.00, 1.00, 0.15 );
52   else if( this->m_Quadrant == 1 )
53     this->m_Marker->SetViewport( 0.00, 0.00, 0.15, 0.15 );
54   else if( this->m_Quadrant == 2 )
55     this->m_Marker->SetViewport( 0.00, 0.85, 0.15, 1.00 );
56   else if( this->m_Quadrant == 3 )
57     this->m_Marker->SetViewport( 0.85, 0.85, 1.00, 1.00 );
58 }
59
60 // -------------------------------------------------------------------------
61 vtkRenderer* cpExtensions::QT::RendererWidget::
62 GetRenderer( )
63 {
64   return( this->m_Renderer );
65 }
66
67 // -------------------------------------------------------------------------
68 const vtkRenderer* cpExtensions::QT::RendererWidget::
69 GetRenderer( ) const
70 {
71   return( this->m_Renderer );
72 }
73
74 // -------------------------------------------------------------------------
75 vtkInteractorStyle* cpExtensions::QT::RendererWidget::
76 GetStyle( )
77 {
78   auto iren = this->GetInteractor( );
79   if( iren != NULL )
80     return(
81       dynamic_cast< vtkInteractorStyle* >( iren->GetInteractorStyle( ) )
82       );
83   else
84     return( NULL );
85 }
86
87 // -------------------------------------------------------------------------
88 const vtkInteractorStyle* cpExtensions::QT::RendererWidget::
89 GetStyle( ) const
90 {
91   // Ugly, but necessary :-(
92   Self* self = const_cast< Self* >( this );
93   if( self != NULL )
94   {
95     auto iren = self->GetInteractor( );
96     if( iren != NULL )
97       return(
98         dynamic_cast< const vtkInteractorStyle* >(
99           iren->GetInteractorStyle( )
100           )
101         );
102     else
103       return( NULL );
104   }
105   else
106     return( NULL );
107 }
108
109 // -------------------------------------------------------------------------
110 void cpExtensions::QT::RendererWidget::
111 SetStyle( vtkInteractorStyle* style )
112 {
113   this->GetInteractor( )->SetInteractorStyle( style );
114 }
115
116 // -------------------------------------------------------------------------
117 vtkCamera* cpExtensions::QT::RendererWidget::
118 GetActiveCamera( )
119 {
120   return( this->m_Renderer->GetActiveCamera( ) );
121 }
122
123 // -------------------------------------------------------------------------
124 const vtkCamera* cpExtensions::QT::RendererWidget::
125 GetActiveCamera( ) const
126 {
127   return( this->m_Renderer->GetActiveCamera( ) );
128 }
129
130 // -------------------------------------------------------------------------
131 void cpExtensions::QT::RendererWidget::
132 AddViewProp( vtkProp* prop, const std::string& name )
133 {
134   if( prop != NULL )
135   {
136     auto i = this->m_ViewProps.find( name );
137     if( i == this->m_ViewProps.end( ) )
138       i =
139         this->m_ViewProps.insert(
140           TPropCollection::value_type( name, TProps( ) )
141           ).first;
142     i->second.insert( prop );
143     this->m_Renderer->AddViewProp( prop );
144
145   } // fi
146 }
147
148 // -------------------------------------------------------------------------
149 void cpExtensions::QT::RendererWidget::
150 AddViewProps( vtkPropCollection* props, const std::string& name )
151 {
152   if( props != NULL )
153   {
154     auto i = this->m_ViewProps.find( name );
155     if( i == this->m_ViewProps.end( ) )
156       i =
157         this->m_ViewProps.insert(
158           TPropCollection::value_type( name, TProps( ) )
159           ).first;
160     props->InitTraversal( );
161     while( vtkProp* prop = props->GetNextProp( ) )
162     {
163       i->second.insert( prop );
164       this->m_Renderer->AddViewProp( prop );
165
166     } // elihw
167
168   } // fi
169 }
170
171 // -------------------------------------------------------------------------
172 void cpExtensions::QT::RendererWidget::
173 AddAuxViewProp( vtkProp* prop, const std::string& name )
174 {
175   if( prop != NULL )
176   {
177     auto i = this->m_AuxViewProps.find( name );
178     if( i == this->m_AuxViewProps.end( ) )
179       i =
180         this->m_AuxViewProps.insert(
181           TPropCollection::value_type( name, TProps( ) )
182           ).first;
183     i->second.insert( prop );
184     this->m_Renderer->AddViewProp( prop );
185
186   } // fi
187 }
188
189 // -------------------------------------------------------------------------
190 void cpExtensions::QT::RendererWidget::
191 AddAuxViewProps( vtkPropCollection* props, const std::string& name )
192 {
193   if( props != NULL )
194   {
195     auto i = this->m_AuxViewProps.find( name );
196     if( i == this->m_AuxViewProps.end( ) )
197       i =
198         this->m_AuxViewProps.insert(
199           TPropCollection::value_type( name, TProps( ) )
200           ).first;
201     props->InitTraversal( );
202     while( vtkProp* prop = props->GetNextProp( ) )
203     {
204       i->second.insert( prop );
205       this->m_Renderer->AddViewProp( prop );
206
207     } // elhiw
208
209   } // fi
210 }
211
212 // -------------------------------------------------------------------------
213 cpExtensions::QT::RendererWidget::
214 TProps& cpExtensions::QT::RendererWidget::
215 GetViewProps( const std::string& name )
216 {
217   static TProps zero;
218   auto i = this->m_ViewProps.find( name );
219   if( i == this->m_ViewProps.end( ) )
220   {
221     zero.clear( );
222     return( zero );
223   }
224   else
225     return( i->second );
226 }
227
228 // -------------------------------------------------------------------------
229 const cpExtensions::QT::RendererWidget::
230 TProps& cpExtensions::QT::RendererWidget::
231 GetViewProps( const std::string& name ) const
232 {
233   static const TProps zero;
234   auto i = this->m_ViewProps.find( name );
235   if( i == this->m_ViewProps.end( ) )
236     return( zero );
237   else
238     return( i->second );
239 }
240
241 // -------------------------------------------------------------------------
242 cpExtensions::QT::RendererWidget::
243 TProps& cpExtensions::QT::RendererWidget::
244 GetAuxViewProps( const std::string& name )
245 {
246   static TProps zero;
247   auto i = this->m_AuxViewProps.find( name );
248   if( i == this->m_AuxViewProps.end( ) )
249   {
250     zero.clear( );
251     return( zero );
252   }
253   else
254     return( i->second );
255 }
256
257 // -------------------------------------------------------------------------
258 const cpExtensions::QT::RendererWidget::
259 TProps& cpExtensions::QT::RendererWidget::
260 GetAuxViewProps( const std::string& name ) const
261 {
262   static const TProps zero;
263   auto i = this->m_AuxViewProps.find( name );
264   if( i == this->m_AuxViewProps.end( ) )
265     return( zero );
266   else
267     return( i->second );
268 }
269
270 // -------------------------------------------------------------------------
271 void cpExtensions::QT::RendererWidget::
272 RemoveViewProps( const std::string& name )
273 {
274   auto i = this->m_ViewProps.find( name );
275   if( i != this->m_ViewProps.end( ) )
276   {
277     for( auto p = i->second.begin( ); p != i->second.end( ); ++p )
278       this->m_Renderer->RemoveViewProp( *p );
279     this->m_ViewProps.erase( i );
280
281   } // fi
282
283   i = this->m_AuxViewProps.find( name );
284   if( i != this->m_AuxViewProps.end( ) )
285   {
286     for( auto p = i->second.begin( ); p != i->second.end( ); ++p )
287       this->m_Renderer->RemoveViewProp( *p );
288     this->m_AuxViewProps.erase( i );
289
290   } // fi
291 }
292
293 // -------------------------------------------------------------------------
294 void cpExtensions::QT::RendererWidget::
295 RemoveViewProps( )
296 {
297   this->m_Renderer->RemoveAllViewProps( );
298   this->m_ViewProps.clear( );
299   this->m_AuxViewProps.clear( );
300 }
301
302 // -------------------------------------------------------------------------
303 void cpExtensions::QT::RendererWidget::
304 HideViewProps( const std::string& name )
305 {
306   auto i = this->m_ViewProps.find( name );
307   if( i != this->m_ViewProps.end( ) )
308     for( auto p = i->second.begin( ); p != i->second.end( ); ++p )
309       ( *p )->VisibilityOff( );
310   i = this->m_AuxViewProps.find( name );
311   if( i != this->m_AuxViewProps.end( ) )
312     for( auto p = i->second.begin( ); p != i->second.end( ); ++p )
313       ( *p )->VisibilityOff( );
314 }
315
316 // -------------------------------------------------------------------------
317 void cpExtensions::QT::RendererWidget::
318 ShowViewProps( const std::string& name )
319 {
320   auto i = this->m_ViewProps.find( name );
321   if( i != this->m_ViewProps.end( ) )
322     for( auto p = i->second.begin( ); p != i->second.end( ); ++p )
323       ( *p )->VisibilityOn( );
324   i = this->m_AuxViewProps.find( name );
325   if( i != this->m_AuxViewProps.end( ) )
326     for( auto p = i->second.begin( ); p != i->second.end( ); ++p )
327       ( *p )->VisibilityOn( );
328 }
329
330 // -------------------------------------------------------------------------
331 void cpExtensions::QT::RendererWidget::
332 ResetCamera( )
333 {
334   this->m_Renderer->ResetCamera( );
335 }
336
337 // -------------------------------------------------------------------------
338 void cpExtensions::QT::RendererWidget::
339 Render( )
340 {
341   this->GetRenderWindow( )->Render( );
342 }
343
344 #endif // cpExtensions_QT4
345
346 // eof - $RCSfile$