]> Creatis software - cpPlugins.git/blob - lib/cpBaseQtApplication/ActorImageProperties.cxx
f6ba33e522fe070bc124f4b726aabf5cb34845de
[cpPlugins.git] / lib / cpBaseQtApplication / ActorImageProperties.cxx
1 #include <cpBaseQtApplication/ActorImageProperties.h>
2
3 #include <cpBaseQtApplication/ui_ActorImageProperties.h>
4
5 #include <vtkImageSlice.h>
6 #include <vtkImageData.h>
7 #include <vtkImageMapper3D.h>
8 #include <vtkImageProperty.h>
9
10 // -------------------------------------------------------------------------
11 cpBaseQtApplication::ActorImageProperties::
12 ActorImageProperties( QWidget* parent )
13   : cpBaseQtApplication::ActorProperties( parent ),
14     m_UI( new Ui::ActorImageProperties )
15 {
16   this->m_UI->setupUi( this );
17 }
18
19 // -------------------------------------------------------------------------
20 cpBaseQtApplication::ActorImageProperties::
21 ~ActorImageProperties( )
22 {
23   delete this->m_UI;
24 }
25
26 // -------------------------------------------------------------------------
27 bool cpBaseQtApplication::ActorImageProperties::
28 addActor( vtkProp* obj )
29 {
30   auto actor = dynamic_cast< vtkImageSlice* >( obj );
31   if( actor != NULL )
32   {
33     this->m_Actors.insert( obj );
34     if( this->m_Actors.size( ) == 1 )
35       this->_updateWidgets( );
36     return( true );
37   }
38   else
39     return( false );
40 }
41
42 // -------------------------------------------------------------------------
43 void cpBaseQtApplication::ActorImageProperties::
44 _updateWidgets( )
45 {
46   auto actor =
47     dynamic_cast< vtkImageSlice* >( this->m_Actors.begin( )->GetPointer( ) );
48   if( actor == NULL )
49     return;
50
51   // Get properties
52   auto prop = actor->GetProperty( );
53   auto mapper = actor->GetMapper( );
54   auto image = mapper->GetInput( );
55   double r[ 2 ];
56   image->GetScalarRange( r );
57   double win = prop->GetColorWindow( );
58   double lev = prop->GetColorLevel( );
59   double op = prop->GetOpacity( );
60   int interp = prop->GetInterpolationType( );
61
62   this->m_UI->ValueWindow->setMinimum( 0 );
63   this->m_UI->ValueWindow->setMaximum( r[ 1 ] - r[ 0 ] );
64   this->m_UI->ValueLevel->setMinimum( r[ 0 ] );
65   this->m_UI->ValueLevel->setMaximum( r[ 1 ] );
66   this->m_UI->ValueWindow->setValue( win );
67   this->m_UI->ValueLevel->setValue( lev );
68
69   double w = win / ( r[ 1 ] - r[ 0 ] );
70   double l = ( lev - r[ 0 ] ) / ( r[ 1 ] - r[ 0 ] );
71
72   op *= double( this->m_UI->Opacity->maximum( ) );
73   this->m_UI->Opacity->setValue( int( op ) );
74
75   w *= double( this->m_UI->Window->maximum( ) );
76   this->m_UI->Window->setValue( int( w ) );
77
78   l *= double( this->m_UI->Level->maximum( ) );
79   this->m_UI->Level->setValue( int( l ) );
80
81   if( interp == VTK_CUBIC_INTERPOLATION )
82     this->m_UI->Interpolation->setCurrentIndex( 0 );
83   else if( interp == VTK_LINEAR_INTERPOLATION )
84     this->m_UI->Interpolation->setCurrentIndex( 1 );
85   else if( interp == VTK_NEAREST_INTERPOLATION )
86     this->m_UI->Interpolation->setCurrentIndex( 2 );
87
88   // Connect stuff
89   this->connect(
90     this->m_UI->Opacity, SIGNAL( valueChanged( int ) ),
91     this, SLOT( _Opacity( int ) )
92     );
93   this->connect(
94     this->m_UI->Window, SIGNAL( valueChanged( int ) ),
95     this, SLOT( _Window( int ) )
96     );
97   this->connect(
98     this->m_UI->Level, SIGNAL( valueChanged( int ) ),
99     this, SLOT( _Level( int ) )
100     );
101   this->connect(
102     this->m_UI->ValueWindow, SIGNAL( valueChanged( double ) ),
103     this, SLOT( _Window( double ) )
104     );
105   this->connect(
106     this->m_UI->ValueLevel, SIGNAL( valueChanged( double ) ),
107     this, SLOT( _Level( double ) )
108     );
109   this->connect(
110     this->m_UI->Interpolation, SIGNAL( currentIndexChanged( int ) ),
111     this, SLOT( _Interpolation( int ) )
112     );
113 }
114
115 // -------------------------------------------------------------------------
116 void cpBaseQtApplication::ActorImageProperties::
117 _Opacity( int v )
118 {
119   double op = double( v ) / double( this->m_UI->Opacity->maximum( ) );
120   auto aIt = this->m_Actors.begin( );
121   for( ; aIt != this->m_Actors.end( ); ++aIt )
122   {
123     auto ma = dynamic_cast< vtkImageSlice* >( aIt->GetPointer( ) );
124     ma->GetProperty( )->SetOpacity( op );
125     ma->Modified( );
126
127   } // rof
128   this->_render( );
129 }
130
131 // -------------------------------------------------------------------------
132 void cpBaseQtApplication::ActorImageProperties::
133 _Window( int v )
134 {
135   double x = this->m_UI->ValueWindow->minimum( );
136   double y = this->m_UI->ValueWindow->maximum( );
137   double z = double( v ) / double( this->m_UI->Window->maximum( ) );
138   double w = ( ( y - x ) * z ) + x;
139   bool prev = this->m_UI->ValueWindow->blockSignals( true );
140   this->m_UI->ValueWindow->setValue( w );
141   this->m_UI->ValueWindow->blockSignals( prev );
142   auto aIt = this->m_Actors.begin( );
143   for( ; aIt != this->m_Actors.end( ); ++aIt )
144   {
145     auto ma = dynamic_cast< vtkImageSlice* >( aIt->GetPointer( ) );
146     ma->GetProperty( )->SetColorWindow( w );
147     ma->Modified( );
148
149   } // rof
150   this->_render( );
151 }
152
153 // -------------------------------------------------------------------------
154 void cpBaseQtApplication::ActorImageProperties::
155 _Level( int v )
156 {
157   double x = this->m_UI->ValueLevel->minimum( );
158   double y = this->m_UI->ValueLevel->maximum( );
159   double z = double( v ) / double( this->m_UI->Level->maximum( ) );
160   double l = ( ( y - x ) * z ) + x;
161   bool prev = this->m_UI->ValueLevel->blockSignals( true );
162   this->m_UI->ValueLevel->setValue( l );
163   this->m_UI->ValueLevel->blockSignals( prev );
164   auto aIt = this->m_Actors.begin( );
165   for( ; aIt != this->m_Actors.end( ); ++aIt )
166   {
167     auto ma = dynamic_cast< vtkImageSlice* >( aIt->GetPointer( ) );
168     ma->GetProperty( )->SetColorLevel( l );
169     ma->Modified( );
170
171   } // rof
172   this->_render( );
173 }
174
175 // -------------------------------------------------------------------------
176 void cpBaseQtApplication::ActorImageProperties::
177 _Window( double v )
178 {
179   auto aIt = this->m_Actors.begin( );
180   for( ; aIt != this->m_Actors.end( ); ++aIt )
181   {
182     auto ma = dynamic_cast< vtkImageSlice* >( aIt->GetPointer( ) );
183     ma->GetProperty( )->SetColorWindow( v );
184     ma->Modified( );
185
186   } // rof
187   this->_render( );
188
189   double x = this->m_UI->ValueWindow->minimum( );
190   double y = this->m_UI->ValueWindow->maximum( );
191   double z = ( v - x ) / ( y - x );
192   double w = double( z ) * double( this->m_UI->Window->maximum( ) );
193   bool prev = this->m_UI->Window->blockSignals( true );
194   this->m_UI->Window->setValue( int( w ) );
195   this->m_UI->Window->blockSignals( prev );
196 }
197
198 // -------------------------------------------------------------------------
199 void cpBaseQtApplication::ActorImageProperties::
200 _Level( double v )
201 {
202   auto aIt = this->m_Actors.begin( );
203   for( ; aIt != this->m_Actors.end( ); ++aIt )
204   {
205     auto ma = dynamic_cast< vtkImageSlice* >( aIt->GetPointer( ) );
206     ma->GetProperty( )->SetColorLevel( v );
207     ma->Modified( );
208
209   } // rof
210   this->_render( );
211
212   double x = this->m_UI->ValueLevel->minimum( );
213   double y = this->m_UI->ValueLevel->maximum( );
214   double z = ( v - x ) / ( y - x );
215   double l = double( z ) * double( this->m_UI->Level->maximum( ) );
216   bool prev = this->m_UI->Level->blockSignals( true );
217   this->m_UI->Level->setValue( int( l ) );
218   this->m_UI->Level->blockSignals( prev );
219 }
220
221 // -------------------------------------------------------------------------
222 void cpBaseQtApplication::ActorImageProperties::
223 _Interpolation( int v )
224 {
225   int interp = VTK_NEAREST_INTERPOLATION;
226   if     ( v == 0 ) interp = VTK_CUBIC_INTERPOLATION;
227   else if( v == 1 ) interp = VTK_LINEAR_INTERPOLATION;
228   else if( v == 2 ) interp = VTK_NEAREST_INTERPOLATION;
229
230   auto aIt = this->m_Actors.begin( );
231   for( ; aIt != this->m_Actors.end( ); ++aIt )
232   {
233     auto ma = dynamic_cast< vtkImageSlice* >( aIt->GetPointer( ) );
234     ma->GetProperty( )->SetInterpolationType( interp );
235     ma->Modified( );
236
237   } // rof
238   this->_render( );
239 }
240
241 // eof - $RCSfile$