Ver código fonte

Add ctkITKErrorLogMessageHandler and associated test

Jean-Christophe Fillion-Robin 14 anos atrás
pai
commit
9e2f8ead1f

+ 1 - 0
CMakeLists.txt

@@ -303,6 +303,7 @@ SET(CTK_LIBS
   Widgets:OFF
   DICOM/Core:OFF
   DICOM/Widgets:OFF
+  ImageProcessing/ITK/Core:OFF
   #Messaging/Core:OFF  # MessagingCore library need some love :) - Let's disable it for now :(
   Scripting/Python/Core:OFF
   Scripting/Python/Widgets:OFF

+ 54 - 0
Libs/ImageProcessing/ITK/Core/CMakeLists.txt

@@ -0,0 +1,54 @@
+PROJECT(CTKImageProcessingITKCore)
+
+#
+# 3rd party dependencies
+#
+INCLUDE(${ITK_USE_FILE})
+
+#
+# See CTK/CMake/ctkMacroBuildLib.cmake for details
+#
+
+SET(KIT_export_directive "CTK_IMAGEPROCESSING_ITK_CORE_EXPORT")
+
+  
+# Source files
+SET(KIT_SRCS
+  ctkITKErrorLogMessageHandler.cpp
+  ctkITKErrorLogMessageHandler.h
+  )
+
+# Headers that should run through moc
+SET(KIT_MOC_SRCS
+  )
+
+# UI files
+SET(KIT_UI_FORMS
+)
+
+# Resources
+SET(KIT_resources
+)
+
+# Target libraries - See CMake/ctkFunctionGetTargetLibraries.cmake
+# The following macro will read the target libraries from the file 'target_libraries.cmake'
+ctkFunctionGetTargetLibraries(KIT_target_libraries)
+
+ctkMacroBuildLib(
+  NAME ${PROJECT_NAME}
+  EXPORT_DIRECTIVE ${KIT_export_directive}
+  SRCS ${KIT_SRCS}
+  MOC_SRCS ${KIT_MOC_SRCS}
+  UI_FORMS ${KIT_UI_FORMS}
+  TARGET_LIBRARIES ${KIT_target_libraries}
+  RESOURCES ${KIT_resources}
+  LIBRARY_TYPE ${CTK_LIBRARY_MODE}
+  )
+
+# Plugins
+#ADD_SUBDIRECTORY(Plugins)
+
+# Testing
+IF(BUILD_TESTING)
+  ADD_SUBDIRECTORY(Testing)
+ENDIF(BUILD_TESTING)

+ 1 - 0
Libs/ImageProcessing/ITK/Core/Testing/CMakeLists.txt

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

+ 77 - 0
Libs/ImageProcessing/ITK/Core/Testing/Cpp/CMakeLists.txt

@@ -0,0 +1,77 @@
+SET(KIT ${PROJECT_NAME})
+
+#
+# Tests
+#
+SET(TEST_SOURCES
+  ctkITKErrorLogModelTest1.cpp
+  )
+
+#
+# Tests expecting CTKData to be set
+#
+IF(EXISTS "${CTKData_DIR}")
+  LIST(APPEND TEST_SOURCES
+    )
+ENDIF()
+
+#
+# Pre-requisites
+#
+
+INCLUDE_DIRECTORIES(
+ ${CMAKE_CURRENT_SOURCE_DIR}
+ ${CMAKE_CURRENT_BINARY_DIR}
+ )
+
+#
+# Test helpers
+#
+
+#SET(KIT_HELPER_SRCS
+#  ctkVTKObjectTestHelper.cpp
+#  )
+
+#QT4_WRAP_CPP(KIT_HELPER_SRCS ctkVTKObjectTestHelper.h)
+
+#
+# Tests
+#
+
+CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cpp
+  ${TEST_SOURCES}
+  #EXTRA_INCLUDE TestingMacros.h
+  )
+
+SET (TestsToRun ${Tests})
+REMOVE (TestsToRun ${KIT}CppTests.cpp)
+
+SET(LIBRARY_NAME ${PROJECT_NAME})
+
+ADD_EXECUTABLE(${KIT}CppTests ${Tests} ${KIT_HELPER_SRCS})
+TARGET_LINK_LIBRARIES(${KIT}CppTests ${LIBRARY_NAME} ${CTK_BASE_LIBRARIES})
+
+SET( KIT_TESTS ${CPP_TEST_PATH}/${KIT}CppTests)
+
+MACRO( SIMPLE_TEST  TESTNAME )
+  ADD_TEST( ${TESTNAME} ${KIT_TESTS} ${TESTNAME}
+            -D ${CTKData_DIR}/Data
+            -V ${CTKData_DIR}/Baseline/Libs/Visualization/VTK/Core/${TESTNAME}.png
+            -T ${CTK_BINARY_DIR}/Testing/Temporary
+            )
+  SET_PROPERTY(TEST ${TESTNAME} PROPERTY LABELS ${PROJECT_NAME})
+ENDMACRO( SIMPLE_TEST  )
+
+#
+# Add Tests
+#
+
+#SIMPLE_TEST( ctkVTKObjectTest1 )
+
+#
+# Add Tests expecting CTKData to be set
+#
+IF(EXISTS "${CTKData_DIR}")
+  SIMPLE_TEST( ctkITKErrorLogModelTest1 )
+ENDIF()
+

+ 171 - 0
Libs/ImageProcessing/ITK/Core/Testing/Cpp/ctkITKErrorLogModelTest1.cpp

@@ -0,0 +1,171 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  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.
+
+=========================================================================*/
+
+// Qt includes
+#include <QCoreApplication>
+#include <QDebug>
+#include <QStringList>
+
+#ifdef __GNUC__
+// Disable warnings related to 'itkSmartPointer.h' file
+// See http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
+// Note: Ideally the incriminated functions and macros should be fixed upstream ...
+# pragma GCC diagnostic ignored "-Wold-style-cast"
+#endif
+
+// CTK includes
+#include "ctkErrorLogModel.h"
+#include "ctkITKErrorLogMessageHandler.h"
+#include "ctkModelTester.h"
+
+// ITK includes
+#include <itkOutputWindow.h>
+
+// STL includes
+#include <cstdlib>
+#include <iostream>
+
+namespace
+{
+//-----------------------------------------------------------------------------
+// Utility function
+
+//-----------------------------------------------------------------------------
+QString checkRowCount(int line, int currentRowCount, int expectedRowCount)
+{
+  if (currentRowCount != expectedRowCount)
+    {
+    QString errorMsg("Line %1 - Expected rowCount: %2 - Current rowCount: %3\n");
+    return errorMsg.arg(line).arg(expectedRowCount).arg(currentRowCount);
+    }
+  return QString();
+}
+
+//-----------------------------------------------------------------------------
+QString checkTextMessages(int line, const ctkErrorLogModel& model, const QStringList& expectedMessages)
+{
+  for(int i=0; i < expectedMessages.count(); ++i)
+    {
+    QModelIndex descriptionIndex = model.index(i, ctkErrorLogModel::DescriptionColumn);
+    QString currentMessage = descriptionIndex.data(ctkErrorLogModel::DescriptionTextRole).toString();
+    if (currentMessage.compare(expectedMessages.value(i)) != 0)
+      {
+      QString errorMsg("Line %1 - Problem with row%2 !\n"
+                       "\tExpected message [%3]\n"
+                       "\tCurrent message [%4]\n");
+      return errorMsg.arg(line).arg(i).arg(expectedMessages.value(i)).arg(currentMessage);
+      }
+    }
+  return QString();
+}
+
+//-----------------------------------------------------------------------------
+void printErrorMessage(const QString& errorMessage)
+{
+  fprintf(stderr, "%s", qPrintable(errorMessage));
+  fflush(stderr);
+}
+
+} // end namespace
+
+//-----------------------------------------------------------------------------
+int ctkITKErrorLogModelTest1(int argc, char * argv [])
+{
+  QCoreApplication app(argc, argv);
+  Q_UNUSED(app);
+
+  ctkErrorLogModel model;
+  ctkModelTester modelTester;
+  modelTester.setVerbose(false);
+  QString errorMsg;
+
+  try
+    {
+    modelTester.setModel(&model);
+
+    // --------------------------------------------------------------------------
+    // Monitor ITK messages
+
+    model.registerMsgHandler(new ctkITKErrorLogMessageHandler);
+    model.setMsgHandlerEnabled(ctkITKErrorLogMessageHandler::HandlerName, true);
+
+    errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ 0);
+    if (!errorMsg.isEmpty())
+      {
+      model.disableAllMsgHandler();
+      printErrorMessage(errorMsg);
+      return EXIT_FAILURE;
+      }
+
+    QString itkMessage0("This is a ITK debug message");
+    itk::OutputWindowDisplayDebugText(qPrintable(itkMessage0));
+
+    QString itkMessage1("This is a ITK warning message");
+    itk::OutputWindowDisplayWarningText(qPrintable(itkMessage1));
+
+    QString itkMessage2("This is a ITK error message");
+    itk::OutputWindowDisplayErrorText(qPrintable(itkMessage2));
+
+    QStringList expectedITKMessages;
+    expectedITKMessages << itkMessage0 << itkMessage1 << itkMessage2;
+
+    errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ expectedITKMessages.count());
+    if (!errorMsg.isEmpty())
+      {
+      model.disableAllMsgHandler();
+      printErrorMessage(errorMsg);
+      return EXIT_FAILURE;
+      }
+
+    errorMsg = checkTextMessages(__LINE__, model, expectedITKMessages);
+    if (!errorMsg.isEmpty())
+      {
+      model.disableAllMsgHandler();
+      printErrorMessage(errorMsg);
+      return EXIT_FAILURE;
+      }
+
+    // Clear
+    model.clear();
+
+    // Disable ITK messages monitoring
+    model.setMsgHandlerEnabled(ctkITKErrorLogMessageHandler::HandlerName, false);
+
+    itk::OutputWindowDisplayDebugText("This ITK debug message should appear in the console");
+    itk::OutputWindowDisplayWarningText("This ITK warning message should appear in the console");
+    itk::OutputWindowDisplayErrorText("This ITK error message should appear in the console");
+
+    errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ 0);
+    if (!errorMsg.isEmpty())
+      {
+      model.disableAllMsgHandler();
+      printErrorMessage(errorMsg);
+      return EXIT_FAILURE;
+      }
+    }
+  catch (const char* error)
+    {
+    model.disableAllMsgHandler();
+    std::cerr << error << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  return EXIT_SUCCESS;
+}

+ 186 - 0
Libs/ImageProcessing/ITK/Core/ctkITKErrorLogMessageHandler.cpp

@@ -0,0 +1,186 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  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 "ctkITKErrorLogMessageHandler.h"
+
+#ifdef __GNUC__
+// Disable warnings related to 'itkSmartPointer.h' file
+// See http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
+// Note: Ideally the incriminated functions and macros should be fixed upstream ...
+# pragma GCC diagnostic ignored "-Wold-style-cast"
+#endif
+
+// ITK includes
+#include <itkObjectFactory.h>
+#include <itkOutputWindow.h>
+
+namespace itk
+{
+
+// --------------------------------------------------------------------------
+// ctkITKOutputWindow
+
+// --------------------------------------------------------------------------
+class ctkITKOutputWindow : public OutputWindow
+{
+public:
+  /** Standard class typedefs. */
+  typedef ctkITKOutputWindow       Self;
+  typedef OutputWindow             Superclass;
+  typedef SmartPointer<Self>       Pointer;
+  typedef SmartPointer<const Self> ConstPointer;
+
+  /** Standard New method. */
+  itkNewMacro(ctkITKOutputWindow);
+
+  /** Run-time type information (and related methods). */
+  itkTypeMacro(ctkITKOutputWindow, OutputWindow);
+
+  ctkITKOutputWindow():MessageHandler(0){}
+  ~ctkITKOutputWindow(){}
+
+  virtual void DisplayText(const char*);
+  virtual void DisplayErrorText(const char*);
+  virtual void DisplayWarningText(const char*);
+  virtual void DisplayGenericWarningText(const char*);
+
+  virtual void DisplayDebugText(const char*);
+
+  ctkErrorLogAbstractMessageHandler * MessageHandler;
+};
+
+// --------------------------------------------------------------------------
+// ctkITKOutputWindow methods
+
+//----------------------------------------------------------------------------
+void ctkITKOutputWindow::DisplayText(const char* text)
+{
+  this->MessageHandler->errorLogModel()->addEntry(
+        ctkErrorLogModel::Info, this->MessageHandler->handlerPrettyName(), text);
+}
+
+//----------------------------------------------------------------------------
+void ctkITKOutputWindow::DisplayErrorText(const char* text)
+{
+  this->MessageHandler->errorLogModel()->addEntry(
+        ctkErrorLogModel::Error, this->MessageHandler->handlerPrettyName(), text);
+}
+
+//----------------------------------------------------------------------------
+void ctkITKOutputWindow::DisplayWarningText(const char* text)
+{
+  this->MessageHandler->errorLogModel()->addEntry(
+        ctkErrorLogModel::Warning, this->MessageHandler->handlerPrettyName(), text);
+}
+
+//----------------------------------------------------------------------------
+void ctkITKOutputWindow::DisplayGenericWarningText(const char* text)
+{
+  this->DisplayWarningText(text);
+}
+
+//----------------------------------------------------------------------------
+void ctkITKOutputWindow::DisplayDebugText(const char* text)
+{
+  this->MessageHandler->errorLogModel()->addEntry(
+        ctkErrorLogModel::Debug, this->MessageHandler->handlerPrettyName(), text);
+}
+
+} // End of itk namespace
+
+// --------------------------------------------------------------------------
+// ctkITKErrorLogMessageHandlerPrivate
+
+// --------------------------------------------------------------------------
+class ctkITKErrorLogMessageHandlerPrivate
+{
+  Q_DECLARE_PUBLIC(ctkITKErrorLogMessageHandler);
+protected:
+  ctkITKErrorLogMessageHandler* const q_ptr;
+public:
+  ctkITKErrorLogMessageHandlerPrivate(ctkITKErrorLogMessageHandler& object);
+  ~ctkITKErrorLogMessageHandlerPrivate();
+
+  itk::OutputWindow::Pointer SavedITKOutputWindow;
+  itk::ctkITKOutputWindow::Pointer CTKITKOutputWindow;
+
+};
+
+// --------------------------------------------------------------------------
+// ctkITKErrorLogMessageHandlerPrivate methods
+
+// --------------------------------------------------------------------------
+ctkITKErrorLogMessageHandlerPrivate::
+ctkITKErrorLogMessageHandlerPrivate(ctkITKErrorLogMessageHandler& object) : q_ptr(&object)
+{
+  Q_Q(ctkITKErrorLogMessageHandler);
+  this->SavedITKOutputWindow = 0;
+  this->CTKITKOutputWindow = itk::ctkITKOutputWindow::New();
+  this->CTKITKOutputWindow->MessageHandler = q;
+}
+
+// --------------------------------------------------------------------------
+ctkITKErrorLogMessageHandlerPrivate::~ctkITKErrorLogMessageHandlerPrivate()
+{
+  this->SavedITKOutputWindow = 0;
+  this->CTKITKOutputWindow = 0;
+}
+
+// --------------------------------------------------------------------------
+// ctkITKErrorLogMessageHandler methods
+
+// --------------------------------------------------------------------------
+QString ctkITKErrorLogMessageHandler::HandlerName = QLatin1String("ITK");
+
+//----------------------------------------------------------------------------
+ctkITKErrorLogMessageHandler::ctkITKErrorLogMessageHandler() :
+  Superclass(), d_ptr(new ctkITKErrorLogMessageHandlerPrivate(*this))
+{
+}
+
+//----------------------------------------------------------------------------
+ctkITKErrorLogMessageHandler::~ctkITKErrorLogMessageHandler()
+{
+}
+
+//----------------------------------------------------------------------------
+QString ctkITKErrorLogMessageHandler::handlerName()const
+{
+  return ctkITKErrorLogMessageHandler::HandlerName;
+}
+
+//----------------------------------------------------------------------------
+void ctkITKErrorLogMessageHandler::setEnabledInternal(bool value)
+{
+  Q_D(ctkITKErrorLogMessageHandler);
+  if (value)
+    {
+    d->SavedITKOutputWindow = itk::OutputWindow::GetInstance();
+    itk::OutputWindow::SetInstance(d->CTKITKOutputWindow);
+    }
+  else
+    {
+    Q_ASSERT(d->SavedITKOutputWindow.IsNotNull());
+    itk::OutputWindow::SetInstance(d->SavedITKOutputWindow);
+    d->SavedITKOutputWindow = 0;
+    }
+}
+

+ 58 - 0
Libs/ImageProcessing/ITK/Core/ctkITKErrorLogMessageHandler.h

@@ -0,0 +1,58 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  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.
+
+=========================================================================*/
+
+#ifndef __ctkITKErrorLogMessageHandler_h
+#define __ctkITKErrorLogMessageHandler_h
+
+// Qt includes
+#include <QtGlobal>
+#include <QScopedPointer>
+
+// CTK includes
+#include <ctkErrorLogModel.h>
+#include "ctkImageProcessingITKCoreExport.h"
+
+class ctkITKErrorLogMessageHandlerPrivate;
+
+class CTK_IMAGEPROCESSING_ITK_CORE_EXPORT ctkITKErrorLogMessageHandler :
+    public ctkErrorLogAbstractMessageHandler
+{
+public:
+  typedef ctkErrorLogAbstractMessageHandler Superclass;
+
+  ctkITKErrorLogMessageHandler();
+  virtual ~ctkITKErrorLogMessageHandler();
+
+  static QString HandlerName;
+
+  virtual QString handlerName()const;
+
+  virtual void setEnabledInternal(bool value);
+
+protected:
+  QScopedPointer<ctkITKErrorLogMessageHandlerPrivate> d_ptr;
+
+private:
+  Q_DECLARE_PRIVATE(ctkITKErrorLogMessageHandler);
+  Q_DISABLE_COPY(ctkITKErrorLogMessageHandler);
+
+};
+
+#endif

+ 10 - 0
Libs/ImageProcessing/ITK/Core/target_libraries.cmake

@@ -0,0 +1,10 @@
+#
+# See CMake/ctkFunctionGetTargetLibraries.cmake
+# 
+# This file should list the libraries required to build the current CTK libraries
+#
+
+SET(target_libraries
+  ITK_LIBRARIES
+  CTKCore
+  )