瀏覽代碼

ENH: Added reader interface and slicermodule plugin to implement the XML Slicer reader

Xavi Planes 14 年之前
父節點
當前提交
567feb8367

+ 1 - 0
CMakeLists.txt

@@ -291,6 +291,7 @@ SET(CTK_PLUGINS
   org.commontk.cli:OFF
   org.commontk.plugingenerator.core:OFF
   org.commontk.plugingenerator.ui:OFF
+  org.commontk.slicermodule:OFF
   )
 
 #-----------------------------------------------------------------------------

+ 3 - 0
Libs/ModuleDescription/CMakeLists.txt

@@ -20,10 +20,13 @@ SET(KIT_SRCS
   ctkModuleParameter.cpp
   ctkModuleParameterGroup.h
   ctkModuleParameterGroup.cpp
+  ctkModuleDescriptionReader.cpp
   )
 
 # Headers that should run through moc
 SET(KIT_MOC_SRCS
+  ctkModuleDescriptionReaderInterface.h
+  ctkModuleDescriptionReader.h
 )
 
 # UI files

+ 53 - 0
Libs/ModuleDescription/ctkModuleDescriptionReader.cpp

@@ -0,0 +1,53 @@
+/*=============================================================================
+
+Library: CTK
+
+Copyright (c) 2010 Brigham and Women's Hospital (BWH) All Rights Reserved.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+=============================================================================*/
+
+
+#include "ctkModuleDescriptionReader.h"
+
+ctkModuleDescriptionReader::ctkModuleDescriptionReader() 
+{
+
+}
+
+ctkModuleDescriptionReader::~ctkModuleDescriptionReader()
+{
+
+}
+
+QString ctkModuleDescriptionReader::xmlContent() const
+{
+  return this->XmlContent;
+}
+
+void ctkModuleDescriptionReader::setXml( QString val )
+{
+  this->XmlContent = val;
+}
+
+ctkModuleDescription ctkModuleDescriptionReader::moduleDescription() const
+{
+  return this->Description;
+}
+
+void ctkModuleDescriptionReader::setDescription( ctkModuleDescription val )
+{
+  this->Description = val;
+}
+

+ 57 - 0
Libs/ModuleDescription/ctkModuleDescriptionReader.h

@@ -0,0 +1,57 @@
+/*=============================================================================
+
+Library: CTK
+
+Copyright (c) 2010 Brigham and Women's Hospital (BWH) All Rights Reserved.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+=============================================================================*/
+
+#ifndef __ctkModuleDescriptionReader_h
+#define __ctkModuleDescriptionReader_h
+
+#include <QString>
+
+#include "CTKModuleDescriptionExport.h"
+#include "ctkModuleDescriptionReaderInterface.h"
+
+/** 
+ * \brief Base Reader of ModuleDescription
+ *
+ * This is the base interface
+ */
+class CTK_MODULDESC_EXPORT ctkModuleDescriptionReader :
+  virtual public ctkModuleDescriptionReaderInterface
+{
+  Q_OBJECT
+public:
+  ctkModuleDescriptionReader();
+  ~ctkModuleDescriptionReader();
+
+  //! xml is the content of the XML file
+  QString xmlContent() const;
+  void setXml(QString val);
+
+  //!
+  ctkModuleDescription moduleDescription() const;
+  void setDescription(ctkModuleDescription val);
+
+protected:
+  ///
+  QString XmlContent;
+  ///
+  ctkModuleDescription Description;
+};
+
+#endif

+ 50 - 0
Libs/ModuleDescription/ctkModuleDescriptionReaderInterface.h

@@ -0,0 +1,50 @@
+/*=============================================================================
+
+Library: CTK
+
+Copyright (c) 2010 Brigham and Women's Hospital (BWH) All Rights Reserved.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+=============================================================================*/
+
+#ifndef __ctkModuleDescriptionReaderInterface_h
+#define __ctkModuleDescriptionReaderInterface_h
+
+#include <QString>
+
+#include "CTKModuleDescriptionExport.h"
+#include "ctkModuleDescription.h"
+
+/** 
+ * \brief Interface of ModuleDescription reader
+ */
+class CTK_MODULDESC_EXPORT ctkModuleDescriptionReaderInterface : public QObject
+{
+    Q_OBJECT
+public:
+  ctkModuleDescriptionReaderInterface(){};
+  ~ctkModuleDescriptionReaderInterface(){};
+
+  //! xml is the content of the XML file
+  virtual void setXml(QString val) = 0;
+
+  //!
+  virtual ctkModuleDescription moduleDescription() const = 0;
+
+  //!
+  virtual void Update( ) = 0;
+
+};
+
+#endif

+ 50 - 0
Plugins/org.commontk.slicermodule/CMakeLists.txt

@@ -0,0 +1,50 @@
+PROJECT(org_commontk_slicermodule)
+
+SET(PLUGIN_export_directive "org_commontk_slicermodule_EXPORT")
+
+SET(PLUGIN_SRCS
+  ctkSlicerModulePlugin.cpp
+  ctkSlicerModuleReader.cxx
+)
+
+# Files which should be processed by Qts moc
+SET(PLUGIN_MOC_SRCS
+  ctkSlicerModulePlugin_p.h
+  ctkSlicerModuleReader.h
+)
+
+# Qt Designer files which should be processed by Qts uic
+SET(PLUGIN_UI_FORMS
+)
+
+# QRC Files which should be compiled into the plugin
+SET(PLUGIN_resources
+)
+
+
+# Update CTK_BASE_LIBRARIES with QT libraries
+IF(QT4_FOUND)
+  SET(CTK_BASE_LIBRARIES ${CTK_BASE_LIBRARIES} ${QT_LIBRARIES} CACHE INTERNAL "CTK base libraries" FORCE)
+ENDIF()
+
+#Compute the plugin dependencies
+ctkMacroGetTargetLibraries(PLUGIN_target_libraries)
+
+ctkMacroBuildPlugin(
+  NAME ${PROJECT_NAME}
+  EXPORT_DIRECTIVE ${PLUGIN_export_directive}
+  SRCS ${PLUGIN_SRCS}
+  MOC_SRCS ${PLUGIN_MOC_SRCS}
+  UI_FORMS ${PLUGIN_UI_FORMS}
+  RESOURCES ${PLUGIN_resources}
+  TARGET_LIBRARIES ${PLUGIN_target_libraries}
+)
+
+SET( QT_USE_QTXML ON )
+INCLUDE(${QT_USE_FILE})
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${QT_LIBRARIES})
+
+# Testing
+IF(BUILD_TESTING)
+  ADD_SUBDIRECTORY(Testing)
+ENDIF(BUILD_TESTING)

+ 1 - 0
Plugins/org.commontk.slicermodule/Testing/CMakeLists.txt

@@ -0,0 +1 @@
+ADD_SUBDIRECTORY(Cpp)

+ 29 - 0
Plugins/org.commontk.slicermodule/Testing/Cpp/CMakeLists.txt

@@ -0,0 +1,29 @@
+SET(KIT ${PROJECT_NAME})
+
+CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cpp
+  ctkSlicerModuleTest.cpp
+  )
+
+SET (TestsToRun ${Tests})
+REMOVE (TestsToRun ${KIT}CppTests.cpp)
+
+SET(LIBRARY_NAME ${PROJECT_NAME})
+
+ADD_EXECUTABLE(${KIT}CppTests ${Tests})
+TARGET_LINK_LIBRARIES(${KIT}CppTests ${LIBRARY_NAME} ${CTK_BASE_LIBRARIES})
+
+SET( KIT_TESTS ${CPP_TEST_PATH}/${KIT}CppTests)
+
+SET(TEST_DATA ${${PROJECT_NAME}_SOURCE_DIR}/TestData)
+
+MACRO( SIMPLE_TEST  TESTNAME DATA_FILE )
+  ADD_TEST( ${TESTNAME} "${KIT_TESTS}" "${TESTNAME}" "${TEST_DATA}/${DATA_FILE}" )
+  SET_PROPERTY(TEST ${TESTNAME} PROPERTY LABELS ${PROJECT_NAME})
+ENDMACRO( SIMPLE_TEST  )
+
+#
+# Add Tests
+#
+
+SIMPLE_TEST( ctkSlicerModuleTest ParserTest1.xml )
+

Libs/ModuleDescription/Testing/Cpp/TestData/ParserTest1.xml → Plugins/org.commontk.slicermodule/Testing/Cpp/TestData/ParserTest1.xml


Libs/ModuleDescription/Testing/Cpp/TestData/ParserTest2.xml → Plugins/org.commontk.slicermodule/Testing/Cpp/TestData/ParserTest2.xml


+ 109 - 0
Plugins/org.commontk.slicermodule/Testing/Cpp/ctkSlicerModuleTest.cpp

@@ -0,0 +1,109 @@
+/*=========================================================================
+
+  Library:   CTK
+ 
+  Copyright (c) 2010  CISTIB - Universitat Pompeu Fabra
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+ 
+=========================================================================*/
+
+// CTK includes
+#include "ctkModuleDescription.h"
+#include "ctkPluginFrameworkFactory.h"
+#include "ctkPluginFramework.h"
+#include "ctkPluginException.h"
+#include "ctkModuleDescriptionReaderInterface.h"
+#include "ctkCommandLineParser.h"
+
+// STD includes
+#include <iostream>
+
+#include "QFile"
+
+
+//-----------------------------------------------------------------------------
+int ctkSlicerModuleTest(int argc, char * argv [] )
+{
+
+  ctkCommandLineParser parser;
+  parser.addArgument("", "-F", QVariant::String);
+  bool ok = false;
+  QHash<QString, QVariant> parsedArgs = parser.parseArguments(argc, argv, &ok);
+  if (!ok)
+  {
+    std::cerr << qPrintable(parser.errorString()) << std::endl;
+    return EXIT_FAILURE;
+  }
+  QString xmlFileName = parsedArgs["-F"].toString();
+
+  ctkPluginFrameworkFactory fwFactory;
+  ctkPluginFramework* framework = fwFactory.getFramework();
+
+  try {
+    framework->init();
+  }
+  catch (const ctkPluginException& exc)
+  {
+    qCritical() << "Failed to initialize the plug-in framework:" << exc;
+    exit(1);
+  }
+
+
+#ifdef CMAKE_INTDIR
+  QString pluginPath = "/../plugins/" CMAKE_INTDIR "/";
+#else
+  QString pluginPath = "/plugins/";
+#endif
+
+  try
+  {
+    QString pluginLocation = "." + pluginPath + "liborg_commontk_slicermodule.dll";
+    ctkPlugin* plugin = framework->getPluginContext()->installPlugin(QUrl::fromLocalFile(pluginLocation));
+    plugin->start(ctkPlugin::START_TRANSIENT);
+
+    framework->start();
+
+    ctkServiceReference* serviceRef;
+    serviceRef = framework->getPluginContext()->getServiceReference( 
+      "ctkModuleDescriptionReaderInterface" );
+
+    ctkModuleDescriptionReaderInterface* reader;
+    reader = qobject_cast<ctkModuleDescriptionReaderInterface*>
+      (framework->getPluginContext()->getService(serviceRef));
+
+
+    // Read file
+    QFile file( xmlFileName );
+    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
+    {
+      std::cout << "XML file " << xmlFileName.toStdString( ) << " could not be opened." << endl;
+      return false;
+    }
+    QTextStream stream( &file );
+
+    // Parse XML file
+    reader->setXml( stream.readAll() );
+    reader->Update();
+
+    // Print module description
+    QTextStream(stdout) << reader->moduleDescription( );
+
+  }
+  catch (const ctkPluginException& e)
+  {
+    qCritical() << e.what();
+  }
+
+  return EXIT_SUCCESS;
+}

+ 70 - 0
Plugins/org.commontk.slicermodule/ctkSlicerModulePlugin.cpp

@@ -0,0 +1,70 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) 2010 CISTIB - Universitat Pompeu Fabra
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=============================================================================*/
+
+
+#include "ctkSlicerModulePlugin_p.h"
+#include "ctkSlicerModuleReader.h"
+#include <QtPlugin>
+#include <QDebug>
+
+ctkSlicerModulePlugin* ctkSlicerModulePlugin::instance = 0;
+
+ctkSlicerModulePlugin::ctkSlicerModulePlugin()
+  : context(0)
+{
+}
+
+ctkSlicerModulePlugin::~ctkSlicerModulePlugin()
+{
+  
+}
+
+void ctkSlicerModulePlugin::start(ctkPluginContext* context)
+{
+  instance = this;
+  this->context = context;
+
+  this->reader = new ctkSlicerModuleReader( );
+  context->registerService(QStringList("ctkModuleDescriptionReaderInterface"),
+    this->reader);
+
+  qDebug() << "Registered Slicer Module Description";
+}
+
+void ctkSlicerModulePlugin::stop(ctkPluginContext* context)
+{
+  delete this->reader;
+  this->reader = NULL;
+  Q_UNUSED(context)
+}
+
+ctkSlicerModulePlugin* ctkSlicerModulePlugin::getInstance()
+{
+  return instance;
+}
+
+ctkPluginContext* ctkSlicerModulePlugin::getPluginContext() const
+{
+  return context;
+}
+
+Q_EXPORT_PLUGIN2(org_commontk_slicermodule, ctkSlicerModulePlugin)
+
+

+ 57 - 0
Plugins/org.commontk.slicermodule/ctkSlicerModulePlugin_p.h

@@ -0,0 +1,57 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) 2010 CISTIB - Universitat Pompeu Fabra
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=============================================================================*/
+
+
+#ifndef CTKSLICERMODULEPLUGIN_P_H
+#define CTKSLICERMODULEPLUGIN_P_H
+
+#include <ctkPluginActivator.h>
+
+class ctkModuleDescriptionReaderInterface;
+
+class ctkSlicerModulePlugin :
+  public QObject, public ctkPluginActivator
+{
+  Q_OBJECT
+  Q_INTERFACES(ctkPluginActivator)
+
+public:
+
+  ctkSlicerModulePlugin();
+  ~ctkSlicerModulePlugin();
+
+  void start(ctkPluginContext* context);
+  void stop(ctkPluginContext* context);
+
+  static ctkSlicerModulePlugin* getInstance();
+
+  ctkPluginContext* getPluginContext() const;
+
+
+private:
+
+  static ctkSlicerModulePlugin* instance;
+  ctkPluginContext* context;
+  ctkModuleDescriptionReaderInterface* reader;
+
+
+}; // ctkSlicerModulePlugin
+
+#endif // CTKSLICERMODULEPLUGIN_P_H

+ 35 - 0
Plugins/org.commontk.slicermodule/ctkSlicerModuleReader.cxx

@@ -0,0 +1,35 @@
+/*=========================================================================
+
+  Copyright 2005 Brigham and Women's Hospital (BWH) All Rights Reserved.
+
+  See Doc/copyright/copyright.txt
+  or http://www.slicer.org/copyright/copyright.txt for details.
+
+  Program:   Module Description Parser
+  Module:    $HeadURL: http://svn.slicer.org/Slicer3/trunk/Libs/SlicerExecutionModel/ctkSlicerModuleReader/ctkSlicerModuleReader.cxx $
+  Date:      $Date: 2010-07-22 21:09:03 +0200 (jue, 22 jul 2010) $
+  Version:   $Revision: 14306 $
+
+==========================================================================*/
+#include "ctkSlicerModuleReader.h"
+#include <QtXml/QDomDocument>
+
+
+void ctkSlicerModuleReader::Update()
+{
+  QDomDocument domDocument;
+  QString errorStr;
+  int errorLine;
+  int errorColumn;
+
+  if (!domDocument.setContent(xmlContent(), &errorStr, &errorLine,
+    &errorColumn)) {
+      throw std::runtime_error( 
+        tr("Parse error at line %1, column %2:\n%3")
+        .arg(errorLine)
+        .arg(errorColumn)
+        .arg(errorStr).toStdString().c_str() );
+      return ;
+  }
+
+}

+ 46 - 0
Plugins/org.commontk.slicermodule/ctkSlicerModuleReader.h

@@ -0,0 +1,46 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) 2010 Brigham and Women's Hospital (BWH) All Rights Reserved.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=============================================================================*/
+
+#ifndef __ctkSlicerModuleReader_h
+#define __ctkSlicerModuleReader_h
+
+#include <string>
+
+#include "ctkModuleDescriptionReader.h"
+
+class ModuleDescription;
+class ModuleParameterGroup;
+
+
+/**
+ * Reader of Slicer Module XML description
+ *
+ */
+class ctkSlicerModuleReader : public ctkModuleDescriptionReader
+{
+  Q_OBJECT
+public:
+  ctkSlicerModuleReader() {};
+  ~ctkSlicerModuleReader() {};
+
+  void Update( );
+};
+
+#endif

+ 10 - 0
Plugins/org.commontk.slicermodule/target_libraries.cmake

@@ -0,0 +1,10 @@
+# See CMake/ctkMacroGetTargetLibraries.cmake
+#
+# This file should list the libraries required to build the current CTK plugin.
+# For specifying required plugins, see the manifest_headers.cmake file.
+#
+
+SET(target_libraries
+  CTKPluginFramework
+  CTKModuleDescription
+)