1 #include <cpPlugins/DataObjectVisualizationQtDialog.h>
3 #include <vtkImageActor.h>
4 #include <vtkImageData.h>
5 #include <vtkImageProperty.h>
7 #include <vtkPolyData.h>
8 #include <vtkProperty.h>
13 #include <QColorDialog>
14 #include <QDoubleSpinBox>
15 #include <QPushButton>
18 // -------------------------------------------------------------------------
19 cpPlugins::DataObjectVisualizationQtDialog::
20 DataObjectVisualizationQtDialog( QWidget* parent, Qt::WindowFlags f )
21 : QDialog( parent, f ),
23 m_WidgetsUpdated( false )
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 );
33 // -------------------------------------------------------------------------
34 cpPlugins::DataObjectVisualizationQtDialog::
35 ~DataObjectVisualizationQtDialog( )
38 delete this->m_ToolsLayout;
39 delete this->m_MainLayout;
42 // -------------------------------------------------------------------------
43 cpPlugins::DataObject* cpPlugins::DataObjectVisualizationQtDialog::
44 getDataObject( ) const
46 return( this->m_DataObject );
49 // -------------------------------------------------------------------------
50 bool cpPlugins::DataObjectVisualizationQtDialog::
51 setDataObject( DataObject* obj )
53 if( this->m_DataObject != NULL || obj == NULL )
55 this->m_DataObject = obj;
56 this->m_WidgetsUpdated = false;
60 // -------------------------------------------------------------------------
61 int cpPlugins::DataObjectVisualizationQtDialog::
64 this->_updateWidgets( );
65 int ret = this->QDialog::exec( );
68 this->updateParameters( );
75 // -------------------------------------------------------------------------
76 void cpPlugins::DataObjectVisualizationQtDialog::
80 this->m_Buttons = new QDialogButtonBox( QDialogButtonBox::Ok );
82 this->m_Buttons, SIGNAL( accepted( ) ), this, SLOT( accept( ) )
85 this->m_Buttons, SIGNAL( rejected( ) ), this, SLOT( reject( ) )
87 this->m_ToolsLayout->addWidget( this->m_Buttons );
88 // TODO: this->updateView( );
89 this->m_WidgetsUpdated = true;
92 // -------------------------------------------------------------------------
93 void cpPlugins::DataObjectVisualizationQtDialog::
96 if( this->m_WidgetsUpdated || this->m_DataObject == NULL )
100 std::stringstream title;
102 << "Parameters for an object of class \""
103 << this->m_DataObject->GetClassName( )
105 this->m_Title->setText( title.str( ).c_str( ) );
107 // Configure particular objects
108 this->_configureForImage( );
109 this->_configureForMesh( );
112 this->_addButtons( );
115 // -------------------------------------------------------------------------
116 void cpPlugins::DataObjectVisualizationQtDialog::
117 _configureForImage( )
120 auto image = this->m_DataObject->GetVTK< vtkImageData >( );
121 auto aIt = this->m_DataObject->BeginVTKActors( );
122 if( image == NULL || aIt == this->m_DataObject->EndVTKActors( ) )
124 auto actor = dynamic_cast< vtkImageActor* >( aIt->Actor.GetPointer( ) );
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( );
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 );
143 win_box, SIGNAL( valueChanged( double ) ),
144 this, SLOT( _boxWindow( double ) )
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 ) );
152 win_sld, SIGNAL( valueChanged( int ) ),
153 this, SLOT( _sldWindow( int ) )
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 );
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 );
171 lev_box, SIGNAL( valueChanged( double ) ),
172 this, SLOT( _boxLevel( double ) )
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 ) );
180 lev_sld, SIGNAL( valueChanged( int ) ),
181 this, SLOT( _sldLevel( int ) )
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 );
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 ) );
198 op_sld, SIGNAL( valueChanged( int ) ),
199 this, SLOT( _sldOpacity( int ) )
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 );
211 // -------------------------------------------------------------------------
212 void cpPlugins::DataObjectVisualizationQtDialog::
216 auto mesh = this->m_DataObject->GetVTK< vtkPolyData >( );
217 auto aIt = this->m_DataObject->BeginVTKActors( );
218 if( mesh == NULL || aIt == this->m_DataObject->EndVTKActors( ) )
220 auto actor = dynamic_cast< vtkActor* >( aIt->Actor.GetPointer( ) );
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( ) );
230 ps_box, SIGNAL( valueChanged( int ) ),
231 this, SLOT( _boxPointSize( int ) )
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 );
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( ) );
247 lw_box, SIGNAL( valueChanged( int ) ),
248 this, SLOT( _boxLineWidth( int ) )
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 );
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 ) );
263 sv_box, SIGNAL( stateChanged( int ) ),
264 this, SLOT( _scalarVisibility( int ) )
267 QHBoxLayout* sv_layout = new QHBoxLayout( );
268 sv_layout->addWidget( sv_box );
269 this->m_ToolsLayout->addLayout( sv_layout );
272 actor->GetProperty( )->GetColor( cr, cg, cb );
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( ) )
289 QHBoxLayout* color_layout = new QHBoxLayout( );
290 color_layout->addWidget( color_button );
291 this->m_ToolsLayout->addLayout( color_layout );
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 );
298 ( unsigned int )( actor->GetProperty( )->GetOpacity( ) * double( 10 ) )
301 op_sld, SIGNAL( valueChanged( int ) ),
302 this, SLOT( _sldOpacity( int ) )
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 );
314 // -------------------------------------------------------------------------
315 void cpPlugins::DataObjectVisualizationQtDialog::
316 _setWindow( double w )
319 if( this->m_DataObject == NULL )
321 auto aIt = this->m_DataObject->BeginVTKActors( );
322 for( ; aIt != this->m_DataObject->EndVTKActors( ); ++aIt )
324 auto actor = dynamic_cast< vtkImageActor* >( aIt->Actor.GetPointer( ) );
327 actor->GetProperty( )->SetColorWindow( w );
337 // -------------------------------------------------------------------------
338 void cpPlugins::DataObjectVisualizationQtDialog::
339 _setLevel( double l )
342 if( this->m_DataObject == NULL )
344 auto aIt = this->m_DataObject->BeginVTKActors( );
345 for( ; aIt != this->m_DataObject->EndVTKActors( ); ++aIt )
347 auto actor = dynamic_cast< vtkImageActor* >( aIt->Actor.GetPointer( ) );
350 actor->GetProperty( )->SetColorLevel( l );
360 // -------------------------------------------------------------------------
361 void cpPlugins::DataObjectVisualizationQtDialog::
365 if( this->m_DataObject == NULL )
367 this->m_DataObject->RenderVTKActors( );
371 // -------------------------------------------------------------------------
372 void cpPlugins::DataObjectVisualizationQtDialog::
373 _boxWindow( double v )
375 auto* box = this->findChild< QDoubleSpinBox* >( "win_box" );
376 auto* sld = this->findChild< QSlider* >( "win_sld" );
377 if( box == NULL || sld == NULL )
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;
387 bool o = sld->blockSignals( true );
388 sld->setValue( ( unsigned int )( s ) );
389 sld->blockSignals( o );
390 this->_setWindow( v );
393 // -------------------------------------------------------------------------
394 void cpPlugins::DataObjectVisualizationQtDialog::
397 auto* box = this->findChild< QDoubleSpinBox* >( "win_box" );
398 auto* sld = this->findChild< QSlider* >( "win_sld" );
399 if( box == NULL || sld == NULL )
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;
409 bool o = box->blockSignals( true );
411 box->blockSignals( o );
412 this->_setWindow( s );
415 // -------------------------------------------------------------------------
416 void cpPlugins::DataObjectVisualizationQtDialog::
417 _boxLevel( double v )
419 auto* box = this->findChild< QDoubleSpinBox* >( "lev_box" );
420 auto* sld = this->findChild< QSlider* >( "lev_sld" );
421 if( box == NULL || sld == NULL )
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;
431 bool o = sld->blockSignals( true );
432 sld->setValue( ( unsigned int )( s ) );
433 sld->blockSignals( o );
434 this->_setLevel( v );
437 // -------------------------------------------------------------------------
438 void cpPlugins::DataObjectVisualizationQtDialog::
441 auto* box = this->findChild< QDoubleSpinBox* >( "lev_box" );
442 auto* sld = this->findChild< QSlider* >( "lev_sld" );
443 if( box == NULL || sld == NULL )
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;
453 bool o = box->blockSignals( true );
455 box->blockSignals( o );
456 this->_setLevel( s );
459 // -------------------------------------------------------------------------
460 void cpPlugins::DataObjectVisualizationQtDialog::
464 if( this->m_DataObject == NULL )
466 auto* sld = this->findChild< QSlider* >( "op_sld" );
470 double min = double( sld->minimum( ) );
471 double max = double( sld->maximum( ) );
472 double s = ( double( v ) - min ) / ( max - min );
474 auto aIt = this->m_DataObject->BeginVTKActors( );
475 for( ; aIt != this->m_DataObject->EndVTKActors( ); ++aIt )
477 auto ia = dynamic_cast< vtkImageActor* >( aIt->Actor.GetPointer( ) );
478 auto ma = dynamic_cast< vtkActor* >( aIt->Actor.GetPointer( ) );
481 ia->GetProperty( )->SetOpacity( s );
484 else if( ma != NULL )
486 ma->GetProperty( )->SetOpacity( s );
496 // -------------------------------------------------------------------------
497 void cpPlugins::DataObjectVisualizationQtDialog::
498 _boxPointSize( int v )
502 // -------------------------------------------------------------------------
503 void cpPlugins::DataObjectVisualizationQtDialog::
504 _boxLineWidth( int v )
508 // -------------------------------------------------------------------------
509 void cpPlugins::DataObjectVisualizationQtDialog::
510 _scalarVisibility( int v )
513 if( this->m_DataObject == NULL )
515 auto* btn = this->findChild< QPushButton* >( "color_button" );
516 auto* chk = this->findChild< QCheckBox* >( "sv_box" );
517 if( btn == NULL || chk == NULL )
519 QPalette pal = btn->palette( );
520 QColor color = pal.color( QPalette::Button );
522 rgb[ 0 ] = double( color.red( ) ) / double( 255 );
523 rgb[ 1 ] = double( color.green( ) ) / double( 255 );
524 rgb[ 2 ] = double( color.blue( ) ) / double( 255 );
526 auto aIt = this->m_DataObject->BeginVTKActors( );
527 for( ; aIt != this->m_DataObject->EndVTKActors( ); ++aIt )
529 auto ma = dynamic_cast< vtkActor* >( aIt->Actor.GetPointer( ) );
532 if( !( chk->isChecked( ) ) )
534 ma->GetMapper( )->ScalarVisibilityOff( );
535 ma->GetProperty( )->SetColor( rgb );
538 ma->GetMapper( )->ScalarVisibilityOn( );
548 // -------------------------------------------------------------------------
549 void cpPlugins::DataObjectVisualizationQtDialog::
553 if( this->m_DataObject == NULL )
555 auto* btn = this->findChild< QPushButton* >( "color_button" );
556 auto* chk = this->findChild< QCheckBox* >( "sv_box" );
557 if( btn == NULL || chk == NULL )
560 QPalette pal = btn->palette( );
562 QColorDialog::getColor(
563 pal.color( QPalette::Button ),
566 QColorDialog::DontUseNativeDialog
568 if( color.isValid( ) )
570 pal.setColor( QPalette::Button, color );
571 btn->setAutoFillBackground( true );
572 btn->setPalette( pal );
574 this->_scalarVisibility( 0 );
580 #endif // cpPlugins_QT4