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