1 #include <cpPlugins/Interface/Parameters.h>
2 #include <cpPlugins/Interface/ProcessObject.h>
3 #include <third_party/tinyxml/tinyxml.h>
5 // -------------------------------------------------------------------------
7 ProcessObject* cpPlugins::Interface::Parameters::
10 return( this->m_Process );
13 // -------------------------------------------------------------------------
14 const cpPlugins::Interface::
15 ProcessObject* cpPlugins::Interface::Parameters::
16 GetProcessObject( ) const
18 return( this->m_Process );
21 // -------------------------------------------------------------------------
22 void cpPlugins::Interface::Parameters::
23 SetProcessObject( ProcessObject* v )
28 // -------------------------------------------------------------------------
29 void cpPlugins::Interface::Parameters::
32 this->Superclass::Modified( );
33 if( this->m_Process != NULL )
34 this->m_Process->Modified( );
37 // -------------------------------------------------------------------------
38 void cpPlugins::Interface::Parameters::
41 this->m_Parameters.clear( );
45 // -------------------------------------------------------------------------
46 void cpPlugins::Interface::Parameters::
47 GetNames( std::vector< TString >& container ) const
50 TParameters::const_iterator i = this->m_Parameters.begin( );
51 for( ; i != this->m_Parameters.end( ); ++i )
52 container.push_back( i->first );
55 // -------------------------------------------------------------------------
56 cpPlugins::Interface::Parameters::
57 Type cpPlugins::Interface::Parameters::
58 GetType( const TString& name ) const
60 auto i = this->m_Parameters.find( name );
61 if( i != this->m_Parameters.end( ) )
62 return( i->second.first );
64 return( Self::NoType );
67 // -------------------------------------------------------------------------
68 #define cpPlugins_Parameters_TypeAsString( Y ) \
69 if( i->second.first == Self::Y ) \
72 std::string cpPlugins::Interface::Parameters::
73 GetTypeAsString( const TString& name ) const
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" );
102 // -------------------------------------------------------------------------
103 #define cpPlugins_Parameters_TypeFromString( Y, str ) \
104 if( str == std::string( #Y ) ) \
107 cpPlugins::Interface::Parameters::
108 Type cpPlugins::Interface::Parameters::
109 GetTypeFromString( const std::string& t )
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 );
137 // -------------------------------------------------------------------------
138 std::string cpPlugins::Interface::Parameters::
139 GetString( const std::string& name, bool force ) const
141 auto i = this->m_Parameters.find( name );
142 if( i != this->m_Parameters.end( ) )
144 if( i->second.first == Self::String || force )
145 return( i->second.second );
153 // -------------------------------------------------------------------------
154 void cpPlugins::Interface::Parameters::
155 SetString( const std::string& name, const std::string& v, bool force )
157 auto i = this->m_Parameters.find( name );
158 if( i != this->m_Parameters.end( ) )
160 if( i->second.first == Self::String || force )
162 i->second.second = v;
170 // -------------------------------------------------------------------------
171 void cpPlugins::Interface::Parameters::
173 const std::string& name, const std::vector< std::string >& choices
176 // It is invalid not to give choices when configuring
177 if( choices.size( ) == 0 )
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 ];
185 this->m_Parameters[ name ] =
186 TParameter( Self::Choices, str_choices.str( ) );
190 // -------------------------------------------------------------------------
191 std::vector< std::string > cpPlugins::Interface::Parameters::
192 GetChoices( const TString& name ) const
194 std::vector< std::string > choices;
196 TParameters::const_iterator i = this->m_Parameters.find( name );
197 if( i != this->m_Parameters.end( ) )
199 if( i->second.first == Self::Choices )
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 );
206 while( std::getline( str, token, '#' ) )
207 choices.push_back( token );
215 // -------------------------------------------------------------------------
216 std::string cpPlugins::Interface::Parameters::
217 GetSelectedChoice( const std::string& name ) const
219 auto i = this->m_Parameters.find( name );
220 if( i != this->m_Parameters.end( ) )
222 if( i->second.first == Self::Choices )
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 );
237 // -------------------------------------------------------------------------
238 bool cpPlugins::Interface::Parameters::
239 SetSelectedChoice( const TString& name, const TString& choice )
241 auto i = this->m_Parameters.find( name );
242 if( i != this->m_Parameters.end( ) )
244 if( i->second.first == Self::Choices )
246 std::vector< std::string > c = this->GetChoices( name );
247 if( std::find( c.begin( ), c.end( ), choice ) != c.end( ) )
249 std::istringstream str_choices( i->second.second );
251 std::getline( str_choices, choices, '@' );
252 std::stringstream new_choices;
253 new_choices << choices << "@" << choice;
254 i->second.second = new_choices.str( );
267 // -------------------------------------------------------------------------
268 #define cpPlugins_Parameters_ClearList( Y ) \
269 void cpPlugins::Interface::Parameters:: \
270 Clear##Y##List( const TString& name ) \
272 auto i = this->m_Parameters.find( name ); \
273 if( i == this->m_Parameters.end( ) ) \
275 if( i->second.first != Self::Y##List ) \
277 i->second.second = ""; \
281 cpPlugins_Parameters_ClearList( Index );
282 cpPlugins_Parameters_ClearList( Point );
283 cpPlugins_Parameters_ClearList( Vector );
285 // -------------------------------------------------------------------------
286 bool cpPlugins::Interface::Parameters::
287 ToXML( TiXmlElement* parent_elem ) const
289 if( parent_elem == NULL )
292 auto pIt = this->m_Parameters.begin( );
293 for( ; pIt != this->m_Parameters.end( ); ++pIt )
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 );
305 // -------------------------------------------------------------------------
306 bool cpPlugins::Interface::Parameters::
307 FromXML( const TiXmlElement* filter_elem )
309 this->m_Parameters.clear( );
311 const TiXmlElement* param = filter_elem->FirstChildElement( "parameter" );
313 while( param != NULL )
315 const char* param_name = param->Attribute( "name" );
316 const char* param_type = param->Attribute( "type" );
317 if( param_name != NULL && param_type != NULL )
320 value.second = param->Attribute( "value" );
321 value.first = Self::GetTypeFromString( param_type );
322 this->m_Parameters[ param_name ] = value;
325 param = param->NextSiblingElement( "parameter" );
332 // -------------------------------------------------------------------------
333 cpPlugins::Interface::Parameters::
341 // -------------------------------------------------------------------------
342 cpPlugins::Interface::Parameters::
347 // -------------------------------------------------------------------------
348 void cpPlugins::Interface::Parameters::
349 PrintSelf( std::ostream& os, itk::Indent indent ) const
351 TParameters::const_iterator i = this->m_Parameters.begin( );
352 for( ; i != this->m_Parameters.end( ); ++i )
355 << i->second.first << " | "
356 << i->second.second << ")"
360 // -------------------------------------------------------------------------
361 cpPlugins::Interface::Parameters::
362 TParameters& cpPlugins::Interface::Parameters::
365 return( this->m_Parameters );
368 // -------------------------------------------------------------------------
369 const cpPlugins::Interface::Parameters::
370 TParameters& cpPlugins::Interface::Parameters::
371 GetRawParameters( ) const
373 return( this->m_Parameters );