main.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "ctkCmdLineModuleManager.h"
  16. #include "ctkCmdLineModuleBackendLocalProcess.h"
  17. #include "ctkCmdLineModuleFrontendFactoryQtGui.h"
  18. #include "ctkCmdLineModuleFrontendQtGui.h"
  19. #include "ctkCmdLineModuleFuture.h"
  20. #include "ctkException.h"
  21. #include "ctkCmdLineModuleRunException.h"
  22. #include <QApplication>
  23. #include <QDesktopServices>
  24. #include <QWidget>
  25. #include <QUrl>
  26. #include <QDebug>
  27. #include <cstdlib>
  28. int main(int argc, char** argv)
  29. {
  30. QApplication myApp(argc, argv);
  31. myApp.setOrganizationName("CommonTK");
  32. myApp.setApplicationName("ModuleManagerSnippet");
  33. // [instantiate-mm]
  34. // Instantiate a ctkCmdLineModuleManager class.
  35. ctkCmdLineModuleManager moduleManager(
  36. // Use "strict" validation mode, rejecting modules with non-valid XML descriptions.
  37. ctkCmdLineModuleManager::STRICT_VALIDATION,
  38. // Use the default cache location for this application
  39. QDesktopServices::storageLocation(QDesktopServices::CacheLocation)
  40. );
  41. // [instantiate-mm]
  42. // [register-backend]
  43. // Instantiate a back-end for running executable modules in a local process.
  44. // This back-end handles the "file" Url scheme.
  45. QScopedPointer<ctkCmdLineModuleBackend> processBackend(new ctkCmdLineModuleBackendLocalProcess);
  46. // Register the back-end with the module manager.
  47. moduleManager.registerBackend(processBackend.data());
  48. // [register-backend]
  49. // [register-module]
  50. ctkCmdLineModuleReference moduleRef;
  51. try
  52. {
  53. // Register a local executable as a module, the ctkCmdLineModuleBackendLocalProcess
  54. // can handle it.
  55. moduleRef = moduleManager.registerModule(QUrl::fromLocalFile("C:/modules/MyModule.exe"));
  56. }
  57. catch (const ctkInvalidArgumentException& e)
  58. {
  59. // Module validation failed.
  60. qDebug() << e;
  61. return EXIT_FAILURE;
  62. }
  63. // [register-module]
  64. // [create-frontend]
  65. // We use the "Qt Gui" frontend factory.
  66. QScopedPointer<ctkCmdLineModuleFrontendFactory> frontendFactory(new ctkCmdLineModuleFrontendFactoryQtGui);
  67. myApp.addLibraryPath(QCoreApplication::applicationDirPath() + "/../");
  68. QScopedPointer<ctkCmdLineModuleFrontend> frontend(frontendFactory->create(moduleRef));
  69. // Create the actual GUI representation.
  70. QWidget* gui = qobject_cast<QWidget*>(frontend->guiHandle());
  71. // [create-frontend]
  72. Q_UNUSED(gui);
  73. // Now try and run the module (using the default values for the parameters)
  74. // and print out any reported output and results.
  75. // [run-module]
  76. try
  77. {
  78. ctkCmdLineModuleFuture future = moduleManager.run(frontend.data());
  79. future.waitForFinished();
  80. qDebug() << "Console output:";
  81. qDebug() << future.readAllOutputData();
  82. qDebug() << "Error output:";
  83. qDebug() << future.readAllErrorData();
  84. qDebug() << "Results:";
  85. qDebug() << future.results();
  86. }
  87. catch (const ctkCmdLineModuleRunException& e)
  88. {
  89. qWarning() << e;
  90. }
  91. // [run-module]
  92. return EXIT_SUCCESS;
  93. }