1 #include <cpPlugins/Interface/ParametersQtDialog.h>
3 #ifdef cpPlugins_Interface_QT4
7 #include <cpPlugins/Interface/ParametersListWidget.h>
8 #include <cpPlugins/Interface/ProcessObject.h>
10 #include <vtkCommand.h>
11 #include <vtkRenderWindowInteractor.h>
15 #include <QDoubleSpinBox>
16 #include <QFileDialog>
17 #include <QHBoxLayout>
18 #include <QInputDialog>
20 #include <QPushButton>
24 class SingleSeedCommand
28 static SingleSeedCommand* New( )
29 { return( new SingleSeedCommand ); }
30 virtual void Execute( vtkObject* caller, unsigned long eid, void* data )
32 // Get seed, avoiding segfaults!!!
33 if( eid != vtkCommand::PlacePointEvent || this->Dialog == NULL )
35 vtkSeedWidget* widget = dynamic_cast< vtkSeedWidget* >( caller );
38 vtkSeedRepresentation* rep = widget->GetSeedRepresentation( );
41 if( rep->GetNumberOfSeeds( ) == 0 )
44 rep->GetSeedWorldPosition( 0, seed );
46 // Delete all seeds (remember that this command is just for one seed)
47 while( rep->GetNumberOfSeeds( ) > 0 )
48 widget->DeleteSeed( 0 );
50 if( this->Dialog->getParameters( )->HasIndex( this->Name ) )
53 else if( this->Dialog->getParameters( )->HasPoint( this->Name ) )
55 this->Dialog->getParameters( )->SetPoint( this->Name, 3, seed );
56 this->Dialog->updateParameters( );
57 auto filter = this->Dialog->getParameters( )->GetProcessObject( );
60 auto plugins = filter->GetPlugins( );
63 auto app = plugins->GetApplication( );
65 app->UpdateActualFilter( );
79 virtual ~SingleSeedCommand( )
84 cpPlugins::Interface::ParametersQtDialog* Dialog;
89 // -------------------------------------------------------------------------
90 cpPlugins::Interface::ParametersQtDialog::
91 ParametersQtDialog( QWidget* parent, Qt::WindowFlags f )
92 : QDialog( parent, f ),
94 m_Interactive( false )
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 cpPlugins::Interface::
116 Parameters* cpPlugins::Interface::ParametersQtDialog::
117 getParameters( ) const
119 return( this->m_Parameters );
122 // -------------------------------------------------------------------------
123 void cpPlugins::Interface::ParametersQtDialog::
124 addInteractor( vtkRenderWindowInteractor* interactor )
126 this->m_Interactors.insert( interactor );
129 // -------------------------------------------------------------------------
130 cpPlugins::Interface::ParametersQtDialog::
131 TInteractors& cpPlugins::Interface::ParametersQtDialog::
134 return( this->m_Interactors );
137 // -------------------------------------------------------------------------
138 const cpPlugins::Interface::ParametersQtDialog::
139 TInteractors& cpPlugins::Interface::ParametersQtDialog::
140 getInteractors( ) const
142 return( this->m_Interactors );
145 // -------------------------------------------------------------------------
146 bool cpPlugins::Interface::ParametersQtDialog::
147 isInteractive( ) const
149 return( this->m_Interactive );
152 // -------------------------------------------------------------------------
153 void cpPlugins::Interface::ParametersQtDialog::
154 setInteractive( bool i )
156 this->m_Interactive = i;
159 // -------------------------------------------------------------------------
160 void cpPlugins::Interface::ParametersQtDialog::
163 this->m_Interactive = true;
166 // -------------------------------------------------------------------------
167 void cpPlugins::Interface::ParametersQtDialog::
170 this->m_Interactive = false;
173 // -------------------------------------------------------------------------
174 bool cpPlugins::Interface::ParametersQtDialog::
175 setParameters( Parameters* parameters )
177 if( this->m_Parameters != NULL || parameters == NULL )
179 this->m_Parameters = parameters;
182 auto filter = this->m_Parameters->GetProcessObject( );
183 std::stringstream title;
184 title << "Parameters for \"" << filter->GetName( ) << "\"";
185 this->m_Title->setText( title.str( ).c_str( ) );
188 this->m_Widgets.clear( );
189 auto& raw_params = this->m_Parameters->GetRawParameters( );
190 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
192 QWidget* w_input = NULL;
193 switch( pIt->second.first )
195 case Parameters::String:
197 QLineEdit* v_string = new QLineEdit( this );
198 v_string->setObjectName( pIt->first.c_str( ) );
199 v_string->setText( pIt->second.second.c_str( ) );
203 case Parameters::Bool:
205 QCheckBox* v_bool = new QCheckBox( this );
206 v_bool->setObjectName( pIt->first.c_str( ) );
207 v_bool->setText( "[ON/OFF]" );
208 v_bool->setChecked( pIt->second.second == "1" );
212 case Parameters::Int:
213 case Parameters::Uint:
215 std::istringstream tok_str( pIt->second.second );
218 QSpinBox* v_uint = new QSpinBox( this );
219 v_uint->setObjectName( pIt->first.c_str( ) );
220 if( pIt->second.first == Parameters::Uint )
221 v_uint->setMinimum( 0 );
223 v_uint->setMinimum( -std::numeric_limits< int >::max( ) );
224 v_uint->setMaximum( std::numeric_limits< int >::max( ) );
225 v_uint->setValue( v );
229 case Parameters::Real:
231 std::istringstream tok_str( pIt->second.second );
234 QDoubleSpinBox* v_double = new QDoubleSpinBox( this );
235 v_double->setObjectName( pIt->first.c_str( ) );
236 v_double->setDecimals( 3 );
237 v_double->setMinimum( -std::numeric_limits< double >::max( ) );
238 v_double->setMaximum( std::numeric_limits< double >::max( ) );
239 v_double->setValue( v );
243 case Parameters::OpenFileName:
245 QFrame* frame = new QFrame( this );
246 QHBoxLayout* layout = new QHBoxLayout( frame );
247 QLineEdit* v_string = new QLineEdit( frame );
248 v_string->setObjectName( pIt->first.c_str( ) );
249 v_string->setText( pIt->second.second.c_str( ) );
250 QPushButton* v_button = new QPushButton( frame );
251 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
252 v_button->setText( "..." );
254 v_button, SIGNAL( clicked( ) ),
255 this, SLOT( _dlg_OpenSingleFile( ) )
257 layout->addWidget( v_string );
258 layout->addWidget( v_button );
261 case Parameters::SaveFileName:
263 QFrame* frame = new QFrame( this );
264 QHBoxLayout* layout = new QHBoxLayout( frame );
265 QLineEdit* v_string = new QLineEdit( frame );
266 v_string->setObjectName( pIt->first.c_str( ) );
267 v_string->setText( pIt->second.second.c_str( ) );
268 QPushButton* v_button = new QPushButton( frame );
269 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
270 v_button->setText( "..." );
272 v_button, SIGNAL( clicked( ) ),
273 this, SLOT( _dlg_SaveSingleFile( ) )
275 layout->addWidget( v_string );
276 layout->addWidget( v_button );
280 case Parameters::PathName:
282 QFrame* frame = new QFrame( this );
283 QHBoxLayout* layout = new QHBoxLayout( frame );
284 QLineEdit* v_string = new QLineEdit( frame );
285 v_string->setObjectName( pIt->first.c_str( ) );
286 v_string->setText( pIt->second.second.c_str( ) );
287 QPushButton* v_button = new QPushButton( frame );
288 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
289 v_button->setText( "..." );
291 v_button, SIGNAL( clicked( ) ),
292 this, SLOT( _dlg_OpenSinglePath( ) )
294 layout->addWidget( v_string );
295 layout->addWidget( v_button );
299 case Parameters::StringList:
301 case Parameters::BoolList:
303 case Parameters::IntList:
305 QFrame* frame = new QFrame( this );
306 QHBoxLayout* layout = new QHBoxLayout( frame );
307 QLineEdit* v_string = new QLineEdit( frame );
308 v_string->setObjectName( pIt->first.c_str( ) );
309 v_string->setText( pIt->second.second.c_str( ) );
310 QPushButton* v_button = new QPushButton( frame );
311 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
312 v_button->setText( "+" );
314 v_button, SIGNAL( clicked( ) ),
315 this, SLOT( _dlg_AddInt( ) )
317 layout->addWidget( v_string );
318 layout->addWidget( v_button );
322 case Parameters::UintList:
324 QFrame* frame = new QFrame( this );
325 QHBoxLayout* layout = new QHBoxLayout( frame );
326 QLineEdit* v_string = new QLineEdit( frame );
327 v_string->setObjectName( pIt->first.c_str( ) );
328 v_string->setText( pIt->second.second.c_str( ) );
329 QPushButton* v_button = new QPushButton( frame );
330 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
331 v_button->setText( "+" );
333 v_button, SIGNAL( clicked( ) ),
334 this, SLOT( _dlg_AddUint( ) )
336 layout->addWidget( v_string );
337 layout->addWidget( v_button );
341 case Parameters::RealList:
343 QFrame* frame = new QFrame( this );
344 QHBoxLayout* layout = new QHBoxLayout( frame );
345 QLineEdit* v_string = new QLineEdit( frame );
346 v_string->setObjectName( pIt->first.c_str( ) );
347 v_string->setText( pIt->second.second.c_str( ) );
348 QPushButton* v_button = new QPushButton( frame );
349 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
350 v_button->setText( "+" );
352 v_button, SIGNAL( clicked( ) ),
353 this, SLOT( _dlg_AddReal( ) )
355 layout->addWidget( v_string );
356 layout->addWidget( v_button );
360 case Parameters::OpenFileNameList:
362 QFrame* frame = new QFrame( this );
363 QHBoxLayout* layout = new QHBoxLayout( frame );
364 QLineEdit* v_string = new QLineEdit( frame );
365 v_string->setObjectName( pIt->first.c_str( ) );
366 v_string->setText( pIt->second.second.c_str( ) );
367 QPushButton* v_button = new QPushButton( frame );
368 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
369 v_button->setText( "..." );
371 v_button, SIGNAL( clicked( ) ),
372 this, SLOT( _dlg_OpenMultipleFiles( ) )
374 layout->addWidget( v_string );
375 layout->addWidget( v_button );
379 case Parameters::SaveFileNameList:
381 case Parameters::PathNameList:
383 case Parameters::Choices:
385 QComboBox* v_choices = new QComboBox( this );
386 v_choices->setObjectName( pIt->first.c_str( ) );
388 std::istringstream str0( pIt->second.second );
390 std::getline( str0, choices, '@' );
391 std::istringstream str1( choices );
394 while( std::getline( str1, token, '#' ) )
395 v_choices->insertItem( id++, token.c_str( ) );
404 // Ok, a representation was created
405 if( w_input != NULL )
407 QHBoxLayout* new_layout = new QHBoxLayout( );
408 QLabel* label = new QLabel( this );
409 label->setText( QString( pIt->first.c_str( ) ) );
410 new_layout->addWidget( label );
411 new_layout->addWidget( w_input );
412 this->m_ToolsLayout->addLayout( new_layout );
419 this->m_Buttons = new QDialogButtonBox(
420 QDialogButtonBox::Ok | QDialogButtonBox::Cancel
423 this->m_Buttons, SIGNAL( accepted( ) ), this, SLOT( accept( ) )
426 this->m_Buttons, SIGNAL( rejected( ) ), this, SLOT( reject( ) )
428 this->m_ToolsLayout->addWidget( this->m_Buttons );
436 // -------------------------------------------------------------------------
437 int cpPlugins::Interface::ParametersQtDialog::
440 int ret = this->QDialog::exec( );
442 this->updateParameters( );
448 // -------------------------------------------------------------------------
449 void cpPlugins::Interface::ParametersQtDialog::
452 this->QDialog::show( );
455 // -------------------------------------------------------------------------
456 void cpPlugins::Interface::ParametersQtDialog::
459 if( this->m_Parameters == NULL )
463 auto& raw_params = this->m_Parameters->GetRawParameters( );
464 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
466 QString pName = pIt->first.c_str( );
467 switch( pIt->second.first )
469 case Parameters::String:
470 case Parameters::OpenFileName:
471 case Parameters::SaveFileName:
472 case Parameters::PathName:
473 case Parameters::IntList:
474 case Parameters::UintList:
475 case Parameters::RealList:
476 case Parameters::OpenFileNameList:
478 QLineEdit* v_string = this->findChild< QLineEdit* >( pName );
479 if( v_string != NULL )
480 pIt->second.second = v_string->text( ).toStdString( );
483 case Parameters::Bool:
485 QCheckBox* v_bool = this->findChild< QCheckBox* >( pName );
487 pIt->second.second = ( v_bool->isChecked( ) )? "1": "0";
490 case Parameters::Int:
491 case Parameters::Uint:
493 QSpinBox* v_uint = this->findChild< QSpinBox* >( pName );
496 std::stringstream str;
497 str << v_uint->value( );
498 pIt->second.second = str.str( );
503 case Parameters::Real:
505 QDoubleSpinBox* v_double = this->findChild< QDoubleSpinBox* >( pName );
508 std::stringstream str;
509 str << v_double->value( );
510 pIt->second.second = str.str( );
515 case Parameters::StringList:
517 case Parameters::BoolList:
519 case Parameters::SaveFileNameList:
521 case Parameters::PathNameList:
523 case Parameters::Choices:
525 QComboBox* v_choices = this->findChild< QComboBox* >( pName );
526 if( v_choices != NULL )
528 std::istringstream str_choices( pIt->second.second );
529 std::string real_choices;
530 std::getline( str_choices, real_choices, '@' );
533 v_choices->currentText( ).toStdString( );
545 // -------------------------------------------------------------------------
546 void cpPlugins::Interface::ParametersQtDialog::
549 if( this->m_Parameters == NULL )
553 auto& raw_params = this->m_Parameters->GetRawParameters( );
554 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
556 QString pName = pIt->first.c_str( );
557 switch( pIt->second.first )
559 case Parameters::String:
560 case Parameters::OpenFileName:
561 case Parameters::SaveFileName:
562 case Parameters::PathName:
563 case Parameters::IntList:
564 case Parameters::UintList:
565 case Parameters::RealList:
566 case Parameters::OpenFileNameList:
568 QLineEdit* v_string = this->findChild< QLineEdit* >( pName );
569 if( v_string != NULL )
570 v_string->setText( pIt->second.second.c_str( ) );
573 case Parameters::Bool:
575 QCheckBox* v_bool = this->findChild< QCheckBox* >( pName );
577 v_bool->setChecked( pIt->second.second == "1" );
580 case Parameters::Int:
581 case Parameters::Uint:
583 QSpinBox* v_uint = this->findChild< QSpinBox* >( pName );
586 std::istringstream tok_str( pIt->second.second );
589 v_uint->setValue( v );
594 case Parameters::Real:
596 QDoubleSpinBox* v_double = this->findChild< QDoubleSpinBox* >( pName );
599 std::istringstream tok_str( pIt->second.second );
602 v_double->setValue( v );
607 case Parameters::StringList:
609 case Parameters::BoolList:
611 case Parameters::SaveFileNameList:
613 case Parameters::PathNameList:
615 case Parameters::Choices:
617 QComboBox* v_choices = this->findChild< QComboBox* >( pName );
618 if( v_choices != NULL )
620 std::istringstream str_choices( pIt->second.second );
621 std::string choices, real_choice;
622 std::getline( str_choices, choices, '@' );
623 std::getline( str_choices, real_choice, '@' );
624 std::istringstream str( choices );
626 int id = -1, cont = 0;
627 while( std::getline( str, token, '#' ) )
629 if( token == real_choice )
636 v_choices->setCurrentIndex( id );
648 // -------------------------------------------------------------------------
649 void cpPlugins::Interface::ParametersQtDialog::
650 _dlg_OpenSingleFile( )
652 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
655 std::string bName = btn->objectName( ).toStdString( );
656 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
657 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
660 std::string param_name = line->objectName( ).toStdString( );
661 if( param_name != "" )
663 std::string param_value =
664 this->m_Parameters->GetOpenFileName( param_name );
665 if( param_value == "" )
668 filters << "Any file (*)";
670 // Show dialog and check if it was accepted
671 QFileDialog dialog( this );
672 dialog.setFileMode( QFileDialog::ExistingFile );
673 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
674 dialog.setNameFilters( filters );
675 dialog.setAcceptMode( QFileDialog::AcceptOpen );
677 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
686 // -------------------------------------------------------------------------
687 void cpPlugins::Interface::ParametersQtDialog::
688 _dlg_SaveSingleFile( )
690 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
693 std::string bName = btn->objectName( ).toStdString( );
694 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
695 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
698 std::string param_name = line->objectName( ).toStdString( );
699 if( param_name != "" )
701 std::string param_value =
702 this->m_Parameters->GetSaveFileName( param_name );
703 if( param_value == "" )
706 filters << "Any file (*)";
708 // Show dialog and check if it was accepted
709 QFileDialog dialog( this );
710 dialog.setFileMode( QFileDialog::AnyFile );
711 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
712 dialog.setNameFilters( filters );
713 dialog.setAcceptMode( QFileDialog::AcceptSave );
715 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
724 // -------------------------------------------------------------------------
725 void cpPlugins::Interface::ParametersQtDialog::
726 _dlg_OpenSinglePath( )
728 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
731 std::string bName = btn->objectName( ).toStdString( );
732 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
733 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
736 std::string param_name = line->objectName( ).toStdString( );
737 if( param_name != "" )
739 std::string param_value =
740 this->m_Parameters->GetPathName( param_name );
741 if( param_value == "" )
744 // Show dialog and check if it was accepted
745 QFileDialog dialog( this );
746 dialog.setFileMode( QFileDialog::Directory );
747 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
748 dialog.setAcceptMode( QFileDialog::AcceptOpen );
750 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
759 // -------------------------------------------------------------------------
760 void cpPlugins::Interface::ParametersQtDialog::
761 _dlg_OpenMultipleFiles( )
763 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
766 std::string bName = btn->objectName( ).toStdString( );
767 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
769 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
772 std::string param_name = line->objectName( ).toStdString( );
773 if( param_name != "" )
776 filters << "Any file (*)";
778 // Show dialog and check if it was accepted
779 QFileDialog dialog( this );
780 dialog.setFileMode( QFileDialog::ExistingFiles );
781 dialog.setNameFilters( filters );
782 dialog.setAcceptMode( QFileDialog::AcceptOpen );
785 if( dialog.selectedFiles( ).size( ) > 0 )
787 std::stringstream str;
788 auto files = dialog.selectedFiles( );
789 auto fIt = files.begin( );
790 str << fIt->toStdString( );
792 for( ; fIt != files.end( ); ++fIt )
793 str << "#" << fIt->toStdString( );
794 line->setText( str.str( ).c_str( ) );
807 // -------------------------------------------------------------------------
808 void cpPlugins::Interface::ParametersQtDialog::
811 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
814 std::string bName = btn->objectName( ).toStdString( );
815 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
816 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
819 std::string param_name = line->objectName( ).toStdString( );
820 if( param_name != "" )
824 QInputDialog::getInt(
827 std::string( "Add new value to \"" ) +
832 0, -2147483647, 2147483647, 1,
837 std::string values = line->text( ).toStdString( );
840 std::stringstream str;
841 str << values << value;
842 line->setText( str.str( ).c_str( ) );
853 // -------------------------------------------------------------------------
854 void cpPlugins::Interface::ParametersQtDialog::
857 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
860 std::string bName = btn->objectName( ).toStdString( );
861 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
862 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
865 std::string param_name = line->objectName( ).toStdString( );
866 if( param_name != "" )
870 QInputDialog::getInt(
873 std::string( "Add new value to \"" ) +
883 std::string values = line->text( ).toStdString( );
886 std::stringstream str;
887 str << values << value;
888 line->setText( str.str( ).c_str( ) );
899 // -------------------------------------------------------------------------
900 void cpPlugins::Interface::ParametersQtDialog::
903 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
906 std::string bName = btn->objectName( ).toStdString( );
907 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
908 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
911 std::string param_name = line->objectName( ).toStdString( );
912 if( param_name != "" )
916 QInputDialog::getDouble(
919 std::string( "Add new value to \"" ) +
924 0, -2147483647, 2147483647, 1,
929 std::string values = line->text( ).toStdString( );
932 std::stringstream str;
933 str << values << value;
934 line->setText( str.str( ).c_str( ) );
945 #endif // cpPlugins_Interface_QT4