]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Parameters.cxx
3423bea68cbea27a735027cbb2d5a633454391f2
[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 // -------------------------------------------------------------------------
6 const cpPlugins::Interface::Parameters::
7 TParameters& cpPlugins::Interface::Parameters::
8 GetRawParameters( ) const
9 {
10   return( this->m_Parameters );
11 }
12
13 // -------------------------------------------------------------------------
14 cpPlugins::Interface::
15 ProcessObject* cpPlugins::Interface::Parameters::
16 GetProcessObject( )
17 {
18   return( this->m_Process );
19 }
20
21 // -------------------------------------------------------------------------
22 const cpPlugins::Interface::
23 ProcessObject* cpPlugins::Interface::Parameters::
24 GetProcessObject( ) const
25 {
26   return( this->m_Process );
27 }
28
29 // -------------------------------------------------------------------------
30 void cpPlugins::Interface::Parameters::
31 SetProcessObject( ProcessObject* v )
32 {
33   this->m_Process = v;
34 }
35
36 // -------------------------------------------------------------------------
37 void cpPlugins::Interface::Parameters::
38 Modified( ) const
39 {
40   this->Superclass::Modified( );
41   if( this->m_Process != NULL )
42     this->m_Process->Modified( );
43 }
44
45 // -------------------------------------------------------------------------
46 void cpPlugins::Interface::Parameters::
47 Clear( )
48 {
49   this->m_Parameters.clear( );
50   this->Modified( );
51 }
52
53 // -------------------------------------------------------------------------
54 void cpPlugins::Interface::Parameters::
55 GetNames( std::vector< TString >& container ) const
56 {
57   container.clear( );
58   TParameters::const_iterator i = this->m_Parameters.begin( );
59   for( ; i != this->m_Parameters.end( ); ++i )
60     container.push_back( i->first );
61 }
62
63 // -------------------------------------------------------------------------
64 cpPlugins::Interface::Parameters::
65 Type cpPlugins::Interface::Parameters::
66 GetType( const TString& name ) const
67 {
68   auto i = this->m_Parameters.find( name );
69   if( i != this->m_Parameters.end( ) )
70     return( i->second.first );
71   else
72     return( Self::NoType );
73 }
74
75 // -------------------------------------------------------------------------
76 #define cpPlugins_Parameters_TypeAsString( Y )  \
77   if( i->second.first == Self::Y )              \
78     return( #Y )
79
80 std::string cpPlugins::Interface::Parameters::
81 GetTypeAsString( const TString& name ) const
82 {
83   auto i = this->m_Parameters.find( name );
84   cpPlugins_Parameters_TypeAsString( String );
85   else cpPlugins_Parameters_TypeAsString( Bool );
86   else cpPlugins_Parameters_TypeAsString( Int );
87   else cpPlugins_Parameters_TypeAsString( Uint );
88   else cpPlugins_Parameters_TypeAsString( Real );
89   else cpPlugins_Parameters_TypeAsString( Index );
90   else cpPlugins_Parameters_TypeAsString( Point );
91   else cpPlugins_Parameters_TypeAsString( Vector );
92   else cpPlugins_Parameters_TypeAsString( FileName );
93   else cpPlugins_Parameters_TypeAsString( PathName );
94   else cpPlugins_Parameters_TypeAsString( StringList );
95   else cpPlugins_Parameters_TypeAsString( BoolList );
96   else cpPlugins_Parameters_TypeAsString( IntList );
97   else cpPlugins_Parameters_TypeAsString( UintList );
98   else cpPlugins_Parameters_TypeAsString( RealList );
99   else cpPlugins_Parameters_TypeAsString( IndexList );
100   else cpPlugins_Parameters_TypeAsString( PointList );
101   else cpPlugins_Parameters_TypeAsString( VectorList );
102   else cpPlugins_Parameters_TypeAsString( FileNameList );
103   else cpPlugins_Parameters_TypeAsString( PathNameList );
104   else cpPlugins_Parameters_TypeAsString( Choices );
105   else return( "NoType" );
106 }
107
108 // -------------------------------------------------------------------------
109 #define cpPlugins_Parameters_TypeFromString( Y, str )   \
110   if( str == std::string( #Y ) )                        \
111     return( Self::Y )
112
113 cpPlugins::Interface::Parameters::
114 Type cpPlugins::Interface::Parameters::
115 GetTypeFromString( const std::string& t )
116 {
117   cpPlugins_Parameters_TypeFromString( String, t );
118   else cpPlugins_Parameters_TypeFromString( Bool, t );
119   else cpPlugins_Parameters_TypeFromString( Int, t );
120   else cpPlugins_Parameters_TypeFromString( Uint, t );
121   else cpPlugins_Parameters_TypeFromString( Real, t );
122   else cpPlugins_Parameters_TypeFromString( Index, t );
123   else cpPlugins_Parameters_TypeFromString( Point, t );
124   else cpPlugins_Parameters_TypeFromString( Vector, t );
125   else cpPlugins_Parameters_TypeFromString( FileName, t );
126   else cpPlugins_Parameters_TypeFromString( PathName, t );
127   else cpPlugins_Parameters_TypeFromString( StringList, t );
128   else cpPlugins_Parameters_TypeFromString( BoolList, t );
129   else cpPlugins_Parameters_TypeFromString( IntList, t );
130   else cpPlugins_Parameters_TypeFromString( UintList, t );
131   else cpPlugins_Parameters_TypeFromString( RealList, t );
132   else cpPlugins_Parameters_TypeFromString( IndexList, t );
133   else cpPlugins_Parameters_TypeFromString( PointList, t );
134   else cpPlugins_Parameters_TypeFromString( VectorList, t );
135   else cpPlugins_Parameters_TypeFromString( FileNameList, t );
136   else cpPlugins_Parameters_TypeFromString( PathNameList, t );
137   else cpPlugins_Parameters_TypeFromString( Choices, t );
138   else return( Self::NoType );
139 }
140
141 // -------------------------------------------------------------------------
142 std::string cpPlugins::Interface::Parameters::
143 GetString( const std::string& name, bool force ) const
144 {
145   auto i = this->m_Parameters.find( name );
146   if( i != this->m_Parameters.end( ) )
147   {
148     if( i->second.first == Self::String || force )
149       return( i->second.second );
150     else
151       return( "" );
152   }
153   else
154     return( "" );
155 }
156
157 // -------------------------------------------------------------------------
158 void cpPlugins::Interface::Parameters::
159 SetString( const std::string& name, const std::string& v, bool force )
160 {
161   auto i = this->m_Parameters.find( name );
162   if( i != this->m_Parameters.end( ) )
163   {
164     if( i->second.first == Self::String || force )
165     {
166       i->second.second = v;
167       this->Modified( );
168
169     } // fi
170
171   } // fi
172 }
173
174 // -------------------------------------------------------------------------
175 void cpPlugins::Interface::Parameters::
176 ConfigureAsChoices(
177   const std::string& name, const std::vector< std::string >& choices
178   )
179 {
180   // It is invalid not to give choices when configuring
181   if( choices.size( ) == 0 )
182     return;
183
184   std::stringstream str_choices;
185   str_choices << choices[ 0 ];
186   for( unsigned int i = 1; i < choices.size( ); ++i )
187     str_choices << "#" << choices[ i ];
188   str_choices << "@";
189   this->m_Parameters[ name ] =
190     TParameter( Self::Choices, str_choices.str( ) );
191   this->Modified( );
192 }
193
194 // -------------------------------------------------------------------------
195 std::vector< std::string > cpPlugins::Interface::Parameters::
196 GetChoices( const TString& name ) const
197 {
198   std::vector< std::string > choices;
199
200   TParameters::const_iterator i = this->m_Parameters.find( name );
201   if( i != this->m_Parameters.end( ) )
202   {
203     if( i->second.first == Self::Choices )
204     {
205       std::istringstream str_choices( i->second.second );
206       std::string real_choices;
207       std::getline( str_choices, real_choices, '@' );
208       std::istringstream str( real_choices );
209       std::string token;
210       while( std::getline( str, token, '#' ) )
211         choices.push_back( token );
212
213     } // fi
214
215   } // fi
216   return( choices );
217 }
218
219 // -------------------------------------------------------------------------
220 std::string cpPlugins::Interface::Parameters::
221 GetSelectedChoice( const std::string& name ) const
222 {
223   auto i = this->m_Parameters.find( name );
224   if( i != this->m_Parameters.end( ) )
225   {
226     if( i->second.first == Self::Choices )
227     {
228       std::istringstream str_choices( i->second.second );
229       std::string real_choice;
230       std::getline( str_choices, real_choice, '@' );
231       std::getline( str_choices, real_choice, '@' );
232       return( real_choice );
233     }
234     else
235       return( "" );
236   }
237   else
238     return( "" );
239 }
240
241 // -------------------------------------------------------------------------
242 bool cpPlugins::Interface::Parameters::
243 SetSelectedChoice( const TString& name, const TString& choice )
244 {
245   auto i = this->m_Parameters.find( name );
246   if( i != this->m_Parameters.end( ) )
247   {
248     if( i->second.first == Self::Choices )
249     {
250       std::vector< std::string > c = this->GetChoices( name );
251       if( std::find( c.begin( ), c.end( ), choice ) != c.end( ) )
252       {
253         std::istringstream str_choices( i->second.second );
254         std::string choices;
255         std::getline( str_choices, choices, '@' );
256         std::stringstream new_choices;
257         new_choices << choices << "@" << choice;
258         i->second.second = new_choices.str( );
259         return( true );
260       }
261       else
262         return( false );
263     }
264     else
265       return( false );
266   }
267   else
268     return( false );
269 }
270
271 // -------------------------------------------------------------------------
272 #define cpPlugins_Parameters_ClearList( Y )                     \
273   void cpPlugins::Interface::Parameters::                       \
274   Clear##Y##List( const TString& name )                         \
275   {                                                             \
276     auto i = this->m_Parameters.find( name );                   \
277     if( i == this->m_Parameters.end( ) )                        \
278       return;                                                   \
279     if( i->second.first != Self::Y##List )                      \
280       return;                                                   \
281     i->second.second = "";                                      \
282     this->Modified( );                                          \
283   }
284
285 cpPlugins_Parameters_ClearList( Index );
286 cpPlugins_Parameters_ClearList( Point );
287 cpPlugins_Parameters_ClearList( Vector );
288
289 // -------------------------------------------------------------------------
290 bool cpPlugins::Interface::Parameters::
291 ToXML( TiXmlElement* parent_elem ) const
292 {
293   if( parent_elem == NULL )
294     return( false );
295
296   auto pIt = this->m_Parameters.begin( );
297   for( ; pIt != this->m_Parameters.end( ); ++pIt )
298   {
299     TiXmlElement* p = new TiXmlElement( "parameter" );
300     p->SetAttribute( "name", pIt->first.c_str( ) );
301     p->SetAttribute( "value", pIt->second.second.c_str( ) );
302     p->SetAttribute( "type", this->GetTypeAsString( pIt->first ).c_str( ) );
303     parent_elem->LinkEndChild( p );
304
305   } // rof
306   return( true );
307 }
308
309 // -------------------------------------------------------------------------
310 bool cpPlugins::Interface::Parameters::
311 FromXML( const TiXmlElement* filter_elem )
312 {
313   this->m_Parameters.clear( );
314
315   const TiXmlElement* param = filter_elem->FirstChildElement( "parameter" );
316   bool ret = false;
317   while( param != NULL )
318   {
319     const char* param_name = param->Attribute( "name" );
320     const char* param_type = param->Attribute( "type" );
321     if( param_name != NULL && param_type != NULL )
322     {
323       TParameter value;
324       value.second = param->Attribute( "value" );
325       value.first = Self::GetTypeFromString( param_type );
326       this->m_Parameters[ param_name ] = value;
327
328     } // fi
329     param = param->NextSiblingElement( "parameter" );
330     ret = true;
331
332   } // elihw
333   return( ret );
334 }
335
336 // -------------------------------------------------------------------------
337 cpPlugins::Interface::Parameters::
338 Parameters( )
339   : Superclass( ),
340     m_Process( NULL )
341 {
342   this->Clear( );
343 }
344
345 // -------------------------------------------------------------------------
346 cpPlugins::Interface::Parameters::
347 ~Parameters( )
348 {
349 }
350
351 // -------------------------------------------------------------------------
352 void cpPlugins::Interface::Parameters::
353 PrintSelf( std::ostream& os, itk::Indent indent ) const
354 {
355   TParameters::const_iterator i = this->m_Parameters.begin( );
356   for( ; i != this->m_Parameters.end( ); ++i )
357     os << indent
358        << i->first << ": ("
359        << i->second.first << " | "
360        << i->second.second << ")"
361        << std::endl;
362 }
363
364 // eof - $RCSfile$