| 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 |
|
| 47 |
namespace SIM { namespace Coin3D { namespace Quarter { |
| 48 |
|
| 49 |
class EventFilterP { |
| 50 |
public: |
| 51 |
QList<InputDevice *> devices; |
| 52 |
QuarterWidget * quarterwidget; |
| 53 |
QPoint globalmousepos; |
| 54 |
SbVec2s windowsize; |
| 55 |
|
| 56 |
void trackWindowSize(QResizeEvent * event) |
| 57 |
{ |
| 58 |
this->windowsize = SbVec2s(event->size().width(), |
| 59 |
event->size().height()); |
| 60 |
|
| 61 |
Q_FOREACH(InputDevice * device, this->devices) { |
| 62 |
device->setWindowSize(this->windowsize); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
void trackPointerPosition(QMouseEvent * event) |
| 67 |
{ |
| 68 |
assert(this->windowsize[1] != -1); |
| 69 |
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0) |
| 70 |
this->globalmousepos = event->globalPosition().toPoint(); |
| 71 |
#else |
| 72 |
this->globalmousepos = event->globalPos(); |
| 73 |
#endif |
| 74 |
|
| 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); |
| 80 |
} |
| 81 |
} |
| 82 |
}; |
| 83 |
|
| 84 |
#define PRIVATE(obj) obj->pimpl |
| 85 |
|
| 86 |
}}} // namespace |
| 87 |
|
| 88 |
using namespace SIM::Coin3D::Quarter; |
| 89 |
|
| 90 |
EventFilter::EventFilter(QObject * parent) |
| 91 |
: QObject(parent) |
| 92 |
{ |
| 93 |
PRIVATE(this) = new EventFilterP; |
| 94 |
|
| 95 |
QuarterWidget* quarter = dynamic_cast<QuarterWidget *>(parent); |
| 96 |
PRIVATE(this)->quarterwidget = quarter; |
| 97 |
assert(PRIVATE(this)->quarterwidget); |
| 98 |
|
| 99 |
PRIVATE(this)->windowsize = SbVec2s(PRIVATE(this)->quarterwidget->width(), |
| 100 |
PRIVATE(this)->quarterwidget->height()); |
| 101 |
|
| 102 |
PRIVATE(this)->devices += new Mouse(quarter); |
| 103 |
PRIVATE(this)->devices += new Keyboard(quarter); |
| 104 |
|
| 105 |
#ifdef HAVE_SPACENAV_LIB |
| 106 |
PRIVATE(this)->devices += new SpaceNavigatorDevice(quarter); |
| 107 |
#endif // HAVE_SPACENAV_LIB |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
EventFilter::~EventFilter() |
| 112 |
{ |
| 113 |
qDeleteAll(PRIVATE(this)->devices); |
| 114 |
delete PRIVATE(this); |
| 115 |
} |
| 116 |
|
| 117 |
/*! |
| 118 |
Adds a device for event translation |
| 119 |
*/ |
| 120 |
void |
| 121 |
EventFilter::registerInputDevice(InputDevice * device) |
| 122 |
{ |
| 123 |
PRIVATE(this)->devices += device; |
| 124 |
} |
| 125 |
|
| 126 |
/*! |
| 127 |
Removes a device from event translation |
| 128 |
*/ |
| 129 |
void |
| 130 |
EventFilter::unregisterInputDevice(InputDevice * device) |
| 131 |
{ |
| 132 |
int i = PRIVATE(this)->devices.indexOf(device); |
| 133 |
if (i != -1) { |
| 134 |
PRIVATE(this)->devices.removeAt(i); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 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 |
| 141 |
returns false. |
| 142 |
*/ |
| 143 |
bool |
| 144 |
EventFilter::eventFilter(QObject * obj, QEvent * qevent) |
| 145 |
{ |
| 146 |
Q_UNUSED(obj); |
| 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)); |
| 155 |
break; |
| 156 |
case QEvent::Resize: |
| 157 |
PRIVATE(this)->trackWindowSize(dynamic_cast<QResizeEvent *>(qevent)); |
| 158 |
break; |
| 159 |
default: |
| 160 |
break; |
| 161 |
} |
| 162 |
|
| 163 |
// translate QEvent into SoEvent and see if it is handled by scene |
| 164 |
// graph |
| 165 |
Q_FOREACH(InputDevice * device, PRIVATE(this)->devices) { |
| 166 |
const SoEvent * soevent = device->translateEvent(qevent); |
| 167 |
if (soevent && PRIVATE(this)->quarterwidget->processSoEvent(soevent)) { |
| 168 |
return true; |
| 169 |
} |
| 170 |
} |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
/*! |
| 175 |
Returns mouse position in global coordinates |
| 176 |
*/ |
| 177 |
const QPoint & |
| 178 |
EventFilter::globalMousePosition() const |
| 179 |
{ |
| 180 |
return PRIVATE(this)->globalmousepos; |
| 181 |
} |
| 182 |
|
| 183 |
#undef PRIVATE |