]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/ParametersQtDialog.cxx
e28a086e2e1f069bf9c017e8851da56040ffdca5
[cpPlugins.git] / lib / cpPlugins / Interface / ParametersQtDialog.cxx
1 #include <cpPlugins/Interface/ParametersQtDialog.h>
2
3 #ifdef cpPlugins_Interface_QT4
4
5 #include <limits>
6
7 #include <cpPlugins/Interface/ParametersListWidget.h>
8
9 #include <vtkSmartPointer.h>
10 #include <cpExtensions/Interaction/SeedWidget.h>
11 #include <vtkPointHandleRepresentation3D.h>
12 #include <vtkProperty.h>
13 #include <vtkSeedRepresentation.h>
14
15 #include <QCheckBox>
16 #include <QDialogButtonBox>
17 #include <QDoubleSpinBox>
18 #include <QHBoxLayout>
19 #include <QLineEdit>
20 #include <QWidget>
21
22 // -------------------------------------------------------------------------
23 cpPlugins::Interface::ParametersQtDialog::
24 ParametersQtDialog( QWidget* parent, Qt::WindowFlags f )
25   : QDialog( parent, f ),
26     m_Parameters( NULL ),
27     m_IsModal( false ),
28     m_Interactor( NULL )
29 {
30   this->m_Title = new QLabel( this );
31   this->m_Title->setText( "Parameters dialog title" );
32
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 );
37 }
38
39 // -------------------------------------------------------------------------
40 cpPlugins::Interface::ParametersQtDialog::
41 ~ParametersQtDialog( )
42 {
43   delete this->m_Title;
44   delete this->m_MainLayout;
45   delete this->m_ToolsLayout;
46
47   for( unsigned int i = 0; i < this->m_Widgets.size( ); ++i )
48     this->m_Widgets[ i ]->Delete( );
49 }
50
51 // -------------------------------------------------------------------------
52 bool cpPlugins::Interface::ParametersQtDialog::
53 IsModal( ) const
54 {
55   return( this->m_IsModal );
56 }
57
58 // -------------------------------------------------------------------------
59 cpPlugins::Interface::
60 Parameters* cpPlugins::Interface::ParametersQtDialog::
61 getParameters( ) const
62 {
63   return( this->m_Parameters );
64 }
65
66 // -------------------------------------------------------------------------
67 void cpPlugins::Interface::ParametersQtDialog::
68 setInteractor( vtkRenderWindowInteractor* interactor )
69 {
70   this->m_Interactor = interactor;
71 }
72
73 // -------------------------------------------------------------------------
74 bool cpPlugins::Interface::ParametersQtDialog::
75 setParameters( Parameters* parameters )
76 {
77   this->m_IsModal = true;
78   this->m_Parameters = parameters;
79   if( this->m_Parameters == NULL )
80     return( false );
81
82   // Put values
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 )
87   {
88     Parameters::Type pt = this->m_Parameters->GetType( *nIt );
89
90     QWidget* w_input = NULL;
91     if( pt == Parameters::String )
92     {
93       QLineEdit* v_string = new QLineEdit( this );
94       v_string->setText( "Enter some text!!!" );
95       w_input = v_string;
96     }
97     else if( pt == Parameters::Bool )
98     {
99       QCheckBox* v_bool = new QCheckBox( this );
100       v_bool->setText( "[ON/OFF]" );
101       v_bool->setChecked( this->m_Parameters->GetBool( *nIt ) );
102       w_input = v_bool;
103     }
104     else if( pt == Parameters::Uint )
105     {
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 ) );
110       w_input = v_uint;
111     }
112     else if( pt == Parameters::Int )
113     {
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 ) );
118       w_input = v_int;
119     }
120     else if( pt == Parameters::Real )
121     {
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 ) );
127       w_input = v_double;
128     }
129     else if(
130       pt == Parameters::StringList ||
131       pt == Parameters::IntList ||
132       pt == Parameters::UintList ||
133       pt == Parameters::RealList
134       )
135     {
136       cpPlugins::Interface::ParametersListWidget* l_double =
137         new cpPlugins::Interface::ParametersListWidget( *nIt, this );
138       w_input = l_double;
139     }
140     else if( pt == Parameters::Point || pt == Parameters::Index )
141     {
142       if( this->m_Interactor != NULL )
143       {
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 );
151
152         cpExtensions::Interaction::SeedWidget* widget =
153           cpExtensions::Interaction::SeedWidget::New( );
154         widget->SetInteractor( this->m_Interactor );
155         widget->SetRepresentation( rep );
156         widget->On( );
157
158         this->m_Widgets.push_back( widget );
159         this->m_IsModal = false;
160
161       } // fi
162
163     } // fi
164
165     // Ok, a representation was created
166     if( w_input != NULL )
167     {
168       w_input->setObjectName( QString( nIt->c_str( ) ) );
169
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 );
176
177     } // fi
178
179   } // rof
180   return( this->m_IsModal );
181 }
182
183 // -------------------------------------------------------------------------
184 void cpPlugins::Interface::ParametersQtDialog::
185 setTitle( const std::string& title )
186 {
187   this->m_Title->setText( title.c_str( ) );
188 }
189
190 // -------------------------------------------------------------------------
191 int cpPlugins::Interface::ParametersQtDialog::
192 exec( )
193 {
194   if( !this->m_IsModal )
195     return( 0 );
196
197   // Add buttons
198   QDialogButtonBox* bb = new QDialogButtonBox(
199     QDialogButtonBox::Ok | QDialogButtonBox::Cancel
200     );
201   QObject::connect( bb, SIGNAL( accepted( ) ), this, SLOT( accept( ) ) );
202   QObject::connect( bb, SIGNAL( rejected( ) ), this, SLOT( reject( ) ) );
203   this->m_ToolsLayout->addWidget( bb );
204
205   int ret = this->QDialog::exec( );
206   this->syncParameters( );
207   return( ret );
208 }
209
210 // -------------------------------------------------------------------------
211 void cpPlugins::Interface::ParametersQtDialog::
212 show( )
213 {
214   if( this->m_IsModal )
215     return;
216
217   this->QDialog::show( );
218 }
219
220 // -------------------------------------------------------------------------
221 void cpPlugins::Interface::ParametersQtDialog::
222 syncParameters( )
223 {
224   std::cout << "TODO: SyncParameters" << std::endl;
225 }
226
227
228
229
230
231
232
233
234
235 /* TODO
236    enum Type
237    {
238    Index,
239    Point,
240    StringList,
241    BoolList,
242    IntList,
243    UintList,
244    IndexList,
245    PointList,
246    Choices,
247    NoType
248    };
249 */
250 /*
251   bool cpPlugins::Interface::
252   ParametersQtDialog(
253   Parameters* parameters, const std::string& title, QWidget* parent
254   )
255   {
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( );
262
263   // Put a title
264   QLabel* dlg_title = new QLabel( dlg );
265   dlg_title->setText( title.c_str( ) );
266   verticalLayout->addWidget( dlg_title );
267
268   // Put values
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 )
273   {
274   Parameters::Type pt = this->m_Parameters->GetType( *nIt );
275
276   QWidget* w_input = NULL;
277   if( pt == Parameters::String )
278   {
279   QLineEdit* v_string = new QLineEdit( dlg );
280   v_string->setText( "Enter some text!!!" );
281   w_input = v_string;
282   }
283   else if( pt == Parameters::Bool )
284   {
285   QCheckBox* v_bool = new QCheckBox( dlg );
286   v_bool->setText( "[ON/OFF]" );
287   v_bool->setChecked( this->m_Parameters->GetBool( *nIt ) );
288   w_input = v_bool;
289   }
290   else if( pt == Parameters::Uint )
291   {
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 ) );
296   w_input = v_uint;
297   }
298   else if( pt == Parameters::Int )
299   {
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 ) );
304   w_input = v_int;
305   }
306   else if( pt == Parameters::Real )
307   {
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 ) );
313   w_input = v_double;
314   }
315   else if(
316   pt == Parameters::StringList ||
317   pt == Parameters::IntList ||
318   pt == Parameters::UintList ||
319   pt == Parameters::RealList
320   )
321   {
322   cpPlugins::Interface::ParametersListWidget* l_double =
323   new cpPlugins::Interface::ParametersListWidget( *nIt, dlg );
324   w_input = l_double;
325
326   } // fi
327
328     // Ok, a representation was created
329     if( w_input != NULL )
330     {
331     w_input->setObjectName( QString( nIt->c_str( ) ) );
332
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 );
339
340     } // fi
341
342     } // rof
343
344     // Add buttons
345     QDialogButtonBox* bb = new QDialogButtonBox(
346     QDialogButtonBox::Ok | QDialogButtonBox::Cancel
347     );
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 );
352
353     // Execute
354     QMetaObject::connectSlotsByName( dlg );
355     if( !( dlg->exec( ) ) )
356     return( false );
357
358     // Get values back
359     nIt = names.begin( );
360     for( ; nIt != names.end( ); ++nIt )
361     {
362     Parameters::Type pt = this->m_Parameters->GetType( *nIt );
363     if( pt == Parameters::String )
364     {
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( ) );
368     }
369     else if( pt == Parameters::Bool )
370     {
371     QCheckBox* v_bool = dlg->findChild< QCheckBox* >( nIt->c_str( ) );
372     if( v_bool != NULL )
373     this->m_Parameters->SetBool( *nIt, v_bool->isChecked( ) );
374     }
375     else if( pt == Parameters::Uint )
376     {
377     QSpinBox* v_uint = dlg->findChild< QSpinBox* >( nIt->c_str( ) );
378     if( v_uint != NULL )
379     this->m_Parameters->SetUint( *nIt, v_uint->value( ) );
380     }
381     else if( pt == Parameters::Int )
382     {
383     QSpinBox* v_int = dlg->findChild< QSpinBox* >( nIt->c_str( ) );
384     if( v_int != NULL )
385     this->m_Parameters->SetInt( *nIt, v_int->value( ) );
386     }
387     else if( pt == Parameters::Real )
388     {
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( ) );
393     }
394     else if(
395     pt == Parameters::StringList ||
396     pt == Parameters::IntList ||
397     pt == Parameters::UintList ||
398     pt == Parameters::RealList
399     )
400     {
401     cpPlugins::Interface::ParametersListWidget* l_double =
402     dlg->findChild< cpPlugins::Interface::ParametersListWidget* >(
403     nIt->c_str( )
404     );
405     if( l_double != NULL )
406     {
407     if( pt == Parameters::StringList )
408     {
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 ] );
412     }
413     else if( pt == Parameters::IntList )
414     {
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 ] );
418     }
419     else if( pt == Parameters::UintList )
420     {
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 ] );
424     }
425     else if( pt == Parameters::RealList )
426     {
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 ] );
430
431     } // fi
432
433     } // fi
434
435     } // fi
436
437     } // rof
438     return( true );
439     }
440 */
441
442 #endif // cpPlugins_Interface_QT4
443
444 // eof - $RCSfile$