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( )
119 auto image = this->m_DataObject->GetVTK< vtkImageData >( );
120 auto aIt = this->m_DataObject->BeginVTKActors( );
121 if( image == NULL || aIt == this->m_DataObject->EndVTKActors( ) )
123 auto actor = dynamic_cast< vtkImageActor* >( aIt->Actor.GetPointer( ) );
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( );
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 );
142 win_box, SIGNAL( valueChanged( double ) ),
143 this, SLOT( _boxWindow( double ) )
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 ) );
151 win_sld, SIGNAL( valueChanged( int ) ),
152 this, SLOT( _sldWindow( int ) )
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 );
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 );
170 lev_box, SIGNAL( valueChanged( double ) ),
171 this, SLOT( _boxLevel( double ) )
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 ) );
179 lev_sld, SIGNAL( valueChanged( int ) ),
180 this, SLOT( _sldLevel( int ) )
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 );
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 ) );
197 op_sld, SIGNAL( valueChanged( int ) ),
198 this, SLOT( _sldOpacity( int ) )
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 );
209 // -------------------------------------------------------------------------
210 void cpPlugins::DataObjectVisualizationQtDialog::
213 auto mesh = this->m_DataObject->GetVTK< vtkPolyData >( );
214 auto aIt = this->m_DataObject->BeginVTKActors( );
215 if( mesh == NULL || aIt == this->m_DataObject->EndVTKActors( ) )
217 auto actor = dynamic_cast< vtkActor* >( aIt->Actor.GetPointer( ) );
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( ) );
227 ps_box, SIGNAL( valueChanged( int ) ),
228 this, SLOT( _boxPointSize( int ) )
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 );
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( ) );
244 lw_box, SIGNAL( valueChanged( int ) ),
245 this, SLOT( _boxLineWidth( int ) )
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 );
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 ) );
260 sv_box, SIGNAL( stateChanged( int ) ),
261 this, SLOT( _scalarVisibility( int ) )
264 QHBoxLayout* sv_layout = new QHBoxLayout( );
265 sv_layout->addWidget( sv_box );
266 this->m_ToolsLayout->addLayout( sv_layout );
269 actor->GetProperty( )->GetColor( cr, cg, cb );
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( ) )
286 QHBoxLayout* color_layout = new QHBoxLayout( );
287 color_layout->addWidget( color_button );
288 this->m_ToolsLayout->addLayout( color_layout );
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 );
295 ( unsigned int )( actor->GetProperty( )->GetOpacity( ) * double( 10 ) )
298 op_sld, SIGNAL( valueChanged( int ) ),
299 this, SLOT( _sldOpacity( int ) )
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 );
310 // -------------------------------------------------------------------------
311 void cpPlugins::DataObjectVisualizationQtDialog::
312 _setWindow( double w )
314 if( this->m_DataObject == NULL )
316 auto aIt = this->m_DataObject->BeginVTKActors( );
317 for( ; aIt != this->m_DataObject->EndVTKActors( ); ++aIt )
319 auto actor = dynamic_cast< vtkImageActor* >( aIt->Actor.GetPointer( ) );
322 actor->GetProperty( )->SetColorWindow( w );
331 // -------------------------------------------------------------------------
332 void cpPlugins::DataObjectVisualizationQtDialog::
333 _setLevel( double l )
335 if( this->m_DataObject == NULL )
337 auto aIt = this->m_DataObject->BeginVTKActors( );
338 for( ; aIt != this->m_DataObject->EndVTKActors( ); ++aIt )
340 auto actor = dynamic_cast< vtkImageActor* >( aIt->Actor.GetPointer( ) );
343 actor->GetProperty( )->SetColorLevel( l );
352 // -------------------------------------------------------------------------
353 void cpPlugins::DataObjectVisualizationQtDialog::
356 if( this->m_DataObject == NULL )
358 this->m_DataObject->RenderVTKActors( );
361 // -------------------------------------------------------------------------
362 void cpPlugins::DataObjectVisualizationQtDialog::
363 _boxWindow( double v )
365 auto* box = this->findChild< QDoubleSpinBox* >( "win_box" );
366 auto* sld = this->findChild< QSlider* >( "win_sld" );
367 if( box == NULL || sld == NULL )
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;
377 bool o = sld->blockSignals( true );
378 sld->setValue( ( unsigned int )( s ) );
379 sld->blockSignals( o );
380 this->_setWindow( v );
383 // -------------------------------------------------------------------------
384 void cpPlugins::DataObjectVisualizationQtDialog::
387 auto* box = this->findChild< QDoubleSpinBox* >( "win_box" );
388 auto* sld = this->findChild< QSlider* >( "win_sld" );
389 if( box == NULL || sld == NULL )
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;
399 bool o = box->blockSignals( true );
401 box->blockSignals( o );
402 this->_setWindow( s );
405 // -------------------------------------------------------------------------
406 void cpPlugins::DataObjectVisualizationQtDialog::
407 _boxLevel( double v )
409 auto* box = this->findChild< QDoubleSpinBox* >( "lev_box" );
410 auto* sld = this->findChild< QSlider* >( "lev_sld" );
411 if( box == NULL || sld == NULL )
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;
421 bool o = sld->blockSignals( true );
422 sld->setValue( ( unsigned int )( s ) );
423 sld->blockSignals( o );
424 this->_setLevel( v );
427 // -------------------------------------------------------------------------
428 void cpPlugins::DataObjectVisualizationQtDialog::
431 auto* box = this->findChild< QDoubleSpinBox* >( "lev_box" );
432 auto* sld = this->findChild< QSlider* >( "lev_sld" );
433 if( box == NULL || sld == NULL )
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;
443 bool o = box->blockSignals( true );
445 box->blockSignals( o );
446 this->_setLevel( s );
449 // -------------------------------------------------------------------------
450 void cpPlugins::DataObjectVisualizationQtDialog::
453 if( this->m_DataObject == NULL )
455 auto* sld = this->findChild< QSlider* >( "op_sld" );
459 double min = double( sld->minimum( ) );
460 double max = double( sld->maximum( ) );
461 double s = ( double( v ) - min ) / ( max - min );
463 auto aIt = this->m_DataObject->BeginVTKActors( );
464 for( ; aIt != this->m_DataObject->EndVTKActors( ); ++aIt )
466 auto ia = dynamic_cast< vtkImageActor* >( aIt->Actor.GetPointer( ) );
467 auto ma = dynamic_cast< vtkActor* >( aIt->Actor.GetPointer( ) );
470 ia->GetProperty( )->SetOpacity( s );
473 else if( ma != NULL )
475 ma->GetProperty( )->SetOpacity( s );
484 // -------------------------------------------------------------------------
485 void cpPlugins::DataObjectVisualizationQtDialog::
486 _boxPointSize( int v )
490 // -------------------------------------------------------------------------
491 void cpPlugins::DataObjectVisualizationQtDialog::
492 _boxLineWidth( int v )
496 // -------------------------------------------------------------------------
497 void cpPlugins::DataObjectVisualizationQtDialog::
498 _scalarVisibility( int v )
500 if( this->m_DataObject == NULL )
502 auto* btn = this->findChild< QPushButton* >( "color_button" );
503 auto* chk = this->findChild< QCheckBox* >( "sv_box" );
504 if( btn == NULL || chk == NULL )
506 QPalette pal = btn->palette( );
507 QColor color = pal.color( QPalette::Button );
509 rgb[ 0 ] = double( color.red( ) ) / double( 255 );
510 rgb[ 1 ] = double( color.green( ) ) / double( 255 );
511 rgb[ 2 ] = double( color.blue( ) ) / double( 255 );
513 auto aIt = this->m_DataObject->BeginVTKActors( );
514 for( ; aIt != this->m_DataObject->EndVTKActors( ); ++aIt )
516 auto ma = dynamic_cast< vtkActor* >( aIt->Actor.GetPointer( ) );
519 if( !( chk->isChecked( ) ) )
521 ma->GetMapper( )->ScalarVisibilityOff( );
522 ma->GetProperty( )->SetColor( rgb );
525 ma->GetMapper( )->ScalarVisibilityOn( );
534 // -------------------------------------------------------------------------
535 void cpPlugins::DataObjectVisualizationQtDialog::
538 if( this->m_DataObject == NULL )
540 auto* btn = this->findChild< QPushButton* >( "color_button" );
541 auto* chk = this->findChild< QCheckBox* >( "sv_box" );
542 if( btn == NULL || chk == NULL )
545 QPalette pal = btn->palette( );
547 QColorDialog::getColor(
548 pal.color( QPalette::Button ),
551 QColorDialog::DontUseNativeDialog
553 if( color.isValid( ) )
555 pal.setColor( QPalette::Button, color );
556 btn->setAutoFillBackground( true );
557 btn->setPalette( pal );
559 this->_scalarVisibility( 0 );
564 #endif // cpPlugins_QT4