fw4spl
VtkRenderWindowInteractorManager.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "visuVTKQt/VtkRenderWindowInteractorManager.hpp"
8 
9 #include <fwCom/Signal.hpp>
10 #include <fwCom/Signal.hxx>
11 
12 #include <fwData/String.hpp>
13 
14 #include <fwGuiQt/container/QtContainer.hpp>
15 
16 #include <fwRenderVTK/registry/macros.hpp>
17 #include <fwRenderVTK/SRender.hpp>
18 
19 #include <fwTools/UUID.hpp>
20 
21 #include <QDropEvent>
22 #include <QEvent>
23 #include <QMimeData>
24 #include <QSurfaceFormat>
25 #include <QVBoxLayout>
26 #include <QVTKOpenGLWidget.h>
27 #include <vtkGenericOpenGLRenderWindow.h>
28 #include <vtkRenderer.h>
29 #include <vtkRenderWindow.h>
30 #include <vtkRenderWindowInteractor.h>
31 
32 class DropFilter : public QObject
33 {
34 public:
35  DropFilter(::fwServices::IService::sptr service) :
36  m_service(service)
37  {
38  }
39 protected:
40  bool eventFilter(QObject* obj, QEvent* event);
41 
42 private:
43  ::fwServices::IService::wptr m_service;
44 };
45 
46 //------------------------------------------------------------------------------
47 
48 bool DropFilter::eventFilter(QObject* obj, QEvent* event)
49 {
50  if( event->type() == QEvent::DragEnter)
51  {
52  QDragEnterEvent* dragEvent = dynamic_cast< QDragEnterEvent* >(event);
53  if (dragEvent->mimeData()->hasFormat("text/plain"))
54  {
55  QString data = dragEvent->mimeData()->text();
56  dragEvent->acceptProposedAction();
57  }
58  }
59  else if (event->type() == QEvent::Drop )
60  {
61  QDropEvent* dropEvent = dynamic_cast< QDropEvent* >(event);
62  QString data = dropEvent->mimeData()->text();
63  ::fwServices::IService::sptr service = m_service.lock();
64  auto sig = service->signal< ::fwRenderVTK::SRender::DroppedSignalType >(
65  ::fwRenderVTK::SRender::s_DROPPED_SIG);
66  sig->asyncEmit( data.toStdString() );
67  }
68  else
69  {
70  // standard event processing
71  return QObject::eventFilter(obj, event);
72  }
73  return true;
74 }
75 
76 //-----------------------------------------------------------------------------
77 
78 fwRenderVTKRegisterMacro( ::visuVTKQt::VtkRenderWindowInteractorManager,
79  ::fwRenderVTK::IVtkRenderWindowInteractorManager::REGISTRY_KEY );
80 
81 //-----------------------------------------------------------------------------
82 
83 namespace visuVTKQt
84 {
85 
86 //-----------------------------------------------------------------------------
87 
88 VtkRenderWindowInteractorManager::VtkRenderWindowInteractorManager(
90 {
91 }
92 
93 //-----------------------------------------------------------------------------
94 
95 VtkRenderWindowInteractorManager::~VtkRenderWindowInteractorManager()
96 {
97 }
98 
99 //-----------------------------------------------------------------------------
100 
101 void VtkRenderWindowInteractorManager::installInteractor( ::fwGui::container::fwContainer::sptr _parent )
102 {
103  SLM_ASSERT("Invalid parent.", _parent );
104  m_parentContainer = ::fwGuiQt::container::QtContainer::dynamicCast( _parent );
105 
106  QVBoxLayout* layout = new QVBoxLayout();
107  layout->setContentsMargins(0, 0, 0, 0);
108  m_parentContainer->setLayout(layout);
109 
110  // Create the render window and the associated QVTKOpenGLWidget
111  vtkNew<vtkGenericOpenGLRenderWindow> window;
112  m_QVTKOpenGLWidget = new QVTKOpenGLWidget(m_parentContainer->getQtContainer());
113  m_QVTKOpenGLWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
114  layout->addWidget(m_QVTKOpenGLWidget);
115  m_QVTKOpenGLWidget->SetRenderWindow(window.Get());
116 
117  // Install Drop filter
118  if(auto locked = m_renderService.lock())
119  {
120  m_QVTKOpenGLWidget->setAcceptDrops(true);
121  m_QVTKOpenGLWidget->installEventFilter(new DropFilter(locked));
122  }
123 
124  m_interactor = window->GetInteractor();
125 }
126 
127 //-----------------------------------------------------------------------------
128 
129 void VtkRenderWindowInteractorManager::uninstallInteractor()
130 {
131  m_interactor = nullptr;
132 
133  delete m_QVTKOpenGLWidget;
134  m_QVTKOpenGLWidget.clear();
135 
136  m_parentContainer->clean();
137 }
138 
139 //-----------------------------------------------------------------------------
140 
141 ::vtkRenderWindowInteractor* VtkRenderWindowInteractorManager::getInteractor()
142 {
143  return m_interactor;
144 }
145 
146 //-----------------------------------------------------------------------------
147 
148 } // namespace visuVTKQt
149 
The bundle visuVTKQt contains a vtk Renderer window interactor manager using Qt. It must be set in th...
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
Defines a class to manage vtkRenderWindowInteractor in a window.