FreeCAD
1/**************************************************************************\
2* Copyright (c) Kongsberg Oil & Gas Technologies AS
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are
7* met:
8*
9* Redistributions of source code must retain the above copyright notice,
10* this list of conditions and the following disclaimer.
11*
12* Redistributions in binary form must reproduce the above copyright
13* notice, this list of conditions and the following disclaimer in the
14* documentation and/or other materials provided with the distribution.
15*
16* Neither the name of the copyright holder nor the names of its
17* contributors may be used to endorse or promote products derived from
18* this software without specific prior written permission.
19*
20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31\**************************************************************************/
32
33/*! \class SIM::Coin3D::Quarter::EventFilter EventFilter.h Quarter/eventhandlers/EventFilter.h
34
35*/
36
37#include <QEvent>38#include <QMouseEvent>39
40#include "QuarterWidget.h"41#include "devices/Keyboard.h"42#include "devices/Mouse.h"43#include "devices/SpaceNavigatorDevice.h"44#include "eventhandlers/EventFilter.h"45
46
47namespace SIM { namespace Coin3D { namespace Quarter {48
49class EventFilterP {50public:51QList<InputDevice *> devices;52QuarterWidget * quarterwidget;53QPoint globalmousepos;54SbVec2s windowsize;55
56void trackWindowSize(QResizeEvent * event)57{58this->windowsize = SbVec2s(event->size().width(),59event->size().height());60
61Q_FOREACH(InputDevice * device, this->devices) {62device->setWindowSize(this->windowsize);63}64}65
66void trackPointerPosition(QMouseEvent * event)67{68assert(this->windowsize[1] != -1);69#if QT_VERSION < QT_VERSION_CHECK(6,0,0)70this->globalmousepos = event->globalPos();71#else72this->globalmousepos = event->globalPosition().toPoint();73#endif74
75SbVec2s mousepos(event->pos().x(), this->windowsize[1] - event->pos().y() - 1);76// the following corrects for high-dpi displays (e.g. mac retina)77mousepos *= quarterwidget->devicePixelRatio();78Q_FOREACH(InputDevice * device, this->devices) {79device->setMousePosition(mousepos);80}81}82};83
84#define PRIVATE(obj) obj->pimpl85
86}}} // namespace87
88using namespace SIM::Coin3D::Quarter;89
90EventFilter::EventFilter(QObject * parent)91: QObject(parent)92{
93PRIVATE(this) = new EventFilterP;94
95QuarterWidget* quarter = dynamic_cast<QuarterWidget *>(parent);96PRIVATE(this)->quarterwidget = quarter;97assert(PRIVATE(this)->quarterwidget);98
99PRIVATE(this)->windowsize = SbVec2s(PRIVATE(this)->quarterwidget->width(),100PRIVATE(this)->quarterwidget->height());101
102PRIVATE(this)->devices += new Mouse(quarter);103PRIVATE(this)->devices += new Keyboard(quarter);104
105#ifdef HAVE_SPACENAV_LIB106PRIVATE(this)->devices += new SpaceNavigatorDevice(quarter);107#endif // HAVE_SPACENAV_LIB108
109}
110
111EventFilter::~EventFilter()112{
113qDeleteAll(PRIVATE(this)->devices);114delete PRIVATE(this);115}
116
117/*!
118Adds a device for event translation
119*/
120void121EventFilter::registerInputDevice(InputDevice * device)122{
123PRIVATE(this)->devices += device;124}
125
126/*!
127Removes a device from event translation
128*/
129void130EventFilter::unregisterInputDevice(InputDevice * device)131{
132int i = PRIVATE(this)->devices.indexOf(device);133if (i != -1) {134PRIVATE(this)->devices.removeAt(i);135}136}
137
138/*! Translates Qt Events into Coin events and passes them on to the
139event QuarterWidget for processing. If the event can not be
140translated or processed, it is forwarded to Qt and the method
141returns false.
142*/
143bool
144EventFilter::eventFilter(QObject * obj, QEvent * qevent)145{
146Q_UNUSED(obj);147// make sure every device has updated screen size and mouse position148// before translating events149switch (qevent->type()) {150case QEvent::MouseMove:151case QEvent::MouseButtonPress:152case QEvent::MouseButtonRelease:153case QEvent::MouseButtonDblClick:154PRIVATE(this)->trackPointerPosition(static_cast<QMouseEvent *>(qevent));155break;156case QEvent::Resize:157PRIVATE(this)->trackWindowSize(static_cast<QResizeEvent *>(qevent));158break;159default:160break;161}162
163// translate QEvent into SoEvent and see if it is handled by scene164// graph165Q_FOREACH(InputDevice * device, PRIVATE(this)->devices) {166const SoEvent * soevent = device->translateEvent(qevent);167if (soevent && PRIVATE(this)->quarterwidget->processSoEvent(soevent)) {168return true;169}170}171return false;172}
173
174/*!
175Returns mouse position in global coordinates
176*/
177const QPoint &178EventFilter::globalMousePosition() const179{
180return PRIVATE(this)->globalmousepos;181}
182
183#undef PRIVATE184