]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/ParametersQtDialog.cxx
Double click widget integrated with segmentation and deformation filters.
[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 <vtkRenderWindowInteractor.h>
10
11 #include <QCheckBox>
12 #include <QDialogButtonBox>
13 #include <QDoubleSpinBox>
14 #include <QHBoxLayout>
15 #include <QLineEdit>
16 #include <QWidget>
17
18 // -------------------------------------------------------------------------
19 cpPlugins::Interface::ParametersQtDialog::
20 ParametersQtDialog( QWidget* parent, Qt::WindowFlags f )
21   : QDialog( parent, f ),
22     m_Parameters( NULL ),
23     m_IsModal( false )
24 {
25   this->m_Title = new QLabel( this );
26   this->m_Title->setText( "Parameters dialog title" );
27
28   this->m_MainLayout = new QGridLayout( this );
29   this->m_ToolsLayout = new QVBoxLayout( );
30   this->m_ToolsLayout->addWidget( this->m_Title );
31   this->m_MainLayout->addLayout( this->m_ToolsLayout, 0, 0, 1, 1 );
32 }
33
34 // -------------------------------------------------------------------------
35 cpPlugins::Interface::ParametersQtDialog::
36 ~ParametersQtDialog( )
37 {
38   delete this->m_Title;
39   delete this->m_ToolsLayout;
40   delete this->m_MainLayout;
41 }
42
43 // -------------------------------------------------------------------------
44 bool cpPlugins::Interface::ParametersQtDialog::
45 IsModal( ) const
46 {
47   return( this->m_IsModal );
48 }
49
50 // -------------------------------------------------------------------------
51 cpPlugins::Interface::
52 Parameters* cpPlugins::Interface::ParametersQtDialog::
53 getParameters( ) const
54 {
55   return( this->m_Parameters );
56 }
57
58 // -------------------------------------------------------------------------
59 void cpPlugins::Interface::ParametersQtDialog::
60 addInteractor( vtkRenderWindowInteractor* interactor )
61 {
62   this->m_Interactors.insert( interactor );
63 }
64
65 // -------------------------------------------------------------------------
66 bool cpPlugins::Interface::ParametersQtDialog::
67 setParameters( Parameters* parameters )
68 {
69   this->m_IsModal = true;
70   this->m_Parameters = parameters;
71   if( this->m_Parameters == NULL )
72     return( false );
73
74   // Put values
75   std::vector< std::string > names;
76   this->m_Parameters->GetNames( names );
77   std::vector< std::string >::const_iterator nIt = names.begin( );
78   for( ; nIt != names.end( ); ++nIt )
79   {
80     Parameters::Type pt = this->m_Parameters->GetType( *nIt );
81
82     QWidget* w_input = NULL;
83     if( pt == Parameters::String )
84     {
85       QLineEdit* v_string = new QLineEdit( this );
86       v_string->setText( "Enter some text!!!" );
87       w_input = v_string;
88     }
89     else if( pt == Parameters::Bool )
90     {
91       QCheckBox* v_bool = new QCheckBox( this );
92       v_bool->setText( "[ON/OFF]" );
93       v_bool->setChecked( this->m_Parameters->GetBool( *nIt ) );
94       w_input = v_bool;
95     }
96     else if( pt == Parameters::Uint )
97     {
98       QSpinBox* v_uint = new QSpinBox( this );
99       v_uint->setMinimum( 0 );
100       v_uint->setMaximum( std::numeric_limits< int >::max( ) );
101       v_uint->setValue( this->m_Parameters->GetUint( *nIt ) );
102       w_input = v_uint;
103     }
104     else if( pt == Parameters::Int )
105     {
106       QSpinBox* v_int = new QSpinBox( this );
107       v_int->setMinimum( -std::numeric_limits< int >::max( ) );
108       v_int->setMaximum(  std::numeric_limits< int >::max( ) );
109       v_int->setValue( this->m_Parameters->GetInt( *nIt ) );
110       w_input = v_int;
111     }
112     else if( pt == Parameters::Real )
113     {
114       QDoubleSpinBox* v_double = new QDoubleSpinBox( this );
115       v_double->setDecimals( 3 );
116       v_double->setMinimum( -std::numeric_limits< double >::max( ) );
117       v_double->setMaximum(  std::numeric_limits< double >::max( ) );
118       v_double->setValue( this->m_Parameters->GetReal( *nIt ) );
119       w_input = v_double;
120     }
121     else if(
122       pt == Parameters::StringList ||
123       pt == Parameters::IntList ||
124       pt == Parameters::UintList ||
125       pt == Parameters::RealList
126       )
127     {
128       cpPlugins::Interface::ParametersListWidget* l_double =
129         new cpPlugins::Interface::ParametersListWidget( *nIt, this );
130       w_input = l_double;
131     }
132     else if( pt == Parameters::Point || pt == Parameters::Index )
133     {
134       auto iIt = this->m_Interactors.begin( );
135       for( ; iIt != this->m_Interactors.end( ); ++iIt )
136       {
137         TStyle* style =
138           dynamic_cast< TStyle* >( ( *iIt )->GetInteractorStyle( ) );
139         std::cout << "ACA VOY --> " << style << std::endl;
140
141       } // rof
142       this->m_IsModal = false;
143
144       /*
145         if( this->m_Interactor != NULL )
146         {
147
148         } // fi
149       */
150
151     } // fi
152
153     // Ok, a representation was created
154     if( w_input != NULL )
155     {
156       w_input->setObjectName( QString( nIt->c_str( ) ) );
157
158       QHBoxLayout* new_layout = new QHBoxLayout( );
159       QLabel* label = new QLabel( this );
160       label->setText( QString( nIt->c_str( ) ) );
161       new_layout->addWidget( label );
162       new_layout->addWidget( w_input );
163       this->m_ToolsLayout->addLayout( new_layout );
164
165     } // fi
166
167   } // rof
168   return( this->m_IsModal );
169 }
170
171 // -------------------------------------------------------------------------
172 void cpPlugins::Interface::ParametersQtDialog::
173 setTitle( const std::string& title )
174 {
175   this->m_Title->setText( title.c_str( ) );
176 }
177
178 // -------------------------------------------------------------------------
179 int cpPlugins::Interface::ParametersQtDialog::
180 exec( )
181 {
182   if( !this->m_IsModal )
183     return( 0 );
184
185   // Add buttons
186   QDialogButtonBox* bb = new QDialogButtonBox(
187     QDialogButtonBox::Ok | QDialogButtonBox::Cancel
188     );
189   QObject::connect( bb, SIGNAL( accepted( ) ), this, SLOT( accept( ) ) );
190   QObject::connect( bb, SIGNAL( rejected( ) ), this, SLOT( reject( ) ) );
191   this->m_ToolsLayout->addWidget( bb );
192
193   int ret = this->QDialog::exec( );
194   if( ret == 1 )
195     this->syncParameters( );
196   return( ret );
197 }
198
199 // -------------------------------------------------------------------------
200 void cpPlugins::Interface::ParametersQtDialog::
201 show( )
202 {
203   if( this->m_IsModal )
204     return;
205
206   this->QDialog::show( );
207 }
208
209 // -------------------------------------------------------------------------
210 void cpPlugins::Interface::ParametersQtDialog::
211 syncParameters( )
212 {
213   if( this->m_Parameters == NULL )
214     return;
215
216   // Get values
217   std::vector< std::string > names;
218   this->m_Parameters->GetNames( names );
219   std::vector< std::string >::const_iterator nIt = names.begin( );
220   for( ; nIt != names.end( ); ++nIt )
221   {
222     Parameters::Type pt = this->m_Parameters->GetType( *nIt );
223
224     if( pt == Parameters::String )
225     {
226       QLineEdit* v = this->findChild< QLineEdit* >( nIt->c_str( ) );
227       if( v != NULL )
228         this->m_Parameters->SetString( *nIt, v->text( ).toStdString( ) );
229     }
230     else if( pt == Parameters::Bool )
231     {
232       QCheckBox* v = this->findChild< QCheckBox* >( nIt->c_str( ) );
233       if( v != NULL )
234         this->m_Parameters->SetBool( *nIt, v->isChecked( ) );
235     }
236     else if( pt == Parameters::Uint )
237     {
238       QSpinBox* v = this->findChild< QSpinBox* >( nIt->c_str( ) );
239       if( v != NULL )
240         this->m_Parameters->SetUint( *nIt, v->value( ) );
241     }
242     else if( pt == Parameters::Int )
243     {
244       QSpinBox* v = this->findChild< QSpinBox* >( nIt->c_str( ) );
245       if( v != NULL )
246         this->m_Parameters->SetInt( *nIt, v->value( ) );
247     }
248     else if( pt == Parameters::Real )
249     {
250       QDoubleSpinBox* v = this->findChild< QDoubleSpinBox* >( nIt->c_str( ) );
251       if( v != NULL )
252         this->m_Parameters->SetReal( *nIt, v->value( ) );
253     }
254     else if(
255       pt == Parameters::StringList ||
256       pt == Parameters::IntList ||
257       pt == Parameters::UintList ||
258       pt == Parameters::RealList
259       )
260     {
261       cpPlugins::Interface::ParametersListWidget* lst =
262         this->findChild< cpPlugins::Interface::ParametersListWidget* >(
263           nIt->c_str( )
264           );
265       if( lst != NULL )
266       {
267         if( pt == Parameters::StringList )
268         {
269           this->m_Parameters->ClearStringList( *nIt );
270           std::vector< std::string > values = lst->GetStringValues( );
271           for( int r = 0; r < values.size( ); ++r )
272             this->m_Parameters->AddToStringList( *nIt, values[ r ] );
273         }
274         else if( pt == Parameters::IntList )
275         {
276           this->m_Parameters->ClearIntList( *nIt );
277           std::vector< int > values = lst->GetIntValues( );
278           for( int r = 0; r < values.size( ); ++r )
279             this->m_Parameters->AddToIntList( *nIt, values[ r ] );
280         }
281         else if( pt == Parameters::UintList )
282         {
283           this->m_Parameters->ClearUintList( *nIt );
284           std::vector< unsigned int > values = lst->GetUintValues( );
285           for( int r = 0; r < values.size( ); ++r )
286             this->m_Parameters->AddToUintList( *nIt, values[ r ] );
287         }
288         else if( pt == Parameters::RealList )
289         {
290           this->m_Parameters->ClearRealList( *nIt );
291           std::vector< double > values = lst->GetDoubleValues( );
292           for( int r = 0; r < values.size( ); ++r )
293             this->m_Parameters->AddToRealList( *nIt, values[ r ] );
294
295         } // fi
296
297       } // fi
298     }
299     else if( pt == Parameters::Point || pt == Parameters::Index )
300     {
301     } // fi
302
303   } // rof
304 }
305
306
307
308
309
310
311
312
313
314 /* TODO
315    enum Type
316    {
317    Index,
318    Point,
319    StringList,
320    BoolList,
321    IntList,
322    UintList,
323    IndexList,
324    PointList,
325    Choices,
326    NoType
327    };
328 */
329 /*
330     }
331     else if(
332     pt == Parameters::StringList ||
333     pt == Parameters::IntList ||
334     pt == Parameters::UintList ||
335     pt == Parameters::RealList
336     )
337     {
338     cpPlugins::Interface::ParametersListWidget* lst =
339     this->findChild< cpPlugins::Interface::ParametersListWidget* >(
340     nIt->c_str( )
341     );
342     if( lst != NULL )
343     {
344     if( pt == Parameters::StringList )
345     {
346     std::vector< std::string > values = lst->GetStringValues( );
347     for( int r = 0; r < values.size( ); ++r )
348     this->m_Parameters->AddToStringList( *nIt, values[ r ] );
349     }
350     else if( pt == Parameters::IntList )
351     {
352     std::vector< int > values = lst->GetIntValues( );
353     for( int r = 0; r < values.size( ); ++r )
354     this->m_Parameters->AddToIntList( *nIt, values[ r ] );
355     }
356     else if( pt == Parameters::UintList )
357     {
358     std::vector< unsigned int > values = lst->GetUintValues( );
359     for( int r = 0; r < values.size( ); ++r )
360     this->m_Parameters->AddToUintList( *nIt, values[ r ] );
361     }
362     else if( pt == Parameters::RealList )
363     {
364     std::vector< double > values = lst->GetDoubleValues( );
365     for( int r = 0; r < values.size( ); ++r )
366     this->m_Parameters->AddToRealList( *nIt, values[ r ] );
367
368     } // fi
369
370     } // fi
371
372     } // fi
373
374     } // rof
375     return( true );
376     }
377 */
378
379 #endif // cpPlugins_Interface_QT4
380
381 // eof - $RCSfile$