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