ctkSlicerModuleTest.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.commontk.org/LICENSE
  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. // CTK includes
  15. #include "ctkModuleDescription.h"
  16. #include "ctkPluginFrameworkFactory.h"
  17. #include "ctkPluginFramework.h"
  18. #include "ctkPluginException.h"
  19. #include "ctkModuleDescriptionReaderInterface.h"
  20. #include "ctkCommandLineParser.h"
  21. // STD includes
  22. #include <iostream>
  23. #include "QFile"
  24. //-----------------------------------------------------------------------------
  25. int ctkSlicerModuleTest(int argc, char * argv [] )
  26. {
  27. ctkCommandLineParser parser;
  28. parser.addArgument("", "-F", QVariant::String);
  29. bool ok = false;
  30. QHash<QString, QVariant> parsedArgs = parser.parseArguments(argc, argv, &ok);
  31. if (!ok)
  32. {
  33. std::cerr << qPrintable(parser.errorString()) << std::endl;
  34. return EXIT_FAILURE;
  35. }
  36. QString xmlFileName = parsedArgs["-F"].toString();
  37. ctkPluginFrameworkFactory fwFactory;
  38. ctkPluginFramework* framework = fwFactory.getFramework();
  39. try {
  40. framework->init();
  41. }
  42. catch (const ctkPluginException& exc)
  43. {
  44. qCritical() << "Failed to initialize the plug-in framework:" << exc;
  45. exit(1);
  46. }
  47. #ifdef CMAKE_INTDIR
  48. QString pluginPath = "/../plugins/" CMAKE_INTDIR "/";
  49. #else
  50. QString pluginPath = "/plugins/";
  51. #endif
  52. try
  53. {
  54. QString pluginLocation = "." + pluginPath + "liborg_commontk_slicermodule.dll";
  55. ctkPlugin* plugin = framework->getPluginContext()->installPlugin(QUrl::fromLocalFile(pluginLocation));
  56. plugin->start(ctkPlugin::START_TRANSIENT);
  57. framework->start();
  58. ctkServiceReference* serviceRef;
  59. serviceRef = framework->getPluginContext()->getServiceReference(
  60. "ctkModuleDescriptionReaderInterface" );
  61. ctkModuleDescriptionReaderInterface* reader;
  62. reader = qobject_cast<ctkModuleDescriptionReaderInterface*>
  63. (framework->getPluginContext()->getService(serviceRef));
  64. // Read file
  65. QFile file( xmlFileName );
  66. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  67. {
  68. std::cout << "XML file " << xmlFileName.toStdString( ) << " could not be opened." << endl;
  69. return false;
  70. }
  71. QTextStream stream( &file );
  72. // Parse XML file
  73. reader->setXmlContent( stream.readAll() );
  74. reader->Update();
  75. // Print module description
  76. QTextStream(stdout) << reader->moduleDescription( );
  77. }
  78. catch (const ctkPluginException& e)
  79. {
  80. qCritical() << e.what();
  81. }
  82. return EXIT_SUCCESS;
  83. }