[osg-users] osg+QT MODKEY How to

legeochen legeochen at gmail.com
Mon Mar 9 17:20:02 PDT 2009


Hi guys:
Thanks all for your answers. It helps a lot!
cheers :)
Legeo
2009/3/10 René Molenaar <megamillerzoid at gmail.com>

> Hi,
>
> i use the following code to get the modifiers in.
> call this in your mousePressEvent.
> (and don't forget to convert the keyrelease, like other non-text
> key-releases)
>
>
> /**
>  * Check the current event for modifiers
>  *
>  * @brief  Mouse Press Event
>  * @param event Mouse event to be placed in the event queue
>  */
> #if USE_QT4
> void AdapterWidget::adaptModifiers( QInputEvent* event )
> {
>     int modkey = event->modifiers() & (Qt::ShiftModifier |
> Qt::ControlModifier | Qt::AltModifier);
>     unsigned int modkeyosg = 0;
>     if (modkey & Qt::ShiftModifier)    modkeyosg |=
> osgGA::GUIEventAdapter::MODKEY_SHIFT;
>     if (modkey & Qt::ControlModifier)  modkeyosg |=
> osgGA::GUIEventAdapter::MODKEY_CTRL;
>     if (modkey & Qt::AltModifier)      modkeyosg |=
> osgGA::GUIEventAdapter::MODKEY_ALT;
>     m_gw->getEventQueue()->getCurrentEventState()->setModKeyMask( modkeyosg
> );
> }
> #else
> void AdapterWidget::adaptModifiers( QMouseEvent* event )
> {
>     int modkey = event->stateAfter() & (Qt::ShiftButton | Qt::ControlButton
> | Qt::AltButton);
>     unsigned int modkeyosg = 0;
>     if (modkey & Qt::ShiftButton)    modkeyosg |=
> osgGA::GUIEventAdapter::MODKEY_SHIFT;
>     if (modkey & Qt::ControlButton)  modkeyosg |=
> osgGA::GUIEventAdapter::MODKEY_CTRL;
>     if (modkey & Qt::AltButton)      modkeyosg |=
> osgGA::GUIEventAdapter::MODKEY_ALT;
>     m_gw->getEventQueue()->getCurrentEventState()->setModKeyMask( modkeyosg
> );
> }
> #endif
>
> Rene
>
> 2009/3/9 Fabien Lavignotte <Fabien.Lavignotte at vegatechnologies.fr>
>
> The osgViewerQt example does not take into account modifier key. So, the
>> OSG modifier mask is always zero.
>> I have changed the conversion between Qt event and OSG event to handle
>> modifier properly.
>> Here is my code. You can adapt this code in your widget. Let me know if
>> you need some help.
>> Fabien
>>
>> /*! Helper function which converts a Qt mouse button code into a osg
>> mouse button code */
>> inline void convertMouseEvent( osgGA::EventQueue* eventQueue,
>> QMouseEvent* eventQt, osgGA::GUIEventAdapter::EventType eventType )
>> {
>> eventQueue->getCurrentEventState()->setX( eventQt->x() );
>> eventQueue->getCurrentEventState()->setY( eventQt->y() );
>> int button = 0;
>> switch( eventQt->button() )
>> {
>> case Qt::LeftButton :
>> button = osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON;
>> break;
>> case Qt::MidButton :
>> button = osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON;
>> break;
>> case Qt::RightButton :
>> button = osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON;
>> break;
>> }
>> if ( eventQt->type() == QEvent::MouseButtonPress )
>> eventQueue->getCurrentEventState()->setButtonMask( button |
>> eventQueue->getCurrentEventState()->getButtonMask());
>> else
>> eventQueue->getCurrentEventState()->setButtonMask( ~button &
>> eventQueue->getCurrentEventState()->getButtonMask());
>> unsigned int modKeyMask = 0;
>> if( eventQt->modifiers() & Qt::ShiftModifier)
>> {
>> modKeyMask |= osgGA::GUIEventAdapter::MODKEY_SHIFT;
>> }
>> if( eventQt->modifiers() & Qt::ControlModifier)
>> {
>> modKeyMask |= osgGA::GUIEventAdapter::MODKEY_CTRL;
>> }
>> if( eventQt->modifiers() & Qt::AltModifier)
>> {
>> modKeyMask |= osgGA::GUIEventAdapter::MODKEY_ALT;
>> }
>> osgGA::GUIEventAdapter* event = eventQueue->createEvent();
>> event->setEventType(eventType);
>> event->setTime(eventQueue->getTime());
>> event->setModKeyMask(modKeyMask);
>> event->setButton(button);
>> eventQueue->addEvent(event);
>> }
>>
>> /*! overwrites the mouse pressed handler */
>>
>> void Viewer::mousePressEvent( QMouseEvent* event )
>> {
>>        GLWidget::mousePressEvent(event);
>>        if ( _graphicsWindow.valid() )
>>        {
>>                convertMouseEvent( _graphicsWindow->getEventQueue(),
>> event, osgGA::GUIEventAdapter::PUSH );
>>        }
>> }
>>
>> /*! overwrites the mouse released handler */
>> void Viewer::mouseReleaseEvent( QMouseEvent* event )
>> {
>>        GLWidget::mousePressEvent(event);
>>        if ( _graphicsWindow.valid() )
>>        {
>>                convertMouseEvent( _graphicsWindow->getEventQueue(),
>> event, osgGA::GUIEventAdapter::RELEASE );
>>        }
>> }
>>
>>
>> ________________________________
>>
>> From: osg-users-bounces at lists.openscenegraph.org
>> [mailto:osg-users-bounces at lists.openscenegraph.org] On Behalf Of Dieter
>> Pfeffer
>> Sent: lundi 9 mars 2009 09:34
>> To: OpenSceneGraph Users
>> Subject: Re: [osg-users] osg+QT MODKEY How to
>>
>>
>> Hi Legeo
>>
>> I had a similar problem - using osg, qt4 and the AdapterWidget example.
>>
>> In AdapterWidget::keypressEvent (...)
>> osgGA::GUIEventAdapter::KeySymbol) * (event->text().toAscii().data()
>> returns 0 in the call
>> _gw->getEventQueue()->keyPress( (... ) )  for keys like:
>>
>> Qt::Key_up ... Have a look at the Qt documentation Qt::Key.
>>
>> I have managed it with:
>>
>> int c = *event->text().toAscii().data();
>>
>> if ( c == 0)
>>
>> {
>>
>> switch (event->key())
>>
>> {
>>
>> case Qt::Key_PageDown:
>>
>> _gw->getEventQueue()->keyPress(
>> osgGA::GUIEventAdapter::KeySymbol::KEY_Page_Down );
>>
>> break;
>>
>> case Qt::Key_PageUp:
>>
>> _gw->getEventQueue()->keyPress(
>> osgGA::GUIEventAdapter::KeySymbol::KEY_Page_Up );
>>
>> break;
>>
>> case Qt::Key_Up:
>>
>> _gw->getEventQueue()->keyPress( osgGA::GUIEventAdapter::KEY_Up);
>>
>> break;
>>
>> }
>>
>> }
>> else
>> _gw->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) *
>> (event->text().toAscii().data() ) );
>>
>> There might be a better solution but it works.
>>
>> Dieter
>>
>>
>>
>>
>> Unclassified Mail
>>
>>
>>        -----Original Message-----
>>        From: osg-users-bounces at lists.openscenegraph.org
>> [mailto:osg-users-bounces at lists.openscenegraph.org]On Behalf Of
>> legeochen
>>        Sent: Saturday, 07 March, 2009 09:14
>>        To: OpenSceneGraph Users
>>        Subject: [osg-users] osg+QT MODKEY How to
>>
>>
>>        Hi all,
>>           I'm trying to add a Modkey event handler to my application
>> with osg and qt4. But it seems that the getModKeyMask() fuction fails to
>> work. Return value of getModKeyMask() always is 0, even if I have a
>> MODKEY pressed. And, this is my code:
>>                if ( ea.getEventType() == osgGA::GUIEventAdapter::PUSH
>> &&
>>                     ea.getButtonMask() ==
>> osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON &&
>>                     ea.getModKeyMask() ==
>> osgGA::GUIEventAdapter::MODKEY_SHIFT )
>>                 {
>>                    /*---------------------------------------------
>>                            do something here
>>                    -------------------------------------------------*/
>>                }
>>        How can I handle it? Thanks in advance!
>>
>>        cheers
>>        legeo
>>
>>
>>
>> ------------------------------------------------------------------------
>> ------------------------------------
>> Disclaimer:
>>
>> If you are not the intended recipient of this email, please notify the
>> sender and delete it.
>> Any unauthorized copying, disclosure or distribution of this email or
>> its attachment(s) is forbidden.
>> Thales Nederland BV will not accept liability for any damage caused by
>> this email or its attachment(s).
>> Thales Nederland BV is seated in Hengelo and is registered at the
>> Chamber of Commerce under number 06061578.
>> ------------------------------------------------------------------------
>> ------------------------------------
>>
>>
>>
>> ______________________________________________________________________
>> This email has been scanned by the MessageLabs Email Security System.
>> For more information please visit http://www.messagelabs.com/email
>> ______________________________________________________________________
>>
>>
>> ______________________________________________________________________
>> This email has been scanned by the MessageLabs Email Security System.
>> For more information please visit http://www.messagelabs.com/email
>> ______________________________________________________________________
>>  _______________________________________________
>> osg-users mailing list
>> osg-users at lists.openscenegraph.org
>> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20090310/12ac7496/attachment-0003.htm>


More information about the osg-users mailing list