fw4spl
fwRenderQt/src/fwRenderQt/IAdaptor.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 "fwRenderQt/IAdaptor.hpp"
8 
9 #include "fwRenderQt/registry/Adaptor.hpp"
10 #include "fwRenderQt/Scene2DGraphicsView.hpp"
11 
12 #include <fwServices/macros.hpp>
13 #include <fwServices/registry/ObjectService.hpp>
14 
15 #include <QGraphicsItemGroup>
16 
17 namespace fwRenderQt
18 {
19 
20 IAdaptor::IAdaptor() noexcept :
21  m_zValue(0.f),
22  m_opacity(1.f)
23 {
24 }
25 
26 //-----------------------------------------------------------------------------
27 
29 {
30 }
31 
32 //-----------------------------------------------------------------------------
33 
34 void IAdaptor::setZValue(float _zValue)
35 {
36  m_zValue = _zValue;
37 }
38 
39 //-----------------------------------------------------------------------------
40 
41 float IAdaptor::getZValue() const
42 {
43  return m_zValue;
44 }
45 
46 //-----------------------------------------------------------------------------
47 
48 ::fwRenderQt::SRender::sptr IAdaptor::getScene2DRender() const
49 {
50  const auto& registry = ::fwRenderQt::registry::getAdaptorRegistry();
51  const auto& iter = registry.find(this->getID());
52  SLM_ASSERT("Adaptor "+ this->getID() +" not registered", iter != registry.end());
53 
54  ::fwRenderQt::SRender::sptr render =
55  ::fwRenderQt::SRender::dynamicCast(::fwTools::fwID::getObject(iter->second));
56  SLM_ASSERT("Service SRender "+ iter->second +" not instanced", render);
57  return render;
58 }
59 
60 //-----------------------------------------------------------------------------
61 
63 {
64  return ViewSizeRatio(
65  (float) ( m_viewInitialSize.first / this->getScene2DRender()->getView()->width() ),
66  (float) ( m_viewInitialSize.second / this->getScene2DRender()->getView()->height() ) );
67 }
68 
69 //-----------------------------------------------------------------------------
70 
72 {
73  const ::fwRenderQt::data::Viewport::csptr& viewport = this->getScene2DRender()->getViewport();
74  return ViewportSizeRatio(
75  (float) ( m_viewportInitialSize.first / viewport->getWidth() ),
76  (float) ( m_viewportInitialSize.second / viewport->getHeight() ) );
77 }
78 
79 //-----------------------------------------------------------------------------
80 
81 IAdaptor::Scene2DRatio IAdaptor::getRatio() const
82 {
83  const ViewSizeRatio ratioView = this->getViewSizeRatio();
84  const ViewportSizeRatio ratioViewport = this->getViewportSizeRatio();
85 
86  return Scene2DRatio( ratioView.first / ratioViewport.first,
87  ratioView.second / ratioViewport.second );
88 }
89 
90 //-----------------------------------------------------------------------------
91 
93  const ::fwRenderQt::data::Axis::sptr& _xAxis,
94  const ::fwRenderQt::data::Axis::sptr& _yAxis ) const
95 {
96  double x, y;
97 
98  if (_xAxis->getScaleType() == ::fwRenderQt::data::Axis::LOG)
99  {
100  // Logarithme 10 cannot get negative values
101  if (_xy.first <= 0.)
102  {
103  x = 0.;
104  }
105  else
106  {
107  // Apply the x scale and the log to the x value
108  x = _xAxis->getScale() * log10( _xy.first );
109  }
110  }
111  else
112  {
113  // Apply just the x scale to the x value
114  x = _xAxis->getScale() * _xy.first;
115  }
116 
117  if (_yAxis->getScaleType() == ::fwRenderQt::data::Axis::LOG)
118  {
119  // Logarithm 10 cannot get negative values
120  if (_xy.second <= 0.)
121  {
122  y = 0.;
123  }
124  else
125  {
126  // Apply the y scale and the log to the y value
127  y = _yAxis->getScale() * log10( _xy.second );
128  }
129  }
130  else
131  {
132  // Apply just the y scale to the y value
133  y = _yAxis->getScale() * _xy.second;
134  }
135 
136  return Point2DType( x, y );
137 }
138 
139 //-----------------------------------------------------------------------------
140 
142  const ::fwRenderQt::data::Axis::sptr& _xAxis,
143  const ::fwRenderQt::data::Axis::sptr& _yAxis ) const
144 {
145  // Do the reverse operation of the mapAdaptorToScene function
146  double x, y;
147  if (_xAxis->getScaleType() == ::fwRenderQt::data::Axis::LOG)
148  {
149  x = 10. * exp( _xy.first ) / _xAxis->getScale();
150  }
151  else
152  {
153  x = ( _xy.first ) / _xAxis->getScale();
154  }
155 
156  if (_yAxis->getScaleType() == ::fwRenderQt::data::Axis::LOG)
157  {
158  y = 10. * ( _xy.second ) / _yAxis->getScale();
159  }
160  else
161  {
162  y = _xy.second / _yAxis->getScale();
163  }
164 
165  return Point2DType( x, y );
166 }
167 
168 //-----------------------------------------------------------------------------
169 
171 {
172  const ConfigType config = this->getConfigTree().get_child("config.<xmlattr>");
173 
174  m_viewInitialSize.first = -1.0f;
175  m_viewInitialSize.second = -1.0f;
176 
177  m_viewportInitialSize.first = -1.0f;
178  m_viewportInitialSize.second = -1.0f;
179 
180  // If the corresponding attributes are present in the config, set the xAxis, yAxis and the adaptor zValue
181  if( config.count("xAxis") )
182  {
183  m_xAxis = this->getScene2DRender()->getAxis(config.get<std::string>("xAxis"));
184  SLM_ASSERT("xAxis not found", m_xAxis);
185  }
186  else
187  {
188  m_xAxis = std::make_shared< ::fwRenderQt::data::Axis >();
189  }
190 
191  if( config.count("yAxis") )
192  {
193  m_yAxis = this->getScene2DRender()->getAxis(config.get<std::string>("yAxis"));
194  SLM_ASSERT("yAxis not found", m_xAxis);
195  }
196  else
197  {
198  m_yAxis = std::make_shared< ::fwRenderQt::data::Axis >();
199  }
200 
201  if( config.count("zValue") )
202  {
203  m_zValue = config.get<float>("zValue");
204  }
205 
206  if( config.count("opacity") )
207  {
208  m_opacity = config.get<float>("opacity");
209  }
210 }
211 
212 //-----------------------------------------------------------------------------
213 
215 {
216  // Initialize the initial width of the view
217  if(m_viewInitialSize.first == -1.0f)
218  {
219  m_viewInitialSize.first = this->getScene2DRender()->getView()->width();
220  }
221 
222  // Initialize the initial height of the view
223  if(m_viewInitialSize.second == -1.0f)
224  {
225  m_viewInitialSize.second = this->getScene2DRender()->getView()->height();
226  }
227 }
228 
229 //-----------------------------------------------------------------------------
230 
232 {
233  const ::fwRenderQt::data::Viewport::csptr& viewport = this->getScene2DRender()->getViewport();
234  // Initialize the initial width of the viewport
235  if(m_viewportInitialSize.first == -1.0f)
236  {
237  m_viewportInitialSize.first = viewport->getWidth();
238  }
239 
240  // Initialize the initial height of the viewport
241  if(m_viewportInitialSize.second == -1.0f)
242  {
243  m_viewportInitialSize.second = viewport->getHeight();
244  }
245 }
246 
247 //-----------------------------------------------------------------------------
248 
250 {
251 }
252 
253 //-----------------------------------------------------------------------------
254 
255 } // namespace fwRenderQt
256 
virtual FWRENDERQT_API ~IAdaptor() noexcept
Basic destructor, do nothing.
::fwRenderQt::data::Axis::sptr m_xAxis
The x Axis.
Contains fwAtomsFilter::registry details.
std::pair< float, float > Scene2DRatio
<width, height>
FWRENDERQT_API void initializeViewSize()
Initialize the source values used for computing view&#39;s size ratio.
FWRENDERQT_API IAdaptor() noexcept
Constructor, set the zValue to 0.
FWRENDERQT_API Point2DType mapAdaptorToScene(const Point2DType &_xy, const ::fwRenderQt::data::Axis::sptr &_xAxis, const ::fwRenderQt::data::Axis::sptr &_yAxis) const
FWRENDERQT_API std::shared_ptr< ::fwRenderQt::SRender > getScene2DRender() const
Get the render that manages the IAdaptor.
std::pair< float, float > ViewSizeRatio
<width, height>
std::pair< double, double > Point2DType
Point2D coordinate <X, Y>
std::pair< float, float > ViewportSizeRatio
<width, height>
FWTOOLS_API IDType getID(Policy policy=GENERATE) const
Returns the id of the object. If it is not set and the policy value is.
Definition: fwID.cpp:78
FWRENDERQT_API Point2DType mapSceneToAdaptor(const Point2DType &_xy, const ::fwRenderQt::data::Axis::sptr &_xAxis, const ::fwRenderQt::data::Axis::sptr &_yAxis) const
FWRENDERQT_API float getZValue() const
Get the zValue.
#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
FWRENDERQT_API void initializeViewportSize()
Initialize the source values used for computing viewport&#39;s size ratio.
::fwRenderQt::data::Axis::sptr m_yAxis
The y Axis.
float m_opacity
Opacity of the adaptor. Default value set to 1 (opaque).
FWRENDERQT_API ViewportSizeRatio getViewportSizeRatio() const
Return the ratio between viewport&#39;s initial size and its current size.
FWRENDERQT_API void configureParams()
Parse the xml configuration for Axis, z value and opacity.
virtual FWRENDERQT_API void processInteraction(::fwRenderQt::data::Event &_event)
FWRENDERQT_API ViewSizeRatio getViewSizeRatio() const
Return the ratio between view&#39;s initial size and its current size.
The namespace fwRenderQt contains classes for rendering with Qt.
Definition: Axis.hpp:12
FWRENDERQT_API void setZValue(float _zValue)
Set the zValue.
This class manage events on the scene 2D (mouse event, keyboard event , ...).
Definition: Event.hpp:26
static FWTOOLS_API std::shared_ptr< ::fwTools::Object > getObject(IDType requestID)
Retrieve the object attached to the given id. Return a null sptr if no correspondence exist...
Definition: fwID.cpp:117
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247