]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/ActorPolyDataProperties.cxx
5fd9fe120262b39e2a0ebdeeed7a2626ec7ac78b
[cpPlugins.git] / lib / cpPlugins / ActorPolyDataProperties.cxx
1 #include <cpPlugins/ActorPolyDataProperties.h>
2
3 #ifdef cpPlugins_QT4
4
5 #include <cpPlugins/ui_ActorPolyDataProperties.h>
6 #include <QColorDialog>
7 #include <vtkActor.h>
8 #include <vtkMapper.h>
9 #include <vtkProperty.h>
10
11 // -------------------------------------------------------------------------
12 cpPlugins::ActorPolyDataProperties::
13 ActorPolyDataProperties( QWidget* parent )
14   : cpPlugins::ActorProperties( parent ),
15     m_UI( new Ui::ActorPolyDataProperties )
16 {
17   this->m_UI->setupUi( this );
18 }
19
20 // -------------------------------------------------------------------------
21 cpPlugins::ActorPolyDataProperties::
22 ~ActorPolyDataProperties( )
23 {
24   delete this->m_UI;
25 }
26
27 // -------------------------------------------------------------------------
28 bool cpPlugins::ActorPolyDataProperties::
29 addActor( vtkProp* obj )
30 {
31   auto actor = dynamic_cast< vtkActor* >( obj );
32   if( actor != NULL )
33   {
34     this->m_Actors.insert( obj );
35     if( this->m_Actors.size( ) == 1 )
36       this->_updateWidgets( );
37     return( true );
38   }
39   else
40     return( false );
41 }
42
43 // -------------------------------------------------------------------------
44 void cpPlugins::ActorPolyDataProperties::
45 _updateWidgets( )
46 {
47   auto actor =
48     dynamic_cast< vtkActor* >( this->m_Actors.begin( )->GetPointer( ) );
49   if( actor == NULL )
50     return;
51
52   // Get properties
53   auto mapp = actor->GetMapper( );
54   auto prop = actor->GetProperty( );
55   double rgb[ 3 ];
56   prop->GetColor( rgb );
57   rgb[ 0 ] *= double( 255 );
58   rgb[ 1 ] *= double( 255 );
59   rgb[ 2 ] *= double( 255 );
60   double op = prop->GetOpacity( );
61   double lw = prop->GetLineWidth( );
62   double ps = prop->GetPointSize( );
63   bool sv = ( mapp->GetScalarVisibility( ) == 1 );
64   int rep = prop->GetRepresentation( );
65
66   // Set widget values
67   auto palette = this->m_UI->Color->palette( );
68   palette.setColor(
69     QPalette::Button, QColor( rgb[ 0 ], rgb[ 1 ], rgb[ 2 ] )
70     );
71   this->m_UI->Color->setAutoFillBackground( true );
72   this->m_UI->Color->setPalette( palette );
73
74   op *= double( this->m_UI->Opacity->maximum( ) );
75   this->m_UI->Opacity->setValue( int( op ) );
76   this->m_UI->LineWidth->setValue( int( lw ) );
77   this->m_UI->PointSize->setValue( int( ps ) );
78   this->m_UI->ScalarVisibility->setChecked( sv );
79
80   if( rep == VTK_POINTS )
81     this->m_UI->Representation->setCurrentIndex( 0 );
82   else if( rep == VTK_WIREFRAME )
83     this->m_UI->Representation->setCurrentIndex( 1 );
84   else if( rep == VTK_SURFACE )
85     this->m_UI->Representation->setCurrentIndex( 2 );
86
87   // Connect stuff
88   this->connect(
89     this->m_UI->ScalarVisibility, SIGNAL( stateChanged( int ) ),
90     this, SLOT( _ScalarVisibility( int ) )
91     );
92   this->connect(
93     this->m_UI->Color, SIGNAL( clicked( ) ),
94     this, SLOT( _Color( ) )
95     );
96   this->connect(
97     this->m_UI->Opacity, SIGNAL( valueChanged( int ) ),
98     this, SLOT( _Opacity( int ) )
99     );
100   this->connect(
101     this->m_UI->LineWidth, SIGNAL( valueChanged( int ) ),
102     this, SLOT( _LineWidth( int ) )
103     );
104   this->connect(
105     this->m_UI->PointSize, SIGNAL( valueChanged( int ) ),
106     this, SLOT( _PointSize( int ) )
107     );
108   this->connect(
109     this->m_UI->Representation, SIGNAL( currentIndexChanged( int ) ),
110     this, SLOT( _Representation( int ) )
111     );
112 }
113
114 // -------------------------------------------------------------------------
115 void cpPlugins::ActorPolyDataProperties::
116 _ScalarVisibility( int v )
117 {
118   if( this->m_Actors.size( ) == 0 )
119     return;
120   QPalette pal = this->m_UI->Color->palette( );
121   QColor color = pal.color( QPalette::Button );
122   double rgb[ 3 ];
123   rgb[ 0 ] = double( color.red( ) ) / double( 255 );
124   rgb[ 1 ] = double( color.green( ) ) / double( 255 );
125   rgb[ 2 ] = double( color.blue( ) ) / double( 255 );
126
127   auto aIt = this->m_Actors.begin( );
128   for( ; aIt != this->m_Actors.end( ); ++aIt )
129   {
130     auto ma = dynamic_cast< vtkActor* >( aIt->GetPointer( ) );
131     if( !( this->m_UI->ScalarVisibility->isChecked( ) ) )
132     {
133       ma->GetMapper( )->ScalarVisibilityOff( );
134       ma->GetProperty( )->SetColor( rgb );
135     }
136     else
137       ma->GetMapper( )->ScalarVisibilityOn( );
138     ma->Modified( );
139
140   } // rof
141   this->_render( );
142 }
143
144 // -------------------------------------------------------------------------
145 void cpPlugins::ActorPolyDataProperties::
146 _Color( )
147 {
148   if( this->m_Actors.size( ) == 0 )
149     return;
150   QPalette pal = this->m_UI->Color->palette( );
151   QColor color =
152     QColorDialog::getColor(
153       pal.color( QPalette::Button ),
154       this,
155       "Select Color",
156       QColorDialog::DontUseNativeDialog
157       );
158   if( color.isValid( ) )
159   {
160     pal.setColor( QPalette::Button, color );
161     this->m_UI->Color->setAutoFillBackground( true );
162     this->m_UI->Color->setPalette( pal );
163     this->m_UI->Color->update( );
164     this->_ScalarVisibility( 0 );
165
166   } // fi
167 }
168
169 // -------------------------------------------------------------------------
170 void cpPlugins::ActorPolyDataProperties::
171 _Opacity( int v )
172 {
173   double op = double( v ) / double( this->m_UI->Opacity->maximum( ) );
174   auto aIt = this->m_Actors.begin( );
175   for( ; aIt != this->m_Actors.end( ); ++aIt )
176   {
177     auto ma = dynamic_cast< vtkActor* >( aIt->GetPointer( ) );
178     ma->GetProperty( )->SetOpacity( op );
179     ma->Modified( );
180
181   } // rof
182   this->_render( );
183 }
184
185 // -------------------------------------------------------------------------
186 void cpPlugins::ActorPolyDataProperties::
187 _LineWidth( int v )
188 {
189   auto aIt = this->m_Actors.begin( );
190   for( ; aIt != this->m_Actors.end( ); ++aIt )
191   {
192     auto ma = dynamic_cast< vtkActor* >( aIt->GetPointer( ) );
193     ma->GetProperty( )->SetLineWidth( v );
194     ma->Modified( );
195
196   } // rof
197   this->_render( );
198 }
199
200 // -------------------------------------------------------------------------
201 void cpPlugins::ActorPolyDataProperties::
202 _PointSize( int v )
203 {
204   auto aIt = this->m_Actors.begin( );
205   for( ; aIt != this->m_Actors.end( ); ++aIt )
206   {
207     auto ma = dynamic_cast< vtkActor* >( aIt->GetPointer( ) );
208     ma->GetProperty( )->SetPointSize( v );
209     ma->Modified( );
210
211   } // rof
212   this->_render( );
213 }
214
215 // -------------------------------------------------------------------------
216 void cpPlugins::ActorPolyDataProperties::
217 _Representation( int v )
218 {
219   int rep = VTK_POINTS + VTK_WIREFRAME + VTK_SURFACE;
220   if     ( v == 0 ) rep = VTK_POINTS;
221   else if( v == 1 ) rep = VTK_WIREFRAME;
222   else if( v == 2 ) rep = VTK_SURFACE;
223
224     auto aIt = this->m_Actors.begin( );
225     for( ; aIt != this->m_Actors.end( ); ++aIt )
226     {
227       auto ma = dynamic_cast< vtkActor* >( aIt->GetPointer( ) );
228       auto prop = ma->GetProperty( );
229       if( rep != VTK_POINTS && rep != VTK_WIREFRAME && rep != VTK_SURFACE )
230       {
231         double rgb[ 3 ];
232         prop->GetColor( rgb );
233         rgb[ 0 ] = double( 1 ) - rgb[ 0 ];
234         rgb[ 1 ] = double( 1 ) - rgb[ 1 ];
235         rgb[ 2 ] = double( 1 ) - rgb[ 2 ];
236         prop->SetRepresentation( VTK_SURFACE );
237         prop->SetEdgeColor( rgb );
238         prop->EdgeVisibilityOn( );
239       }
240       else
241       {
242         prop->EdgeVisibilityOff( );
243         prop->SetRepresentation( rep );
244
245       } // fi
246       ma->Modified( );
247
248     } // rof
249   this->_render( );
250 }
251
252 #endif // cpPlugins_QT4
253
254 // eof - $RCSfile$