1 #include <cpPlugins/Interface/ParametersQtDialog.h>
3 #ifdef cpPlugins_Interface_QT4
7 #include <cpPlugins/Interface/ParametersListWidget.h>
9 #include <vtkCommand.h>
10 #include <vtkRenderWindowInteractor.h>
14 #include <QDoubleSpinBox>
15 #include <QFileDialog>
16 #include <QHBoxLayout>
17 #include <QInputDialog>
19 #include <QPushButton>
22 // -------------------------------------------------------------------------
23 #include <cpPlugins/Interface/ProcessObject.h>
24 #include <cpPlugins/Interface/Plugins.h>
25 #include <cpPlugins/Interface/BaseApplication.h>
27 class SingleSeedCommand
31 static SingleSeedCommand* New( )
32 { return( new SingleSeedCommand ); }
33 virtual void Execute( vtkObject* caller, unsigned long eid, void* data )
35 // Get seed, avoiding segfaults!!!
36 if( eid != vtkCommand::PlacePointEvent || this->Dialog == NULL )
38 vtkSeedWidget* widget = dynamic_cast< vtkSeedWidget* >( caller );
41 vtkSeedRepresentation* rep = widget->GetSeedRepresentation( );
44 if( rep->GetNumberOfSeeds( ) == 0 )
47 rep->GetSeedWorldPosition( 0, seed );
49 // Delete all seeds (remember that this command is just for one seed)
50 while( rep->GetNumberOfSeeds( ) > 0 )
51 widget->DeleteSeed( 0 );
53 if( this->Dialog->getParameters( )->HasIndex( this->Name ) )
56 else if( this->Dialog->getParameters( )->HasPoint( this->Name ) )
58 this->Dialog->getParameters( )->SetPoint( this->Name, 3, seed );
59 this->Dialog->updateParameters( );
60 auto filter = this->Dialog->getParameters( )->GetProcessObject( );
63 auto plugins = filter->GetPlugins( );
66 auto app = plugins->GetApplication( );
68 app->UpdateActualFilter( );
82 virtual ~SingleSeedCommand( )
87 cpPlugins::Interface::ParametersQtDialog* Dialog;
91 // -------------------------------------------------------------------------
92 cpPlugins::Interface::ParametersQtDialog::
93 ParametersQtDialog( QWidget* parent, Qt::WindowFlags f )
94 : QDialog( parent, f ),
97 this->m_Title = new QLabel( this );
98 this->m_Title->setText( "Parameters dialog title" );
100 this->m_MainLayout = new QGridLayout( this );
101 this->m_ToolsLayout = new QVBoxLayout( );
102 this->m_ToolsLayout->addWidget( this->m_Title );
103 this->m_MainLayout->addLayout( this->m_ToolsLayout, 0, 0, 1, 1 );
106 // -------------------------------------------------------------------------
107 cpPlugins::Interface::ParametersQtDialog::
108 ~ParametersQtDialog( )
110 delete this->m_Title;
111 delete this->m_ToolsLayout;
112 delete this->m_MainLayout;
115 // -------------------------------------------------------------------------
116 cpPlugins::Interface::
117 Parameters* cpPlugins::Interface::ParametersQtDialog::
118 getParameters( ) const
120 return( this->m_Parameters );
123 // -------------------------------------------------------------------------
124 void cpPlugins::Interface::ParametersQtDialog::
125 addInteractor( vtkRenderWindowInteractor* interactor )
127 this->m_Interactors.insert( interactor );
130 // -------------------------------------------------------------------------
131 cpPlugins::Interface::ParametersQtDialog::
132 TInteractors& cpPlugins::Interface::ParametersQtDialog::
135 return( this->m_Interactors );
138 // -------------------------------------------------------------------------
139 const cpPlugins::Interface::ParametersQtDialog::
140 TInteractors& cpPlugins::Interface::ParametersQtDialog::
141 getInteractors( ) const
143 return( this->m_Interactors );
146 // -------------------------------------------------------------------------
147 bool cpPlugins::Interface::ParametersQtDialog::
148 setParameters( Parameters* parameters )
150 if( this->m_Parameters != NULL || parameters == NULL )
152 this->m_Parameters = parameters;
155 auto filter = this->m_Parameters->GetProcessObject( );
156 std::stringstream title;
157 title << "Parameters for \"" << filter->GetName( ) << "\"";
158 this->m_Title->setText( title.str( ).c_str( ) );
161 auto& raw_params = this->m_Parameters->GetRawParameters( );
162 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
164 QWidget* w_input = NULL;
165 switch( pIt->second.first )
167 case Parameters::String:
169 QLineEdit* v_string = new QLineEdit( this );
170 v_string->setObjectName( pIt->first.c_str( ) );
171 v_string->setText( pIt->second.second.c_str( ) );
175 case Parameters::Bool:
177 QCheckBox* v_bool = new QCheckBox( this );
178 v_bool->setObjectName( pIt->first.c_str( ) );
179 v_bool->setText( "[ON/OFF]" );
180 v_bool->setChecked( pIt->second.second == "1" );
184 case Parameters::Int:
185 case Parameters::Uint:
187 std::istringstream tok_str( pIt->second.second );
190 QSpinBox* v_uint = new QSpinBox( this );
191 v_uint->setObjectName( pIt->first.c_str( ) );
192 if( pIt->second.first == Parameters::Uint )
193 v_uint->setMinimum( 0 );
195 v_uint->setMinimum( -std::numeric_limits< int >::max( ) );
196 v_uint->setMaximum( std::numeric_limits< int >::max( ) );
197 v_uint->setValue( v );
201 case Parameters::Real:
203 std::istringstream tok_str( pIt->second.second );
206 QDoubleSpinBox* v_double = new QDoubleSpinBox( this );
207 v_double->setObjectName( pIt->first.c_str( ) );
208 v_double->setDecimals( 3 );
209 v_double->setMinimum( -std::numeric_limits< double >::max( ) );
210 v_double->setMaximum( std::numeric_limits< double >::max( ) );
211 v_double->setValue( v );
215 case Parameters::Index:
217 case Parameters::Point:
219 case Parameters::Vector:
221 case Parameters::OpenFileName:
223 QFrame* frame = new QFrame( this );
224 QHBoxLayout* layout = new QHBoxLayout( frame );
225 QLineEdit* v_string = new QLineEdit( frame );
226 v_string->setObjectName( pIt->first.c_str( ) );
227 v_string->setText( pIt->second.second.c_str( ) );
228 QPushButton* v_button = new QPushButton( frame );
229 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
230 v_button->setText( "..." );
232 v_button, SIGNAL( clicked( ) ),
233 this, SLOT( _dlg_OpenSingleFile( ) )
235 layout->addWidget( v_string );
236 layout->addWidget( v_button );
239 case Parameters::SaveFileName:
241 QFrame* frame = new QFrame( this );
242 QHBoxLayout* layout = new QHBoxLayout( frame );
243 QLineEdit* v_string = new QLineEdit( frame );
244 v_string->setObjectName( pIt->first.c_str( ) );
245 v_string->setText( pIt->second.second.c_str( ) );
246 QPushButton* v_button = new QPushButton( frame );
247 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
248 v_button->setText( "..." );
250 v_button, SIGNAL( clicked( ) ),
251 this, SLOT( _dlg_SaveSingleFile( ) )
253 layout->addWidget( v_string );
254 layout->addWidget( v_button );
258 case Parameters::PathName:
260 QFrame* frame = new QFrame( this );
261 QHBoxLayout* layout = new QHBoxLayout( frame );
262 QLineEdit* v_string = new QLineEdit( frame );
263 v_string->setObjectName( pIt->first.c_str( ) );
264 v_string->setText( pIt->second.second.c_str( ) );
265 QPushButton* v_button = new QPushButton( frame );
266 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
267 v_button->setText( "..." );
269 v_button, SIGNAL( clicked( ) ),
270 this, SLOT( _dlg_OpenSinglePath( ) )
272 layout->addWidget( v_string );
273 layout->addWidget( v_button );
277 case Parameters::StringList:
279 case Parameters::BoolList:
281 case Parameters::IntList:
283 QFrame* frame = new QFrame( this );
284 QHBoxLayout* layout = new QHBoxLayout( frame );
285 QLineEdit* v_string = new QLineEdit( frame );
286 v_string->setObjectName( pIt->first.c_str( ) );
287 v_string->setText( pIt->second.second.c_str( ) );
288 QPushButton* v_button = new QPushButton( frame );
289 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
290 v_button->setText( "+" );
292 v_button, SIGNAL( clicked( ) ),
293 this, SLOT( _dlg_AddInt( ) )
295 layout->addWidget( v_string );
296 layout->addWidget( v_button );
300 case Parameters::UintList:
302 QFrame* frame = new QFrame( this );
303 QHBoxLayout* layout = new QHBoxLayout( frame );
304 QLineEdit* v_string = new QLineEdit( frame );
305 v_string->setObjectName( pIt->first.c_str( ) );
306 v_string->setText( pIt->second.second.c_str( ) );
307 QPushButton* v_button = new QPushButton( frame );
308 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
309 v_button->setText( "+" );
311 v_button, SIGNAL( clicked( ) ),
312 this, SLOT( _dlg_AddUint( ) )
314 layout->addWidget( v_string );
315 layout->addWidget( v_button );
319 case Parameters::RealList:
321 QFrame* frame = new QFrame( this );
322 QHBoxLayout* layout = new QHBoxLayout( frame );
323 QLineEdit* v_string = new QLineEdit( frame );
324 v_string->setObjectName( pIt->first.c_str( ) );
325 v_string->setText( pIt->second.second.c_str( ) );
326 QPushButton* v_button = new QPushButton( frame );
327 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
328 v_button->setText( "+" );
330 v_button, SIGNAL( clicked( ) ),
331 this, SLOT( _dlg_AddReal( ) )
333 layout->addWidget( v_string );
334 layout->addWidget( v_button );
338 case Parameters::IndexList:
340 case Parameters::PointList:
342 case Parameters::VectorList:
344 case Parameters::OpenFileNameList:
346 QFrame* frame = new QFrame( this );
347 QHBoxLayout* layout = new QHBoxLayout( frame );
348 QLineEdit* v_string = new QLineEdit( frame );
349 v_string->setObjectName( pIt->first.c_str( ) );
350 v_string->setText( pIt->second.second.c_str( ) );
351 QPushButton* v_button = new QPushButton( frame );
352 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
353 v_button->setText( "..." );
355 v_button, SIGNAL( clicked( ) ),
356 this, SLOT( _dlg_OpenMultipleFiles( ) )
358 layout->addWidget( v_string );
359 layout->addWidget( v_button );
363 case Parameters::SaveFileNameList:
365 case Parameters::PathNameList:
367 case Parameters::Choices:
369 QComboBox* v_choices = new QComboBox( this );
370 v_choices->setObjectName( pIt->first.c_str( ) );
372 std::istringstream str0( pIt->second.second );
374 std::getline( str0, choices, '@' );
375 std::istringstream str1( choices );
378 while( std::getline( str1, token, '#' ) )
379 v_choices->insertItem( id++, token.c_str( ) );
388 // Ok, a representation was created
389 if( w_input != NULL )
391 QHBoxLayout* new_layout = new QHBoxLayout( );
392 QLabel* label = new QLabel( this );
393 label->setText( QString( pIt->first.c_str( ) ) );
394 new_layout->addWidget( label );
395 new_layout->addWidget( w_input );
396 this->m_ToolsLayout->addLayout( new_layout );
403 this->m_Buttons = new QDialogButtonBox(
404 QDialogButtonBox::Ok | QDialogButtonBox::Cancel
407 this->m_Buttons, SIGNAL( accepted( ) ), this, SLOT( accept( ) )
410 this->m_Buttons, SIGNAL( rejected( ) ), this, SLOT( reject( ) )
412 this->m_ToolsLayout->addWidget( this->m_Buttons );
420 // -------------------------------------------------------------------------
421 int cpPlugins::Interface::ParametersQtDialog::
424 int ret = this->QDialog::exec( );
426 this->updateParameters( );
432 // -------------------------------------------------------------------------
433 void cpPlugins::Interface::ParametersQtDialog::
436 this->QDialog::show( );
439 // -------------------------------------------------------------------------
440 void cpPlugins::Interface::ParametersQtDialog::
443 if( this->m_Parameters == NULL )
447 auto& raw_params = this->m_Parameters->GetRawParameters( );
448 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
450 QString pName = pIt->first.c_str( );
451 switch( pIt->second.first )
453 case Parameters::String:
454 case Parameters::OpenFileName:
455 case Parameters::SaveFileName:
456 case Parameters::PathName:
457 case Parameters::IntList:
458 case Parameters::UintList:
459 case Parameters::RealList:
460 case Parameters::OpenFileNameList:
462 QLineEdit* v_string = this->findChild< QLineEdit* >( pName );
463 if( v_string != NULL )
464 pIt->second.second = v_string->text( ).toStdString( );
467 case Parameters::Bool:
469 QCheckBox* v_bool = this->findChild< QCheckBox* >( pName );
471 pIt->second.second = ( v_bool->isChecked( ) )? "1": "0";
474 case Parameters::Int:
475 case Parameters::Uint:
477 QSpinBox* v_uint = this->findChild< QSpinBox* >( pName );
480 std::stringstream str;
481 str << v_uint->value( );
482 pIt->second.second = str.str( );
487 case Parameters::Real:
489 QDoubleSpinBox* v_double = this->findChild< QDoubleSpinBox* >( pName );
492 std::stringstream str;
493 str << v_double->value( );
494 pIt->second.second = str.str( );
499 case Parameters::Index:
501 case Parameters::Point:
503 case Parameters::Vector:
505 case Parameters::StringList:
507 case Parameters::BoolList:
509 case Parameters::IndexList:
511 case Parameters::PointList:
513 case Parameters::VectorList:
515 case Parameters::SaveFileNameList:
517 case Parameters::PathNameList:
519 case Parameters::Choices:
521 QComboBox* v_choices = this->findChild< QComboBox* >( pName );
522 if( v_choices != NULL )
524 std::istringstream str_choices( pIt->second.second );
525 std::string real_choices;
526 std::getline( str_choices, real_choices, '@' );
529 v_choices->currentText( ).toStdString( );
541 // -------------------------------------------------------------------------
542 void cpPlugins::Interface::ParametersQtDialog::
545 if( this->m_Parameters == NULL )
549 auto& raw_params = this->m_Parameters->GetRawParameters( );
550 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
552 QString pName = pIt->first.c_str( );
553 switch( pIt->second.first )
555 case Parameters::String:
556 case Parameters::OpenFileName:
557 case Parameters::SaveFileName:
558 case Parameters::PathName:
559 case Parameters::IntList:
560 case Parameters::UintList:
561 case Parameters::RealList:
562 case Parameters::OpenFileNameList:
564 QLineEdit* v_string = this->findChild< QLineEdit* >( pName );
565 if( v_string != NULL )
566 v_string->setText( pIt->second.second.c_str( ) );
569 case Parameters::Bool:
571 QCheckBox* v_bool = this->findChild< QCheckBox* >( pName );
573 v_bool->setChecked( pIt->second.second == "1" );
576 case Parameters::Int:
577 case Parameters::Uint:
579 QSpinBox* v_uint = this->findChild< QSpinBox* >( pName );
582 std::istringstream tok_str( pIt->second.second );
585 v_uint->setValue( v );
590 case Parameters::Real:
592 QDoubleSpinBox* v_double = this->findChild< QDoubleSpinBox* >( pName );
595 std::istringstream tok_str( pIt->second.second );
598 v_double->setValue( v );
603 case Parameters::Index:
605 case Parameters::Point:
607 case Parameters::Vector:
609 case Parameters::StringList:
611 case Parameters::BoolList:
613 case Parameters::IndexList:
615 case Parameters::PointList:
617 case Parameters::VectorList:
619 case Parameters::SaveFileNameList:
621 case Parameters::PathNameList:
623 case Parameters::Choices:
625 QComboBox* v_choices = this->findChild< QComboBox* >( pName );
626 if( v_choices != NULL )
628 std::istringstream str_choices( pIt->second.second );
629 std::string choices, real_choice;
630 std::getline( str_choices, choices, '@' );
631 std::getline( str_choices, real_choice, '@' );
632 std::istringstream str( choices );
634 int id = -1, cont = 0;
635 while( std::getline( str, token, '#' ) )
637 if( token == real_choice )
644 v_choices->setCurrentIndex( id );
656 // -------------------------------------------------------------------------
657 void cpPlugins::Interface::ParametersQtDialog::
658 _dlg_OpenSingleFile( )
660 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
663 std::string bName = btn->objectName( ).toStdString( );
664 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
665 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
668 std::string param_name = line->objectName( ).toStdString( );
669 if( param_name != "" )
671 std::string param_value =
672 this->m_Parameters->GetOpenFileName( param_name );
673 if( param_value == "" )
676 filters << "Any file (*)";
678 // Show dialog and check if it was accepted
679 QFileDialog dialog( this );
680 dialog.setFileMode( QFileDialog::ExistingFile );
681 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
682 dialog.setNameFilters( filters );
683 dialog.setAcceptMode( QFileDialog::AcceptOpen );
685 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
694 // -------------------------------------------------------------------------
695 void cpPlugins::Interface::ParametersQtDialog::
696 _dlg_SaveSingleFile( )
698 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
701 std::string bName = btn->objectName( ).toStdString( );
702 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
703 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
706 std::string param_name = line->objectName( ).toStdString( );
707 if( param_name != "" )
709 std::string param_value =
710 this->m_Parameters->GetSaveFileName( param_name );
711 if( param_value == "" )
714 filters << "Any file (*)";
716 // Show dialog and check if it was accepted
717 QFileDialog dialog( this );
718 dialog.setFileMode( QFileDialog::AnyFile );
719 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
720 dialog.setNameFilters( filters );
721 dialog.setAcceptMode( QFileDialog::AcceptSave );
723 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
732 // -------------------------------------------------------------------------
733 void cpPlugins::Interface::ParametersQtDialog::
734 _dlg_OpenSinglePath( )
736 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
739 std::string bName = btn->objectName( ).toStdString( );
740 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
741 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
744 std::string param_name = line->objectName( ).toStdString( );
745 if( param_name != "" )
747 std::string param_value =
748 this->m_Parameters->GetPathName( param_name );
749 if( param_value == "" )
752 // Show dialog and check if it was accepted
753 QFileDialog dialog( this );
754 dialog.setFileMode( QFileDialog::Directory );
755 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
756 dialog.setAcceptMode( QFileDialog::AcceptOpen );
758 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
767 // -------------------------------------------------------------------------
768 void cpPlugins::Interface::ParametersQtDialog::
769 _dlg_OpenMultipleFiles( )
771 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
774 std::string bName = btn->objectName( ).toStdString( );
775 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
777 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
780 std::string param_name = line->objectName( ).toStdString( );
781 if( param_name != "" )
784 filters << "Any file (*)";
786 // Show dialog and check if it was accepted
787 QFileDialog dialog( this );
788 dialog.setFileMode( QFileDialog::ExistingFiles );
789 dialog.setNameFilters( filters );
790 dialog.setAcceptMode( QFileDialog::AcceptOpen );
793 if( dialog.selectedFiles( ).size( ) > 0 )
795 std::stringstream str;
796 auto files = dialog.selectedFiles( );
797 auto fIt = files.begin( );
798 str << fIt->toStdString( );
800 for( ; fIt != files.end( ); ++fIt )
801 str << "#" << fIt->toStdString( );
802 line->setText( str.str( ).c_str( ) );
815 // -------------------------------------------------------------------------
816 void cpPlugins::Interface::ParametersQtDialog::
819 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
822 std::string bName = btn->objectName( ).toStdString( );
823 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
824 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
827 std::string param_name = line->objectName( ).toStdString( );
828 if( param_name != "" )
832 QInputDialog::getInt(
835 std::string( "Add new value to \"" ) +
840 0, -2147483647, 2147483647, 1,
845 std::string values = line->text( ).toStdString( );
848 std::stringstream str;
849 str << values << value;
850 line->setText( str.str( ).c_str( ) );
861 // -------------------------------------------------------------------------
862 void cpPlugins::Interface::ParametersQtDialog::
865 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
868 std::string bName = btn->objectName( ).toStdString( );
869 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
870 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
873 std::string param_name = line->objectName( ).toStdString( );
874 if( param_name != "" )
878 QInputDialog::getInt(
881 std::string( "Add new value to \"" ) +
891 std::string values = line->text( ).toStdString( );
894 std::stringstream str;
895 str << values << value;
896 line->setText( str.str( ).c_str( ) );
907 // -------------------------------------------------------------------------
908 void cpPlugins::Interface::ParametersQtDialog::
911 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
914 std::string bName = btn->objectName( ).toStdString( );
915 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
916 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
919 std::string param_name = line->objectName( ).toStdString( );
920 if( param_name != "" )
924 QInputDialog::getDouble(
927 std::string( "Add new value to \"" ) +
932 0, -2147483647, 2147483647, 1,
937 std::string values = line->text( ).toStdString( );
940 std::stringstream str;
941 str << values << value;
942 line->setText( str.str( ).c_str( ) );
953 #endif // cpPlugins_Interface_QT4