parametersettable.cpp
1 /***************************************************************************
2  * Copyright (C) 2008-2011 by Tomassino Ferrauto *
3  * t_ferrauto@yahoo.it *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 #include "parametersettable.h"
22 #include "factory.h"
23 #include "configurationparameters.h"
24 #include "configurationhelper.h"
25 
26 // helper function for setting the properties of a parameter or a type
27 void setProperties( farsa::ConfigurationParameters& typeDescr, QString group, farsa::ParameterSettable::Properties props ) {
28  if ( props.testFlag( farsa::ParameterSettable::IsList ) ) {
29  typeDescr.createParameter( group, "isList", "true" );
30  } else {
31  typeDescr.createParameter( group, "isList", "false" );
32  }
33  if ( props.testFlag( farsa::ParameterSettable::IsMandatory ) ) {
34  typeDescr.createParameter( group, "isMandatory", "true" );
35  } else {
36  typeDescr.createParameter( group, "isMandatory", "false" );
37  }
38  if ( props.testFlag( farsa::ParameterSettable::AllowMultiple ) ) {
39  typeDescr.createParameter( group, "allowMultiple", "true" );
40  } else {
41  typeDescr.createParameter( group, "allowMultiple", "false" );
42  }
43 };
44 
45 namespace farsa {
46 
47 const double ParameterSettable::Infinity = std::numeric_limits<double>::infinity();
48 const int ParameterSettable::MaxInteger = std::numeric_limits<int>::max();
49 const int ParameterSettable::MinInteger = std::numeric_limits<int>::min();
50 
51 QMap<QString, RuntimeParameterSetter*> ParameterSettable::runtimeParameters;
52 
54 {
55 }
56 
58 {
59  foreach( RuntimeParameterObserver* obs, observers ) {
60  obs->onObjectDestruction( this );
61  }
62 }
63 
65  if ( !observers.contains( obs ) ) {
66  observers.append( obs );
67  }
68 }
69 
71  observers.removeAll( obs );
72 }
73 
74 void ParameterSettable::notifyChangesToParam( QString paramName ) {
75  foreach( RuntimeParameterObserver* obs, observers ) {
76  obs->onParameterChanges( this, paramName );
77  }
78 }
79 
80 QString ParameterSettable::fullParameterDescriptionPath( QString type, QString param ) {
81  QString ret = type + "/";
82  QStringList groups = param.split( "/", QString::SkipEmptyParts );
83  for( int i=0; i<groups.size()-1; i++ ) {
84  ret += "Subgroups/"+groups[i]+"/";
85  }
86  ret += "Parameters/"+groups.last();
87  return ret;
88 }
89 
90 QString ParameterSettable::fullSubgroupDescriptionPath( QString type, QString sub ) {
91  QString ret = type + "/";
92  QStringList groups = sub.split( "/", QString::SkipEmptyParts );
93  foreach( QString agroup, groups ) {
94  ret += "Subgroups/"+agroup+"/";
95  }
96  return ret;
97 }
98 
99 void ParameterSettable::createParamDescription( QString paramPath, QString traitName, QString traitValue ) {
100  Factory::getTypeDescriptions().createParameter( paramPath, traitName, traitValue );
101 }
102 
103 ParameterSettable::Descriptor ParameterSettable::addTypeDescription( QString type, QString shortHelp, QString longHelp ) {
104  return Descriptor( type, shortHelp, longHelp );
105 }
106 
107 ParameterSettable::Descriptor::Descriptor( QString type, QString shortHelp, QString longHelp ) {
108  this->type = type;
110  typeDescr.createGroup( type );
111  typeDescr.createParameter( type, "shortHelp", shortHelp );
112  if ( longHelp != "" ) {
113  typeDescr.createParameter( type, "longHelp", longHelp );
114  }
115 }
116 
118  return StringDescriptor( type + "/Parameters/" + parameter );
119 }
120 
122  return IntDescriptor( type + "/Parameters/" + parameter );
123 }
124 
126  return RealDescriptor( type + "/Parameters/" + parameter );
127 }
128 
130  return BoolDescriptor( type + "/Parameters/" + parameter );
131 }
132 
134  return EnumDescriptor( type + "/Parameters/" + parameter );
135 }
136 
138  return ObjectDescriptor( type + "/Parameters/" + parameter );
139 }
140 
142  return SubgroupDescriptor( type + "/Subgroups/" + subgroup );
143 }
144 
146  this->paramPath = paramPath;
148  typeDescr.createGroup( paramPath );
149  typeDescr.createParameter( paramPath, "type", "string" );
150 }
151 
153  Factory::getTypeDescriptions().createParameter( paramPath, "default", defaultValue );
154  return (*this);
155 }
156 
158  setProperties( Factory::getTypeDescriptions(), paramPath, properties );
159  return (*this);
160 }
161 
164  typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
165  if ( longHelp != "" ) {
166  typeDescr.createParameter( paramPath, "longHelp", longHelp );
167  }
168  return (*this);
169 }
170 
172  this->paramPath = paramPath;
174  typeDescr.createGroup( paramPath );
175  typeDescr.createParameter( paramPath, "type", "int" );
176 }
177 
179  Factory::getTypeDescriptions().createParameter( paramPath, "default", QString::number(defaultValue) );
180  return (*this);
181 }
182 
184  setProperties( Factory::getTypeDescriptions(), paramPath, properties );
185  return (*this);
186 }
187 
190  if ( lower_bound != MinInteger ) {
191  typeDescr.createParameter( paramPath, "lowerBound", QString::number(lower_bound) );
192  }
193  if ( upper_bound != MaxInteger ) {
194  typeDescr.createParameter( paramPath, "upperBound", QString::number(upper_bound) );
195  }
196  return (*this);
197 }
198 
201  typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
202  if ( longHelp != "" ) {
203  typeDescr.createParameter( paramPath, "longHelp", longHelp );
204  }
205  return (*this);
206 }
207 
209  this->paramPath = paramPath;
211  typeDescr.createGroup( paramPath );
212  typeDescr.createParameter( paramPath, "type", "real" );
213 }
214 
216  Factory::getTypeDescriptions().createParameter( paramPath, "default", QString::number(defaultValue) );
217  return (*this);
218 }
219 
221  setProperties( Factory::getTypeDescriptions(), paramPath, properties );
222  return (*this);
223 }
224 
227  if ( lower_bound != -Infinity ) {
228  typeDescr.createParameter( paramPath, "lowerBound", QString::number(lower_bound) );
229  }
230  if ( upper_bound != +Infinity ) {
231  typeDescr.createParameter( paramPath, "upperBound", QString::number(upper_bound) );
232  }
233  return (*this);
234 }
235 
238  typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
239  if ( longHelp != "" ) {
240  typeDescr.createParameter( paramPath, "longHelp", longHelp );
241  }
242  return (*this);
243 }
244 
246  this->paramPath = paramPath;
248  typeDescr.createGroup( paramPath );
249  typeDescr.createParameter( paramPath, "type", "bool" );
250 }
251 
253  Factory::getTypeDescriptions().createParameter( paramPath, "default", (defaultValue ? "true" : "false") );
254  return (*this);
255 }
256 
258  setProperties( Factory::getTypeDescriptions(), paramPath, properties );
259  return (*this);
260 }
261 
264  typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
265  if ( longHelp != "" ) {
266  typeDescr.createParameter( paramPath, "longHelp", longHelp );
267  }
268  return (*this);
269 }
270 
272  this->paramPath = paramPath;
274  typeDescr.createGroup( paramPath );
275  typeDescr.createParameter( paramPath, "type", "enum" );
276 }
277 
279  Factory::getTypeDescriptions().createParameter( paramPath, "default", defaultValue );
280  return (*this);
281 }
282 
284  setProperties( Factory::getTypeDescriptions(), paramPath, properties );
285  return (*this);
286 }
287 
289  Factory::getTypeDescriptions().createParameter( paramPath, "values", allValues.join(" ") );
290  return (*this);
291 }
292 
295  typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
296  if ( longHelp != "" ) {
297  typeDescr.createParameter( paramPath, "longHelp", longHelp );
298  }
299  return (*this);
300 }
301 
303  this->paramPath = paramPath;
305  typeDescr.createGroup( paramPath );
306  typeDescr.createParameter( paramPath, "type", "object" );
307 }
308 
310  setProperties( Factory::getTypeDescriptions(), paramPath, properties );
311  return (*this);
312 }
313 
315  Factory::getTypeDescriptions().createParameter( paramPath, "class", className );
316  return (*this);
317 }
318 
321  typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
322  if ( longHelp != "" ) {
323  typeDescr.createParameter( paramPath, "longHelp", longHelp );
324  }
325  return (*this);
326 }
327 
329  this->subgroupPath = subgroupPath;
330  Factory::getTypeDescriptions().createGroup( subgroupPath );
331 }
332 
334  setProperties( Factory::getTypeDescriptions(), subgroupPath, properties );
335  return (*this);
336 }
337 
339  Factory::getTypeDescriptions().createParameter( subgroupPath, "type", "object" );
340  Factory::getTypeDescriptions().createParameter( subgroupPath, "class", className );
341  return (*this);
342 }
343 
346  typeDescr.createParameter( subgroupPath, "shortHelp", shortHelp );
347  if ( longHelp != "" ) {
348  typeDescr.createParameter( subgroupPath, "longHelp", longHelp );
349  }
350  return (*this);
351 }
352 
354  return StringDescriptor( subgroupPath + "/Parameters/" + parameter );
355 }
356 
358  return IntDescriptor( subgroupPath + "/Parameters/" + parameter );
359 }
360 
362  return RealDescriptor( subgroupPath + "/Parameters/" + parameter );
363 }
364 
366  return BoolDescriptor( subgroupPath + "/Parameters/" + parameter );
367 }
368 
370  return EnumDescriptor( subgroupPath + "/Parameters/" + parameter );
371 }
372 
374  return ObjectDescriptor( subgroupPath + "/Parameters/" + parameter );
375 }
376 
378  return SubgroupDescriptor( subgroupPath + "/Subgroups/" + subgroup );
379 }
380 
381 } // end namespace farsa
382