fw4spl
SGrid2D.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/SGrid2D.hpp"
8 
9 #include <fwCom/Slots.hxx>
10 
11 #include <fwRenderQt/data/InitQtPen.hpp>
12 #include <fwRenderQt/Scene2DGraphicsView.hpp>
13 
14 #include <fwServices/macros.hpp>
15 
16 #include <QGraphicsItemGroup>
17 
18 namespace scene2D
19 {
20 namespace adaptor
21 {
22 
23 //---------------------------------------------------------------------------------------------------------------
24 
25 fwServicesRegisterMacro( ::fwRenderQt::IAdaptor, ::scene2D::adaptor::SGrid2D);
26 
27 const ::fwCom::Slots::SlotKeyType SGrid2D::s_SET_GRID_SPACING_SLOT = "setGridSpacing";
28 
29 //---------------------------------------------------------------------------------------------------------------
30 
31 SGrid2D::SGrid2D() noexcept :
32  m_xMin(0.f),
33  m_xMax(0.f),
34  m_yMin(0.f),
35  m_yMax(0.f),
36  m_xSpacing(10.f),
37  m_ySpacing(10.f),
38  m_layer(nullptr)
39 {
40  newSlot(s_SET_GRID_SPACING_SLOT, &::scene2D::adaptor::SGrid2D::setGridSpacing, this);
41 }
42 
43 //---------------------------------------------------------------------------------------------------------------
44 
46 {
47 }
48 
49 //---------------------------------------------------------------------------------------------------------------
50 
52 {
53  this->configureParams();
54 
55  const ConfigType config = this->getConfigTree().get_child("config.<xmlattr>");
56 
57  SLM_ASSERT("Attribute 'xMin' is missing", config.count("xMin"));
58  SLM_ASSERT("Attribute 'xMax' is missing", config.count("xMax"));
59  SLM_ASSERT("Attribute 'yMin' is missing", config.count("yMin"));
60  SLM_ASSERT("Attribute 'yMax' is missing", config.count("yMax"));
61 
62  // Set the x/y min/max values
63  m_xMin = config.get<float>("xMin");
64  m_xMax = config.get<float>("xMax");
65  m_yMin = config.get<float>("yMin");
66  m_yMax = config.get<float>("yMax");
67 
68  // If the corresponding attributes are present in the config, set the xSpacing, ySpacing between
69  // the lines and color of the lines:
70  if (config.count("xSpacing"))
71  {
72  m_xSpacing = config.get<float>("xSpacing");
73  }
74 
75  if (config.count("ySpacing"))
76  {
77  m_ySpacing = config.get<float>("ySpacing");
78  }
79 
80  if (config.count("color"))
81  {
82  ::fwRenderQt::data::InitQtPen::setPenColor(m_pen, config.get<std::string>("color"), m_opacity);
83  }
84 }
85 
86 //---------------------------------------------------------------------------------------------------------------
87 
88 void SGrid2D::draw()
89 {
90  SLM_ASSERT("m_xSpacing can not be equal to 0", m_xSpacing != 0.f);
91  SLM_ASSERT("m_ySpacing can not be equal to 0", m_ySpacing != 0.f);
92 
93  // Remove all lines from the scene
94  for (const auto& line : m_lines)
95  {
96  this->getScene2DRender()->getScene()->removeItem(line);
97  }
98  // Clear the lines vector
99  m_lines.clear();
100 
101  this->getScene2DRender()->getScene()->removeItem( m_layer );
102  m_layer = new QGraphicsItemGroup();
103 
104  // Calculate the start, end and step on x for the lines
105  const float xStartVal = getXStartVal(); // Allows to start drawing the grid from 0 with the correct step
106  const float xEndVal = getXEndVal(); // Allows to start drawing the grid from 0 with the correct step
107 
108  // Calculate the start, end and step on y for the lines
109  const float yStartVal = getYStartVal(); // Allows to start drawing the grid from 0 with the correct step
110  const float yEndVal = getYEndVal(); // Allows to start drawing the grid from 0 with the correct step
111 
112  // Holds the current computed coordinates:
113  Point2DType coord1, coord2;
114 
115  // Draw the horizontal lines
116  for ( float yVal = yStartVal; yVal <= yEndVal; yVal += m_ySpacing )
117  {
118  coord1 = this->mapAdaptorToScene(Point2DType( xStartVal, yVal), m_xAxis, m_yAxis );
119  coord2 = this->mapAdaptorToScene(Point2DType( xEndVal, yVal), m_xAxis, m_yAxis );
120 
121  QGraphicsLineItem* line = new QGraphicsLineItem(coord1.first, coord1.second, coord2.first, coord2.second);
122 
123  // Set the line the pen and push it back in to the lines vector
124  line->setPen(m_pen);
125  m_lines.push_back(line);
126  }
127 
128  // Draw the vertical lines
129  for ( float xVal = xStartVal; xVal <= xEndVal; xVal += m_xSpacing )
130  {
131  coord1 = this->mapAdaptorToScene(Point2DType( xVal, yStartVal), m_xAxis, m_yAxis);
132  coord2 = this->mapAdaptorToScene(Point2DType( xVal, yEndVal), m_xAxis, m_yAxis);
133 
134  QGraphicsLineItem* line = new QGraphicsLineItem(coord1.first, coord1.second, coord2.first, coord2.second);
135 
136  // Set the line the pen and push it back in to the lines vector
137  line->setPen(m_pen);
138  m_lines.push_back(line);
139  }
140 
141  // Add the lines contained in the lines vector to the layer
142  for ( unsigned int i = 0; i < m_lines.size(); i++)
143  {
144  m_layer->addToGroup(m_lines.at(i));
145  }
146 
147  // Set the layer position (according to the related axis) and zValue
148  m_layer->setPos(m_xAxis->getOrigin(), m_yAxis->getOrigin());
149  m_layer->setZValue(m_zValue);
150 
151  // Add the layer containing grid's lines to the scene
152  this->getScene2DRender()->getScene()->addItem(m_layer);
153 }
154 
155 //---------------------------------------------------------------------------------------------------------------
156 
158 {
159  // Initialize the layer
160  m_layer = new QGraphicsItemGroup();
161 
162  // Set the pen a style
163  m_pen.setStyle(Qt::DashLine);
164  m_pen.setCosmetic(true);
165 
166  this->updating();
167 }
168 
169 //---------------------------------------------------------------------------------------------------------------
170 
171 float SGrid2D::getXStartVal()
172 {
173  return (int)( m_xMin / m_xSpacing ) * m_xSpacing;
174 }
175 
176 //---------------------------------------------------------------------------------------------------------------
177 
178 float SGrid2D::getXEndVal()
179 {
180  return (int)( m_xMax / m_xSpacing ) * m_xSpacing;
181 }
182 
183 //---------------------------------------------------------------------------------------------------------------
184 
185 float SGrid2D::getYStartVal()
186 {
187  return (int)( m_yMin / m_ySpacing ) * m_ySpacing;
188 }
189 
190 //---------------------------------------------------------------------------------------------------------------
191 
192 float SGrid2D::getYEndVal()
193 {
194  return (int)( m_yMax / m_ySpacing ) * m_ySpacing;
195 }
196 
197 //---------------------------------------------------------------------------------------------------------------
198 
199 void SGrid2D::setGridSpacing(double _x, double _y, std::string _key)
200 {
201  if(_key == "spacing")
202  {
203  m_xSpacing = static_cast<float>(_x);
204  m_ySpacing = static_cast<float>(_y);
205  this->draw();
206  }
207 }
208 
209 //---------------------------------------------------------------------------------------------------------------
210 
212 {
213  this->draw();
214 }
215 
216 //---------------------------------------------------------------------------------------------------------------
217 
219 {
220  if( _event.getType() == ::fwRenderQt::data::Event::Resize)
221  {
222  this->updating();
223  }
224 }
225 
226 //---------------------------------------------------------------------------------------------------------------
227 
229 {
230  // Clear the lines vector
231  m_lines.clear();
232 
233  // Remove the layer (and therefore all its related items) from the scene
234  this->getScene2DRender()->getScene()->removeItem(m_layer);
235 }
236 
237 } // namespace adaptor
238 } // namespace scene2D
239 
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 ...
Definition: SGrid2D.cpp:211
SCENE2D_API void configuring() override
Configure the service before starting. Apply the configuration to service.
Definition: SGrid2D.cpp:51
SCENE2D_API void starting() override
Initialize the layer, set the pen style to DashLine and call the draw() function. ...
Definition: SGrid2D.cpp:157
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< double, double > Point2DType
Point2D coordinate <X, Y>
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.
Render grid on the scene2d.
Definition: SGrid2D.hpp:47
#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::data::Axis::sptr m_yAxis
The y Axis.
float m_opacity
Opacity of the adaptor. Default value set to 1 (opaque).
virtual SCENE2D_API ~SGrid2D() noexcept
Basic destructor, do nothing.
Definition: SGrid2D.cpp:45
FWRENDERQT_API void configureParams()
Parse the xml configuration for Axis, z value and opacity.
SCENE2D_API void stopping() override
Clean the lines vector and remove the layer from the scene.
Definition: SGrid2D.cpp:228
SCENE2D_API SGrid2D() noexcept
Constructor, set the x and y spacing to 10.
Definition: SGrid2D.cpp:31
This class manage events on the scene 2D (mouse event, keyboard event , ...).
Definition: Event.hpp:26
SCENE2D_API void processInteraction(::fwRenderQt::data::Event &_event) override
Manage the given events.
Definition: SGrid2D.cpp:218
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247