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