]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/ParametersQtDialog.cxx
0891a2b5c09c409c9c5d80381ef5114a27b441cd
[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_ToolsLayout;
45   delete this->m_MainLayout;
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   if( ret == 1 )
207     this->syncParameters( );
208   return( ret );
209 }
210
211 // -------------------------------------------------------------------------
212 void cpPlugins::Interface::ParametersQtDialog::
213 show( )
214 {
215   if( this->m_IsModal )
216     return;
217
218   this->QDialog::show( );
219 }
220
221 // -------------------------------------------------------------------------
222 void cpPlugins::Interface::ParametersQtDialog::
223 syncParameters( )
224 {
225   if( this->m_Parameters == NULL )
226     return;
227
228   // Get values
229   std::vector< std::string > names;
230   this->m_Parameters->GetNames( names );
231   std::vector< std::string >::const_iterator nIt = names.begin( );
232   for( ; nIt != names.end( ); ++nIt )
233   {
234     Parameters::Type pt = this->m_Parameters->GetType( *nIt );
235
236     if( pt == Parameters::String )
237     {
238       QLineEdit* v = this->findChild< QLineEdit* >( nIt->c_str( ) );
239       if( v != NULL )
240         this->m_Parameters->SetString( *nIt, v->text( ).toStdString( ) );
241     }
242     else if( pt == Parameters::Bool )
243     {
244       QCheckBox* v = this->findChild< QCheckBox* >( nIt->c_str( ) );
245       if( v != NULL )
246         this->m_Parameters->SetBool( *nIt, v->isChecked( ) );
247     }
248     else if( pt == Parameters::Uint )
249     {
250       QSpinBox* v = this->findChild< QSpinBox* >( nIt->c_str( ) );
251       if( v != NULL )
252         this->m_Parameters->SetUint( *nIt, v->value( ) );
253     }
254     else if( pt == Parameters::Int )
255     {
256       QSpinBox* v = this->findChild< QSpinBox* >( nIt->c_str( ) );
257       if( v != NULL )
258         this->m_Parameters->SetInt( *nIt, v->value( ) );
259     }
260     else if( pt == Parameters::Real )
261     {
262       QDoubleSpinBox* v = this->findChild< QDoubleSpinBox* >( nIt->c_str( ) );
263       if( v != NULL )
264         this->m_Parameters->SetReal( *nIt, v->value( ) );
265     }
266     else if(
267       pt == Parameters::StringList ||
268       pt == Parameters::IntList ||
269       pt == Parameters::UintList ||
270       pt == Parameters::RealList
271       )
272     {
273       cpPlugins::Interface::ParametersListWidget* lst =
274         this->findChild< cpPlugins::Interface::ParametersListWidget* >(
275           nIt->c_str( )
276           );
277       if( lst != NULL )
278       {
279         if( pt == Parameters::StringList )
280         {
281           this->m_Parameters->ClearStringList( *nIt );
282           std::vector< std::string > values = lst->GetStringValues( );
283           for( int r = 0; r < values.size( ); ++r )
284             this->m_Parameters->AddToStringList( *nIt, values[ r ] );
285         }
286         else if( pt == Parameters::IntList )
287         {
288           this->m_Parameters->ClearIntList( *nIt );
289           std::vector< int > values = lst->GetIntValues( );
290           for( int r = 0; r < values.size( ); ++r )
291             this->m_Parameters->AddToIntList( *nIt, values[ r ] );
292         }
293         else if( pt == Parameters::UintList )
294         {
295           this->m_Parameters->ClearUintList( *nIt );
296           std::vector< unsigned int > values = lst->GetUintValues( );
297           for( int r = 0; r < values.size( ); ++r )
298             this->m_Parameters->AddToUintList( *nIt, values[ r ] );
299         }
300         else if( pt == Parameters::RealList )
301         {
302           this->m_Parameters->ClearRealList( *nIt );
303           std::vector< double > values = lst->GetDoubleValues( );
304           for( int r = 0; r < values.size( ); ++r )
305             this->m_Parameters->AddToRealList( *nIt, values[ r ] );
306
307         } // fi
308
309       } // fi
310     }
311     else if( pt == Parameters::Point || pt == Parameters::Index )
312     {
313     } // fi
314
315   } // rof
316 }
317
318
319
320
321
322
323
324
325
326 /* TODO
327    enum Type
328    {
329    Index,
330    Point,
331    StringList,
332    BoolList,
333    IntList,
334    UintList,
335    IndexList,
336    PointList,
337    Choices,
338    NoType
339    };
340 */
341 /*
342     }
343     else if(
344     pt == Parameters::StringList ||
345     pt == Parameters::IntList ||
346     pt == Parameters::UintList ||
347     pt == Parameters::RealList
348     )
349     {
350     cpPlugins::Interface::ParametersListWidget* lst =
351     this->findChild< cpPlugins::Interface::ParametersListWidget* >(
352     nIt->c_str( )
353     );
354     if( lst != NULL )
355     {
356     if( pt == Parameters::StringList )
357     {
358     std::vector< std::string > values = lst->GetStringValues( );
359     for( int r = 0; r < values.size( ); ++r )
360     this->m_Parameters->AddToStringList( *nIt, values[ r ] );
361     }
362     else if( pt == Parameters::IntList )
363     {
364     std::vector< int > values = lst->GetIntValues( );
365     for( int r = 0; r < values.size( ); ++r )
366     this->m_Parameters->AddToIntList( *nIt, values[ r ] );
367     }
368     else if( pt == Parameters::UintList )
369     {
370     std::vector< unsigned int > values = lst->GetUintValues( );
371     for( int r = 0; r < values.size( ); ++r )
372     this->m_Parameters->AddToUintList( *nIt, values[ r ] );
373     }
374     else if( pt == Parameters::RealList )
375     {
376     std::vector< double > values = lst->GetDoubleValues( );
377     for( int r = 0; r < values.size( ); ++r )
378     this->m_Parameters->AddToRealList( *nIt, values[ r ] );
379
380     } // fi
381
382     } // fi
383
384     } // fi
385
386     } // rof
387     return( true );
388     }
389 */
390
391 #endif // cpPlugins_Interface_QT4
392
393 // eof - $RCSfile$