]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Parameters.cxx
First dump for version 0.1.0
[cpPlugins.git] / lib / cpPlugins / Parameters.cxx
diff --git a/lib/cpPlugins/Parameters.cxx b/lib/cpPlugins/Parameters.cxx
new file mode 100644 (file)
index 0000000..c805327
--- /dev/null
@@ -0,0 +1,348 @@
+#include <cpPlugins/Parameters.h>
+#include <tinyxml/tinyxml2.h>
+
+// -------------------------------------------------------------------------
+cpPlugins::Parameters::
+Parameters( )
+{
+  this->Clear( );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Parameters::
+~Parameters( )
+{
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Parameters::
+Modified( ) const
+{
+  this->m_TimeStamp.Modified( );
+}
+
+// -------------------------------------------------------------------------
+itk::ModifiedTimeType cpPlugins::Parameters::
+GetMTime( ) const
+{
+  return( this->m_TimeStamp.GetMTime( ) );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Parameters::
+Clear( )
+{
+  this->m_Parameters.clear( );
+  this->Modified( );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Parameters::
+GetNames( std::vector< std::string >& container ) const
+{
+  container.clear( );
+  TParameters::const_iterator i = this->m_Parameters.begin( );
+  for( ; i != this->m_Parameters.end( ); ++i )
+    container.push_back( i->first );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Parameters::
+Type cpPlugins::Parameters::
+GetType( const std::string& name ) const
+{
+  auto i = this->m_Parameters.find( name );
+  if( i != this->m_Parameters.end( ) )
+    return( i->second.first );
+  else
+    return( Self::NoType );
+}
+
+// -------------------------------------------------------------------------
+#define cpPlugins_Parameters_TypeAsString( Y )  \
+  if( i->second.first == Self::Y )              \
+    return( #Y )
+
+std::string cpPlugins::Parameters::
+GetTypeAsString( const std::string& name ) const
+{
+  auto i = this->m_Parameters.find( name );
+  cpPlugins_Parameters_TypeAsString( String );
+  else cpPlugins_Parameters_TypeAsString( Bool );
+  else cpPlugins_Parameters_TypeAsString( Int );
+  else cpPlugins_Parameters_TypeAsString( Uint );
+  else cpPlugins_Parameters_TypeAsString( Real );
+  else cpPlugins_Parameters_TypeAsString( OpenFileName );
+  else cpPlugins_Parameters_TypeAsString( SaveFileName );
+  else cpPlugins_Parameters_TypeAsString( PathName );
+  else cpPlugins_Parameters_TypeAsString( StringList );
+  else cpPlugins_Parameters_TypeAsString( BoolList );
+  else cpPlugins_Parameters_TypeAsString( IntList );
+  else cpPlugins_Parameters_TypeAsString( UintList );
+  else cpPlugins_Parameters_TypeAsString( RealList );
+  else cpPlugins_Parameters_TypeAsString( OpenFileNameList );
+  else cpPlugins_Parameters_TypeAsString( SaveFileNameList );
+  else cpPlugins_Parameters_TypeAsString( PathNameList );
+  else cpPlugins_Parameters_TypeAsString( Choices );
+  else return( "NoType" );
+}
+
+// -------------------------------------------------------------------------
+#define cpPlugins_Parameters_TypeFromString( Y, str )   \
+  if( str == std::string( #Y ) )                        \
+    return( Self::Y )
+
+cpPlugins::Parameters::
+Type cpPlugins::Parameters::
+GetTypeFromString( const std::string& t )
+{
+  cpPlugins_Parameters_TypeFromString( String, t );
+  else cpPlugins_Parameters_TypeFromString( Bool, t );
+  else cpPlugins_Parameters_TypeFromString( Int, t );
+  else cpPlugins_Parameters_TypeFromString( Uint, t );
+  else cpPlugins_Parameters_TypeFromString( Real, t );
+  else cpPlugins_Parameters_TypeFromString( OpenFileName, t );
+  else cpPlugins_Parameters_TypeFromString( SaveFileName, t );
+  else cpPlugins_Parameters_TypeFromString( PathName, t );
+  else cpPlugins_Parameters_TypeFromString( StringList, t );
+  else cpPlugins_Parameters_TypeFromString( BoolList, t );
+  else cpPlugins_Parameters_TypeFromString( IntList, t );
+  else cpPlugins_Parameters_TypeFromString( UintList, t );
+  else cpPlugins_Parameters_TypeFromString( RealList, t );
+  else cpPlugins_Parameters_TypeFromString( OpenFileNameList, t );
+  else cpPlugins_Parameters_TypeFromString( SaveFileNameList, t );
+  else cpPlugins_Parameters_TypeFromString( PathNameList, t );
+  else cpPlugins_Parameters_TypeFromString( Choices, t );
+  else return( Self::NoType );
+}
+
+// -------------------------------------------------------------------------
+std::string cpPlugins::Parameters::
+GetString( const std::string& name, bool force ) const
+{
+  auto i = this->m_Parameters.find( name );
+  if( i != this->m_Parameters.end( ) )
+  {
+    if( i->second.first == Self::String || force )
+      return( i->second.second );
+    else
+      return( "" );
+  }
+  else
+    return( "" );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Parameters::
+SetString( const std::string& name, const std::string& v, bool force )
+{
+  auto i = this->m_Parameters.find( name );
+  if( i != this->m_Parameters.end( ) )
+  {
+    if( i->second.first == Self::String || force )
+    {
+      if( i->second.second != v )
+      {
+        i->second.second = v;
+        this->Modified( );
+
+      } // fi
+
+    } // fi
+
+  } // fi
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Parameters::
+ConfigureAsChoices(
+  const std::string& name, const std::vector< std::string >& choices
+  )
+{
+  // It is invalid not to give choices when configuring
+  if( choices.size( ) == 0 )
+    return;
+
+  std::stringstream str_choices;
+  str_choices << choices[ 0 ];
+  for( unsigned int i = 1; i < choices.size( ); ++i )
+    str_choices << "#" << choices[ i ];
+  str_choices << "@";
+  this->m_Parameters[ name ] =
+    TParameter( Self::Choices, str_choices.str( ) );
+  this->Modified( );
+}
+
+// -------------------------------------------------------------------------
+std::vector< std::string > cpPlugins::Parameters::
+GetChoices( const std::string& name ) const
+{
+  std::vector< std::string > choices;
+
+  TParameters::const_iterator i = this->m_Parameters.find( name );
+  if( i != this->m_Parameters.end( ) )
+  {
+    if( i->second.first == Self::Choices )
+    {
+      std::istringstream str_choices( i->second.second );
+      std::string real_choices;
+      std::getline( str_choices, real_choices, '@' );
+      std::istringstream str( real_choices );
+      std::string token;
+      while( std::getline( str, token, '#' ) )
+        choices.push_back( token );
+
+    } // fi
+
+  } // fi
+  return( choices );
+}
+
+// -------------------------------------------------------------------------
+std::string cpPlugins::Parameters::
+GetSelectedChoice( const std::string& name ) const
+{
+  auto i = this->m_Parameters.find( name );
+  if( i != this->m_Parameters.end( ) )
+  {
+    if( i->second.first == Self::Choices )
+    {
+      std::istringstream str_choices( i->second.second );
+      std::string real_choice;
+      std::getline( str_choices, real_choice, '@' );
+      std::getline( str_choices, real_choice, '@' );
+      return( real_choice );
+    }
+    else
+      return( "" );
+  }
+  else
+    return( "" );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::Parameters::
+SetSelectedChoice( const std::string& name, const std::string& choice )
+{
+  auto i = this->m_Parameters.find( name );
+  if( i != this->m_Parameters.end( ) )
+  {
+    if( i->second.first == Self::Choices )
+    {
+      std::istringstream str_choices( i->second.second );
+      std::string choices;
+      std::getline( str_choices, choices, '@' );
+      if( choices.find( choice ) != std::string::npos )
+      {
+        std::stringstream new_choices;
+        new_choices << choices << "@" << choice;
+        i->second.second = new_choices.str( );
+        this->Modified( );
+        return( true );
+      }
+      else
+        return( false );
+    }
+    else
+      return( false );
+  }
+  else
+    return( false );
+}
+
+// -------------------------------------------------------------------------
+std::string cpPlugins::Parameters::
+GetAcceptedFileExtensions( const std::string& name ) const
+{
+  auto i = this->m_AcceptedFileExtensions.find( name );
+  if( i != this->m_AcceptedFileExtensions.end( ) )
+    return( i->second );
+  else
+    return( "" );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Parameters::
+SetAcceptedFileExtensions(
+  const std::string& name, const std::string& extensions
+  )
+{
+  auto i = this->m_Parameters.find( name );
+  if( i != this->m_Parameters.end( ) )
+  {
+    bool is_valid = ( i->second.first == Self::OpenFileName );
+    is_valid     |= ( i->second.first == Self::SaveFileName );
+    is_valid     |= ( i->second.first == Self::OpenFileNameList );
+    is_valid     |= ( i->second.first == Self::SaveFileNameList );
+    if( is_valid )
+      this->m_AcceptedFileExtensions[ name ] = extensions;
+
+  } // fi
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::Parameters::
+ToXML(
+  tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* parent_elem
+  ) const
+{
+  if( parent_elem == NULL )
+    return( false );
+
+  auto pIt = this->m_Parameters.begin( );
+  for( ; pIt != this->m_Parameters.end( ); ++pIt )
+  {
+    tinyxml2::XMLElement* p = doc->NewElement( "parameter" );
+    p->SetAttribute( "name", pIt->first.c_str( ) );
+    p->SetAttribute( "value", pIt->second.second.c_str( ) );
+    p->SetAttribute( "type", this->GetTypeAsString( pIt->first ).c_str( ) );
+    parent_elem->InsertEndChild( p );
+
+  } // rof
+  return( true );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::Parameters::
+FromXML( const tinyxml2::XMLElement* filter_elem )
+{
+  const tinyxml2::XMLElement* param =
+    filter_elem->FirstChildElement( "parameter" );
+  bool ret = false;
+  while( param != NULL )
+  {
+    const char* param_name = param->Attribute( "name" );
+    const char* param_type = param->Attribute( "type" );
+    if( param_name != NULL && param_type != NULL )
+    {
+      TParameter value;
+      value.second = param->Attribute( "value" );
+      value.first = Self::GetTypeFromString( param_type );
+      this->m_Parameters[ param_name ] = value;
+
+    } // fi
+    param = param->NextSiblingElement( "parameter" );
+    ret = true;
+
+  } // elihw
+  this->Modified( );
+  return( ret );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Parameters::
+TParameters& cpPlugins::Parameters::
+GetRawParameters( )
+{
+  return( this->m_Parameters );
+}
+
+// -------------------------------------------------------------------------
+const cpPlugins::Parameters::
+TParameters& cpPlugins::Parameters::
+GetRawParameters( ) const
+{
+  return( this->m_Parameters );
+}
+
+// eof - $RCSfile$