7 #include "fwRenderQt/SRender.hpp" 9 #include "fwRenderQt/IAdaptor.hpp" 10 #include "fwRenderQt/registry/Adaptor.hpp" 11 #include "fwRenderQt/Scene2DGraphicsView.hpp" 13 #include <fwCom/helper/SigSlotConnection.hpp> 14 #include <fwCom/Slot.hpp> 15 #include <fwCom/Slot.hxx> 16 #include <fwCom/Slots.hpp> 17 #include <fwCom/Slots.hxx> 19 #include <fwDataTools/Color.hpp> 21 #include <fwGuiQt/container/QtContainer.hpp> 23 #include <fwServices/helper/Config.hpp> 24 #include <fwServices/macros.hpp> 25 #include <fwServices/op/Add.hpp> 27 #include <QGraphicsRectItem> 28 #include <QVBoxLayout> 34 m_sceneStart(-100., -100.),
35 m_sceneWidth(200., 200.),
38 m_antialiasing(false),
39 m_background("
#000000"), 40 m_aspectRatioMode(Qt::IgnoreAspectRatio)
75 ::fwRenderQt::data::Axis::sptr axis;
76 const auto iter = m_axisMap.find(
id);
77 if(iter != m_axisMap.end())
93 if ( !_event.isAccepted() )
95 const auto&
registry = ::fwRenderQt::registry::getAdaptorRegistry();
98 if(elt.second == this->getID())
100 ::fwRenderQt::IAdaptor::sptr adaptor =
102 if(adaptor !=
nullptr && adaptor->isStarted())
104 adaptor->processInteraction( _event );
116 const QPoint qp( static_cast<int>(coord.getX()), static_cast<int>(coord.getY()) );
117 const QPointF qps = m_view->mapToScene(qp);
118 return ::fwRenderQt::data::Coord(qps.x(), qps.y());
127 std::vector < SPTR(::fwRuntime::ConfigurationElement) > vectConfig =
m_configuration->find(
"scene");
128 SLM_ASSERT(
"There is no implementation between \"scene\" tags", !vectConfig.empty());
129 m_sceneConfiguration = vectConfig.at(0);
132 for (iter = m_sceneConfiguration->begin(); iter != m_sceneConfiguration->end(); ++iter)
134 if ((*iter)->getName() ==
"axis")
136 this->configureAxis(*iter);
138 else if ((*iter)->getName() ==
"viewport")
140 this->configureViewport(*iter);
142 else if ((*iter)->getName() ==
"scene")
144 this->configureScene(*iter);
146 else if ((*iter)->getName() ==
"adaptor")
148 this->configureAdaptor(*iter);
152 SLM_ASSERT(
"Bad scene configurationType, unknown xml node : " + (*iter)->getName(),
false);
163 this->startContext();
196 void SRender::startContext()
199 = ::fwGuiQt::container::QtContainer::dynamicCast(this->getContainer());
202 std::uint8_t color[4];
205 m_scene =
new QGraphicsScene( m_sceneStart.getX(), m_sceneStart.getY(), m_sceneWidth.getX(), m_sceneWidth.getY());
206 m_scene->setBackgroundBrush(QBrush(QColor(color[0], color[1], color[2], color[3])));
207 m_scene->setFocus( Qt::MouseFocusReason );
210 m_view->setViewport( m_viewport );
211 m_view->setSceneRender( ::fwRenderQt::SRender::dynamicCast( this->getSptr() ) );
212 m_view->setRenderHint( QPainter::Antialiasing, m_antialiasing );
214 QVBoxLayout* layout =
new QVBoxLayout;
215 layout->addWidget(m_view);
216 qtContainer->setLayout(layout);
218 m_view->updateFromViewport();
223 void SRender::stopContext()
233 return m_aspectRatioMode;
238 void SRender::configureAxis( ConfigurationType _conf )
240 SLM_ASSERT(
"\"axis\" tag required", _conf->getName() ==
"axis");
242 const std::string
id = _conf->getAttributeValue(
"id");
243 const std::string origin = _conf->getAttributeValue(
"origin");
244 const std::string scale = _conf->getAttributeValue(
"scale");
245 const std::string scaleType = _conf->getAttributeValue(
"scaleType");
247 ::fwRenderQt::data::Axis::sptr axis = std::make_shared< ::fwRenderQt::data::Axis >();
248 axis->setOrigin(std::stof( origin ));
249 axis->setScale(std::stof( scale ));
250 axis->setScaleType( scaleType ==
"LINEAR" ? ::fwRenderQt::data::Axis::LINEAR : ::fwRenderQt::data::Axis::LOG);
251 m_axisMap[id] = axis;
256 void SRender::configureViewport( ConfigurationType _conf )
258 SLM_ASSERT(
"\"viewport\" tag required", _conf->getName() ==
"viewport");
260 const std::string
id = _conf->getAttributeValue(
"id");
261 const std::string
x = _conf->getAttributeValue(
"x");
262 const std::string y = _conf->getAttributeValue(
"y");
263 const std::string width = _conf->getAttributeValue(
"width");
264 const std::string height = _conf->getAttributeValue(
"height");
266 m_viewport = ::fwRenderQt::data::Viewport::New();
267 m_viewport->setX(std::stof( x ));
268 m_viewport->setY(std::stof( y ));
269 m_viewport->setWidth(std::stof( width ));
270 m_viewport->setHeight(std::stof( height ));
275 void SRender::configureScene( ConfigurationType _conf )
277 SLM_ASSERT(
"\"scene\" tag required", _conf->getName() ==
"scene");
279 const std::string
x = _conf->getAttributeValue(
"x");
280 const std::string y = _conf->getAttributeValue(
"y");
281 const std::string width = _conf->getAttributeValue(
"width");
282 const std::string height = _conf->getAttributeValue(
"height");
284 m_sceneStart.setX( std::stof( x ) );
285 m_sceneStart.setY( std::stof( y ) );
286 m_sceneWidth.setX( std::stof( width ) );
287 m_sceneWidth.setY( std::stof( height ) );
289 if( _conf->hasAttribute(
"antialiasing"))
291 if( _conf->getAttributeValue(
"antialiasing") ==
"true")
293 m_antialiasing =
true;
297 if( _conf->hasAttribute(
"aspectRatioMode"))
299 const std::string aspectRatio = _conf->getAttributeValue(
"aspectRatioMode");
300 if(aspectRatio ==
"KeepAspectRatioByExpanding")
302 m_aspectRatioMode = Qt::KeepAspectRatioByExpanding;
304 else if(aspectRatio ==
"KeepAspectRatio")
306 m_aspectRatioMode = Qt::KeepAspectRatio;
312 "). Possible values are: KeepAspectRatio, KeepAspectRatioByExpanding or IgnoreAspectRatio.",
313 aspectRatio !=
"IgnoreAspectRatio");
314 m_aspectRatioMode = Qt::IgnoreAspectRatio;
318 if ( _conf->hasAttribute((
"background")) )
320 m_background = _conf->getAttributeValue(
"background");
321 SLM_ASSERT(
"Color format must be hexadecimal.", m_background[0] ==
'#');
327 void SRender::configureAdaptor( ConfigurationType _conf )
329 SLM_ASSERT(
"\"adaptor\" tag required", _conf->getName() ==
"adaptor");
331 const std::string adaptorId = _conf->getAttributeValue(
"uid");
333 auto&
registry = ::fwRenderQt::registry::getAdaptorRegistry();
341 QRectF rec = m_scene->itemsBoundingRect();
343 rec.getRect(&x, &y, &w, &h);
345 if ( ratioPercent != 0 )
347 qreal centerX = x + w/2.0;
348 qreal centerY = y + h/2.0;
349 w = w + w * ratioPercent;
350 h = h + h * ratioPercent;
353 rec.setRect(x, y, w, h);
355 m_sceneStart.setX( x );
356 m_sceneStart.setY( y );
357 m_sceneWidth.setX( w );
358 m_sceneWidth.setY( h );
360 m_scene->setSceneRect( rec );
Contains fwAtomsFilter::registry details.
FWRENDERQT_API void swapping() override
Swap the service from associated object to another object.
FWGUI_API void destroy()
Stops sub-views and toobar services. Destroys view, sub-views and toolbar containers.
FWRENDERQT_API void stopping() override
FWRENDERQT_API void updateSceneSize(float ratioPercent=0)
Update scene size from items bounding rect, this bounding can be enlarged with ratioPercent parameter...
FWRENDERQT_API void starting() override
FWRENDERQT_API::fwRenderQt::data::Coord mapToScene(const ::fwRenderQt::data::Coord &coord) const
Returns the viewport coordinate point mapped to scene coordinates.
virtual FWRENDERQT_API ~SRender() noexcept
Basic destructor, do nothing.
FWRENDERQT_API::fwRenderQt::data::Viewport::sptr getViewport() const
Get the viewport.
FWRENDERQT_API Qt::AspectRatioMode getAspectRatioMode() const
Returns what happens to scene's aspect ratio on view resize events.
FWRENDERQT_API QGraphicsScene * getScene() const
Get the scene.
FWRENDERQT_API::fwRenderQt::data::Axis::sptr getAxis(const std::string &id) const
Get the axis.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
FWGUI_API void create()
Creates view, sub-views and toolbar containers. Manages sub-views and toobar services.
FWRENDERQT_API void configuring() override
Get configuration options from XML.
FWRENDERQT_API void updating() override
Do nothing.
The namespace fwRenderQt contains classes for rendering with Qt.
FWRENDERQT_API SRender() noexcept
Container::iterator Iterator
Defines the configuration element container type.
FWRENDERQT_API void dispatchInteraction(::fwRenderQt::data::Event &_event)
FWRENDERQT_API Scene2DGraphicsView * getView() const
Get the view.
This class manage events on the scene 2D (mouse event, keyboard event , ...).
#define OSLM_ERROR_IF(message, cond)
Defines the QWidget container for UI.
FWGUI_API void initialize()
Initialize managers.