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