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;
302 title << "Parameters for \"" << filter->GetName( ) << "\"";
303 this->m_Title->setText( title.str( ).c_str( ) );
306 auto& raw_params = this->m_Parameters->GetRawParameters( );
307 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
309 QWidget* w_input = NULL;
310 switch( pIt->second.first )
312 case Parameters::String:
314 QLineEdit* v_string = new QLineEdit( this );
315 v_string->setObjectName( pIt->first.c_str( ) );
316 v_string->setText( pIt->second.second.c_str( ) );
320 case Parameters::Bool:
322 QCheckBox* v_bool = new QCheckBox( this );
323 v_bool->setObjectName( pIt->first.c_str( ) );
324 v_bool->setText( "[ON/OFF]" );
325 v_bool->setChecked( pIt->second.second == "1" );
329 case Parameters::Int:
330 case Parameters::Uint:
332 std::istringstream tok_str( pIt->second.second );
335 QSpinBox* v_uint = new QSpinBox( this );
336 v_uint->setObjectName( pIt->first.c_str( ) );
337 if( pIt->second.first == Parameters::Uint )
338 v_uint->setMinimum( 0 );
340 v_uint->setMinimum( -std::numeric_limits< int >::max( ) );
341 v_uint->setMaximum( std::numeric_limits< int >::max( ) );
342 v_uint->setValue( v );
346 case Parameters::Real:
348 std::istringstream tok_str( pIt->second.second );
351 QDoubleSpinBox* v_double = new QDoubleSpinBox( this );
352 v_double->setObjectName( pIt->first.c_str( ) );
353 v_double->setDecimals( 3 );
354 v_double->setMinimum( -std::numeric_limits< double >::max( ) );
355 v_double->setMaximum( std::numeric_limits< double >::max( ) );
356 v_double->setValue( v );
360 case Parameters::OpenFileName:
362 QFrame* frame = new QFrame( this );
363 QHBoxLayout* layout = new QHBoxLayout( frame );
364 QLineEdit* v_string = new QLineEdit( frame );
365 v_string->setObjectName( pIt->first.c_str( ) );
366 v_string->setText( pIt->second.second.c_str( ) );
367 QPushButton* v_button = new QPushButton( frame );
368 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
369 v_button->setText( "..." );
371 v_button, SIGNAL( clicked( ) ),
372 this, SLOT( _dlg_OpenSingleFile( ) )
374 layout->addWidget( v_string );
375 layout->addWidget( v_button );
378 case Parameters::SaveFileName:
380 QFrame* frame = new QFrame( this );
381 QHBoxLayout* layout = new QHBoxLayout( frame );
382 QLineEdit* v_string = new QLineEdit( frame );
383 v_string->setObjectName( pIt->first.c_str( ) );
384 v_string->setText( pIt->second.second.c_str( ) );
385 QPushButton* v_button = new QPushButton( frame );
386 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
387 v_button->setText( "..." );
389 v_button, SIGNAL( clicked( ) ),
390 this, SLOT( _dlg_SaveSingleFile( ) )
392 layout->addWidget( v_string );
393 layout->addWidget( v_button );
397 case Parameters::PathName:
399 QFrame* frame = new QFrame( this );
400 QHBoxLayout* layout = new QHBoxLayout( frame );
401 QLineEdit* v_string = new QLineEdit( frame );
402 v_string->setObjectName( pIt->first.c_str( ) );
403 v_string->setText( pIt->second.second.c_str( ) );
404 QPushButton* v_button = new QPushButton( frame );
405 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
406 v_button->setText( "..." );
408 v_button, SIGNAL( clicked( ) ),
409 this, SLOT( _dlg_OpenSinglePath( ) )
411 layout->addWidget( v_string );
412 layout->addWidget( v_button );
416 case Parameters::StringList:
418 case Parameters::BoolList:
420 case Parameters::IntList:
422 QFrame* frame = new QFrame( this );
423 QHBoxLayout* layout = new QHBoxLayout( frame );
424 QLineEdit* v_string = new QLineEdit( frame );
425 v_string->setObjectName( pIt->first.c_str( ) );
426 v_string->setText( pIt->second.second.c_str( ) );
427 QPushButton* v_button = new QPushButton( frame );
428 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
429 v_button->setText( "+" );
431 v_button, SIGNAL( clicked( ) ),
432 this, SLOT( _dlg_AddInt( ) )
434 layout->addWidget( v_string );
435 layout->addWidget( v_button );
439 case Parameters::UintList:
441 QFrame* frame = new QFrame( this );
442 QHBoxLayout* layout = new QHBoxLayout( frame );
443 QLineEdit* v_string = new QLineEdit( frame );
444 v_string->setObjectName( pIt->first.c_str( ) );
445 v_string->setText( pIt->second.second.c_str( ) );
446 QPushButton* v_button = new QPushButton( frame );
447 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
448 v_button->setText( "+" );
450 v_button, SIGNAL( clicked( ) ),
451 this, SLOT( _dlg_AddUint( ) )
453 layout->addWidget( v_string );
454 layout->addWidget( v_button );
458 case Parameters::RealList:
460 QFrame* frame = new QFrame( this );
461 QHBoxLayout* layout = new QHBoxLayout( frame );
462 QLineEdit* v_string = new QLineEdit( frame );
463 v_string->setObjectName( pIt->first.c_str( ) );
464 v_string->setText( pIt->second.second.c_str( ) );
465 QPushButton* v_button = new QPushButton( frame );
466 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
467 v_button->setText( "+" );
469 v_button, SIGNAL( clicked( ) ),
470 this, SLOT( _dlg_AddReal( ) )
472 layout->addWidget( v_string );
473 layout->addWidget( v_button );
477 case Parameters::OpenFileNameList:
479 QFrame* frame = new QFrame( this );
480 QHBoxLayout* layout = new QHBoxLayout( frame );
481 QLineEdit* v_string = new QLineEdit( frame );
482 v_string->setObjectName( pIt->first.c_str( ) );
483 v_string->setMaxLength( std::numeric_limits< int >::max( ) );
484 v_string->setText( pIt->second.second.c_str( ) );
485 QPushButton* v_button = new QPushButton( frame );
486 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
487 v_button->setText( "..." );
489 v_button, SIGNAL( clicked( ) ),
490 this, SLOT( _dlg_OpenMultipleFiles( ) )
492 layout->addWidget( v_string );
493 layout->addWidget( v_button );
497 case Parameters::SaveFileNameList:
499 case Parameters::PathNameList:
501 case Parameters::Choices:
503 QComboBox* v_choices = new QComboBox( this );
504 v_choices->setObjectName( pIt->first.c_str( ) );
506 std::istringstream str0( pIt->second.second );
508 std::getline( str0, choices, '@' );
509 std::istringstream str1( choices );
512 while( std::getline( str1, token, '#' ) )
513 v_choices->insertItem( id++, token.c_str( ) );
522 // Ok, a representation was created
523 if( w_input != NULL )
525 QHBoxLayout* new_layout = new QHBoxLayout( );
526 QLabel* label = new QLabel( this );
527 label->setText( QString( pIt->first.c_str( ) ) );
528 new_layout->addWidget( label );
529 new_layout->addWidget( w_input );
530 this->m_ToolsLayout->addLayout( new_layout );
537 this->_addButtons( );
540 // -------------------------------------------------------------------------
541 void cpPlugins::Interface::ParametersQtDialog::
542 _dlg_OpenSingleFile( )
544 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
547 std::string bName = btn->objectName( ).toStdString( );
548 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
549 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
552 std::string param_name = line->objectName( ).toStdString( );
553 if( param_name != "" )
555 std::string param_value =
556 this->m_Parameters->GetOpenFileName( param_name );
557 if( param_value == "" )
559 QStringList dialog_filters;
560 std::string extensions =
561 this->m_Parameters->GetAcceptedFileExtensions( param_name );
562 if( extensions != "" )
563 dialog_filters << extensions.c_str( );
564 dialog_filters << "Any file (*)";
566 // Show dialog and check if it was accepted
567 QFileDialog dialog( this );
568 dialog.setFileMode( QFileDialog::ExistingFile );
569 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
570 dialog.setNameFilters( dialog_filters );
571 dialog.setAcceptMode( QFileDialog::AcceptOpen );
573 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
582 // -------------------------------------------------------------------------
583 void cpPlugins::Interface::ParametersQtDialog::
584 _dlg_SaveSingleFile( )
586 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
589 std::string bName = btn->objectName( ).toStdString( );
590 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
591 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
594 std::string param_name = line->objectName( ).toStdString( );
595 if( param_name != "" )
597 std::string param_value =
598 this->m_Parameters->GetSaveFileName( param_name );
599 if( param_value == "" )
601 QStringList dialog_filters;
602 std::string extensions =
603 this->m_Parameters->GetAcceptedFileExtensions( param_name );
604 if( extensions != "" )
605 dialog_filters << extensions.c_str( );
606 dialog_filters << "Any file (*)";
608 // Show dialog and check if it was accepted
609 QFileDialog dialog( this );
610 dialog.setFileMode( QFileDialog::AnyFile );
611 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
612 dialog.setNameFilters( dialog_filters );
613 dialog.setAcceptMode( QFileDialog::AcceptSave );
615 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
624 // -------------------------------------------------------------------------
625 void cpPlugins::Interface::ParametersQtDialog::
626 _dlg_OpenSinglePath( )
628 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
631 std::string bName = btn->objectName( ).toStdString( );
632 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
633 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
636 std::string param_name = line->objectName( ).toStdString( );
637 if( param_name != "" )
639 std::string param_value =
640 this->m_Parameters->GetPathName( param_name );
641 if( param_value == "" )
644 // Show dialog and check if it was accepted
645 QFileDialog dialog( this );
646 dialog.setFileMode( QFileDialog::Directory );
647 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
648 dialog.setAcceptMode( QFileDialog::AcceptOpen );
650 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
659 // -------------------------------------------------------------------------
660 void cpPlugins::Interface::ParametersQtDialog::
661 _dlg_OpenMultipleFiles( )
663 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
666 std::string bName = btn->objectName( ).toStdString( );
667 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
669 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
672 std::string param_name = line->objectName( ).toStdString( );
673 if( param_name != "" )
675 QStringList dialog_filters;
676 std::string extensions =
677 this->m_Parameters->GetAcceptedFileExtensions( param_name );
678 if( extensions != "" )
679 dialog_filters << extensions.c_str( );
680 dialog_filters << "Any file (*)";
682 // Show dialog and check if it was accepted
683 QFileDialog dialog( this );
684 dialog.setFileMode( QFileDialog::ExistingFiles );
685 dialog.setNameFilters( dialog_filters );
686 dialog.setAcceptMode( QFileDialog::AcceptOpen );
689 if( dialog.selectedFiles( ).size( ) > 0 )
691 std::stringstream str;
692 auto files = dialog.selectedFiles( );
693 auto fIt = files.begin( );
694 str << fIt->toStdString( );
696 for( ; fIt != files.end( ); ++fIt )
697 str << "#" << fIt->toStdString( );
698 line->setText( str.str( ).c_str( ) );
711 // -------------------------------------------------------------------------
712 void cpPlugins::Interface::ParametersQtDialog::
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" ) );
720 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
723 std::string param_name = line->objectName( ).toStdString( );
724 if( param_name != "" )
728 QInputDialog::getInt(
731 std::string( "Add new value to \"" ) +
737 -std::numeric_limits< int >::max( ),
738 std::numeric_limits< int >::max( ),
743 std::string values = line->text( ).toStdString( );
746 std::stringstream str;
747 str << values << value;
748 line->setText( str.str( ).c_str( ) );
759 // -------------------------------------------------------------------------
760 void cpPlugins::Interface::ParametersQtDialog::
763 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
766 std::string bName = btn->objectName( ).toStdString( );
767 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
768 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
771 std::string param_name = line->objectName( ).toStdString( );
772 if( param_name != "" )
776 QInputDialog::getInt(
779 std::string( "Add new value to \"" ) +
784 0, 0, std::numeric_limits< int >::max( ), 1,
789 std::string values = line->text( ).toStdString( );
792 std::stringstream str;
793 str << values << value;
794 line->setText( str.str( ).c_str( ) );
805 // -------------------------------------------------------------------------
806 void cpPlugins::Interface::ParametersQtDialog::
809 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
812 std::string bName = btn->objectName( ).toStdString( );
813 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
814 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
817 std::string param_name = line->objectName( ).toStdString( );
818 if( param_name != "" )
822 QInputDialog::getDouble(
825 std::string( "Add new value to \"" ) +
831 -std::numeric_limits< double >::max( ),
832 std::numeric_limits< double >::max( ),
837 std::string values = line->text( ).toStdString( );
840 std::stringstream str;
841 str << values << value;
842 line->setText( str.str( ).c_str( ) );
853 #endif // cpPlugins_Interface_QT4