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>
28 class SingleSeedCommand
32 static SingleSeedCommand* New( )
33 { return( new SingleSeedCommand ); }
34 virtual void Execute( vtkObject* caller, unsigned long eid, void* data )
36 // Get seed, avoiding segfaults!!!
37 if( eid != vtkCommand::PlacePointEvent || this->Dialog == NULL )
39 vtkSeedWidget* widget = dynamic_cast< vtkSeedWidget* >( caller );
42 vtkSeedRepresentation* rep = widget->GetSeedRepresentation( );
45 if( rep->GetNumberOfSeeds( ) == 0 )
48 rep->GetSeedWorldPosition( 0, seed );
50 // Delete all seeds (remember that this command is just for one seed)
51 while( rep->GetNumberOfSeeds( ) > 0 )
52 widget->DeleteSeed( 0 );
54 if( this->Dialog->getParameters( )->HasIndex( this->Name ) )
57 else if( this->Dialog->getParameters( )->HasPoint( this->Name ) )
59 this->Dialog->getParameters( )->SetPoint( this->Name, 3, seed );
60 this->Dialog->updateParameters( );
61 auto filter = this->Dialog->getParameters( )->GetProcessObject( );
64 auto plugins = filter->GetPlugins( );
67 auto app = plugins->GetApplication( );
69 app->UpdateActualFilter( );
83 virtual ~SingleSeedCommand( )
88 cpPlugins::Interface::ParametersQtDialog* Dialog;
93 // -------------------------------------------------------------------------
94 cpPlugins::Interface::ParametersQtDialog::
95 ParametersQtDialog( QWidget* parent, Qt::WindowFlags f )
96 : QDialog( parent, f ),
98 m_Interactive( false )
100 this->m_Title = new QLabel( this );
101 this->m_Title->setText( "Parameters dialog title" );
103 this->m_MainLayout = new QGridLayout( this );
104 this->m_ToolsLayout = new QVBoxLayout( );
105 this->m_ToolsLayout->addWidget( this->m_Title );
106 this->m_MainLayout->addLayout( this->m_ToolsLayout, 0, 0, 1, 1 );
109 // -------------------------------------------------------------------------
110 cpPlugins::Interface::ParametersQtDialog::
111 ~ParametersQtDialog( )
113 delete this->m_Title;
114 delete this->m_ToolsLayout;
115 delete this->m_MainLayout;
118 // -------------------------------------------------------------------------
119 cpPlugins::Interface::
120 Parameters* cpPlugins::Interface::ParametersQtDialog::
121 getParameters( ) const
123 return( this->m_Parameters );
126 // -------------------------------------------------------------------------
127 void cpPlugins::Interface::ParametersQtDialog::
128 addInteractor( vtkRenderWindowInteractor* interactor )
130 this->m_Interactors.insert( interactor );
133 // -------------------------------------------------------------------------
134 cpPlugins::Interface::ParametersQtDialog::
135 TInteractors& cpPlugins::Interface::ParametersQtDialog::
138 return( this->m_Interactors );
141 // -------------------------------------------------------------------------
142 const cpPlugins::Interface::ParametersQtDialog::
143 TInteractors& cpPlugins::Interface::ParametersQtDialog::
144 getInteractors( ) const
146 return( this->m_Interactors );
149 // -------------------------------------------------------------------------
150 bool cpPlugins::Interface::ParametersQtDialog::
151 isInteractive( ) const
153 return( this->m_Interactive );
156 // -------------------------------------------------------------------------
157 void cpPlugins::Interface::ParametersQtDialog::
158 setInteractive( bool i )
160 this->m_Interactive = i;
163 // -------------------------------------------------------------------------
164 void cpPlugins::Interface::ParametersQtDialog::
167 this->m_Interactive = true;
170 // -------------------------------------------------------------------------
171 void cpPlugins::Interface::ParametersQtDialog::
174 this->m_Interactive = false;
177 // -------------------------------------------------------------------------
178 bool cpPlugins::Interface::ParametersQtDialog::
179 setParameters( Parameters* parameters )
181 if( this->m_Parameters != NULL || parameters == NULL )
183 this->m_Parameters = parameters;
186 auto filter = this->m_Parameters->GetProcessObject( );
187 std::stringstream title;
188 title << "Parameters for \"" << filter->GetName( ) << "\"";
189 this->m_Title->setText( title.str( ).c_str( ) );
192 this->m_Widgets.clear( );
193 auto& raw_params = this->m_Parameters->GetRawParameters( );
194 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
196 QWidget* w_input = NULL;
197 switch( pIt->second.first )
199 case Parameters::String:
201 QLineEdit* v_string = new QLineEdit( this );
202 v_string->setObjectName( pIt->first.c_str( ) );
203 v_string->setText( pIt->second.second.c_str( ) );
207 case Parameters::Bool:
209 QCheckBox* v_bool = new QCheckBox( this );
210 v_bool->setObjectName( pIt->first.c_str( ) );
211 v_bool->setText( "[ON/OFF]" );
212 v_bool->setChecked( pIt->second.second == "1" );
216 case Parameters::Int:
217 case Parameters::Uint:
219 std::istringstream tok_str( pIt->second.second );
222 QSpinBox* v_uint = new QSpinBox( this );
223 v_uint->setObjectName( pIt->first.c_str( ) );
224 if( pIt->second.first == Parameters::Uint )
225 v_uint->setMinimum( 0 );
227 v_uint->setMinimum( -std::numeric_limits< int >::max( ) );
228 v_uint->setMaximum( std::numeric_limits< int >::max( ) );
229 v_uint->setValue( v );
233 case Parameters::Real:
235 std::istringstream tok_str( pIt->second.second );
238 QDoubleSpinBox* v_double = new QDoubleSpinBox( this );
239 v_double->setObjectName( pIt->first.c_str( ) );
240 v_double->setDecimals( 3 );
241 v_double->setMinimum( -std::numeric_limits< double >::max( ) );
242 v_double->setMaximum( std::numeric_limits< double >::max( ) );
243 v_double->setValue( v );
247 case Parameters::Index:
249 case Parameters::Point:
251 case Parameters::Vector:
253 case Parameters::OpenFileName:
255 QFrame* frame = new QFrame( this );
256 QHBoxLayout* layout = new QHBoxLayout( frame );
257 QLineEdit* v_string = new QLineEdit( frame );
258 v_string->setObjectName( pIt->first.c_str( ) );
259 v_string->setText( pIt->second.second.c_str( ) );
260 QPushButton* v_button = new QPushButton( frame );
261 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
262 v_button->setText( "..." );
264 v_button, SIGNAL( clicked( ) ),
265 this, SLOT( _dlg_OpenSingleFile( ) )
267 layout->addWidget( v_string );
268 layout->addWidget( v_button );
271 case Parameters::SaveFileName:
273 QFrame* frame = new QFrame( this );
274 QHBoxLayout* layout = new QHBoxLayout( frame );
275 QLineEdit* v_string = new QLineEdit( frame );
276 v_string->setObjectName( pIt->first.c_str( ) );
277 v_string->setText( pIt->second.second.c_str( ) );
278 QPushButton* v_button = new QPushButton( frame );
279 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
280 v_button->setText( "..." );
282 v_button, SIGNAL( clicked( ) ),
283 this, SLOT( _dlg_SaveSingleFile( ) )
285 layout->addWidget( v_string );
286 layout->addWidget( v_button );
290 case Parameters::PathName:
292 QFrame* frame = new QFrame( this );
293 QHBoxLayout* layout = new QHBoxLayout( frame );
294 QLineEdit* v_string = new QLineEdit( frame );
295 v_string->setObjectName( pIt->first.c_str( ) );
296 v_string->setText( pIt->second.second.c_str( ) );
297 QPushButton* v_button = new QPushButton( frame );
298 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
299 v_button->setText( "..." );
301 v_button, SIGNAL( clicked( ) ),
302 this, SLOT( _dlg_OpenSinglePath( ) )
304 layout->addWidget( v_string );
305 layout->addWidget( v_button );
309 case Parameters::StringList:
311 case Parameters::BoolList:
313 case Parameters::IntList:
315 QFrame* frame = new QFrame( this );
316 QHBoxLayout* layout = new QHBoxLayout( frame );
317 QLineEdit* v_string = new QLineEdit( frame );
318 v_string->setObjectName( pIt->first.c_str( ) );
319 v_string->setText( pIt->second.second.c_str( ) );
320 QPushButton* v_button = new QPushButton( frame );
321 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
322 v_button->setText( "+" );
324 v_button, SIGNAL( clicked( ) ),
325 this, SLOT( _dlg_AddInt( ) )
327 layout->addWidget( v_string );
328 layout->addWidget( v_button );
332 case Parameters::UintList:
334 QFrame* frame = new QFrame( this );
335 QHBoxLayout* layout = new QHBoxLayout( frame );
336 QLineEdit* v_string = new QLineEdit( frame );
337 v_string->setObjectName( pIt->first.c_str( ) );
338 v_string->setText( pIt->second.second.c_str( ) );
339 QPushButton* v_button = new QPushButton( frame );
340 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
341 v_button->setText( "+" );
343 v_button, SIGNAL( clicked( ) ),
344 this, SLOT( _dlg_AddUint( ) )
346 layout->addWidget( v_string );
347 layout->addWidget( v_button );
351 case Parameters::RealList:
353 QFrame* frame = new QFrame( this );
354 QHBoxLayout* layout = new QHBoxLayout( frame );
355 QLineEdit* v_string = new QLineEdit( frame );
356 v_string->setObjectName( pIt->first.c_str( ) );
357 v_string->setText( pIt->second.second.c_str( ) );
358 QPushButton* v_button = new QPushButton( frame );
359 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
360 v_button->setText( "+" );
362 v_button, SIGNAL( clicked( ) ),
363 this, SLOT( _dlg_AddReal( ) )
365 layout->addWidget( v_string );
366 layout->addWidget( v_button );
370 case Parameters::IndexList:
372 case Parameters::PointList:
374 case Parameters::VectorList:
376 case Parameters::OpenFileNameList:
378 QFrame* frame = new QFrame( this );
379 QHBoxLayout* layout = new QHBoxLayout( frame );
380 QLineEdit* v_string = new QLineEdit( frame );
381 v_string->setObjectName( pIt->first.c_str( ) );
382 v_string->setText( pIt->second.second.c_str( ) );
383 QPushButton* v_button = new QPushButton( frame );
384 v_button->setObjectName( ( pIt->first + "_=?btn" ).c_str( ) );
385 v_button->setText( "..." );
387 v_button, SIGNAL( clicked( ) ),
388 this, SLOT( _dlg_OpenMultipleFiles( ) )
390 layout->addWidget( v_string );
391 layout->addWidget( v_button );
395 case Parameters::SaveFileNameList:
397 case Parameters::PathNameList:
399 case Parameters::Choices:
401 QComboBox* v_choices = new QComboBox( this );
402 v_choices->setObjectName( pIt->first.c_str( ) );
404 std::istringstream str0( pIt->second.second );
406 std::getline( str0, choices, '@' );
407 std::istringstream str1( choices );
410 while( std::getline( str1, token, '#' ) )
411 v_choices->insertItem( id++, token.c_str( ) );
420 // Ok, a representation was created
421 if( w_input != NULL )
423 QHBoxLayout* new_layout = new QHBoxLayout( );
424 QLabel* label = new QLabel( this );
425 label->setText( QString( pIt->first.c_str( ) ) );
426 new_layout->addWidget( label );
427 new_layout->addWidget( w_input );
428 this->m_ToolsLayout->addLayout( new_layout );
435 this->m_Buttons = new QDialogButtonBox(
436 QDialogButtonBox::Ok | QDialogButtonBox::Cancel
439 this->m_Buttons, SIGNAL( accepted( ) ), this, SLOT( accept( ) )
442 this->m_Buttons, SIGNAL( rejected( ) ), this, SLOT( reject( ) )
444 this->m_ToolsLayout->addWidget( this->m_Buttons );
452 // -------------------------------------------------------------------------
453 int cpPlugins::Interface::ParametersQtDialog::
456 int ret = this->QDialog::exec( );
458 this->updateParameters( );
464 // -------------------------------------------------------------------------
465 void cpPlugins::Interface::ParametersQtDialog::
468 this->QDialog::show( );
471 // -------------------------------------------------------------------------
472 void cpPlugins::Interface::ParametersQtDialog::
475 if( this->m_Parameters == NULL )
479 auto& raw_params = this->m_Parameters->GetRawParameters( );
480 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
482 QString pName = pIt->first.c_str( );
483 switch( pIt->second.first )
485 case Parameters::String:
486 case Parameters::OpenFileName:
487 case Parameters::SaveFileName:
488 case Parameters::PathName:
489 case Parameters::IntList:
490 case Parameters::UintList:
491 case Parameters::RealList:
492 case Parameters::OpenFileNameList:
494 QLineEdit* v_string = this->findChild< QLineEdit* >( pName );
495 if( v_string != NULL )
496 pIt->second.second = v_string->text( ).toStdString( );
499 case Parameters::Bool:
501 QCheckBox* v_bool = this->findChild< QCheckBox* >( pName );
503 pIt->second.second = ( v_bool->isChecked( ) )? "1": "0";
506 case Parameters::Int:
507 case Parameters::Uint:
509 QSpinBox* v_uint = this->findChild< QSpinBox* >( pName );
512 std::stringstream str;
513 str << v_uint->value( );
514 pIt->second.second = str.str( );
519 case Parameters::Real:
521 QDoubleSpinBox* v_double = this->findChild< QDoubleSpinBox* >( pName );
524 std::stringstream str;
525 str << v_double->value( );
526 pIt->second.second = str.str( );
531 case Parameters::Index:
533 case Parameters::Point:
535 case Parameters::Vector:
537 case Parameters::StringList:
539 case Parameters::BoolList:
541 case Parameters::IndexList:
543 case Parameters::PointList:
545 case Parameters::VectorList:
547 case Parameters::SaveFileNameList:
549 case Parameters::PathNameList:
551 case Parameters::Choices:
553 QComboBox* v_choices = this->findChild< QComboBox* >( pName );
554 if( v_choices != NULL )
556 std::istringstream str_choices( pIt->second.second );
557 std::string real_choices;
558 std::getline( str_choices, real_choices, '@' );
561 v_choices->currentText( ).toStdString( );
573 // -------------------------------------------------------------------------
574 void cpPlugins::Interface::ParametersQtDialog::
577 if( this->m_Parameters == NULL )
581 auto& raw_params = this->m_Parameters->GetRawParameters( );
582 for( auto pIt = raw_params.begin( ); pIt != raw_params.end( ); ++pIt )
584 QString pName = pIt->first.c_str( );
585 switch( pIt->second.first )
587 case Parameters::String:
588 case Parameters::OpenFileName:
589 case Parameters::SaveFileName:
590 case Parameters::PathName:
591 case Parameters::IntList:
592 case Parameters::UintList:
593 case Parameters::RealList:
594 case Parameters::OpenFileNameList:
596 QLineEdit* v_string = this->findChild< QLineEdit* >( pName );
597 if( v_string != NULL )
598 v_string->setText( pIt->second.second.c_str( ) );
601 case Parameters::Bool:
603 QCheckBox* v_bool = this->findChild< QCheckBox* >( pName );
605 v_bool->setChecked( pIt->second.second == "1" );
608 case Parameters::Int:
609 case Parameters::Uint:
611 QSpinBox* v_uint = this->findChild< QSpinBox* >( pName );
614 std::istringstream tok_str( pIt->second.second );
617 v_uint->setValue( v );
622 case Parameters::Real:
624 QDoubleSpinBox* v_double = this->findChild< QDoubleSpinBox* >( pName );
627 std::istringstream tok_str( pIt->second.second );
630 v_double->setValue( v );
635 case Parameters::Index:
637 case Parameters::Point:
639 case Parameters::Vector:
641 case Parameters::StringList:
643 case Parameters::BoolList:
645 case Parameters::IndexList:
647 case Parameters::PointList:
649 case Parameters::VectorList:
651 case Parameters::SaveFileNameList:
653 case Parameters::PathNameList:
655 case Parameters::Choices:
657 QComboBox* v_choices = this->findChild< QComboBox* >( pName );
658 if( v_choices != NULL )
660 std::istringstream str_choices( pIt->second.second );
661 std::string choices, real_choice;
662 std::getline( str_choices, choices, '@' );
663 std::getline( str_choices, real_choice, '@' );
664 std::istringstream str( choices );
666 int id = -1, cont = 0;
667 while( std::getline( str, token, '#' ) )
669 if( token == real_choice )
676 v_choices->setCurrentIndex( id );
688 // -------------------------------------------------------------------------
689 void cpPlugins::Interface::ParametersQtDialog::
690 _dlg_OpenSingleFile( )
692 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
695 std::string bName = btn->objectName( ).toStdString( );
696 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
697 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
700 std::string param_name = line->objectName( ).toStdString( );
701 if( param_name != "" )
703 std::string param_value =
704 this->m_Parameters->GetOpenFileName( param_name );
705 if( param_value == "" )
708 filters << "Any file (*)";
710 // Show dialog and check if it was accepted
711 QFileDialog dialog( this );
712 dialog.setFileMode( QFileDialog::ExistingFile );
713 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
714 dialog.setNameFilters( filters );
715 dialog.setAcceptMode( QFileDialog::AcceptOpen );
717 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
726 // -------------------------------------------------------------------------
727 void cpPlugins::Interface::ParametersQtDialog::
728 _dlg_SaveSingleFile( )
730 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
733 std::string bName = btn->objectName( ).toStdString( );
734 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
735 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
738 std::string param_name = line->objectName( ).toStdString( );
739 if( param_name != "" )
741 std::string param_value =
742 this->m_Parameters->GetSaveFileName( param_name );
743 if( param_value == "" )
746 filters << "Any file (*)";
748 // Show dialog and check if it was accepted
749 QFileDialog dialog( this );
750 dialog.setFileMode( QFileDialog::AnyFile );
751 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
752 dialog.setNameFilters( filters );
753 dialog.setAcceptMode( QFileDialog::AcceptSave );
755 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
764 // -------------------------------------------------------------------------
765 void cpPlugins::Interface::ParametersQtDialog::
766 _dlg_OpenSinglePath( )
768 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
771 std::string bName = btn->objectName( ).toStdString( );
772 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
773 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
776 std::string param_name = line->objectName( ).toStdString( );
777 if( param_name != "" )
779 std::string param_value =
780 this->m_Parameters->GetPathName( param_name );
781 if( param_value == "" )
784 // Show dialog and check if it was accepted
785 QFileDialog dialog( this );
786 dialog.setFileMode( QFileDialog::Directory );
787 dialog.setDirectory( QFileDialog::tr( param_value.c_str( ) ) );
788 dialog.setAcceptMode( QFileDialog::AcceptOpen );
790 line->setText( *( dialog.selectedFiles( ).begin( ) ) );
799 // -------------------------------------------------------------------------
800 void cpPlugins::Interface::ParametersQtDialog::
801 _dlg_OpenMultipleFiles( )
803 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
806 std::string bName = btn->objectName( ).toStdString( );
807 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
809 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
812 std::string param_name = line->objectName( ).toStdString( );
813 if( param_name != "" )
816 filters << "Any file (*)";
818 // Show dialog and check if it was accepted
819 QFileDialog dialog( this );
820 dialog.setFileMode( QFileDialog::ExistingFiles );
821 dialog.setNameFilters( filters );
822 dialog.setAcceptMode( QFileDialog::AcceptOpen );
825 if( dialog.selectedFiles( ).size( ) > 0 )
827 std::stringstream str;
828 auto files = dialog.selectedFiles( );
829 auto fIt = files.begin( );
830 str << fIt->toStdString( );
832 for( ; fIt != files.end( ); ++fIt )
833 str << "#" << fIt->toStdString( );
834 line->setText( str.str( ).c_str( ) );
847 // -------------------------------------------------------------------------
848 void cpPlugins::Interface::ParametersQtDialog::
851 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
854 std::string bName = btn->objectName( ).toStdString( );
855 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
856 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
859 std::string param_name = line->objectName( ).toStdString( );
860 if( param_name != "" )
864 QInputDialog::getInt(
867 std::string( "Add new value to \"" ) +
872 0, -2147483647, 2147483647, 1,
877 std::string values = line->text( ).toStdString( );
880 std::stringstream str;
881 str << values << value;
882 line->setText( str.str( ).c_str( ) );
893 // -------------------------------------------------------------------------
894 void cpPlugins::Interface::ParametersQtDialog::
897 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
900 std::string bName = btn->objectName( ).toStdString( );
901 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
902 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
905 std::string param_name = line->objectName( ).toStdString( );
906 if( param_name != "" )
910 QInputDialog::getInt(
913 std::string( "Add new value to \"" ) +
923 std::string values = line->text( ).toStdString( );
926 std::stringstream str;
927 str << values << value;
928 line->setText( str.str( ).c_str( ) );
939 // -------------------------------------------------------------------------
940 void cpPlugins::Interface::ParametersQtDialog::
943 QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
946 std::string bName = btn->objectName( ).toStdString( );
947 std::string line_name = bName.substr( 0, bName.find( "_=?btn" ) );
948 QLineEdit* line = this->findChild< QLineEdit* >( line_name.c_str( ) );
951 std::string param_name = line->objectName( ).toStdString( );
952 if( param_name != "" )
956 QInputDialog::getDouble(
959 std::string( "Add new value to \"" ) +
964 0, -2147483647, 2147483647, 1,
969 std::string values = line->text( ).toStdString( );
972 std::stringstream str;
973 str << values << value;
974 line->setText( str.str( ).c_str( ) );
985 #endif // cpPlugins_Interface_QT4