1 #include <cpPlugins/ParametersQtDialog.h>
7 #include <cpPlugins/ProcessObject.h>
11 #include <QDoubleSpinBox>
12 #include <QFileDialog>
13 #include <QHBoxLayout>
14 #include <QInputDialog>
16 #include <QPushButton>
19 // -------------------------------------------------------------------------
20 cpPlugins::ParametersQtDialog::
21 ParametersQtDialog( QWidget* parent, Qt::WindowFlags f, bool manual )
22 : QDialog( parent, f ),
23 m_ProcessObject( NULL ),
24 m_WidgetsUpdated( false ),
29 this->m_Title = new QLabel( this );
30 this->m_Title->setText( "Parameters dialog title" );
31 this->m_MainLayout = new QGridLayout( this );
32 this->m_ToolsLayout = new QVBoxLayout( );
33 this->m_ToolsLayout->addWidget( this->m_Title );
34 this->m_MainLayout->addLayout( this->m_ToolsLayout, 0, 0, 1, 1 );
39 // -------------------------------------------------------------------------
40 cpPlugins::ParametersQtDialog::
41 ~ParametersQtDialog( )
46 delete this->m_ToolsLayout;
47 delete this->m_MainLayout;
52 // -------------------------------------------------------------------------
54 ProcessObject* cpPlugins::ParametersQtDialog::
55 getProcessObject( ) const
57 return( this->m_ProcessObject );
60 // -------------------------------------------------------------------------
61 bool cpPlugins::ParametersQtDialog::
62 setProcessObject( ProcessObject* obj )
64 if( this->m_ProcessObject != NULL || obj == NULL )
66 this->m_ProcessObject = obj;
67 this->m_WidgetsUpdated = false;
71 // -------------------------------------------------------------------------
72 int cpPlugins::ParametersQtDialog::
75 this->_updateWidgets( );
78 int ret = this->QDialog::exec( );
80 this->updateParameters( );
86 // -------------------------------------------------------------------------
87 void cpPlugins::ParametersQtDialog::
90 if( this->m_ProcessObject == NULL )
95 // Check if an explicit re-execution button is needed
96 if( this->m_ProcessObject->GetCouldHaveExplicitReExecution( ) )
99 this->findChild< QCheckBox* >( "___ExplicitReExecution___" );
101 this->m_ProcessObject->SetExplicitReExecution( v_bool->isChecked( ) );
106 auto parameters = this->m_ProcessObject->GetParameters( );
107 auto& raw_params = parameters->GetRawParameters( );
108 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
110 QString pName = pIt->first.c_str( );
111 switch( pIt->second.first )
113 case Parameters::String:
114 case Parameters::OpenFileName:
115 case Parameters::SaveFileName:
116 case Parameters::PathName:
117 case Parameters::IntList:
118 case Parameters::UintList:
119 case Parameters::RealList:
120 case Parameters::OpenFileNameList:
122 QLineEdit* v_string = this->findChild< QLineEdit* >( pName );
123 if( v_string != NULL )
124 pIt->second.second = v_string->text( ).toStdString( );
127 case Parameters::Bool:
129 QCheckBox* v_bool = this->findChild< QCheckBox* >( pName );
131 pIt->second.second = ( v_bool->isChecked( ) )? "1": "0";
134 case Parameters::Int:
135 case Parameters::Uint:
137 QSpinBox* v_uint = this->findChild< QSpinBox* >( pName );
140 std::stringstream str;
141 str << v_uint->value( );
142 pIt->second.second = str.str( );
147 case Parameters::Real:
149 QDoubleSpinBox* v_double = this->findChild< QDoubleSpinBox* >( pName );
152 std::stringstream str;
153 str << v_double->value( );
154 pIt->second.second = str.str( );
159 case Parameters::StringList:
161 case Parameters::BoolList:
163 case Parameters::SaveFileNameList:
165 case Parameters::PathNameList:
167 case Parameters::Choices:
169 QComboBox* v_choices = this->findChild< QComboBox* >( pName );
170 if( v_choices != NULL )
172 std::istringstream str_choices( pIt->second.second );
173 std::string real_choices;
174 std::getline( str_choices, real_choices, '@' );
177 v_choices->currentText( ).toStdString( );
187 this->m_ProcessObject->Modified( );
190 // -------------------------------------------------------------------------
191 void cpPlugins::ParametersQtDialog::
194 if( this->m_ProcessObject == NULL )
199 // Check if an explicit re-execution button is needed
200 if( this->m_ProcessObject->GetCouldHaveExplicitReExecution( ) )
203 this->findChild< QCheckBox* >( "___ExplicitReExecution___" );
205 v_bool->setChecked( this->m_ProcessObject->GetExplicitReExecution( ) );
210 auto parameters = this->m_ProcessObject->GetParameters( );
211 auto& raw_params = parameters->GetRawParameters( );
212 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
214 QString pName = pIt->first.c_str( );
215 switch( pIt->second.first )
217 case Parameters::String:
218 case Parameters::OpenFileName:
219 case Parameters::SaveFileName:
220 case Parameters::PathName:
221 case Parameters::IntList:
222 case Parameters::UintList:
223 case Parameters::RealList:
224 case Parameters::OpenFileNameList:
226 QLineEdit* v_string = this->findChild< QLineEdit* >( pName );
227 if( v_string != NULL )
228 v_string->setText( pIt->second.second.c_str( ) );
231 case Parameters::Bool:
233 QCheckBox* v_bool = this->findChild< QCheckBox* >( pName );
235 v_bool->setChecked( pIt->second.second == "1" );
238 case Parameters::Int:
239 case Parameters::Uint:
241 QSpinBox* v_uint = this->findChild< QSpinBox* >( pName );
244 std::istringstream tok_str( pIt->second.second );
247 v_uint->setValue( v );
252 case Parameters::Real:
254 QDoubleSpinBox* v_double = this->findChild< QDoubleSpinBox* >( pName );
257 std::istringstream tok_str( pIt->second.second );
260 v_double->setValue( v );
265 case Parameters::StringList:
267 case Parameters::BoolList:
269 case Parameters::SaveFileNameList:
271 case Parameters::PathNameList:
273 case Parameters::Choices:
275 QComboBox* v_choices = this->findChild< QComboBox* >( pName );
276 if( v_choices != NULL )
278 std::istringstream str_choices( pIt->second.second );
279 std::string choices, real_choice;
280 std::getline( str_choices, choices, '@' );
281 std::getline( str_choices, real_choice, '@' );
282 std::istringstream str( choices );
284 int id = -1, cont = 0;
285 while( std::getline( str, token, '#' ) )
287 if( token == real_choice )
294 v_choices->setCurrentIndex( id );
306 // -------------------------------------------------------------------------
307 void cpPlugins::ParametersQtDialog::
314 this->m_Buttons = new QDialogButtonBox(
315 QDialogButtonBox::Ok | QDialogButtonBox::Cancel
318 this->m_Buttons, SIGNAL( accepted( ) ), this, SLOT( accept( ) )
321 this->m_Buttons, SIGNAL( rejected( ) ), this, SLOT( reject( ) )
323 this->m_ToolsLayout->addWidget( this->m_Buttons );
326 this->m_WidgetsUpdated = true;
329 // -------------------------------------------------------------------------
330 void cpPlugins::ParametersQtDialog::
333 if( this->m_WidgetsUpdated || this->m_ProcessObject == NULL )
339 std::stringstream title;
341 << "Parameters for an object of class \""
342 << this->m_ProcessObject->GetClassName( )
344 this->m_Title->setText( title.str( ).c_str( ) );
346 // Check if an explicit re-execution button is needed
347 if( this->m_ProcessObject->GetCouldHaveExplicitReExecution( ) )
349 QCheckBox* v_bool = new QCheckBox( this );
350 v_bool->setObjectName( "___ExplicitReExecution___" );
351 v_bool->setText( "Explicit re-execution" );
352 v_bool->setChecked( this->m_ProcessObject->GetExplicitReExecution( ) );
354 QHBoxLayout* new_layout = new QHBoxLayout( );
355 new_layout->addWidget( v_bool );
356 this->m_ToolsLayout->addLayout( new_layout );
361 auto parameters = this->m_ProcessObject->GetParameters( );
362 auto& raw_params = parameters->GetRawParameters( );
363 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
365 QWidget* w_input = NULL;
366 switch( pIt->second.first )
368 case Parameters::String:
370 QLineEdit* v_string = new QLineEdit( this );
371 v_string->setObjectName( pIt->first.c_str( ) );
372 v_string->setText( pIt->second.second.c_str( ) );
376 case Parameters::Bool:
378 QCheckBox* v_bool = new QCheckBox( this );
379 v_bool->setObjectName( pIt->first.c_str( ) );
380 v_bool->setText( "[ON/OFF]" );
381 v_bool->setChecked( pIt->second.second == "1" );
385 case Parameters::Int:
386 case Parameters::Uint:
388 std::istringstream tok_str( pIt->second.second );
391 QSpinBox* v_uint = new QSpinBox( this );
392 v_uint->setObjectName( pIt->first.c_str( ) );
393 if( pIt->second.first == Parameters::Uint )
394 v_uint->setMinimum( 0 );
396 v_uint->setMinimum( -std::numeric_limits< int >::max( ) );
397 v_uint->setMaximum( std::numeric_limits< int >::max( ) );
398 v_uint->setValue( v );
402 case Parameters::Real:
404 std::istringstream tok_str( pIt->second.second );
407 QDoubleSpinBox* v_double = new QDoubleSpinBox( this );
408 v_double->setObjectName( pIt->first.c_str( ) );
409 v_double->setDecimals( 3 );
410 v_double->setMinimum( -std::numeric_limits< double >::max( ) );
411 v_double->setMaximum( std::numeric_limits< double >::max( ) );
412 v_double->setValue( v );
416 case Parameters::OpenFileName:
418 QFrame* frame = new QFrame( this );
419 QHBoxLayout* layout = new QHBoxLayout( frame );
420 QLineEdit* v_string = new QLineEdit( frame );
421 v_string->setObjectName( pIt->first.c_str( ) );
422 v_string->setText( pIt->second.second.c_str( ) );
423 QPushButton* v_button = new QPushButton( frame );
424 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
425 v_button->setText( "..." );
427 v_button, SIGNAL( clicked( ) ),
428 this, SLOT( _dlg_OpenSingleFile( ) )
430 layout->addWidget( v_string );
431 layout->addWidget( v_button );
434 case Parameters::SaveFileName:
436 QFrame* frame = new QFrame( this );
437 QHBoxLayout* layout = new QHBoxLayout( frame );
438 QLineEdit* v_string = new QLineEdit( frame );
439 v_string->setObjectName( pIt->first.c_str( ) );
440 v_string->setText( pIt->second.second.c_str( ) );
441 QPushButton* v_button = new QPushButton( frame );
442 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
443 v_button->setText( "..." );
445 v_button, SIGNAL( clicked( ) ),
446 this, SLOT( _dlg_SaveSingleFile( ) )
448 layout->addWidget( v_string );
449 layout->addWidget( v_button );
453 case Parameters::PathName:
455 QFrame* frame = new QFrame( this );
456 QHBoxLayout* layout = new QHBoxLayout( frame );
457 QLineEdit* v_string = new QLineEdit( frame );
458 v_string->setObjectName( pIt->first.c_str( ) );
459 v_string->setText( pIt->second.second.c_str( ) );
460 QPushButton* v_button = new QPushButton( frame );
461 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
462 v_button->setText( "..." );
464 v_button, SIGNAL( clicked( ) ),
465 this, SLOT( _dlg_OpenSinglePath( ) )
467 layout->addWidget( v_string );
468 layout->addWidget( v_button );
472 case Parameters::StringList:
474 case Parameters::BoolList:
476 case Parameters::IntList:
478 QFrame* frame = new QFrame( this );
479 QHBoxLayout* layout = new QHBoxLayout( frame );
480 QLineEdit* v_string = new QLineEdit( frame );
481 v_string->setObjectName( pIt->first.c_str( ) );
482 v_string->setText( pIt->second.second.c_str( ) );
483 QPushButton* v_button = new QPushButton( frame );
484 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
485 v_button->setText( "+" );
487 v_button, SIGNAL( clicked( ) ),
488 this, SLOT( _dlg_AddInt( ) )
490 layout->addWidget( v_string );
491 layout->addWidget( v_button );
495 case Parameters::UintList:
497 QFrame* frame = new QFrame( this );
498 QHBoxLayout* layout = new QHBoxLayout( frame );
499 QLineEdit* v_string = new QLineEdit( frame );
500 v_string->setObjectName( pIt->first.c_str( ) );
501 v_string->setText( pIt->second.second.c_str( ) );
502 QPushButton* v_button = new QPushButton( frame );
503 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
504 v_button->setText( "+" );
506 v_button, SIGNAL( clicked( ) ),
507 this, SLOT( _dlg_AddUint( ) )
509 layout->addWidget( v_string );
510 layout->addWidget( v_button );
514 case Parameters::RealList:
516 QFrame* frame = new QFrame( this );
517 QHBoxLayout* layout = new QHBoxLayout( frame );
518 QLineEdit* v_string = new QLineEdit( frame );
519 v_string->setObjectName( pIt->first.c_str( ) );
520 v_string->setText( pIt->second.second.c_str( ) );
521 QPushButton* v_button = new QPushButton( frame );
522 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
523 v_button->setText( "+" );
525 v_button, SIGNAL( clicked( ) ),
526 this, SLOT( _dlg_AddReal( ) )
528 layout->addWidget( v_string );
529 layout->addWidget( v_button );
533 case Parameters::OpenFileNameList:
535 QFrame* frame = new QFrame( this );
536 QHBoxLayout* layout = new QHBoxLayout( frame );
537 QLineEdit* v_string = new QLineEdit( frame );
538 v_string->setObjectName( pIt->first.c_str( ) );
539 v_string->setMaxLength( std::numeric_limits< int >::max( ) );
540 v_string->setText( pIt->second.second.c_str( ) );
541 QPushButton* v_button = new QPushButton( frame );
542 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
543 v_button->setText( "..." );
545 v_button, SIGNAL( clicked( ) ),
546 this, SLOT( _dlg_OpenMultipleFiles( ) )
548 layout->addWidget( v_string );
549 layout->addWidget( v_button );
553 case Parameters::SaveFileNameList:
555 case Parameters::PathNameList:
557 case Parameters::Choices:
559 QComboBox* v_choices = new QComboBox( this );
560 v_choices->setObjectName( pIt->first.c_str( ) );
562 std::istringstream str0( pIt->second.second );
564 std::getline( str0, choices, '@' );
565 std::istringstream str1( choices );
568 while( std::getline( str1, token, '#' ) )
569 v_choices->insertItem( id++, token.c_str( ) );
578 // Ok, a representation was created
579 if( w_input != NULL )
581 QHBoxLayout* new_layout = new QHBoxLayout( );
582 QLabel* label = new QLabel( this );
583 label->setText( QString( pIt->first.c_str( ) ) );
584 new_layout->addWidget( label );
585 new_layout->addWidget( w_input );
586 this->m_ToolsLayout->addLayout( new_layout );
593 this->_addButtons( );
596 // -------------------------------------------------------------------------
597 void cpPlugins::ParametersQtDialog::
598 _dlg_OpenSingleFile( )
600 auto parameters = this->m_ProcessObject->GetParameters( );
601 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
604 std::string bName = btn->objectName( ).toStdString( );
605 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
606 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
609 std::string param_name = line->objectName( ).toStdString( );
610 if( param_name != "" )
612 std::string param_value = parameters->GetOpenFileName( param_name );
613 if( param_value == "" )
615 QStringList dialog_filters;
616 std::string extensions = parameters->GetAcceptedFileExtensions( param_name );
617 if( extensions != "" )
618 dialog_filters << extensions.c_str( );
619 dialog_filters << "Any file (*)";
621 // Show dialog and check if it was accepted
622 QFileDialog dialog( this );
623 dialog.setFileMode( QFileDialog::ExistingFile );
624 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
625 dialog.setNameFilters( dialog_filters );
626 dialog.setAcceptMode( QFileDialog::AcceptOpen );
628 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
637 // -------------------------------------------------------------------------
638 void cpPlugins::ParametersQtDialog::
639 _dlg_SaveSingleFile( )
641 auto parameters = this->m_ProcessObject->GetParameters( );
642 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
645 std::string bName = btn->objectName( ).toStdString( );
646 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
647 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
650 std::string param_name = line->objectName( ).toStdString( );
651 if( param_name != "" )
653 std::string param_value = parameters->GetSaveFileName( param_name );
654 if( param_value == "" )
656 QStringList dialog_filters;
657 std::string extensions = parameters->GetAcceptedFileExtensions( param_name );
658 if( extensions != "" )
659 dialog_filters << extensions.c_str( );
660 dialog_filters << "Any file (*)";
662 // Show dialog and check if it was accepted
663 QFileDialog dialog( this );
664 dialog.setFileMode( QFileDialog::AnyFile );
665 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
666 dialog.setNameFilters( dialog_filters );
667 dialog.setAcceptMode( QFileDialog::AcceptSave );
669 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
678 // -------------------------------------------------------------------------
679 void cpPlugins::ParametersQtDialog::
680 _dlg_OpenSinglePath( )
682 auto parameters = this->m_ProcessObject->GetParameters( );
683 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
686 std::string bName = btn->objectName( ).toStdString( );
687 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
688 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
691 std::string param_name = line->objectName( ).toStdString( );
692 if( param_name != "" )
694 std::string param_value = parameters->GetPathName( param_name );
695 if( param_value == "" )
698 // Show dialog and check if it was accepted
699 QFileDialog dialog( this );
700 dialog.setFileMode( QFileDialog::Directory );
701 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
702 dialog.setAcceptMode( QFileDialog::AcceptOpen );
704 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
713 // -------------------------------------------------------------------------
714 void cpPlugins::ParametersQtDialog::
715 _dlg_OpenMultipleFiles( )
717 auto parameters = this->m_ProcessObject->GetParameters( );
718 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
721 std::string bName = btn->objectName( ).toStdString( );
722 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
724 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
727 std::string param_name = line->objectName( ).toStdString( );
728 if( param_name != "" )
730 QStringList dialog_filters;
731 std::string extensions = parameters->GetAcceptedFileExtensions( param_name );
732 if( extensions != "" )
733 dialog_filters << extensions.c_str( );
734 dialog_filters << "Any file (*)";
736 // Show dialog and check if it was accepted
737 QFileDialog dialog( this );
738 dialog.setFileMode( QFileDialog::ExistingFiles );
739 dialog.setNameFilters( dialog_filters );
740 dialog.setAcceptMode( QFileDialog::AcceptOpen );
743 if( dialog.selectedFiles( ).size( ) > 0 )
745 std::stringstream str;
746 auto files = dialog.selectedFiles( );
747 auto fIt = files.begin( );
748 str << fIt->toStdString( );
750 for( ; fIt != files.end( ); ++fIt )
751 str << "#" << fIt->toStdString( );
752 line->setText( str.str( ).c_str( ) );
765 // -------------------------------------------------------------------------
766 void cpPlugins::ParametersQtDialog::
769 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
772 std::string bName = btn->objectName( ).toStdString( );
773 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
774 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
777 std::string param_name = line->objectName( ).toStdString( );
778 if( param_name != "" )
782 QInputDialog::getInt(
785 std::string( "Add new value to \"" ) +
791 -std::numeric_limits< int >::max( ),
792 std::numeric_limits< int >::max( ),
797 std::string values = line->text( ).toStdString( );
800 std::stringstream str;
801 str << values << value;
802 line->setText( str.str( ).c_str( ) );
813 // -------------------------------------------------------------------------
814 void cpPlugins::ParametersQtDialog::
817 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
820 std::string bName = btn->objectName( ).toStdString( );
821 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
822 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
825 std::string param_name = line->objectName( ).toStdString( );
826 if( param_name != "" )
830 QInputDialog::getInt(
833 std::string( "Add new value to \"" ) +
838 0, 0, std::numeric_limits< int >::max( ), 1,
843 std::string values = line->text( ).toStdString( );
846 std::stringstream str;
847 str << values << value;
848 line->setText( str.str( ).c_str( ) );
859 // -------------------------------------------------------------------------
860 void cpPlugins::ParametersQtDialog::
863 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
866 std::string bName = btn->objectName( ).toStdString( );
867 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
868 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
871 std::string param_name = line->objectName( ).toStdString( );
872 if( param_name != "" )
876 QInputDialog::getDouble(
879 std::string( "Add new value to \"" ) +
885 -std::numeric_limits< double >::max( ),
886 std::numeric_limits< double >::max( ),
891 std::string values = line->text( ).toStdString( );
894 std::stringstream str;
895 str << values << value;
896 line->setText( str.str( ).c_str( ) );
907 #endif // cpPlugins_QT4