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