1 #include <cpPlugins/Interface/ParametersQtDialog.h>
3 #ifdef cpPlugins_Interface_QT4
7 #include <cpPlugins/Interface/ProcessObject.h>
11 #include <QDoubleSpinBox>
12 #include <QFileDialog>
13 #include <QHBoxLayout>
14 #include <QInputDialog>
16 #include <QPushButton>
19 // -------------------------------------------------------------------------
20 cpPlugins::Interface::ParametersQtDialog::
21 ParametersQtDialog( QWidget* parent, Qt::WindowFlags f )
22 : QDialog( parent, f ),
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::Interface::ParametersQtDialog::
37 ~ParametersQtDialog( )
40 delete this->m_ToolsLayout;
41 delete this->m_MainLayout;
44 // -------------------------------------------------------------------------
45 cpPlugins::Interface::
46 Parameters* cpPlugins::Interface::ParametersQtDialog::
47 getParameters( ) const
49 return( this->m_Parameters );
52 // -------------------------------------------------------------------------
53 bool cpPlugins::Interface::ParametersQtDialog::
54 setParameters( Parameters* parameters )
56 if( this->m_Parameters != NULL || parameters == NULL )
58 this->m_Parameters = parameters;
59 this->m_WidgetsUpdated = false;
63 // -------------------------------------------------------------------------
64 int cpPlugins::Interface::ParametersQtDialog::
67 this->_updateWidgets( );
70 int ret = this->QDialog::exec( );
72 this->updateParameters( );
78 // -------------------------------------------------------------------------
79 void cpPlugins::Interface::ParametersQtDialog::
82 if( this->m_Parameters == NULL )
86 auto& raw_params = this->m_Parameters->GetRawParameters( );
87 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
89 QString pName = pIt->first.c_str( );
90 switch( pIt->second.first )
92 case Parameters::String:
93 case Parameters::OpenFileName:
94 case Parameters::SaveFileName:
95 case Parameters::PathName:
96 case Parameters::IntList:
97 case Parameters::UintList:
98 case Parameters::RealList:
99 case Parameters::OpenFileNameList:
101 QLineEdit* v_string = this->findChild< QLineEdit* >( pName );
102 if( v_string != NULL )
103 pIt->second.second = v_string->text( ).toStdString( );
106 case Parameters::Bool:
108 QCheckBox* v_bool = this->findChild< QCheckBox* >( pName );
110 pIt->second.second = ( v_bool->isChecked( ) )? "1": "0";
113 case Parameters::Int:
114 case Parameters::Uint:
116 QSpinBox* v_uint = this->findChild< QSpinBox* >( pName );
119 std::stringstream str;
120 str << v_uint->value( );
121 pIt->second.second = str.str( );
126 case Parameters::Real:
128 QDoubleSpinBox* v_double = this->findChild< QDoubleSpinBox* >( pName );
131 std::stringstream str;
132 str << v_double->value( );
133 pIt->second.second = str.str( );
138 case Parameters::StringList:
140 case Parameters::BoolList:
142 case Parameters::SaveFileNameList:
144 case Parameters::PathNameList:
146 case Parameters::Choices:
148 QComboBox* v_choices = this->findChild< QComboBox* >( pName );
149 if( v_choices != NULL )
151 std::istringstream str_choices( pIt->second.second );
152 std::string real_choices;
153 std::getline( str_choices, real_choices, '@' );
156 v_choices->currentText( ).toStdString( );
166 this->m_Parameters->Modified( );
169 // -------------------------------------------------------------------------
170 void cpPlugins::Interface::ParametersQtDialog::
173 if( this->m_Parameters == NULL )
177 auto& raw_params = this->m_Parameters->GetRawParameters( );
178 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
180 QString pName = pIt->first.c_str( );
181 switch( pIt->second.first )
183 case Parameters::String:
184 case Parameters::OpenFileName:
185 case Parameters::SaveFileName:
186 case Parameters::PathName:
187 case Parameters::IntList:
188 case Parameters::UintList:
189 case Parameters::RealList:
190 case Parameters::OpenFileNameList:
192 QLineEdit* v_string = this->findChild< QLineEdit* >( pName );
193 if( v_string != NULL )
194 v_string->setText( pIt->second.second.c_str( ) );
197 case Parameters::Bool:
199 QCheckBox* v_bool = this->findChild< QCheckBox* >( pName );
201 v_bool->setChecked( pIt->second.second == "1" );
204 case Parameters::Int:
205 case Parameters::Uint:
207 QSpinBox* v_uint = this->findChild< QSpinBox* >( pName );
210 std::istringstream tok_str( pIt->second.second );
213 v_uint->setValue( v );
218 case Parameters::Real:
220 QDoubleSpinBox* v_double = this->findChild< QDoubleSpinBox* >( pName );
223 std::istringstream tok_str( pIt->second.second );
226 v_double->setValue( v );
231 case Parameters::StringList:
233 case Parameters::BoolList:
235 case Parameters::SaveFileNameList:
237 case Parameters::PathNameList:
239 case Parameters::Choices:
241 QComboBox* v_choices = this->findChild< QComboBox* >( pName );
242 if( v_choices != NULL )
244 std::istringstream str_choices( pIt->second.second );
245 std::string choices, real_choice;
246 std::getline( str_choices, choices, '@' );
247 std::getline( str_choices, real_choice, '@' );
248 std::istringstream str( choices );
250 int id = -1, cont = 0;
251 while( std::getline( str, token, '#' ) )
253 if( token == real_choice )
260 v_choices->setCurrentIndex( id );
272 // -------------------------------------------------------------------------
273 void cpPlugins::Interface::ParametersQtDialog::
277 this->m_Buttons = new QDialogButtonBox(
278 QDialogButtonBox::Ok | QDialogButtonBox::Cancel
281 this->m_Buttons, SIGNAL( accepted( ) ), this, SLOT( accept( ) )
284 this->m_Buttons, SIGNAL( rejected( ) ), this, SLOT( reject( ) )
286 this->m_ToolsLayout->addWidget( this->m_Buttons );
289 this->m_WidgetsUpdated = true;
292 // -------------------------------------------------------------------------
293 void cpPlugins::Interface::ParametersQtDialog::
296 if( this->m_WidgetsUpdated || this->m_Parameters == NULL )
300 auto filter = this->m_Parameters->GetProcessObject( );
301 std::stringstream title;
303 << "Parameters for an object of class \""
304 << filter->GetClassName( )
306 this->m_Title->setText( title.str( ).c_str( ) );
309 auto& raw_params = this->m_Parameters->GetRawParameters( );
310 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
312 QWidget* w_input = NULL;
313 switch( pIt->second.first )
315 case Parameters::String:
317 QLineEdit* v_string = new QLineEdit( this );
318 v_string->setObjectName( pIt->first.c_str( ) );
319 v_string->setText( pIt->second.second.c_str( ) );
323 case Parameters::Bool:
325 QCheckBox* v_bool = new QCheckBox( this );
326 v_bool->setObjectName( pIt->first.c_str( ) );
327 v_bool->setText( "[ON/OFF]" );
328 v_bool->setChecked( pIt->second.second == "1" );
332 case Parameters::Int:
333 case Parameters::Uint:
335 std::istringstream tok_str( pIt->second.second );
338 QSpinBox* v_uint = new QSpinBox( this );
339 v_uint->setObjectName( pIt->first.c_str( ) );
340 if( pIt->second.first == Parameters::Uint )
341 v_uint->setMinimum( 0 );
343 v_uint->setMinimum( -std::numeric_limits< int >::max( ) );
344 v_uint->setMaximum( std::numeric_limits< int >::max( ) );
345 v_uint->setValue( v );
349 case Parameters::Real:
351 std::istringstream tok_str( pIt->second.second );
354 QDoubleSpinBox* v_double = new QDoubleSpinBox( this );
355 v_double->setObjectName( pIt->first.c_str( ) );
356 v_double->setDecimals( 3 );
357 v_double->setMinimum( -std::numeric_limits< double >::max( ) );
358 v_double->setMaximum( std::numeric_limits< double >::max( ) );
359 v_double->setValue( v );
363 case Parameters::OpenFileName:
365 QFrame* frame = new QFrame( this );
366 QHBoxLayout* layout = new QHBoxLayout( frame );
367 QLineEdit* v_string = new QLineEdit( frame );
368 v_string->setObjectName( pIt->first.c_str( ) );
369 v_string->setText( pIt->second.second.c_str( ) );
370 QPushButton* v_button = new QPushButton( frame );
371 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
372 v_button->setText( "..." );
374 v_button, SIGNAL( clicked( ) ),
375 this, SLOT( _dlg_OpenSingleFile( ) )
377 layout->addWidget( v_string );
378 layout->addWidget( v_button );
381 case Parameters::SaveFileName:
383 QFrame* frame = new QFrame( this );
384 QHBoxLayout* layout = new QHBoxLayout( frame );
385 QLineEdit* v_string = new QLineEdit( frame );
386 v_string->setObjectName( pIt->first.c_str( ) );
387 v_string->setText( pIt->second.second.c_str( ) );
388 QPushButton* v_button = new QPushButton( frame );
389 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
390 v_button->setText( "..." );
392 v_button, SIGNAL( clicked( ) ),
393 this, SLOT( _dlg_SaveSingleFile( ) )
395 layout->addWidget( v_string );
396 layout->addWidget( v_button );
400 case Parameters::PathName:
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_OpenSinglePath( ) )
414 layout->addWidget( v_string );
415 layout->addWidget( v_button );
419 case Parameters::StringList:
421 case Parameters::BoolList:
423 case Parameters::IntList:
425 QFrame* frame = new QFrame( this );
426 QHBoxLayout* layout = new QHBoxLayout( frame );
427 QLineEdit* v_string = new QLineEdit( frame );
428 v_string->setObjectName( pIt->first.c_str( ) );
429 v_string->setText( pIt->second.second.c_str( ) );
430 QPushButton* v_button = new QPushButton( frame );
431 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
432 v_button->setText( "+" );
434 v_button, SIGNAL( clicked( ) ),
435 this, SLOT( _dlg_AddInt( ) )
437 layout->addWidget( v_string );
438 layout->addWidget( v_button );
442 case Parameters::UintList:
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_AddUint( ) )
456 layout->addWidget( v_string );
457 layout->addWidget( v_button );
461 case Parameters::RealList:
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_AddReal( ) )
475 layout->addWidget( v_string );
476 layout->addWidget( v_button );
480 case Parameters::OpenFileNameList:
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->setMaxLength( std::numeric_limits< int >::max( ) );
487 v_string->setText( pIt->second.second.c_str( ) );
488 QPushButton* v_button = new QPushButton( frame );
489 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
490 v_button->setText( "..." );
492 v_button, SIGNAL( clicked( ) ),
493 this, SLOT( _dlg_OpenMultipleFiles( ) )
495 layout->addWidget( v_string );
496 layout->addWidget( v_button );
500 case Parameters::SaveFileNameList:
502 case Parameters::PathNameList:
504 case Parameters::Choices:
506 QComboBox* v_choices = new QComboBox( this );
507 v_choices->setObjectName( pIt->first.c_str( ) );
509 std::istringstream str0( pIt->second.second );
511 std::getline( str0, choices, '@' );
512 std::istringstream str1( choices );
515 while( std::getline( str1, token, '#' ) )
516 v_choices->insertItem( id++, token.c_str( ) );
525 // Ok, a representation was created
526 if( w_input != NULL )
528 QHBoxLayout* new_layout = new QHBoxLayout( );
529 QLabel* label = new QLabel( this );
530 label->setText( QString( pIt->first.c_str( ) ) );
531 new_layout->addWidget( label );
532 new_layout->addWidget( w_input );
533 this->m_ToolsLayout->addLayout( new_layout );
540 this->_addButtons( );
543 // -------------------------------------------------------------------------
544 void cpPlugins::Interface::ParametersQtDialog::
545 _dlg_OpenSingleFile( )
547 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
550 std::string bName = btn->objectName( ).toStdString( );
551 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
552 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
555 std::string param_name = line->objectName( ).toStdString( );
556 if( param_name != "" )
558 std::string param_value =
559 this->m_Parameters->GetOpenFileName( param_name );
560 if( param_value == "" )
562 QStringList dialog_filters;
563 std::string extensions =
564 this->m_Parameters->GetAcceptedFileExtensions( param_name );
565 if( extensions != "" )
566 dialog_filters << extensions.c_str( );
567 dialog_filters << "Any file (*)";
569 // Show dialog and check if it was accepted
570 QFileDialog dialog( this );
571 dialog.setFileMode( QFileDialog::ExistingFile );
572 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
573 dialog.setNameFilters( dialog_filters );
574 dialog.setAcceptMode( QFileDialog::AcceptOpen );
576 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
585 // -------------------------------------------------------------------------
586 void cpPlugins::Interface::ParametersQtDialog::
587 _dlg_SaveSingleFile( )
589 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
592 std::string bName = btn->objectName( ).toStdString( );
593 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
594 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
597 std::string param_name = line->objectName( ).toStdString( );
598 if( param_name != "" )
600 std::string param_value =
601 this->m_Parameters->GetSaveFileName( param_name );
602 if( param_value == "" )
604 QStringList dialog_filters;
605 std::string extensions =
606 this->m_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::Interface::ParametersQtDialog::
629 _dlg_OpenSinglePath( )
631 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
634 std::string bName = btn->objectName( ).toStdString( );
635 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
636 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
639 std::string param_name = line->objectName( ).toStdString( );
640 if( param_name != "" )
642 std::string param_value =
643 this->m_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::Interface::ParametersQtDialog::
664 _dlg_OpenMultipleFiles( )
666 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
669 std::string bName = btn->objectName( ).toStdString( );
670 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
672 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
675 std::string param_name = line->objectName( ).toStdString( );
676 if( param_name != "" )
678 QStringList dialog_filters;
679 std::string extensions =
680 this->m_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::Interface::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::Interface::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::Interface::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_Interface_QT4