ctkCmdLineModuleFrontend.h 8.7 KB

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