ctkVTKErrorLogMessageHandler.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  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. // CTK includes
  15. #include "ctkVTKErrorLogMessageHandler.h"
  16. // VTK includes
  17. #include <vtkObjectFactory.h>
  18. #include <vtkOutputWindow.h>
  19. namespace {
  20. // --------------------------------------------------------------------------
  21. // ctkVTKOutputWindow
  22. // --------------------------------------------------------------------------
  23. class ctkVTKOutputWindow : public vtkOutputWindow
  24. {
  25. public:
  26. static ctkVTKOutputWindow *New();
  27. vtkTypeMacro(ctkVTKOutputWindow,vtkOutputWindow);
  28. void PrintSelf(ostream& os, vtkIndent indent);
  29. ctkVTKOutputWindow():MessageHandler(0){}
  30. ~ctkVTKOutputWindow(){}
  31. virtual void DisplayText(const char*);
  32. virtual void DisplayErrorText(const char*);
  33. virtual void DisplayWarningText(const char*);
  34. virtual void DisplayGenericWarningText(const char*);
  35. virtual void DisplayDebugText(const char*);
  36. ctkErrorLogAbstractMessageHandler * MessageHandler;
  37. };
  38. // --------------------------------------------------------------------------
  39. // ctkVTKOutputWindow methods
  40. // --------------------------------------------------------------------------
  41. vtkStandardNewMacro(ctkVTKOutputWindow);
  42. //----------------------------------------------------------------------------
  43. void ctkVTKOutputWindow::PrintSelf(ostream& os, vtkIndent indent)
  44. {
  45. this->Superclass::PrintSelf(os,indent);
  46. }
  47. //----------------------------------------------------------------------------
  48. void ctkVTKOutputWindow::DisplayText(const char* text)
  49. {
  50. this->MessageHandler->errorLogModel()->addEntry(
  51. ctkErrorLogModel::Info, this->MessageHandler->handlerPrettyName(), text);
  52. }
  53. //----------------------------------------------------------------------------
  54. void ctkVTKOutputWindow::DisplayErrorText(const char* text)
  55. {
  56. this->MessageHandler->errorLogModel()->addEntry(
  57. ctkErrorLogModel::Error, this->MessageHandler->handlerPrettyName(), text);
  58. }
  59. //----------------------------------------------------------------------------
  60. void ctkVTKOutputWindow::DisplayWarningText(const char* text)
  61. {
  62. this->MessageHandler->errorLogModel()->addEntry(
  63. ctkErrorLogModel::Warning, this->MessageHandler->handlerPrettyName(), text);
  64. }
  65. //----------------------------------------------------------------------------
  66. void ctkVTKOutputWindow::DisplayGenericWarningText(const char* text)
  67. {
  68. this->DisplayWarningText(text);
  69. }
  70. //----------------------------------------------------------------------------
  71. void ctkVTKOutputWindow::DisplayDebugText(const char* text)
  72. {
  73. this->MessageHandler->errorLogModel()->addEntry(
  74. ctkErrorLogModel::Debug, this->MessageHandler->handlerPrettyName(), text);
  75. }
  76. } // End of anonymous namespace
  77. // --------------------------------------------------------------------------
  78. // ctkVTKErrorLogMessageHandlerPrivate
  79. // --------------------------------------------------------------------------
  80. class ctkVTKErrorLogMessageHandlerPrivate
  81. {
  82. Q_DECLARE_PUBLIC(ctkVTKErrorLogMessageHandler);
  83. protected:
  84. ctkVTKErrorLogMessageHandler* const q_ptr;
  85. public:
  86. ctkVTKErrorLogMessageHandlerPrivate(ctkVTKErrorLogMessageHandler& object);
  87. ~ctkVTKErrorLogMessageHandlerPrivate();
  88. vtkOutputWindow * SavedVTKOutputWindow;
  89. ctkVTKOutputWindow * CTKVTKOutputWindow;
  90. };
  91. // --------------------------------------------------------------------------
  92. // ctkVTKErrorLogMessageHandlerPrivate methods
  93. // --------------------------------------------------------------------------
  94. ctkVTKErrorLogMessageHandlerPrivate::
  95. ctkVTKErrorLogMessageHandlerPrivate(ctkVTKErrorLogMessageHandler& object) : q_ptr(&object)
  96. {
  97. Q_Q(ctkVTKErrorLogMessageHandler);
  98. this->SavedVTKOutputWindow = 0;
  99. this->CTKVTKOutputWindow = ctkVTKOutputWindow::New();
  100. this->CTKVTKOutputWindow->MessageHandler = q;
  101. }
  102. // --------------------------------------------------------------------------
  103. ctkVTKErrorLogMessageHandlerPrivate::~ctkVTKErrorLogMessageHandlerPrivate()
  104. {
  105. if (this->SavedVTKOutputWindow)
  106. {
  107. this->SavedVTKOutputWindow->Delete();
  108. }
  109. this->CTKVTKOutputWindow->Delete();
  110. }
  111. // --------------------------------------------------------------------------
  112. // ctkVTKErrorLogMessageHandler methods
  113. // --------------------------------------------------------------------------
  114. QString ctkVTKErrorLogMessageHandler::HandlerName = QLatin1String("VTK");
  115. //----------------------------------------------------------------------------
  116. ctkVTKErrorLogMessageHandler::ctkVTKErrorLogMessageHandler() :
  117. Superclass(), d_ptr(new ctkVTKErrorLogMessageHandlerPrivate(*this))
  118. {
  119. }
  120. //----------------------------------------------------------------------------
  121. ctkVTKErrorLogMessageHandler::~ctkVTKErrorLogMessageHandler()
  122. {
  123. }
  124. //----------------------------------------------------------------------------
  125. QString ctkVTKErrorLogMessageHandler::handlerName()const
  126. {
  127. return ctkVTKErrorLogMessageHandler::HandlerName;
  128. }
  129. //----------------------------------------------------------------------------
  130. void ctkVTKErrorLogMessageHandler::setEnabledInternal(bool value)
  131. {
  132. Q_D(ctkVTKErrorLogMessageHandler);
  133. if (value)
  134. {
  135. d->SavedVTKOutputWindow = vtkOutputWindow::GetInstance();
  136. d->SavedVTKOutputWindow->Register(0);
  137. vtkOutputWindow::SetInstance(d->CTKVTKOutputWindow);
  138. }
  139. else
  140. {
  141. Q_ASSERT(d->SavedVTKOutputWindow);
  142. vtkOutputWindow::SetInstance(d->SavedVTKOutputWindow);
  143. d->SavedVTKOutputWindow->Delete();
  144. d->SavedVTKOutputWindow = 0;
  145. }
  146. }