]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Interface/Parameters.h
...
[cpPlugins.git] / lib / cpPlugins / Interface / Parameters.h
index dfcfc793300e552a1055e82cbda7725d888f39cd..765f82f851d7b9bbb3f0ef2b3bec0c8ce70f5e1a 100644 (file)
 
 #include <map>
 #include <ostream>
+#include <sstream>
 #include <string>
 #include <vector>
 
 #include <itkObject.h>
 #include <itkObjectFactory.h>
 
+// Some forward declarations
+class TiXmlElement;
+
+// -------------------------------------------------------------------------
+#define cpPlugins_Parameters_Configure( Y )                     \
+  void ConfigureAs##Y( const std::string& name )                \
+  {                                                             \
+    this->m_Parameters[ name ] = TParameter( Self::Y, "" );     \
+    this->Modified( );                                          \
+  }                                                             \
+  bool Has##Y( const std::string& name ) const                  \
+  {                                                             \
+    auto i = this->m_Parameters.find( name );                   \
+    if( i != this->m_Parameters.end( ) )                        \
+      return( i->second.first == Self::Y );                     \
+    else                                                        \
+      return( false );                                          \
+  }
+
+// -------------------------------------------------------------------------
+#define cpPlugins_Parameters_GetSet( Y )                                \
+  T##Y Get##Y( const std::string& name ) const                          \
+  {                                                                     \
+    auto i = this->m_Parameters.find( name );                           \
+    if( i != this->m_Parameters.end( ) )                                \
+    {                                                                   \
+      if( i->second.first == Self::Y )                                  \
+      {                                                                 \
+        if( typeid( T##Y ) != typeid( std::string ) )                   \
+        {                                                               \
+          std::istringstream tok_str( i->second.second );               \
+          T##Y v;                                                       \
+          tok_str >> v;                                                 \
+          return( v );                                                  \
+        }                                                               \
+        else                                                            \
+        {                                                               \
+          const T##Y* ptr =                                             \
+            reinterpret_cast< const T##Y* >(                            \
+              &( i->second.second )                                     \
+              );                                                        \
+          return( *ptr );                                               \
+        }                                                               \
+      }                                                                 \
+    }                                                                   \
+    return( T##Y( 0 ) );                                                \
+  }                                                                     \
+  void Set##Y( const std::string& name, const T##Y& v )                 \
+  {                                                                     \
+    auto i = this->m_Parameters.find( name );                           \
+    if( i != this->m_Parameters.end( ) )                                \
+    {                                                                   \
+      if( i->second.first == Self::Y )                                  \
+      {                                                                 \
+        if( typeid( T##Y ) != typeid( std::string ) )                   \
+        {                                                               \
+          std::stringstream str;                                        \
+          str << v;                                                     \
+          i->second.second = str.str( );                                \
+        }                                                               \
+        else                                                            \
+          i->second.second =                                            \
+            *( reinterpret_cast< const std::string* >( &v ) );          \
+        this->Modified( );                                              \
+      }                                                                 \
+    }                                                                   \
+  }
+
+// -------------------------------------------------------------------------
+#define cpPlugins_Parameters_GetSetList( Y )                            \
+  std::vector< T##Y > Get##Y##List( const std::string& name ) const     \
+  {                                                                     \
+    std::vector< T##Y > lst;                                            \
+    std::vector< std::string >* slst =                                  \
+      reinterpret_cast< std::vector< std::string >* >( &lst );          \
+    auto i = this->m_Parameters.find( name );                           \
+    if( i != this->m_Parameters.end( ) )                                \
+    {                                                                   \
+      if( i->second.first == Self::Y##List )                            \
+      {                                                                 \
+        std::istringstream str( i->second.second );                     \
+        std::string token;                                              \
+        while( std::getline( str, token, '#' ) )                        \
+        {                                                               \
+          if( typeid( T##Y ) != typeid( std::string ) )                 \
+          {                                                             \
+            std::istringstream tok_str( token );                        \
+            T##Y v;                                                     \
+            tok_str >> v;                                               \
+            lst.push_back( v );                                         \
+          }                                                             \
+          else                                                          \
+            slst->push_back( token );                                   \
+        }                                                               \
+      }                                                                 \
+    }                                                                   \
+    return( lst );                                                      \
+  }                                                                     \
+  void AddTo##Y##List( const std::string& name, const T##Y& v )         \
+  {                                                                     \
+    auto i = this->m_Parameters.find( name );                           \
+    if( i != this->m_Parameters.end( ) )                                \
+    {                                                                   \
+      if( i->second.first == Self::Y##List )                            \
+      {                                                                 \
+        std::stringstream str;                                          \
+        str << i->second.second << "#" << v;                            \
+        i->second.second = str.str( );                                  \
+      }                                                                 \
+    }                                                                   \
+  }                                                                     \
+  void Clear##Y##List( const std::string& name )                        \
+  {                                                                     \
+    auto i = this->m_Parameters.find( name );                           \
+    if( i != this->m_Parameters.end( ) )                                \
+    {                                                                   \
+      if( i->second.first == Self::Y##List )                            \
+        i->second.second = "";                                          \
+    }                                                                   \
+  }
+
 namespace cpPlugins
 {
   namespace Interface
   {
     // Some forward declarations
     class ProcessObject;
+    class ParametersQtDialog;
 
     /**
      */
     class cpPlugins_Interface_EXPORT Parameters
       : public itk::Object
     {
+      friend class ParametersQtDialog;
+
     public:
       typedef Parameters                      Self;
       typedef itk::Object                     Superclass;
@@ -31,12 +156,14 @@ namespace cpPlugins
 
       enum Type
       {
-        String     , Bool       , Int        ,
-        Uint       , Real       , Index      ,
-        Point      , Vector     , StringList ,
-        BoolList   , IntList    , UintList   ,
-        RealList   , IndexList  , PointList  ,
-        VectorList , Choices    , NoType
+        String       , Bool             , Int              ,
+        Uint         , Real             , Index            ,
+        Point        , Vector           , OpenFileName     ,
+        SaveFileName , PathName         , StringList       ,
+        BoolList     , IntList          , UintList         ,
+        RealList     , IndexList        , PointList        ,
+        VectorList   , OpenFileNameList , SaveFileNameList ,
+        PathNameList , Choices          , NoType
       };
 
       typedef bool          TBool;
@@ -44,14 +171,58 @@ namespace cpPlugins
       typedef unsigned long TUint;
       typedef double        TReal;
       typedef std::string   TString;
+      typedef std::string   TOpenFileName;
+      typedef std::string   TSaveFileName;
+      typedef std::string   TPathName;
 
-      typedef std::pair< Self::Type, TString > TParameter;
-      typedef std::map< TString, TParameter >  TParameters;
+      typedef std::pair< Self::Type, std::string > TParameter;
+      typedef std::map< std::string, TParameter >  TParameters;
 
     public:
       itkNewMacro( Self );
       itkTypeMacro( cpPlugins::Interface::Parameters, itk::Object );
 
+      cpPlugins_Parameters_Configure( String );
+      cpPlugins_Parameters_Configure( Bool );
+      cpPlugins_Parameters_Configure( Int );
+      cpPlugins_Parameters_Configure( Uint );
+      cpPlugins_Parameters_Configure( Real );
+      cpPlugins_Parameters_Configure( Index );
+      cpPlugins_Parameters_Configure( Point );
+      cpPlugins_Parameters_Configure( Vector );
+      cpPlugins_Parameters_Configure( OpenFileName );
+      cpPlugins_Parameters_Configure( SaveFileName );
+      cpPlugins_Parameters_Configure( PathName );
+      cpPlugins_Parameters_Configure( StringList );
+      cpPlugins_Parameters_Configure( BoolList );
+      cpPlugins_Parameters_Configure( IntList );
+      cpPlugins_Parameters_Configure( UintList );
+      cpPlugins_Parameters_Configure( RealList );
+      cpPlugins_Parameters_Configure( IndexList );
+      cpPlugins_Parameters_Configure( PointList );
+      cpPlugins_Parameters_Configure( VectorList );
+      cpPlugins_Parameters_Configure( OpenFileNameList );
+      cpPlugins_Parameters_Configure( SaveFileNameList );
+      cpPlugins_Parameters_Configure( PathNameList );
+      cpPlugins_Parameters_Configure( Choices );
+
+      cpPlugins_Parameters_GetSet( Bool );
+      cpPlugins_Parameters_GetSet( Int );
+      cpPlugins_Parameters_GetSet( Uint );
+      cpPlugins_Parameters_GetSet( Real );
+      cpPlugins_Parameters_GetSet( OpenFileName );
+      cpPlugins_Parameters_GetSet( SaveFileName );
+      cpPlugins_Parameters_GetSet( PathName );
+
+      cpPlugins_Parameters_GetSetList( String );
+      cpPlugins_Parameters_GetSetList( Bool );
+      cpPlugins_Parameters_GetSetList( Int );
+      cpPlugins_Parameters_GetSetList( Uint );
+      cpPlugins_Parameters_GetSetList( Real );
+      cpPlugins_Parameters_GetSetList( OpenFileName );
+      cpPlugins_Parameters_GetSetList( SaveFileName );
+      cpPlugins_Parameters_GetSetList( PathName );
+
     public:
       // To impact pipeline
       virtual ProcessObject* GetProcessObject( );
@@ -62,153 +233,98 @@ namespace cpPlugins
       // Parameters container configuration
       void Clear( );
 
-      void ConfigureAsString( const TString& name );
-      void ConfigureAsBool( const TString& name );
-      void ConfigureAsInt( const TString& name );
-      void ConfigureAsUint( const TString& name );
-      void ConfigureAsReal( const TString& name );
-      void ConfigureAsIndex( const TString& name );
-      void ConfigureAsPoint( const TString& name );
-      void ConfigureAsVector( const TString& name );
-      void ConfigureAsStringList( const TString& name );
-      void ConfigureAsBoolList( const TString& name );
-      void ConfigureAsIntList( const TString& name );
-      void ConfigureAsUintList( const TString& name );
-      void ConfigureAsRealList( const TString& name );
-      void ConfigureAsIndexList( const TString& name );
-      void ConfigureAsPointList( const TString& name );
-      void ConfigureAsVectorList( const TString& name );
-      void ConfigureAsChoices( const TString& name );
-      void ConfigureAsChoices(
-        const TString& name, const std::vector< TString >& choices
-        );
-
       // Get methods
-      void GetNames( std::vector< TString >& container ) const;
-      Type GetType( const TString& name ) const;
-
-      bool HasString( const TString& name ) const;
-      bool HasBool( const TString& name ) const;
-      bool HasInt( const TString& name ) const;
-      bool HasUint( const TString& name ) const;
-      bool HasReal( const TString& name ) const;
-      bool HasIndex( const TString& name ) const;
-      bool HasPoint( const TString& name ) const;
-      bool HasVector( const TString& name ) const;
-      bool HasStringList( const TString& name ) const;
-      bool HasBoolList( const TString& name ) const;
-      bool HasIntList( const TString& name ) const;
-      bool HasUintList( const TString& name ) const;
-      bool HasRealList( const TString& name ) const;
-      bool HasIndexList( const TString& name ) const;
-      bool HasPointList( const TString& name ) const;
-      bool HasVectorList( const TString& name ) const;
-      bool HasChoices( const TString& name ) const;
-
-      TString GetString( const TString& name, bool force = true ) const;
-      TBool GetBool( const TString& name ) const;
-      TInt GetInt( const TString& name ) const;
-      TUint GetUint( const TString& name ) const;
-      TReal GetReal( const TString& name ) const;
-
-      void GetStringList(
-        std::vector< TString >& lst, const TString& name
-        ) const;
-      void GetBoolList(
-        std::vector< TBool >& lst, const TString& name
-        ) const;
-      void GetIntList(
-        std::vector< TInt >& lst, const TString& name
+      void GetNames( std::vector< std::string >& container ) const;
+      Type GetType( const std::string& name ) const;
+      std::string GetTypeAsString( const std::string& name ) const;
+      static Type GetTypeFromString( const std::string& t );
+
+      // Base string methods
+      std::string GetString(
+        const std::string& name, bool force = true
         ) const;
-      void GetUintList(
-        std::vector< TUint >& lst, const TString& name
-        ) const;
-      void GetRealList(
-        std::vector< TReal >& lst, const TString& name
-        ) const;
-
-      void GetChoices(
-        std::vector< TString >& choices, const TString& name
-        ) const;
-      TString GetSelectedChoice( const TString& name ) const;
+      void SetString(
+        const std::string& name, const std::string& v, bool force = true
+        );
 
-      template< class I >
-        inline I GetIndex( const TString& name, const TUint& dim ) const;
-      template< class P >
-        inline P GetPoint( const TString& name, const TUint& dim ) const;
-      template< class V >
-        inline V GetVector( const TString& name, const TUint& dim ) const;
+      void ConfigureAsChoices(
+        const std::string& name, const std::vector< std::string >& choices
+        );
+      std::vector< std::string > GetChoices( const std::string& name ) const;
+      std::string GetSelectedChoice( const std::string& name ) const;
+      bool SetSelectedChoice(
+        const std::string& name, const std::string& choice
+        );
 
+      // Some templated methods
       template< class I >
-        inline void GetIndexList(
-          std::vector< I >& lst, const TString& name, const TUint& dim
+        I GetIndex(
+          const std::string& name, const unsigned int& dim
           ) const;
       template< class P >
-        inline void GetPointList(
-          std::vector< P >& lst, const TString& name, const TUint& dim
+        P GetPoint(
+          const std::string& name, const unsigned int& dim
           ) const;
       template< class V >
-        inline void GetVectorList(
-          std::vector< V >& lst, const TString& name, const TUint& dim
+        V GetVector(
+          const std::string& name, const unsigned int& dim
           ) const;
 
-      // Set methods
-      void SetString(
-        const TString& name, const TString& v, bool force = true
-        );
-      void SetBool( const TString& name, const TBool& v );
-      void SetInt( const TString& name, const TInt& v );
-      void SetUint( const TString& name, const TUint& v );
-      void SetReal( const TString& name, const TReal& v );
-
       template< class I >
-        inline void SetIndex(
-          const TString& name, const TUint& dim, const I& v
+        void SetIndex(
+          const std::string& name, const unsigned int& dim, const I& v
           );
       template< class P >
-        inline void SetPoint(
-          const TString& name, const TUint& dim, const P& v
+        void SetPoint(
+          const std::string& name, const unsigned int& dim, const P& v
           );
       template< class V >
-        inline void SetVector(
-          const TString& name, const TUint& dim, const V& v
+        void SetVector(
+          const std::string& name, const unsigned int& dim, const V& v
           );
 
-      void AddToStringList( const TString& name, const TString& v );
-      void AddToBoolList( const TString& name, const TBool& v );
-      void AddToIntList( const TString& name, const TInt& v );
-      void AddToUintList( const TString& name, const TUint& v );
-      void AddToRealList( const TString& name, const TReal& v );
+      template< class I >
+        std::vector< I > GetIndexList(
+          const std::string& name, const unsigned int& dim
+          ) const;
+      template< class P >
+        std::vector< P > GetPointList(
+          const std::string& name, const unsigned int& dim
+          ) const;
+      template< class V >
+        std::vector< V > GetVectorList(
+          const std::string& name, const unsigned int& dim
+          ) const;
 
       template< class I >
-        inline void AddToIndexList(
-          const TString& name, const TUint& dim, const I& v
+        void AddToIndexList(
+          const std::string& name, const unsigned int& dim, const I& v
           );
       template< class P >
-        inline void AddToPointList(
-          const TString& name, const TUint& dim, const P& v
+        void AddToPointList(
+          const std::string& name, const unsigned int& dim, const P& v
           );
       template< class P >
-        inline void AddToVectorList(
-          const TString& name, const TUint& dim, const P& v
+        void AddToVectorList(
+          const std::string& name, const unsigned int& dim, const P& v
           );
 
-      void ClearStringList( const TString& name );
-      void ClearBoolList( const TString& name );
-      void ClearIntList( const TString& name );
-      void ClearUintList( const TString& name );
-      void ClearRealList( const TString& name );
-      void ClearIndexList( const TString& name );
-      void ClearPointList( const TString& name );
-      void ClearVectorList( const TString& name );
+      void ClearIndexList( const std::string& name );
+      void ClearPointList( const std::string& name );
+      void ClearVectorList( const std::string& name );
 
-      bool SetSelectedChoice( const TString& name, const TString& choice );
+      // XML "streaming"
+      bool ToXML( TiXmlElement* parent_elem ) const;
+      bool FromXML( const TiXmlElement* filter_elem );
 
     protected:
       Parameters( );
       virtual ~Parameters( );
       void PrintSelf( std::ostream& os, itk::Indent indent ) const;
 
+      TParameters& GetRawParameters( );
+      const TParameters& GetRawParameters( ) const;
+
     private:
       // Purposely not implemented
       Parameters( const Self& other );