1 #include <cpPlugins/ParametersQDialog.h>
7 #include <cpPlugins/ProcessObject.h>
11 #include <QDoubleSpinBox>
12 #include <QFileDialog>
13 #include <QHBoxLayout>
14 #include <QInputDialog>
16 #include <QPushButton>
19 // -------------------------------------------------------------------------
20 cpPlugins::ParametersQDialog::
21 ParametersQDialog( 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 );
38 this, SIGNAL( accepted( ) ), this, SLOT( _dlg_Accepted( ) )
42 // -------------------------------------------------------------------------
43 cpPlugins::ParametersQDialog::
49 delete this->m_ToolsLayout;
50 delete this->m_MainLayout;
55 // -------------------------------------------------------------------------
57 ProcessObject* cpPlugins::ParametersQDialog::
58 getProcessObject( ) const
60 return( this->m_ProcessObject );
63 // -------------------------------------------------------------------------
64 bool cpPlugins::ParametersQDialog::
65 setProcessObject( ProcessObject* obj )
67 if( this->m_ProcessObject != NULL || obj == NULL )
69 this->m_ProcessObject = obj;
70 this->m_WidgetsUpdated = false;
71 this->_updateWidgets( );
76 // -------------------------------------------------------------------------
77 void cpPlugins::ParametersQDialog::
80 if( this->m_ProcessObject == NULL )
85 // Check if an explicit re-execution button is needed
86 if( this->m_ProcessObject->GetCouldHaveExplicitReExecution( ) )
89 this->findChild< QCheckBox* >( "___ExplicitReExecution___" );
91 this->m_ProcessObject->SetExplicitReExecution( v_bool->isChecked( ) );
96 auto parameters = this->m_ProcessObject->GetParameters( );
97 auto& raw_params = parameters->GetRawParameters( );
98 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
100 QString pName = pIt->first.c_str( );
101 switch( pIt->second.first )
103 case Parameters::String:
104 case Parameters::OpenFileName:
105 case Parameters::SaveFileName:
106 case Parameters::PathName:
107 case Parameters::IntList:
108 case Parameters::UintList:
109 case Parameters::RealList:
110 case Parameters::OpenFileNameList:
112 QLineEdit* v_string = this->findChild< QLineEdit* >( pName );
113 if( v_string != NULL )
114 pIt->second.second = v_string->text( ).toStdString( );
117 case Parameters::Bool:
119 QCheckBox* v_bool = this->findChild< QCheckBox* >( pName );
121 pIt->second.second = ( v_bool->isChecked( ) )? "1": "0";
124 case Parameters::Int:
125 case Parameters::Uint:
127 QSpinBox* v_uint = this->findChild< QSpinBox* >( pName );
130 std::stringstream str;
131 str << v_uint->value( );
132 pIt->second.second = str.str( );
137 case Parameters::Real:
139 QDoubleSpinBox* v_double = this->findChild< QDoubleSpinBox* >( pName );
142 std::stringstream str;
143 str << v_double->value( );
144 pIt->second.second = str.str( );
149 case Parameters::StringList:
151 case Parameters::BoolList:
153 case Parameters::SaveFileNameList:
155 case Parameters::PathNameList:
157 case Parameters::Choices:
159 QComboBox* v_choices = this->findChild< QComboBox* >( pName );
160 if( v_choices != NULL )
162 std::istringstream str_choices( pIt->second.second );
163 std::string real_choices;
164 std::getline( str_choices, real_choices, '@' );
167 v_choices->currentText( ).toStdString( );
177 this->m_ProcessObject->Modified( );
180 // -------------------------------------------------------------------------
181 void cpPlugins::ParametersQDialog::
184 if( this->m_ProcessObject == NULL )
189 // Check if an explicit re-execution button is needed
190 if( this->m_ProcessObject->GetCouldHaveExplicitReExecution( ) )
193 this->findChild< QCheckBox* >( "___ExplicitReExecution___" );
195 v_bool->setChecked( this->m_ProcessObject->GetExplicitReExecution( ) );
200 auto parameters = this->m_ProcessObject->GetParameters( );
201 auto& raw_params = parameters->GetRawParameters( );
202 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
204 QString pName = pIt->first.c_str( );
205 switch( pIt->second.first )
207 case Parameters::String:
208 case Parameters::OpenFileName:
209 case Parameters::SaveFileName:
210 case Parameters::PathName:
211 case Parameters::IntList:
212 case Parameters::UintList:
213 case Parameters::RealList:
214 case Parameters::OpenFileNameList:
216 QLineEdit* v_string = this->findChild< QLineEdit* >( pName );
217 if( v_string != NULL )
218 v_string->setText( pIt->second.second.c_str( ) );
221 case Parameters::Bool:
223 QCheckBox* v_bool = this->findChild< QCheckBox* >( pName );
225 v_bool->setChecked( pIt->second.second == "1" );
228 case Parameters::Int:
229 case Parameters::Uint:
231 QSpinBox* v_uint = this->findChild< QSpinBox* >( pName );
234 std::istringstream tok_str( pIt->second.second );
237 v_uint->setValue( v );
242 case Parameters::Real:
244 QDoubleSpinBox* v_double = this->findChild< QDoubleSpinBox* >( pName );
247 std::istringstream tok_str( pIt->second.second );
250 v_double->setValue( v );
255 case Parameters::StringList:
257 case Parameters::BoolList:
259 case Parameters::SaveFileNameList:
261 case Parameters::PathNameList:
263 case Parameters::Choices:
265 QComboBox* v_choices = this->findChild< QComboBox* >( pName );
266 if( v_choices != NULL )
268 std::istringstream str_choices( pIt->second.second );
269 std::string choices, real_choice;
270 std::getline( str_choices, choices, '@' );
271 std::getline( str_choices, real_choice, '@' );
272 std::istringstream str( choices );
274 int id = -1, cont = 0;
275 while( std::getline( str, token, '#' ) )
277 if( token == real_choice )
284 v_choices->setCurrentIndex( id );
296 // -------------------------------------------------------------------------
297 void cpPlugins::ParametersQDialog::
304 this->m_Buttons = new QDialogButtonBox(
305 QDialogButtonBox::Ok | QDialogButtonBox::Cancel
308 this->m_Buttons, SIGNAL( accepted( ) ), this, SLOT( accept( ) )
311 this->m_Buttons, SIGNAL( rejected( ) ), this, SLOT( reject( ) )
313 this->m_ToolsLayout->addWidget( this->m_Buttons );
316 this->m_WidgetsUpdated = true;
319 // -------------------------------------------------------------------------
320 void cpPlugins::ParametersQDialog::
323 if( this->m_WidgetsUpdated || this->m_ProcessObject == NULL )
329 std::stringstream title;
331 << "Parameters for an object of class \""
332 << this->m_ProcessObject->GetClassName( )
334 this->m_Title->setText( title.str( ).c_str( ) );
336 // Check if an explicit re-execution button is needed
337 if( this->m_ProcessObject->GetCouldHaveExplicitReExecution( ) )
339 QCheckBox* v_bool = new QCheckBox( this );
340 v_bool->setObjectName( "___ExplicitReExecution___" );
341 v_bool->setText( "Explicit re-execution" );
342 v_bool->setChecked( this->m_ProcessObject->GetExplicitReExecution( ) );
344 QHBoxLayout* new_layout = new QHBoxLayout( );
345 new_layout->addWidget( v_bool );
346 this->m_ToolsLayout->addLayout( new_layout );
351 auto parameters = this->m_ProcessObject->GetParameters( );
352 auto& raw_params = parameters->GetRawParameters( );
353 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
355 QWidget* w_input = NULL;
356 switch( pIt->second.first )
358 case Parameters::String:
360 QLineEdit* v_string = new QLineEdit( this );
361 v_string->setObjectName( pIt->first.c_str( ) );
362 v_string->setText( pIt->second.second.c_str( ) );
366 case Parameters::Bool:
368 QCheckBox* v_bool = new QCheckBox( this );
369 v_bool->setObjectName( pIt->first.c_str( ) );
370 v_bool->setText( "[ON/OFF]" );
371 v_bool->setChecked( pIt->second.second == "1" );
375 case Parameters::Int:
376 case Parameters::Uint:
378 std::istringstream tok_str( pIt->second.second );
381 QSpinBox* v_uint = new QSpinBox( this );
382 v_uint->setObjectName( pIt->first.c_str( ) );
383 if( pIt->second.first == Parameters::Uint )
384 v_uint->setMinimum( 0 );
386 v_uint->setMinimum( -std::numeric_limits< int >::max( ) );
387 v_uint->setMaximum( std::numeric_limits< int >::max( ) );
388 v_uint->setValue( v );
392 case Parameters::Real:
394 std::istringstream tok_str( pIt->second.second );
397 QDoubleSpinBox* v_double = new QDoubleSpinBox( this );
398 v_double->setObjectName( pIt->first.c_str( ) );
399 v_double->setDecimals( 3 );
400 v_double->setMinimum( -std::numeric_limits< double >::max( ) );
401 v_double->setMaximum( std::numeric_limits< double >::max( ) );
402 v_double->setValue( v );
406 case Parameters::OpenFileName:
408 QFrame* frame = new QFrame( this );
409 QHBoxLayout* layout = new QHBoxLayout( frame );
410 QLineEdit* v_string = new QLineEdit( frame );
411 v_string->setObjectName( pIt->first.c_str( ) );
412 v_string->setText( pIt->second.second.c_str( ) );
413 QPushButton* v_button = new QPushButton( frame );
414 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
415 v_button->setText( "..." );
417 v_button, SIGNAL( clicked( ) ),
418 this, SLOT( _dlg_OpenSingleFile( ) )
420 layout->addWidget( v_string );
421 layout->addWidget( v_button );
424 case Parameters::SaveFileName:
426 QFrame* frame = new QFrame( this );
427 QHBoxLayout* layout = new QHBoxLayout( frame );
428 QLineEdit* v_string = new QLineEdit( frame );
429 v_string->setObjectName( pIt->first.c_str( ) );
430 v_string->setText( pIt->second.second.c_str( ) );
431 QPushButton* v_button = new QPushButton( frame );
432 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
433 v_button->setText( "..." );
435 v_button, SIGNAL( clicked( ) ),
436 this, SLOT( _dlg_SaveSingleFile( ) )
438 layout->addWidget( v_string );
439 layout->addWidget( v_button );
443 case Parameters::PathName:
445 QFrame* frame = new QFrame( this );
446 QHBoxLayout* layout = new QHBoxLayout( frame );
447 QLineEdit* v_string = new QLineEdit( frame );
448 v_string->setObjectName( pIt->first.c_str( ) );
449 v_string->setText( pIt->second.second.c_str( ) );
450 QPushButton* v_button = new QPushButton( frame );
451 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
452 v_button->setText( "..." );
454 v_button, SIGNAL( clicked( ) ),
455 this, SLOT( _dlg_OpenSinglePath( ) )
457 layout->addWidget( v_string );
458 layout->addWidget( v_button );
462 case Parameters::StringList:
464 case Parameters::BoolList:
466 case Parameters::IntList:
468 QFrame* frame = new QFrame( this );
469 QHBoxLayout* layout = new QHBoxLayout( frame );
470 QLineEdit* v_string = new QLineEdit( frame );
471 v_string->setObjectName( pIt->first.c_str( ) );
472 v_string->setText( pIt->second.second.c_str( ) );
473 QPushButton* v_button = new QPushButton( frame );
474 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
475 v_button->setText( "+" );
477 v_button, SIGNAL( clicked( ) ),
478 this, SLOT( _dlg_AddInt( ) )
480 layout->addWidget( v_string );
481 layout->addWidget( v_button );
485 case Parameters::UintList:
487 QFrame* frame = new QFrame( this );
488 QHBoxLayout* layout = new QHBoxLayout( frame );
489 QLineEdit* v_string = new QLineEdit( frame );
490 v_string->setObjectName( pIt->first.c_str( ) );
491 v_string->setText( pIt->second.second.c_str( ) );
492 QPushButton* v_button = new QPushButton( frame );
493 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
494 v_button->setText( "+" );
496 v_button, SIGNAL( clicked( ) ),
497 this, SLOT( _dlg_AddUint( ) )
499 layout->addWidget( v_string );
500 layout->addWidget( v_button );
504 case Parameters::RealList:
506 QFrame* frame = new QFrame( this );
507 QHBoxLayout* layout = new QHBoxLayout( frame );
508 QLineEdit* v_string = new QLineEdit( frame );
509 v_string->setObjectName( pIt->first.c_str( ) );
510 v_string->setText( pIt->second.second.c_str( ) );
511 QPushButton* v_button = new QPushButton( frame );
512 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
513 v_button->setText( "+" );
515 v_button, SIGNAL( clicked( ) ),
516 this, SLOT( _dlg_AddReal( ) )
518 layout->addWidget( v_string );
519 layout->addWidget( v_button );
523 case Parameters::OpenFileNameList:
525 QFrame* frame = new QFrame( this );
526 QHBoxLayout* layout = new QHBoxLayout( frame );
527 QLineEdit* v_string = new QLineEdit( frame );
528 v_string->setObjectName( pIt->first.c_str( ) );
529 v_string->setMaxLength( std::numeric_limits< int >::max( ) );
530 v_string->setText( pIt->second.second.c_str( ) );
531 QPushButton* v_button = new QPushButton( frame );
532 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
533 v_button->setText( "..." );
535 v_button, SIGNAL( clicked( ) ),
536 this, SLOT( _dlg_OpenMultipleFiles( ) )
538 layout->addWidget( v_string );
539 layout->addWidget( v_button );
543 case Parameters::SaveFileNameList:
545 case Parameters::PathNameList:
547 case Parameters::Choices:
549 QComboBox* v_choices = new QComboBox( this );
550 v_choices->setObjectName( pIt->first.c_str( ) );
552 std::istringstream str0( pIt->second.second );
554 std::getline( str0, choices, '@' );
555 std::istringstream str1( choices );
558 while( std::getline( str1, token, '#' ) )
559 v_choices->insertItem( id++, token.c_str( ) );
568 // Ok, a representation was created
569 if( w_input != NULL )
571 QHBoxLayout* new_layout = new QHBoxLayout( );
572 QLabel* label = new QLabel( this );
573 label->setText( QString( pIt->first.c_str( ) ) );
574 new_layout->addWidget( label );
575 new_layout->addWidget( w_input );
576 this->m_ToolsLayout->addLayout( new_layout );
583 this->_addButtons( );
586 // -------------------------------------------------------------------------
587 void cpPlugins::ParametersQDialog::
590 this->updateParameters( );
593 // -------------------------------------------------------------------------
594 void cpPlugins::ParametersQDialog::
595 _dlg_OpenSingleFile( )
597 auto parameters = this->m_ProcessObject->GetParameters( );
598 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
601 std::string bName = btn->objectName( ).toStdString( );
602 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
603 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
606 std::string param_name = line->objectName( ).toStdString( );
607 if( param_name != "" )
609 std::string param_value = parameters->GetOpenFileName( param_name );
610 if( param_value == "" )
612 QStringList dialog_filters;
613 std::string extensions = parameters->GetAcceptedFileExtensions( param_name );
614 if( extensions != "" )
615 dialog_filters << extensions.c_str( );
616 dialog_filters << "Any file (*)";
618 // Show dialog and check if it was accepted
619 QFileDialog dialog( this );
620 dialog.setFileMode( QFileDialog::ExistingFile );
621 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
622 dialog.setNameFilters( dialog_filters );
623 dialog.setAcceptMode( QFileDialog::AcceptOpen );
625 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
634 // -------------------------------------------------------------------------
635 void cpPlugins::ParametersQDialog::
636 _dlg_SaveSingleFile( )
638 auto parameters = this->m_ProcessObject->GetParameters( );
639 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
642 std::string bName = btn->objectName( ).toStdString( );
643 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
644 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
647 std::string param_name = line->objectName( ).toStdString( );
648 if( param_name != "" )
650 std::string param_value = parameters->GetSaveFileName( param_name );
651 if( param_value == "" )
653 QStringList dialog_filters;
654 std::string extensions = parameters->GetAcceptedFileExtensions( param_name );
655 if( extensions != "" )
656 dialog_filters << extensions.c_str( );
657 dialog_filters << "Any file (*)";
659 // Show dialog and check if it was accepted
660 QFileDialog dialog( this );
661 dialog.setFileMode( QFileDialog::AnyFile );
662 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
663 dialog.setNameFilters( dialog_filters );
664 dialog.setAcceptMode( QFileDialog::AcceptSave );
666 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
675 // -------------------------------------------------------------------------
676 void cpPlugins::ParametersQDialog::
677 _dlg_OpenSinglePath( )
679 auto parameters = this->m_ProcessObject->GetParameters( );
680 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
683 std::string bName = btn->objectName( ).toStdString( );
684 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
685 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
688 std::string param_name = line->objectName( ).toStdString( );
689 if( param_name != "" )
691 std::string param_value = parameters->GetPathName( param_name );
692 if( param_value == "" )
695 // Show dialog and check if it was accepted
696 QFileDialog dialog( this );
697 dialog.setFileMode( QFileDialog::Directory );
698 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
699 dialog.setAcceptMode( QFileDialog::AcceptOpen );
701 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
710 // -------------------------------------------------------------------------
711 void cpPlugins::ParametersQDialog::
712 _dlg_OpenMultipleFiles( )
714 auto parameters = this->m_ProcessObject->GetParameters( );
715 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
718 std::string bName = btn->objectName( ).toStdString( );
719 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
721 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
724 std::string param_name = line->objectName( ).toStdString( );
725 if( param_name != "" )
727 QStringList dialog_filters;
728 std::string extensions = parameters->GetAcceptedFileExtensions( param_name );
729 if( extensions != "" )
730 dialog_filters << extensions.c_str( );
731 dialog_filters << "Any file (*)";
733 // Show dialog and check if it was accepted
734 QFileDialog dialog( this );
735 dialog.setFileMode( QFileDialog::ExistingFiles );
736 dialog.setNameFilters( dialog_filters );
737 dialog.setAcceptMode( QFileDialog::AcceptOpen );
740 if( dialog.selectedFiles( ).size( ) > 0 )
742 std::stringstream str;
743 auto files = dialog.selectedFiles( );
744 auto fIt = files.begin( );
745 str << fIt->toStdString( );
747 for( ; fIt != files.end( ); ++fIt )
748 str << "#" << fIt->toStdString( );
749 line->setText( str.str( ).c_str( ) );
762 // -------------------------------------------------------------------------
763 void cpPlugins::ParametersQDialog::
766 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
769 std::string bName = btn->objectName( ).toStdString( );
770 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
771 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
774 std::string param_name = line->objectName( ).toStdString( );
775 if( param_name != "" )
779 QInputDialog::getInt(
782 std::string( "Add new value to \"" ) +
788 -std::numeric_limits< int >::max( ),
789 std::numeric_limits< int >::max( ),
794 std::string values = line->text( ).toStdString( );
797 std::stringstream str;
798 str << values << value;
799 line->setText( str.str( ).c_str( ) );
810 // -------------------------------------------------------------------------
811 void cpPlugins::ParametersQDialog::
814 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
817 std::string bName = btn->objectName( ).toStdString( );
818 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
819 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
822 std::string param_name = line->objectName( ).toStdString( );
823 if( param_name != "" )
827 QInputDialog::getInt(
830 std::string( "Add new value to \"" ) +
835 0, 0, std::numeric_limits< int >::max( ), 1,
840 std::string values = line->text( ).toStdString( );
843 std::stringstream str;
844 str << values << value;
845 line->setText( str.str( ).c_str( ) );
856 // -------------------------------------------------------------------------
857 void cpPlugins::ParametersQDialog::
860 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
863 std::string bName = btn->objectName( ).toStdString( );
864 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
865 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
868 std::string param_name = line->objectName( ).toStdString( );
869 if( param_name != "" )
873 QInputDialog::getDouble(
876 std::string( "Add new value to \"" ) +
882 -std::numeric_limits< double >::max( ),
883 std::numeric_limits< double >::max( ),
888 std::string values = line->text( ).toStdString( );
891 std::stringstream str;
892 str << values << value;
893 line->setText( str.str( ).c_str( ) );
904 #endif // cpPlugins_QT4