ctkCmdLineModuleFrontend.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "ctkCmdLineModuleFrontend.h"
  16. #include "ctkCmdLineModuleDescription.h"
  17. #include "ctkCmdLineModuleParameter.h"
  18. #include "ctkCmdLineModuleParameterGroup.h"
  19. #include "ctkCmdLineModuleReference.h"
  20. #include "ctkCmdLineModuleFuture.h"
  21. #include "ctkException.h"
  22. #include <QUrl>
  23. #include <QFutureWatcher>
  24. //----------------------------------------------------------------------------
  25. struct ctkCmdLineModuleFrontendPrivate
  26. {
  27. ctkCmdLineModuleFrontendPrivate(const ctkCmdLineModuleReference& moduleRef, ctkCmdLineModuleFrontend* q)
  28. : q(q)
  29. , ModuleReference(moduleRef)
  30. {
  31. }
  32. void _q_resultReadyAt(int index)
  33. {
  34. q->resultReady(Future.resultAt(index));
  35. }
  36. ctkCmdLineModuleFrontend* q;
  37. ctkCmdLineModuleReference ModuleReference;
  38. QList<QString> ParameterNames;
  39. ctkCmdLineModuleFuture Future;
  40. QFutureWatcher<ctkCmdLineModuleResult> FutureWatcher;
  41. };
  42. //----------------------------------------------------------------------------
  43. ctkCmdLineModuleFrontend::ctkCmdLineModuleFrontend(const ctkCmdLineModuleReference& moduleRef)
  44. : d(new ctkCmdLineModuleFrontendPrivate(moduleRef, this))
  45. {
  46. connect(&d->FutureWatcher, SIGNAL(resultReadyAt(int)), SLOT(_q_resultReadyAt(int)));
  47. }
  48. //----------------------------------------------------------------------------
  49. void ctkCmdLineModuleFrontend::setFuture(const ctkCmdLineModuleFuture &future)
  50. {
  51. d->Future = future;
  52. // Reset all simple output (return) parameter values
  53. foreach(const ctkCmdLineModuleParameter& param, this->parameters(QString(), Output))
  54. {
  55. if (param.index() == 1000)
  56. {
  57. this->setValue(param.name(), param.defaultValue());
  58. }
  59. }
  60. d->FutureWatcher.setFuture(d->Future);
  61. }
  62. //----------------------------------------------------------------------------
  63. ctkCmdLineModuleFrontend::~ctkCmdLineModuleFrontend()
  64. {
  65. }
  66. QList<QString> ctkCmdLineModuleFrontend::parameterNames() const
  67. {
  68. if (!d->ParameterNames.isEmpty()) return d->ParameterNames;
  69. foreach (ctkCmdLineModuleParameterGroup paramGroup,
  70. moduleReference().description().parameterGroups())
  71. {
  72. foreach (ctkCmdLineModuleParameter param, paramGroup.parameters())
  73. {
  74. d->ParameterNames.push_back(param.name());
  75. }
  76. }
  77. return d->ParameterNames;
  78. }
  79. //----------------------------------------------------------------------------
  80. ctkCmdLineModuleReference ctkCmdLineModuleFrontend::moduleReference() const
  81. {
  82. return d->ModuleReference;
  83. }
  84. //----------------------------------------------------------------------------
  85. QUrl ctkCmdLineModuleFrontend::location() const
  86. {
  87. return d->ModuleReference.location();
  88. }
  89. //----------------------------------------------------------------------------
  90. ctkCmdLineModuleFuture ctkCmdLineModuleFrontend::future() const
  91. {
  92. return d->Future;
  93. }
  94. //----------------------------------------------------------------------------
  95. bool ctkCmdLineModuleFrontend::isRunning() const
  96. {
  97. return d->Future.isRunning();
  98. }
  99. //----------------------------------------------------------------------------
  100. bool ctkCmdLineModuleFrontend::isPaused() const
  101. {
  102. return d->Future.isPaused();
  103. }
  104. //----------------------------------------------------------------------------
  105. QHash<QString, QVariant> ctkCmdLineModuleFrontend::values() const
  106. {
  107. QHash<QString,QVariant> result;
  108. foreach(QString parameterName, parameterNames())
  109. {
  110. result.insert(parameterName, value(parameterName));
  111. }
  112. return result;
  113. }
  114. //----------------------------------------------------------------------------
  115. void ctkCmdLineModuleFrontend::setValues(const QHash<QString, QVariant> &values)
  116. {
  117. QHashIterator<QString,QVariant> iter(values);
  118. while(iter.hasNext())
  119. {
  120. iter.next();
  121. setValue(iter.key(), iter.value());
  122. }
  123. }
  124. //----------------------------------------------------------------------------
  125. QList<ctkCmdLineModuleParameter> ctkCmdLineModuleFrontend::parameters(const QString &type, ParameterFilters filters)
  126. {
  127. ctkCmdLineModuleDescription description = this->moduleReference().description();
  128. QList<ctkCmdLineModuleParameter> parameters;
  129. foreach(ctkCmdLineModuleParameterGroup group, description.parameterGroups())
  130. {
  131. foreach(ctkCmdLineModuleParameter param, group.parameters())
  132. {
  133. if (filters.testFlag(Input) &&
  134. (param.channel().isEmpty() || param.channel().compare("input", Qt::CaseInsensitive) == 0))
  135. {
  136. if (type.isEmpty() || param.tag().compare(type, Qt::CaseInsensitive) == 0)
  137. {
  138. parameters << param;
  139. }
  140. }
  141. if (filters.testFlag(Output) && param.channel().compare("output", Qt::CaseInsensitive) == 0)
  142. {
  143. if (type.isEmpty() || param.tag().compare(type, Qt::CaseInsensitive) == 0)
  144. {
  145. parameters << param;
  146. }
  147. }
  148. }
  149. }
  150. return parameters;
  151. }
  152. //----------------------------------------------------------------------------
  153. void ctkCmdLineModuleFrontend::resetValues()
  154. {
  155. foreach(ctkCmdLineModuleParameter param, this->parameters())
  156. {
  157. this->setValue(param.name(), param.defaultValue());
  158. }
  159. }
  160. //----------------------------------------------------------------------------
  161. void ctkCmdLineModuleFrontend::resultReady(const ctkCmdLineModuleResult &result)
  162. {
  163. try
  164. {
  165. if (this->moduleReference().description().parameter(result.parameter()).channel() != "output")
  166. {
  167. qWarning() << "Module" << this->moduleReference().location() << "is reporting results for non-output parameter"
  168. << result.parameter() << ". Report ignored.";
  169. return;
  170. }
  171. this->setValue(result.parameter(), result.value());
  172. }
  173. catch (const ctkInvalidArgumentException&)
  174. {}
  175. }
  176. #include "moc_ctkCmdLineModuleFrontend.h"