]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/DataObjectVisualizationQtDialog.cxx
MVC integration at 50%
[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   /* TODO
120   auto image = this->m_DataObject->GetVTK< vtkImageData >( );
121   auto aIt = this->m_DataObject->BeginVTKActors( );
122   if( image == NULL || aIt == this->m_DataObject->EndVTKActors( ) )
123     return;
124   auto actor = dynamic_cast< vtkImageActor* >( aIt->Actor.GetPointer( ) );
125   if( actor == NULL )
126     return;
127   
128   double r[ 2 ];
129   image->GetScalarRange( r );
130   double w = actor->GetProperty( )->GetColorWindow( );
131   double l = actor->GetProperty( )->GetColorLevel( );
132   double sw = double( 1000 ) * w / ( r[ 1 ] - r[ 0 ] );
133   double sl = double( 1000 ) * ( l - r[ 0 ] ) / ( r[ 1 ] - r[ 0 ] );
134   double op = double( 10 ) * actor->GetProperty( )->GetOpacity( );
135
136   QDoubleSpinBox* win_box = new QDoubleSpinBox( this );
137   win_box->setObjectName( "win_box" );
138   win_box->setDecimals( 3 );
139   win_box->setMinimum( 0 );
140   win_box->setMaximum( r[ 1 ] - r[ 0 ] );
141   win_box->setValue( w );
142   win_box->connect(
143     win_box, SIGNAL( valueChanged( double ) ),
144     this, SLOT( _boxWindow( double ) )
145     );
146
147   QSlider* win_sld = new QSlider( Qt::Horizontal, this );
148   win_sld->setObjectName( "win_sld" );
149   win_sld->setRange( 0, 1000 );
150   win_sld->setValue( ( unsigned int )( sw ) );
151   win_sld->connect(
152     win_sld, SIGNAL( valueChanged( int ) ),
153     this, SLOT( _sldWindow( int ) )
154     );
155
156   QHBoxLayout* win_layout = new QHBoxLayout( );
157   QLabel* win_label = new QLabel( this );
158   win_label->setText( QString( "Window: " ) );
159   win_layout->addWidget( win_label );
160   win_layout->addWidget( win_box );
161   win_layout->addWidget( win_sld );
162   this->m_ToolsLayout->addLayout( win_layout );
163
164   QDoubleSpinBox* lev_box = new QDoubleSpinBox( this );
165   lev_box->setObjectName( "lev_box" );
166   lev_box->setDecimals( 3 );
167   lev_box->setMinimum( r[ 0 ] );
168   lev_box->setMaximum( r[ 1 ] );
169   lev_box->setValue( l );
170   lev_box->connect(
171     lev_box, SIGNAL( valueChanged( double ) ),
172     this, SLOT( _boxLevel( double ) )
173     );
174
175   QSlider* lev_sld = new QSlider( Qt::Horizontal, this );
176   lev_sld->setObjectName( "lev_sld" );
177   lev_sld->setRange( 0, 1000 );
178   lev_sld->setValue( ( unsigned int )( sl ) );
179   lev_sld->connect(
180     lev_sld, SIGNAL( valueChanged( int ) ),
181     this, SLOT( _sldLevel( int ) )
182     );
183
184   QHBoxLayout* lev_layout = new QHBoxLayout( );
185   QLabel* lev_label = new QLabel( this );
186   lev_label->setText( QString( "Level: " ) );
187   lev_layout->addWidget( lev_label );
188   lev_layout->addWidget( lev_box );
189   lev_layout->addWidget( lev_sld );
190   this->m_ToolsLayout->addLayout( lev_layout );
191
192   // Configure generic objects
193   QSlider* op_sld = new QSlider( Qt::Horizontal, this );
194   op_sld->setObjectName( "op_sld" );
195   op_sld->setRange( 0, 10 );
196   op_sld->setValue( ( unsigned int )( op ) );
197   op_sld->connect(
198     op_sld, SIGNAL( valueChanged( int ) ),
199     this, SLOT( _sldOpacity( int ) )
200     );
201
202   QHBoxLayout* op_layout = new QHBoxLayout( );
203   QLabel* op_label = new QLabel( this );
204   op_label->setText( QString( "Opacity: " ) );
205   op_layout->addWidget( op_label );
206   op_layout->addWidget( op_sld );
207   this->m_ToolsLayout->addLayout( op_layout );
208 */
209 }
210
211 // -------------------------------------------------------------------------
212 void cpPlugins::DataObjectVisualizationQtDialog::
213 _configureForMesh( )
214 {
215   /* TODO
216   auto mesh = this->m_DataObject->GetVTK< vtkPolyData >( );
217   auto aIt = this->m_DataObject->BeginVTKActors( );
218   if( mesh == NULL || aIt == this->m_DataObject->EndVTKActors( ) )
219     return;
220   auto actor = dynamic_cast< vtkActor* >( aIt->Actor.GetPointer( ) );
221   if( actor == NULL )
222     return;
223
224   QSpinBox* ps_box = new QSpinBox( this );
225   ps_box->setObjectName( "ps_box" );
226   ps_box->setMinimum( 1 );
227   ps_box->setMaximum( 100 );
228   ps_box->setValue( actor->GetProperty( )->GetPointSize( ) );
229   ps_box->connect(
230     ps_box, SIGNAL( valueChanged( int ) ),
231     this, SLOT( _boxPointSize( int ) )
232     );
233
234   QHBoxLayout* ps_layout = new QHBoxLayout( );
235   QLabel* ps_label = new QLabel( this );
236   ps_label->setText( QString( "Point size: " ) );
237   ps_layout->addWidget( ps_label );
238   ps_layout->addWidget( ps_box );
239   this->m_ToolsLayout->addLayout( ps_layout );
240
241   QSpinBox* lw_box = new QSpinBox( this );
242   lw_box->setObjectName( "lw_box" );
243   lw_box->setMinimum( 1 );
244   lw_box->setMaximum( 100 );
245   lw_box->setValue( actor->GetProperty( )->GetLineWidth( ) );
246   lw_box->connect(
247     lw_box, SIGNAL( valueChanged( int ) ),
248     this, SLOT( _boxLineWidth( int ) )
249     );
250
251   QHBoxLayout* lw_layout = new QHBoxLayout( );
252   QLabel* lw_label = new QLabel( this );
253   lw_label->setText( QString( "Line width: " ) );
254   lw_layout->addWidget( lw_label );
255   lw_layout->addWidget( lw_box );
256   this->m_ToolsLayout->addLayout( lw_layout );
257
258   QCheckBox* sv_box = new QCheckBox( this );
259   sv_box->setObjectName( "sv_box" );
260   sv_box->setText( "Scalar visibility: " );
261   sv_box->setChecked( ( actor->GetMapper( )->GetScalarVisibility( ) == 1 ) );
262   sv_box->connect(
263     sv_box, SIGNAL( stateChanged( int ) ),
264     this, SLOT( _scalarVisibility( int ) )
265     );
266
267   QHBoxLayout* sv_layout = new QHBoxLayout( );
268   sv_layout->addWidget( sv_box );
269   this->m_ToolsLayout->addLayout( sv_layout );
270
271   double cr, cg, cb;
272   actor->GetProperty( )->GetColor( cr, cg, cb );
273   cr *= double( 255 );
274   cg *= double( 255 );
275   cb *= double( 255 );
276
277   QPushButton* color_button = new QPushButton( "Color", this );
278   color_button->setObjectName( "color_button" );
279   QPalette color_palette = color_button->palette( );
280   color_palette.setColor( QPalette::Button, QColor( cr, cg, cb ) );
281   color_button->setAutoFillBackground( true );
282   color_button->setPalette( color_palette );
283   color_button->update( );
284   color_button->connect(
285     color_button, SIGNAL( clicked( ) ),
286     this, SLOT( _color( ) )
287     );
288
289   QHBoxLayout* color_layout = new QHBoxLayout( );
290   color_layout->addWidget( color_button );
291   this->m_ToolsLayout->addLayout( color_layout );
292
293   // Configure generic objects
294   QSlider* op_sld = new QSlider( Qt::Horizontal, this );
295   op_sld->setObjectName( "op_sld" );
296   op_sld->setRange( 0, 10 );
297   op_sld->setValue(
298     ( unsigned int )( actor->GetProperty( )->GetOpacity( ) * double( 10 ) )
299     );
300   op_sld->connect(
301     op_sld, SIGNAL( valueChanged( int ) ),
302     this, SLOT( _sldOpacity( int ) )
303     );
304
305   QHBoxLayout* op_layout = new QHBoxLayout( );
306   QLabel* op_label = new QLabel( this );
307   op_label->setText( QString( "Opacity: " ) );
308   op_layout->addWidget( op_label );
309   op_layout->addWidget( op_sld );
310   this->m_ToolsLayout->addLayout( op_layout );
311 */
312 }
313
314 // -------------------------------------------------------------------------
315 void cpPlugins::DataObjectVisualizationQtDialog::
316 _setWindow( double w )
317 {
318   /* TODO
319   if( this->m_DataObject == NULL )
320     return;
321   auto aIt = this->m_DataObject->BeginVTKActors( );
322   for( ; aIt != this->m_DataObject->EndVTKActors( ); ++aIt )
323   {
324     auto actor = dynamic_cast< vtkImageActor* >( aIt->Actor.GetPointer( ) );
325     if( actor != NULL )
326     {
327       actor->GetProperty( )->SetColorWindow( w );
328       actor->Modified( );
329
330     } // fi
331
332   } // rof
333   this->_render( );
334 */
335 }
336
337 // -------------------------------------------------------------------------
338 void cpPlugins::DataObjectVisualizationQtDialog::
339 _setLevel( double l )
340 {
341   /* TODO
342   if( this->m_DataObject == NULL )
343     return;
344   auto aIt = this->m_DataObject->BeginVTKActors( );
345   for( ; aIt != this->m_DataObject->EndVTKActors( ); ++aIt )
346   {
347     auto actor = dynamic_cast< vtkImageActor* >( aIt->Actor.GetPointer( ) );
348     if( actor != NULL )
349     {
350       actor->GetProperty( )->SetColorLevel( l );
351       actor->Modified( );
352
353     } // fi
354
355   } // rof
356   this->_render( );
357 */
358 }
359
360 // -------------------------------------------------------------------------
361 void cpPlugins::DataObjectVisualizationQtDialog::
362 _render( )
363 {
364   /* TODO
365   if( this->m_DataObject == NULL )
366     return;
367   this->m_DataObject->RenderVTKActors( );
368 */
369 }
370
371 // -------------------------------------------------------------------------
372 void cpPlugins::DataObjectVisualizationQtDialog::
373 _boxWindow( double v )
374 {
375   auto* box = this->findChild< QDoubleSpinBox* >( "win_box" );
376   auto* sld = this->findChild< QSlider* >( "win_sld" );
377   if( box == NULL || sld == NULL )
378     return;
379
380   double min = double( sld->minimum( ) );
381   double max = double( sld->maximum( ) );
382   double vmin = box->minimum( );
383   double vmax = box->maximum( );
384   double s = ( v - vmin ) / ( vmax - vmin );
385   s = ( ( max - min ) * s ) + min;
386
387   bool o = sld->blockSignals( true );
388   sld->setValue( ( unsigned int )( s ) );
389   sld->blockSignals( o );
390   this->_setWindow( v );
391 }
392
393 // -------------------------------------------------------------------------
394 void cpPlugins::DataObjectVisualizationQtDialog::
395 _sldWindow( int v )
396 {
397   auto* box = this->findChild< QDoubleSpinBox* >( "win_box" );
398   auto* sld = this->findChild< QSlider* >( "win_sld" );
399   if( box == NULL || sld == NULL )
400     return;
401
402   double min = double( sld->minimum( ) );
403   double max = double( sld->maximum( ) );
404   double vmin = box->minimum( );
405   double vmax = box->maximum( );
406   double s = ( double( v ) - min ) / ( max - min );
407   s = ( ( vmax - vmin ) * s ) + vmin;
408
409   bool o = box->blockSignals( true );
410   box->setValue( s );
411   box->blockSignals( o );
412   this->_setWindow( s );
413 }
414
415 // -------------------------------------------------------------------------
416 void cpPlugins::DataObjectVisualizationQtDialog::
417 _boxLevel( double v )
418 {
419   auto* box = this->findChild< QDoubleSpinBox* >( "lev_box" );
420   auto* sld = this->findChild< QSlider* >( "lev_sld" );
421   if( box == NULL || sld == NULL )
422     return;
423
424   double min = double( sld->minimum( ) );
425   double max = double( sld->maximum( ) );
426   double vmin = box->minimum( );
427   double vmax = box->maximum( );
428   double s = ( v - vmin ) / ( vmax - vmin );
429   s = ( ( max - min ) * s ) + min;
430
431   bool o = sld->blockSignals( true );
432   sld->setValue( ( unsigned int )( s ) );
433   sld->blockSignals( o );
434   this->_setLevel( v );
435 }
436
437 // -------------------------------------------------------------------------
438 void cpPlugins::DataObjectVisualizationQtDialog::
439 _sldLevel( int v )
440 {
441   auto* box = this->findChild< QDoubleSpinBox* >( "lev_box" );
442   auto* sld = this->findChild< QSlider* >( "lev_sld" );
443   if( box == NULL || sld == NULL )
444     return;
445
446   double min = double( sld->minimum( ) );
447   double max = double( sld->maximum( ) );
448   double vmin = box->minimum( );
449   double vmax = box->maximum( );
450   double s = ( double( v ) - min ) / ( max - min );
451   s = ( ( vmax - vmin ) * s ) + vmin;
452
453   bool o = box->blockSignals( true );
454   box->setValue( s );
455   box->blockSignals( o );
456   this->_setLevel( s );
457 }
458
459 // -------------------------------------------------------------------------
460 void cpPlugins::DataObjectVisualizationQtDialog::
461 _sldOpacity( int v )
462 {
463   /* TODO
464   if( this->m_DataObject == NULL )
465     return;
466   auto* sld = this->findChild< QSlider* >( "op_sld" );
467   if( sld == NULL )
468     return;
469
470   double min = double( sld->minimum( ) );
471   double max = double( sld->maximum( ) );
472   double s = ( double( v ) - min ) / ( max - min );
473
474   auto aIt = this->m_DataObject->BeginVTKActors( );
475   for( ; aIt != this->m_DataObject->EndVTKActors( ); ++aIt )
476   {
477     auto ia = dynamic_cast< vtkImageActor* >( aIt->Actor.GetPointer( ) );
478     auto ma = dynamic_cast< vtkActor* >( aIt->Actor.GetPointer( ) );
479     if( ia != NULL )
480     {
481       ia->GetProperty( )->SetOpacity( s );
482       ia->Modified( );
483     }
484     else if( ma != NULL )
485     {
486       ma->GetProperty( )->SetOpacity( s );
487       ma->Modified( );
488
489     } // fi
490
491   } // rof
492   this->_render( );
493 */
494 }
495
496 // -------------------------------------------------------------------------
497 void cpPlugins::DataObjectVisualizationQtDialog::
498 _boxPointSize( int v )
499 {
500 }
501
502 // -------------------------------------------------------------------------
503 void cpPlugins::DataObjectVisualizationQtDialog::
504 _boxLineWidth( int v )
505 {
506 }
507
508 // -------------------------------------------------------------------------
509 void cpPlugins::DataObjectVisualizationQtDialog::
510 _scalarVisibility( int v )
511 {
512   /* TODO
513   if( this->m_DataObject == NULL )
514     return;
515   auto* btn = this->findChild< QPushButton* >( "color_button" );
516   auto* chk = this->findChild< QCheckBox* >( "sv_box" );
517   if( btn == NULL || chk == NULL )
518     return;
519   QPalette pal = btn->palette( );
520   QColor color = pal.color( QPalette::Button );
521   double rgb[ 3 ];
522   rgb[ 0 ] = double( color.red( ) ) / double( 255 );
523   rgb[ 1 ] = double( color.green( ) ) / double( 255 );
524   rgb[ 2 ] = double( color.blue( ) ) / double( 255 );
525
526   auto aIt = this->m_DataObject->BeginVTKActors( );
527   for( ; aIt != this->m_DataObject->EndVTKActors( ); ++aIt )
528   {
529     auto ma = dynamic_cast< vtkActor* >( aIt->Actor.GetPointer( ) );
530     if( ma != NULL )
531     {
532       if( !( chk->isChecked( ) ) )
533       {
534         ma->GetMapper( )->ScalarVisibilityOff( );
535         ma->GetProperty( )->SetColor( rgb );
536       }
537       else
538         ma->GetMapper( )->ScalarVisibilityOn( );
539       ma->Modified( );
540
541     } // fi
542
543   } // rof
544   this->_render( );
545 */
546 }
547
548 // -------------------------------------------------------------------------
549 void cpPlugins::DataObjectVisualizationQtDialog::
550 _color( )
551 {
552   /* TODO
553   if( this->m_DataObject == NULL )
554     return;
555   auto* btn = this->findChild< QPushButton* >( "color_button" );
556   auto* chk = this->findChild< QCheckBox* >( "sv_box" );
557   if( btn == NULL || chk == NULL )
558     return;
559
560   QPalette pal = btn->palette( );
561   QColor color =
562     QColorDialog::getColor(
563       pal.color( QPalette::Button ),
564       this,
565       "Select Color",
566       QColorDialog::DontUseNativeDialog
567       );
568   if( color.isValid( ) )
569   {
570     pal.setColor( QPalette::Button, color );
571     btn->setAutoFillBackground( true );
572     btn->setPalette( pal );
573     btn->update( );
574     this->_scalarVisibility( 0 );
575
576   } // fi
577 */
578 }
579
580 #endif // cpPlugins_QT4
581
582 // eof - $RCSfile$