fw4spl
SViewportInteractor.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 "scene2D/adaptor/SViewportInteractor.hpp"
8 
9 #include <fwRenderQt/Scene2DGraphicsView.hpp>
10 
11 #include <fwServices/macros.hpp>
12 
13 fwServicesRegisterMacro( ::fwRenderQt::IAdaptor, ::scene2D::adaptor::SViewportInteractor);
14 
15 namespace scene2D
16 {
17 namespace adaptor
18 {
19 
20 SViewportInteractor::SViewportInteractor() noexcept :
21  m_viewportIsTranslated(false)
22 {
23 }
24 
25 //-----------------------------------------------------------------------------
26 
27 SViewportInteractor::~SViewportInteractor() noexcept
28 {
29 }
30 
31 //-----------------------------------------------------------------------------
32 
34 {
35 }
36 
37 //-----------------------------------------------------------------------------
38 
40 {
41 }
42 
43 //-----------------------------------------------------------------------------
44 
46 {
47 }
48 
49 //-----------------------------------------------------------------------------
50 
52 {
53 }
54 
55 //-----------------------------------------------------------------------------
56 
58 {
59  if ( _event.getType() == ::fwRenderQt::data::Event::MouseWheelUp
60  && _event.getModifier() == ::fwRenderQt::data::Event::ShiftModifier )
61  {
62  this->zoom(true);
63  }
64  else if ( _event.getType() == ::fwRenderQt::data::Event::MouseWheelDown
65  && _event.getModifier() == ::fwRenderQt::data::Event::ShiftModifier )
66  {
67  this->zoom(false);
68  }
69  else if ( _event.getType() == ::fwRenderQt::data::Event::MouseButtonPress
70  && _event.getButton() == ::fwRenderQt::data::Event::LeftButton
71  && _event.getModifier() == ::fwRenderQt::data::Event::ShiftModifier )
72  {
73  m_viewportIsTranslated = true;
74  m_lastCoordEvent = _event.getCoord();
75  }
76  else if ( m_viewportIsTranslated )
77  {
78  if ( _event.getType() == ::fwRenderQt::data::Event::MouseMove )
79  {
80  ::fwRenderQt::data::Coord coord = _event.getCoord();
81  ::fwRenderQt::data::Viewport::sptr sceneViewport = this->getScene2DRender()->getViewport();
82 
83  const float dx = coord.getX() - m_lastCoordEvent.getX();
84  const float xTrans = dx * sceneViewport->getWidth() / (float) this->getScene2DRender()->getView()->width();
85 
86  const float dy = coord.getY() - m_lastCoordEvent.getY();
87  const float yTrans = dy * sceneViewport->getHeight() /
88  (float) this->getScene2DRender()->getView()->height();
89 
90  sceneViewport->setX( sceneViewport->getX() - xTrans );
91  sceneViewport->setY( sceneViewport->getY() - yTrans );
92  this->getScene2DRender()->getView()->updateFromViewport();
93 
94  m_lastCoordEvent = coord;
95  }
96  else if ( _event.getType() == ::fwRenderQt::data::Event::MouseButtonRelease )
97  {
98  m_viewportIsTranslated = false;
99  }
100  }
101 }
102 
103 //-----------------------------------------------------------------------------
104 
105 void SViewportInteractor::zoom( bool zoomIn )
106 {
107  ::fwRenderQt::data::Viewport::sptr sceneViewport = this->getScene2DRender()->getViewport();
108 
109  float y = sceneViewport->getY();
110  float x = sceneViewport->getX();
111 
112  float width = sceneViewport->getWidth();
113  float height = sceneViewport->getHeight();
114 
115  const float zoomPercent = 10.f / 100.0f;
116  const float centerX = x + width/2.0f;
117  const float centerY = y + height/2.0f;
118 
119  float newWidth;
120  float newHeight;
121  if ( zoomIn )
122  {
123  newWidth = width * zoomPercent;
124  newHeight = height * zoomPercent;
125  }
126  else
127  {
128  newWidth = -width * zoomPercent;
129  newHeight = -height * zoomPercent;
130  }
131 
132  newWidth += width;
133  newHeight += height;
134 
135  x = centerX - newWidth / 2;
136  y = centerY - newHeight / 2;
137 
138  width = newWidth;
139  height = newHeight;
140 
141  // Set viewport
142  sceneViewport->setX( x );
143  sceneViewport->setY( y );
144  sceneViewport->setWidth( width );
145  sceneViewport->setHeight( height );
146  this->getScene2DRender()->getView()->updateFromViewport();
147 }
148 
149 //-----------------------------------------------------------------------------
150 
151 } // namespace adaptor
152 } // namespace scene2D
153 
Root class for all scene2d adaptors.
SCENE2D_API void processInteraction(::fwRenderQt::data::Event &_event) override
FWRENDERQT_API std::shared_ptr< ::fwRenderQt::SRender > getScene2DRender() const
Get the render that manages the IAdaptor.
This bundles contains data and services used to display a 2D Qt scene.
SCENE2D_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
Adaptor implementation that manages the camera on the view.
SCENE2D_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
SCENE2D_API void starting() override
Initialize the service activity.
SCENE2D_API void configuring() override
Configure the service before starting. Apply the configuration to service.
This class manage events on the scene 2D (mouse event, keyboard event , ...).
Definition: Event.hpp:26