ctkCmdLineModuleInstance.cpp 4.6 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. #include <QVariant>
  26. struct ctkCmdLineModuleInstancePrivate
  27. {
  28. ctkCmdLineModuleInstancePrivate(ctkCmdLineModuleInstance* 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. ctkCmdLineModuleInstance* q;
  41. };
  42. ctkCmdLineModuleInstance::ctkCmdLineModuleInstance(const ctkCmdLineModuleReference& moduleRef)
  43. : d(new ctkCmdLineModuleInstancePrivate(this, moduleRef))
  44. {
  45. }
  46. ctkCmdLineModuleInstance::~ctkCmdLineModuleInstance()
  47. {
  48. }
  49. QList<QString> ctkCmdLineModuleInstance::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 ctkCmdLineModuleInstance::moduleReference() const
  63. {
  64. return d->ModuleReference;
  65. }
  66. QString ctkCmdLineModuleInstance::location() const
  67. {
  68. return d->ModuleReference.location();
  69. }
  70. QStringList ctkCmdLineModuleInstance::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
  102. {
  103. args.push_back(valuesIter.value().toString());
  104. }
  105. foreach(QString arg, args)
  106. {
  107. cmdLineArgs << argFlag << arg;
  108. }
  109. }
  110. }
  111. QList<int> indexes = indexedArgs.keys();
  112. qSort(indexes.begin(), indexes.end());
  113. foreach(int index, indexes)
  114. {
  115. cmdLineArgs << indexedArgs[index];
  116. }
  117. return cmdLineArgs;
  118. }
  119. QFuture<QString> ctkCmdLineModuleInstance::run() const
  120. {
  121. QStringList args = commandLineArguments();
  122. // Instances of ctkCmdLineModuleProcessTask are auto-deleted by the
  123. // thread pool.
  124. ctkCmdLineModuleProcessTask* moduleProcess =
  125. new ctkCmdLineModuleProcessTask(d->ModuleReference.location(), args);
  126. return moduleProcess->start();
  127. }
  128. QHash<QString, QVariant> ctkCmdLineModuleInstance::values() const
  129. {
  130. QHash<QString,QVariant> result;
  131. foreach(QString parameterName, parameterNames())
  132. {
  133. result.insert(parameterName, value(parameterName));
  134. }
  135. return result;
  136. }
  137. void ctkCmdLineModuleInstance::setValues(const QHash<QString, QVariant> &values)
  138. {
  139. QHashIterator<QString,QVariant> iter(values);
  140. while(iter.hasNext())
  141. {
  142. iter.next();
  143. setValue(iter.key(), iter.value());
  144. }
  145. }