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