ctkCmdLineModuleManager.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 CTKCMDLINEMODULEMANAGER_H
  16. #define CTKCMDLINEMODULEMANAGER_H
  17. #include <ctkCommandLineModulesCoreExport.h>
  18. #include <QStringList>
  19. #include <QString>
  20. #include "ctkCmdLineModuleReference.h"
  21. struct ctkCmdLineModuleBackend;
  22. struct ctkCmdLineModuleFrontendFactory;
  23. class ctkCmdLineModuleFrontend;
  24. class ctkCmdLineModuleFuture;
  25. struct ctkCmdLineModuleManagerPrivate;
  26. /**
  27. * @ingroup CommandLineModulesCore_API
  28. *
  29. * @brief Main manager class to handle loading and unloading of modules.
  30. *
  31. * This is the central managing class for CTK "command line modules". To register modules,
  32. * you need a command line module back-end which is capable of handling the URL scheme
  33. * under which you want to register your modules.
  34. *
  35. * A default back-end for handling "file" URLs which runs local executables is
  36. * available in ctkCmdLineModuleBackendLocalProcess.
  37. *
  38. * To create and run a specific front-end for a given module, you need a concrete
  39. * ctkCmdLineModuleFrontend implementation. The implementation provided in
  40. * ctkCmdLineModuleFrontendQtGui creates a default Qt widgets based GUI for a given
  41. * ctkCmdLineModuleReference instance.
  42. *
  43. * Here is an example how this and related classes could be used:
  44. *
  45. * First, we create ctkCmdLineModuleManager instance.
  46. * \snippet ModuleManager/main.cpp instantiate-mm
  47. *
  48. * Then we need at least one back-end which can handle registering modules.
  49. * \snippet ModuleManager/main.cpp register-backend
  50. *
  51. * Finally, we can register a module.
  52. * \snippet ModuleManager/main.cpp register-module
  53. *
  54. * @see ctkCmdLineModuleFrontend
  55. * @see ctkCmdLineModuleBackend
  56. */
  57. class CTK_CMDLINEMODULECORE_EXPORT ctkCmdLineModuleManager : public QObject
  58. {
  59. Q_OBJECT
  60. public:
  61. enum ValidationMode {
  62. /** registerModule() will throw an exception if the XML is invalid */
  63. STRICT_VALIDATION,
  64. /** no XML schema validation at all */
  65. SKIP_VALIDATION,
  66. /**
  67. * registerModule() will validate the XML description but proceed with
  68. * registration on validation error.
  69. */
  70. WEAK_VALIDATION
  71. };
  72. /**
  73. * @brief Create a module manager instance.
  74. * @param validationMode The validation mode for the XML description of the module parameters.
  75. * @param cacheDir The directory where to cache information about registered modules.
  76. *
  77. * If the <code>validationMode</code> argument is set to <code>SKIP_VALIDATION</code>, no XML validation
  78. * takes place and certain front-ends might fail to generate a GUI. If it is set to
  79. * <code>WEAK_VALIDATION</code>, module registrations will proceed on error but the error status
  80. * is available via ctkCmdLineModuleReference::xmlValidationErrorString().
  81. */
  82. ctkCmdLineModuleManager(ValidationMode validationMode = STRICT_VALIDATION,
  83. const QString& cacheDir = QString());
  84. ~ctkCmdLineModuleManager();
  85. /**
  86. * @brief Get the validation mode.
  87. * @return The validation mode.
  88. */
  89. ValidationMode validationMode() const;
  90. /**
  91. * @brief Registers a new back-end.
  92. * @param backend The new back-end.
  93. * @throws ctkInvalidArgumentException if another back-end was already registered handling
  94. * one of the URL schemes this back-end claims to handle.
  95. */
  96. void registerBackend(ctkCmdLineModuleBackend* backend);
  97. /**
  98. * @brief Registers a module, identified by the give URL.
  99. * @param location The URL for the new module.
  100. * @return A module reference.
  101. * @throws ctkInvalidArgumentException if no back-end for the given URL scheme was registered
  102. * or the XML description for the module is invalid.
  103. */
  104. ctkCmdLineModuleReference registerModule(const QUrl& location);
  105. /**
  106. * @brief Unregister a previously registered module.
  107. * @param moduleRef The reference for the module to unregister.
  108. *
  109. * This method does nothing if the module reference <code>moduleRef</code> is invalid or
  110. * reference an un-registered module.
  111. */
  112. void unregisterModule(const ctkCmdLineModuleReference& moduleRef);
  113. /**
  114. * @brief Returns a ctkCmdLineModuleReference object for the given URL.
  115. * @param location The location URL for which to get a module reference.
  116. * @return The module reference for the location or an invalid module reference
  117. * if no module was registered under the given location URL.
  118. */
  119. ctkCmdLineModuleReference moduleReference(const QUrl& location) const;
  120. /**
  121. * @brief Returns module references for all currently registered modules.
  122. * @return A list of module references.
  123. */
  124. QList<ctkCmdLineModuleReference> moduleReferences() const;
  125. /**
  126. * @brief Run a module front-end.
  127. * @param frontend The module front-end to run.
  128. * @return A ctkCmdLineModuleFuture object which can be used to interact with the
  129. * running front-end.
  130. *
  131. * This method takes a ctkCmdLineModuleFrontend pointer and uses the registered back-end
  132. * for this module's location URL scheme to run it asynchronously. The returned future object
  133. * is the only way to interact with the task started by the specific back-end.
  134. *
  135. * @see ctkCmdLineModuleFuture
  136. * @see ctkCmdLineModuleFutureWatcher
  137. */
  138. ctkCmdLineModuleFuture run(ctkCmdLineModuleFrontend* frontend);
  139. Q_SIGNALS:
  140. /**
  141. * @brief This signal is emitted whenever a module is registered.
  142. */
  143. void moduleRegistered(const ctkCmdLineModuleReference&);
  144. /**
  145. * @brief This signal is emitted whenever a module is un-registered.
  146. */
  147. void moduleUnregistered(const ctkCmdLineModuleReference&);
  148. private:
  149. QScopedPointer<ctkCmdLineModuleManagerPrivate> d;
  150. Q_DISABLE_COPY(ctkCmdLineModuleManager)
  151. };
  152. #endif // CTKCMDLINEMODULEMANAGER_H