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