ctkCmdLineModuleInstance.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkCmdLineModuleInstance.h"
  16. #include "ctkCmdLineModuleDescription.h"
  17. #include "ctkCmdLineModuleParameter.h"
  18. #include "ctkCmdLineModuleParameterGroup.h"
  19. #include "ctkCmdLineModuleReference.h"
  20. #include "ctkCmdLineModuleProcess_p.h"
  21. #include "ctkException.h"
  22. #include <QStringList>
  23. #include <QDebug>
  24. struct ctkCmdLineModuleInstancePrivate
  25. {
  26. ctkCmdLineModuleInstancePrivate(ctkCmdLineModuleInstance* qq,
  27. const ctkCmdLineModuleReference& moduleRef)
  28. : ModuleReference(moduleRef), q(qq)
  29. {
  30. }
  31. QString normalizeFlag(const QString& flag)
  32. {
  33. return flag.trimmed().remove(QRegExp("^-*"));
  34. }
  35. ctkCmdLineModuleReference ModuleReference;
  36. QList<QString> ParameterNames;
  37. private:
  38. ctkCmdLineModuleInstance* q;
  39. };
  40. ctkCmdLineModuleInstance::ctkCmdLineModuleInstance(const ctkCmdLineModuleReference& moduleRef)
  41. : d(new ctkCmdLineModuleInstancePrivate(this, moduleRef))
  42. {
  43. }
  44. ctkCmdLineModuleInstance::~ctkCmdLineModuleInstance()
  45. {
  46. }
  47. QList<QString> ctkCmdLineModuleInstance::parameterNames() const
  48. {
  49. if (!d->ParameterNames.isEmpty()) return d->ParameterNames;
  50. foreach (ctkCmdLineModuleParameterGroup paramGroup,
  51. moduleReference().description().parameterGroups())
  52. {
  53. foreach (ctkCmdLineModuleParameter param, paramGroup.parameters())
  54. {
  55. d->ParameterNames.push_back(param.name());
  56. }
  57. }
  58. return d->ParameterNames;
  59. }
  60. ctkCmdLineModuleReference ctkCmdLineModuleInstance::moduleReference() const
  61. {
  62. return d->ModuleReference;
  63. }
  64. QString ctkCmdLineModuleInstance::location() const
  65. {
  66. return d->ModuleReference.location();
  67. }
  68. QStringList ctkCmdLineModuleInstance::commandLineArguments() const
  69. {
  70. QStringList cmdLineArgs;
  71. QHash<int, QString> indexedArgs;
  72. QHash<QString,QVariant> currentValues = values();
  73. ctkCmdLineModuleDescription description = moduleReference().description();
  74. QHashIterator<QString,QVariant> valuesIter(currentValues);
  75. while(valuesIter.hasNext())
  76. {
  77. valuesIter.next();
  78. ctkCmdLineModuleParameter parameter = description.parameter(valuesIter.key());
  79. if (parameter.index() > -1)
  80. {
  81. indexedArgs.insert(parameter.index(), valuesIter.value().toString());
  82. }
  83. else
  84. {
  85. QString argFlag;
  86. if (parameter.longFlag().isEmpty())
  87. {
  88. argFlag = QString("-") + d->normalizeFlag(parameter.flag());
  89. }
  90. else
  91. {
  92. argFlag = QString("--") + d->normalizeFlag(parameter.longFlag());
  93. }
  94. QStringList args;
  95. if (parameter.multiple())
  96. {
  97. args = valuesIter.value().toString().split(',', QString::SkipEmptyParts);
  98. }
  99. else
  100. {
  101. args.push_back(valuesIter.value().toString());
  102. }
  103. foreach(QString arg, args)
  104. {
  105. cmdLineArgs << argFlag << arg;
  106. }
  107. }
  108. }
  109. QList<int> indexes = indexedArgs.keys();
  110. qSort(indexes.begin(), indexes.end());
  111. foreach(int index, indexes)
  112. {
  113. cmdLineArgs << indexedArgs[index];
  114. }
  115. return cmdLineArgs;
  116. }
  117. struct ctkCmdLineModuleFuture {};
  118. void ctkCmdLineModuleInstance::run() const
  119. {
  120. // // TODO: manage memory
  121. QStringList args = commandLineArguments();
  122. qDebug() << args;
  123. // ctkCmdLineModuleProcessRunner* moduleProcess =
  124. // new ctkCmdLineModuleProcessRunner(d->ModuleReference.location(), args);
  125. // return moduleProcess->start();
  126. }
  127. QHash<QString, QVariant> ctkCmdLineModuleInstance::values() const
  128. {
  129. QHash<QString,QVariant> result;
  130. foreach(QString parameterName, parameterNames())
  131. {
  132. result.insert(parameterName, value(parameterName));
  133. }
  134. return result;
  135. }
  136. void ctkCmdLineModuleInstance::setValues(const QHash<QString, QVariant> &values)
  137. {
  138. QHashIterator<QString,QVariant> iter(values);
  139. while(iter.hasNext())
  140. {
  141. iter.next();
  142. setValue(iter.key(), iter.value());
  143. }
  144. }