1 #include <cpPlugins/Parameters.h>
2 #include <tinyxml/tinyxml2.h>
4 // -------------------------------------------------------------------------
5 cpPlugins::Parameters::
11 // -------------------------------------------------------------------------
12 cpPlugins::Parameters::
17 // -------------------------------------------------------------------------
18 void cpPlugins::Parameters::
21 this->m_TimeStamp.Modified( );
24 // -------------------------------------------------------------------------
25 itk::ModifiedTimeType cpPlugins::Parameters::
28 return( this->m_TimeStamp.GetMTime( ) );
31 // -------------------------------------------------------------------------
32 void cpPlugins::Parameters::
35 this->m_Parameters.clear( );
39 // -------------------------------------------------------------------------
40 void cpPlugins::Parameters::
41 GetNames( std::vector< std::string >& container ) const
44 TParameters::const_iterator i = this->m_Parameters.begin( );
45 for( ; i != this->m_Parameters.end( ); ++i )
46 container.push_back( i->first );
49 // -------------------------------------------------------------------------
50 cpPlugins::Parameters::
51 Type cpPlugins::Parameters::
52 GetType( const std::string& name ) const
54 auto i = this->m_Parameters.find( name );
55 if( i != this->m_Parameters.end( ) )
56 return( i->second.first );
58 return( Self::NoType );
61 // -------------------------------------------------------------------------
62 #define cpPlugins_Parameters_TypeAsString( Y ) \
63 if( i->second.first == Self::Y ) \
66 std::string cpPlugins::Parameters::
67 GetTypeAsString( const std::string& name ) const
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" );
90 // -------------------------------------------------------------------------
91 #define cpPlugins_Parameters_TypeFromString( Y, str ) \
92 if( str == std::string( #Y ) ) \
95 cpPlugins::Parameters::
96 Type cpPlugins::Parameters::
97 GetTypeFromString( const std::string& t )
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 );
119 // -------------------------------------------------------------------------
120 std::string cpPlugins::Parameters::
121 GetString( const std::string& name, bool force ) const
123 auto i = this->m_Parameters.find( name );
124 if( i != this->m_Parameters.end( ) )
126 if( i->second.first == Self::String || force )
127 return( i->second.second );
135 // -------------------------------------------------------------------------
136 void cpPlugins::Parameters::
137 SetString( const std::string& name, const std::string& v, bool force )
139 auto i = this->m_Parameters.find( name );
140 if( i != this->m_Parameters.end( ) )
142 if( i->second.first == Self::String || force )
144 if( i->second.second != v )
146 i->second.second = v;
156 // -------------------------------------------------------------------------
157 void cpPlugins::Parameters::
159 const std::string& name, const std::vector< std::string >& choices
162 // It is invalid not to give choices when configuring
163 if( choices.size( ) == 0 )
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 ];
171 this->m_Parameters[ name ] =
172 TParameter( Self::Choices, str_choices.str( ) );
176 // -------------------------------------------------------------------------
177 std::vector< std::string > cpPlugins::Parameters::
178 GetChoices( const std::string& name ) const
180 std::vector< std::string > choices;
182 TParameters::const_iterator i = this->m_Parameters.find( name );
183 if( i != this->m_Parameters.end( ) )
185 if( i->second.first == Self::Choices )
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 );
192 while( std::getline( str, token, '#' ) )
193 choices.push_back( token );
201 // -------------------------------------------------------------------------
202 std::string cpPlugins::Parameters::
203 GetSelectedChoice( const std::string& name ) const
205 auto i = this->m_Parameters.find( name );
206 if( i != this->m_Parameters.end( ) )
208 if( i->second.first == Self::Choices )
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 );
223 // -------------------------------------------------------------------------
224 bool cpPlugins::Parameters::
225 SetSelectedChoice( const std::string& name, const std::string& choice )
227 auto i = this->m_Parameters.find( name );
228 if( i != this->m_Parameters.end( ) )
230 if( i->second.first == Self::Choices )
232 std::istringstream str_choices( i->second.second );
234 std::getline( str_choices, choices, '@' );
235 if( choices.find( choice ) != std::string::npos )
237 std::stringstream new_choices;
238 new_choices << choices << "@" << choice;
239 i->second.second = new_choices.str( );
253 // -------------------------------------------------------------------------
254 std::string cpPlugins::Parameters::
255 GetAcceptedFileExtensions( const std::string& name ) const
257 auto i = this->m_AcceptedFileExtensions.find( name );
258 if( i != this->m_AcceptedFileExtensions.end( ) )
264 // -------------------------------------------------------------------------
265 void cpPlugins::Parameters::
266 SetAcceptedFileExtensions(
267 const std::string& name, const std::string& extensions
270 auto i = this->m_Parameters.find( name );
271 if( i != this->m_Parameters.end( ) )
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 );
278 this->m_AcceptedFileExtensions[ name ] = extensions;
283 // -------------------------------------------------------------------------
284 bool cpPlugins::Parameters::
286 tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* parent_elem
289 if( parent_elem == NULL )
292 auto pIt = this->m_Parameters.begin( );
293 for( ; pIt != this->m_Parameters.end( ); ++pIt )
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 );
305 // -------------------------------------------------------------------------
306 bool cpPlugins::Parameters::
307 FromXML( const tinyxml2::XMLElement* filter_elem )
309 const tinyxml2::XMLElement* param =
310 filter_elem->FirstChildElement( "parameter" );
312 while( param != NULL )
314 const char* param_name = param->Attribute( "name" );
315 const char* param_type = param->Attribute( "type" );
316 if( param_name != NULL && param_type != NULL )
319 value.second = param->Attribute( "value" );
320 value.first = Self::GetTypeFromString( param_type );
321 this->m_Parameters[ param_name ] = value;
324 param = param->NextSiblingElement( "parameter" );
332 // -------------------------------------------------------------------------
333 cpPlugins::Parameters::
334 TParameters& cpPlugins::Parameters::
337 return( this->m_Parameters );
340 // -------------------------------------------------------------------------
341 const cpPlugins::Parameters::
342 TParameters& cpPlugins::Parameters::
343 GetRawParameters( ) const
345 return( this->m_Parameters );