fw4spl
scene2D/src/scene2D/adaptor/SLine.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/SLine.hpp"
8 
9 #include <fwRenderQt/data/InitQtPen.hpp>
10 
11 #include <fwServices/macros.hpp>
12 
13 #include <QGraphicsItemGroup>
14 
15 fwServicesRegisterMacro( ::fwRenderQt::IAdaptor, ::scene2D::adaptor::SLine);
16 
17 namespace scene2D
18 {
19 namespace adaptor
20 {
21 
22 SLine::SLine() noexcept :
23  m_x1(0.f),
24  m_x2(0.f),
25  m_y1(0.f),
26  m_y2(0.f),
27  m_layer(nullptr)
28 {
29 }
30 
31 //---------------------------------------------------------------------------------------------------------------
32 
33 SLine::~SLine() noexcept
34 {
35 }
36 
37 //---------------------------------------------------------------------------------------------------------------
38 
40 {
41  this->configureParams();
42 
43  const ConfigType config = this->getConfigTree().get_child("config.<xmlattr>");
44 
45  SLM_ASSERT("Attribute 'x1' is missing", config.count("x1"));
46  SLM_ASSERT("Attribute 'x2' is missing", config.count("x2"));
47  SLM_ASSERT("Attribute 'y1' is missing", config.count("y1"));
48  SLM_ASSERT("Attribute 'y2' is missing", config.count("y2"));
49 
50  // Set the beginning and ending coordinates values
51  m_x1 = config.get<float>("x1");
52  m_x2 = config.get<float>("x2");
53  m_y1 = config.get<float>("y1");
54  m_y2 = config.get<float>("y2");
55 
56  // If the corresponding attributes are present in the config, set the color of the line
57  if (config.count("color"))
58  {
59  ::fwRenderQt::data::InitQtPen::setPenColor(m_pen, config.get<std::string>("color"));
60  }
61 }
62 
63 //---------------------------------------------------------------------------------------------------------------
64 
65 void SLine::draw()
66 {
67  const Point2DType pt1 = this->mapAdaptorToScene(Point2DType( m_x1, m_y1), m_xAxis, m_yAxis);
68  const Point2DType pt2 = this->mapAdaptorToScene(Point2DType( m_x2, m_y2), m_xAxis, m_yAxis);
69 
70  // Draw the line
71  QGraphicsLineItem* line = new QGraphicsLineItem(pt1.first, pt1.second,
72  pt2.first, pt2.second);
73  // Set the line the pen
74  line->setPen(m_pen);
75 
76  // Add the line to the layer
77  m_layer->addToGroup(line);
78 
79  // Set the layer position (according to the related axis) and zValue
80  m_layer->setPos(m_xAxis->getOrigin(), m_yAxis->getOrigin());
81  m_layer->setZValue(m_zValue);
82  // Add the layer to the scene
83  this->getScene2DRender()->getScene()->addItem(m_layer);
84 }
85 
86 //---------------------------------------------------------------------------------------------------------------
87 
89 {
90  // Initialize the layer
91  m_layer = new QGraphicsItemGroup();
92  m_pen.setCosmetic(true);
93 
94  this->draw();
95 }
96 
97 //---------------------------------------------------------------------------------------------------------------
98 
100 {
101  this->draw();
102 }
103 
104 //---------------------------------------------------------------------------------------------------------------
105 
107 {
108  // Remove the layer (and therefore its related line item) from the scene
109  this->getScene2DRender()->getScene()->removeItem(m_layer);
110 }
111 
112 } // namespace adaptor
113 } // namespace scene2D
114 
Root class for all scene2d adaptors.
::fwRenderQt::data::Axis::sptr m_xAxis
The x Axis.
SCENE2D_API void stopping() override
Remove the layer from the scene.
virtual SCENE2D_API ~SLine() noexcept
Basic destructor, do nothing.
Line adaptor. Draw a line on the scene2D.
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.
SCENE2D_API SLine() noexcept
Basic constructor, do nothing.
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.
#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.
FWRENDERQT_API void configureParams()
Parse the xml configuration for Axis, z value and opacity.
SCENE2D_API void starting() override
Initialize the layer and call the draw() function.
SCENE2D_API void configuring() override
Configure the service before starting. Apply the configuration to service.
SCENE2D_API void updating() override
Do nothing.
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247