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