1
/**************************************************************************\
2
* Copyright (c) Kongsberg Oil & Gas Technologies AS
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are
9
* Redistributions of source code must retain the above copyright notice,
10
* this list of conditions and the following disclaimer.
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.
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.
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
\**************************************************************************/
33
/*! \class SIM::Coin3D::Quarter::EventFilter EventFilter.h Quarter/eventhandlers/EventFilter.h
40
#include "QuarterWidget.h"
41
#include "devices/Keyboard.h"
42
#include "devices/Mouse.h"
43
#include "devices/SpaceNavigatorDevice.h"
44
#include "eventhandlers/EventFilter.h"
47
namespace SIM { namespace Coin3D { namespace Quarter {
51
QList<InputDevice *> devices;
52
QuarterWidget * quarterwidget;
53
QPoint globalmousepos;
56
void trackWindowSize(QResizeEvent * event)
58
this->windowsize = SbVec2s(event->size().width(),
59
event->size().height());
61
Q_FOREACH(InputDevice * device, this->devices) {
62
device->setWindowSize(this->windowsize);
66
void trackPointerPosition(QMouseEvent * event)
68
assert(this->windowsize[1] != -1);
69
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
70
this->globalmousepos = event->globalPosition().toPoint();
72
this->globalmousepos = event->globalPos();
75
SbVec2s mousepos(event->pos().x(), this->windowsize[1] - event->pos().y() - 1);
76
// the following corrects for high-dpi displays (e.g. mac retina)
77
mousepos *= quarterwidget->devicePixelRatio();
78
Q_FOREACH(InputDevice * device, this->devices) {
79
device->setMousePosition(mousepos);
84
#define PRIVATE(obj) obj->pimpl
88
using namespace SIM::Coin3D::Quarter;
90
EventFilter::EventFilter(QObject * parent)
93
PRIVATE(this) = new EventFilterP;
95
QuarterWidget* quarter = dynamic_cast<QuarterWidget *>(parent);
96
PRIVATE(this)->quarterwidget = quarter;
97
assert(PRIVATE(this)->quarterwidget);
99
PRIVATE(this)->windowsize = SbVec2s(PRIVATE(this)->quarterwidget->width(),
100
PRIVATE(this)->quarterwidget->height());
102
PRIVATE(this)->devices += new Mouse(quarter);
103
PRIVATE(this)->devices += new Keyboard(quarter);
105
#ifdef HAVE_SPACENAV_LIB
106
PRIVATE(this)->devices += new SpaceNavigatorDevice(quarter);
107
#endif // HAVE_SPACENAV_LIB
111
EventFilter::~EventFilter()
113
qDeleteAll(PRIVATE(this)->devices);
114
delete PRIVATE(this);
118
Adds a device for event translation
121
EventFilter::registerInputDevice(InputDevice * device)
123
PRIVATE(this)->devices += device;
127
Removes a device from event translation
130
EventFilter::unregisterInputDevice(InputDevice * device)
132
int i = PRIVATE(this)->devices.indexOf(device);
134
PRIVATE(this)->devices.removeAt(i);
138
/*! Translates Qt Events into Coin events and passes them on to the
139
event QuarterWidget for processing. If the event cannot be
140
translated or processed, it is forwarded to Qt and the method
144
EventFilter::eventFilter(QObject * obj, QEvent * qevent)
147
// make sure every device has updated screen size and mouse position
148
// before translating events
149
switch (qevent->type()) {
150
case QEvent::MouseMove:
151
case QEvent::MouseButtonPress:
152
case QEvent::MouseButtonRelease:
153
case QEvent::MouseButtonDblClick:
154
PRIVATE(this)->trackPointerPosition(dynamic_cast<QMouseEvent *>(qevent));
157
PRIVATE(this)->trackWindowSize(dynamic_cast<QResizeEvent *>(qevent));
163
// translate QEvent into SoEvent and see if it is handled by scene
165
Q_FOREACH(InputDevice * device, PRIVATE(this)->devices) {
166
const SoEvent * soevent = device->translateEvent(qevent);
167
if (soevent && PRIVATE(this)->quarterwidget->processSoEvent(soevent)) {
175
Returns mouse position in global coordinates
178
EventFilter::globalMousePosition() const
180
return PRIVATE(this)->globalmousepos;