7 #include "scene2D/adaptor/SSquare.hpp" 9 #include <fwServices/macros.hpp> 11 #include <QGraphicsItemGroup> 22 SSquare::SSquare() noexcept :
26 m_pointIsCaptured(false)
32 SSquare::~SSquare() noexcept
42 const ConfigType config = this->
getConfigTree().get_child(
"config.<xmlattr>");
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"));
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");
52 if ( config.count(
"color") )
54 this->setColor(config.get<std::string>(
"color"));
62 m_layer =
new QGraphicsItemGroup();
64 m_rec =
new QGraphicsRectItem( m_coord.getX(), m_coord.getY(), m_size, m_size );
66 pen.setColor( Qt::GlobalColor( Qt::black ) );
68 m_rec->setBrush( m_color );
69 m_layer->addToGroup(m_rec);
70 m_layer->setOpacity(0.8);
92 void SSquare::setColor(
const std::string& _color )
94 m_color = QColor(QString::fromStdString(_color));
95 if (!m_color.isValid())
98 m_color = Qt::GlobalColor(Qt::white);
99 SLM_WARN(
"Color "+_color+
" unknown, default value used.");
107 if ( _event.getType() == ::fwRenderQt::data::Event::MouseButtonPress &&
108 _event.getButton() == ::fwRenderQt::data::Event::LeftButton )
110 if ( this->coordViewIsInItem( _event.getCoord(), m_rec ) )
113 m_pointIsCaptured =
true;
114 m_oldCoord = this->coordViewToCoordItem( _event.getCoord(), m_rec );
115 m_rec->setBrush( Qt::yellow );
116 _event.setAccepted(
true);
119 else if ( m_pointIsCaptured && _event.getType() == ::fwRenderQt::data::Event::MouseMove )
122 m_rec->moveBy( newCoord.getX() - m_oldCoord.getX(), newCoord.getY() - m_oldCoord.getY() );
123 m_oldCoord = newCoord;
124 _event.setAccepted(
true);
126 else if ( m_pointIsCaptured && _event.getType() == ::fwRenderQt::data::Event::MouseButtonRelease )
128 m_rec->setBrush( m_color );
129 m_pointIsCaptured =
false;
130 _event.setAccepted(
true);
136 bool SSquare::coordViewIsInItem( const ::fwRenderQt::data::Coord& coord, QGraphicsItem* item )
139 QPointF sp( scenePoint.getX(), scenePoint.getY() );
140 QPointF ip = item->mapFromScene( sp );
141 return item->contains( ip );
Root class for all scene2d adaptors.
::fwRenderQt::data::Axis::sptr m_xAxis
The x Axis.
Create a square on the scene2D.
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 ...
SCENE2D_API void configuring() override
Configure the service before starting. Apply the configuration to service.
#define SLM_WARN(message)
This bundles contains data and services used to display a 2D Qt scene.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
SCENE2D_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
::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
#define SLM_TRACE(message)
This class manage events on the scene 2D (mouse event, keyboard event , ...).
SCENE2D_API void starting() override
Initialize the service activity.
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.