Orfeo Toolbox  3.16
otbWrapperParameterGroup.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
37 #include "otbWrapperParameterKey.h"
38 #include "otbWrapperRAMParameter.h"
39 
40 #include <boost/algorithm/string.hpp>
41 
42 namespace otb
43 {
44 namespace Wrapper
45 {
46 
48 {
49 }
50 
52 {
53 }
54 
55 std::vector<std::string>
57 {
58  std::vector<std::string> parameters;
59 
60  ParameterListType::iterator pit;
61  for (pit = m_ParameterList.begin(); pit != m_ParameterList.end(); ++pit)
62  {
63  Parameter* param = *pit;
64  parameters.push_back( param->GetKey() );
65 
66  if (recursive && dynamic_cast<ParameterGroup*>(param))
67  {
68  ParameterGroup* paramAsGroup = dynamic_cast<ParameterGroup*>(param);
69  std::vector<std::string> subparams = paramAsGroup->GetParametersKeys();
70  for (std::vector<std::string>::const_iterator it = subparams.begin();
71  it != subparams.end(); ++it)
72  {
73  parameters.push_back( std::string(paramAsGroup->GetKey()) + "." + *it );
74  }
75  }
76  else if (recursive && dynamic_cast<ChoiceParameter*>(param))
77  {
78  ChoiceParameter* paramAsChoice = dynamic_cast<ChoiceParameter*>(param);
79 
80  std::vector<std::string> subparams = paramAsChoice->GetParametersKeys();
81  for (std::vector<std::string>::const_iterator it = subparams.begin();
82  it != subparams.end(); ++it)
83  {
84  parameters.push_back( std::string(paramAsChoice->GetKey()) + "." + *it );
85  }
86  }
87  }
88  return parameters;
89 }
90 
91 
93 void
94 ParameterGroup::AddChoice(std::string paramKey, std::string paramName)
95 {
96  ParameterKey pKey( paramKey );
97  // Split the parameter name
98  std::vector<std::string> splittedKey = pKey.Split();
99 
100  if( splittedKey.size() > 1 )
101  {
102  // Get the last subkey
103  std::string lastkey = pKey.GetLastElement();
104 
105  std::string parentkey = pKey.GetRoot();
106  Parameter::Pointer parentParam = GetParameterByKey(parentkey);
107 
108  // parentParam must be a choice, a listBox or this is an error
109  ChoiceParameter* comboboxParentAsChoice = dynamic_cast<ChoiceParameter*>(parentParam.GetPointer());
110  ListViewParameter* listBoxParentAsChoice = dynamic_cast<ListViewParameter*>(parentParam.GetPointer());
111 
112  if (comboboxParentAsChoice)
113  {
114  comboboxParentAsChoice->AddChoice(lastkey, paramName);
115  }
116  else if (listBoxParentAsChoice)
117  {
118  listBoxParentAsChoice->AddChoice(lastkey, paramName);
119  }
120  else
121  {
122  itkExceptionMacro(<<parentkey << " is not a choice");
123  }
124  }
125  else
126  {
127  itkExceptionMacro(<<"No choice parameter key given");
128  }
129 }
130 
132 void
133 ParameterGroup::ClearChoices(std::string paramKey)
134 {
135  ParameterKey pKey( paramKey );
136  // Split the parameter name
137  std::vector<std::string> splittedKey = pKey.Split();
138 
139  std::string parentkey;
140  Parameter::Pointer parentParam;
141 
142  if (splittedKey.size() > 1)
143  {
144  parentkey = pKey.GetRoot();
145  parentParam = GetParameterByKey(parentkey);
146  }
147  else
148  {
149  parentParam = GetParameterByKey(splittedKey[0]);
150  }
151 
152  // parentParam must be a choice, a listBox or this is an error
153  ListViewParameter* listBoxParentAsChoice = dynamic_cast<ListViewParameter*>(parentParam.GetPointer());
154 
155  if (listBoxParentAsChoice)
156  {
157  listBoxParentAsChoice->ClearChoices();
158  }
159  else
160  {
161  itkExceptionMacro(<<parentkey << " is not a ListView");
162  }
163 }
164 
166 std::vector<int>
167 ParameterGroup::GetSelectedItems(std::string paramKey)
168 {
169  std::vector<int> selectedItems;
170  ParameterKey pKey( paramKey );
171  // Split the parameter name
172  std::vector<std::string> splittedKey = pKey.Split();
173 
174  std::string parentkey;
175  Parameter::Pointer parentParam;
176 
177  if (splittedKey.size() > 1)
178  {
179  parentkey = pKey.GetRoot();
180  parentParam = GetParameterByKey(parentkey);
181  }
182  else
183  {
184  parentParam = GetParameterByKey(splittedKey[0]);
185  }
186 
187  // parentParam must be a choice, a listBox or this is an error
188  ListViewParameter* listBoxParentAsChoice = dynamic_cast<ListViewParameter*>(parentParam.GetPointer());
189 
190  if (listBoxParentAsChoice)
191  {
192  selectedItems = listBoxParentAsChoice->GetSelectedItems();
193  }
194  else
195  {
196  itkExceptionMacro(<<parentkey << " is not a ListView");
197  }
198 
199  return selectedItems;
200 }
201 
202 
204 void
205 ParameterGroup::AddParameter(ParameterType type, std::string paramKey, std::string paramName)
206 {
207  ParameterKey pKey(paramKey);
208  // Split the parameter name
209  std::vector<std::string> splittedKey = pKey.Split();
210 
211  // Get the last subkey
212  std::string lastkey = pKey.GetLastElement();
213 
214  std::string parentkey;
215  Parameter::Pointer parentParam;
216 
217  if (splittedKey.size() > 1)
218  {
219  parentkey = pKey.GetRoot();
220  parentParam = GetParameterByKey(parentkey);
221  }
222  else
223  {
224  parentParam = this;
225  }
226 
227  ParameterGroup* parentAsGroup = dynamic_cast<ParameterGroup*> (parentParam.GetPointer());
228  if (parentAsGroup)
229  {
230  Parameter::Pointer newParam;
231  switch (type)
232  {
233  case ParameterType_Empty:
234  {
235  newParam = EmptyParameter::New();
236  }
237  break;
238  case ParameterType_Int:
239  {
240  newParam = IntParameter::New();
241  }
242  break;
243  case ParameterType_Float:
244  {
245  newParam = FloatParameter::New();
246  }
247  break;
249  {
250  newParam = StringParameter::New();
251  }
252  break;
254  {
255  newParam = InputFilenameParameter::New();
256  }
257  break;
259  {
260  newParam = OutputFilenameParameter::New();
261  }
262  break;
264  {
265  newParam = DirectoryParameter::New();
266  }
267  break;
269  {
270  newParam = InputImageParameter::New();
271  }
272  break;
274  {
275  newParam = InputVectorDataParameter::New();
276  }
277  break;
279  {
280  newParam = OutputImageParameter::New();
281  }
282  break;
284  {
285  newParam = OutputVectorDataParameter::New();
286  }
287  break;
289  {
290  newParam = RadiusParameter::New();
291  }
292  break;
294  {
295  newParam = ChoiceParameter::New();
296  }
297  break;
298  case ParameterType_Group:
299  {
300  newParam = ParameterGroup::New();
301  }
302  break;
304  {
305  newParam = StringListParameter::New();
306  }
307  break;
309  {
310  newParam = InputImageListParameter::New();
311  }
312  break;
314  {
316  }
317  break;
319  {
320  newParam = ListViewParameter::New();
321  }
322  break;
324  {
325  newParam = ComplexInputImageParameter::New();
326  }
327  break;
329  {
331  }
332  break;
333  case ParameterType_RAM:
334  {
335  newParam = RAMParameter::New();
336  }
337  break;
338  }
339 
340  if (newParam.IsNull())
341  {
342  itkExceptionMacro(<< "Parameter type not supported for " << paramKey);
343  }
344 
345  newParam->SetKey(lastkey);
346  newParam->SetName(paramName);
347 
348  // If splittedKey is greater than 1, that means that the parameter
349  // is not a root, and have a parent(s):
350  // - Add the parent as root of this param
351  // - Add the param as a child of its parents
352  if (splittedKey.size() > 1)
353  {
354  newParam->SetRoot(parentParam);
355  parentParam->AddChild(newParam);
356  }
357 
358  parentAsGroup->AddParameter(newParam);
359  }
360  else
361  {
362  itkExceptionMacro(<< "Cannot add " << lastkey << " to parameter " << parentkey);
363  }
364 }
365 
366 void
368 {
369  m_ParameterList.push_back(p);
370 }
371 
374 {
375  return m_ParameterList[i];
376 }
377 
380 {
381  ParameterKey pName(name);
382  // Split the parameter name
383  std::vector<std::string> splittedName = pName.Split();
384 
385  // Get the first parameter key
386  std::string parentName = pName.GetFirstElement();
387 
388  // Look for parentName in the current group
389  Parameter::Pointer parentParam;
390  ParameterListType::iterator it;
391  for (it = m_ParameterList.begin(); it != m_ParameterList.end(); ++it)
392  {
393  Parameter::Pointer param = *it;
394  if (param->GetKey() == parentName)
395  {
396  parentParam = param;
397  break;
398  }
399  }
400 
401  if (parentParam.IsNull())
402  {
403  itkExceptionMacro(<< "Could not find parameter " << name)
404  }
405 
406  // If the name contains a child, make a recursive call
407  if (splittedName.size() > 1)
408  {
409  // Handle ParameterGroup case
410  ParameterGroup* parentAsGroup = dynamic_cast<ParameterGroup*>(parentParam.GetPointer());
411  if (parentAsGroup)
412  {
413  // Remove the parent from the param name
414  std::ostringstream childNameOss;
415  std::vector<std::string>::const_iterator it = splittedName.begin() + 1;
416  while(it != splittedName.end())
417  {
418  childNameOss << *it;
419  ++it;
420  if (it != splittedName.end())
421  {
422  childNameOss << ".";
423  }
424  }
425  std::string childName = childNameOss.str();
426 
427  return parentAsGroup->GetParameterByKey(childName);
428  }
429 
430  // Handle ChoiceParameter case
431  ChoiceParameter* parentAsChoice = dynamic_cast<ChoiceParameter*>(parentParam.GetPointer());
432  if (parentAsChoice)
433  {
434  // Check that splittedName[1] is one of the choice
435  ParameterGroup::Pointer associatedParam;
436 
437  // will throw if splittedName[1] is not a choice key
438  associatedParam = parentAsChoice->GetChoiceParameterGroupByKey(splittedName[1]);
439 
440  if (splittedName.size() > 2)
441  {
442  if (associatedParam.IsNull())
443  {
444  itkExceptionMacro(<< "Choice " << splittedName[1] << "in "
445  << splittedName[0] << " has no key named "
446  << splittedName[2]);
447  }
448 
449  // Remove the parent and the choice value from the param name
450  std::ostringstream childNameOss;
451  std::vector<std::string>::const_iterator it = splittedName.begin() + 2;
452  while(it != splittedName.end())
453  {
454  childNameOss << *it;
455  ++it;
456  if (it != splittedName.end())
457  {
458  childNameOss << ".";
459  }
460  }
461  std::string childName = childNameOss.str();
462  return associatedParam->GetParameterByKey(childName);
463  }
464  return associatedParam.GetPointer();
465  }
466  // Neither ParameterGroup, neither ChoiceParameter
467  itkExceptionMacro(<< "No parameter with key " << name);
468  }
469 
470  return parentParam.GetPointer();
471 }
472 
473 unsigned int
475 {
476  return m_ParameterList.size();
477 }
478 
479 }
480 }

Generated at Sun Feb 3 2013 00:59:20 for Orfeo Toolbox with doxygen 1.8.1.1