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