fw4spl
SHistogramCursor.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/SHistogramCursor.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::SHistogramCursor);
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 SHistogramCursor::SHistogramCursor() noexcept :
32  m_color(Qt::red),
33  m_borderColor(Qt::gray),
34  m_index(nullptr),
35  m_pointSize(6.f),
36  m_layer(nullptr)
37 {
38 }
39 
40 //---------------------------------------------------------------------------------------------------------------
41 
42 SHistogramCursor::~SHistogramCursor() 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("borderColor"))
60  {
61  ::fwRenderQt::data::InitQtPen::setPenColor(m_borderColor, config.get<std::string>("borderColor"));
62  }
63 
64  if (config.count("pointSize"))
65  {
66  m_pointSize = config.get<float>("pointSize");
67  }
68 }
69 
70 //---------------------------------------------------------------------------------------------------------------
71 
73 {
74  m_index = new QGraphicsEllipseItem();
75  m_index->setBrush( m_color.color() );
76  m_index->setPen( m_borderColor );
77  m_index->setZValue(m_zValue);
78  m_index->setCacheMode( QGraphicsItem::DeviceCoordinateCache );
79  m_color.setCosmetic(true);
80 
81  // Initialize the layer
82  m_layer = new QGraphicsItemGroup();
83 
84  m_layer->addToGroup( m_index );
85 
86  // Set the layer position (according to the related axis) and zValue
87  m_layer->setPos(m_xAxis->getOrigin(), m_yAxis->getOrigin());
88  m_layer->setZValue(m_zValue);
89 
90  // Add the layer containing grid's lines to the scene
91  this->getScene2DRender()->getScene()->addItem(m_layer);
92 }
93 
94 //---------------------------------------------------------------------------------------------------------------
95 
97 {
98 }
99 
100 //---------------------------------------------------------------------------------------------------------------
101 
103 {
104  this->initializeViewSize();
105  this->initializeViewportSize();
106 
107  ::fwData::Histogram::csptr histogram = this->getInput< ::fwData::Histogram>(s_HISTOGRAM_INPUT);
108  ::fwData::Histogram::fwHistogramValues values = histogram->getValues();
109  const float histogramMinValue = histogram->getMinValue();
110  const float histogramBinsWidth = histogram->getBinsWidth();
111 
112  // Event coordinates in scene
113  ::fwRenderQt::data::Coord sceneCoord = this->getScene2DRender()->mapToScene( m_coord );
114 
115  int histIndex = (int) sceneCoord.getX();
116  int index = (histIndex - histogramMinValue) / histogramBinsWidth;
117 
118  if(index >= 0 && index < (int)values.size()) // avoid std out_of_range on MS Windows
119  {
120  ::fwRenderQt::data::Viewport::sptr viewport = this->getScene2DRender()->getViewport();
121  const double viewportHeight = viewport->getHeight();
122  const double viewportWidth = viewport->getWidth();
123 
124  const double viewportSizeRatio = viewportHeight / viewportWidth;
125  const double viewInitialSizeRatio = m_viewInitialSize.first / m_viewInitialSize.second;
126 
127  const Scene2DRatio ratio = this->getRatio(); // Total ratio
128  const double viewportRatio = this->getViewportSizeRatio().first;
129 
130  double diameterH = m_pointSize;
131  double diameterV = m_pointSize * viewportSizeRatio;
132 
133  diameterV /= viewportRatio;
134  diameterV *= viewInitialSizeRatio;
135 
136  // Apply the ratio of the scene 2D in order to keep the same size for the circles if viewport's size or
137  // view's size change:
138  diameterH *= ratio.first;
139  diameterV *= ratio.second;
140 
141  ::fwData::Point::csptr point = this->getInput< ::fwData::Point>(s_POINT_INPUT);
142 
143  const double x = point->getCoord()[0] - diameterH / 2;
144  const double y = point->getCoord()[1] - diameterV / 2;
145 
146  m_index->setRect( x, y, diameterH, diameterV );
147  }
148 }
149 
150 //---------------------------------------------------------------------------------------------------------------
151 
153 {
154  this->initializeViewSize();
155  this->initializeViewportSize();
156 
157  if( _event.getType() == ::fwRenderQt::data::Event::MouseMove )
158  {
159  m_coord = _event.getCoord();
160  }
161 
162  updating();
163 }
164 
165 //----------------------------------------------------------------------------------------------------------
166 
168 {
169  KeyConnectionsMap connections;
170  connections.push( s_HISTOGRAM_INPUT, ::fwData::Histogram::s_MODIFIED_SIG, s_UPDATE_SLOT );
171  connections.push( s_VIEWPORT_INPUT, ::fwRenderQt::data::Viewport::s_MODIFIED_SIG, s_UPDATE_SLOT );
172  return connections;
173 }
174 
175 //---------------------------------------------------------------------------------------------------------------
176 
177 } // namespace adaptor
178 } // namespace scene2D
Root class for all scene2d adaptors.
::fwRenderQt::data::Axis::sptr m_xAxis
The x Axis.
SCENE2D_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
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>
FWRENDERQT_API void initializeViewSize()
Initialize the source values used for computing view&#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...
FWRENDERQT_API std::shared_ptr< ::fwRenderQt::SRender > getScene2DRender() const
Get the render that manages the IAdaptor.
QPen m_color
Color used for graphic item&#39;s inner color.
static FWRENDERQT_API void setPenColor(QPen &_pen, std::string _color)
Set a pen a color.
Definition: InitQtPen.cpp:18
QPen m_borderColor
Color used for graphic item&#39;s border color.
This bundles contains data and services used to display a 2D Qt scene.
QGraphicsItemGroup * m_layer
The layer.
FWRENDERQT_API void initializeViewportSize()
Initialize the source values used for computing viewport&#39;s size ratio.
SCENE2D_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
::fwRenderQt::data::Coord m_coord
Coordinates of the current event.
::fwRenderQt::data::Axis::sptr m_yAxis
The y Axis.
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 configuring() override
Configure the service before starting. Apply the configuration to service.
IAdaptor implementation to show a cursor on histogram pointed by mouse.
This class manage events on the scene 2D (mouse event, keyboard event , ...).
Definition: Event.hpp:26
SCENE2D_API void starting() override
Initialize the service activity.
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_SLOT
Slot to call start method.
Definition: IService.hpp:177
SCENE2D_API void processInteraction(::fwRenderQt::data::Event &_event) override
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247