]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/QT/PropertyWidget.cxx
3b719ff53e838d7921ba46fc852d49dec38e4436
[cpPlugins.git] / lib / cpExtensions / QT / PropertyWidget.cxx
1 #include <cpExtensions/QT/PropertyWidget.h>
2
3 #ifdef cpExtensions_QT4
4
5 #include <QColorDialog>
6 #include <vtkActor.h>
7 #include <vtkMapper.h>
8 #include <vtkProp.h>
9 #include <vtkProperty.h>
10 #include <vtkRenderWindow.h>
11
12 #include <cpExtensions/ui_PropertyWidget.h>
13
14 // -------------------------------------------------------------------------
15 cpExtensions::QT::PropertyWidget::
16 PropertyWidget( QWidget* parent )
17   : QWidget( parent ),
18     m_UI( new Ui::PropertyWidget ),
19     m_Prop( NULL ),
20     m_Window( NULL )
21 {
22   this->m_UI->setupUi( this );
23   this->connect(
24     this->m_UI->m_Color, SIGNAL( clicked( ) ),
25     this, SLOT( _Color( ) )
26     );
27   this->connect(
28     this->m_UI->m_Opacity, SIGNAL( valueChanged( int ) ),
29     this, SLOT( _Opacity( int ) )
30     );
31   this->connect(
32     this->m_UI->m_LineWidth, SIGNAL( valueChanged( double ) ),
33     this, SLOT( _LineWidth( double ) )
34     );
35   this->connect(
36     this->m_UI->m_PointSize, SIGNAL( valueChanged( double ) ),
37     this, SLOT( _PointSize( double ) )
38     );
39 }
40
41 // -------------------------------------------------------------------------
42 cpExtensions::QT::PropertyWidget::
43 ~PropertyWidget( )
44 {
45   delete this->m_UI;
46 }
47
48 // -------------------------------------------------------------------------
49 vtkProp* cpExtensions::QT::PropertyWidget::
50 GetProp( )
51 {
52   return( this->m_Prop );
53 }
54
55 // -------------------------------------------------------------------------
56 vtkRenderWindow* cpExtensions::QT::PropertyWidget::
57 GetRenderWindow( )
58 {
59   return( this->m_Window );
60 }
61
62 // -------------------------------------------------------------------------
63 void cpExtensions::QT::PropertyWidget::
64 SetProp( vtkProp* p )
65 {
66   this->m_Prop = p;
67   if( this->m_Prop == NULL )
68     return;
69
70   auto actor = dynamic_cast< vtkActor* >( this->m_Prop );
71   if( actor == NULL )
72     return;
73
74   double opacity = actor->GetProperty( )->GetOpacity( );
75   double lw = actor->GetProperty( )->GetLineWidth( );
76   double ps = actor->GetProperty( )->GetPointSize( );
77   opacity *= this->m_UI->m_Opacity->maximum( );
78
79   this->m_UI->m_Opacity->setValue( opacity );
80   this->m_UI->m_LineWidth->setValue( lw );
81   this->m_UI->m_PointSize->setValue( ps );
82 }
83
84 // -------------------------------------------------------------------------
85 void cpExtensions::QT::PropertyWidget::
86 SetRenderWindow( vtkRenderWindow* w )
87 {
88   this->m_Window = w;
89 }
90
91 // -------------------------------------------------------------------------
92 void cpExtensions::QT::PropertyWidget::
93 _Color( )
94 {
95   if( this->m_Prop == NULL )
96     return;
97
98   auto actor = dynamic_cast< vtkActor* >( this->m_Prop );
99   if( actor == NULL )
100     return;
101
102   double rgb[ 3 ];
103   actor->GetProperty( )->GetColor( rgb );
104   QColor color =
105     QColorDialog::getColor(
106       QColor( rgb[ 0 ] * 255, rgb[ 1 ] * 255, rgb[ 2 ] * 255 ),
107       this,
108       "Select Color",
109       QColorDialog::DontUseNativeDialog
110       );
111   if( color.isValid( ) )
112   {
113     rgb[ 0 ] = double( color.red( ) ) / double( 255 );
114     rgb[ 1 ] = double( color.green( ) ) / double( 255 );
115     rgb[ 2 ] = double( color.blue( ) ) / double( 255 );
116     actor->GetMapper( )->ScalarVisibilityOff( );
117     actor->GetProperty( )->SetColor( rgb );
118     actor->Modified( );
119     if( this->m_Window != NULL )
120       this->m_Window->Render( );
121
122   } // fi
123 }
124
125 // -------------------------------------------------------------------------
126 void cpExtensions::QT::PropertyWidget::
127 _Opacity( int o )
128 {
129   if( this->m_Prop == NULL )
130     return;
131
132   auto actor = dynamic_cast< vtkActor* >( this->m_Prop );
133   if( actor == NULL )
134     return;
135   double v = double( o ) / double( this->m_UI->m_Opacity->maximum( ) );
136   actor->GetProperty( )->SetOpacity( v );
137   actor->Modified( );
138   if( this->m_Window != NULL )
139     this->m_Window->Render( );
140 }
141
142 // -------------------------------------------------------------------------
143 void cpExtensions::QT::PropertyWidget::
144 _LineWidth( double lw )
145 {
146   if( this->m_Prop == NULL )
147     return;
148
149   auto actor = dynamic_cast< vtkActor* >( this->m_Prop );
150   if( actor == NULL )
151     return;
152   actor->GetProperty( )->SetLineWidth( lw );
153   actor->Modified( );
154   if( this->m_Window != NULL )
155     this->m_Window->Render( );
156 }
157
158 // -------------------------------------------------------------------------
159 void cpExtensions::QT::PropertyWidget::
160 _PointSize( double ps )
161 {
162   if( this->m_Prop == NULL )
163     return;
164
165   auto actor = dynamic_cast< vtkActor* >( this->m_Prop );
166   if( actor == NULL )
167     return;
168   actor->GetProperty( )->SetPointSize( ps );
169   actor->Modified( );
170   if( this->m_Window != NULL )
171     this->m_Window->Render( );
172 }
173
174 #endif // cpExtensions_QT4
175
176 // eof - $RCSfile$