ctkCmdLineModule.cpp 4.9 KB

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