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