7 #include "fwRenderQt/Scene2DGraphicsView.hpp" 9 #include "fwRenderQt/data/Size.hpp" 10 #include "fwRenderQt/SRender.hpp" 12 #include <QMouseEvent> 19 Scene2DGraphicsView::Scene2DGraphicsView(QGraphicsScene* scene, QWidget* widget) :
20 QGraphicsView(scene, widget)
22 if( !this->hasMouseTracking() )
24 this->setMouseTracking(
true );
34 scene->setItemIndexMethod( QGraphicsScene::NoIndex );
36 this->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
37 this->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
39 this->setViewportUpdateMode( QGraphicsView::BoundingRectViewportUpdate );
40 this->setOptimizationFlags( QGraphicsView::DontSavePainterState
41 | QGraphicsView::DontAdjustForAntialiasing );
42 this->setCacheMode( QGraphicsView::CacheBackground );
43 this->setAttribute( Qt::WA_TranslucentBackground,
false );
44 this->setFrameStyle( 0 );
49 void Scene2DGraphicsView::setSceneRender( ::fwRenderQt::SRender::sptr sceneRender )
51 m_scene2DRender = sceneRender;
56 void Scene2DGraphicsView::keyPressEvent(QKeyEvent* _event)
59 sceneEvent.setType( ::fwRenderQt::data::Event::KeyPress);
60 sceneEvent.setButton( ::fwRenderQt::data::Event::NoButton);
61 sceneEvent.setModifier( this->getScene2DModifierFromEvent( _event) );
62 sceneEvent.setKey(_event->key());
64 m_scene2DRender.lock()->dispatchInteraction( sceneEvent );
69 void Scene2DGraphicsView::keyReleaseEvent(QKeyEvent* _event)
72 sceneEvent.setType( ::fwRenderQt::data::Event::KeyRelease);
73 sceneEvent.setButton( ::fwRenderQt::data::Event::NoButton);
74 sceneEvent.setModifier( this->getScene2DModifierFromEvent( _event) );
75 sceneEvent.setKey(_event->key());
77 m_scene2DRender.lock()->dispatchInteraction( sceneEvent );
82 void Scene2DGraphicsView::resizeEvent(QResizeEvent* _event)
84 this->updateFromViewport();
87 sceneEvent.setType( ::fwRenderQt::data::Event::Resize);
88 sceneEvent.setButton( ::fwRenderQt::data::Event::NoButton);
89 sceneEvent.setModifier( ::fwRenderQt::data::Event::NoModifier);
91 sceneEvent.setOldSize( ::
fwRenderQt::data::Size( _event->oldSize().width(), _event->oldSize().height() ) );
93 m_scene2DRender.lock()->dispatchInteraction( sceneEvent );
98 void Scene2DGraphicsView::mousePressEvent ( QMouseEvent* _event )
100 OSLM_TRACE(
"Press in x = " << _event->localPos().x() <<
" y = " << _event->localPos().y() );
103 sceneEvent.setType( ::fwRenderQt::data::Event::MouseButtonPress );
105 sceneEvent.setButton( this->getScene2DButtonFromEvent( _event ) );
106 sceneEvent.setModifier( this->getScene2DModifierFromEvent( _event) );
108 m_scene2DRender.lock()->dispatchInteraction( sceneEvent );
113 ::fwRenderQt::data::Event::Modifier Scene2DGraphicsView::getScene2DModifierFromEvent( QInputEvent* _event )
115 ::fwRenderQt::data::Event::Modifier modifier;
117 if(_event->modifiers() == Qt::ControlModifier)
119 modifier = ::fwRenderQt::data::Event::ControlModifier;
121 else if(_event->modifiers() == Qt::AltModifier)
123 modifier = ::fwRenderQt::data::Event::AltModifier;
125 else if(_event->modifiers() == Qt::ShiftModifier)
127 modifier = ::fwRenderQt::data::Event::ShiftModifier;
131 modifier = ::fwRenderQt::data::Event::NoModifier;
140 ::fwRenderQt::data::Event::Button Scene2DGraphicsView::getScene2DButtonFromEvent( QMouseEvent* _event )
142 ::fwRenderQt::data::Event::Button button;
144 if( _event->button() == Qt::LeftButton )
146 button = ::fwRenderQt::data::Event::LeftButton;
148 else if( _event->button() == Qt::RightButton )
150 button = ::fwRenderQt::data::Event::RightButton;
152 else if( _event->button() == Qt::MidButton )
154 button = ::fwRenderQt::data::Event::MidButton;
158 button = ::fwRenderQt::data::Event::NoButton;
166 void Scene2DGraphicsView::mouseDoubleClickEvent ( QMouseEvent* _event )
168 OSLM_TRACE(
"DoubleClick in x = " << _event->localPos().x() <<
" y = " << _event->localPos().y() );
171 sceneEvent.setType( ::fwRenderQt::data::Event::MouseButtonDoubleClick );
173 sceneEvent.setButton( this->getScene2DButtonFromEvent( _event ) );
174 sceneEvent.setModifier( this->getScene2DModifierFromEvent( _event) );
176 m_scene2DRender.lock()->dispatchInteraction( sceneEvent );
181 void Scene2DGraphicsView::mouseReleaseEvent ( QMouseEvent* _event )
183 OSLM_TRACE(
"Release in x = " << _event->localPos().x() <<
" y = " << _event->localPos().y() );
185 sceneEvent.setType( ::fwRenderQt::data::Event::MouseButtonRelease );
187 sceneEvent.setButton( this->getScene2DButtonFromEvent( _event ) );
188 sceneEvent.setModifier( this->getScene2DModifierFromEvent( _event) );
190 m_scene2DRender.lock()->dispatchInteraction( sceneEvent );
195 void Scene2DGraphicsView::mouseMoveEvent ( QMouseEvent* _event )
197 OSLM_TRACE(
"Move in x = " << _event->localPos().x() <<
" y = " << _event->localPos().y() );
199 sceneEvent.setType( ::fwRenderQt::data::Event::MouseMove );
201 sceneEvent.setButton( this->getScene2DButtonFromEvent( _event ) );
202 sceneEvent.setModifier( this->getScene2DModifierFromEvent( _event) );
204 m_scene2DRender.lock()->dispatchInteraction( sceneEvent );
209 void Scene2DGraphicsView::wheelEvent ( QWheelEvent* _event )
211 const bool scrollUp = _event->delta() > 0;
212 OSLM_TRACE(
"Scroll " << (scrollUp ?
"up" :
"down") <<
" _event");
214 sceneEvent.setType( (scrollUp) ? ::fwRenderQt::data::Event::MouseWheelUp : ::fwRenderQt::data::Event::MouseWheelDown);
216 sceneEvent.setModifier( this->getScene2DModifierFromEvent( _event) );
218 m_scene2DRender.lock()->dispatchInteraction( sceneEvent );
223 void Scene2DGraphicsView::setViewport( ::fwRenderQt::data::Viewport::sptr viewport )
225 m_viewport = viewport;
230 void Scene2DGraphicsView::updateFromViewport()
232 SLM_ASSERT(
"Viewport is not set", m_viewport);
234 this->fitInView(m_viewport->getX(), m_viewport->getY(),
235 m_viewport->getWidth(), m_viewport->getHeight(),
236 m_scene2DRender.lock()->getAspectRatioMode() );
241 void Scene2DGraphicsView::paintEvent(QPaintEvent* event)
244 QPaintEvent* newEvent =
new QPaintEvent( event->region().boundingRect() );
245 QGraphicsView::paintEvent(newEvent);
#define OSLM_TRACE(message)
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
The namespace fwRenderQt contains classes for rendering with Qt.
This class manage events on the scene 2D (mouse event, keyboard event , ...).