]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/ParametersQtDialog.cxx
sphere widget done, finally
[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 bool cpPlugins::Interface::ParametersQtDialog::
137 setParameters( Parameters* parameters )
138 {
139   this->m_IsModal = true;
140   this->m_Parameters = parameters;
141   if( this->m_Parameters == NULL )
142     return( false );
143
144   // Put values
145   std::vector< std::string > names;
146   this->m_Parameters->GetNames( names );
147   std::vector< std::string >::const_iterator nIt = names.begin( );
148   for( ; nIt != names.end( ); ++nIt )
149   {
150     Parameters::Type pt = this->m_Parameters->GetType( *nIt );
151
152     QWidget* w_input = NULL;
153     if( pt == Parameters::String )
154     {
155       QLineEdit* v_string = new QLineEdit( this );
156       v_string->setText( "Enter some text!!!" );
157       w_input = v_string;
158     }
159     else if( pt == Parameters::Bool )
160     {
161       QCheckBox* v_bool = new QCheckBox( this );
162       v_bool->setText( "[ON/OFF]" );
163       v_bool->setChecked( this->m_Parameters->GetBool( *nIt ) );
164       w_input = v_bool;
165     }
166     else if( pt == Parameters::Uint )
167     {
168       QSpinBox* v_uint = new QSpinBox( this );
169       v_uint->setMinimum( 0 );
170       v_uint->setMaximum( std::numeric_limits< int >::max( ) );
171       v_uint->setValue( this->m_Parameters->GetUint( *nIt ) );
172       w_input = v_uint;
173     }
174     else if( pt == Parameters::Int )
175     {
176       QSpinBox* v_int = new QSpinBox( this );
177       v_int->setMinimum( -std::numeric_limits< int >::max( ) );
178       v_int->setMaximum(  std::numeric_limits< int >::max( ) );
179       v_int->setValue( this->m_Parameters->GetInt( *nIt ) );
180       w_input = v_int;
181     }
182     else if( pt == Parameters::Real )
183     {
184       QDoubleSpinBox* v_double = new QDoubleSpinBox( this );
185       v_double->setDecimals( 3 );
186       v_double->setMinimum( -std::numeric_limits< double >::max( ) );
187       v_double->setMaximum(  std::numeric_limits< double >::max( ) );
188       v_double->setValue( this->m_Parameters->GetReal( *nIt ) );
189       w_input = v_double;
190     }
191     else if(
192       pt == Parameters::StringList ||
193       pt == Parameters::IntList ||
194       pt == Parameters::UintList ||
195       pt == Parameters::RealList
196       )
197     {
198       cpPlugins::Interface::ParametersListWidget* l_double =
199         new cpPlugins::Interface::ParametersListWidget( *nIt, this );
200       w_input = l_double;
201     }
202     else if( pt == Parameters::Point || pt == Parameters::Index )
203     {
204       vtkSmartPointer< SingleSeedCommand > command =
205         vtkSmartPointer< SingleSeedCommand >::New( );
206       command->Dialog = this;
207       command->Name = *nIt;
208
209       auto iIt = this->m_Interactors.begin( );
210       for( ; iIt != this->m_Interactors.end( ); ++iIt )
211       {
212         TStyle* style =
213           dynamic_cast< TStyle* >( ( *iIt )->GetInteractorStyle( ) );
214         if( style != NULL )
215         {
216           style->SeedWidgetOn( );
217           style->SetSeedWidgetCommand( command );
218
219         } // fi
220
221       } // rof
222       this->m_IsModal = false;
223
224     } // fi
225
226     // Ok, a representation was created
227     if( w_input != NULL )
228     {
229       w_input->setObjectName( QString( nIt->c_str( ) ) );
230
231       QHBoxLayout* new_layout = new QHBoxLayout( );
232       QLabel* label = new QLabel( this );
233       label->setText( QString( nIt->c_str( ) ) );
234       new_layout->addWidget( label );
235       new_layout->addWidget( w_input );
236       this->m_ToolsLayout->addLayout( new_layout );
237
238     } // fi
239
240   } // rof
241   return( this->m_IsModal );
242 }
243
244 // -------------------------------------------------------------------------
245 void cpPlugins::Interface::ParametersQtDialog::
246 setTitle( const std::string& title )
247 {
248   this->m_Title->setText( title.c_str( ) );
249 }
250
251 // -------------------------------------------------------------------------
252 int cpPlugins::Interface::ParametersQtDialog::
253 exec( )
254 {
255   if( !this->m_IsModal )
256     return( 0 );
257
258   // Add buttons
259   QDialogButtonBox* bb = new QDialogButtonBox(
260     QDialogButtonBox::Ok | QDialogButtonBox::Cancel
261     );
262   QObject::connect( bb, SIGNAL( accepted( ) ), this, SLOT( accept( ) ) );
263   QObject::connect( bb, SIGNAL( rejected( ) ), this, SLOT( reject( ) ) );
264   this->m_ToolsLayout->addWidget( bb );
265
266   int ret = this->QDialog::exec( );
267   if( ret == 1 )
268     this->syncParameters( );
269   return( ret );
270 }
271
272 // -------------------------------------------------------------------------
273 void cpPlugins::Interface::ParametersQtDialog::
274 show( )
275 {
276   if( this->m_IsModal )
277     return;
278
279   this->QDialog::show( );
280 }
281
282 // -------------------------------------------------------------------------
283 void cpPlugins::Interface::ParametersQtDialog::
284 syncParameters( )
285 {
286   if( this->m_Parameters == NULL )
287     return;
288
289   // Get values
290   std::vector< std::string > names;
291   this->m_Parameters->GetNames( names );
292   std::vector< std::string >::const_iterator nIt = names.begin( );
293   for( ; nIt != names.end( ); ++nIt )
294   {
295     Parameters::Type pt = this->m_Parameters->GetType( *nIt );
296
297     if( pt == Parameters::String )
298     {
299       QLineEdit* v = this->findChild< QLineEdit* >( nIt->c_str( ) );
300       if( v != NULL )
301         this->m_Parameters->SetString( *nIt, v->text( ).toStdString( ) );
302     }
303     else if( pt == Parameters::Bool )
304     {
305       QCheckBox* v = this->findChild< QCheckBox* >( nIt->c_str( ) );
306       if( v != NULL )
307         this->m_Parameters->SetBool( *nIt, v->isChecked( ) );
308     }
309     else if( pt == Parameters::Uint )
310     {
311       QSpinBox* v = this->findChild< QSpinBox* >( nIt->c_str( ) );
312       if( v != NULL )
313         this->m_Parameters->SetUint( *nIt, v->value( ) );
314     }
315     else if( pt == Parameters::Int )
316     {
317       QSpinBox* v = this->findChild< QSpinBox* >( nIt->c_str( ) );
318       if( v != NULL )
319         this->m_Parameters->SetInt( *nIt, v->value( ) );
320     }
321     else if( pt == Parameters::Real )
322     {
323       QDoubleSpinBox* v = this->findChild< QDoubleSpinBox* >( nIt->c_str( ) );
324       if( v != NULL )
325         this->m_Parameters->SetReal( *nIt, v->value( ) );
326     }
327     else if(
328       pt == Parameters::StringList ||
329       pt == Parameters::IntList ||
330       pt == Parameters::UintList ||
331       pt == Parameters::RealList
332       )
333     {
334       cpPlugins::Interface::ParametersListWidget* lst =
335         this->findChild< cpPlugins::Interface::ParametersListWidget* >(
336           nIt->c_str( )
337           );
338       if( lst != NULL )
339       {
340         if( pt == Parameters::StringList )
341         {
342           this->m_Parameters->ClearStringList( *nIt );
343           std::vector< std::string > values = lst->GetStringValues( );
344           for( int r = 0; r < values.size( ); ++r )
345             this->m_Parameters->AddToStringList( *nIt, values[ r ] );
346         }
347         else if( pt == Parameters::IntList )
348         {
349           this->m_Parameters->ClearIntList( *nIt );
350           std::vector< int > values = lst->GetIntValues( );
351           for( int r = 0; r < values.size( ); ++r )
352             this->m_Parameters->AddToIntList( *nIt, values[ r ] );
353         }
354         else if( pt == Parameters::UintList )
355         {
356           this->m_Parameters->ClearUintList( *nIt );
357           std::vector< unsigned int > values = lst->GetUintValues( );
358           for( int r = 0; r < values.size( ); ++r )
359             this->m_Parameters->AddToUintList( *nIt, values[ r ] );
360         }
361         else if( pt == Parameters::RealList )
362         {
363           this->m_Parameters->ClearRealList( *nIt );
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     else if( pt == Parameters::Point || pt == Parameters::Index )
373     {
374     } // fi
375
376   } // rof
377 }
378
379
380
381
382
383
384
385
386
387 /* TODO
388    enum Type
389    {
390    Index,
391    Point,
392    StringList,
393    BoolList,
394    IntList,
395    UintList,
396    IndexList,
397    PointList,
398    Choices,
399    NoType
400    };
401 */
402 /*
403     }
404     else if(
405     pt == Parameters::StringList ||
406     pt == Parameters::IntList ||
407     pt == Parameters::UintList ||
408     pt == Parameters::RealList
409     )
410     {
411     cpPlugins::Interface::ParametersListWidget* lst =
412     this->findChild< cpPlugins::Interface::ParametersListWidget* >(
413     nIt->c_str( )
414     );
415     if( lst != NULL )
416     {
417     if( pt == Parameters::StringList )
418     {
419     std::vector< std::string > values = lst->GetStringValues( );
420     for( int r = 0; r < values.size( ); ++r )
421     this->m_Parameters->AddToStringList( *nIt, values[ r ] );
422     }
423     else if( pt == Parameters::IntList )
424     {
425     std::vector< int > values = lst->GetIntValues( );
426     for( int r = 0; r < values.size( ); ++r )
427     this->m_Parameters->AddToIntList( *nIt, values[ r ] );
428     }
429     else if( pt == Parameters::UintList )
430     {
431     std::vector< unsigned int > values = lst->GetUintValues( );
432     for( int r = 0; r < values.size( ); ++r )
433     this->m_Parameters->AddToUintList( *nIt, values[ r ] );
434     }
435     else if( pt == Parameters::RealList )
436     {
437     std::vector< double > values = lst->GetDoubleValues( );
438     for( int r = 0; r < values.size( ); ++r )
439     this->m_Parameters->AddToRealList( *nIt, values[ r ] );
440
441     } // fi
442
443     } // fi
444
445     } // fi
446
447     } // rof
448     return( true );
449     }
450 */
451
452 #endif // cpPlugins_Interface_QT4
453
454 // eof - $RCSfile$