ctkSlicerModuleTest.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 CISTIB - Universitat Pompeu Fabra
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QApplication>
  16. #include <QFile>
  17. // CTK includes
  18. #include "ctkModuleDescription.h"
  19. #include "ctkPluginFrameworkFactory.h"
  20. #include "ctkPluginFramework.h"
  21. #include "ctkPluginException.h"
  22. #include "ctkModuleDescriptionReader.h"
  23. #include "ctkModuleDescriptionConverterInterface.h"
  24. #include "ctkCommandLineParser.h"
  25. // STD includes
  26. #include <iostream>
  27. ctkModuleDescription ReadModuleDescription( ctkPluginContext* context, const QString &xmlFileName ) ;
  28. void BuildCommandLine( ctkPluginContext* context, const ctkModuleDescription& module ) ;
  29. //-----------------------------------------------------------------------------
  30. int ctkSlicerModuleTest(int argc, char * argv [] )
  31. {
  32. QApplication app(argc, argv);
  33. ctkCommandLineParser parser;
  34. parser.addArgument("", "-F", QVariant::String);
  35. bool ok = false;
  36. QHash<QString, QVariant> parsedArgs = parser.parseArguments(argc, argv, &ok);
  37. if (!ok)
  38. {
  39. std::cerr << qPrintable(parser.errorString()) << std::endl;
  40. return EXIT_FAILURE;
  41. }
  42. QString xmlFileName = parsedArgs["-F"].toString();
  43. ctkPluginFrameworkFactory fwFactory;
  44. ctkPluginFramework* framework = fwFactory.getFramework();
  45. try {
  46. framework->init();
  47. }
  48. catch (const ctkPluginException& exc)
  49. {
  50. qCritical() << "Failed to initialize the plug-in framework:" << exc;
  51. exit(1);
  52. }
  53. #ifdef CMAKE_INTDIR
  54. QString pluginPath = "/../plugins/" CMAKE_INTDIR "/";
  55. #else
  56. QString pluginPath = "/plugins/";
  57. #endif
  58. try
  59. {
  60. QString pluginLocation = "." + pluginPath + "liborg_commontk_slicermodule.dll";
  61. ctkPlugin* plugin = framework->getPluginContext()->installPlugin(QUrl::fromLocalFile(pluginLocation));
  62. plugin->start(ctkPlugin::START_TRANSIENT);
  63. framework->start();
  64. ctkModuleDescription module;
  65. module = ReadModuleDescription( framework->getPluginContext(), xmlFileName );
  66. BuildCommandLine( framework->getPluginContext(), module );
  67. }
  68. catch (const ctkPluginException& e)
  69. {
  70. qCritical() << e.what();
  71. }
  72. return EXIT_SUCCESS;
  73. }
  74. ctkModuleDescription ReadModuleDescription(
  75. ctkPluginContext* context, const QString &xmlFileName )
  76. {
  77. ctkServiceReference* serviceRef;
  78. serviceRef = context->getServiceReference(
  79. "ctkModuleDescriptionReaderInterface" );
  80. ctkModuleDescriptionReader* reader;
  81. reader = qobject_cast<ctkModuleDescriptionReader*>
  82. (context->getService(serviceRef));
  83. // Read file
  84. QFile file( xmlFileName );
  85. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  86. {
  87. std::cout << "XML file " << xmlFileName.toStdString( ) << " could not be opened." << std::endl;
  88. exit(1);
  89. }
  90. // Parse XML file
  91. reader->setInput( &file );
  92. reader->update();
  93. QTextStream stream(stdout);
  94. stream << reader->moduleDescription( );
  95. return reader->moduleDescription( );
  96. }
  97. void BuildCommandLine( ctkPluginContext* context, const ctkModuleDescription& module )
  98. {
  99. ctkServiceReference* serviceRef;
  100. serviceRef = context->getServiceReference(
  101. "ctkModuleDescriptionConverterInterface" );
  102. ctkModuleDescriptionConverterInterface* converter;
  103. converter = qobject_cast<ctkModuleDescriptionConverterInterface*>
  104. (context->getService(serviceRef));
  105. QStringList commandLineString;
  106. converter->setModuleDescription( module );
  107. converter->update();
  108. commandLineString = converter->GetOutput().toStringList();
  109. QTextStream stream(stdout);
  110. stream << commandLineString;
  111. }