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