1 /*=============================================================================
2 
3   Library: CTK
4 
5   Copyright (c) German Cancer Research Center,
6     Division of Medical and Biological Informatics
7 
8   Licensed under the Apache License, Version 2.0 (the "License");
9   you may not use this file except in compliance with the License.
10   You may obtain a copy of the License at
11 
12     http://www.apache.org/licenses/LICENSE-2.0
13 
14   Unless required by applicable law or agreed to in writing, software
15   distributed under the License is distributed on an "AS IS" BASIS,
16   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   See the License for the specific language governing permissions and
18   limitations under the License.
19 
20 =============================================================================*/
21 
22 #include "ctkCmdLineModuleManager.h"
23 #include "ctkCmdLineModuleBackendLocalProcess.h"
24 #include "ctkCmdLineModuleFrontendFactoryQtGui.h"
25 #include "ctkCmdLineModuleFrontendQtGui.h"
26 #include "ctkCmdLineModuleFuture.h"
27 #include "ctkException.h"
28 #include "ctkCmdLineModuleRunException.h"
29 
30 #include <QApplication>
31 #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
32 #include <QDesktopServices>
33 #else
34 #include <QStandardPaths>
35 #endif
36 #include <QWidget>
37 #include <QUrl>
38 #include <QDebug>
39 
40 #include <cstdlib>
41 
main(int argc,char ** argv)42 int main(int argc, char** argv)
43 {
44   QApplication myApp(argc, argv);
45   myApp.setOrganizationName("CommonTK");
46   myApp.setApplicationName("ModuleManagerSnippet");
47 
48   // [instantiate-mm]
49   // Instantiate a ctkCmdLineModuleManager class.
50   ctkCmdLineModuleManager moduleManager(
51         // Use "strict" validation mode, rejecting modules with non-valid XML descriptions.
52         ctkCmdLineModuleManager::STRICT_VALIDATION,
53         // Use the default cache location for this application
54       #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
55         QDesktopServices::storageLocation(QDesktopServices::CacheLocation)
56       #else
57         QStandardPaths::writableLocation(QStandardPaths::CacheLocation)
58       #endif
59         );
60   // [instantiate-mm]
61 
62   // [register-backend]
63   // Instantiate a back-end for running executable modules in a local process.
64   // This back-end handles the "file" Url scheme.
65   QScopedPointer<ctkCmdLineModuleBackend> processBackend(new ctkCmdLineModuleBackendLocalProcess);
66 
67   // Register the back-end with the module manager.
68   moduleManager.registerBackend(processBackend.data());
69   // [register-backend]
70 
71   // [register-module]
72   ctkCmdLineModuleReference moduleRef;
73   try
74   {
75     // Register a local executable as a module, the ctkCmdLineModuleBackendLocalProcess
76     // can handle it.
77     moduleRef = moduleManager.registerModule(QUrl::fromLocalFile("C:/modules/MyModule.exe"));
78   }
79   catch (const ctkInvalidArgumentException& e)
80   {
81     // Module validation failed.
82     qDebug() << e;
83     return EXIT_FAILURE;
84   }
85   // [register-module]
86 
87   // [create-frontend]
88   // We use the "Qt Gui" frontend factory.
89   QScopedPointer<ctkCmdLineModuleFrontendFactory> frontendFactory(new ctkCmdLineModuleFrontendFactoryQtGui);
90   myApp.addLibraryPath(QCoreApplication::applicationDirPath() + "/../");
91 
92   QScopedPointer<ctkCmdLineModuleFrontend> frontend(frontendFactory->create(moduleRef));
93 
94   // Create the actual GUI representation.
95   QWidget* gui = qobject_cast<QWidget*>(frontend->guiHandle());
96   // [create-frontend]
97   Q_UNUSED(gui);
98 
99   // Now try and run the module (using the default values for the parameters)
100   // and print out any reported output and results.
101   // [run-module]
102   try
103   {
104     ctkCmdLineModuleFuture future = moduleManager.run(frontend.data());
105     future.waitForFinished();
106     qDebug() << "Console output:";
107     qDebug() << future.readAllOutputData();
108     qDebug() << "Error output:";
109     qDebug() << future.readAllErrorData();
110     qDebug() << "Results:";
111     qDebug() << future.results();
112   }
113   catch (const ctkCmdLineModuleRunException& e)
114   {
115     qWarning() << e;
116   }
117   // [run-module]
118 
119   return EXIT_SUCCESS;
120 }
121