ctkCmdLineModule.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "ctkCmdLineModule.h"
  16. #include "ctkCmdLineModuleDescription.h"
  17. #include "ctkCmdLineModuleParameter.h"
  18. #include "ctkCmdLineModuleParameterGroup.h"
  19. #include "ctkCmdLineModuleReference.h"
  20. #include "ctkCmdLineModuleProcessTask.h"
  21. #include "ctkCmdLineModuleFuture.h"
  22. #include "ctkException.h"
  23. #include <QStringList>
  24. #include <QDebug>
  25. #include <QVariant>
  26. struct ctkCmdLineModulePrivate
  27. {
  28. ctkCmdLineModulePrivate(ctkCmdLineModule* qq,
  29. const ctkCmdLineModuleReference& moduleRef)
  30. : ModuleReference(moduleRef), q(qq)
  31. {
  32. }
  33. QString normalizeFlag(const QString& flag)
  34. {
  35. return flag.trimmed().remove(QRegExp("^-*"));
  36. }
  37. ctkCmdLineModuleReference ModuleReference;
  38. QList<QString> ParameterNames;
  39. private:
  40. ctkCmdLineModule* q;
  41. };
  42. ctkCmdLineModule::ctkCmdLineModule(const ctkCmdLineModuleReference& moduleRef)
  43. : d(new ctkCmdLineModulePrivate(this, moduleRef))
  44. {
  45. }
  46. ctkCmdLineModule::~ctkCmdLineModule()
  47. {
  48. }
  49. QList<QString> ctkCmdLineModule::parameterNames() const
  50. {
  51. if (!d->ParameterNames.isEmpty()) return d->ParameterNames;
  52. foreach (ctkCmdLineModuleParameterGroup paramGroup,
  53. moduleReference().description().parameterGroups())
  54. {
  55. foreach (ctkCmdLineModuleParameter param, paramGroup.parameters())
  56. {
  57. d->ParameterNames.push_back(param.name());
  58. }
  59. }
  60. return d->ParameterNames;
  61. }
  62. ctkCmdLineModuleReference ctkCmdLineModule::moduleReference() const
  63. {
  64. return d->ModuleReference;
  65. }
  66. QString ctkCmdLineModule::location() const
  67. {
  68. return d->ModuleReference.location();
  69. }
  70. QStringList ctkCmdLineModule::commandLineArguments() const
  71. {
  72. QStringList cmdLineArgs;
  73. QHash<int, QString> indexedArgs;
  74. QHash<QString,QVariant> currentValues = values();
  75. ctkCmdLineModuleDescription description = moduleReference().description();
  76. QHashIterator<QString,QVariant> valuesIter(currentValues);
  77. while(valuesIter.hasNext())
  78. {
  79. valuesIter.next();
  80. ctkCmdLineModuleParameter parameter = description.parameter(valuesIter.key());
  81. if (parameter.index() > -1)
  82. {
  83. indexedArgs.insert(parameter.index(), valuesIter.value().toString());
  84. }
  85. else
  86. {
  87. QString argFlag;
  88. if (parameter.longFlag().isEmpty())
  89. {
  90. argFlag = QString("-") + d->normalizeFlag(parameter.flag());
  91. }
  92. else
  93. {
  94. argFlag = QString("--") + d->normalizeFlag(parameter.longFlag());
  95. }
  96. QStringList args;
  97. if (parameter.multiple())
  98. {
  99. args = valuesIter.value().toString().split(',', QString::SkipEmptyParts);
  100. }
  101. else if (parameter.tag() == "boolean")
  102. {
  103. if (valuesIter.value().toBool())
  104. {
  105. cmdLineArgs << argFlag;
  106. }
  107. }
  108. else
  109. {
  110. args.push_back(valuesIter.value().toString());
  111. }
  112. foreach(QString arg, args)
  113. {
  114. cmdLineArgs << argFlag << arg;
  115. }
  116. }
  117. }
  118. QList<int> indexes = indexedArgs.keys();
  119. qSort(indexes.begin(), indexes.end());
  120. foreach(int index, indexes)
  121. {
  122. cmdLineArgs << indexedArgs[index];
  123. }
  124. return cmdLineArgs;
  125. }
  126. ctkCmdLineModuleFuture ctkCmdLineModule::run() const
  127. {
  128. QStringList args = commandLineArguments();
  129. // Instances of ctkCmdLineModuleProcessTask are auto-deleted by the
  130. // thread pool.
  131. ctkCmdLineModuleProcessTask* moduleProcess =
  132. new ctkCmdLineModuleProcessTask(d->ModuleReference.location(), args);
  133. return moduleProcess->start();
  134. }
  135. QHash<QString, QVariant> ctkCmdLineModule::values() const
  136. {
  137. QHash<QString,QVariant> result;
  138. foreach(QString parameterName, parameterNames())
  139. {
  140. result.insert(parameterName, value(parameterName));
  141. }
  142. return result;
  143. }
  144. void ctkCmdLineModule::setValues(const QHash<QString, QVariant> &values)
  145. {
  146. QHashIterator<QString,QVariant> iter(values);
  147. while(iter.hasNext())
  148. {
  149. iter.next();
  150. setValue(iter.key(), iter.value());
  151. }
  152. }