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 )
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::ParametersQtDialog::
184 if( this->m_ProcessObject == NULL )
190 auto parameters = this->m_ProcessObject->GetParameters( );
191 auto& raw_params = parameters->GetRawParameters( );
192 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
194 QString pName = pIt->first.c_str( );
195 switch( pIt->second.first )
197 case Parameters::String:
198 case Parameters::OpenFileName:
199 case Parameters::SaveFileName:
200 case Parameters::PathName:
201 case Parameters::IntList:
202 case Parameters::UintList:
203 case Parameters::RealList:
204 case Parameters::OpenFileNameList:
206 QLineEdit* v_string = this->findChild< QLineEdit* >( pName );
207 if( v_string != NULL )
208 v_string->setText( pIt->second.second.c_str( ) );
211 case Parameters::Bool:
213 QCheckBox* v_bool = this->findChild< QCheckBox* >( pName );
215 v_bool->setChecked( pIt->second.second == "1" );
218 case Parameters::Int:
219 case Parameters::Uint:
221 QSpinBox* v_uint = this->findChild< QSpinBox* >( pName );
224 std::istringstream tok_str( pIt->second.second );
227 v_uint->setValue( v );
232 case Parameters::Real:
234 QDoubleSpinBox* v_double = this->findChild< QDoubleSpinBox* >( pName );
237 std::istringstream tok_str( pIt->second.second );
240 v_double->setValue( v );
245 case Parameters::StringList:
247 case Parameters::BoolList:
249 case Parameters::SaveFileNameList:
251 case Parameters::PathNameList:
253 case Parameters::Choices:
255 QComboBox* v_choices = this->findChild< QComboBox* >( pName );
256 if( v_choices != NULL )
258 std::istringstream str_choices( pIt->second.second );
259 std::string choices, real_choice;
260 std::getline( str_choices, choices, '@' );
261 std::getline( str_choices, real_choice, '@' );
262 std::istringstream str( choices );
264 int id = -1, cont = 0;
265 while( std::getline( str, token, '#' ) )
267 if( token == real_choice )
274 v_choices->setCurrentIndex( id );
286 // -------------------------------------------------------------------------
287 void cpPlugins::ParametersQtDialog::
294 this->m_Buttons = new QDialogButtonBox(
295 QDialogButtonBox::Ok | QDialogButtonBox::Cancel
298 this->m_Buttons, SIGNAL( accepted( ) ), this, SLOT( accept( ) )
301 this->m_Buttons, SIGNAL( rejected( ) ), this, SLOT( reject( ) )
303 this->m_ToolsLayout->addWidget( this->m_Buttons );
306 this->m_WidgetsUpdated = true;
309 // -------------------------------------------------------------------------
310 void cpPlugins::ParametersQtDialog::
313 if( this->m_WidgetsUpdated || this->m_ProcessObject == NULL )
319 std::stringstream title;
321 << "Parameters for an object of class \""
322 << this->m_ProcessObject->GetClassName( )
324 this->m_Title->setText( title.str( ).c_str( ) );
327 auto parameters = this->m_ProcessObject->GetParameters( );
328 auto& raw_params = parameters->GetRawParameters( );
329 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
331 QWidget* w_input = NULL;
332 switch( pIt->second.first )
334 case Parameters::String:
336 QLineEdit* v_string = new QLineEdit( this );
337 v_string->setObjectName( pIt->first.c_str( ) );
338 v_string->setText( pIt->second.second.c_str( ) );
342 case Parameters::Bool:
344 QCheckBox* v_bool = new QCheckBox( this );
345 v_bool->setObjectName( pIt->first.c_str( ) );
346 v_bool->setText( "[ON/OFF]" );
347 v_bool->setChecked( pIt->second.second == "1" );
351 case Parameters::Int:
352 case Parameters::Uint:
354 std::istringstream tok_str( pIt->second.second );
357 QSpinBox* v_uint = new QSpinBox( this );
358 v_uint->setObjectName( pIt->first.c_str( ) );
359 if( pIt->second.first == Parameters::Uint )
360 v_uint->setMinimum( 0 );
362 v_uint->setMinimum( -std::numeric_limits< int >::max( ) );
363 v_uint->setMaximum( std::numeric_limits< int >::max( ) );
364 v_uint->setValue( v );
368 case Parameters::Real:
370 std::istringstream tok_str( pIt->second.second );
373 QDoubleSpinBox* v_double = new QDoubleSpinBox( this );
374 v_double->setObjectName( pIt->first.c_str( ) );
375 v_double->setDecimals( 3 );
376 v_double->setMinimum( -std::numeric_limits< double >::max( ) );
377 v_double->setMaximum( std::numeric_limits< double >::max( ) );
378 v_double->setValue( v );
382 case Parameters::OpenFileName:
384 QFrame* frame = new QFrame( this );
385 QHBoxLayout* layout = new QHBoxLayout( frame );
386 QLineEdit* v_string = new QLineEdit( frame );
387 v_string->setObjectName( pIt->first.c_str( ) );
388 v_string->setText( pIt->second.second.c_str( ) );
389 QPushButton* v_button = new QPushButton( frame );
390 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
391 v_button->setText( "..." );
393 v_button, SIGNAL( clicked( ) ),
394 this, SLOT( _dlg_OpenSingleFile( ) )
396 layout->addWidget( v_string );
397 layout->addWidget( v_button );
400 case Parameters::SaveFileName:
402 QFrame* frame = new QFrame( this );
403 QHBoxLayout* layout = new QHBoxLayout( frame );
404 QLineEdit* v_string = new QLineEdit( frame );
405 v_string->setObjectName( pIt->first.c_str( ) );
406 v_string->setText( pIt->second.second.c_str( ) );
407 QPushButton* v_button = new QPushButton( frame );
408 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
409 v_button->setText( "..." );
411 v_button, SIGNAL( clicked( ) ),
412 this, SLOT( _dlg_SaveSingleFile( ) )
414 layout->addWidget( v_string );
415 layout->addWidget( v_button );
419 case Parameters::PathName:
421 QFrame* frame = new QFrame( this );
422 QHBoxLayout* layout = new QHBoxLayout( frame );
423 QLineEdit* v_string = new QLineEdit( frame );
424 v_string->setObjectName( pIt->first.c_str( ) );
425 v_string->setText( pIt->second.second.c_str( ) );
426 QPushButton* v_button = new QPushButton( frame );
427 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
428 v_button->setText( "..." );
430 v_button, SIGNAL( clicked( ) ),
431 this, SLOT( _dlg_OpenSinglePath( ) )
433 layout->addWidget( v_string );
434 layout->addWidget( v_button );
438 case Parameters::StringList:
440 case Parameters::BoolList:
442 case Parameters::IntList:
444 QFrame* frame = new QFrame( this );
445 QHBoxLayout* layout = new QHBoxLayout( frame );
446 QLineEdit* v_string = new QLineEdit( frame );
447 v_string->setObjectName( pIt->first.c_str( ) );
448 v_string->setText( pIt->second.second.c_str( ) );
449 QPushButton* v_button = new QPushButton( frame );
450 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
451 v_button->setText( "+" );
453 v_button, SIGNAL( clicked( ) ),
454 this, SLOT( _dlg_AddInt( ) )
456 layout->addWidget( v_string );
457 layout->addWidget( v_button );
461 case Parameters::UintList:
463 QFrame* frame = new QFrame( this );
464 QHBoxLayout* layout = new QHBoxLayout( frame );
465 QLineEdit* v_string = new QLineEdit( frame );
466 v_string->setObjectName( pIt->first.c_str( ) );
467 v_string->setText( pIt->second.second.c_str( ) );
468 QPushButton* v_button = new QPushButton( frame );
469 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
470 v_button->setText( "+" );
472 v_button, SIGNAL( clicked( ) ),
473 this, SLOT( _dlg_AddUint( ) )
475 layout->addWidget( v_string );
476 layout->addWidget( v_button );
480 case Parameters::RealList:
482 QFrame* frame = new QFrame( this );
483 QHBoxLayout* layout = new QHBoxLayout( frame );
484 QLineEdit* v_string = new QLineEdit( frame );
485 v_string->setObjectName( pIt->first.c_str( ) );
486 v_string->setText( pIt->second.second.c_str( ) );
487 QPushButton* v_button = new QPushButton( frame );
488 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
489 v_button->setText( "+" );
491 v_button, SIGNAL( clicked( ) ),
492 this, SLOT( _dlg_AddReal( ) )
494 layout->addWidget( v_string );
495 layout->addWidget( v_button );
499 case Parameters::OpenFileNameList:
501 QFrame* frame = new QFrame( this );
502 QHBoxLayout* layout = new QHBoxLayout( frame );
503 QLineEdit* v_string = new QLineEdit( frame );
504 v_string->setObjectName( pIt->first.c_str( ) );
505 v_string->setMaxLength( std::numeric_limits< int >::max( ) );
506 v_string->setText( pIt->second.second.c_str( ) );
507 QPushButton* v_button = new QPushButton( frame );
508 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
509 v_button->setText( "..." );
511 v_button, SIGNAL( clicked( ) ),
512 this, SLOT( _dlg_OpenMultipleFiles( ) )
514 layout->addWidget( v_string );
515 layout->addWidget( v_button );
519 case Parameters::SaveFileNameList:
521 case Parameters::PathNameList:
523 case Parameters::Choices:
525 QComboBox* v_choices = new QComboBox( this );
526 v_choices->setObjectName( pIt->first.c_str( ) );
528 std::istringstream str0( pIt->second.second );
530 std::getline( str0, choices, '@' );
531 std::istringstream str1( choices );
534 while( std::getline( str1, token, '#' ) )
535 v_choices->insertItem( id++, token.c_str( ) );
544 // Ok, a representation was created
545 if( w_input != NULL )
547 QHBoxLayout* new_layout = new QHBoxLayout( );
548 QLabel* label = new QLabel( this );
549 label->setText( QString( pIt->first.c_str( ) ) );
550 new_layout->addWidget( label );
551 new_layout->addWidget( w_input );
552 this->m_ToolsLayout->addLayout( new_layout );
559 this->_addButtons( );
562 // -------------------------------------------------------------------------
563 void cpPlugins::ParametersQtDialog::
564 _dlg_OpenSingleFile( )
566 auto parameters = this->m_ProcessObject->GetParameters( );
567 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
570 std::string bName = btn->objectName( ).toStdString( );
571 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
572 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
575 std::string param_name = line->objectName( ).toStdString( );
576 if( param_name != "" )
578 std::string param_value = parameters->GetOpenFileName( param_name );
579 if( param_value == "" )
581 QStringList dialog_filters;
582 std::string extensions = parameters->GetAcceptedFileExtensions( param_name );
583 if( extensions != "" )
584 dialog_filters << extensions.c_str( );
585 dialog_filters << "Any file (*)";
587 // Show dialog and check if it was accepted
588 QFileDialog dialog( this );
589 dialog.setFileMode( QFileDialog::ExistingFile );
590 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
591 dialog.setNameFilters( dialog_filters );
592 dialog.setAcceptMode( QFileDialog::AcceptOpen );
594 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
603 // -------------------------------------------------------------------------
604 void cpPlugins::ParametersQtDialog::
605 _dlg_SaveSingleFile( )
607 auto parameters = this->m_ProcessObject->GetParameters( );
608 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
611 std::string bName = btn->objectName( ).toStdString( );
612 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
613 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
616 std::string param_name = line->objectName( ).toStdString( );
617 if( param_name != "" )
619 std::string param_value = parameters->GetSaveFileName( param_name );
620 if( param_value == "" )
622 QStringList dialog_filters;
623 std::string extensions = parameters->GetAcceptedFileExtensions( param_name );
624 if( extensions != "" )
625 dialog_filters << extensions.c_str( );
626 dialog_filters << "Any file (*)";
628 // Show dialog and check if it was accepted
629 QFileDialog dialog( this );
630 dialog.setFileMode( QFileDialog::AnyFile );
631 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
632 dialog.setNameFilters( dialog_filters );
633 dialog.setAcceptMode( QFileDialog::AcceptSave );
635 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
644 // -------------------------------------------------------------------------
645 void cpPlugins::ParametersQtDialog::
646 _dlg_OpenSinglePath( )
648 auto parameters = this->m_ProcessObject->GetParameters( );
649 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
652 std::string bName = btn->objectName( ).toStdString( );
653 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
654 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
657 std::string param_name = line->objectName( ).toStdString( );
658 if( param_name != "" )
660 std::string param_value = parameters->GetPathName( param_name );
661 if( param_value == "" )
664 // Show dialog and check if it was accepted
665 QFileDialog dialog( this );
666 dialog.setFileMode( QFileDialog::Directory );
667 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
668 dialog.setAcceptMode( QFileDialog::AcceptOpen );
670 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
679 // -------------------------------------------------------------------------
680 void cpPlugins::ParametersQtDialog::
681 _dlg_OpenMultipleFiles( )
683 auto parameters = this->m_ProcessObject->GetParameters( );
684 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
687 std::string bName = btn->objectName( ).toStdString( );
688 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
690 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
693 std::string param_name = line->objectName( ).toStdString( );
694 if( param_name != "" )
696 QStringList dialog_filters;
697 std::string extensions = parameters->GetAcceptedFileExtensions( param_name );
698 if( extensions != "" )
699 dialog_filters << extensions.c_str( );
700 dialog_filters << "Any file (*)";
702 // Show dialog and check if it was accepted
703 QFileDialog dialog( this );
704 dialog.setFileMode( QFileDialog::ExistingFiles );
705 dialog.setNameFilters( dialog_filters );
706 dialog.setAcceptMode( QFileDialog::AcceptOpen );
709 if( dialog.selectedFiles( ).size( ) > 0 )
711 std::stringstream str;
712 auto files = dialog.selectedFiles( );
713 auto fIt = files.begin( );
714 str << fIt->toStdString( );
716 for( ; fIt != files.end( ); ++fIt )
717 str << "#" << fIt->toStdString( );
718 line->setText( str.str( ).c_str( ) );
731 // -------------------------------------------------------------------------
732 void cpPlugins::ParametersQtDialog::
735 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
738 std::string bName = btn->objectName( ).toStdString( );
739 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
740 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
743 std::string param_name = line->objectName( ).toStdString( );
744 if( param_name != "" )
748 QInputDialog::getInt(
751 std::string( "Add new value to \"" ) +
757 -std::numeric_limits< int >::max( ),
758 std::numeric_limits< int >::max( ),
763 std::string values = line->text( ).toStdString( );
766 std::stringstream str;
767 str << values << value;
768 line->setText( str.str( ).c_str( ) );
779 // -------------------------------------------------------------------------
780 void cpPlugins::ParametersQtDialog::
783 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
786 std::string bName = btn->objectName( ).toStdString( );
787 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
788 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
791 std::string param_name = line->objectName( ).toStdString( );
792 if( param_name != "" )
796 QInputDialog::getInt(
799 std::string( "Add new value to \"" ) +
804 0, 0, std::numeric_limits< int >::max( ), 1,
809 std::string values = line->text( ).toStdString( );
812 std::stringstream str;
813 str << values << value;
814 line->setText( str.str( ).c_str( ) );
825 // -------------------------------------------------------------------------
826 void cpPlugins::ParametersQtDialog::
829 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
832 std::string bName = btn->objectName( ).toStdString( );
833 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
834 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
837 std::string param_name = line->objectName( ).toStdString( );
838 if( param_name != "" )
842 QInputDialog::getDouble(
845 std::string( "Add new value to \"" ) +
851 -std::numeric_limits< double >::max( ),
852 std::numeric_limits< double >::max( ),
857 std::string values = line->text( ).toStdString( );
860 std::stringstream str;
861 str << values << value;
862 line->setText( str.str( ).c_str( ) );
873 #endif // cpPlugins_QT4