1 #include <cpPlugins/Interface/ParametersQtDialog.h>
3 #ifdef cpPlugins_Interface_QT4
7 #include <cpPlugins/Interface/ParametersListWidget.h>
9 #include <vtkSmartPointer.h>
10 #include <cpExtensions/Interaction/SeedWidget.h>
11 #include <vtkPointHandleRepresentation3D.h>
12 #include <vtkProperty.h>
13 #include <vtkSeedRepresentation.h>
16 #include <QDialogButtonBox>
17 #include <QDoubleSpinBox>
18 #include <QHBoxLayout>
22 // -------------------------------------------------------------------------
23 cpPlugins::Interface::ParametersQtDialog::
24 ParametersQtDialog( QWidget* parent, Qt::WindowFlags f )
25 : QDialog( parent, f ),
30 this->m_Title = new QLabel( this );
31 this->m_Title->setText( "Parameters dialog title" );
33 this->m_MainLayout = new QGridLayout( this );
34 this->m_ToolsLayout = new QVBoxLayout( );
35 this->m_ToolsLayout->addWidget( this->m_Title );
36 this->m_MainLayout->addLayout( this->m_ToolsLayout, 0, 0, 1, 1 );
39 // -------------------------------------------------------------------------
40 cpPlugins::Interface::ParametersQtDialog::
41 ~ParametersQtDialog( )
44 delete this->m_MainLayout;
45 delete this->m_ToolsLayout;
47 for( unsigned int i = 0; i < this->m_Widgets.size( ); ++i )
48 this->m_Widgets[ i ]->Delete( );
51 // -------------------------------------------------------------------------
52 bool cpPlugins::Interface::ParametersQtDialog::
55 return( this->m_IsModal );
58 // -------------------------------------------------------------------------
59 cpPlugins::Interface::
60 Parameters* cpPlugins::Interface::ParametersQtDialog::
61 getParameters( ) const
63 return( this->m_Parameters );
66 // -------------------------------------------------------------------------
67 void cpPlugins::Interface::ParametersQtDialog::
68 setInteractor( vtkRenderWindowInteractor* interactor )
70 this->m_Interactor = interactor;
73 // -------------------------------------------------------------------------
74 bool cpPlugins::Interface::ParametersQtDialog::
75 setParameters( Parameters* parameters )
77 this->m_IsModal = true;
78 this->m_Parameters = parameters;
79 if( this->m_Parameters == NULL )
83 std::vector< std::string > names;
84 this->m_Parameters->GetNames( names );
85 std::vector< std::string >::const_iterator nIt = names.begin( );
86 for( ; nIt != names.end( ); ++nIt )
88 Parameters::Type pt = this->m_Parameters->GetType( *nIt );
90 QWidget* w_input = NULL;
91 if( pt == Parameters::String )
93 QLineEdit* v_string = new QLineEdit( this );
94 v_string->setText( "Enter some text!!!" );
97 else if( pt == Parameters::Bool )
99 QCheckBox* v_bool = new QCheckBox( this );
100 v_bool->setText( "[ON/OFF]" );
101 v_bool->setChecked( this->m_Parameters->GetBool( *nIt ) );
104 else if( pt == Parameters::Uint )
106 QSpinBox* v_uint = new QSpinBox( this );
107 v_uint->setMinimum( 0 );
108 v_uint->setMaximum( std::numeric_limits< int >::max( ) );
109 v_uint->setValue( this->m_Parameters->GetUint( *nIt ) );
112 else if( pt == Parameters::Int )
114 QSpinBox* v_int = new QSpinBox( this );
115 v_int->setMinimum( -std::numeric_limits< int >::max( ) );
116 v_int->setMaximum( std::numeric_limits< int >::max( ) );
117 v_int->setValue( this->m_Parameters->GetInt( *nIt ) );
120 else if( pt == Parameters::Real )
122 QDoubleSpinBox* v_double = new QDoubleSpinBox( this );
123 v_double->setDecimals( 3 );
124 v_double->setMinimum( -std::numeric_limits< double >::max( ) );
125 v_double->setMaximum( std::numeric_limits< double >::max( ) );
126 v_double->setValue( this->m_Parameters->GetReal( *nIt ) );
130 pt == Parameters::StringList ||
131 pt == Parameters::IntList ||
132 pt == Parameters::UintList ||
133 pt == Parameters::RealList
136 cpPlugins::Interface::ParametersListWidget* l_double =
137 new cpPlugins::Interface::ParametersListWidget( *nIt, this );
140 else if( pt == Parameters::Point || pt == Parameters::Index )
142 if( this->m_Interactor != NULL )
144 // Create a point widget and its representation
145 vtkSmartPointer< vtkPointHandleRepresentation3D > handle =
146 vtkSmartPointer< vtkPointHandleRepresentation3D >::New( );
147 handle->GetProperty( )->SetColor( 1, 0, 0 );
148 vtkSmartPointer< vtkSeedRepresentation > rep =
149 vtkSmartPointer< vtkSeedRepresentation >::New( );
150 rep->SetHandleRepresentation( handle );
152 cpExtensions::Interaction::SeedWidget* widget =
153 cpExtensions::Interaction::SeedWidget::New( );
154 widget->SetInteractor( this->m_Interactor );
155 widget->SetRepresentation( rep );
158 this->m_Widgets.push_back( widget );
159 this->m_IsModal = false;
165 // Ok, a representation was created
166 if( w_input != NULL )
168 w_input->setObjectName( QString( nIt->c_str( ) ) );
170 QHBoxLayout* new_layout = new QHBoxLayout( );
171 QLabel* label = new QLabel( this );
172 label->setText( QString( nIt->c_str( ) ) );
173 new_layout->addWidget( label );
174 new_layout->addWidget( w_input );
175 this->m_ToolsLayout->addLayout( new_layout );
180 return( this->m_IsModal );
183 // -------------------------------------------------------------------------
184 void cpPlugins::Interface::ParametersQtDialog::
185 setTitle( const std::string& title )
187 this->m_Title->setText( title.c_str( ) );
190 // -------------------------------------------------------------------------
191 int cpPlugins::Interface::ParametersQtDialog::
194 if( !this->m_IsModal )
198 QDialogButtonBox* bb = new QDialogButtonBox(
199 QDialogButtonBox::Ok | QDialogButtonBox::Cancel
201 QObject::connect( bb, SIGNAL( accepted( ) ), this, SLOT( accept( ) ) );
202 QObject::connect( bb, SIGNAL( rejected( ) ), this, SLOT( reject( ) ) );
203 this->m_ToolsLayout->addWidget( bb );
205 int ret = this->QDialog::exec( );
206 this->syncParameters( );
210 // -------------------------------------------------------------------------
211 void cpPlugins::Interface::ParametersQtDialog::
214 if( this->m_IsModal )
217 this->QDialog::show( );
220 // -------------------------------------------------------------------------
221 void cpPlugins::Interface::ParametersQtDialog::
224 std::cout << "TODO: SyncParameters" << std::endl;
251 bool cpPlugins::Interface::
253 Parameters* parameters, const std::string& title, QWidget* parent
256 // Create dialog with a simple layout
257 QDialog* dlg = new QDialog( parent );
258 dlg->setWindowFlags( Qt::FramelessWindowHint );
259 dlg->setWindowFlags( Qt::WindowTitleHint );
260 QGridLayout* gridLayout = new QGridLayout( dlg );
261 QVBoxLayout* verticalLayout = new QVBoxLayout( );
264 QLabel* dlg_title = new QLabel( dlg );
265 dlg_title->setText( title.c_str( ) );
266 verticalLayout->addWidget( dlg_title );
269 std::vector< std::string > names;
270 this->m_Parameters->GetNames( names );
271 std::vector< std::string >::const_iterator nIt = names.begin( );
272 for( ; nIt != names.end( ); ++nIt )
274 Parameters::Type pt = this->m_Parameters->GetType( *nIt );
276 QWidget* w_input = NULL;
277 if( pt == Parameters::String )
279 QLineEdit* v_string = new QLineEdit( dlg );
280 v_string->setText( "Enter some text!!!" );
283 else if( pt == Parameters::Bool )
285 QCheckBox* v_bool = new QCheckBox( dlg );
286 v_bool->setText( "[ON/OFF]" );
287 v_bool->setChecked( this->m_Parameters->GetBool( *nIt ) );
290 else if( pt == Parameters::Uint )
292 QSpinBox* v_uint = new QSpinBox( dlg );
293 v_uint->setMinimum( 0 );
294 v_uint->setMaximum( std::numeric_limits< int >::max( ) );
295 v_uint->setValue( this->m_Parameters->GetUint( *nIt ) );
298 else if( pt == Parameters::Int )
300 QSpinBox* v_int = new QSpinBox( dlg );
301 v_int->setMinimum( -std::numeric_limits< int >::max( ) );
302 v_int->setMaximum( std::numeric_limits< int >::max( ) );
303 v_int->setValue( this->m_Parameters->GetInt( *nIt ) );
306 else if( pt == Parameters::Real )
308 QDoubleSpinBox* v_double = new QDoubleSpinBox( dlg );
309 v_double->setDecimals( 3 );
310 v_double->setMinimum( -std::numeric_limits< double >::max( ) );
311 v_double->setMaximum( std::numeric_limits< double >::max( ) );
312 v_double->setValue( this->m_Parameters->GetReal( *nIt ) );
316 pt == Parameters::StringList ||
317 pt == Parameters::IntList ||
318 pt == Parameters::UintList ||
319 pt == Parameters::RealList
322 cpPlugins::Interface::ParametersListWidget* l_double =
323 new cpPlugins::Interface::ParametersListWidget( *nIt, dlg );
328 // Ok, a representation was created
329 if( w_input != NULL )
331 w_input->setObjectName( QString( nIt->c_str( ) ) );
333 QHBoxLayout* horizontalLayout = new QHBoxLayout( );
334 QLabel* label = new QLabel( dlg );
335 label->setText( QString( nIt->c_str( ) ) );
336 horizontalLayout->addWidget( label );
337 horizontalLayout->addWidget( w_input );
338 verticalLayout->addLayout( horizontalLayout );
345 QDialogButtonBox* bb = new QDialogButtonBox(
346 QDialogButtonBox::Ok | QDialogButtonBox::Cancel
348 QObject::connect( bb, SIGNAL( accepted( ) ), dlg, SLOT( accept( ) ) );
349 QObject::connect( bb, SIGNAL( rejected( ) ), dlg, SLOT( reject( ) ) );
350 verticalLayout->addWidget( bb );
351 gridLayout->addLayout( verticalLayout, 0, 0, 1, 1 );
354 QMetaObject::connectSlotsByName( dlg );
355 if( !( dlg->exec( ) ) )
359 nIt = names.begin( );
360 for( ; nIt != names.end( ); ++nIt )
362 Parameters::Type pt = this->m_Parameters->GetType( *nIt );
363 if( pt == Parameters::String )
365 QLineEdit* v_string = dlg->findChild< QLineEdit* >( nIt->c_str( ) );
366 if( v_string != NULL )
367 this->m_Parameters->SetString( *nIt, v_string->text( ).toStdString( ) );
369 else if( pt == Parameters::Bool )
371 QCheckBox* v_bool = dlg->findChild< QCheckBox* >( nIt->c_str( ) );
373 this->m_Parameters->SetBool( *nIt, v_bool->isChecked( ) );
375 else if( pt == Parameters::Uint )
377 QSpinBox* v_uint = dlg->findChild< QSpinBox* >( nIt->c_str( ) );
379 this->m_Parameters->SetUint( *nIt, v_uint->value( ) );
381 else if( pt == Parameters::Int )
383 QSpinBox* v_int = dlg->findChild< QSpinBox* >( nIt->c_str( ) );
385 this->m_Parameters->SetInt( *nIt, v_int->value( ) );
387 else if( pt == Parameters::Real )
389 QDoubleSpinBox* v_double =
390 dlg->findChild< QDoubleSpinBox* >( nIt->c_str( ) );
391 if( v_double != NULL )
392 this->m_Parameters->SetReal( *nIt, v_double->value( ) );
395 pt == Parameters::StringList ||
396 pt == Parameters::IntList ||
397 pt == Parameters::UintList ||
398 pt == Parameters::RealList
401 cpPlugins::Interface::ParametersListWidget* l_double =
402 dlg->findChild< cpPlugins::Interface::ParametersListWidget* >(
405 if( l_double != NULL )
407 if( pt == Parameters::StringList )
409 std::vector< std::string > values = l_double->GetStringValues( );
410 for( int r = 0; r < values.size( ); ++r )
411 this->m_Parameters->AddToStringList( *nIt, values[ r ] );
413 else if( pt == Parameters::IntList )
415 std::vector< int > values = l_double->GetIntValues( );
416 for( int r = 0; r < values.size( ); ++r )
417 this->m_Parameters->AddToIntList( *nIt, values[ r ] );
419 else if( pt == Parameters::UintList )
421 std::vector< unsigned int > values = l_double->GetUintValues( );
422 for( int r = 0; r < values.size( ); ++r )
423 this->m_Parameters->AddToUintList( *nIt, values[ r ] );
425 else if( pt == Parameters::RealList )
427 std::vector< double > values = l_double->GetDoubleValues( );
428 for( int r = 0; r < values.size( ); ++r )
429 this->m_Parameters->AddToRealList( *nIt, values[ r ] );
442 #endif // cpPlugins_Interface_QT4