fw4spl
SSquare.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/SSquare.hpp"
8 
9 #include <fwServices/macros.hpp>
10 
11 #include <QGraphicsItemGroup>
12 
13 fwServicesRegisterMacro( ::fwRenderQt::IAdaptor, ::scene2D::adaptor::SSquare);
14 
15 namespace scene2D
16 {
17 namespace adaptor
18 {
19 
20 //-----------------------------------------------------------------------------
21 
22 SSquare::SSquare() noexcept :
23  m_size(0),
24  m_layer(nullptr),
25  m_rec(nullptr),
26  m_pointIsCaptured(false)
27 {
28 }
29 
30 //-----------------------------------------------------------------------------
31 
32 SSquare::~SSquare() noexcept
33 {
34 }
35 
36 //-----------------------------------------------------------------------------
37 
39 {
40  this->configureParams();
41 
42  const ConfigType config = this->getConfigTree().get_child("config.<xmlattr>");
43 
44  SLM_ASSERT("Attribute 'x' is missing", config.count("x"));
45  SLM_ASSERT("Attribute 'y' is missing", config.count("y"));
46  SLM_ASSERT("Attribute 'size' is missing", config.count("size"));
47 
48  m_coord.setX( config.get<double>("x") );
49  m_coord.setY( config.get<double>("y") );
50  m_size = config.get<std::uint32_t>("size");
51 
52  if ( config.count("color") )
53  {
54  this->setColor(config.get<std::string>("color"));
55  }
56 }
57 
58 //-----------------------------------------------------------------------------
59 
61 {
62  m_layer = new QGraphicsItemGroup();
63 
64  m_rec = new QGraphicsRectItem( m_coord.getX(), m_coord.getY(), m_size, m_size );
65  QPen pen;
66  pen.setColor( Qt::GlobalColor( Qt::black ) );
67  m_rec->setPen( pen );
68  m_rec->setBrush( m_color );
69  m_layer->addToGroup(m_rec);
70  m_layer->setOpacity(0.8);
71 
72  m_layer->setPos( m_xAxis->getOrigin(), m_yAxis->getOrigin() );
73  m_layer->setZValue( m_zValue );
74  this->getScene2DRender()->getScene()->addItem(m_layer);
75 }
76 
77 //-----------------------------------------------------------------------------
78 
80 {
81 }
82 
83 //-----------------------------------------------------------------------------
84 
86 {
87  this->getScene2DRender()->getScene()->removeItem(m_layer);
88 }
89 
90 //-----------------------------------------------------------------------------
91 
92 void SSquare::setColor(const std::string& _color )
93 {
94  m_color = QColor(QString::fromStdString(_color));
95  if (!m_color.isValid())
96  {
97  // Default value: black (if the color id cannot be parsed)
98  m_color = Qt::GlobalColor(Qt::white);
99  SLM_WARN("Color "+_color+" unknown, default value used.");
100  }
101 }
102 
103 //-----------------------------------------------------------------------------
104 
106 {
107  if ( _event.getType() == ::fwRenderQt::data::Event::MouseButtonPress &&
108  _event.getButton() == ::fwRenderQt::data::Event::LeftButton )
109  {
110  if ( this->coordViewIsInItem( _event.getCoord(), m_rec ) )
111  {
112  SLM_TRACE("Point is captured");
113  m_pointIsCaptured = true;
114  m_oldCoord = this->coordViewToCoordItem( _event.getCoord(), m_rec );
115  m_rec->setBrush( Qt::yellow );
116  _event.setAccepted(true);
117  }
118  }
119  else if ( m_pointIsCaptured && _event.getType() == ::fwRenderQt::data::Event::MouseMove )
120  {
121  ::fwRenderQt::data::Coord newCoord = this->coordViewToCoordItem( _event.getCoord(), m_rec );
122  m_rec->moveBy( newCoord.getX() - m_oldCoord.getX(), newCoord.getY() - m_oldCoord.getY() );
123  m_oldCoord = newCoord;
124  _event.setAccepted(true);
125  }
126  else if ( m_pointIsCaptured && _event.getType() == ::fwRenderQt::data::Event::MouseButtonRelease )
127  {
128  m_rec->setBrush( m_color );
129  m_pointIsCaptured = false;
130  _event.setAccepted(true);
131  }
132 }
133 
134 //-----------------------------------------------------------------------------
135 
136 bool SSquare::coordViewIsInItem( const ::fwRenderQt::data::Coord& coord, QGraphicsItem* item )
137 {
138  ::fwRenderQt::data::Coord scenePoint = this->getScene2DRender()->mapToScene( coord );
139  QPointF sp( scenePoint.getX(), scenePoint.getY() );
140  QPointF ip = item->mapFromScene( sp );
141  return item->contains( ip );
142 }
143 
144 //-----------------------------------------------------------------------------
145 
146 ::fwRenderQt::data::Coord SSquare::coordViewToCoordItem( const ::fwRenderQt::data::Coord& coord, QGraphicsItem* item )
147 {
148  ::fwRenderQt::data::Coord scenePoint = this->getScene2DRender()->mapToScene( coord );
149  //QPointF sp ( scenePoint.getX(), scenePoint.getY() );
150  //QPointF ip = item->mapFromScene( sp );
151  //return ::fwRenderQt::data::Coord( ip.x(), ip.y() );
152  return scenePoint;
153 }
154 //-----------------------------------------------------------------------------
155 
156 } // namespace adaptor
157 } // namespace scene2D
158 
Root class for all scene2d adaptors.
::fwRenderQt::data::Axis::sptr m_xAxis
The x Axis.
Create a square on the scene2D.
Definition: SSquare.hpp:41
FWRENDERQT_API std::shared_ptr< ::fwRenderQt::SRender > getScene2DRender() const
Get the render that manages the IAdaptor.
SCENE2D_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
Definition: SSquare.cpp:79
SCENE2D_API void configuring() override
Configure the service before starting. Apply the configuration to service.
Definition: SSquare.cpp:38
#define SLM_WARN(message)
Definition: spyLog.hpp:261
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
SCENE2D_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
Definition: SSquare.cpp:85
::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 processInteraction(::fwRenderQt::data::Event &_event) override
Definition: SSquare.cpp:105
#define SLM_TRACE(message)
Definition: spyLog.hpp:228
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.
Definition: SSquare.cpp:60
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247