main.cpp 3.9 KB

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