| 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 |
/*! |
| 34 |
\class SIM::Coin3D::Quarter::Mouse Mouse.h Quarter/devices/Mouse.h |
| 35 |
|
| 36 |
\brief The Mouse class provides translation of mouse events on the |
| 37 |
QuarterWidget. |
| 38 |
*/ |
| 39 |
|
| 40 |
#ifdef _MSC_VER |
| 41 |
#pragma warning(disable : 4267) |
| 42 |
#endif |
| 43 |
|
| 44 |
#include <QEvent> |
| 45 |
#include <QMouseEvent> |
| 46 |
#include <QWheelEvent> |
| 47 |
|
| 48 |
#include <Inventor/SbVec2s.h> |
| 49 |
#include <Inventor/SoMouseWheelEvent.h> |
| 50 |
#include <Inventor/errors/SoDebugError.h> |
| 51 |
#include <Inventor/events/SoEvents.h> |
| 52 |
|
| 53 |
#include "QuarterWidget.h" |
| 54 |
#include "devices/Mouse.h" |
| 55 |
|
| 56 |
|
| 57 |
namespace SIM { namespace Coin3D { namespace Quarter { |
| 58 |
|
| 59 |
class MouseP { |
| 60 |
public: |
| 61 |
explicit MouseP(Mouse * publ) { |
| 62 |
this->publ = publ; |
| 63 |
this->location2 = new SoLocation2Event; |
| 64 |
this->mousebutton = new SoMouseButtonEvent; |
| 65 |
this->wheel = new SoMouseWheelEvent; |
| 66 |
} |
| 67 |
|
| 68 |
~MouseP() { |
| 69 |
delete this->location2; |
| 70 |
delete this->mousebutton; |
| 71 |
delete this->wheel; |
| 72 |
} |
| 73 |
|
| 74 |
const SoEvent * mouseMoveEvent(QMouseEvent * event); |
| 75 |
const SoEvent * mouseWheelEvent(QWheelEvent * event); |
| 76 |
const SoEvent * mouseButtonEvent(QMouseEvent * event); |
| 77 |
|
| 78 |
void resizeEvent(QResizeEvent * event); |
| 79 |
|
| 80 |
class SoLocation2Event * location2; |
| 81 |
class SoMouseButtonEvent * mousebutton; |
| 82 |
class SoMouseWheelEvent * wheel; |
| 83 |
SbVec2s windowsize; |
| 84 |
Mouse * publ; |
| 85 |
}; |
| 86 |
|
| 87 |
}}} // namespace |
| 88 |
|
| 89 |
using namespace SIM::Coin3D::Quarter; |
| 90 |
|
| 91 |
#define PRIVATE(obj) obj->pimpl |
| 92 |
#define PUBLIC(obj) obj->publ |
| 93 |
|
| 94 |
Mouse::Mouse(QuarterWidget* quarter) : |
| 95 |
InputDevice(quarter) |
| 96 |
{ |
| 97 |
PRIVATE(this) = new MouseP(this); |
| 98 |
} |
| 99 |
|
| 100 |
Mouse::~Mouse() |
| 101 |
{ |
| 102 |
delete PRIVATE(this); |
| 103 |
} |
| 104 |
|
| 105 |
/*! Translates from QMouseEvents to SoLocation2Events and |
| 106 |
SoMouseButtonEvents |
| 107 |
*/ |
| 108 |
const SoEvent * |
| 109 |
Mouse::translateEvent(QEvent * event) |
| 110 |
{ |
| 111 |
switch (event->type()) { |
| 112 |
case QEvent::MouseMove: |
| 113 |
return PRIVATE(this)->mouseMoveEvent((QMouseEvent *) event); |
| 114 |
case QEvent::MouseButtonPress: |
| 115 |
case QEvent::MouseButtonRelease: |
| 116 |
// a dblclick event comes in a series of press, release, dblclick, |
| 117 |
// release, so we can simply treat it as an ordinary press event. |
| 118 |
// -mortene. |
| 119 |
case QEvent::MouseButtonDblClick: |
| 120 |
return PRIVATE(this)->mouseButtonEvent((QMouseEvent *) event); |
| 121 |
case QEvent::Wheel: |
| 122 |
return PRIVATE(this)->mouseWheelEvent((QWheelEvent *) event); |
| 123 |
case QEvent::Resize: |
| 124 |
PRIVATE(this)->resizeEvent((QResizeEvent *) event); |
| 125 |
return nullptr; |
| 126 |
default: |
| 127 |
return nullptr; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
void |
| 132 |
MouseP::resizeEvent(QResizeEvent * event) |
| 133 |
{ |
| 134 |
this->windowsize = SbVec2s(event->size().width(), |
| 135 |
event->size().height()); |
| 136 |
} |
| 137 |
|
| 138 |
const SoEvent * |
| 139 |
MouseP::mouseMoveEvent(QMouseEvent * event) |
| 140 |
{ |
| 141 |
PUBLIC(this)->setModifiers(this->location2, event); |
| 142 |
|
| 143 |
assert(this->windowsize[1] != -1); |
| 144 |
SbVec2s pos(event->pos().x(), this->windowsize[1] - event->pos().y() - 1); |
| 145 |
// the following corrects for high-dpi displays (e.g. mac retina) |
| 146 |
pos *= publ->quarter->devicePixelRatio(); |
| 147 |
this->location2->setPosition(pos); |
| 148 |
this->mousebutton->setPosition(pos); |
| 149 |
return this->location2; |
| 150 |
} |
| 151 |
|
| 152 |
const SoEvent * |
| 153 |
MouseP::mouseWheelEvent(QWheelEvent * event) |
| 154 |
{ |
| 155 |
PUBLIC(this)->setModifiers(this->wheel, event); |
| 156 |
QPoint pnt = event->position().toPoint(); |
| 157 |
SbVec2s pos(pnt.x(), PUBLIC(this)->windowsize[1] - pnt.y() - 1); |
| 158 |
// the following corrects for high-dpi displays (e.g. mac retina) |
| 159 |
pos *= publ->quarter->devicePixelRatio(); |
| 160 |
this->location2->setPosition(pos); //I don't know why location2 is assigned here, I assumed it important --DeepSOIC |
| 161 |
this->wheel->setPosition(pos); |
| 162 |
|
| 163 |
// QWheelEvent::delta() returns the distance that the wheel is |
| 164 |
// rotated, in eights of a degree. A positive value indicates that |
| 165 |
// the wheel was rotated forwards away from the user; a negative |
| 166 |
// value indicates that the wheel was rotated backwards toward the |
| 167 |
// user. A typical wheel click is 120, but values coming from touchpad |
| 168 |
// can be a lot lower |
| 169 |
this->wheel->setDelta(event->angleDelta().y()); |
| 170 |
|
| 171 |
return this->wheel; |
| 172 |
} |
| 173 |
|
| 174 |
const SoEvent * |
| 175 |
MouseP::mouseButtonEvent(QMouseEvent * event) |
| 176 |
{ |
| 177 |
PUBLIC(this)->setModifiers(this->mousebutton, event); |
| 178 |
SbVec2s pos(event->pos().x(), PUBLIC(this)->windowsize[1] - event->pos().y() - 1); |
| 179 |
// the following corrects for high-dpi displays (e.g. mac retina) |
| 180 |
pos *= publ->quarter->devicePixelRatio(); |
| 181 |
this->location2->setPosition(pos); |
| 182 |
this->mousebutton->setPosition(pos); |
| 183 |
|
| 184 |
((event->type() == QEvent::MouseButtonPress) || |
| 185 |
(event->type() == QEvent::MouseButtonDblClick)) ? |
| 186 |
this->mousebutton->setState(SoButtonEvent::DOWN): |
| 187 |
this->mousebutton->setState(SoButtonEvent::UP); |
| 188 |
|
| 189 |
switch (event->button()) { |
| 190 |
case Qt::LeftButton: |
| 191 |
this->mousebutton->setButton(SoMouseButtonEvent::BUTTON1); |
| 192 |
break; |
| 193 |
case Qt::RightButton: |
| 194 |
this->mousebutton->setButton(SoMouseButtonEvent::BUTTON2); |
| 195 |
break; |
| 196 |
case Qt::MiddleButton: |
| 197 |
this->mousebutton->setButton(SoMouseButtonEvent::BUTTON3); |
| 198 |
break; |
| 199 |
default: |
| 200 |
this->mousebutton->setButton(SoMouseButtonEvent::ANY); |
| 201 |
SoDebugError::postInfo("Mouse::mouseButtonEvent", |
| 202 |
"Unhandled ButtonState = %x", event->button()); |
| 203 |
break; |
| 204 |
} |
| 205 |
return this->mousebutton; |
| 206 |
} |
| 207 |
|
| 208 |
#undef PRIVATE |
| 209 |
#undef PUBLIC |