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