]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Parameters.cxx
MPR 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       return( TReal( std::atof( i->second.second.second.c_str( ) ) ) );
233
234   } // fi
235   return( TReal( 0 ) );
236 }
237
238 // -------------------------------------------------------------------------
239 void cpPlugins::Interface::Parameters::
240 GetStringList( std::vector< TString >& lst, const TString& name ) const
241 {
242   lst.clear( );
243   TParameters::const_iterator i = this->m_Parameters.find( name );
244   if( i == this->m_Parameters.end( ) )
245     return;
246   if( i->second.first != Self::StringList )
247     return;
248
249   std::istringstream str( i->second.second.second );
250   std::string token;
251   while( std::getline( str, token, '#' ) )
252     lst.push_back( token );
253 }
254
255 // -------------------------------------------------------------------------
256 void cpPlugins::Interface::Parameters::
257 GetBoolList( std::vector< TBool >& lst, const TString& name ) const
258 {
259   lst.clear( );
260   TParameters::const_iterator i = this->m_Parameters.find( name );
261   if( i == this->m_Parameters.end( ) )
262     return;
263   if( i->second.first != Self::BoolList )
264     return;
265
266   std::istringstream str( i->second.second.second );
267   std::string token;
268   while( std::getline( str, token, '#' ) )
269     lst.push_back( std::atoi( token.c_str( ) ) == 1 );
270 }
271
272 // -------------------------------------------------------------------------
273 void cpPlugins::Interface::Parameters::
274 GetIntList( std::vector< TInt >& lst, const TString& name ) const
275 {
276   lst.clear( );
277   TParameters::const_iterator i = this->m_Parameters.find( name );
278   if( i == this->m_Parameters.end( ) )
279     return;
280   if( i->second.first != Self::IntList )
281     return;
282
283   std::istringstream str( i->second.second.second );
284   std::string token;
285   while( std::getline( str, token, '#' ) )
286     lst.push_back( TInt( std::atoi( token.c_str( ) ) ) );
287 }
288
289 // -------------------------------------------------------------------------
290 void cpPlugins::Interface::Parameters::
291 GetUintList( std::vector< TUint >& lst, const TString& name ) const
292 {
293   lst.clear( );
294   TParameters::const_iterator i = this->m_Parameters.find( name );
295   if( i == this->m_Parameters.end( ) )
296     return;
297   if( i->second.first != Self::UintList )
298     return;
299
300   std::istringstream str( i->second.second.second );
301   std::string token;
302   while( std::getline( str, token, '#' ) )
303     lst.push_back( TUint( std::atoi( token.c_str( ) ) ) );
304 }
305
306 // -------------------------------------------------------------------------
307 void cpPlugins::Interface::Parameters::
308 GetRealList( std::vector< TReal >& lst, const TString& name ) const
309 {
310   lst.clear( );
311   TParameters::const_iterator i = this->m_Parameters.find( name );
312   if( i == this->m_Parameters.end( ) )
313     return;
314   if( i->second.first != Self::RealList )
315     return;
316
317   std::istringstream str( i->second.second.second );
318   std::string token;
319   while( std::getline( str, token, '#' ) )
320     lst.push_back( TReal( std::atof( token.c_str( ) ) ) );
321 }
322
323 // -------------------------------------------------------------------------
324 void cpPlugins::Interface::Parameters::
325 GetChoices( std::vector< TString >& choices, const TString& name ) const
326 {
327   choices.clear( );
328   TParameters::const_iterator i = this->m_Parameters.find( name );
329   if( i == this->m_Parameters.end( ) )
330     return;
331   if( i->second.first != Self::Choices )
332     return;
333
334   std::istringstream str( i->second.second.first );
335   std::string token;
336   while( std::getline( str, token, '#' ) )
337     choices.push_back( token );
338 }
339
340 // -------------------------------------------------------------------------
341 cpPlugins::Interface::Parameters::
342 TString cpPlugins::Interface::Parameters::
343 GetSelectedChoice( const TString& name ) const
344 {
345   TParameters::const_iterator i = this->m_Parameters.find( name );
346   if( i == this->m_Parameters.end( ) )
347     return( "" );
348   if( i->second.first != Self::Choices )
349     return( "" );
350   return( i->second.second.second );
351 }
352
353 // -------------------------------------------------------------------------
354 void cpPlugins::Interface::Parameters::
355 SetString( const TString& name, const TString& v )
356 {
357   TParameters::iterator i = this->m_Parameters.find( name );
358   if( i == this->m_Parameters.end( ) )
359     return;
360   if( i->second.first != Self::String )
361     return;
362   i->second.second.second = v;
363   this->Modified( );
364 }
365
366 // -------------------------------------------------------------------------
367 #define cpPlugins_Parameters_Set( Y )                           \
368   void cpPlugins::Interface::Parameters::                       \
369   Set##Y( const TString& name, const T##Y& v )                  \
370   {                                                             \
371     TParameters::iterator i = this->m_Parameters.find( name );  \
372     if( i == this->m_Parameters.end( ) )                        \
373       return;                                                   \
374     if( i->second.first != Self::Y )                            \
375       return;                                                   \
376     std::stringstream str;                                      \
377     str << v;                                                   \
378     i->second.second.second = str.str( );                       \
379     this->Modified( );                                          \
380   }
381
382 cpPlugins_Parameters_Set( Bool );
383 cpPlugins_Parameters_Set( Int );
384 cpPlugins_Parameters_Set( Uint );
385 cpPlugins_Parameters_Set( Real );
386
387 // -------------------------------------------------------------------------
388 #define cpPlugins_Parameters_Add( Y )                           \
389   void cpPlugins::Interface::Parameters::                       \
390   AddTo##Y##List( const TString& name, const T##Y& v )          \
391   {                                                             \
392     TParameters::iterator i = this->m_Parameters.find( name );  \
393     if( i == this->m_Parameters.end( ) )                        \
394       return;                                                   \
395     if( i->second.first != Self::Y##List )                      \
396       return;                                                   \
397     std::stringstream str;                                      \
398     if( i->second.second.second == "" )                         \
399       str << v;                                                 \
400     else                                                        \
401       str << "#" << v;                                          \
402     i->second.second.second += str.str( );                      \
403     this->Modified( );                                          \
404   }
405
406 cpPlugins_Parameters_Add( String );
407 cpPlugins_Parameters_Add( Bool );
408 cpPlugins_Parameters_Add( Int );
409 cpPlugins_Parameters_Add( Uint );
410 cpPlugins_Parameters_Add( Real );
411
412 // -------------------------------------------------------------------------
413 #define cpPlugins_Parameters_Clear( Y )                         \
414   void cpPlugins::Interface::Parameters::                       \
415   Clear##Y##List( const TString& name )                         \
416   {                                                             \
417     TParameters::iterator i = this->m_Parameters.find( name );  \
418     if( i == this->m_Parameters.end( ) )                        \
419       return;                                                   \
420     if( i->second.first != Self::Y##List )                      \
421       return;                                                   \
422     i->second.second.second = "";                               \
423     this->Modified( );                                          \
424   }
425
426 cpPlugins_Parameters_Clear( String );
427 cpPlugins_Parameters_Clear( Bool );
428 cpPlugins_Parameters_Clear( Int );
429 cpPlugins_Parameters_Clear( Uint );
430 cpPlugins_Parameters_Clear( Real );
431 cpPlugins_Parameters_Clear( Index );
432 cpPlugins_Parameters_Clear( Point );
433 cpPlugins_Parameters_Clear( Vector );
434
435 // -------------------------------------------------------------------------
436 bool cpPlugins::Interface::Parameters::
437 SetSelectedChoice( const TString& name, const TString& choice )
438 {
439   TParameters::iterator i = this->m_Parameters.find( name );
440   if( i == this->m_Parameters.end( ) )
441     return( false );
442   if( i->second.first != Self::Choices )
443     return( false );
444   if( i->second.second.first.find( choice ) != std::string::npos )
445   {
446     i->second.second.second = choice;
447     this->Modified( );
448     return( true );
449   }
450   else
451     return( false );
452 }
453
454 // -------------------------------------------------------------------------
455 cpPlugins::Interface::Parameters::
456 Parameters( )
457   : Superclass( ),
458     m_Process( NULL )
459 {
460   this->Clear( );
461 }
462
463 // -------------------------------------------------------------------------
464 cpPlugins::Interface::Parameters::
465 ~Parameters( )
466 {
467 }
468
469 // -------------------------------------------------------------------------
470 void cpPlugins::Interface::Parameters::
471 PrintSelf( std::ostream& os, itk::Indent indent ) const
472 {
473   TParameters::const_iterator i = this->m_Parameters.begin( );
474   for( ; i != this->m_Parameters.end( ); ++i )
475     os << indent
476        << i->first << ": ("
477        << i->second.first << " | "
478        << i->second.second.first << " | "
479        << i->second.second.second << ")"
480        << std::endl;
481 }
482
483 // eof - $RCSfile$