ctkCmdLineModuleFrontend.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. #ifndef CTKCMDLINEMODULEFRONTEND_H
  16. #define CTKCMDLINEMODULEFRONTEND_H
  17. #include "ctkCommandLineModulesCoreExport.h"
  18. #include <QObject>
  19. template<class K, class V> class QHash;
  20. class QUrl;
  21. class ctkCmdLineModuleFuture;
  22. class ctkCmdLineModuleReference;
  23. class ctkCmdLineModuleParameter;
  24. class ctkCmdLineModuleResult;
  25. struct ctkCmdLineModuleFrontendPrivate;
  26. /**
  27. * \class ctkCmdLineModuleFrontend
  28. * \brief Abstract base class for all front-end command
  29. * line module implementations.
  30. * \ingroup CommandLineModulesCore
  31. * \see ctkCmdLineModuleFrontendQtGui
  32. * \see ctkCmdLineModuleFrontendQtWebKit
  33. */
  34. class CTK_CMDLINEMODULECORE_EXPORT ctkCmdLineModuleFrontend : public QObject
  35. {
  36. Q_OBJECT
  37. Q_ENUMS(ParamterValueRole)
  38. public:
  39. enum ParameterValueRole {
  40. /**
  41. * Data returned using this role must not be of any type not supported by
  42. * QVariant by default. For complex parameter types (like file, image,
  43. * geometry, etc.) the data must be convertible to a QString pointing
  44. * to a local resource.
  45. *
  46. * This role is usually used by backends for retrieving data and is mainly
  47. * important for data which acts as a handle to the real data (e.g. a
  48. * backend usually needs to get the absolute path to a local file for the
  49. * current value of an input image parameter, instead of the image label
  50. * displayed in a GUI).
  51. */
  52. LocalResourceRole = 0,
  53. /**
  54. * Describes data suitable for displaying in a GUI. For many parameter types
  55. * (e.g. scalar and vector parameters) data returned by this role will be
  56. * the same as returned by the LocalResourceRole role.
  57. **/
  58. DisplayRole = 1,
  59. /**
  60. * This role can be used in custom frontends to return a QVariant
  61. * containing for example an in-memory representation of a complex object.
  62. * One can then either convert the in-memory representation to a local
  63. * resource before running a module such that arbitrary backends relying on
  64. * the LocalResourceRole role can process the data. Or one creates a custom
  65. * backend which knows how to handle QVariants returned by this role.
  66. */
  67. UserRole = 8
  68. };
  69. enum ParameterFilter {
  70. Input = 0x01,
  71. Output = 0x02,
  72. All = Input | Output
  73. };
  74. Q_DECLARE_FLAGS(ParameterFilters, ParameterFilter)
  75. ~ctkCmdLineModuleFrontend();
  76. /**
  77. * @brief Returns the GUI representation, currently supporting only
  78. * QObject subclasses.
  79. * @return The GUI that can then be embeded in an applicaiton
  80. * window for instance.
  81. */
  82. virtual QObject* guiHandle() const = 0;
  83. /**
  84. * @brief GUIs will need to be able to read parameters,
  85. * here we retrieve by role.
  86. *
  87. * @return QVariant
  88. * @see ParameterValueRole
  89. */
  90. virtual QVariant value(const QString& parameter,
  91. int role = LocalResourceRole) const = 0;
  92. /**
  93. * @brief Set the value of a certain parameter.
  94. *
  95. * @param parameter The name of the parameter, as defined in the XML.
  96. * @param value The value for that parameter.
  97. * @param role The role for which to set the data.
  98. *
  99. * @see ParameterValueRole
  100. */
  101. virtual void setValue(const QString& parameter, const QVariant& value,
  102. int role = DisplayRole) = 0;
  103. /**
  104. * @brief Return the ctkCmdLineModuleFuture, derived from QFuture to
  105. * provide asynchronous processing and interaction with the running frontend.
  106. *
  107. * Note that the future returned by this method will be different after the
  108. * frontend was started. Either use isRunning() to check wether this frontend
  109. * is currently running or connect to the started() signal.
  110. *
  111. * @see ctkCmdLineModuleFuture
  112. */
  113. virtual ctkCmdLineModuleFuture future() const;
  114. /**
  115. * @brief Returns a QUrl to define the location of the module that is run.
  116. *
  117. * For a local process this may be the file location of the command
  118. * line module. For other implementations, such as a web-service,
  119. * this could be a web URL.
  120. *
  121. * @return QUrl A resource independent URL defining where the module is.
  122. */
  123. QUrl location() const;
  124. /**
  125. * @brief Returns a ctkCmdLineModuleReference value object that refers
  126. * and provides access to the module.
  127. * @return ctkCmdLineModuleReference
  128. */
  129. ctkCmdLineModuleReference moduleReference() const;
  130. /**
  131. * @brief Returns a list of all valid parameter names.
  132. */
  133. virtual QList<QString> parameterNames() const;
  134. /**
  135. * @brief Returns a map of parameter names and values.
  136. */
  137. virtual QHash<QString,QVariant> values() const;
  138. /**
  139. * @brief Enables the parameter values to be set.
  140. */
  141. virtual void setValues(const QHash<QString,QVariant>& values);
  142. /**
  143. * @brief Indicates if the underlying process is currently active.
  144. * @return true if running and false otherwise.
  145. */
  146. bool isRunning() const;
  147. /**
  148. * @brief Indicates if the underlying process is currently paused.
  149. * @return true if paused and false otherwise.
  150. */
  151. bool isPaused() const;
  152. // convenience methods
  153. /**
  154. * @brief Useful method to return subsets of parameter objects, searhing
  155. * by type for example "image" and filter for example "input"/"output".
  156. * @param type The type of parameter, as defined in the XML element.
  157. * @param filters flag to define whether we want input/output.
  158. * @return QList of ctkCmdLineModuleParameter depending on type and filters.
  159. * @see ParameterFilter
  160. */
  161. QList<ctkCmdLineModuleParameter> parameters(
  162. const QString& type = QString(),
  163. ParameterFilters filters = All);
  164. void resetValues();
  165. Q_SIGNALS:
  166. /**
  167. * @brief This signal is emitted whenever a parameter value is changed by using
  168. * the ctkCmdLineModuleFrontent class.
  169. * @param parameter The parameter name.
  170. * @param value The new parameter value.
  171. *
  172. * Please note that this signal is not emitted if a parameter value is
  173. * changed in the generated GUI.
  174. */
  175. void valueChanged(const QString& parameter, const QVariant& value);
  176. /**
  177. * @brief This signal is emitted when the frontend is run.
  178. *
  179. * You can use this signal to get the ctkCmdLineModuleFuture instance
  180. * from future() to interact with the running frontend.
  181. */
  182. void started();
  183. protected:
  184. /**
  185. * @brief Constructor.
  186. */
  187. ctkCmdLineModuleFrontend(const ctkCmdLineModuleReference& moduleRef);
  188. /**
  189. * @brief Sets the ctkCmdLineModuleFuture which effectively
  190. * contains the backend that is run.
  191. */
  192. void setFuture(const ctkCmdLineModuleFuture& future);
  193. private Q_SLOTS:
  194. /**
  195. * @brief Provides results as reported by the running module.
  196. * @param result
  197. *
  198. * This method is called when a running module reports a new
  199. * result. The default implementation updates the current value
  200. * of the output parameter in the GUI with the reported value.
  201. */
  202. virtual void resultReady(const ctkCmdLineModuleResult& result);
  203. private:
  204. Q_DISABLE_COPY(ctkCmdLineModuleFrontend)
  205. friend struct ctkCmdLineModuleFrontendPrivate;
  206. friend class ctkCmdLineModuleManager;
  207. friend class ctkCmdLineModulePrivate;
  208. Q_PRIVATE_SLOT(d, void _q_resultReadyAt(int))
  209. QScopedPointer<ctkCmdLineModuleFrontendPrivate> d;
  210. };
  211. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkCmdLineModuleFrontend::ParameterFilters)
  212. #endif // CTKCMDLINEMODULEFRONTEND_H