Przeglądaj źródła

ENH: Added ctkVTKRenderView and its corresponding plugin in Visualization/VTK/Widgets

Note that this is work in progress. This widget should mature in the coming week
with the port of slicer to Qt.
Jean-Christophe Fillion-Robin 15 lat temu
rodzic
commit
70cefac990

+ 6 - 1
Libs/Visualization/VTK/Widgets/CMakeLists.txt

@@ -28,12 +28,17 @@ SET(KIT_SRCS
   ctkVTKAbstractMatrixWidget_p.h
   ctkVTKMatrixWidget.cpp
   ctkVTKMatrixWidget.h
+  ctkVTKRenderView.cpp
+  ctkVTKRenderView.h
+  ctkVTKRenderView_p.h
   )
 
 # Headers that should run through moc
 SET(KIT_MOC_SRCS
   ctkVTKAbstractMatrixWidget_p.h
   ctkVTKMatrixWidget.h
+  ctkVTKRenderView.h
+  ctkVTKRenderView_p.h
   )
 
 # UI files
@@ -66,7 +71,7 @@ ctkMacroBuildLib(
   )
 
 # Plugins
-#ADD_SUBDIRECTORY(Plugins)
+ADD_SUBDIRECTORY(Plugins)
 
 # Testing
 IF(BUILD_TESTING)

+ 44 - 0
Libs/Visualization/VTK/Widgets/Plugins/CMakeLists.txt

@@ -0,0 +1,44 @@
+PROJECT(${PROJECT_NAME}Plugins)
+
+#
+# See CTK/CMake/ctkMacroBuildQtDesignerPlugin.cmake for details
+#
+
+SET(PLUGIN_export_directive "CTK_VISUALIZATION_VTK_WIDGETS_PLUGINS_EXPORT")
+
+# Source files
+SET(PLUGIN_SRCS
+  ctkVTKWidgetsPlugins.cpp
+  ctkVTKWidgetsPlugins.h
+  ctkVTKWidgetsAbstractPlugin.cpp
+  ctkVTKWidgetsAbstractPlugin.h
+
+  ctkVTKRenderViewPlugin.cpp
+  ctkVTKRenderViewPlugin.h
+  )
+
+# Headers that should run through moc
+SET(PLUGIN_MOC_SRCS
+  ctkVTKWidgetsPlugins.h
+
+  ctkVTKRenderViewPlugin.h
+  )
+
+# Resources
+SET(PLUGIN_resources
+  #Resources/CTKVisualizationVTKWidgetsPlugins.qrc
+)
+
+# Target libraries
+SET(PLUGIN_target_libraries
+  CTKWidgets
+  )
+
+ctkMacroBuildQtDesignerPlugin(
+  NAME ${PROJECT_NAME}
+  EXPORT_DIRECTIVE ${PLUGIN_export_directive}
+  SRCS ${PLUGIN_SRCS}
+  MOC_SRCS ${PLUGIN_MOC_SRCS}
+  RESOURCES ${PLUGIN_resources}
+  TARGET_LIBRARIES ${PLUGIN_target_libraries}
+)

+ 63 - 0
Libs/Visualization/VTK/Widgets/Plugins/ctkVTKRenderViewPlugin.cpp

@@ -0,0 +1,63 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc. 
+  All rights reserved.
+  Distributed under a BSD License. See LICENSE.txt file.
+
+  This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+  See the above copyright notice for more information.
+
+=========================================================================*/
+
+// CTK includes
+#include "ctkVTKRenderViewPlugin.h"
+#include "ctkVTKRenderView.h"
+
+//-----------------------------------------------------------------------------
+ctkVTKRenderViewPlugin::ctkVTKRenderViewPlugin(QObject *_parent):QObject(_parent)
+{
+}
+
+//-----------------------------------------------------------------------------
+QWidget *ctkVTKRenderViewPlugin::createWidget(QWidget *_parent)
+{
+  ctkVTKRenderView* _widget = new ctkVTKRenderView(_parent);
+  return _widget;
+}
+
+//-----------------------------------------------------------------------------
+QString ctkVTKRenderViewPlugin::domXml() const
+{
+  return "<widget class=\"ctkVTKRenderView\" \
+          name=\"VTKRenderView\">\n"
+          " <property name=\"geometry\">\n"
+          "  <rect>\n"
+          "   <x>0</x>\n"
+          "   <y>0</y>\n"
+          "   <width>200</width>\n"
+          "   <height>200</height>\n"
+          "  </rect>\n"
+          " </property>\n"
+          "</widget>\n";
+}
+
+//-----------------------------------------------------------------------------
+QString ctkVTKRenderViewPlugin::includeFile() const
+{
+  return "ctkVTKRenderView.h";
+}
+
+//-----------------------------------------------------------------------------
+bool ctkVTKRenderViewPlugin::isContainer() const
+{
+  return false;
+}
+
+//-----------------------------------------------------------------------------
+QString ctkVTKRenderViewPlugin::name() const
+{
+  return "ctkVTKRenderView";
+}

+ 38 - 0
Libs/Visualization/VTK/Widgets/Plugins/ctkVTKRenderViewPlugin.h

@@ -0,0 +1,38 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc. 
+  All rights reserved.
+  Distributed under a BSD License. See LICENSE.txt file.
+
+  This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+  See the above copyright notice for more information.
+
+=========================================================================*/
+
+#ifndef __ctkVTKRenderViewPlugin_h
+#define __ctkVTKRenderViewPlugin_h
+
+// CTK includes
+#include "ctkVTKWidgetsAbstractPlugin.h"
+
+class CTK_VISUALIZATION_VTK_WIDGETS_PLUGINS_EXPORT ctkVTKRenderViewPlugin :
+  public QObject,
+  public ctkVTKWidgetsAbstractPlugin
+{
+  Q_OBJECT
+
+public:
+  ctkVTKRenderViewPlugin(QObject *_parent = 0);
+  
+  QWidget *createWidget(QWidget *_parent);
+  QString domXml() const;
+  QString includeFile() const;
+  bool isContainer() const;
+  QString name() const;
+  
+};
+
+#endif

+ 57 - 0
Libs/Visualization/VTK/Widgets/Plugins/ctkVTKWidgetsAbstractPlugin.cpp

@@ -0,0 +1,57 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc. 
+  All rights reserved.
+  Distributed under a BSD License. See LICENSE.txt file.
+
+  This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+  See the above copyright notice for more information.
+
+=========================================================================*/
+
+// CTK includes
+#include "ctkVTKWidgetsAbstractPlugin.h"
+
+//-----------------------------------------------------------------------------
+ctkVTKWidgetsAbstractPlugin::ctkVTKWidgetsAbstractPlugin()
+{
+  this->Initialized = false;
+}
+
+//-----------------------------------------------------------------------------
+QString ctkVTKWidgetsAbstractPlugin::group() const
+{ 
+  return "CTK [VTK Widgets]";
+}
+
+//-----------------------------------------------------------------------------
+QIcon ctkVTKWidgetsAbstractPlugin::icon() const
+{
+  return QIcon(); 
+}
+
+//-----------------------------------------------------------------------------
+QString ctkVTKWidgetsAbstractPlugin::toolTip() const
+{ 
+  return QString(); 
+}
+
+//-----------------------------------------------------------------------------
+QString ctkVTKWidgetsAbstractPlugin::whatsThis() const
+{
+  return QString(); 
+}
+
+//-----------------------------------------------------------------------------
+void ctkVTKWidgetsAbstractPlugin::initialize(QDesignerFormEditorInterface *formEditor)
+{
+  Q_UNUSED(formEditor);
+  if (this->Initialized)
+    {
+    return;
+    }
+  this->Initialized = true;
+}

+ 45 - 0
Libs/Visualization/VTK/Widgets/Plugins/ctkVTKWidgetsAbstractPlugin.h

@@ -0,0 +1,45 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc. 
+  All rights reserved.
+  Distributed under a BSD License. See LICENSE.txt file.
+
+  This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+  See the above copyright notice for more information.
+
+=========================================================================*/
+
+#ifndef __ctkVTKWidgetsAbstractPlugin_h
+#define __ctkVTKWidgetsAbstractPlugin_h
+
+// Qt includes
+#include <QDesignerCustomWidgetInterface>
+
+// CTK includes
+#include "CTKVisualizationVTKWidgetsPluginsExport.h"
+
+class CTK_VISUALIZATION_VTK_WIDGETS_PLUGINS_EXPORT ctkVTKWidgetsAbstractPlugin :
+  public QDesignerCustomWidgetInterface
+{
+  Q_INTERFACES(QDesignerCustomWidgetInterface);
+public:
+
+  ctkVTKWidgetsAbstractPlugin();
+  
+  // Do *NOT* reimplement this method.
+  QString group() const;
+  
+  // You can reimplement these methods
+  virtual QIcon icon() const;
+  virtual QString toolTip() const;
+  virtual QString whatsThis() const;
+  virtual void initialize(QDesignerFormEditorInterface *formEditor);
+  
+protected:
+  bool Initialized;
+};
+
+#endif

+ 21 - 0
Libs/Visualization/VTK/Widgets/Plugins/ctkVTKWidgetsPlugins.cpp

@@ -0,0 +1,21 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc. 
+  All rights reserved.
+  Distributed under a BSD License. See LICENSE.txt file.
+
+  This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+  See the above copyright notice for more information.
+
+=========================================================================*/
+
+// Qt includes
+#include <QtPlugin>
+
+// CTK includes
+#include "ctkVTKWidgetsPlugins.h"
+
+Q_EXPORT_PLUGIN2(customwidgetplugin, ctkVTKWidgetsPlugins);

+ 43 - 0
Libs/Visualization/VTK/Widgets/Plugins/ctkVTKWidgetsPlugins.h

@@ -0,0 +1,43 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc. 
+  All rights reserved.
+  Distributed under a BSD License. See LICENSE.txt file.
+
+  This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+  See the above copyright notice for more information.
+
+=========================================================================*/
+
+#ifndef __ctkVTKWidgetsPlugins_h
+#define __ctkVTKWidgetsPlugins_h
+
+// Qt includes
+#include <QDesignerCustomWidgetCollectionInterface>
+
+// CTK includes
+#include "CTKVisualizationVTKWidgetsPluginsExport.h"
+#include "ctkVTKRenderViewPlugin.h"
+
+
+/// \class Group the plugins in one library
+class CTK_VISUALIZATION_VTK_WIDGETS_PLUGINS_EXPORT ctkVTKWidgetsPlugins :
+  public QObject,
+  public QDesignerCustomWidgetCollectionInterface
+{
+  Q_OBJECT
+  Q_INTERFACES(QDesignerCustomWidgetCollectionInterface);
+
+public:
+  QList<QDesignerCustomWidgetInterface*> customWidgets() const
+    {
+    QList<QDesignerCustomWidgetInterface *> plugins;
+    plugins << new ctkVTKRenderViewPlugin;
+    return plugins;
+    }
+};
+
+#endif

+ 161 - 0
Libs/Visualization/VTK/Widgets/ctkVTKRenderView.cpp

@@ -0,0 +1,161 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc. 
+  All rights reserved.
+  Distributed under a BSD License. See LICENSE.txt file.
+
+  This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+  See the above copyright notice for more information.
+
+=========================================================================*/
+
+// Qt includes
+#include <QTimer>
+#include <QVBoxLayout>
+
+// CTK includes
+#include "ctkVTKRenderView.h"
+#include "ctkVTKRenderView_p.h"
+
+// VTK includes
+#include <vtkRendererCollection.h>
+#include <vtkRenderWindowInteractor.h>
+#include <vtkTextProperty.h>
+
+// --------------------------------------------------------------------------
+// ctkVTKRenderViewPrivate methods
+
+// --------------------------------------------------------------------------
+ctkVTKRenderViewPrivate::ctkVTKRenderViewPrivate()
+{
+  this->Renderer = vtkSmartPointer<vtkRenderer>::New();
+  this->RenderWindow = vtkSmartPointer<vtkRenderWindow>::New();
+  this->Axes = vtkSmartPointer<vtkAxesActor>::New();
+  this->Orientation = vtkSmartPointer<vtkOrientationMarkerWidget>::New();
+  this->CornerAnnotation = vtkSmartPointer<vtkCornerAnnotation>::New();
+  this->RenderPending = false;
+}
+
+// --------------------------------------------------------------------------
+void ctkVTKRenderViewPrivate::setupCornerAnnotation()
+{
+  if (!this->Renderer->HasViewProp(this->CornerAnnotation))
+    {
+    this->Renderer->AddViewProp(this->CornerAnnotation);
+    this->CornerAnnotation->SetMaximumLineHeight(0.07);
+    vtkTextProperty *tprop = this->CornerAnnotation->GetTextProperty();
+    tprop->ShadowOn();
+    }
+  this->CornerAnnotation->ClearAllTexts();
+}
+
+//---------------------------------------------------------------------------
+void ctkVTKRenderViewPrivate::setupRendering()
+{
+  Q_ASSERT(this->RenderWindow);
+  this->RenderWindow->SetAlphaBitPlanes(1);
+  this->RenderWindow->SetMultiSamples(0);
+  this->RenderWindow->StereoCapableWindowOn();
+  
+  this->RenderWindow->GetRenderers()->RemoveAllItems();
+  
+  // Add renderer
+  this->RenderWindow->AddRenderer(this->Renderer);
+  
+  // Setup the corner annotation
+  this->setupCornerAnnotation();
+
+  this->VTKWidget->SetRenderWindow(this->RenderWindow);
+}
+
+//---------------------------------------------------------------------------
+void ctkVTKRenderViewPrivate::setupDefaultInteractor()
+{
+  CTK_P(ctkVTKRenderView);
+  p->setInteractor(this->RenderWindow->GetInteractor());
+}
+
+//---------------------------------------------------------------------------
+// ctkVTKRenderView methods
+
+// --------------------------------------------------------------------------
+ctkVTKRenderView::ctkVTKRenderView(QWidget* _parent) : Superclass(_parent)
+{
+  CTK_INIT_PRIVATE(ctkVTKRenderView);
+  CTK_D(ctkVTKRenderView);
+  
+  d->VTKWidget = new QVTKWidget(this);
+  this->setLayout(new QVBoxLayout);
+  this->layout()->setMargin(0);
+  this->layout()->setSpacing(0);
+  this->layout()->addWidget(d->VTKWidget);
+
+  d->setupRendering();
+  d->setupDefaultInteractor();
+}
+
+// --------------------------------------------------------------------------
+ctkVTKRenderView::~ctkVTKRenderView()
+{
+}
+
+//----------------------------------------------------------------------------
+CTK_GET_CXX(ctkVTKRenderView, vtkRenderWindowInteractor*, interactor, CurrentInteractor);
+
+//----------------------------------------------------------------------------
+void ctkVTKRenderView::scheduleRender()
+{
+  CTK_D(ctkVTKRenderView);
+  if (!d->RenderPending)
+    {
+    d->RenderPending = true;
+    QTimer::singleShot(0, this, SLOT(forceRender()));
+    }
+}
+
+//----------------------------------------------------------------------------
+void ctkVTKRenderView::forceRender()
+{
+  CTK_D(ctkVTKRenderView);
+  d->RenderWindow->Render();
+  d->RenderPending = false;
+}
+
+//----------------------------------------------------------------------------
+void ctkVTKRenderView::setInteractor(vtkRenderWindowInteractor* newInteractor)
+{
+  Q_ASSERT(newInteractor);
+  CTK_D(ctkVTKRenderView);
+  d->RenderWindow->SetInteractor(newInteractor);
+  d->Orientation->SetOrientationMarker(d->Axes);
+  d->Orientation->SetInteractor(newInteractor);
+  d->Orientation->SetEnabled(1);
+  d->Orientation->InteractiveOff();
+  d->CurrentInteractor = newInteractor; 
+}
+
+//----------------------------------------------------------------------------
+void ctkVTKRenderView::setCornerAnnotationText(const QString& text)
+{
+  CTK_D(ctkVTKRenderView);
+  d->CornerAnnotation->ClearAllTexts();
+  d->CornerAnnotation->SetText(2, text.toLatin1());
+}
+
+// --------------------------------------------------------------------------
+void ctkVTKRenderView::setBackgroundColor(double r, double g, double b)
+{
+  CTK_D(ctkVTKRenderView);
+  double background_color[3] = {r, g, b};
+  d->Renderer->SetBackground(background_color);
+}
+
+//----------------------------------------------------------------------------
+void ctkVTKRenderView::resetCamera()
+{
+  CTK_D(ctkVTKRenderView);
+  d->Renderer->ResetCamera();
+}

+ 62 - 0
Libs/Visualization/VTK/Widgets/ctkVTKRenderView.h

@@ -0,0 +1,62 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc. 
+  All rights reserved.
+  Distributed under a BSD License. See LICENSE.txt file.
+
+  This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+  See the above copyright notice for more information.
+
+=========================================================================*/
+
+#ifndef __ctkVTKRenderView_h
+#define __ctkVTKRenderView_h
+
+// Qt includes
+#include <QWidget>
+
+// CTK includes
+#include <ctkPimpl.h>
+
+#include "CTKVisualizationVTKWidgetsExport.h"
+
+class ctkVTKRenderViewPrivate;
+class vtkRenderWindowInteractor;
+
+class CTK_VISUALIZATION_VTK_WIDGETS_EXPORT ctkVTKRenderView : public QWidget
+{
+  Q_OBJECT
+public:
+  /// Constructors
+  typedef QWidget   Superclass;
+  explicit ctkVTKRenderView(QWidget* parent = 0);
+  virtual ~ctkVTKRenderView();
+
+  /// If a render has already been scheduled, this called is a no-op
+  void scheduleRender();
+
+  /// Force a render even if a render is already ocurring
+  void forceRender();
+
+  /// Set/Get window interactor
+  vtkRenderWindowInteractor* interactor()const;
+  void setInteractor(vtkRenderWindowInteractor* newInteractor);
+
+  /// Set corner annotation text
+  void setCornerAnnotationText(const QString& text);
+
+  /// Set background color
+  void setBackgroundColor(double r, double g, double b);
+
+  void resetCamera();
+
+  //virtual void setCornerText(const QString& text);
+  
+private:
+  CTK_DECLARE_PRIVATE(ctkVTKRenderView);
+}; 
+
+#endif

+ 65 - 0
Libs/Visualization/VTK/Widgets/ctkVTKRenderView_p.h

@@ -0,0 +1,65 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc. 
+  All rights reserved.
+  Distributed under a BSD License. See LICENSE.txt file.
+
+  This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+  See the above copyright notice for more information.
+
+=========================================================================*/
+
+#ifndef __ctkVTKRenderView_p_h
+#define __ctkVTKRenderView_p_h
+
+// Qt includes
+#include <QObject>
+
+// CTK includes
+#include <ctkPimpl.h>
+#include <ctkVTKObject.h>
+#include "ctkVTKRenderView.h"
+
+// VTK includes
+#include <QVTKWidget.h>
+#include <vtkAxesActor.h>
+#include <vtkCornerAnnotation.h>
+#include <vtkOrientationMarkerWidget.h>
+#include <vtkRenderer.h>
+#include <vtkRenderWindow.h>
+#include <vtkSmartPointer.h>
+#include <vtkWeakPointer.h>
+
+class vtkRenderWindowInteractor;
+
+//-----------------------------------------------------------------------------
+class ctkVTKRenderViewPrivate : public QObject,
+                                public ctkPrivate<ctkVTKRenderView>
+{
+  Q_OBJECT
+  CTK_DECLARE_PUBLIC(ctkVTKRenderView);
+public:
+  ctkVTKRenderViewPrivate();
+
+  /// Convenient setup methods
+  void setupCornerAnnotation();
+  void setupRendering();
+  void setupDefaultInteractor();
+
+  QVTKWidget*                                   VTKWidget;
+  vtkSmartPointer<vtkRenderer>                  Renderer;
+  vtkSmartPointer<vtkRenderWindow>              RenderWindow;
+  bool                                          RenderPending;
+  
+  vtkSmartPointer<vtkAxesActor>                 Axes;
+  vtkSmartPointer<vtkOrientationMarkerWidget>   Orientation;
+  vtkSmartPointer<vtkCornerAnnotation>          CornerAnnotation;
+
+  vtkWeakPointer<vtkRenderWindowInteractor>     CurrentInteractor;
+
+};
+
+#endif