7 #include "scene2D/adaptor/SViewportRangeSelector.hpp" 9 #include <fwCom/Signal.hpp> 10 #include <fwCom/Signal.hxx> 12 #include <fwRenderQt/data/Viewport.hpp> 13 #include <fwRenderQt/Scene2DGraphicsView.hpp> 15 #include <fwServices/macros.hpp> 17 #include <QGraphicsRectItem> 26 static const ::fwServices::IService::KeyType s_VIEWPORT_INOUT =
"viewport";
30 SViewportRangeSelector::SViewportRangeSelector() noexcept :
32 m_isLeftInteracting( false ),
33 m_isRightInteracting( false ),
34 m_isInteracting( false ),
35 m_clickCatchRange( 15 ),
44 SViewportRangeSelector::~SViewportRangeSelector() noexcept
54 const ConfigType config = this->
getConfigTree().get_child(
"config.<xmlattr>");
55 if (config.count(
"initialWidth"))
57 m_initialWidth = config.get<
float>(
"initialWidth");
60 if (config.count(
"initialPos"))
62 m_initialX = config.get<
float>(
"initialPos");
71 ::fwRenderQt::data::Viewport::sptr sceneViewport = this->
getScene2DRender()->getViewport();
73 const double viewportWidth = sceneViewport->getWidth();
74 const double defaultWidth = 2. * viewportWidth / 4.;
76 if( m_initialWidth > viewportWidth || m_initialWidth < m_clickCatchRange )
78 SLM_WARN(
"Set viewport width to a default value instead of the given one because it can't be accepted.");
79 m_initialWidth = defaultWidth;
82 const double defaultPos = (viewportWidth - m_initialWidth) / 2.;
83 if( m_initialX < sceneViewport->getX() || (m_initialX + m_initialWidth) > viewportWidth)
85 SLM_WARN(
"Set viewport position to a default value since the given one is not correct.");
86 m_initialX = defaultPos;
90 ::fwRenderQt::data::Viewport::sptr viewport =
91 this->getInOut< ::fwRenderQt::data::Viewport>(s_VIEWPORT_INOUT);
96 m_shutter =
new QGraphicsRectItem(
97 pair.first, 0, m_initialWidth *
m_xAxis->getScale(), pair.second );
98 m_shutter->setBrush( QBrush(QColor(127, 127, 127, 127)) );
99 m_shutter->setPen( Qt::NoPen );
101 m_layer =
new QGraphicsItemGroup();
102 m_layer->addToGroup( m_shutter );
110 QRectF rect = m_shutter->rect();
113 ::fwData::Object::ModifiedSignalType::sptr sig;
143 Point2DType( m_shutter->rect().x(), m_shutter->rect().y()),
145 const double shutterWidth = m_shutter->rect().width() *
m_xAxis->getScale();
149 const bool onShutterLeft = mouseOnShutterLeft( coord );
150 const bool onShutterRight = mouseOnShutterRight( coord );
151 const bool onShutterMiddle = mouseOnShutterMiddle( coord );
153 QRectF rect = m_shutter->rect();
155 if( _event.getType() == ::fwRenderQt::data::Event::MouseButtonPress )
159 m_isLeftInteracting =
true;
161 else if( onShutterRight )
163 m_isRightInteracting =
true;
165 else if( onShutterMiddle )
170 m_isInteracting =
true;
171 m_dragStartPoint = coord;
172 m_dragStartShutterPos.setX( shutterCoordPair.first );
173 m_dragStartShutterPos.setY( shutterCoordPair.second );
176 else if( _event.getType() == ::fwRenderQt::data::Event::MouseButtonRelease )
178 m_isInteracting =
false;
179 m_isLeftInteracting =
false;
180 m_isRightInteracting =
false;
183 if( onShutterMiddle )
192 else if( _event.getType() == ::fwRenderQt::data::Event::MouseMove )
196 if( !m_isLeftInteracting && !m_isRightInteracting && !m_isInteracting )
198 if( onShutterLeft || onShutterRight )
202 else if( onShutterMiddle )
216 if( m_isLeftInteracting )
219 const double rightSide = rect.x() + rect.width();
221 if( coord.getX() < rightSide - m_clickCatchRange)
223 if( coord.getX() >= sceneRect.x() )
225 rect.setX( coord.getX() );
229 rect.setX( sceneRect.x() );
234 rect.setX( rightSide - m_clickCatchRange );
239 else if( m_isRightInteracting )
241 const double newWidth = coord.getX() - shutterCoordPair.first;
242 const double shutterRightPos = abs(sceneRect.x()) + shutterCoordPair.first + newWidth;
244 if( newWidth > m_clickCatchRange )
246 if( shutterRightPos < sceneRect.width() )
248 rect.setWidth( newWidth );
252 rect.setWidth( sceneRect.width() - shutterCoordPair.first - abs(sceneRect.x()) );
257 rect.setWidth( m_clickCatchRange );
262 else if( m_isInteracting )
264 const double offset = coord.getX() - m_dragStartPoint.getX();
265 const double newX = m_dragStartShutterPos.getX() + offset;
266 const double shutterRightPos = abs(sceneRect.x()) + newX + shutterWidth;
268 if( newX >= sceneRect.x() && shutterRightPos < sceneRect.width() )
272 else if( newX < sceneRect.x() )
274 rect.setX( sceneRect.x() );
276 else if( shutterRightPos >= sceneRect.width() )
278 rect.setX( sceneRect.width() - shutterWidth - abs(sceneRect.x()) );
281 rect.setWidth( shutterWidth );
288 m_shutter->setRect( rect );
289 m_layer->removeFromGroup( m_shutter );
290 m_layer->addToGroup( m_shutter );
295 ::fwRenderQt::data::Viewport::sptr viewport =
296 this->getInOut< ::fwRenderQt::data::Viewport>(s_VIEWPORT_INOUT);
298 ::fwData::Object::ModifiedSignalType::sptr sig =
312 ::fwRenderQt::data::Viewport::sptr viewport =
313 this->getInOut< ::fwRenderQt::data::Viewport>(s_VIEWPORT_INOUT);
316 viewport->setX( fromSceneCoord.first );
317 viewport->setY( fromSceneCoord.second );
320 viewport->setWidth( pair.first );
321 viewport->setHeight( this->
getScene2DRender()->getViewport()->getHeight() );
332 return ( _coord.getX() > m_shutter->rect().x() + m_clickCatchRange )
333 && ( _coord.getX() < m_shutter->rect().x() + m_shutter->rect().width() - m_clickCatchRange );
343 return ( _coord.getX() >= shutterCoordPair.first - m_clickCatchRange )
344 && ( _coord.getX() <= shutterCoordPair.first + m_clickCatchRange );
352 Point2DType( m_shutter->rect().x(), m_shutter->rect().y()),
355 const double shutterRightPos = shutterCoordPair.first + m_shutter->rect().width() *
m_xAxis->getScale();
357 return ( _coord.getX() >= shutterRightPos - m_clickCatchRange )
358 && ( _coord.getX() <= shutterRightPos + m_clickCatchRange );
Root class for all scene2d adaptors.
::fwRenderQt::data::Axis::sptr m_xAxis
The x Axis.
This class is a helper to define the connections of a service and its data.
SCENE2D_API void updateViewportFromShutter(double x, double y, double width, double height)
Class allowing to block a Connection.
FWRENDERQT_API Point2DType mapAdaptorToScene(const Point2DType &_xy, const ::fwRenderQt::data::Axis::sptr &_xAxis, const ::fwRenderQt::data::Axis::sptr &_yAxis) const
SCENE2D_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
FWSERVICES_API SharedFutureType update()
Invoke updating() if m_globalState == STARTED. Does nothing otherwise.
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>
#define SLM_WARN(message)
SCENE2D_API void processInteraction(::fwRenderQt::data::Event &_event) override
SCENE2D_API void configuring() override
Configure the service before starting. Apply the configuration to service.
This bundles contains data and services used to display a 2D Qt scene.
UpdateSlotType::sptr m_slotUpdate
Slot to call update method.
FWRENDERQT_API Point2DType mapSceneToAdaptor(const Point2DType &_xy, const ::fwRenderQt::data::Axis::sptr &_xAxis, const ::fwRenderQt::data::Axis::sptr &_yAxis) const
::fwRenderQt::data::Axis::sptr m_yAxis
The y Axis.
FWRENDERQT_API void configureParams()
Parse the xml configuration for Axis, z value and opacity.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
The viewport range selector adaptor allows to select a delimited range of a viewport. It uses a graphical delimiter (called shutter) that can be moved from both left to right and right to left directions (in those cases, shutter's width is changing).
SCENE2D_API KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated objects signals, this method is used for obj...
SCENE2D_API void starting() override
Initialize the service activity.
This class manage events on the scene 2D (mouse event, keyboard event , ...).
SCENE2D_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_SLOT
Slot to call start method.
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.