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 )
22 : QDialog( parent, f ),
23 m_ProcessObject( NULL ),
24 m_WidgetsUpdated( false )
26 this->m_Title = new QLabel( this );
27 this->m_Title->setText( "Parameters dialog title" );
29 this->m_MainLayout = new QGridLayout( this );
30 this->m_ToolsLayout = new QVBoxLayout( );
31 this->m_ToolsLayout->addWidget( this->m_Title );
32 this->m_MainLayout->addLayout( this->m_ToolsLayout, 0, 0, 1, 1 );
35 // -------------------------------------------------------------------------
36 cpPlugins::ParametersQtDialog::
37 ~ParametersQtDialog( )
40 delete this->m_ToolsLayout;
41 delete this->m_MainLayout;
44 // -------------------------------------------------------------------------
46 ProcessObject* cpPlugins::ParametersQtDialog::
47 getProcessObject( ) const
49 return( this->m_ProcessObject );
52 // -------------------------------------------------------------------------
53 bool cpPlugins::ParametersQtDialog::
54 setProcessObject( ProcessObject* obj )
56 if( this->m_ProcessObject != NULL || obj == NULL )
58 this->m_ProcessObject = obj;
59 this->m_WidgetsUpdated = false;
63 // -------------------------------------------------------------------------
64 int cpPlugins::ParametersQtDialog::
67 this->_updateWidgets( );
70 int ret = this->QDialog::exec( );
72 this->updateParameters( );
78 // -------------------------------------------------------------------------
79 void cpPlugins::ParametersQtDialog::
82 if( this->m_ProcessObject == NULL )
86 auto parameters = this->m_ProcessObject->GetParameters( );
87 auto& raw_params = parameters->GetRawParameters( );
88 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
90 QString pName = pIt->first.c_str( );
91 switch( pIt->second.first )
93 case Parameters::String:
94 case Parameters::OpenFileName:
95 case Parameters::SaveFileName:
96 case Parameters::PathName:
97 case Parameters::IntList:
98 case Parameters::UintList:
99 case Parameters::RealList:
100 case Parameters::OpenFileNameList:
102 QLineEdit* v_string = this->findChild< QLineEdit* >( pName );
103 if( v_string != NULL )
104 pIt->second.second = v_string->text( ).toStdString( );
107 case Parameters::Bool:
109 QCheckBox* v_bool = this->findChild< QCheckBox* >( pName );
111 pIt->second.second = ( v_bool->isChecked( ) )? "1": "0";
114 case Parameters::Int:
115 case Parameters::Uint:
117 QSpinBox* v_uint = this->findChild< QSpinBox* >( pName );
120 std::stringstream str;
121 str << v_uint->value( );
122 pIt->second.second = str.str( );
127 case Parameters::Real:
129 QDoubleSpinBox* v_double = this->findChild< QDoubleSpinBox* >( pName );
132 std::stringstream str;
133 str << v_double->value( );
134 pIt->second.second = str.str( );
139 case Parameters::StringList:
141 case Parameters::BoolList:
143 case Parameters::SaveFileNameList:
145 case Parameters::PathNameList:
147 case Parameters::Choices:
149 QComboBox* v_choices = this->findChild< QComboBox* >( pName );
150 if( v_choices != NULL )
152 std::istringstream str_choices( pIt->second.second );
153 std::string real_choices;
154 std::getline( str_choices, real_choices, '@' );
157 v_choices->currentText( ).toStdString( );
167 this->m_ProcessObject->Modified( );
170 // -------------------------------------------------------------------------
171 void cpPlugins::ParametersQtDialog::
174 if( this->m_ProcessObject == NULL )
178 auto parameters = this->m_ProcessObject->GetParameters( );
179 auto& raw_params = parameters->GetRawParameters( );
180 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
182 QString pName = pIt->first.c_str( );
183 switch( pIt->second.first )
185 case Parameters::String:
186 case Parameters::OpenFileName:
187 case Parameters::SaveFileName:
188 case Parameters::PathName:
189 case Parameters::IntList:
190 case Parameters::UintList:
191 case Parameters::RealList:
192 case Parameters::OpenFileNameList:
194 QLineEdit* v_string = this->findChild< QLineEdit* >( pName );
195 if( v_string != NULL )
196 v_string->setText( pIt->second.second.c_str( ) );
199 case Parameters::Bool:
201 QCheckBox* v_bool = this->findChild< QCheckBox* >( pName );
203 v_bool->setChecked( pIt->second.second == "1" );
206 case Parameters::Int:
207 case Parameters::Uint:
209 QSpinBox* v_uint = this->findChild< QSpinBox* >( pName );
212 std::istringstream tok_str( pIt->second.second );
215 v_uint->setValue( v );
220 case Parameters::Real:
222 QDoubleSpinBox* v_double = this->findChild< QDoubleSpinBox* >( pName );
225 std::istringstream tok_str( pIt->second.second );
228 v_double->setValue( v );
233 case Parameters::StringList:
235 case Parameters::BoolList:
237 case Parameters::SaveFileNameList:
239 case Parameters::PathNameList:
241 case Parameters::Choices:
243 QComboBox* v_choices = this->findChild< QComboBox* >( pName );
244 if( v_choices != NULL )
246 std::istringstream str_choices( pIt->second.second );
247 std::string choices, real_choice;
248 std::getline( str_choices, choices, '@' );
249 std::getline( str_choices, real_choice, '@' );
250 std::istringstream str( choices );
252 int id = -1, cont = 0;
253 while( std::getline( str, token, '#' ) )
255 if( token == real_choice )
262 v_choices->setCurrentIndex( id );
274 // -------------------------------------------------------------------------
275 void cpPlugins::ParametersQtDialog::
279 this->m_Buttons = new QDialogButtonBox(
280 QDialogButtonBox::Ok | QDialogButtonBox::Cancel
283 this->m_Buttons, SIGNAL( accepted( ) ), this, SLOT( accept( ) )
286 this->m_Buttons, SIGNAL( rejected( ) ), this, SLOT( reject( ) )
288 this->m_ToolsLayout->addWidget( this->m_Buttons );
291 this->m_WidgetsUpdated = true;
294 // -------------------------------------------------------------------------
295 void cpPlugins::ParametersQtDialog::
298 if( this->m_WidgetsUpdated || this->m_ProcessObject == NULL )
302 std::stringstream title;
304 << "Parameters for an object of class \""
305 << this->m_ProcessObject->GetClassName( )
307 this->m_Title->setText( title.str( ).c_str( ) );
310 auto parameters = this->m_ProcessObject->GetParameters( );
311 auto& raw_params = parameters->GetRawParameters( );
312 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
314 QWidget* w_input = NULL;
315 switch( pIt->second.first )
317 case Parameters::String:
319 QLineEdit* v_string = new QLineEdit( this );
320 v_string->setObjectName( pIt->first.c_str( ) );
321 v_string->setText( pIt->second.second.c_str( ) );
325 case Parameters::Bool:
327 QCheckBox* v_bool = new QCheckBox( this );
328 v_bool->setObjectName( pIt->first.c_str( ) );
329 v_bool->setText( "[ON/OFF]" );
330 v_bool->setChecked( pIt->second.second == "1" );
334 case Parameters::Int:
335 case Parameters::Uint:
337 std::istringstream tok_str( pIt->second.second );
340 QSpinBox* v_uint = new QSpinBox( this );
341 v_uint->setObjectName( pIt->first.c_str( ) );
342 if( pIt->second.first == Parameters::Uint )
343 v_uint->setMinimum( 0 );
345 v_uint->setMinimum( -std::numeric_limits< int >::max( ) );
346 v_uint->setMaximum( std::numeric_limits< int >::max( ) );
347 v_uint->setValue( v );
351 case Parameters::Real:
353 std::istringstream tok_str( pIt->second.second );
356 QDoubleSpinBox* v_double = new QDoubleSpinBox( this );
357 v_double->setObjectName( pIt->first.c_str( ) );
358 v_double->setDecimals( 3 );
359 v_double->setMinimum( -std::numeric_limits< double >::max( ) );
360 v_double->setMaximum( std::numeric_limits< double >::max( ) );
361 v_double->setValue( v );
365 case Parameters::OpenFileName:
367 QFrame* frame = new QFrame( this );
368 QHBoxLayout* layout = new QHBoxLayout( frame );
369 QLineEdit* v_string = new QLineEdit( frame );
370 v_string->setObjectName( pIt->first.c_str( ) );
371 v_string->setText( pIt->second.second.c_str( ) );
372 QPushButton* v_button = new QPushButton( frame );
373 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
374 v_button->setText( "..." );
376 v_button, SIGNAL( clicked( ) ),
377 this, SLOT( _dlg_OpenSingleFile( ) )
379 layout->addWidget( v_string );
380 layout->addWidget( v_button );
383 case Parameters::SaveFileName:
385 QFrame* frame = new QFrame( this );
386 QHBoxLayout* layout = new QHBoxLayout( frame );
387 QLineEdit* v_string = new QLineEdit( frame );
388 v_string->setObjectName( pIt->first.c_str( ) );
389 v_string->setText( pIt->second.second.c_str( ) );
390 QPushButton* v_button = new QPushButton( frame );
391 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
392 v_button->setText( "..." );
394 v_button, SIGNAL( clicked( ) ),
395 this, SLOT( _dlg_SaveSingleFile( ) )
397 layout->addWidget( v_string );
398 layout->addWidget( v_button );
402 case Parameters::PathName:
404 QFrame* frame = new QFrame( this );
405 QHBoxLayout* layout = new QHBoxLayout( frame );
406 QLineEdit* v_string = new QLineEdit( frame );
407 v_string->setObjectName( pIt->first.c_str( ) );
408 v_string->setText( pIt->second.second.c_str( ) );
409 QPushButton* v_button = new QPushButton( frame );
410 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
411 v_button->setText( "..." );
413 v_button, SIGNAL( clicked( ) ),
414 this, SLOT( _dlg_OpenSinglePath( ) )
416 layout->addWidget( v_string );
417 layout->addWidget( v_button );
421 case Parameters::StringList:
423 case Parameters::BoolList:
425 case Parameters::IntList:
427 QFrame* frame = new QFrame( this );
428 QHBoxLayout* layout = new QHBoxLayout( frame );
429 QLineEdit* v_string = new QLineEdit( frame );
430 v_string->setObjectName( pIt->first.c_str( ) );
431 v_string->setText( pIt->second.second.c_str( ) );
432 QPushButton* v_button = new QPushButton( frame );
433 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
434 v_button->setText( "+" );
436 v_button, SIGNAL( clicked( ) ),
437 this, SLOT( _dlg_AddInt( ) )
439 layout->addWidget( v_string );
440 layout->addWidget( v_button );
444 case Parameters::UintList:
446 QFrame* frame = new QFrame( this );
447 QHBoxLayout* layout = new QHBoxLayout( frame );
448 QLineEdit* v_string = new QLineEdit( frame );
449 v_string->setObjectName( pIt->first.c_str( ) );
450 v_string->setText( pIt->second.second.c_str( ) );
451 QPushButton* v_button = new QPushButton( frame );
452 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
453 v_button->setText( "+" );
455 v_button, SIGNAL( clicked( ) ),
456 this, SLOT( _dlg_AddUint( ) )
458 layout->addWidget( v_string );
459 layout->addWidget( v_button );
463 case Parameters::RealList:
465 QFrame* frame = new QFrame( this );
466 QHBoxLayout* layout = new QHBoxLayout( frame );
467 QLineEdit* v_string = new QLineEdit( frame );
468 v_string->setObjectName( pIt->first.c_str( ) );
469 v_string->setText( pIt->second.second.c_str( ) );
470 QPushButton* v_button = new QPushButton( frame );
471 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
472 v_button->setText( "+" );
474 v_button, SIGNAL( clicked( ) ),
475 this, SLOT( _dlg_AddReal( ) )
477 layout->addWidget( v_string );
478 layout->addWidget( v_button );
482 case Parameters::OpenFileNameList:
484 QFrame* frame = new QFrame( this );
485 QHBoxLayout* layout = new QHBoxLayout( frame );
486 QLineEdit* v_string = new QLineEdit( frame );
487 v_string->setObjectName( pIt->first.c_str( ) );
488 v_string->setMaxLength( std::numeric_limits< int >::max( ) );
489 v_string->setText( pIt->second.second.c_str( ) );
490 QPushButton* v_button = new QPushButton( frame );
491 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
492 v_button->setText( "..." );
494 v_button, SIGNAL( clicked( ) ),
495 this, SLOT( _dlg_OpenMultipleFiles( ) )
497 layout->addWidget( v_string );
498 layout->addWidget( v_button );
502 case Parameters::SaveFileNameList:
504 case Parameters::PathNameList:
506 case Parameters::Choices:
508 QComboBox* v_choices = new QComboBox( this );
509 v_choices->setObjectName( pIt->first.c_str( ) );
511 std::istringstream str0( pIt->second.second );
513 std::getline( str0, choices, '@' );
514 std::istringstream str1( choices );
517 while( std::getline( str1, token, '#' ) )
518 v_choices->insertItem( id++, token.c_str( ) );
527 // Ok, a representation was created
528 if( w_input != NULL )
530 QHBoxLayout* new_layout = new QHBoxLayout( );
531 QLabel* label = new QLabel( this );
532 label->setText( QString( pIt->first.c_str( ) ) );
533 new_layout->addWidget( label );
534 new_layout->addWidget( w_input );
535 this->m_ToolsLayout->addLayout( new_layout );
542 this->_addButtons( );
545 // -------------------------------------------------------------------------
546 void cpPlugins::ParametersQtDialog::
547 _dlg_OpenSingleFile( )
549 auto parameters = this->m_ProcessObject->GetParameters( );
550 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
553 std::string bName = btn->objectName( ).toStdString( );
554 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
555 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
558 std::string param_name = line->objectName( ).toStdString( );
559 if( param_name != "" )
561 std::string param_value = parameters->GetOpenFileName( param_name );
562 if( param_value == "" )
564 QStringList dialog_filters;
565 std::string extensions = parameters->GetAcceptedFileExtensions( param_name );
566 if( extensions != "" )
567 dialog_filters << extensions.c_str( );
568 dialog_filters << "Any file (*)";
570 // Show dialog and check if it was accepted
571 QFileDialog dialog( this );
572 dialog.setFileMode( QFileDialog::ExistingFile );
573 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
574 dialog.setNameFilters( dialog_filters );
575 dialog.setAcceptMode( QFileDialog::AcceptOpen );
577 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
586 // -------------------------------------------------------------------------
587 void cpPlugins::ParametersQtDialog::
588 _dlg_SaveSingleFile( )
590 auto parameters = this->m_ProcessObject->GetParameters( );
591 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
594 std::string bName = btn->objectName( ).toStdString( );
595 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
596 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
599 std::string param_name = line->objectName( ).toStdString( );
600 if( param_name != "" )
602 std::string param_value = parameters->GetSaveFileName( param_name );
603 if( param_value == "" )
605 QStringList dialog_filters;
606 std::string extensions = parameters->GetAcceptedFileExtensions( param_name );
607 if( extensions != "" )
608 dialog_filters << extensions.c_str( );
609 dialog_filters << "Any file (*)";
611 // Show dialog and check if it was accepted
612 QFileDialog dialog( this );
613 dialog.setFileMode( QFileDialog::AnyFile );
614 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
615 dialog.setNameFilters( dialog_filters );
616 dialog.setAcceptMode( QFileDialog::AcceptSave );
618 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
627 // -------------------------------------------------------------------------
628 void cpPlugins::ParametersQtDialog::
629 _dlg_OpenSinglePath( )
631 auto parameters = this->m_ProcessObject->GetParameters( );
632 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
635 std::string bName = btn->objectName( ).toStdString( );
636 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
637 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
640 std::string param_name = line->objectName( ).toStdString( );
641 if( param_name != "" )
643 std::string param_value = parameters->GetPathName( param_name );
644 if( param_value == "" )
647 // Show dialog and check if it was accepted
648 QFileDialog dialog( this );
649 dialog.setFileMode( QFileDialog::Directory );
650 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
651 dialog.setAcceptMode( QFileDialog::AcceptOpen );
653 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
662 // -------------------------------------------------------------------------
663 void cpPlugins::ParametersQtDialog::
664 _dlg_OpenMultipleFiles( )
666 auto parameters = this->m_ProcessObject->GetParameters( );
667 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
670 std::string bName = btn->objectName( ).toStdString( );
671 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
673 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
676 std::string param_name = line->objectName( ).toStdString( );
677 if( param_name != "" )
679 QStringList dialog_filters;
680 std::string extensions = parameters->GetAcceptedFileExtensions( param_name );
681 if( extensions != "" )
682 dialog_filters << extensions.c_str( );
683 dialog_filters << "Any file (*)";
685 // Show dialog and check if it was accepted
686 QFileDialog dialog( this );
687 dialog.setFileMode( QFileDialog::ExistingFiles );
688 dialog.setNameFilters( dialog_filters );
689 dialog.setAcceptMode( QFileDialog::AcceptOpen );
692 if( dialog.selectedFiles( ).size( ) > 0 )
694 std::stringstream str;
695 auto files = dialog.selectedFiles( );
696 auto fIt = files.begin( );
697 str << fIt->toStdString( );
699 for( ; fIt != files.end( ); ++fIt )
700 str << "#" << fIt->toStdString( );
701 line->setText( str.str( ).c_str( ) );
714 // -------------------------------------------------------------------------
715 void cpPlugins::ParametersQtDialog::
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" ) );
723 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
726 std::string param_name = line->objectName( ).toStdString( );
727 if( param_name != "" )
731 QInputDialog::getInt(
734 std::string( "Add new value to \"" ) +
740 -std::numeric_limits< int >::max( ),
741 std::numeric_limits< int >::max( ),
746 std::string values = line->text( ).toStdString( );
749 std::stringstream str;
750 str << values << value;
751 line->setText( str.str( ).c_str( ) );
762 // -------------------------------------------------------------------------
763 void cpPlugins::ParametersQtDialog::
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 \"" ) +
787 0, 0, std::numeric_limits< int >::max( ), 1,
792 std::string values = line->text( ).toStdString( );
795 std::stringstream str;
796 str << values << value;
797 line->setText( str.str( ).c_str( ) );
808 // -------------------------------------------------------------------------
809 void cpPlugins::ParametersQtDialog::
812 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
815 std::string bName = btn->objectName( ).toStdString( );
816 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
817 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
820 std::string param_name = line->objectName( ).toStdString( );
821 if( param_name != "" )
825 QInputDialog::getDouble(
828 std::string( "Add new value to \"" ) +
834 -std::numeric_limits< double >::max( ),
835 std::numeric_limits< double >::max( ),
840 std::string values = line->text( ).toStdString( );
843 std::stringstream str;
844 str << values << value;
845 line->setText( str.str( ).c_str( ) );
856 #endif // cpPlugins_QT4