fw4spl
SHistogramValue.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
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/SHistogramValue.hpp"
8 
9 #include <fwData/Histogram.hpp>
10 #include <fwData/Point.hpp>
11 
12 #include <fwRenderQt/data/InitQtPen.hpp>
13 #include <fwRenderQt/Scene2DGraphicsView.hpp>
14 
15 #include <fwServices/macros.hpp>
16 
17 #include <QFont>
18 #include <QGraphicsEllipseItem>
19 
20 fwServicesRegisterMacro( ::fwRenderQt::IAdaptor, ::scene2D::adaptor::SHistogramValue);
21 
22 namespace scene2D
23 {
24 namespace adaptor
25 {
26 
27 static const ::fwServices::IService::KeyType s_POINT_INPUT = "point";
28 static const ::fwServices::IService::KeyType s_HISTOGRAM_INPUT = "histogram";
29 static const ::fwServices::IService::KeyType s_VIEWPORT_INPUT = "viewport";
30 
31 SHistogramValue::SHistogramValue() noexcept :
32  m_color(Qt::white),
33  m_text(nullptr),
34  m_isInteracting(false),
35  m_fontSize(8.f),
36  m_layer(nullptr)
37 {
38 }
39 
40 //---------------------------------------------------------------------------------------------------------------
41 
42 SHistogramValue::~SHistogramValue() noexcept
43 {
44 }
45 
46 //---------------------------------------------------------------------------------------------------------------
47 
49 {
50  this->configureParams();
51 
52  const ConfigType config = this->getConfigTree().get_child("config.<xmlattr>");
53 
54  if (config.count("color"))
55  {
56  ::fwRenderQt::data::InitQtPen::setPenColor(m_color, config.get<std::string>("color"));
57  }
58 
59  if (config.count("fontSize"))
60  {
61  m_fontSize = config.get<float>("fontSize");
62  }
63 }
64 
65 //---------------------------------------------------------------------------------------------------------------
66 
68 {
69  m_font.setPointSize(m_fontSize);
70  m_font.setLetterSpacing( QFont::AbsoluteSpacing, 0.2 );
71  m_font.setKerning( true );
72  m_font.setFixedPitch( true );
73 
74  m_text = new QGraphicsSimpleTextItem();
75  m_text->setBrush( QBrush(m_color.color()) );
76  m_text->setFont( m_font );
77  m_text->setCacheMode( QGraphicsItem::DeviceCoordinateCache );
78  m_text->setVisible( false );
79 
80  // Initialize the layer
81  m_layer = new QGraphicsItemGroup();
82  m_layer->addToGroup( m_text );
83 
84  // Set the layer position (according to the related axis) and zValue
85  m_layer->setPos(m_xAxis->getOrigin(), m_yAxis->getOrigin());
86  m_layer->setZValue(m_zValue);
87 
88  // Add the layer containing grid's lines to the scene
89  this->getScene2DRender()->getScene()->addItem(m_layer);
90 }
91 
92 //---------------------------------------------------------------------------------------------------------------
93 
95 {
96 }
97 
98 //---------------------------------------------------------------------------------------------------------------
99 
101 {
102  this->initializeViewSize();
103  this->initializeViewportSize();
104 
105  ::fwData::Histogram::csptr histogram = this->getInput< ::fwData::Histogram>(s_HISTOGRAM_INPUT);
106  ::fwData::Histogram::fwHistogramValues values = histogram->getValues();
107  const float histogramMinValue = histogram->getMinValue();
108  const float histogramBinsWidth = histogram->getBinsWidth();
109 
110  // Event coordinates in scene
111  ::fwRenderQt::data::Coord sceneCoord = this->getScene2DRender()->mapToScene( m_coord );
112 
113  int histIndex = (int) sceneCoord.getX();
114  int index = (histIndex - histogramMinValue) / histogramBinsWidth;
115 
116  if(index >= 0 && index < (int)values.size() && m_isInteracting) // avoid std out_of_range on Windows
117  {
118  ::fwRenderQt::data::Viewport::sptr viewport = this->getScene2DRender()->getViewport();
119  const double viewportHeight = viewport->getHeight();
120  const double viewportWidth = viewport->getWidth();
121 
122  const double viewportSizeRatio = viewportHeight / viewportWidth;
123  const double viewInitialSizeRatio = m_viewInitialSize.first / m_viewInitialSize.second;
124 
125  Scene2DRatio ratio = this->getRatio(); // Total ratio
126  double viewportWidthRatio = this->getViewportSizeRatio().first;
127 
128  double diameterH = m_fontSize;
129  double diameterV = m_fontSize * viewportSizeRatio;
130 
131  diameterV /= viewportWidthRatio;
132  diameterV *= viewInitialSizeRatio;
133 
134  diameterH *= ratio.first;
135  diameterV *= ratio.second;
136 
137  m_text->setText( QString::number( histIndex ) );
138 
139  double scaleX = m_fontSize;
140  double scaleY = m_fontSize * viewportSizeRatio;
141 
142  scaleY /= viewportWidthRatio;
143  scaleY *= viewInitialSizeRatio;
144 
145  scaleX = scaleX * ratio.first;
146  scaleY = scaleY * ratio.second;
147 
148  QTransform transform;
149  transform.scale(scaleX, scaleY);
150 
151  ::fwData::Point::csptr point = this->getInput< ::fwData::Point>(s_POINT_INPUT);
152 
153  m_text->setTransform( transform );
154  m_text->setPos( point->getCoord()[0] + diameterH * 2, point->getCoord()[1] - diameterV * 2 );
155  m_text->setVisible( true );
156  }
157  else
158  {
159  m_text->setVisible( false );
160  }
161 }
162 
163 //---------------------------------------------------------------------------------------------------------------
164 
166 {
167  this->initializeViewSize();
168  this->initializeViewportSize();
169 
170  if( _event.getType() == ::fwRenderQt::data::Event::MouseMove )
171  {
172  m_coord = _event.getCoord();
173  }
174  else if( _event.getType() == ::fwRenderQt::data::Event::MouseButtonPress )
175  {
176  m_isInteracting = true;
177  }
178  else if( _event.getType() == ::fwRenderQt::data::Event::MouseButtonRelease )
179  {
180  m_isInteracting = false;
181  }
182 
183  updating();
184 }
185 
186 //----------------------------------------------------------------------------------------------------------
187 
189 {
190  KeyConnectionsMap connections;
191  connections.push( s_HISTOGRAM_INPUT, ::fwData::Histogram::s_MODIFIED_SIG, s_UPDATE_SLOT );
192  connections.push( s_VIEWPORT_INPUT, ::fwRenderQt::data::Viewport::s_MODIFIED_SIG, s_UPDATE_SLOT );
193  return connections;
194 }
195 
196 //---------------------------------------------------------------------------------------------------------------
197 
198 } // namespace adaptor
199 } // namespace scene2D
Root class for all scene2d adaptors.
::fwRenderQt::data::Axis::sptr m_xAxis
The x Axis.
This class is a helper to define the connections of a service and its data.
Definition: IService.hpp:454
std::pair< float, float > Scene2DRatio
<width, height>
QPen m_color
Color used for graphic item&#39;s inner color.
QGraphicsItemGroup * m_layer
The layer.
FWRENDERQT_API void initializeViewSize()
Initialize the source values used for computing view&#39;s size ratio.
bool m_isInteracting
If true, display the currently pointed intensity value.
FWRENDERQT_API std::shared_ptr< ::fwRenderQt::SRender > getScene2DRender() const
Get the render that manages the IAdaptor.
float m_fontSize
Size of the font used for rendering the current value of this tracker.
QGraphicsSimpleTextItem * m_text
An item which display the current values of the associated histogram pointed by this cursor...
static FWRENDERQT_API void setPenColor(QPen &_pen, std::string _color)
Set a pen a color.
Definition: InitQtPen.cpp:18
This bundles contains data and services used to display a 2D Qt scene.
SCENE2D_API void configuring() override
Configure the service before starting. Apply the configuration to service.
FWRENDERQT_API void initializeViewportSize()
Initialize the source values used for computing viewport&#39;s size ratio.
SCENE2D_API::fwServices::IService::KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated objects signals, this method is used for obj...
SCENE2D_API void starting() override
Initialize the service activity.
::fwRenderQt::data::Axis::sptr m_yAxis
The y Axis.
::fwRenderQt::data::Coord m_coord
Coordinates of the current event.
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.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
SCENE2D_API void processInteraction(::fwRenderQt::data::Event &_event) override
IAdaptor implementation to show the histogram values clicked by mouse.
SCENE2D_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
This class manage events on the scene 2D (mouse event, keyboard event , ...).
Definition: Event.hpp:26
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_SLOT
Slot to call start method.
Definition: IService.hpp:177
SCENE2D_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247