]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/DataObjectVisualizationQtDialog.cxx
cd6ca7662d387bcdb9f47e923fd82827f74e4bbe
[cpPlugins.git] / lib / cpPlugins / DataObjectVisualizationQtDialog.cxx
1 #include <cpPlugins/DataObjectVisualizationQtDialog.h>
2
3 #include <vtkImageActor.h>
4 #include <vtkImageData.h>
5 #include <vtkImageProperty.h>
6 #include <vtkMapper.h>
7 #include <vtkPolyData.h>
8 #include <vtkProperty.h>
9
10 #ifdef cpPlugins_QT4
11
12 #include <QCheckBox>
13 #include <QColorDialog>
14 #include <QDoubleSpinBox>
15 #include <QPushButton>
16 #include <QSlider>
17
18 // -------------------------------------------------------------------------
19 cpPlugins::DataObjectVisualizationQtDialog::
20 DataObjectVisualizationQtDialog( QWidget* parent, Qt::WindowFlags f )
21   : QDialog( parent, f ),
22     m_DataObject( NULL ),
23     m_WidgetsUpdated( false )
24 {
25   this->m_Title = new QLabel( this );
26   this->m_Title->setText( "Visualization properties" );
27   this->m_MainLayout = new QGridLayout( this );
28   this->m_ToolsLayout = new QVBoxLayout( );
29   this->m_ToolsLayout->addWidget( this->m_Title );
30   this->m_MainLayout->addLayout( this->m_ToolsLayout, 0, 0, 1, 1 );
31 }
32
33 // -------------------------------------------------------------------------
34 cpPlugins::DataObjectVisualizationQtDialog::
35 ~DataObjectVisualizationQtDialog( )
36 {
37   delete this->m_Title;
38   delete this->m_ToolsLayout;
39   delete this->m_MainLayout;
40 }
41
42 // -------------------------------------------------------------------------
43 cpPlugins::DataObject* cpPlugins::DataObjectVisualizationQtDialog::
44 getDataObject( ) const
45 {
46   return( this->m_DataObject );
47 }
48
49 // -------------------------------------------------------------------------
50 bool cpPlugins::DataObjectVisualizationQtDialog::
51 setDataObject( DataObject* obj )
52 {
53   if( this->m_DataObject != NULL || obj == NULL )
54     return( false );
55   this->m_DataObject = obj;
56   this->m_WidgetsUpdated = false;
57   return( true );
58 }
59
60 // -------------------------------------------------------------------------
61 int cpPlugins::DataObjectVisualizationQtDialog::
62 exec( )
63 {
64   this->_updateWidgets( );
65   int ret = this->QDialog::exec( );
66   /* TODO
67      if( ret == 1 )
68      this->updateParameters( );
69      else
70      this->updateView( );
71   */
72   return( ret );
73 }
74
75 // -------------------------------------------------------------------------
76 void cpPlugins::DataObjectVisualizationQtDialog::
77 _addButtons( )
78 {
79   // Add buttons
80   this->m_Buttons = new QDialogButtonBox( QDialogButtonBox::Ok );
81   this->connect(
82     this->m_Buttons, SIGNAL( accepted( ) ), this, SLOT( accept( ) )
83     );
84   this->connect(
85     this->m_Buttons, SIGNAL( rejected( ) ), this, SLOT( reject( ) )
86     );
87   this->m_ToolsLayout->addWidget( this->m_Buttons );
88   // TODO: this->updateView( );
89   this->m_WidgetsUpdated = true;
90 }
91
92 // -------------------------------------------------------------------------
93 void cpPlugins::DataObjectVisualizationQtDialog::
94 _updateWidgets( )
95 {
96   if( this->m_WidgetsUpdated || this->m_DataObject == NULL )
97     return;
98
99   // Set dialog title
100   std::stringstream title;
101   title
102     << "Parameters for an object of class \""
103     << this->m_DataObject->GetClassName( )
104     << "\"";
105   this->m_Title->setText( title.str( ).c_str( ) );
106
107   // Configure particular objects
108   this->_configureForImage( );
109   this->_configureForMesh( );
110
111   // Update values
112   this->_addButtons( );
113 }
114
115 // -------------------------------------------------------------------------
116 void cpPlugins::DataObjectVisualizationQtDialog::
117 _configureForImage( )
118 {
119   auto image = this->m_DataObject->GetVTK< vtkImageData >( );
120   auto aIt = this->m_DataObject->BeginVTKActors( );
121   if( image == NULL || aIt == this->m_DataObject->EndVTKActors( ) )
122     return;
123   auto actor = dynamic_cast< vtkImageActor* >( aIt->Actor.GetPointer( ) );
124   if( actor == NULL )
125     return;
126   
127   double r[ 2 ];
128   image->GetScalarRange( r );
129   double w = actor->GetProperty( )->GetColorWindow( );
130   double l = actor->GetProperty( )->GetColorLevel( );
131   double sw = double( 1000 ) * w / ( r[ 1 ] - r[ 0 ] );
132   double sl = double( 1000 ) * ( l - r[ 0 ] ) / ( r[ 1 ] - r[ 0 ] );
133   double op = double( 10 ) * actor->GetProperty( )->GetOpacity( );
134
135   QDoubleSpinBox* win_box = new QDoubleSpinBox( this );
136   win_box->setObjectName( "win_box" );
137   win_box->setDecimals( 3 );
138   win_box->setMinimum( 0 );
139   win_box->setMaximum( r[ 1 ] - r[ 0 ] );
140   win_box->setValue( w );
141   win_box->connect(
142     win_box, SIGNAL( valueChanged( double ) ),
143     this, SLOT( _boxWindow( double ) )
144     );
145
146   QSlider* win_sld = new QSlider( Qt::Horizontal, this );
147   win_sld->setObjectName( "win_sld" );
148   win_sld->setRange( 0, 1000 );
149   win_sld->setValue( ( unsigned int )( sw ) );
150   win_sld->connect(
151     win_sld, SIGNAL( valueChanged( int ) ),
152     this, SLOT( _sldWindow( int ) )
153     );
154
155   QHBoxLayout* win_layout = new QHBoxLayout( );
156   QLabel* win_label = new QLabel( this );
157   win_label->setText( QString( "Window: " ) );
158   win_layout->addWidget( win_label );
159   win_layout->addWidget( win_box );
160   win_layout->addWidget( win_sld );
161   this->m_ToolsLayout->addLayout( win_layout );
162
163   QDoubleSpinBox* lev_box = new QDoubleSpinBox( this );
164   lev_box->setObjectName( "lev_box" );
165   lev_box->setDecimals( 3 );
166   lev_box->setMinimum( r[ 0 ] );
167   lev_box->setMaximum( r[ 1 ] );
168   lev_box->setValue( l );
169   lev_box->connect(
170     lev_box, SIGNAL( valueChanged( double ) ),
171     this, SLOT( _boxLevel( double ) )
172     );
173
174   QSlider* lev_sld = new QSlider( Qt::Horizontal, this );
175   lev_sld->setObjectName( "lev_sld" );
176   lev_sld->setRange( 0, 1000 );
177   lev_sld->setValue( ( unsigned int )( sl ) );
178   lev_sld->connect(
179     lev_sld, SIGNAL( valueChanged( int ) ),
180     this, SLOT( _sldLevel( int ) )
181     );
182
183   QHBoxLayout* lev_layout = new QHBoxLayout( );
184   QLabel* lev_label = new QLabel( this );
185   lev_label->setText( QString( "Level: " ) );
186   lev_layout->addWidget( lev_label );
187   lev_layout->addWidget( lev_box );
188   lev_layout->addWidget( lev_sld );
189   this->m_ToolsLayout->addLayout( lev_layout );
190
191   // Configure generic objects
192   QSlider* op_sld = new QSlider( Qt::Horizontal, this );
193   op_sld->setObjectName( "op_sld" );
194   op_sld->setRange( 0, 10 );
195   op_sld->setValue( ( unsigned int )( op ) );
196   op_sld->connect(
197     op_sld, SIGNAL( valueChanged( int ) ),
198     this, SLOT( _sldOpacity( int ) )
199     );
200
201   QHBoxLayout* op_layout = new QHBoxLayout( );
202   QLabel* op_label = new QLabel( this );
203   op_label->setText( QString( "Opacity: " ) );
204   op_layout->addWidget( op_label );
205   op_layout->addWidget( op_sld );
206   this->m_ToolsLayout->addLayout( op_layout );
207 }
208
209 // -------------------------------------------------------------------------
210 void cpPlugins::DataObjectVisualizationQtDialog::
211 _configureForMesh( )
212 {
213   auto mesh = this->m_DataObject->GetVTK< vtkPolyData >( );
214   auto aIt = this->m_DataObject->BeginVTKActors( );
215   if( mesh == NULL || aIt == this->m_DataObject->EndVTKActors( ) )
216     return;
217   auto actor = dynamic_cast< vtkActor* >( aIt->Actor.GetPointer( ) );
218   if( actor == NULL )
219     return;
220
221   QSpinBox* ps_box = new QSpinBox( this );
222   ps_box->setObjectName( "ps_box" );
223   ps_box->setMinimum( 1 );
224   ps_box->setMaximum( 100 );
225   ps_box->setValue( actor->GetProperty( )->GetPointSize( ) );
226   ps_box->connect(
227     ps_box, SIGNAL( valueChanged( int ) ),
228     this, SLOT( _boxPointSize( int ) )
229     );
230
231   QHBoxLayout* ps_layout = new QHBoxLayout( );
232   QLabel* ps_label = new QLabel( this );
233   ps_label->setText( QString( "Point size: " ) );
234   ps_layout->addWidget( ps_label );
235   ps_layout->addWidget( ps_box );
236   this->m_ToolsLayout->addLayout( ps_layout );
237
238   QSpinBox* lw_box = new QSpinBox( this );
239   lw_box->setObjectName( "lw_box" );
240   lw_box->setMinimum( 1 );
241   lw_box->setMaximum( 100 );
242   lw_box->setValue( actor->GetProperty( )->GetLineWidth( ) );
243   lw_box->connect(
244     lw_box, SIGNAL( valueChanged( int ) ),
245     this, SLOT( _boxLineWidth( int ) )
246     );
247
248   QHBoxLayout* lw_layout = new QHBoxLayout( );
249   QLabel* lw_label = new QLabel( this );
250   lw_label->setText( QString( "Line width: " ) );
251   lw_layout->addWidget( lw_label );
252   lw_layout->addWidget( lw_box );
253   this->m_ToolsLayout->addLayout( lw_layout );
254
255   QCheckBox* sv_box = new QCheckBox( this );
256   sv_box->setObjectName( "sv_box" );
257   sv_box->setText( "Scalar visibility: " );
258   sv_box->setChecked( ( actor->GetMapper( )->GetScalarVisibility( ) == 1 ) );
259   sv_box->connect(
260     sv_box, SIGNAL( stateChanged( int ) ),
261     this, SLOT( _scalarVisibility( int ) )
262     );
263
264   QHBoxLayout* sv_layout = new QHBoxLayout( );
265   sv_layout->addWidget( sv_box );
266   this->m_ToolsLayout->addLayout( sv_layout );
267
268   double cr, cg, cb;
269   actor->GetProperty( )->GetColor( cr, cg, cb );
270   cr *= double( 255 );
271   cg *= double( 255 );
272   cb *= double( 255 );
273
274   QPushButton* color_button = new QPushButton( "Color", this );
275   color_button->setObjectName( "color_button" );
276   QPalette color_palette = color_button->palette( );
277   color_palette.setColor( QPalette::Button, QColor( cr, cg, cb ) );
278   color_button->setAutoFillBackground( true );
279   color_button->setPalette( color_palette );
280   color_button->update( );
281   color_button->connect(
282     color_button, SIGNAL( clicked( ) ),
283     this, SLOT( _color( ) )
284     );
285
286   QHBoxLayout* color_layout = new QHBoxLayout( );
287   color_layout->addWidget( color_button );
288   this->m_ToolsLayout->addLayout( color_layout );
289
290   // Configure generic objects
291   QSlider* op_sld = new QSlider( Qt::Horizontal, this );
292   op_sld->setObjectName( "op_sld" );
293   op_sld->setRange( 0, 10 );
294   op_sld->setValue(
295     ( unsigned int )( actor->GetProperty( )->GetOpacity( ) * double( 10 ) )
296     );
297   op_sld->connect(
298     op_sld, SIGNAL( valueChanged( int ) ),
299     this, SLOT( _sldOpacity( int ) )
300     );
301
302   QHBoxLayout* op_layout = new QHBoxLayout( );
303   QLabel* op_label = new QLabel( this );
304   op_label->setText( QString( "Opacity: " ) );
305   op_layout->addWidget( op_label );
306   op_layout->addWidget( op_sld );
307   this->m_ToolsLayout->addLayout( op_layout );
308 }
309
310 // -------------------------------------------------------------------------
311 void cpPlugins::DataObjectVisualizationQtDialog::
312 _setWindow( double w )
313 {
314   if( this->m_DataObject == NULL )
315     return;
316   auto aIt = this->m_DataObject->BeginVTKActors( );
317   for( ; aIt != this->m_DataObject->EndVTKActors( ); ++aIt )
318   {
319     auto actor = dynamic_cast< vtkImageActor* >( aIt->Actor.GetPointer( ) );
320     if( actor != NULL )
321     {
322       actor->GetProperty( )->SetColorWindow( w );
323       actor->Modified( );
324
325     } // fi
326
327   } // rof
328   this->_render( );
329 }
330
331 // -------------------------------------------------------------------------
332 void cpPlugins::DataObjectVisualizationQtDialog::
333 _setLevel( double l )
334 {
335   if( this->m_DataObject == NULL )
336     return;
337   auto aIt = this->m_DataObject->BeginVTKActors( );
338   for( ; aIt != this->m_DataObject->EndVTKActors( ); ++aIt )
339   {
340     auto actor = dynamic_cast< vtkImageActor* >( aIt->Actor.GetPointer( ) );
341     if( actor != NULL )
342     {
343       actor->GetProperty( )->SetColorLevel( l );
344       actor->Modified( );
345
346     } // fi
347
348   } // rof
349   this->_render( );
350 }
351
352 // -------------------------------------------------------------------------
353 void cpPlugins::DataObjectVisualizationQtDialog::
354 _render( )
355 {
356   if( this->m_DataObject == NULL )
357     return;
358   this->m_DataObject->RenderVTKActors( );
359 }
360
361 // -------------------------------------------------------------------------
362 void cpPlugins::DataObjectVisualizationQtDialog::
363 _boxWindow( double v )
364 {
365   auto* box = this->findChild< QDoubleSpinBox* >( "win_box" );
366   auto* sld = this->findChild< QSlider* >( "win_sld" );
367   if( box == NULL || sld == NULL )
368     return;
369
370   double min = double( sld->minimum( ) );
371   double max = double( sld->maximum( ) );
372   double vmin = box->minimum( );
373   double vmax = box->maximum( );
374   double s = ( v - vmin ) / ( vmax - vmin );
375   s = ( ( max - min ) * s ) + min;
376
377   bool o = sld->blockSignals( true );
378   sld->setValue( ( unsigned int )( s ) );
379   sld->blockSignals( o );
380   this->_setWindow( v );
381 }
382
383 // -------------------------------------------------------------------------
384 void cpPlugins::DataObjectVisualizationQtDialog::
385 _sldWindow( int v )
386 {
387   auto* box = this->findChild< QDoubleSpinBox* >( "win_box" );
388   auto* sld = this->findChild< QSlider* >( "win_sld" );
389   if( box == NULL || sld == NULL )
390     return;
391
392   double min = double( sld->minimum( ) );
393   double max = double( sld->maximum( ) );
394   double vmin = box->minimum( );
395   double vmax = box->maximum( );
396   double s = ( double( v ) - min ) / ( max - min );
397   s = ( ( vmax - vmin ) * s ) + vmin;
398
399   bool o = box->blockSignals( true );
400   box->setValue( s );
401   box->blockSignals( o );
402   this->_setWindow( s );
403 }
404
405 // -------------------------------------------------------------------------
406 void cpPlugins::DataObjectVisualizationQtDialog::
407 _boxLevel( double v )
408 {
409   auto* box = this->findChild< QDoubleSpinBox* >( "lev_box" );
410   auto* sld = this->findChild< QSlider* >( "lev_sld" );
411   if( box == NULL || sld == NULL )
412     return;
413
414   double min = double( sld->minimum( ) );
415   double max = double( sld->maximum( ) );
416   double vmin = box->minimum( );
417   double vmax = box->maximum( );
418   double s = ( v - vmin ) / ( vmax - vmin );
419   s = ( ( max - min ) * s ) + min;
420
421   bool o = sld->blockSignals( true );
422   sld->setValue( ( unsigned int )( s ) );
423   sld->blockSignals( o );
424   this->_setLevel( v );
425 }
426
427 // -------------------------------------------------------------------------
428 void cpPlugins::DataObjectVisualizationQtDialog::
429 _sldLevel( int v )
430 {
431   auto* box = this->findChild< QDoubleSpinBox* >( "lev_box" );
432   auto* sld = this->findChild< QSlider* >( "lev_sld" );
433   if( box == NULL || sld == NULL )
434     return;
435
436   double min = double( sld->minimum( ) );
437   double max = double( sld->maximum( ) );
438   double vmin = box->minimum( );
439   double vmax = box->maximum( );
440   double s = ( double( v ) - min ) / ( max - min );
441   s = ( ( vmax - vmin ) * s ) + vmin;
442
443   bool o = box->blockSignals( true );
444   box->setValue( s );
445   box->blockSignals( o );
446   this->_setLevel( s );
447 }
448
449 // -------------------------------------------------------------------------
450 void cpPlugins::DataObjectVisualizationQtDialog::
451 _sldOpacity( int v )
452 {
453   if( this->m_DataObject == NULL )
454     return;
455   auto* sld = this->findChild< QSlider* >( "op_sld" );
456   if( sld == NULL )
457     return;
458
459   double min = double( sld->minimum( ) );
460   double max = double( sld->maximum( ) );
461   double s = ( double( v ) - min ) / ( max - min );
462
463   auto aIt = this->m_DataObject->BeginVTKActors( );
464   for( ; aIt != this->m_DataObject->EndVTKActors( ); ++aIt )
465   {
466     auto ia = dynamic_cast< vtkImageActor* >( aIt->Actor.GetPointer( ) );
467     auto ma = dynamic_cast< vtkActor* >( aIt->Actor.GetPointer( ) );
468     if( ia != NULL )
469     {
470       ia->GetProperty( )->SetOpacity( s );
471       ia->Modified( );
472     }
473     else if( ma != NULL )
474     {
475       ma->GetProperty( )->SetOpacity( s );
476       ma->Modified( );
477
478     } // fi
479
480   } // rof
481   this->_render( );
482 }
483
484 // -------------------------------------------------------------------------
485 void cpPlugins::DataObjectVisualizationQtDialog::
486 _boxPointSize( int v )
487 {
488 }
489
490 // -------------------------------------------------------------------------
491 void cpPlugins::DataObjectVisualizationQtDialog::
492 _boxLineWidth( int v )
493 {
494 }
495
496 // -------------------------------------------------------------------------
497 void cpPlugins::DataObjectVisualizationQtDialog::
498 _scalarVisibility( int v )
499 {
500   if( this->m_DataObject == NULL )
501     return;
502   auto* btn = this->findChild< QPushButton* >( "color_button" );
503   auto* chk = this->findChild< QCheckBox* >( "sv_box" );
504   if( btn == NULL || chk == NULL )
505     return;
506   QPalette pal = btn->palette( );
507   QColor color = pal.color( QPalette::Button );
508   double rgb[ 3 ];
509   rgb[ 0 ] = double( color.red( ) ) / double( 255 );
510   rgb[ 1 ] = double( color.green( ) ) / double( 255 );
511   rgb[ 2 ] = double( color.blue( ) ) / double( 255 );
512
513   auto aIt = this->m_DataObject->BeginVTKActors( );
514   for( ; aIt != this->m_DataObject->EndVTKActors( ); ++aIt )
515   {
516     auto ma = dynamic_cast< vtkActor* >( aIt->Actor.GetPointer( ) );
517     if( ma != NULL )
518     {
519       if( !( chk->isChecked( ) ) )
520       {
521         ma->GetMapper( )->ScalarVisibilityOff( );
522         ma->GetProperty( )->SetColor( rgb );
523       }
524       else
525         ma->GetMapper( )->ScalarVisibilityOn( );
526       ma->Modified( );
527
528     } // fi
529
530   } // rof
531   this->_render( );
532 }
533
534 // -------------------------------------------------------------------------
535 void cpPlugins::DataObjectVisualizationQtDialog::
536 _color( )
537 {
538   if( this->m_DataObject == NULL )
539     return;
540   auto* btn = this->findChild< QPushButton* >( "color_button" );
541   auto* chk = this->findChild< QCheckBox* >( "sv_box" );
542   if( btn == NULL || chk == NULL )
543     return;
544
545   QPalette pal = btn->palette( );
546   QColor color =
547     QColorDialog::getColor(
548       pal.color( QPalette::Button ),
549       this,
550       "Select Color",
551       QColorDialog::DontUseNativeDialog
552       );
553   if( color.isValid( ) )
554   {
555     pal.setColor( QPalette::Button, color );
556     btn->setAutoFillBackground( true );
557     btn->setPalette( pal );
558     btn->update( );
559     this->_scalarVisibility( 0 );
560
561   } // fi
562 }
563
564 #endif // cpPlugins_QT4
565
566 // eof - $RCSfile$