1 #include <cpPlugins/ActorPropertiesQtDialog.h>
6 #include <vtkAxesActor.h>
7 #include <vtkImageActor.h>
8 #include <vtkImageData.h>
9 #include <vtkImageProperty.h>
10 #include <vtkMapper.h>
11 #include <vtkProperty.h>
14 #include <QColorDialog>
15 #include <QDoubleSpinBox>
16 #include <QPushButton>
19 // -------------------------------------------------------------------------
20 cpPlugins::ActorPropertiesQtDialog::
21 ActorPropertiesQtDialog( QWidget* parent, Qt::WindowFlags f )
22 : 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::ActorPropertiesQtDialog::
35 ~ActorPropertiesQtDialog( )
37 this->m_Actors.clear( );
38 this->m_Windows.clear( );
40 delete this->m_ToolsLayout;
41 delete this->m_MainLayout;
44 // -------------------------------------------------------------------------
45 bool cpPlugins::ActorPropertiesQtDialog::
46 addActor( vtkProp* obj )
51 if( this->m_Actors.size( ) > 0 )
53 bool s = this->_addActor< vtkAxesActor >( obj );
54 if( !s ) s = this->_addActor< vtkImageActor >( obj );
55 if( !s ) s = this->_addActor< vtkActor >( obj );
60 this->m_Actors.insert( obj );
61 this->m_WidgetsUpdated = false;
67 // -------------------------------------------------------------------------
68 bool cpPlugins::ActorPropertiesQtDialog::
69 addRenderWindow( vtkRenderWindow* win )
73 this->m_Windows.insert( win );
74 this->m_WidgetsUpdated = false;
78 // -------------------------------------------------------------------------
79 int cpPlugins::ActorPropertiesQtDialog::
82 this->_updateWidgets( );
83 int ret = this->QDialog::exec( );
86 this->updateParameters( );
93 // -------------------------------------------------------------------------
94 void cpPlugins::ActorPropertiesQtDialog::
98 this->m_Buttons = new QDialogButtonBox( QDialogButtonBox::Ok );
100 this->m_Buttons, SIGNAL( accepted( ) ), this, SLOT( accept( ) )
103 this->m_Buttons, SIGNAL( rejected( ) ), this, SLOT( reject( ) )
105 this->m_ToolsLayout->addWidget( this->m_Buttons );
106 this->m_WidgetsUpdated = true;
109 // -------------------------------------------------------------------------
110 void cpPlugins::ActorPropertiesQtDialog::
113 if( this->m_WidgetsUpdated || this->m_Actors.size( ) == 0 )
115 bool s = this->_configureForAxes( );
116 if( !s ) s = this->_configureForImage( );
117 if( !s ) s = this->_configureForMesh( );
118 this->_addButtons( );
121 // -------------------------------------------------------------------------
122 bool cpPlugins::ActorPropertiesQtDialog::
125 if( this->m_Actors.size( ) == 0 )
128 dynamic_cast< vtkAxesActor* >( this->m_Actors.begin( )->GetPointer( ) );
133 std::stringstream title;
134 title << "Parameters for an object of class \"Axes\"";
135 this->m_Title->setText( title.str( ).c_str( ) );
140 // -------------------------------------------------------------------------
141 bool cpPlugins::ActorPropertiesQtDialog::
142 _configureForImage( )
144 if( this->m_Actors.size( ) == 0 )
147 dynamic_cast< vtkImageActor* >( this->m_Actors.begin( )->GetPointer( ) );
150 auto image = actor->GetInput( );
155 auto prop = actor->GetProperty( );
157 image->GetScalarRange( r );
158 double w = actor->GetProperty( )->GetColorWindow( );
159 double l = actor->GetProperty( )->GetColorLevel( );
160 double sw = double( 1000 ) * w / ( r[ 1 ] - r[ 0 ] );
161 double sl = double( 1000 ) * ( l - r[ 0 ] ) / ( r[ 1 ] - r[ 0 ] );
162 double op = double( 10 ) * prop->GetOpacity( );
165 std::stringstream title;
166 title << "Parameters for an object of class \"Image\"";
167 this->m_Title->setText( title.str( ).c_str( ) );
170 QDoubleSpinBox* win_box = new QDoubleSpinBox( this );
171 win_box->setObjectName( "win_box" );
172 win_box->setDecimals( 3 );
173 win_box->setMinimum( 0 );
174 win_box->setMaximum( r[ 1 ] - r[ 0 ] );
175 win_box->setValue( w );
177 win_box, SIGNAL( valueChanged( double ) ),
178 this, SLOT( _boxWindow( double ) )
181 QSlider* win_sld = new QSlider( Qt::Horizontal, this );
182 win_sld->setObjectName( "win_sld" );
183 win_sld->setRange( 0, 1000 );
184 win_sld->setValue( ( unsigned int )( sw ) );
186 win_sld, SIGNAL( valueChanged( int ) ),
187 this, SLOT( _sldWindow( int ) )
190 QHBoxLayout* win_layout = new QHBoxLayout( );
191 QLabel* win_label = new QLabel( this );
192 win_label->setText( QString( "Window: " ) );
193 win_layout->addWidget( win_label );
194 win_layout->addWidget( win_box );
195 win_layout->addWidget( win_sld );
196 this->m_ToolsLayout->addLayout( win_layout );
198 QDoubleSpinBox* lev_box = new QDoubleSpinBox( this );
199 lev_box->setObjectName( "lev_box" );
200 lev_box->setDecimals( 3 );
201 lev_box->setMinimum( r[ 0 ] );
202 lev_box->setMaximum( r[ 1 ] );
203 lev_box->setValue( l );
205 lev_box, SIGNAL( valueChanged( double ) ),
206 this, SLOT( _boxLevel( double ) )
209 QSlider* lev_sld = new QSlider( Qt::Horizontal, this );
210 lev_sld->setObjectName( "lev_sld" );
211 lev_sld->setRange( 0, 1000 );
212 lev_sld->setValue( ( unsigned int )( sl ) );
214 lev_sld, SIGNAL( valueChanged( int ) ),
215 this, SLOT( _sldLevel( int ) )
218 QHBoxLayout* lev_layout = new QHBoxLayout( );
219 QLabel* lev_label = new QLabel( this );
220 lev_label->setText( QString( "Level: " ) );
221 lev_layout->addWidget( lev_label );
222 lev_layout->addWidget( lev_box );
223 lev_layout->addWidget( lev_sld );
224 this->m_ToolsLayout->addLayout( lev_layout );
226 // Configure generic objects
227 QSlider* op_sld = new QSlider( Qt::Horizontal, this );
228 op_sld->setObjectName( "op_sld" );
229 op_sld->setRange( 0, 10 );
230 op_sld->setValue( ( unsigned int )( op ) );
232 op_sld, SIGNAL( valueChanged( int ) ),
233 this, SLOT( _sldOpacity( int ) )
236 QHBoxLayout* op_layout = new QHBoxLayout( );
237 QLabel* op_label = new QLabel( this );
238 op_label->setText( QString( "Opacity: " ) );
239 op_layout->addWidget( op_label );
240 op_layout->addWidget( op_sld );
241 this->m_ToolsLayout->addLayout( op_layout );
246 // -------------------------------------------------------------------------
247 bool cpPlugins::ActorPropertiesQtDialog::
250 if( this->m_Actors.size( ) == 0 )
253 dynamic_cast< vtkActor* >( this->m_Actors.begin( )->GetPointer( ) );
256 auto prop = actor->GetProperty( );
259 std::stringstream title;
260 title << "Parameters for an object of class \"Mesh\"";
261 this->m_Title->setText( title.str( ).c_str( ) );
264 QSpinBox* ps_box = new QSpinBox( this );
265 ps_box->setObjectName( "ps_box" );
266 ps_box->setMinimum( 1 );
267 ps_box->setMaximum( 100 );
268 ps_box->setValue( prop->GetPointSize( ) );
270 ps_box, SIGNAL( valueChanged( int ) ),
271 this, SLOT( _boxPointSize( int ) )
274 QHBoxLayout* ps_layout = new QHBoxLayout( );
275 QLabel* ps_label = new QLabel( this );
276 ps_label->setText( QString( "Point size: " ) );
277 ps_layout->addWidget( ps_label );
278 ps_layout->addWidget( ps_box );
279 this->m_ToolsLayout->addLayout( ps_layout );
281 QSpinBox* lw_box = new QSpinBox( this );
282 lw_box->setObjectName( "lw_box" );
283 lw_box->setMinimum( 1 );
284 lw_box->setMaximum( 100 );
285 lw_box->setValue( prop->GetLineWidth( ) );
287 lw_box, SIGNAL( valueChanged( int ) ),
288 this, SLOT( _boxLineWidth( int ) )
291 QHBoxLayout* lw_layout = new QHBoxLayout( );
292 QLabel* lw_label = new QLabel( this );
293 lw_label->setText( QString( "Line width: " ) );
294 lw_layout->addWidget( lw_label );
295 lw_layout->addWidget( lw_box );
296 this->m_ToolsLayout->addLayout( lw_layout );
298 QCheckBox* sv_box = new QCheckBox( this );
299 sv_box->setObjectName( "sv_box" );
300 sv_box->setText( "Scalar visibility: " );
301 sv_box->setChecked( ( actor->GetMapper( )->GetScalarVisibility( ) == 1 ) );
303 sv_box, SIGNAL( stateChanged( int ) ),
304 this, SLOT( _scalarVisibility( int ) )
307 QHBoxLayout* sv_layout = new QHBoxLayout( );
308 sv_layout->addWidget( sv_box );
309 this->m_ToolsLayout->addLayout( sv_layout );
312 prop->GetColor( cr, cg, cb );
317 QPushButton* color_button = new QPushButton( "Color", this );
318 color_button->setObjectName( "color_button" );
319 QPalette color_palette = color_button->palette( );
320 color_palette.setColor( QPalette::Button, QColor( cr, cg, cb ) );
321 color_button->setAutoFillBackground( true );
322 color_button->setPalette( color_palette );
323 color_button->update( );
324 color_button->connect(
325 color_button, SIGNAL( clicked( ) ),
326 this, SLOT( _color( ) )
329 QHBoxLayout* color_layout = new QHBoxLayout( );
330 color_layout->addWidget( color_button );
331 this->m_ToolsLayout->addLayout( color_layout );
333 // Configure generic objects
334 QSlider* op_sld = new QSlider( Qt::Horizontal, this );
335 op_sld->setObjectName( "op_sld" );
336 op_sld->setRange( 0, 10 );
338 ( unsigned int )( prop->GetOpacity( ) * double( 10 ) )
341 op_sld, SIGNAL( valueChanged( int ) ),
342 this, SLOT( _sldOpacity( int ) )
345 QHBoxLayout* op_layout = new QHBoxLayout( );
346 QLabel* op_label = new QLabel( this );
347 op_label->setText( QString( "Opacity: " ) );
348 op_layout->addWidget( op_label );
349 op_layout->addWidget( op_sld );
350 this->m_ToolsLayout->addLayout( op_layout );
355 // -------------------------------------------------------------------------
356 void cpPlugins::ActorPropertiesQtDialog::
357 _setWindow( double w )
359 if( this->m_Actors.size( ) == 0 )
361 auto aIt = this->m_Actors.begin( );
362 for( ; aIt != this->m_Actors.end( ); ++aIt )
364 auto actor = dynamic_cast< vtkImageActor* >( aIt->GetPointer( ) );
367 actor->GetProperty( )->SetColorWindow( w );
376 // -------------------------------------------------------------------------
377 void cpPlugins::ActorPropertiesQtDialog::
378 _setLevel( double l )
380 if( this->m_Actors.size( ) == 0 )
382 auto aIt = this->m_Actors.begin( );
383 for( ; aIt != this->m_Actors.end( ); ++aIt )
385 auto actor = dynamic_cast< vtkImageActor* >( aIt->GetPointer( ) );
388 actor->GetProperty( )->SetColorLevel( l );
397 // -------------------------------------------------------------------------
398 void cpPlugins::ActorPropertiesQtDialog::
401 for( auto i = this->m_Windows.begin( ); i != this->m_Windows.end( ); ++i )
405 // -------------------------------------------------------------------------
406 template< class _TActor >
407 bool cpPlugins::ActorPropertiesQtDialog::
408 _addActor( vtkProp* obj )
410 auto new_obj = dynamic_cast< _TActor* >( obj );
412 dynamic_cast< _TActor* >( this->m_Actors.begin( )->GetPointer( ) );
413 if( new_obj != NULL && pre_obj != NULL )
415 this->m_Actors.insert( obj );
416 this->m_WidgetsUpdated = false;
423 // -------------------------------------------------------------------------
424 void cpPlugins::ActorPropertiesQtDialog::
425 _boxWindow( double v )
427 auto* box = this->findChild< QDoubleSpinBox* >( "win_box" );
428 auto* sld = this->findChild< QSlider* >( "win_sld" );
429 if( box == NULL || sld == NULL )
432 double min = double( sld->minimum( ) );
433 double max = double( sld->maximum( ) );
434 double vmin = box->minimum( );
435 double vmax = box->maximum( );
436 double s = ( v - vmin ) / ( vmax - vmin );
437 s = ( ( max - min ) * s ) + min;
439 bool o = sld->blockSignals( true );
440 sld->setValue( ( unsigned int )( s ) );
441 sld->blockSignals( o );
442 this->_setWindow( v );
445 // -------------------------------------------------------------------------
446 void cpPlugins::ActorPropertiesQtDialog::
449 auto* box = this->findChild< QDoubleSpinBox* >( "win_box" );
450 auto* sld = this->findChild< QSlider* >( "win_sld" );
451 if( box == NULL || sld == NULL )
454 double min = double( sld->minimum( ) );
455 double max = double( sld->maximum( ) );
456 double vmin = box->minimum( );
457 double vmax = box->maximum( );
458 double s = ( double( v ) - min ) / ( max - min );
459 s = ( ( vmax - vmin ) * s ) + vmin;
461 bool o = box->blockSignals( true );
463 box->blockSignals( o );
464 this->_setWindow( s );
467 // -------------------------------------------------------------------------
468 void cpPlugins::ActorPropertiesQtDialog::
469 _boxLevel( double v )
471 auto* box = this->findChild< QDoubleSpinBox* >( "lev_box" );
472 auto* sld = this->findChild< QSlider* >( "lev_sld" );
473 if( box == NULL || sld == NULL )
476 double min = double( sld->minimum( ) );
477 double max = double( sld->maximum( ) );
478 double vmin = box->minimum( );
479 double vmax = box->maximum( );
480 double s = ( v - vmin ) / ( vmax - vmin );
481 s = ( ( max - min ) * s ) + min;
483 bool o = sld->blockSignals( true );
484 sld->setValue( ( unsigned int )( s ) );
485 sld->blockSignals( o );
486 this->_setLevel( v );
489 // -------------------------------------------------------------------------
490 void cpPlugins::ActorPropertiesQtDialog::
493 auto* box = this->findChild< QDoubleSpinBox* >( "lev_box" );
494 auto* sld = this->findChild< QSlider* >( "lev_sld" );
495 if( box == NULL || sld == NULL )
498 double min = double( sld->minimum( ) );
499 double max = double( sld->maximum( ) );
500 double vmin = box->minimum( );
501 double vmax = box->maximum( );
502 double s = ( double( v ) - min ) / ( max - min );
503 s = ( ( vmax - vmin ) * s ) + vmin;
505 bool o = box->blockSignals( true );
507 box->blockSignals( o );
508 this->_setLevel( s );
511 // -------------------------------------------------------------------------
512 void cpPlugins::ActorPropertiesQtDialog::
515 if( this->m_Actors.size( ) == 0 )
517 auto* sld = this->findChild< QSlider* >( "op_sld" );
521 double min = double( sld->minimum( ) );
522 double max = double( sld->maximum( ) );
523 double s = ( double( v ) - min ) / ( max - min );
525 auto aIt = this->m_Actors.begin( );
526 for( ; aIt != this->m_Actors.end( ); ++aIt )
528 auto ia = dynamic_cast< vtkImageActor* >( aIt->GetPointer( ) );
529 auto ma = dynamic_cast< vtkActor* >( aIt->GetPointer( ) );
532 ia->GetProperty( )->SetOpacity( s );
535 else if( ma != NULL )
537 ma->GetProperty( )->SetOpacity( s );
546 // -------------------------------------------------------------------------
547 void cpPlugins::ActorPropertiesQtDialog::
548 _boxPointSize( int v )
550 if( this->m_Actors.size( ) == 0 )
552 auto aIt = this->m_Actors.begin( );
553 for( ; aIt != this->m_Actors.end( ); ++aIt )
555 auto ma = dynamic_cast< vtkActor* >( aIt->GetPointer( ) );
558 ma->GetProperty( )->SetPointSize( v );
567 // -------------------------------------------------------------------------
568 void cpPlugins::ActorPropertiesQtDialog::
569 _boxLineWidth( int v )
571 if( this->m_Actors.size( ) == 0 )
573 auto aIt = this->m_Actors.begin( );
574 for( ; aIt != this->m_Actors.end( ); ++aIt )
576 auto ma = dynamic_cast< vtkActor* >( aIt->GetPointer( ) );
579 ma->GetProperty( )->SetLineWidth( v );
588 // -------------------------------------------------------------------------
589 void cpPlugins::ActorPropertiesQtDialog::
590 _scalarVisibility( int v )
592 if( this->m_Actors.size( ) == 0 )
594 auto* btn = this->findChild< QPushButton* >( "color_button" );
595 auto* chk = this->findChild< QCheckBox* >( "sv_box" );
596 if( btn == NULL || chk == NULL )
598 QPalette pal = btn->palette( );
599 QColor color = pal.color( QPalette::Button );
601 rgb[ 0 ] = double( color.red( ) ) / double( 255 );
602 rgb[ 1 ] = double( color.green( ) ) / double( 255 );
603 rgb[ 2 ] = double( color.blue( ) ) / double( 255 );
605 auto aIt = this->m_Actors.begin( );
606 for( ; aIt != this->m_Actors.end( ); ++aIt )
608 auto ma = dynamic_cast< vtkActor* >( aIt->GetPointer( ) );
611 if( !( chk->isChecked( ) ) )
613 ma->GetMapper( )->ScalarVisibilityOff( );
614 ma->GetProperty( )->SetColor( rgb );
617 ma->GetMapper( )->ScalarVisibilityOn( );
626 // -------------------------------------------------------------------------
627 void cpPlugins::ActorPropertiesQtDialog::
630 if( this->m_Actors.size( ) == 0 )
632 auto* btn = this->findChild< QPushButton* >( "color_button" );
633 auto* chk = this->findChild< QCheckBox* >( "sv_box" );
634 if( btn == NULL || chk == NULL )
637 QPalette pal = btn->palette( );
639 QColorDialog::getColor(
640 pal.color( QPalette::Button ),
643 QColorDialog::DontUseNativeDialog
645 if( color.isValid( ) )
647 pal.setColor( QPalette::Button, color );
648 btn->setAutoFillBackground( true );
649 btn->setPalette( pal );
651 this->_scalarVisibility( 0 );
656 #endif // cpPlugins_QT4