1 #include <cpPlugins/Interface/ParametersQtDialog.h>
3 #ifdef cpPlugins_Interface_QT4
7 #include <cpPlugins/Interface/ParametersListWidget.h>
9 #include <vtkCommand.h>
10 #include <vtkRenderWindowInteractor.h>
13 #include <QDialogButtonBox>
14 #include <QDoubleSpinBox>
15 #include <QHBoxLayout>
19 // -------------------------------------------------------------------------
20 class SingleSeedCommand
24 static SingleSeedCommand* New( )
25 { return( new SingleSeedCommand ); }
26 virtual void Execute( vtkObject* caller, unsigned long eid, void* data )
28 if( eid != vtkCommand::PlacePointEvent )
34 // -------------------------------------------------------------------------
35 cpPlugins::Interface::ParametersQtDialog::
36 ParametersQtDialog( QWidget* parent, Qt::WindowFlags f )
37 : QDialog( parent, f ),
41 this->m_Title = new QLabel( this );
42 this->m_Title->setText( "Parameters dialog title" );
44 this->m_MainLayout = new QGridLayout( this );
45 this->m_ToolsLayout = new QVBoxLayout( );
46 this->m_ToolsLayout->addWidget( this->m_Title );
47 this->m_MainLayout->addLayout( this->m_ToolsLayout, 0, 0, 1, 1 );
50 // -------------------------------------------------------------------------
51 cpPlugins::Interface::ParametersQtDialog::
52 ~ParametersQtDialog( )
55 delete this->m_ToolsLayout;
56 delete this->m_MainLayout;
59 // -------------------------------------------------------------------------
60 bool cpPlugins::Interface::ParametersQtDialog::
63 return( this->m_IsModal );
66 // -------------------------------------------------------------------------
67 cpPlugins::Interface::
68 Parameters* cpPlugins::Interface::ParametersQtDialog::
69 getParameters( ) const
71 return( this->m_Parameters );
74 // -------------------------------------------------------------------------
75 void cpPlugins::Interface::ParametersQtDialog::
76 addInteractor( vtkRenderWindowInteractor* interactor )
78 this->m_Interactors.insert( interactor );
81 // -------------------------------------------------------------------------
82 bool cpPlugins::Interface::ParametersQtDialog::
83 setParameters( Parameters* parameters )
85 this->m_IsModal = true;
86 this->m_Parameters = parameters;
87 if( this->m_Parameters == NULL )
91 std::vector< std::string > names;
92 this->m_Parameters->GetNames( names );
93 std::vector< std::string >::const_iterator nIt = names.begin( );
94 for( ; nIt != names.end( ); ++nIt )
96 Parameters::Type pt = this->m_Parameters->GetType( *nIt );
98 QWidget* w_input = NULL;
99 if( pt == Parameters::String )
101 QLineEdit* v_string = new QLineEdit( this );
102 v_string->setText( "Enter some text!!!" );
105 else if( pt == Parameters::Bool )
107 QCheckBox* v_bool = new QCheckBox( this );
108 v_bool->setText( "[ON/OFF]" );
109 v_bool->setChecked( this->m_Parameters->GetBool( *nIt ) );
112 else if( pt == Parameters::Uint )
114 QSpinBox* v_uint = new QSpinBox( this );
115 v_uint->setMinimum( 0 );
116 v_uint->setMaximum( std::numeric_limits< int >::max( ) );
117 v_uint->setValue( this->m_Parameters->GetUint( *nIt ) );
120 else if( pt == Parameters::Int )
122 QSpinBox* v_int = new QSpinBox( this );
123 v_int->setMinimum( -std::numeric_limits< int >::max( ) );
124 v_int->setMaximum( std::numeric_limits< int >::max( ) );
125 v_int->setValue( this->m_Parameters->GetInt( *nIt ) );
128 else if( pt == Parameters::Real )
130 QDoubleSpinBox* v_double = new QDoubleSpinBox( this );
131 v_double->setDecimals( 3 );
132 v_double->setMinimum( -std::numeric_limits< double >::max( ) );
133 v_double->setMaximum( std::numeric_limits< double >::max( ) );
134 v_double->setValue( this->m_Parameters->GetReal( *nIt ) );
138 pt == Parameters::StringList ||
139 pt == Parameters::IntList ||
140 pt == Parameters::UintList ||
141 pt == Parameters::RealList
144 cpPlugins::Interface::ParametersListWidget* l_double =
145 new cpPlugins::Interface::ParametersListWidget( *nIt, this );
148 else if( pt == Parameters::Point || pt == Parameters::Index )
150 vtkSmartPointer< SingleSeedCommand > command =
151 vtkSmartPointer< SingleSeedCommand >::New( );
153 auto iIt = this->m_Interactors.begin( );
154 for( ; iIt != this->m_Interactors.end( ); ++iIt )
157 dynamic_cast< TStyle* >( ( *iIt )->GetInteractorStyle( ) );
160 style->SeedWidgetOn( );
161 style->SetSeedWidgetCommand( command );
166 this->m_IsModal = false;
170 // Ok, a representation was created
171 if( w_input != NULL )
173 w_input->setObjectName( QString( nIt->c_str( ) ) );
175 QHBoxLayout* new_layout = new QHBoxLayout( );
176 QLabel* label = new QLabel( this );
177 label->setText( QString( nIt->c_str( ) ) );
178 new_layout->addWidget( label );
179 new_layout->addWidget( w_input );
180 this->m_ToolsLayout->addLayout( new_layout );
185 return( this->m_IsModal );
188 // -------------------------------------------------------------------------
189 void cpPlugins::Interface::ParametersQtDialog::
190 setTitle( const std::string& title )
192 this->m_Title->setText( title.c_str( ) );
195 // -------------------------------------------------------------------------
196 int cpPlugins::Interface::ParametersQtDialog::
199 if( !this->m_IsModal )
203 QDialogButtonBox* bb = new QDialogButtonBox(
204 QDialogButtonBox::Ok | QDialogButtonBox::Cancel
206 QObject::connect( bb, SIGNAL( accepted( ) ), this, SLOT( accept( ) ) );
207 QObject::connect( bb, SIGNAL( rejected( ) ), this, SLOT( reject( ) ) );
208 this->m_ToolsLayout->addWidget( bb );
210 int ret = this->QDialog::exec( );
212 this->syncParameters( );
216 // -------------------------------------------------------------------------
217 void cpPlugins::Interface::ParametersQtDialog::
220 if( this->m_IsModal )
223 this->QDialog::show( );
226 // -------------------------------------------------------------------------
227 void cpPlugins::Interface::ParametersQtDialog::
230 if( this->m_Parameters == NULL )
234 std::vector< std::string > names;
235 this->m_Parameters->GetNames( names );
236 std::vector< std::string >::const_iterator nIt = names.begin( );
237 for( ; nIt != names.end( ); ++nIt )
239 Parameters::Type pt = this->m_Parameters->GetType( *nIt );
241 if( pt == Parameters::String )
243 QLineEdit* v = this->findChild< QLineEdit* >( nIt->c_str( ) );
245 this->m_Parameters->SetString( *nIt, v->text( ).toStdString( ) );
247 else if( pt == Parameters::Bool )
249 QCheckBox* v = this->findChild< QCheckBox* >( nIt->c_str( ) );
251 this->m_Parameters->SetBool( *nIt, v->isChecked( ) );
253 else if( pt == Parameters::Uint )
255 QSpinBox* v = this->findChild< QSpinBox* >( nIt->c_str( ) );
257 this->m_Parameters->SetUint( *nIt, v->value( ) );
259 else if( pt == Parameters::Int )
261 QSpinBox* v = this->findChild< QSpinBox* >( nIt->c_str( ) );
263 this->m_Parameters->SetInt( *nIt, v->value( ) );
265 else if( pt == Parameters::Real )
267 QDoubleSpinBox* v = this->findChild< QDoubleSpinBox* >( nIt->c_str( ) );
269 this->m_Parameters->SetReal( *nIt, v->value( ) );
272 pt == Parameters::StringList ||
273 pt == Parameters::IntList ||
274 pt == Parameters::UintList ||
275 pt == Parameters::RealList
278 cpPlugins::Interface::ParametersListWidget* lst =
279 this->findChild< cpPlugins::Interface::ParametersListWidget* >(
284 if( pt == Parameters::StringList )
286 this->m_Parameters->ClearStringList( *nIt );
287 std::vector< std::string > values = lst->GetStringValues( );
288 for( int r = 0; r < values.size( ); ++r )
289 this->m_Parameters->AddToStringList( *nIt, values[ r ] );
291 else if( pt == Parameters::IntList )
293 this->m_Parameters->ClearIntList( *nIt );
294 std::vector< int > values = lst->GetIntValues( );
295 for( int r = 0; r < values.size( ); ++r )
296 this->m_Parameters->AddToIntList( *nIt, values[ r ] );
298 else if( pt == Parameters::UintList )
300 this->m_Parameters->ClearUintList( *nIt );
301 std::vector< unsigned int > values = lst->GetUintValues( );
302 for( int r = 0; r < values.size( ); ++r )
303 this->m_Parameters->AddToUintList( *nIt, values[ r ] );
305 else if( pt == Parameters::RealList )
307 this->m_Parameters->ClearRealList( *nIt );
308 std::vector< double > values = lst->GetDoubleValues( );
309 for( int r = 0; r < values.size( ); ++r )
310 this->m_Parameters->AddToRealList( *nIt, values[ r ] );
316 else if( pt == Parameters::Point || pt == Parameters::Index )
349 pt == Parameters::StringList ||
350 pt == Parameters::IntList ||
351 pt == Parameters::UintList ||
352 pt == Parameters::RealList
355 cpPlugins::Interface::ParametersListWidget* lst =
356 this->findChild< cpPlugins::Interface::ParametersListWidget* >(
361 if( pt == Parameters::StringList )
363 std::vector< std::string > values = lst->GetStringValues( );
364 for( int r = 0; r < values.size( ); ++r )
365 this->m_Parameters->AddToStringList( *nIt, values[ r ] );
367 else if( pt == Parameters::IntList )
369 std::vector< int > values = lst->GetIntValues( );
370 for( int r = 0; r < values.size( ); ++r )
371 this->m_Parameters->AddToIntList( *nIt, values[ r ] );
373 else if( pt == Parameters::UintList )
375 std::vector< unsigned int > values = lst->GetUintValues( );
376 for( int r = 0; r < values.size( ); ++r )
377 this->m_Parameters->AddToUintList( *nIt, values[ r ] );
379 else if( pt == Parameters::RealList )
381 std::vector< double > values = lst->GetDoubleValues( );
382 for( int r = 0; r < values.size( ); ++r )
383 this->m_Parameters->AddToRealList( *nIt, values[ r ] );
396 #endif // cpPlugins_Interface_QT4