]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Parameters.cxx
Merge branch 'master' of ssh://git.creatis.insa-lyon.fr/cpPlugins
[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 const cpPlugins::Interface::
8 ProcessObject* cpPlugins::Interface::Parameters::
9 GetProcessObject( ) const
10 {
11   return( this->m_Process );
12 }
13
14 // -------------------------------------------------------------------------
15 void cpPlugins::Interface::Parameters::
16 SetProcessObject( ProcessObject* v )
17 {
18   this->m_Process = v;
19 }
20
21 // -------------------------------------------------------------------------
22 void cpPlugins::Interface::Parameters::
23 Modified( ) const
24 {
25   this->Superclass::Modified( );
26   if( this->m_Process != NULL )
27     this->m_Process->Modified( );
28 }
29
30 // -------------------------------------------------------------------------
31 void cpPlugins::Interface::Parameters::
32 Clear( )
33 {
34   this->m_Parameters.clear( );
35   this->Modified( );
36 }
37
38 // -------------------------------------------------------------------------
39 void cpPlugins::Interface::Parameters::
40 ConfigureAsString( const TString& name, const TString& v )
41 {
42   this->m_Parameters[ name ] = TParameter( Self::String, TValues( v, v ) );
43   this->Modified( );
44 }
45
46 // -------------------------------------------------------------------------
47 #define cpPlugins_Parameters_Configure( Y )             \
48   void cpPlugins::Interface::Parameters::               \
49   ConfigureAs##Y( const TString& name, const T##Y& v )  \
50   {                                                     \
51     std::stringstream str;                              \
52     str << v;                                           \
53     std::string s = str.str( );                         \
54     this->m_Parameters[ name ] =                        \
55       TParameter( Self::Y, TValues( s, s ) );           \
56     this->Modified( );                                  \
57   }
58
59 cpPlugins_Parameters_Configure( Bool );
60 cpPlugins_Parameters_Configure( Int );
61 cpPlugins_Parameters_Configure( Uint );
62 cpPlugins_Parameters_Configure( Real );
63
64 // -------------------------------------------------------------------------
65 #define cpPlugins_Parameters_List_Configure( Y )        \
66   void cpPlugins::Interface::Parameters::               \
67   ConfigureAs##Y##List( const TString& name )           \
68   {                                                     \
69     this->m_Parameters[ name ] =                        \
70       TParameter( Self::Y##List, TValues( "", "" ) );   \
71     this->Modified( );                                  \
72   }
73
74 cpPlugins_Parameters_List_Configure( String );
75 cpPlugins_Parameters_List_Configure( Bool );
76 cpPlugins_Parameters_List_Configure( Int );
77 cpPlugins_Parameters_List_Configure( Uint );
78 cpPlugins_Parameters_List_Configure( Real );
79 cpPlugins_Parameters_List_Configure( Index );
80 cpPlugins_Parameters_List_Configure( Point );
81
82 // -------------------------------------------------------------------------
83 void cpPlugins::Interface::Parameters::
84 ConfigureAsChoices(
85   const TString& name, const std::vector< TString >& choices
86   )
87 {
88   // It is invalid not to give choices when configuring
89   if( choices.size( ) == 0 )
90     return;
91
92   std::stringstream str_choices;
93   str_choices << choices[ 0 ];
94   for( unsigned int i = 1; i < choices.size( ); ++i )
95     str_choices << "#" << choices[ i ];
96   this->m_Parameters[ name ] =
97     TParameter( Self::Choices, TValues( str_choices.str( ), "" ) );
98   this->Modified( );
99 }
100
101 // -------------------------------------------------------------------------
102 void cpPlugins::Interface::Parameters::
103 GetNames( std::vector< TString >& container ) const
104 {
105   container.clear( );
106   TParameters::const_iterator i = this->m_Parameters.begin( );
107   for( ; i != this->m_Parameters.end( ); ++i )
108     container.push_back( i->first );
109 }
110
111 // -------------------------------------------------------------------------
112 cpPlugins::Interface::Parameters::
113 Type cpPlugins::Interface::Parameters::
114 GetType( const TString& name ) const
115 {
116   TParameters::const_iterator i = this->m_Parameters.find( name );
117   if( i != this->m_Parameters.end( ) )
118     return( i->second.first );
119   else
120     return( Self::NoType );
121 }
122
123 // -------------------------------------------------------------------------
124 #define cpPlugins_Parameters_Has( Y )                                   \
125   bool cpPlugins::Interface::Parameters::                               \
126   Has##Y( const TString& name ) const                                   \
127   {                                                                     \
128     TParameters::const_iterator i = this->m_Parameters.find( name );    \
129     if( i != this->m_Parameters.end( ) )                                \
130       return( i->second.first == Self::Y );                             \
131     else                                                                \
132       return( false );                                                  \
133   }
134
135 cpPlugins_Parameters_Has( String );
136 cpPlugins_Parameters_Has( Bool );
137 cpPlugins_Parameters_Has( Int );
138 cpPlugins_Parameters_Has( Uint );
139 cpPlugins_Parameters_Has( Real );
140 cpPlugins_Parameters_Has( Index );
141 cpPlugins_Parameters_Has( Point );
142 cpPlugins_Parameters_Has( StringList );
143 cpPlugins_Parameters_Has( BoolList );
144 cpPlugins_Parameters_Has( IntList );
145 cpPlugins_Parameters_Has( UintList );
146 cpPlugins_Parameters_Has( RealList );
147 cpPlugins_Parameters_Has( IndexList );
148 cpPlugins_Parameters_Has( PointList );
149 cpPlugins_Parameters_Has( Choices );
150
151 // -------------------------------------------------------------------------
152 cpPlugins::Interface::Parameters::
153 TString cpPlugins::Interface::Parameters::
154 GetString( const TString& name ) const
155 {
156   TParameters::const_iterator i = this->m_Parameters.find( name );
157   if( i != this->m_Parameters.end( ) )
158   {
159     if( i->second.first == Self::String )
160       return( i->second.second.second );
161
162   } // fi
163   return( "" );
164 }
165
166 // -------------------------------------------------------------------------
167 cpPlugins::Interface::Parameters::
168 TBool cpPlugins::Interface::Parameters::
169 GetBool( const TString& name ) const
170 {
171   TParameters::const_iterator i = this->m_Parameters.find( name );
172   if( i != this->m_Parameters.end( ) )
173   {
174     if( i->second.first == Self::Bool )
175       return( std::atoi( i->second.second.second.c_str( ) ) == 1 );
176
177   } // fi
178   return( false );
179 }
180
181 // -------------------------------------------------------------------------
182 cpPlugins::Interface::Parameters::
183 TInt cpPlugins::Interface::Parameters::
184 GetInt( const TString& name ) const
185 {
186   TParameters::const_iterator i = this->m_Parameters.find( name );
187   if( i != this->m_Parameters.end( ) )
188   {
189     if( i->second.first == Self::Int )
190       return( TInt( std::atoi( i->second.second.second.c_str( ) ) ) );
191
192   } // fi
193   return( TInt( 0 ) );
194 }
195
196 // -------------------------------------------------------------------------
197 cpPlugins::Interface::Parameters::
198 TUint cpPlugins::Interface::Parameters::
199 GetUint( const TString& name ) const
200 {
201   TParameters::const_iterator i = this->m_Parameters.find( name );
202   if( i != this->m_Parameters.end( ) )
203   {
204     if( i->second.first == Self::Uint )
205       return( TUint( std::atoi( i->second.second.second.c_str( ) ) ) );
206
207   } // fi
208   return( TUint( 0 ) );
209 }
210
211 // -------------------------------------------------------------------------
212 cpPlugins::Interface::Parameters::
213 TReal cpPlugins::Interface::Parameters::
214 GetReal( const TString& name ) const
215 {
216   TParameters::const_iterator i = this->m_Parameters.find( name );
217   if( i != this->m_Parameters.end( ) )
218   {
219     if( i->second.first == Self::Real )
220       return( TReal( std::atof( i->second.second.second.c_str( ) ) ) );
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.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.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.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.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.second );
306   std::string token;
307   while( std::getline( str, token, '#' ) )
308     lst.push_back( TReal( std::atof( token.c_str( ) ) ) );
309 }
310
311 // -------------------------------------------------------------------------
312 void cpPlugins::Interface::Parameters::
313 GetChoices( std::vector< TString >& choices, const TString& name ) const
314 {
315   choices.clear( );
316   TParameters::const_iterator i = this->m_Parameters.find( name );
317   if( i == this->m_Parameters.end( ) )
318     return;
319   if( i->second.first != Self::Choices )
320     return;
321
322   std::istringstream str( i->second.second.first );
323   std::string token;
324   while( std::getline( str, token, '#' ) )
325     choices.push_back( token );
326 }
327
328 // -------------------------------------------------------------------------
329 cpPlugins::Interface::Parameters::
330 TString cpPlugins::Interface::Parameters::
331 GetSelectedChoice( const TString& name ) const
332 {
333   TParameters::const_iterator i = this->m_Parameters.find( name );
334   if( i == this->m_Parameters.end( ) )
335     return( "" );
336   if( i->second.first != Self::Choices )
337     return( "" );
338   return( i->second.second.second );
339 }
340
341 // -------------------------------------------------------------------------
342 void cpPlugins::Interface::Parameters::
343 SetString( const TString& name, const TString& v )
344 {
345   TParameters::iterator i = this->m_Parameters.find( name );
346   if( i == this->m_Parameters.end( ) )
347     return;
348   if( i->second.first != Self::String )
349     return;
350   i->second.second.second = v;
351   this->Modified( );
352 }
353
354 // -------------------------------------------------------------------------
355 #define cpPlugins_Parameters_Set( Y )                           \
356   void cpPlugins::Interface::Parameters::                       \
357   Set##Y( const TString& name, const T##Y& v )                  \
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::Y )                            \
363       return;                                                   \
364     std::stringstream str;                                      \
365     str << v;                                                   \
366     i->second.second.second = str.str( );                       \
367     this->Modified( );                                          \
368   }
369
370 cpPlugins_Parameters_Set( Bool );
371 cpPlugins_Parameters_Set( Int );
372 cpPlugins_Parameters_Set( Uint );
373 cpPlugins_Parameters_Set( Real );
374
375 // -------------------------------------------------------------------------
376 #define cpPlugins_Parameters_Add( Y )                           \
377   void cpPlugins::Interface::Parameters::                       \
378   AddTo##Y##List( const TString& name, const T##Y& v )          \
379   {                                                             \
380     TParameters::iterator i = this->m_Parameters.find( name );  \
381     if( i == this->m_Parameters.end( ) )                        \
382       return;                                                   \
383     if( i->second.first != Self::Y##List )                      \
384       return;                                                   \
385     std::stringstream str;                                      \
386     if( i->second.second.second == "" )                         \
387       str << v;                                                 \
388     else                                                        \
389       str << "#" << v;                                          \
390     i->second.second.second += str.str( );                      \
391     this->Modified( );                                          \
392   }
393
394 cpPlugins_Parameters_Add( String );
395 cpPlugins_Parameters_Add( Bool );
396 cpPlugins_Parameters_Add( Int );
397 cpPlugins_Parameters_Add( Uint );
398 cpPlugins_Parameters_Add( Real );
399
400 // -------------------------------------------------------------------------
401 #define cpPlugins_Parameters_Clear( Y )                         \
402   void cpPlugins::Interface::Parameters::                       \
403   Clear##Y##List( const TString& name )                         \
404   {                                                             \
405     TParameters::iterator i = this->m_Parameters.find( name );  \
406     if( i == this->m_Parameters.end( ) )                        \
407       return;                                                   \
408     if( i->second.first != Self::Y##List )                      \
409       return;                                                   \
410     i->second.second.second = "";                               \
411     this->Modified( );                                          \
412   }
413
414 cpPlugins_Parameters_Clear( String );
415 cpPlugins_Parameters_Clear( Bool );
416 cpPlugins_Parameters_Clear( Int );
417 cpPlugins_Parameters_Clear( Uint );
418 cpPlugins_Parameters_Clear( Real );
419 cpPlugins_Parameters_Clear( Index );
420 cpPlugins_Parameters_Clear( Point );
421
422 // -------------------------------------------------------------------------
423 bool cpPlugins::Interface::Parameters::
424 SetSelectedChoice( const TString& name, const TString& choice )
425 {
426   TParameters::iterator i = this->m_Parameters.find( name );
427   if( i == this->m_Parameters.end( ) )
428     return( false );
429   if( i->second.first != Self::Choices )
430     return( false );
431   if( i->second.second.first.find( choice ) != std::string::npos )
432   {
433     i->second.second.second = choice;
434     this->Modified( );
435     return( true );
436   }
437   else
438     return( false );
439 }
440
441 // -------------------------------------------------------------------------
442 cpPlugins::Interface::Parameters::
443 Parameters( )
444   : Superclass( ),
445     m_Process( NULL )
446 {
447   this->Clear( );
448 }
449
450 // -------------------------------------------------------------------------
451 cpPlugins::Interface::Parameters::
452 ~Parameters( )
453 {
454 }
455
456 // -------------------------------------------------------------------------
457 void cpPlugins::Interface::Parameters::
458 PrintSelf( std::ostream& os, itk::Indent indent ) const
459 {
460   TParameters::const_iterator i = this->m_Parameters.begin( );
461   for( ; i != this->m_Parameters.end( ); ++i )
462     os << indent
463        << i->first << ": ("
464        << i->second.first << " | "
465        << i->second.second.first << " | "
466        << i->second.second.second << ")"
467        << std::endl;
468 }
469
470 // eof - $RCSfile$