ctkCmdLineModuleFrontend.cpp 6.3 KB

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