]> 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   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< std::string >& 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 std::string& 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 std::string& 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( OpenFileName );
82   else cpPlugins_Parameters_TypeAsString( SaveFileName );
83   else cpPlugins_Parameters_TypeAsString( PathName );
84   else cpPlugins_Parameters_TypeAsString( StringList );
85   else cpPlugins_Parameters_TypeAsString( BoolList );
86   else cpPlugins_Parameters_TypeAsString( IntList );
87   else cpPlugins_Parameters_TypeAsString( UintList );
88   else cpPlugins_Parameters_TypeAsString( RealList );
89   else cpPlugins_Parameters_TypeAsString( OpenFileNameList );
90   else cpPlugins_Parameters_TypeAsString( SaveFileNameList );
91   else cpPlugins_Parameters_TypeAsString( PathNameList );
92   else cpPlugins_Parameters_TypeAsString( Choices );
93   else return( "NoType" );
94 }
95
96 // -------------------------------------------------------------------------
97 #define cpPlugins_Parameters_TypeFromString( Y, str )   \
98   if( str == std::string( #Y ) )                        \
99     return( Self::Y )
100
101 cpPlugins::Interface::Parameters::
102 Type cpPlugins::Interface::Parameters::
103 GetTypeFromString( const std::string& t )
104 {
105   cpPlugins_Parameters_TypeFromString( String, t );
106   else cpPlugins_Parameters_TypeFromString( Bool, t );
107   else cpPlugins_Parameters_TypeFromString( Int, t );
108   else cpPlugins_Parameters_TypeFromString( Uint, t );
109   else cpPlugins_Parameters_TypeFromString( Real, t );
110   else cpPlugins_Parameters_TypeFromString( OpenFileName, t );
111   else cpPlugins_Parameters_TypeFromString( SaveFileName, t );
112   else cpPlugins_Parameters_TypeFromString( PathName, t );
113   else cpPlugins_Parameters_TypeFromString( StringList, t );
114   else cpPlugins_Parameters_TypeFromString( BoolList, t );
115   else cpPlugins_Parameters_TypeFromString( IntList, t );
116   else cpPlugins_Parameters_TypeFromString( UintList, t );
117   else cpPlugins_Parameters_TypeFromString( RealList, t );
118   else cpPlugins_Parameters_TypeFromString( OpenFileNameList, t );
119   else cpPlugins_Parameters_TypeFromString( SaveFileNameList, t );
120   else cpPlugins_Parameters_TypeFromString( PathNameList, t );
121   else cpPlugins_Parameters_TypeFromString( Choices, t );
122   else return( Self::NoType );
123 }
124
125 // -------------------------------------------------------------------------
126 std::string cpPlugins::Interface::Parameters::
127 GetString( const std::string& name, bool force ) const
128 {
129   auto i = this->m_Parameters.find( name );
130   if( i != this->m_Parameters.end( ) )
131   {
132     if( i->second.first == Self::String || force )
133       return( i->second.second );
134     else
135       return( "" );
136   }
137   else
138     return( "" );
139 }
140
141 // -------------------------------------------------------------------------
142 void cpPlugins::Interface::Parameters::
143 SetString( const std::string& name, const std::string& v, bool force )
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     {
150       i->second.second = v;
151       this->Modified( );
152
153     } // fi
154
155   } // fi
156 }
157
158 // -------------------------------------------------------------------------
159 void cpPlugins::Interface::Parameters::
160 ConfigureAsChoices(
161   const std::string& name, const std::vector< std::string >& choices
162   )
163 {
164   // It is invalid not to give choices when configuring
165   if( choices.size( ) == 0 )
166     return;
167
168   std::stringstream str_choices;
169   str_choices << choices[ 0 ];
170   for( unsigned int i = 1; i < choices.size( ); ++i )
171     str_choices << "#" << choices[ i ];
172   str_choices << "@";
173   this->m_Parameters[ name ] =
174     TParameter( Self::Choices, str_choices.str( ) );
175   this->Modified( );
176 }
177
178 // -------------------------------------------------------------------------
179 std::vector< std::string > cpPlugins::Interface::Parameters::
180 GetChoices( const std::string& name ) const
181 {
182   std::vector< std::string > choices;
183
184   TParameters::const_iterator i = this->m_Parameters.find( name );
185   if( i != this->m_Parameters.end( ) )
186   {
187     if( i->second.first == Self::Choices )
188     {
189       std::istringstream str_choices( i->second.second );
190       std::string real_choices;
191       std::getline( str_choices, real_choices, '@' );
192       std::istringstream str( real_choices );
193       std::string token;
194       while( std::getline( str, token, '#' ) )
195         choices.push_back( token );
196
197     } // fi
198
199   } // fi
200   return( choices );
201 }
202
203 // -------------------------------------------------------------------------
204 std::string cpPlugins::Interface::Parameters::
205 GetSelectedChoice( const std::string& name ) const
206 {
207   auto i = this->m_Parameters.find( name );
208   if( i != this->m_Parameters.end( ) )
209   {
210     if( i->second.first == Self::Choices )
211     {
212       std::istringstream str_choices( i->second.second );
213       std::string real_choice;
214       std::getline( str_choices, real_choice, '@' );
215       std::getline( str_choices, real_choice, '@' );
216       return( real_choice );
217     }
218     else
219       return( "" );
220   }
221   else
222     return( "" );
223 }
224
225 // -------------------------------------------------------------------------
226 bool cpPlugins::Interface::Parameters::
227 SetSelectedChoice( const std::string& name, const std::string& choice )
228 {
229   auto i = this->m_Parameters.find( name );
230   if( i != this->m_Parameters.end( ) )
231   {
232     if( i->second.first == Self::Choices )
233     {
234       std::istringstream str_choices( i->second.second );
235       std::string choices;
236       std::getline( str_choices, choices, '@' );
237       if( choices.find( choice ) != std::string::npos )
238       {
239         std::stringstream new_choices;
240         new_choices << choices << "@" << choice;
241         i->second.second = new_choices.str( );
242         return( true );
243       }
244       else
245         return( false );
246     }
247     else
248       return( false );
249   }
250   else
251     return( false );
252 }
253
254 // -------------------------------------------------------------------------
255 bool cpPlugins::Interface::Parameters::
256 ToXML( TiXmlElement* parent_elem ) const
257 {
258   if( parent_elem == NULL )
259     return( false );
260
261   auto pIt = this->m_Parameters.begin( );
262   for( ; pIt != this->m_Parameters.end( ); ++pIt )
263   {
264     TiXmlElement* p = new TiXmlElement( "parameter" );
265     p->SetAttribute( "name", pIt->first.c_str( ) );
266     p->SetAttribute( "value", pIt->second.second.c_str( ) );
267     p->SetAttribute( "type", this->GetTypeAsString( pIt->first ).c_str( ) );
268     parent_elem->LinkEndChild( p );
269
270   } // rof
271   return( true );
272 }
273
274 // -------------------------------------------------------------------------
275 bool cpPlugins::Interface::Parameters::
276 FromXML( const TiXmlElement* filter_elem )
277 {
278   this->m_Parameters.clear( );
279
280   const TiXmlElement* param = filter_elem->FirstChildElement( "parameter" );
281   bool ret = false;
282   while( param != NULL )
283   {
284     const char* param_name = param->Attribute( "name" );
285     const char* param_type = param->Attribute( "type" );
286     if( param_name != NULL && param_type != NULL )
287     {
288       TParameter value;
289       value.second = param->Attribute( "value" );
290       value.first = Self::GetTypeFromString( param_type );
291       this->m_Parameters[ param_name ] = value;
292
293     } // fi
294     param = param->NextSiblingElement( "parameter" );
295     ret = true;
296
297   } // elihw
298   return( ret );
299 }
300
301 // -------------------------------------------------------------------------
302 cpPlugins::Interface::Parameters::
303 Parameters( )
304   : Superclass( ),
305     m_Process( NULL )
306 {
307   this->Clear( );
308 }
309
310 // -------------------------------------------------------------------------
311 cpPlugins::Interface::Parameters::
312 ~Parameters( )
313 {
314 }
315
316 // -------------------------------------------------------------------------
317 void cpPlugins::Interface::Parameters::
318 PrintSelf( std::ostream& os, itk::Indent indent ) const
319 {
320   TParameters::const_iterator i = this->m_Parameters.begin( );
321   for( ; i != this->m_Parameters.end( ); ++i )
322     os << indent
323        << i->first << ": ("
324        << i->second.first << " | "
325        << i->second.second << ")"
326        << std::endl;
327 }
328
329 // -------------------------------------------------------------------------
330 cpPlugins::Interface::Parameters::
331 TParameters& cpPlugins::Interface::Parameters::
332 GetRawParameters( )
333 {
334   return( this->m_Parameters );
335 }
336
337 // -------------------------------------------------------------------------
338 const cpPlugins::Interface::Parameters::
339 TParameters& cpPlugins::Interface::Parameters::
340 GetRawParameters( ) const
341 {
342   return( this->m_Parameters );
343 }
344
345 // eof - $RCSfile$