ctkCmdLineModuleFrontend.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. d->FutureWatcher.setFuture(d->Future);
  53. }
  54. //----------------------------------------------------------------------------
  55. ctkCmdLineModuleFrontend::~ctkCmdLineModuleFrontend()
  56. {
  57. }
  58. QList<QString> ctkCmdLineModuleFrontend::parameterNames() const
  59. {
  60. if (!d->ParameterNames.isEmpty()) return d->ParameterNames;
  61. foreach (ctkCmdLineModuleParameterGroup paramGroup,
  62. moduleReference().description().parameterGroups())
  63. {
  64. foreach (ctkCmdLineModuleParameter param, paramGroup.parameters())
  65. {
  66. d->ParameterNames.push_back(param.name());
  67. }
  68. }
  69. return d->ParameterNames;
  70. }
  71. //----------------------------------------------------------------------------
  72. ctkCmdLineModuleReference ctkCmdLineModuleFrontend::moduleReference() const
  73. {
  74. return d->ModuleReference;
  75. }
  76. //----------------------------------------------------------------------------
  77. QUrl ctkCmdLineModuleFrontend::location() const
  78. {
  79. return d->ModuleReference.location();
  80. }
  81. //----------------------------------------------------------------------------
  82. ctkCmdLineModuleFuture ctkCmdLineModuleFrontend::future() const
  83. {
  84. return d->Future;
  85. }
  86. //----------------------------------------------------------------------------
  87. bool ctkCmdLineModuleFrontend::isRunning() const
  88. {
  89. return d->Future.isRunning();
  90. }
  91. //----------------------------------------------------------------------------
  92. bool ctkCmdLineModuleFrontend::isPaused() const
  93. {
  94. return d->Future.isPaused();
  95. }
  96. //----------------------------------------------------------------------------
  97. QHash<QString, QVariant> ctkCmdLineModuleFrontend::values() const
  98. {
  99. QHash<QString,QVariant> result;
  100. foreach(QString parameterName, parameterNames())
  101. {
  102. result.insert(parameterName, value(parameterName));
  103. }
  104. return result;
  105. }
  106. //----------------------------------------------------------------------------
  107. void ctkCmdLineModuleFrontend::setValues(const QHash<QString, QVariant> &values)
  108. {
  109. QHashIterator<QString,QVariant> iter(values);
  110. while(iter.hasNext())
  111. {
  112. iter.next();
  113. setValue(iter.key(), iter.value());
  114. }
  115. }
  116. //----------------------------------------------------------------------------
  117. QList<ctkCmdLineModuleParameter> ctkCmdLineModuleFrontend::parameters(const QString &type, ParameterFilters filters)
  118. {
  119. ctkCmdLineModuleDescription description = this->moduleReference().description();
  120. QList<ctkCmdLineModuleParameter> parameters;
  121. foreach(ctkCmdLineModuleParameterGroup group, description.parameterGroups())
  122. {
  123. foreach(ctkCmdLineModuleParameter param, group.parameters())
  124. {
  125. if (filters.testFlag(Input) &&
  126. (param.channel().isEmpty() || param.channel().compare("input", Qt::CaseInsensitive) == 0))
  127. {
  128. if (type.isEmpty() || param.tag().compare(type, Qt::CaseInsensitive) == 0)
  129. {
  130. parameters << param;
  131. }
  132. }
  133. if (filters.testFlag(Output) && param.channel().compare("output", Qt::CaseInsensitive) == 0)
  134. {
  135. if (type.isEmpty() || param.tag().compare(type, Qt::CaseInsensitive) == 0)
  136. {
  137. parameters << param;
  138. }
  139. }
  140. }
  141. }
  142. return parameters;
  143. }
  144. //----------------------------------------------------------------------------
  145. void ctkCmdLineModuleFrontend::resetValues()
  146. {
  147. foreach(ctkCmdLineModuleParameter param, this->parameters())
  148. {
  149. this->setValue(param.name(), param.defaultValue());
  150. }
  151. }
  152. //----------------------------------------------------------------------------
  153. void ctkCmdLineModuleFrontend::resultReady(const ctkCmdLineModuleResult &result)
  154. {
  155. try
  156. {
  157. if (this->moduleReference().description().parameter(result.parameter()).channel() != "output")
  158. {
  159. qWarning() << "Module" << this->moduleReference().location() << "is reporting results for non-output parameter"
  160. << result.parameter() << ". Report ignored.";
  161. return;
  162. }
  163. this->setValue(result.parameter(), result.value());
  164. }
  165. catch (const ctkInvalidArgumentException&)
  166. {}
  167. }
  168. #include "moc_ctkCmdLineModuleFrontend.h"