]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Parameters.cxx
52b2e47d423eccc3463a89a133de20acd5982d7b
[cpPlugins.git] / lib / cpPlugins / Interface / Parameters.cxx
1 #include <cpPlugins/Interface/Parameters.h>
2 #include <cpPlugins/Interface/ProcessObject.h>
3
4 #include <sstream>
5
6 // -------------------------------------------------------------------------
7 cpPlugins::Interface::
8 ProcessObject* cpPlugins::Interface::Parameters::
9 GetProcessObject( )
10 {
11   return( this->m_Process );
12 }
13
14 // -------------------------------------------------------------------------
15 const cpPlugins::Interface::
16 ProcessObject* cpPlugins::Interface::Parameters::
17 GetProcessObject( ) const
18 {
19   return( this->m_Process );
20 }
21
22 // -------------------------------------------------------------------------
23 void cpPlugins::Interface::Parameters::
24 SetProcessObject( ProcessObject* v )
25 {
26   this->m_Process = v;
27 }
28
29 // -------------------------------------------------------------------------
30 void cpPlugins::Interface::Parameters::
31 Modified( ) const
32 {
33   this->Superclass::Modified( );
34   if( this->m_Process != NULL )
35     this->m_Process->Modified( );
36 }
37
38 // -------------------------------------------------------------------------
39 void cpPlugins::Interface::Parameters::
40 Clear( )
41 {
42   this->m_Parameters.clear( );
43   this->Modified( );
44 }
45
46 // -------------------------------------------------------------------------
47 #define cpPlugins_Parameters_Configure( Y )                     \
48   void cpPlugins::Interface::Parameters::                       \
49   ConfigureAs##Y( const TString& name )                         \
50   {                                                             \
51     this->m_Parameters[ name ] = TParameter( Self::Y, "" );     \
52     this->Modified( );                                          \
53   }
54
55 cpPlugins_Parameters_Configure( String );
56 cpPlugins_Parameters_Configure( Bool );
57 cpPlugins_Parameters_Configure( Int );
58 cpPlugins_Parameters_Configure( Uint );
59 cpPlugins_Parameters_Configure( Real );
60 cpPlugins_Parameters_Configure( Index );
61 cpPlugins_Parameters_Configure( Point );
62 cpPlugins_Parameters_Configure( Vector );
63 cpPlugins_Parameters_Configure( StringList );
64 cpPlugins_Parameters_Configure( BoolList );
65 cpPlugins_Parameters_Configure( IntList );
66 cpPlugins_Parameters_Configure( UintList );
67 cpPlugins_Parameters_Configure( RealList );
68 cpPlugins_Parameters_Configure( IndexList );
69 cpPlugins_Parameters_Configure( PointList );
70 cpPlugins_Parameters_Configure( VectorList );
71 cpPlugins_Parameters_Configure( Choices );
72
73 // -------------------------------------------------------------------------
74 void cpPlugins::Interface::Parameters::
75 ConfigureAsChoices(
76   const TString& name, const std::vector< TString >& choices
77   )
78 {
79   // It is invalid not to give choices when configuring
80   if( choices.size( ) == 0 )
81     return;
82
83   std::stringstream str_choices;
84   str_choices << choices[ 0 ];
85   for( unsigned int i = 1; i < choices.size( ); ++i )
86     str_choices << "#" << choices[ i ];
87   str_choices << "@";
88   this->m_Parameters[ name ] =
89     TParameter( Self::Choices, str_choices.str( ) );
90   this->Modified( );
91 }
92
93 // -------------------------------------------------------------------------
94 void cpPlugins::Interface::Parameters::
95 GetNames( std::vector< TString >& container ) const
96 {
97   container.clear( );
98   TParameters::const_iterator i = this->m_Parameters.begin( );
99   for( ; i != this->m_Parameters.end( ); ++i )
100     container.push_back( i->first );
101 }
102
103 // -------------------------------------------------------------------------
104 cpPlugins::Interface::Parameters::
105 Type cpPlugins::Interface::Parameters::
106 GetType( const TString& name ) const
107 {
108   TParameters::const_iterator i = this->m_Parameters.find( name );
109   if( i != this->m_Parameters.end( ) )
110     return( i->second.first );
111   else
112     return( Self::NoType );
113 }
114
115 // -------------------------------------------------------------------------
116 #define cpPlugins_Parameters_Has( Y )                                   \
117   bool cpPlugins::Interface::Parameters::                               \
118   Has##Y( const TString& name ) const                                   \
119   {                                                                     \
120     TParameters::const_iterator i = this->m_Parameters.find( name );    \
121     if( i != this->m_Parameters.end( ) )                                \
122       return( i->second.first == Self::Y );                             \
123     else                                                                \
124       return( false );                                                  \
125   }
126
127 cpPlugins_Parameters_Has( String );
128 cpPlugins_Parameters_Has( Bool );
129 cpPlugins_Parameters_Has( Int );
130 cpPlugins_Parameters_Has( Uint );
131 cpPlugins_Parameters_Has( Real );
132 cpPlugins_Parameters_Has( Index );
133 cpPlugins_Parameters_Has( Point );
134 cpPlugins_Parameters_Has( Vector );
135 cpPlugins_Parameters_Has( StringList );
136 cpPlugins_Parameters_Has( BoolList );
137 cpPlugins_Parameters_Has( IntList );
138 cpPlugins_Parameters_Has( UintList );
139 cpPlugins_Parameters_Has( RealList );
140 cpPlugins_Parameters_Has( IndexList );
141 cpPlugins_Parameters_Has( PointList );
142 cpPlugins_Parameters_Has( VectorList );
143 cpPlugins_Parameters_Has( Choices );
144
145 // -------------------------------------------------------------------------
146 cpPlugins::Interface::Parameters::
147 TString cpPlugins::Interface::Parameters::
148 GetString( const TString& name, bool force ) const
149 {
150   TParameters::const_iterator i = this->m_Parameters.find( name );
151   if( i != this->m_Parameters.end( ) )
152   {
153     if( i->second.first == Self::String || !force )
154       return( i->second.second );
155
156   } // fi
157   return( "" );
158 }
159
160 // -------------------------------------------------------------------------
161 cpPlugins::Interface::Parameters::
162 TBool cpPlugins::Interface::Parameters::
163 GetBool( const TString& name ) const
164 {
165   TParameters::const_iterator i = this->m_Parameters.find( name );
166   if( i != this->m_Parameters.end( ) )
167   {
168     if( i->second.first == Self::Bool )
169       return( std::atoi( i->second.second.c_str( ) ) == 1 );
170
171   } // fi
172   return( false );
173 }
174
175 // -------------------------------------------------------------------------
176 cpPlugins::Interface::Parameters::
177 TInt cpPlugins::Interface::Parameters::
178 GetInt( const TString& name ) const
179 {
180   TParameters::const_iterator i = this->m_Parameters.find( name );
181   if( i != this->m_Parameters.end( ) )
182   {
183     if( i->second.first == Self::Int )
184       return( TInt( std::atoi( i->second.second.c_str( ) ) ) );
185
186   } // fi
187   return( TInt( 0 ) );
188 }
189
190 // -------------------------------------------------------------------------
191 cpPlugins::Interface::Parameters::
192 TUint cpPlugins::Interface::Parameters::
193 GetUint( const TString& name ) const
194 {
195   TParameters::const_iterator i = this->m_Parameters.find( name );
196   if( i != this->m_Parameters.end( ) )
197   {
198     if( i->second.first == Self::Uint )
199       return( TUint( std::atoi( i->second.second.c_str( ) ) ) );
200
201   } // fi
202   return( TUint( 0 ) );
203 }
204
205 // -------------------------------------------------------------------------
206 cpPlugins::Interface::Parameters::
207 TReal cpPlugins::Interface::Parameters::
208 GetReal( const TString& name ) const
209 {
210   TParameters::const_iterator i = this->m_Parameters.find( name );
211   if( i != this->m_Parameters.end( ) )
212   {
213     if( i->second.first == Self::Real )
214     {
215       std::istringstream tok_str( i->second.second );
216       float v;
217       tok_str >> v;
218       return( TReal( v ) );
219
220     } // fi
221
222   } // fi
223   return( TReal( 0 ) );
224 }
225
226 // -------------------------------------------------------------------------
227 void cpPlugins::Interface::Parameters::
228 GetStringList( std::vector< TString >& lst, const TString& name ) const
229 {
230   lst.clear( );
231   TParameters::const_iterator i = this->m_Parameters.find( name );
232   if( i == this->m_Parameters.end( ) )
233     return;
234   if( i->second.first != Self::StringList )
235     return;
236
237   std::istringstream str( i->second.second );
238   std::string token;
239   while( std::getline( str, token, '#' ) )
240     lst.push_back( token );
241 }
242
243 // -------------------------------------------------------------------------
244 void cpPlugins::Interface::Parameters::
245 GetBoolList( std::vector< TBool >& lst, const TString& name ) const
246 {
247   lst.clear( );
248   TParameters::const_iterator i = this->m_Parameters.find( name );
249   if( i == this->m_Parameters.end( ) )
250     return;
251   if( i->second.first != Self::BoolList )
252     return;
253
254   std::istringstream str( i->second.second );
255   std::string token;
256   while( std::getline( str, token, '#' ) )
257     lst.push_back( std::atoi( token.c_str( ) ) == 1 );
258 }
259
260 // -------------------------------------------------------------------------
261 void cpPlugins::Interface::Parameters::
262 GetIntList( std::vector< TInt >& lst, const TString& name ) const
263 {
264   lst.clear( );
265   TParameters::const_iterator i = this->m_Parameters.find( name );
266   if( i == this->m_Parameters.end( ) )
267     return;
268   if( i->second.first != Self::IntList )
269     return;
270
271   std::istringstream str( i->second.second );
272   std::string token;
273   while( std::getline( str, token, '#' ) )
274     lst.push_back( TInt( std::atoi( token.c_str( ) ) ) );
275 }
276
277 // -------------------------------------------------------------------------
278 void cpPlugins::Interface::Parameters::
279 GetUintList( std::vector< TUint >& lst, const TString& name ) const
280 {
281   lst.clear( );
282   TParameters::const_iterator i = this->m_Parameters.find( name );
283   if( i == this->m_Parameters.end( ) )
284     return;
285   if( i->second.first != Self::UintList )
286     return;
287
288   std::istringstream str( i->second.second );
289   std::string token;
290   while( std::getline( str, token, '#' ) )
291     lst.push_back( TUint( std::atoi( token.c_str( ) ) ) );
292 }
293
294 // -------------------------------------------------------------------------
295 void cpPlugins::Interface::Parameters::
296 GetRealList( std::vector< TReal >& lst, const TString& name ) const
297 {
298   lst.clear( );
299   TParameters::const_iterator i = this->m_Parameters.find( name );
300   if( i == this->m_Parameters.end( ) )
301     return;
302   if( i->second.first != Self::RealList )
303     return;
304
305   std::istringstream str( i->second.second );
306   std::string token;
307   while( std::getline( str, token, '#' ) )
308   {
309     std::istringstream tok_str( token );
310     float v;
311     tok_str >> v;
312     lst.push_back( TReal( v ) );
313
314   } // elihw
315 }
316
317 // -------------------------------------------------------------------------
318 void cpPlugins::Interface::Parameters::
319 GetChoices( std::vector< TString >& choices, const TString& name ) const
320 {
321   choices.clear( );
322   TParameters::const_iterator i = this->m_Parameters.find( name );
323   if( i == this->m_Parameters.end( ) )
324     return;
325   if( i->second.first != Self::Choices )
326     return;
327
328   std::istringstream str_choices( i->second.second );
329   std::string real_choices;
330   std::getline( str_choices, real_choices, '@' );
331   std::istringstream str( real_choices );
332   std::string token;
333   while( std::getline( str, token, '#' ) )
334     choices.push_back( token );
335 }
336
337 // -------------------------------------------------------------------------
338 cpPlugins::Interface::Parameters::
339 TString cpPlugins::Interface::Parameters::
340 GetSelectedChoice( const TString& name ) const
341 {
342   TParameters::const_iterator i = this->m_Parameters.find( name );
343   if( i == this->m_Parameters.end( ) )
344     return( "" );
345   if( i->second.first != Self::Choices )
346     return( "" );
347
348   std::istringstream str_choices( i->second.second );
349   std::string real_choice;
350   std::getline( str_choices, real_choice, '@' );
351   std::getline( str_choices, real_choice, '@' );
352   return( real_choice );
353 }
354
355 // -------------------------------------------------------------------------
356 void cpPlugins::Interface::Parameters::
357 SetString( const TString& name, const TString& v, bool force )
358 {
359   TParameters::iterator i = this->m_Parameters.find( name );
360   if( i == this->m_Parameters.end( ) )
361     return;
362   if( i->second.first != Self::String && force )
363     return;
364   i->second.second = v;
365   this->Modified( );
366 }
367
368 // -------------------------------------------------------------------------
369 #define cpPlugins_Parameters_Set( Y )                           \
370   void cpPlugins::Interface::Parameters::                       \
371   Set##Y( const TString& name, const T##Y& v )                  \
372   {                                                             \
373     TParameters::iterator i = this->m_Parameters.find( name );  \
374     if( i == this->m_Parameters.end( ) )                        \
375       return;                                                   \
376     if( i->second.first != Self::Y )                            \
377       return;                                                   \
378     std::stringstream str;                                      \
379     str << v;                                                   \
380     i->second.second = str.str( );                              \
381     this->Modified( );                                          \
382   }
383
384 cpPlugins_Parameters_Set( Bool );
385 cpPlugins_Parameters_Set( Int );
386 cpPlugins_Parameters_Set( Uint );
387 cpPlugins_Parameters_Set( Real );
388
389 // -------------------------------------------------------------------------
390 #define cpPlugins_Parameters_Add( Y )                           \
391   void cpPlugins::Interface::Parameters::                       \
392   AddTo##Y##List( const TString& name, const T##Y& v )          \
393   {                                                             \
394     TParameters::iterator i = this->m_Parameters.find( name );  \
395     if( i == this->m_Parameters.end( ) )                        \
396       return;                                                   \
397     if( i->second.first != Self::Y##List )                      \
398       return;                                                   \
399     std::stringstream str;                                      \
400     if( i->second.second == "" )                                \
401       str << v;                                                 \
402     else                                                        \
403       str << "#" << v;                                          \
404     i->second.second += str.str( );                             \
405     this->Modified( );                                          \
406   }
407
408 cpPlugins_Parameters_Add( String );
409 cpPlugins_Parameters_Add( Bool );
410 cpPlugins_Parameters_Add( Int );
411 cpPlugins_Parameters_Add( Uint );
412 cpPlugins_Parameters_Add( Real );
413
414 // -------------------------------------------------------------------------
415 #define cpPlugins_Parameters_Clear( Y )                         \
416   void cpPlugins::Interface::Parameters::                       \
417   Clear##Y##List( const TString& name )                         \
418   {                                                             \
419     TParameters::iterator i = this->m_Parameters.find( name );  \
420     if( i == this->m_Parameters.end( ) )                        \
421       return;                                                   \
422     if( i->second.first != Self::Y##List )                      \
423       return;                                                   \
424     i->second.second = "";                                      \
425     this->Modified( );                                          \
426   }
427
428 cpPlugins_Parameters_Clear( String );
429 cpPlugins_Parameters_Clear( Bool );
430 cpPlugins_Parameters_Clear( Int );
431 cpPlugins_Parameters_Clear( Uint );
432 cpPlugins_Parameters_Clear( Real );
433 cpPlugins_Parameters_Clear( Index );
434 cpPlugins_Parameters_Clear( Point );
435 cpPlugins_Parameters_Clear( Vector );
436
437 // -------------------------------------------------------------------------
438 bool cpPlugins::Interface::Parameters::
439 SetSelectedChoice( const TString& name, const TString& choice )
440 {
441   TParameters::iterator i = this->m_Parameters.find( name );
442   if( i == this->m_Parameters.end( ) )
443     return( false );
444   if( i->second.first != Self::Choices )
445     return( false );
446
447   std::vector< TString > c;
448   this->GetChoices( c, name );
449   if( std::find( c.begin( ), c.end( ), choice ) != c.end( ) )
450   {
451     std::istringstream str_choices( i->second.second );
452     std::string choices;
453     std::getline( str_choices, choices, '@' );
454     std::stringstream new_choices;
455     new_choices << choices << "@" << choice;
456     i->second.second = new_choices.str( );
457     return( true );
458   }
459   else
460     return( false );
461 }
462
463 // -------------------------------------------------------------------------
464 cpPlugins::Interface::Parameters::
465 Parameters( )
466   : Superclass( ),
467     m_Process( NULL )
468 {
469   this->Clear( );
470 }
471
472 // -------------------------------------------------------------------------
473 cpPlugins::Interface::Parameters::
474 ~Parameters( )
475 {
476 }
477
478 // -------------------------------------------------------------------------
479 void cpPlugins::Interface::Parameters::
480 PrintSelf( std::ostream& os, itk::Indent indent ) const
481 {
482   TParameters::const_iterator i = this->m_Parameters.begin( );
483   for( ; i != this->m_Parameters.end( ); ++i )
484     os << indent
485        << i->first << ": ("
486        << i->second.first << " | "
487        << i->second.second << ")"
488        << std::endl;
489 }
490
491 // eof - $RCSfile$