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