]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Parameters.hxx
Widget integration (step 5/6): Interactive plugins now supported, widgets updates...
[cpPlugins.git] / lib / cpPlugins / Interface / Parameters.hxx
1 #ifndef __CPPLUGINS__INTERFACE__PARAMETERS__HXX__
2 #define __CPPLUGINS__INTERFACE__PARAMETERS__HXX__
3
4 // -------------------------------------------------------------------------
5 template< class I >
6 void cpPlugins::Interface::Parameters::
7 ConfigureAsIndex( const TString& name, const TUint& dim, const I& v )
8 {
9   std::stringstream str;
10   str << v[ 0 ];
11   for( unsigned int d = 1; d < dim; ++d )
12     str << ";" << v[ d ];
13   std::string s = str.str( );
14   this->m_Parameters[ name ] = TParameter( Self::Index, TValues( s, s ) );
15   this->Modified( );
16 }
17
18 // -------------------------------------------------------------------------
19 template< class P >
20 void cpPlugins::Interface::Parameters::
21 ConfigureAsPoint( const TString& name, const TUint& dim, const P& v )
22 {
23   std::stringstream str;
24   str << v[ 0 ];
25   for( unsigned int d = 1; d < dim; ++d )
26     str << ";" << v[ d ];
27   std::string s = str.str( );
28   this->m_Parameters[ name ] = TParameter( Self::Point, TValues( s, s ) );
29   this->Modified( );
30 }
31
32 // -------------------------------------------------------------------------
33 template< class I >
34 I cpPlugins::Interface::Parameters::
35 GetIndex( const TString& name, const TUint& dim ) const
36 {
37   I v;
38   TParameters::const_iterator i = this->m_Parameters.find( name );
39   if( i != this->m_Parameters.end( ) )
40   {
41     if( i->second.first == Self::Index )
42     {
43       std::istringstream str( i->second.second.second );
44       std::string token;
45       unsigned int d = 0;
46       while( std::getline( str, token, ';' ) && d < dim )
47       {
48         v[ d ] = std::atoi( token.c_str( ) );
49         d++;
50
51       } // elihw
52       return( v );
53
54     } // fi
55
56   } // fi
57
58   // If parameter not found
59   for( unsigned int d = 0; d < dim; ++d )
60     v[ d ] = 0;
61   return( v );
62 }
63
64 // -------------------------------------------------------------------------
65 template< class P >
66 P cpPlugins::Interface::Parameters::
67 GetPoint( const TString& name, const TUint& dim ) const
68 {
69   P v;
70   TParameters::const_iterator i = this->m_Parameters.find( name );
71   if( i != this->m_Parameters.end( ) )
72   {
73     if( i->second.first == Self::Point )
74     {
75       std::istringstream str( i->second.second.second );
76       std::string token;
77       unsigned int d = 0;
78       while( std::getline( str, token, ';' ) && d < dim )
79       {
80         v[ d ] = std::atof( token.c_str( ) );
81         d++;
82
83       } // elihw
84       return( v );
85
86     } // fi
87
88   } // fi
89
90   // If parameter not found
91   for( unsigned int d = 0; d < dim; ++d )
92     v[ d ] = float( 0 );
93   return( v );
94 }
95
96 // -------------------------------------------------------------------------
97 template< class I >
98 void cpPlugins::Interface::Parameters::
99 GetIndexList(
100   std::vector< I >& lst, const TString& name, const TUint& dim
101   ) const
102 {
103   lst.clear( );
104
105   TParameters::const_iterator i = this->m_Parameters.find( name );
106   if( i == this->m_Parameters.end( ) )
107     return;
108   if( i->second.first == Self::IndexList )
109     return;
110
111   std::istringstream str( i->second.second.second );
112   std::string token;
113   unsigned int d = 0;
114   while( std::getline( str, token, '#' ) )
115   {
116     std::istringstream str2( token );
117     std::string token2;
118     unsigned int d = 0;
119     I v;
120     while( std::getline( str2, token2, ';' ) && d < dim )
121     {
122       v[ d ] = std::atoi( token.c_str( ) );
123       d++;
124
125     } // elihw
126     lst.push_back( v );
127
128   } // elihw
129 }
130
131 // -------------------------------------------------------------------------
132 template< class P >
133 void cpPlugins::Interface::Parameters::
134 GetPointList(
135   std::vector< P >& lst, const TString& name, const TUint& dim
136   ) const
137 {
138   lst.clear( );
139
140   TParameters::const_iterator i = this->m_Parameters.find( name );
141   if( i == this->m_Parameters.end( ) )
142     return;
143   if( i->second.first == Self::PointList )
144     return;
145
146   std::istringstream str( i->second.second.second );
147   std::string token;
148   unsigned int d = 0;
149   while( std::getline( str, token, '#' ) )
150   {
151     std::istringstream str2( token );
152     std::string token2;
153     unsigned int d = 0;
154     P v;
155     while( std::getline( str2, token2, ';' ) && d < dim )
156     {
157       v[ d ] = std::atof( token.c_str( ) );
158       d++;
159
160     } // elihw
161     lst.push_back( v );
162
163   } // elihw
164 }
165
166 // -------------------------------------------------------------------------
167 template< class I >
168 void cpPlugins::Interface::Parameters::
169 SetIndex( const TString& name, const TUint& dim, const I& v )
170 {
171   TParameters::iterator i = this->m_Parameters.find( name );
172   if( i == this->m_Parameters.end( ) )
173     return;
174   if( i->second.first != Self::Index )
175     return;
176
177   std::stringstream str;
178   str << v[ 0 ];
179   for( unsigned int d = 1; d < dim; ++d )
180     str << ";" << v[ d ];
181   i->second.second.second = str.str( );
182   this->Modified( );
183 }
184
185 // -------------------------------------------------------------------------
186 template< class P >
187 void cpPlugins::Interface::Parameters::
188 SetPoint( const TString& name, const TUint& dim, const P& v )
189 {
190   TParameters::iterator i = this->m_Parameters.find( name );
191   if( i == this->m_Parameters.end( ) )
192     return;
193   if( i->second.first != Self::Point )
194     return;
195
196   std::stringstream str;
197   str << v[ 0 ];
198   for( unsigned int d = 1; d < dim; ++d )
199     str << ";" << v[ d ];
200   i->second.second.second = str.str( );
201   this->Modified( );
202 }
203
204 // -------------------------------------------------------------------------
205 template< class I >
206 void cpPlugins::Interface::Parameters::
207 AddToIndexList( const TString& name, const TUint& dim, const I& v )
208 {
209   TParameters::iterator i = this->m_Parameters.find( name );
210   if( i == this->m_Parameters.end( ) )
211     return;
212   if( i->second.first != Self::IndexList )
213     return;
214
215   std::stringstream str;
216   if( i->second.second.second == "" )
217     str << v[ 0 ];
218   else
219     str << "#" << v[ 0 ];
220   for( unsigned int d = 1; d < dim; ++d )
221     str << ";" << v[ d ];
222   i->second.second.second += str.str( );
223   this->Modified( );
224 }
225
226 // -------------------------------------------------------------------------
227 template< class P >
228 void cpPlugins::Interface::Parameters::
229 AddToPointList( const TString& name, const TUint& dim, const P& v )
230 {
231   TParameters::iterator i = this->m_Parameters.find( name );
232   if( i == this->m_Parameters.end( ) )
233     return;
234   if( i->second.first != Self::PointList )
235     return;
236
237   std::stringstream str;
238   if( i->second.second.second == "" )
239     str << v[ 0 ];
240   else
241     str << "#" << v[ 0 ];
242   for( unsigned int d = 1; d < dim; ++d )
243     str << ";" << v[ d ];
244   i->second.second.second += str.str( );
245   this->Modified( );
246 }
247
248 #endif // __CPPLUGINS__INTERFACE__PARAMETERS__HXX__
249
250 // eof - $RCSfile$