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