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>
17 #include <QPushButton>
20 // -------------------------------------------------------------------------
21 #include <cpPlugins/Interface/ProcessObject.h>
22 #include <cpPlugins/Interface/Plugins.h>
23 #include <cpPlugins/Interface/BaseApplication.h>
25 class SingleSeedCommand
29 static SingleSeedCommand* New( )
30 { return( new SingleSeedCommand ); }
31 virtual void Execute( vtkObject* caller, unsigned long eid, void* data )
33 // Get seed, avoiding segfaults!!!
34 if( eid != vtkCommand::PlacePointEvent || this->Dialog == NULL )
36 vtkSeedWidget* widget = dynamic_cast< vtkSeedWidget* >( caller );
39 vtkSeedRepresentation* rep = widget->GetSeedRepresentation( );
42 if( rep->GetNumberOfSeeds( ) == 0 )
45 rep->GetSeedWorldPosition( 0, seed );
47 // Delete all seeds (remember that this command is just for one seed)
48 while( rep->GetNumberOfSeeds( ) > 0 )
49 widget->DeleteSeed( 0 );
51 if( this->Dialog->getParameters( )->HasIndex( this->Name ) )
54 else if( this->Dialog->getParameters( )->HasPoint( this->Name ) )
56 this->Dialog->getParameters( )->SetPoint( this->Name, 3, seed );
57 this->Dialog->syncParameters( );
58 auto filter = this->Dialog->getParameters( )->GetProcessObject( );
61 auto plugins = filter->GetPlugins( );
64 auto app = plugins->GetApplication( );
66 app->UpdateActualFilter( );
80 virtual ~SingleSeedCommand( )
85 cpPlugins::Interface::ParametersQtDialog* Dialog;
89 // -------------------------------------------------------------------------
90 cpPlugins::Interface::ParametersQtDialog::
91 ParametersQtDialog( QWidget* parent, Qt::WindowFlags f )
92 : QDialog( parent, f ),
96 this->m_Title = new QLabel( this );
97 this->m_Title->setText( "Parameters dialog title" );
99 this->m_MainLayout = new QGridLayout( this );
100 this->m_ToolsLayout = new QVBoxLayout( );
101 this->m_ToolsLayout->addWidget( this->m_Title );
102 this->m_MainLayout->addLayout( this->m_ToolsLayout, 0, 0, 1, 1 );
105 // -------------------------------------------------------------------------
106 cpPlugins::Interface::ParametersQtDialog::
107 ~ParametersQtDialog( )
109 delete this->m_Title;
110 delete this->m_ToolsLayout;
111 delete this->m_MainLayout;
114 // -------------------------------------------------------------------------
115 bool cpPlugins::Interface::ParametersQtDialog::
118 return( this->m_IsModal );
121 // -------------------------------------------------------------------------
122 cpPlugins::Interface::
123 Parameters* cpPlugins::Interface::ParametersQtDialog::
124 getParameters( ) const
126 return( this->m_Parameters );
129 // -------------------------------------------------------------------------
130 void cpPlugins::Interface::ParametersQtDialog::
131 addInteractor( vtkRenderWindowInteractor* interactor )
133 this->m_Interactors.insert( interactor );
136 // -------------------------------------------------------------------------
137 cpPlugins::Interface::ParametersQtDialog::
138 TInteractors& cpPlugins::Interface::ParametersQtDialog::
141 return( this->m_Interactors );
144 // -------------------------------------------------------------------------
145 const cpPlugins::Interface::ParametersQtDialog::
146 TInteractors& cpPlugins::Interface::ParametersQtDialog::
147 getInteractors( ) const
149 return( this->m_Interactors );
152 // -------------------------------------------------------------------------
153 bool cpPlugins::Interface::ParametersQtDialog::
154 setParameters( Parameters* parameters )
156 this->m_IsModal = true;
157 this->m_Parameters = parameters;
158 if( this->m_Parameters == NULL )
162 auto filter = this->m_Parameters->GetProcessObject( );
163 std::stringstream title;
164 title << "Parameters for \"" << filter->GetName( ) << "\"";
165 this->m_Title->setText( title.str( ).c_str( ) );
168 auto& raw_params = this->m_Parameters->GetRawParameters( );
169 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
171 QWidget* w_input = NULL;
172 switch( pIt->second.first )
174 case Parameters::String:
176 QLineEdit* v_string = new QLineEdit( this );
177 v_string->setText( pIt->second.second.c_str( ) );
181 case Parameters::Bool:
183 QCheckBox* v_bool = new QCheckBox( this );
184 v_bool->setText( "[ON/OFF]" );
185 v_bool->setChecked( pIt->second.second == "1" );
189 case Parameters::Int:
190 case Parameters::Uint:
192 std::istringstream tok_str( pIt->second.second );
195 QSpinBox* v_uint = new QSpinBox( this );
196 if( pIt->second.first == Parameters::Uint )
197 v_uint->setMinimum( 0 );
199 v_uint->setMinimum( -std::numeric_limits< int >::max( ) );
200 v_uint->setMaximum( std::numeric_limits< int >::max( ) );
201 v_uint->setValue( v );
205 case Parameters::Real:
207 std::istringstream tok_str( pIt->second.second );
210 QDoubleSpinBox* v_double = new QDoubleSpinBox( this );
211 v_double->setDecimals( 3 );
212 v_double->setMinimum( -std::numeric_limits< double >::max( ) );
213 v_double->setMaximum( std::numeric_limits< double >::max( ) );
214 v_double->setValue( v );
218 case Parameters::Index:
220 case Parameters::Point:
222 case Parameters::Vector:
224 case Parameters::FileName:
226 QFrame* frame = new QFrame( this );
227 QHBoxLayout* layout = new QHBoxLayout( frame );
228 QLineEdit* v_string = new QLineEdit( frame );
229 v_string->setText( pIt->second.second.c_str( ) );
230 QPushButton* v_button = new QPushButton( frame );
231 v_button->setObjectName( pIt->first.c_str( ) );
232 v_button->setText( "..." );
234 v_button, SIGNAL( clicked( ) ),
235 this, SLOT( _dlg_MultipleFiles( ) )
238 layout->addWidget( v_string );
239 layout->addWidget( v_button );
243 case Parameters::PathName:
245 case Parameters::StringList:
247 case Parameters::BoolList:
249 case Parameters::IntList:
251 case Parameters::UintList:
253 case Parameters::RealList:
255 case Parameters::IndexList:
257 case Parameters::PointList:
259 case Parameters::VectorList:
261 case Parameters::FileNameList:
263 case Parameters::PathNameList:
265 case Parameters::Choices:
273 else if( pt == Parameters::Real )
275 pt == Parameters::StringList ||
276 pt == Parameters::IntList ||
277 pt == Parameters::UintList ||
278 pt == Parameters::RealList
281 cpPlugins::Interface::ParametersListWidget* l_double =
282 new cpPlugins::Interface::ParametersListWidget( *nIt, this );
285 else if( pt == Parameters::Point || pt == Parameters::Index )
287 vtkSmartPointer< SingleSeedCommand > command =
288 vtkSmartPointer< SingleSeedCommand >::New( );
289 command->Dialog = this;
290 command->Name = *nIt;
292 auto iIt = this->m_Interactors.begin( );
293 for( ; iIt != this->m_Interactors.end( ); ++iIt )
296 dynamic_cast< TStyle* >( ( *iIt )->GetInteractorStyle( ) );
299 style->SeedWidgetOn( );
300 style->SetSeedWidgetCommand( command );
305 this->m_IsModal = false;
310 // Ok, a representation was created
311 if( w_input != NULL )
313 w_input->setObjectName( QString( pIt->first.c_str( ) ) );
315 QHBoxLayout* new_layout = new QHBoxLayout( );
316 QLabel* label = new QLabel( this );
317 label->setText( QString( pIt->first.c_str( ) ) );
318 new_layout->addWidget( label );
319 new_layout->addWidget( w_input );
320 this->m_ToolsLayout->addLayout( new_layout );
325 return( this->m_IsModal );
328 // -------------------------------------------------------------------------
329 int cpPlugins::Interface::ParametersQtDialog::
332 if( !this->m_IsModal )
336 QDialogButtonBox* bb = new QDialogButtonBox(
337 QDialogButtonBox::Ok | QDialogButtonBox::Cancel
339 QObject::connect( bb, SIGNAL( accepted( ) ), this, SLOT( accept( ) ) );
340 QObject::connect( bb, SIGNAL( rejected( ) ), this, SLOT( reject( ) ) );
341 this->m_ToolsLayout->addWidget( bb );
343 int ret = this->QDialog::exec( );
345 this->syncParameters( );
349 // -------------------------------------------------------------------------
350 void cpPlugins::Interface::ParametersQtDialog::
353 if( this->m_IsModal )
356 this->QDialog::show( );
359 // -------------------------------------------------------------------------
360 void cpPlugins::Interface::ParametersQtDialog::
363 if( this->m_Parameters == NULL )
367 std::vector< std::string > names;
368 this->m_Parameters->GetNames( names );
369 std::vector< std::string >::const_iterator nIt = names.begin( );
370 for( ; nIt != names.end( ); ++nIt )
372 Parameters::Type pt = this->m_Parameters->GetType( *nIt );
374 if( pt == Parameters::String )
376 QLineEdit* v = this->findChild< QLineEdit* >( nIt->c_str( ) );
378 this->m_Parameters->SetString( *nIt, v->text( ).toStdString( ) );
380 else if( pt == Parameters::Bool )
382 QCheckBox* v = this->findChild< QCheckBox* >( nIt->c_str( ) );
384 this->m_Parameters->SetBool( *nIt, v->isChecked( ) );
386 else if( pt == Parameters::Uint )
388 QSpinBox* v = this->findChild< QSpinBox* >( nIt->c_str( ) );
390 this->m_Parameters->SetUint( *nIt, v->value( ) );
392 else if( pt == Parameters::Int )
394 QSpinBox* v = this->findChild< QSpinBox* >( nIt->c_str( ) );
396 this->m_Parameters->SetInt( *nIt, v->value( ) );
398 else if( pt == Parameters::Real )
400 QDoubleSpinBox* v = this->findChild< QDoubleSpinBox* >( nIt->c_str( ) );
402 this->m_Parameters->SetReal( *nIt, v->value( ) );
405 pt == Parameters::StringList ||
406 pt == Parameters::IntList ||
407 pt == Parameters::UintList ||
408 pt == Parameters::RealList
411 cpPlugins::Interface::ParametersListWidget* lst =
412 this->findChild< cpPlugins::Interface::ParametersListWidget* >(
417 if( pt == Parameters::StringList )
419 this->m_Parameters->ClearStringList( *nIt );
420 std::vector< std::string > values = lst->GetStringValues( );
421 for( int r = 0; r < values.size( ); ++r )
422 this->m_Parameters->AddToStringList( *nIt, values[ r ] );
424 else if( pt == Parameters::IntList )
426 this->m_Parameters->ClearIntList( *nIt );
427 std::vector< int > values = lst->GetIntValues( );
428 for( int r = 0; r < values.size( ); ++r )
429 this->m_Parameters->AddToIntList( *nIt, values[ r ] );
431 else if( pt == Parameters::UintList )
433 this->m_Parameters->ClearUintList( *nIt );
434 std::vector< unsigned int > values = lst->GetUintValues( );
435 for( int r = 0; r < values.size( ); ++r )
436 this->m_Parameters->AddToUintList( *nIt, values[ r ] );
438 else if( pt == Parameters::RealList )
440 this->m_Parameters->ClearRealList( *nIt );
441 std::vector< double > values = lst->GetDoubleValues( );
442 for( int r = 0; r < values.size( ); ++r )
443 this->m_Parameters->AddToRealList( *nIt, values[ r ] );
449 else if( pt == Parameters::Point || pt == Parameters::Index )
456 // -------------------------------------------------------------------------
457 void cpPlugins::Interface::ParametersQtDialog::
458 _dlg_MultipleFiles( )
460 QObject* sender = this->sender( );
461 // std::cout << "OK " << sender << " " << sender->objectName( ).toStdString( ) << std::endl;
488 pt == Parameters::StringList ||
489 pt == Parameters::IntList ||
490 pt == Parameters::UintList ||
491 pt == Parameters::RealList
494 cpPlugins::Interface::ParametersListWidget* lst =
495 this->findChild< cpPlugins::Interface::ParametersListWidget* >(
500 if( pt == Parameters::StringList )
502 std::vector< std::string > values = lst->GetStringValues( );
503 for( int r = 0; r < values.size( ); ++r )
504 this->m_Parameters->AddToStringList( *nIt, values[ r ] );
506 else if( pt == Parameters::IntList )
508 std::vector< int > values = lst->GetIntValues( );
509 for( int r = 0; r < values.size( ); ++r )
510 this->m_Parameters->AddToIntList( *nIt, values[ r ] );
512 else if( pt == Parameters::UintList )
514 std::vector< unsigned int > values = lst->GetUintValues( );
515 for( int r = 0; r < values.size( ); ++r )
516 this->m_Parameters->AddToUintList( *nIt, values[ r ] );
518 else if( pt == Parameters::RealList )
520 std::vector< double > values = lst->GetDoubleValues( );
521 for( int r = 0; r < values.size( ); ++r )
522 this->m_Parameters->AddToRealList( *nIt, values[ r ] );
535 #endif // cpPlugins_Interface_QT4