]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Parameters.cxx
Now it's broken :-(
[cpPlugins.git] / lib / cpPlugins / Interface / Parameters.cxx
1 #include <cpPlugins/Interface/Parameters.h>
2 #include <cpPlugins/Interface/ProcessObject.h>
3 #include <third_party/tinyxml/tinyxml.h>
4
5 #include <sstream>
6
7 // -------------------------------------------------------------------------
8 cpPlugins::Interface::
9 ProcessObject* cpPlugins::Interface::Parameters::
10 GetProcessObject( )
11 {
12   return( this->m_Process );
13 }
14
15 // -------------------------------------------------------------------------
16 const cpPlugins::Interface::
17 ProcessObject* cpPlugins::Interface::Parameters::
18 GetProcessObject( ) const
19 {
20   return( this->m_Process );
21 }
22
23 // -------------------------------------------------------------------------
24 void cpPlugins::Interface::Parameters::
25 SetProcessObject( ProcessObject* v )
26 {
27   this->m_Process = v;
28 }
29
30 // -------------------------------------------------------------------------
31 void cpPlugins::Interface::Parameters::
32 Modified( ) const
33 {
34   this->Superclass::Modified( );
35   if( this->m_Process != NULL )
36     this->m_Process->Modified( );
37 }
38
39 // -------------------------------------------------------------------------
40 void cpPlugins::Interface::Parameters::
41 Clear( )
42 {
43   this->m_Parameters.clear( );
44   this->Modified( );
45 }
46
47 // -------------------------------------------------------------------------
48 #define cpPlugins_Parameters_Configure( Y )                     \
49   void cpPlugins::Interface::Parameters::                       \
50   ConfigureAs##Y( const TString& name )                         \
51   {                                                             \
52     this->m_Parameters[ name ] = TParameter( Self::Y, "" );     \
53     this->Modified( );                                          \
54   }
55
56 cpPlugins_Parameters_Configure( String );
57 cpPlugins_Parameters_Configure( Bool );
58 cpPlugins_Parameters_Configure( Int );
59 cpPlugins_Parameters_Configure( Uint );
60 cpPlugins_Parameters_Configure( Real );
61 cpPlugins_Parameters_Configure( Index );
62 cpPlugins_Parameters_Configure( Point );
63 cpPlugins_Parameters_Configure( Vector );
64 cpPlugins_Parameters_Configure( StringList );
65 cpPlugins_Parameters_Configure( BoolList );
66 cpPlugins_Parameters_Configure( IntList );
67 cpPlugins_Parameters_Configure( UintList );
68 cpPlugins_Parameters_Configure( RealList );
69 cpPlugins_Parameters_Configure( IndexList );
70 cpPlugins_Parameters_Configure( PointList );
71 cpPlugins_Parameters_Configure( VectorList );
72 cpPlugins_Parameters_Configure( Choices );
73
74 // -------------------------------------------------------------------------
75 void cpPlugins::Interface::Parameters::
76 ConfigureAsChoices(
77   const TString& name, const std::vector< TString >& choices
78   )
79 {
80   // It is invalid not to give choices when configuring
81   if( choices.size( ) == 0 )
82     return;
83
84   std::stringstream str_choices;
85   str_choices << choices[ 0 ];
86   for( unsigned int i = 1; i < choices.size( ); ++i )
87     str_choices << "#" << choices[ i ];
88   str_choices << "@";
89   this->m_Parameters[ name ] =
90     TParameter( Self::Choices, str_choices.str( ) );
91   this->Modified( );
92 }
93
94 // -------------------------------------------------------------------------
95 void cpPlugins::Interface::Parameters::
96 GetNames( std::vector< TString >& container ) const
97 {
98   container.clear( );
99   TParameters::const_iterator i = this->m_Parameters.begin( );
100   for( ; i != this->m_Parameters.end( ); ++i )
101     container.push_back( i->first );
102 }
103
104 // -------------------------------------------------------------------------
105 cpPlugins::Interface::Parameters::
106 Type cpPlugins::Interface::Parameters::
107 GetType( const TString& name ) const
108 {
109   TParameters::const_iterator i = this->m_Parameters.find( name );
110   if( i != this->m_Parameters.end( ) )
111     return( i->second.first );
112   else
113     return( Self::NoType );
114 }
115
116 // -------------------------------------------------------------------------
117 #define cpPlugins_Parameters_Has( Y )                                   \
118   bool cpPlugins::Interface::Parameters::                               \
119   Has##Y( const TString& name ) const                                   \
120   {                                                                     \
121     TParameters::const_iterator i = this->m_Parameters.find( name );    \
122     if( i != this->m_Parameters.end( ) )                                \
123       return( i->second.first == Self::Y );                             \
124     else                                                                \
125       return( false );                                                  \
126   }
127
128 cpPlugins_Parameters_Has( String );
129 cpPlugins_Parameters_Has( Bool );
130 cpPlugins_Parameters_Has( Int );
131 cpPlugins_Parameters_Has( Uint );
132 cpPlugins_Parameters_Has( Real );
133 cpPlugins_Parameters_Has( Index );
134 cpPlugins_Parameters_Has( Point );
135 cpPlugins_Parameters_Has( Vector );
136 cpPlugins_Parameters_Has( StringList );
137 cpPlugins_Parameters_Has( BoolList );
138 cpPlugins_Parameters_Has( IntList );
139 cpPlugins_Parameters_Has( UintList );
140 cpPlugins_Parameters_Has( RealList );
141 cpPlugins_Parameters_Has( IndexList );
142 cpPlugins_Parameters_Has( PointList );
143 cpPlugins_Parameters_Has( VectorList );
144 cpPlugins_Parameters_Has( Choices );
145
146 // -------------------------------------------------------------------------
147 cpPlugins::Interface::Parameters::
148 TString cpPlugins::Interface::Parameters::
149 GetString( const TString& name, bool force ) const
150 {
151   TParameters::const_iterator i = this->m_Parameters.find( name );
152   if( i != this->m_Parameters.end( ) )
153   {
154     if( i->second.first == Self::String || !force )
155       return( i->second.second );
156
157   } // fi
158   return( "" );
159 }
160
161 // -------------------------------------------------------------------------
162 cpPlugins::Interface::Parameters::
163 TBool cpPlugins::Interface::Parameters::
164 GetBool( const TString& name ) const
165 {
166   TParameters::const_iterator i = this->m_Parameters.find( name );
167   if( i != this->m_Parameters.end( ) )
168   {
169     if( i->second.first == Self::Bool )
170       return( std::atoi( i->second.second.c_str( ) ) == 1 );
171
172   } // fi
173   return( false );
174 }
175
176 // -------------------------------------------------------------------------
177 cpPlugins::Interface::Parameters::
178 TInt cpPlugins::Interface::Parameters::
179 GetInt( const TString& name ) const
180 {
181   TParameters::const_iterator i = this->m_Parameters.find( name );
182   if( i != this->m_Parameters.end( ) )
183   {
184     if( i->second.first == Self::Int )
185       return( TInt( std::atoi( i->second.second.c_str( ) ) ) );
186
187   } // fi
188   return( TInt( 0 ) );
189 }
190
191 // -------------------------------------------------------------------------
192 cpPlugins::Interface::Parameters::
193 TUint cpPlugins::Interface::Parameters::
194 GetUint( const TString& name ) const
195 {
196   TParameters::const_iterator i = this->m_Parameters.find( name );
197   if( i != this->m_Parameters.end( ) )
198   {
199     if( i->second.first == Self::Uint )
200       return( TUint( std::atoi( i->second.second.c_str( ) ) ) );
201
202   } // fi
203   return( TUint( 0 ) );
204 }
205
206 // -------------------------------------------------------------------------
207 cpPlugins::Interface::Parameters::
208 TReal cpPlugins::Interface::Parameters::
209 GetReal( const TString& name ) const
210 {
211   TParameters::const_iterator i = this->m_Parameters.find( name );
212   if( i != this->m_Parameters.end( ) )
213   {
214     if( i->second.first == Self::Real )
215     {
216       std::istringstream tok_str( i->second.second );
217       float v;
218       tok_str >> v;
219       return( TReal( v ) );
220
221     } // fi
222
223   } // fi
224   return( TReal( 0 ) );
225 }
226
227 // -------------------------------------------------------------------------
228 void cpPlugins::Interface::Parameters::
229 GetStringList( std::vector< TString >& lst, const TString& name ) const
230 {
231   lst.clear( );
232   TParameters::const_iterator i = this->m_Parameters.find( name );
233   if( i == this->m_Parameters.end( ) )
234     return;
235   if( i->second.first != Self::StringList )
236     return;
237
238   std::istringstream str( i->second.second );
239   std::string token;
240   while( std::getline( str, token, '#' ) )
241     lst.push_back( token );
242 }
243
244 // -------------------------------------------------------------------------
245 void cpPlugins::Interface::Parameters::
246 GetBoolList( std::vector< TBool >& 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::BoolList )
253     return;
254
255   std::istringstream str( i->second.second );
256   std::string token;
257   while( std::getline( str, token, '#' ) )
258     lst.push_back( std::atoi( token.c_str( ) ) == 1 );
259 }
260
261 // -------------------------------------------------------------------------
262 void cpPlugins::Interface::Parameters::
263 GetIntList( std::vector< TInt >& 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::IntList )
270     return;
271
272   std::istringstream str( i->second.second );
273   std::string token;
274   while( std::getline( str, token, '#' ) )
275     lst.push_back( TInt( std::atoi( token.c_str( ) ) ) );
276 }
277
278 // -------------------------------------------------------------------------
279 void cpPlugins::Interface::Parameters::
280 GetUintList( std::vector< TUint >& 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::UintList )
287     return;
288
289   std::istringstream str( i->second.second );
290   std::string token;
291   while( std::getline( str, token, '#' ) )
292     lst.push_back( TUint( std::atoi( token.c_str( ) ) ) );
293 }
294
295 // -------------------------------------------------------------------------
296 void cpPlugins::Interface::Parameters::
297 GetRealList( std::vector< TReal >& 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::RealList )
304     return;
305
306   std::istringstream str( i->second.second );
307   std::string token;
308   while( std::getline( str, token, '#' ) )
309   {
310     std::istringstream tok_str( token );
311     float v;
312     tok_str >> v;
313     lst.push_back( TReal( v ) );
314
315   } // elihw
316 }
317
318 // -------------------------------------------------------------------------
319 void cpPlugins::Interface::Parameters::
320 GetChoices( std::vector< TString >& choices, const TString& name ) const
321 {
322   choices.clear( );
323   TParameters::const_iterator i = this->m_Parameters.find( name );
324   if( i == this->m_Parameters.end( ) )
325     return;
326   if( i->second.first != Self::Choices )
327     return;
328
329   std::istringstream str_choices( i->second.second );
330   std::string real_choices;
331   std::getline( str_choices, real_choices, '@' );
332   std::istringstream str( real_choices );
333   std::string token;
334   while( std::getline( str, token, '#' ) )
335     choices.push_back( token );
336 }
337
338 // -------------------------------------------------------------------------
339 cpPlugins::Interface::Parameters::
340 TString cpPlugins::Interface::Parameters::
341 GetSelectedChoice( const TString& name ) const
342 {
343   TParameters::const_iterator i = this->m_Parameters.find( name );
344   if( i == this->m_Parameters.end( ) )
345     return( "" );
346   if( i->second.first != Self::Choices )
347     return( "" );
348
349   std::istringstream str_choices( i->second.second );
350   std::string real_choice;
351   std::getline( str_choices, real_choice, '@' );
352   std::getline( str_choices, real_choice, '@' );
353   return( real_choice );
354 }
355
356 // -------------------------------------------------------------------------
357 void cpPlugins::Interface::Parameters::
358 SetString( const TString& name, const TString& v, bool force )
359 {
360   TParameters::iterator i = this->m_Parameters.find( name );
361   if( i == this->m_Parameters.end( ) )
362     return;
363   if( i->second.first != Self::String && force )
364     return;
365   i->second.second = v;
366   this->Modified( );
367 }
368
369 // -------------------------------------------------------------------------
370 #define cpPlugins_Parameters_Set( Y )                           \
371   void cpPlugins::Interface::Parameters::                       \
372   Set##Y( const TString& name, const T##Y& v )                  \
373   {                                                             \
374     TParameters::iterator i = this->m_Parameters.find( name );  \
375     if( i == this->m_Parameters.end( ) )                        \
376       return;                                                   \
377     if( i->second.first != Self::Y )                            \
378       return;                                                   \
379     std::stringstream str;                                      \
380     str << v;                                                   \
381     i->second.second = str.str( );                              \
382     this->Modified( );                                          \
383   }
384
385 cpPlugins_Parameters_Set( Bool );
386 cpPlugins_Parameters_Set( Int );
387 cpPlugins_Parameters_Set( Uint );
388 cpPlugins_Parameters_Set( Real );
389
390 // -------------------------------------------------------------------------
391 #define cpPlugins_Parameters_Add( Y )                           \
392   void cpPlugins::Interface::Parameters::                       \
393   AddTo##Y##List( const TString& name, const T##Y& v )          \
394   {                                                             \
395     TParameters::iterator i = this->m_Parameters.find( name );  \
396     if( i == this->m_Parameters.end( ) )                        \
397       return;                                                   \
398     if( i->second.first != Self::Y##List )                      \
399       return;                                                   \
400     std::stringstream str;                                      \
401     if( i->second.second == "" )                                \
402       str << v;                                                 \
403     else                                                        \
404       str << "#" << v;                                          \
405     i->second.second += str.str( );                             \
406     this->Modified( );                                          \
407   }
408
409 cpPlugins_Parameters_Add( String );
410 cpPlugins_Parameters_Add( Bool );
411 cpPlugins_Parameters_Add( Int );
412 cpPlugins_Parameters_Add( Uint );
413 cpPlugins_Parameters_Add( Real );
414
415 // -------------------------------------------------------------------------
416 #define cpPlugins_Parameters_Clear( Y )                         \
417   void cpPlugins::Interface::Parameters::                       \
418   Clear##Y##List( const TString& name )                         \
419   {                                                             \
420     TParameters::iterator i = this->m_Parameters.find( name );  \
421     if( i == this->m_Parameters.end( ) )                        \
422       return;                                                   \
423     if( i->second.first != Self::Y##List )                      \
424       return;                                                   \
425     i->second.second = "";                                      \
426     this->Modified( );                                          \
427   }
428
429 cpPlugins_Parameters_Clear( String );
430 cpPlugins_Parameters_Clear( Bool );
431 cpPlugins_Parameters_Clear( Int );
432 cpPlugins_Parameters_Clear( Uint );
433 cpPlugins_Parameters_Clear( Real );
434 cpPlugins_Parameters_Clear( Index );
435 cpPlugins_Parameters_Clear( Point );
436 cpPlugins_Parameters_Clear( Vector );
437
438 // -------------------------------------------------------------------------
439 bool cpPlugins::Interface::Parameters::
440 SetSelectedChoice( const TString& name, const TString& choice )
441 {
442   TParameters::iterator i = this->m_Parameters.find( name );
443   if( i == this->m_Parameters.end( ) )
444     return( false );
445   if( i->second.first != Self::Choices )
446     return( false );
447
448   std::vector< TString > c;
449   this->GetChoices( c, name );
450   if( std::find( c.begin( ), c.end( ), choice ) != c.end( ) )
451   {
452     std::istringstream str_choices( i->second.second );
453     std::string choices;
454     std::getline( str_choices, choices, '@' );
455     std::stringstream new_choices;
456     new_choices << choices << "@" << choice;
457     i->second.second = new_choices.str( );
458     return( true );
459   }
460   else
461     return( false );
462 }
463
464 // -------------------------------------------------------------------------
465 bool cpPlugins::Interface::Parameters::
466 ToXML( TiXmlElement* parent_elem ) const
467 {
468   if( parent_elem == NULL )
469     return( false );
470
471   auto pIt = this->m_Parameters.begin( );
472   for( ; pIt != this->m_Parameters.end( ); ++pIt )
473   {
474     TiXmlElement* p = new TiXmlElement( "parameter" );
475     p->SetAttribute( "name", pIt->first.c_str( ) );
476     p->SetAttribute( "value", pIt->second.second.c_str( ) );
477     if( pIt->second.first == Self::String )
478       p->SetAttribute( "type", "String" );
479     else if( pIt->second.first == Self::Bool )
480       p->SetAttribute( "type", "Bool" );
481     else if( pIt->second.first == Self::Int )
482       p->SetAttribute( "type", "Int" );
483     else if( pIt->second.first == Self::Uint )
484       p->SetAttribute( "type", "Uint" );
485     else if( pIt->second.first == Self::Real )
486       p->SetAttribute( "type", "Real" );
487     else if( pIt->second.first == Self::Index )
488       p->SetAttribute( "type", "Index" );
489     else if( pIt->second.first == Self::Point )
490       p->SetAttribute( "type", "Point" );
491     else if( pIt->second.first == Self::Vector )
492       p->SetAttribute( "type", "Vector" );
493     else if( pIt->second.first == Self::StringList )
494       p->SetAttribute( "type", "StringList" );
495     else if( pIt->second.first == Self::BoolList )
496       p->SetAttribute( "type", "BoolList" );
497     else if( pIt->second.first == Self::IntList )
498       p->SetAttribute( "type", "IntList" );
499     else if( pIt->second.first == Self::UintList )
500       p->SetAttribute( "type", "UintList" );
501     else if( pIt->second.first == Self::RealList )
502       p->SetAttribute( "type", "RealList" );
503     else if( pIt->second.first == Self::IndexList )
504       p->SetAttribute( "type", "IndexList" );
505     else if( pIt->second.first == Self::PointList )
506       p->SetAttribute( "type", "PointList" );
507     else if( pIt->second.first == Self::VectorList )
508       p->SetAttribute( "type", "VectorList" );
509     else if( pIt->second.first == Self::Choices )
510       p->SetAttribute( "type", "Choices" );
511     parent_elem->LinkEndChild( p );
512
513   } // rof
514   return( true );
515 }
516
517 // -------------------------------------------------------------------------
518 bool cpPlugins::Interface::Parameters::
519 FromXML( const TiXmlElement* filter_elem )
520 {
521   this->m_Parameters.clear( );
522
523   const TiXmlElement* param = filter_elem->FirstChildElement( "parameter" );
524   bool ret = false;
525   while( param != NULL )
526   {
527     const char* param_name = param->Attribute( "name" );
528     const char* param_type = param->Attribute( "type" );
529     if( param_name != NULL && param_type != NULL )
530     {
531       TParameter value;
532       value.second = param->Attribute( "value" );
533
534       std::string param_type_str( param_type );
535       if( param_type_str == "Bool" )
536         value.first = Self::Bool;
537       else if( param_type_str == "Int" )
538         value.first = Self::Int;
539       else if( param_type_str == "Uint" )
540         value.first = Self::Uint;
541       else if( param_type_str == "Real" )
542         value.first = Self::Real;
543       else if( param_type_str == "Index" )
544         value.first = Self::Index;
545       else if( param_type_str == "Point" )
546         value.first = Self::Point;
547       else if( param_type_str == "Vector" )
548         value.first = Self::Vector;
549       else if( param_type_str == "StringList" )
550         value.first = Self::StringList;
551       else if( param_type_str == "BoolList" )
552         value.first = Self::BoolList;
553       else if( param_type_str == "IntList" )
554         value.first = Self::IntList;
555       else if( param_type_str == "UintList" )
556         value.first = Self::UintList;
557       else if( param_type_str == "RealList" )
558         value.first = Self::RealList;
559       else if( param_type_str == "IndexList" )
560         value.first = Self::IndexList;
561       else if( param_type_str == "PointList" )
562         value.first = Self::PointList;
563       else if( param_type_str == "VectorList" )
564         value.first = Self::VectorList;
565       else if( param_type_str == "Choices" )
566         value.first = Self::Choices;
567       else
568         value.first = Self::String;
569
570       this->m_Parameters[ param_name ] = value;
571
572     } // fi
573     param = param->NextSiblingElement( "parameter" );
574     ret = true;
575
576   } // elihw
577   return( ret );
578 }
579
580 // -------------------------------------------------------------------------
581 cpPlugins::Interface::Parameters::
582 Parameters( )
583   : Superclass( ),
584     m_Process( NULL )
585 {
586   this->Clear( );
587 }
588
589 // -------------------------------------------------------------------------
590 cpPlugins::Interface::Parameters::
591 ~Parameters( )
592 {
593 }
594
595 // -------------------------------------------------------------------------
596 void cpPlugins::Interface::Parameters::
597 PrintSelf( std::ostream& os, itk::Indent indent ) const
598 {
599   TParameters::const_iterator i = this->m_Parameters.begin( );
600   for( ; i != this->m_Parameters.end( ); ++i )
601     os << indent
602        << i->first << ": ("
603        << i->second.first << " | "
604        << i->second.second << ")"
605        << std::endl;
606 }
607
608 // eof - $RCSfile$