]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/ActorAxesProperties.cxx
373f2e265873d20deaad5b063d0cefc820e76975
[cpPlugins.git] / lib / cpPlugins / ActorAxesProperties.cxx
1 #include <cpPlugins/ActorAxesProperties.h>
2
3 #ifdef cpPlugins_QT4
4
5 #include <cpPlugins/ui_ActorAxesProperties.h>
6
7 #include <QColorDialog>
8 #include <itkVector.h>
9 #include <vtkAxesActor.h>
10 #include <vtkCaptionActor2D.h>
11 #include <vtkMatrix4x4.h>
12 #include <vtkTextProperty.h>
13
14 // -------------------------------------------------------------------------
15 cpPlugins::ActorAxesProperties::
16 ActorAxesProperties( QWidget* parent )
17   : cpPlugins::ActorProperties( parent ),
18     m_UI( new Ui::ActorAxesProperties )
19 {
20   this->m_UI->setupUi( this );
21 }
22
23 // -------------------------------------------------------------------------
24 cpPlugins::ActorAxesProperties::
25 ~ActorAxesProperties( )
26 {
27   delete this->m_UI;
28 }
29
30 // -------------------------------------------------------------------------
31 bool cpPlugins::ActorAxesProperties::
32 addActor( vtkProp* obj )
33 {
34   auto actor = dynamic_cast< vtkAxesActor* >( obj );
35   if( actor != NULL )
36   {
37     this->m_Actors.insert( obj );
38     if( this->m_Actors.size( ) == 1 )
39       this->_updateWidgets( );
40     return( true );
41   }
42   else
43     return( false );
44 }
45
46 // -------------------------------------------------------------------------
47 void cpPlugins::ActorAxesProperties::
48 _updateWidgets( )
49 {
50   auto actor =
51     dynamic_cast< vtkAxesActor* >( this->m_Actors.begin( )->GetPointer( ) );
52   if( actor == NULL )
53     return;
54
55   // Get properties
56   std::string xtext( actor->GetXAxisLabelText( ) );
57   std::string ytext( actor->GetYAxisLabelText( ) );
58   std::string ztext( actor->GetZAxisLabelText( ) );
59   double scale = actor->GetScale( )[ 0 ];
60   auto prop = actor->GetXAxisCaptionActor2D( )->GetCaptionTextProperty( );
61   double rgb[ 3 ];
62   prop->GetColor( rgb );
63   rgb[ 0 ] *= double( 255 );
64   rgb[ 1 ] *= double( 255 );
65   rgb[ 2 ] *= double( 255 );
66
67   // Set widget values
68   auto palette = this->m_UI->TextColor->palette( );
69   palette.setColor(
70     QPalette::Button, QColor( rgb[ 0 ], rgb[ 1 ], rgb[ 2 ] )
71     );
72   this->m_UI->TextColor->setAutoFillBackground( true );
73   this->m_UI->TextColor->setPalette( palette );
74
75   this->m_UI->XText->setText( xtext.c_str( ) );
76   this->m_UI->YText->setText( ytext.c_str( ) );
77   this->m_UI->ZText->setText( ztext.c_str( ) );
78   this->m_UI->Scale->setValue( scale );
79
80   // Connect stuff
81   this->connect(
82     this->m_UI->Scale, SIGNAL( valueChanged( double ) ),
83     this, SLOT( _Scale( double ) )
84     );
85   this->connect(
86     this->m_UI->XText, SIGNAL( textChanged( const QString& ) ),
87     this, SLOT( _TextChanged( const QString& ) )
88     );
89   this->connect(
90     this->m_UI->YText, SIGNAL( textChanged( const QString& ) ),
91     this, SLOT( _TextChanged( const QString& ) )
92     );
93   this->connect(
94     this->m_UI->ZText, SIGNAL( textChanged( const QString& ) ),
95     this, SLOT( _TextChanged( const QString& ) )
96     );
97   this->connect(
98     this->m_UI->TextColor, SIGNAL( clicked( ) ),
99     this, SLOT( _TextColor( ) )
100     );
101 }
102
103 // -------------------------------------------------------------------------
104 void cpPlugins::ActorAxesProperties::
105 _Scale( double v )
106 {
107   itk::Vector< double, 3 > x, y, z;
108   auto aIt = this->m_Actors.begin( );
109   for( ; aIt != this->m_Actors.end( ); ++aIt )
110   {
111     auto ma = dynamic_cast< vtkAxesActor* >( aIt->GetPointer( ) );
112     auto matrix = ma->GetUserMatrix( );
113     for( unsigned int d = 0; d < 3; ++d )
114     {
115       x[ d ] = matrix->GetElement( d, 0 );
116       y[ d ] = matrix->GetElement( d, 1 );
117       z[ d ] = matrix->GetElement( d, 2 );
118
119     } // rof
120     auto nx = x.GetNorm( );
121     auto ny = y.GetNorm( );
122     auto nz = z.GetNorm( );
123     if( nx > double( 0 ) ) x /= nx;
124     if( ny > double( 0 ) ) y /= ny;
125     if( nz > double( 0 ) ) z /= nz;
126     x *= v;
127     y *= v;
128     z *= v;
129     for( unsigned int d = 0; d < 3; ++d )
130     {
131       matrix->SetElement( d, 0, x[ d ] );
132       matrix->SetElement( d, 1, y[ d ] );
133       matrix->SetElement( d, 2, z[ d ] );
134
135     } // rof
136     ma->Modified( );
137
138   } // rof
139   this->_render( );
140 }
141
142 // -------------------------------------------------------------------------
143 void cpPlugins::ActorAxesProperties::
144 _TextChanged( const QString& text )
145 {
146   auto obj = this->sender( );
147   auto aIt = this->m_Actors.begin( );
148   auto str = text.toStdString( );
149   for( ; aIt != this->m_Actors.end( ); ++aIt )
150   {
151     auto ma = dynamic_cast< vtkAxesActor* >( aIt->GetPointer( ) );
152     if( obj == this->m_UI->XText )
153       ma->SetXAxisLabelText( str.c_str( ) );
154     else if( obj == this->m_UI->YText )
155       ma->SetYAxisLabelText( str.c_str( ) );
156     else if( obj == this->m_UI->ZText )
157       ma->SetZAxisLabelText( str.c_str( ) );
158     ma->Modified( );
159
160   } // rof
161   this->_render( );
162 }
163
164 // -------------------------------------------------------------------------
165 void cpPlugins::ActorAxesProperties::
166 _TextColor( )
167 {
168   if( this->m_Actors.size( ) == 0 )
169     return;
170   QPalette pal = this->m_UI->TextColor->palette( );
171   QColor color =
172     QColorDialog::getColor(
173       pal.color( QPalette::Button ),
174       this,
175       "Select Color",
176       QColorDialog::DontUseNativeDialog
177       );
178   if( color.isValid( ) )
179   {
180     pal.setColor( QPalette::Button, color );
181     this->m_UI->TextColor->setAutoFillBackground( true );
182     this->m_UI->TextColor->setPalette( pal );
183     this->m_UI->TextColor->update( );
184
185     double rgb[ 3 ];
186     rgb[ 0 ] = double( color.red( ) ) / double( 255 );
187     rgb[ 1 ] = double( color.green( ) ) / double( 255 );
188     rgb[ 2 ] = double( color.blue( ) ) / double( 255 );
189     auto aIt = this->m_Actors.begin( );
190     for( ; aIt != this->m_Actors.end( ); ++aIt )
191     {
192       auto ma = dynamic_cast< vtkAxesActor* >( aIt->GetPointer( ) );
193       ma->GetXAxisCaptionActor2D( )->GetCaptionTextProperty( )->SetColor( rgb );
194       ma->GetYAxisCaptionActor2D( )->GetCaptionTextProperty( )->SetColor( rgb );
195       ma->GetZAxisCaptionActor2D( )->GetCaptionTextProperty( )->SetColor( rgb );
196       ma->GetXAxisCaptionActor2D( )->Modified( );
197       ma->GetYAxisCaptionActor2D( )->Modified( );
198       ma->GetZAxisCaptionActor2D( )->Modified( );
199       ma->Modified( );
200
201     } // rof
202     this->_render( );
203
204   } // fi
205 }
206
207 #endif // cpPlugins_QT4
208
209 // eof - $RCSfile$