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  if ( !observers.contains( obs ) ) {
55  observers.append( obs );
56  }
57 }
58 
60  observers.removeAll( obs );
61 }
62 
63 QString ParameterSettable::fullParameterDescriptionPath( QString type, QString param ) {
64  QString ret = type + "/";
65  QStringList groups = param.split( "/", QString::SkipEmptyParts );
66  for( int i=0; i<groups.size()-1; i++ ) {
67  ret += "Subgroups/"+groups[i]+"/";
68  }
69  ret += "Parameters/"+groups.last();
70  return ret;
71 }
72 
73 QString ParameterSettable::fullSubgroupDescriptionPath( QString type, QString sub ) {
74  QString ret = type + "/";
75  QStringList groups = sub.split( "/", QString::SkipEmptyParts );
76  foreach( QString agroup, groups ) {
77  ret += "Subgroups/"+agroup+"/";
78  }
79  return ret;
80 }
81 
82 void ParameterSettable::createParamDescription( QString paramPath, QString traitName, QString traitValue ) {
83  Factory::getTypeDescriptions().createParameter( paramPath, traitName, traitValue );
84 }
85 
86 ParameterSettable::Descriptor ParameterSettable::addTypeDescription( QString type, QString shortHelp, QString longHelp ) {
87  return Descriptor( type, shortHelp, longHelp );
88 }
89 
90 ParameterSettable::Descriptor::Descriptor( QString type, QString shortHelp, QString longHelp ) {
91  this->type = type;
93  typeDescr.createGroup( type );
94  typeDescr.createParameter( type, "shortHelp", shortHelp );
95  if ( longHelp != "" ) {
96  typeDescr.createParameter( type, "longHelp", longHelp );
97  }
98 }
99 
101  return StringDescriptor( type + "/Parameters/" + parameter );
102 }
103 
105  return IntDescriptor( type + "/Parameters/" + parameter );
106 }
107 
109  return RealDescriptor( type + "/Parameters/" + parameter );
110 }
111 
113  return BoolDescriptor( type + "/Parameters/" + parameter );
114 }
115 
117  return EnumDescriptor( type + "/Parameters/" + parameter );
118 }
119 
121  return ObjectDescriptor( type + "/Parameters/" + parameter );
122 }
123 
125  return SubgroupDescriptor( type + "/Subgroups/" + subgroup );
126 }
127 
129  this->paramPath = paramPath;
131  typeDescr.createGroup( paramPath );
132  typeDescr.createParameter( paramPath, "type", "string" );
133 }
134 
136  Factory::getTypeDescriptions().createParameter( paramPath, "default", defaultValue );
137  return (*this);
138 }
139 
141  setProperties( Factory::getTypeDescriptions(), paramPath, properties );
142  return (*this);
143 }
144 
147  typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
148  if ( longHelp != "" ) {
149  typeDescr.createParameter( paramPath, "longHelp", longHelp );
150  }
151  return (*this);
152 }
153 
155  this->paramPath = paramPath;
157  typeDescr.createGroup( paramPath );
158  typeDescr.createParameter( paramPath, "type", "int" );
159 }
160 
162  Factory::getTypeDescriptions().createParameter( paramPath, "default", QString::number(defaultValue) );
163  return (*this);
164 }
165 
167  setProperties( Factory::getTypeDescriptions(), paramPath, properties );
168  return (*this);
169 }
170 
173  if ( lower_bound != MinInteger ) {
174  typeDescr.createParameter( paramPath, "lowerBound", QString::number(lower_bound) );
175  }
176  if ( upper_bound != MaxInteger ) {
177  typeDescr.createParameter( paramPath, "upperBound", QString::number(upper_bound) );
178  }
179  return (*this);
180 }
181 
184  typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
185  if ( longHelp != "" ) {
186  typeDescr.createParameter( paramPath, "longHelp", longHelp );
187  }
188  return (*this);
189 }
190 
192  this->paramPath = paramPath;
194  typeDescr.createGroup( paramPath );
195  typeDescr.createParameter( paramPath, "type", "real" );
196 }
197 
199  Factory::getTypeDescriptions().createParameter( paramPath, "default", QString::number(defaultValue) );
200  return (*this);
201 }
202 
204  setProperties( Factory::getTypeDescriptions(), paramPath, properties );
205  return (*this);
206 }
207 
210  if ( lower_bound != -Infinity ) {
211  typeDescr.createParameter( paramPath, "lowerBound", QString::number(lower_bound) );
212  }
213  if ( upper_bound != +Infinity ) {
214  typeDescr.createParameter( paramPath, "upperBound", QString::number(upper_bound) );
215  }
216  return (*this);
217 }
218 
221  typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
222  if ( longHelp != "" ) {
223  typeDescr.createParameter( paramPath, "longHelp", longHelp );
224  }
225  return (*this);
226 }
227 
229  this->paramPath = paramPath;
231  typeDescr.createGroup( paramPath );
232  typeDescr.createParameter( paramPath, "type", "bool" );
233 }
234 
236  Factory::getTypeDescriptions().createParameter( paramPath, "default", (defaultValue ? "true" : "false") );
237  return (*this);
238 }
239 
241  setProperties( Factory::getTypeDescriptions(), paramPath, properties );
242  return (*this);
243 }
244 
247  typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
248  if ( longHelp != "" ) {
249  typeDescr.createParameter( paramPath, "longHelp", longHelp );
250  }
251  return (*this);
252 }
253 
255  this->paramPath = paramPath;
257  typeDescr.createGroup( paramPath );
258  typeDescr.createParameter( paramPath, "type", "enum" );
259 }
260 
262  Factory::getTypeDescriptions().createParameter( paramPath, "default", defaultValue );
263  return (*this);
264 }
265 
267  setProperties( Factory::getTypeDescriptions(), paramPath, properties );
268  return (*this);
269 }
270 
272  Factory::getTypeDescriptions().createParameter( paramPath, "values", allValues.join(" ") );
273  return (*this);
274 }
275 
278  typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
279  if ( longHelp != "" ) {
280  typeDescr.createParameter( paramPath, "longHelp", longHelp );
281  }
282  return (*this);
283 }
284 
286  this->paramPath = paramPath;
288  typeDescr.createGroup( paramPath );
289  typeDescr.createParameter( paramPath, "type", "object" );
290 }
291 
293  setProperties( Factory::getTypeDescriptions(), paramPath, properties );
294  return (*this);
295 }
296 
298  Factory::getTypeDescriptions().createParameter( paramPath, "class", className );
299  return (*this);
300 }
301 
304  typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
305  if ( longHelp != "" ) {
306  typeDescr.createParameter( paramPath, "longHelp", longHelp );
307  }
308  return (*this);
309 }
310 
312  this->subgroupPath = subgroupPath;
313  Factory::getTypeDescriptions().createGroup( subgroupPath );
314 }
315 
317  setProperties( Factory::getTypeDescriptions(), subgroupPath, properties );
318  return (*this);
319 }
320 
322  Factory::getTypeDescriptions().createParameter( subgroupPath, "type", "object" );
323  Factory::getTypeDescriptions().createParameter( subgroupPath, "class", className );
324  return (*this);
325 }
326 
329  typeDescr.createParameter( subgroupPath, "shortHelp", shortHelp );
330  if ( longHelp != "" ) {
331  typeDescr.createParameter( subgroupPath, "longHelp", longHelp );
332  }
333  return (*this);
334 }
335 
337  return StringDescriptor( subgroupPath + "/Parameters/" + parameter );
338 }
339 
341  return IntDescriptor( subgroupPath + "/Parameters/" + parameter );
342 }
343 
345  return RealDescriptor( subgroupPath + "/Parameters/" + parameter );
346 }
347 
349  return BoolDescriptor( subgroupPath + "/Parameters/" + parameter );
350 }
351 
353  return EnumDescriptor( subgroupPath + "/Parameters/" + parameter );
354 }
355 
357  return ObjectDescriptor( subgroupPath + "/Parameters/" + parameter );
358 }
359 
361  return SubgroupDescriptor( subgroupPath + "/Subgroups/" + subgroup );
362 }
363 
364 } // end namespace farsa
365