ctkCmdLineModuleFrontend.h 7.4 KB

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