]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/ParametersQtDialog.cxx
Widgets updated
[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   return( this->QDialog::exec( ) );
206 }
207
208 // -------------------------------------------------------------------------
209 void cpPlugins::Interface::ParametersQtDialog::
210 show( )
211 {
212   if( this->m_IsModal )
213     return;
214
215   this->QDialog::show( );
216 }
217
218 // -------------------------------------------------------------------------
219 void cpPlugins::Interface::ParametersQtDialog::
220 syncParameters( )
221 {
222   std::cout << "TODO: SyncParameters" << std::endl;
223 }
224
225
226
227
228
229
230
231
232
233     /* TODO
234        enum Type
235        {
236        Index,
237        Point,
238        StringList,
239        BoolList,
240        IntList,
241        UintList,
242        IndexList,
243        PointList,
244        Choices,
245        NoType
246        };
247     */
248 /*
249 bool cpPlugins::Interface::
250 ParametersQtDialog(
251   Parameters* parameters, const std::string& title, QWidget* parent
252   )
253 {
254   // Create dialog with a simple layout
255   QDialog* dlg = new QDialog( parent );
256   dlg->setWindowFlags( Qt::FramelessWindowHint ); 
257   dlg->setWindowFlags( Qt::WindowTitleHint );
258   QGridLayout* gridLayout = new QGridLayout( dlg );
259   QVBoxLayout* verticalLayout = new QVBoxLayout( );
260
261   // Put a title
262   QLabel* dlg_title = new QLabel( dlg );
263   dlg_title->setText( title.c_str( ) );
264   verticalLayout->addWidget( dlg_title );
265
266   // Put values
267   std::vector< std::string > names;
268   this->m_Parameters->GetNames( names );
269   std::vector< std::string >::const_iterator nIt = names.begin( );
270   for( ; nIt != names.end( ); ++nIt )
271   {
272     Parameters::Type pt = this->m_Parameters->GetType( *nIt );
273
274     QWidget* w_input = NULL;
275     if( pt == Parameters::String )
276     {
277       QLineEdit* v_string = new QLineEdit( dlg );
278       v_string->setText( "Enter some text!!!" );
279       w_input = v_string;
280     }
281     else if( pt == Parameters::Bool )
282     {
283       QCheckBox* v_bool = new QCheckBox( dlg );
284       v_bool->setText( "[ON/OFF]" );
285       v_bool->setChecked( this->m_Parameters->GetBool( *nIt ) );
286       w_input = v_bool;
287     }
288     else if( pt == Parameters::Uint )
289     {
290       QSpinBox* v_uint = new QSpinBox( dlg );
291       v_uint->setMinimum( 0 );
292       v_uint->setMaximum( std::numeric_limits< int >::max( ) );
293       v_uint->setValue( this->m_Parameters->GetUint( *nIt ) );
294       w_input = v_uint;
295     }
296     else if( pt == Parameters::Int )
297     {
298       QSpinBox* v_int = new QSpinBox( dlg );
299       v_int->setMinimum( -std::numeric_limits< int >::max( ) );
300       v_int->setMaximum(  std::numeric_limits< int >::max( ) );
301       v_int->setValue( this->m_Parameters->GetInt( *nIt ) );
302       w_input = v_int;
303     }
304     else if( pt == Parameters::Real )
305     {
306       QDoubleSpinBox* v_double = new QDoubleSpinBox( dlg );
307       v_double->setDecimals( 3 );
308       v_double->setMinimum( -std::numeric_limits< double >::max( ) );
309       v_double->setMaximum(  std::numeric_limits< double >::max( ) );
310       v_double->setValue( this->m_Parameters->GetReal( *nIt ) );
311       w_input = v_double;
312     }
313     else if(
314       pt == Parameters::StringList ||
315       pt == Parameters::IntList ||
316       pt == Parameters::UintList ||
317       pt == Parameters::RealList
318       )
319     {
320       cpPlugins::Interface::ParametersListWidget* l_double =
321         new cpPlugins::Interface::ParametersListWidget( *nIt, dlg );
322       w_input = l_double;
323
324     } // fi
325
326     // Ok, a representation was created
327     if( w_input != NULL )
328     {
329       w_input->setObjectName( QString( nIt->c_str( ) ) );
330
331       QHBoxLayout* horizontalLayout = new QHBoxLayout( );
332       QLabel* label = new QLabel( dlg );
333       label->setText( QString( nIt->c_str( ) ) );
334       horizontalLayout->addWidget( label );
335       horizontalLayout->addWidget( w_input );
336       verticalLayout->addLayout( horizontalLayout );
337
338     } // fi
339
340   } // rof
341
342   // Add buttons
343   QDialogButtonBox* bb = new QDialogButtonBox(
344     QDialogButtonBox::Ok | QDialogButtonBox::Cancel
345     );
346   QObject::connect( bb, SIGNAL( accepted( ) ), dlg, SLOT( accept( ) ) );
347   QObject::connect( bb, SIGNAL( rejected( ) ), dlg, SLOT( reject( ) ) );
348   verticalLayout->addWidget( bb );
349   gridLayout->addLayout( verticalLayout, 0, 0, 1, 1 );
350
351   // Execute
352   QMetaObject::connectSlotsByName( dlg );
353   if( !( dlg->exec( ) ) )
354     return( false );
355
356   // Get values back
357   nIt = names.begin( );
358   for( ; nIt != names.end( ); ++nIt )
359   {
360     Parameters::Type pt = this->m_Parameters->GetType( *nIt );
361     if( pt == Parameters::String )
362     {
363       QLineEdit* v_string = dlg->findChild< QLineEdit* >( nIt->c_str( ) );
364       if( v_string != NULL )
365         this->m_Parameters->SetString( *nIt, v_string->text( ).toStdString( ) );
366     }
367     else if( pt == Parameters::Bool )
368     {
369       QCheckBox* v_bool = dlg->findChild< QCheckBox* >( nIt->c_str( ) );
370       if( v_bool != NULL )
371         this->m_Parameters->SetBool( *nIt, v_bool->isChecked( ) );
372     }
373     else if( pt == Parameters::Uint )
374     {
375       QSpinBox* v_uint = dlg->findChild< QSpinBox* >( nIt->c_str( ) );
376       if( v_uint != NULL )
377         this->m_Parameters->SetUint( *nIt, v_uint->value( ) );
378     }
379     else if( pt == Parameters::Int )
380     {
381       QSpinBox* v_int = dlg->findChild< QSpinBox* >( nIt->c_str( ) );
382       if( v_int != NULL )
383         this->m_Parameters->SetInt( *nIt, v_int->value( ) );
384     }
385     else if( pt == Parameters::Real )
386     {
387       QDoubleSpinBox* v_double =
388         dlg->findChild< QDoubleSpinBox* >( nIt->c_str( ) );
389       if( v_double != NULL )
390         this->m_Parameters->SetReal( *nIt, v_double->value( ) );
391     }
392     else if(
393       pt == Parameters::StringList ||
394       pt == Parameters::IntList ||
395       pt == Parameters::UintList ||
396       pt == Parameters::RealList
397       )
398     {
399       cpPlugins::Interface::ParametersListWidget* l_double =
400         dlg->findChild< cpPlugins::Interface::ParametersListWidget* >(
401           nIt->c_str( )
402           );
403       if( l_double != NULL )
404       {
405         if( pt == Parameters::StringList )
406         {
407           std::vector< std::string > values = l_double->GetStringValues( );
408           for( int r = 0; r < values.size( ); ++r )
409             this->m_Parameters->AddToStringList( *nIt, values[ r ] );
410         }
411         else if( pt == Parameters::IntList )
412         {
413           std::vector< int > values = l_double->GetIntValues( );
414           for( int r = 0; r < values.size( ); ++r )
415             this->m_Parameters->AddToIntList( *nIt, values[ r ] );
416         }
417         else if( pt == Parameters::UintList )
418         {
419           std::vector< unsigned int > values = l_double->GetUintValues( );
420           for( int r = 0; r < values.size( ); ++r )
421             this->m_Parameters->AddToUintList( *nIt, values[ r ] );
422         }
423         else if( pt == Parameters::RealList )
424         {
425           std::vector< double > values = l_double->GetDoubleValues( );
426           for( int r = 0; r < values.size( ); ++r )
427             this->m_Parameters->AddToRealList( *nIt, values[ r ] );
428
429         } // fi
430
431       } // fi
432
433     } // fi
434
435   } // rof
436   return( true );
437 }
438 */
439
440 #endif // cpPlugins_Interface_QT4
441
442 // eof - $RCSfile$