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
\**************************************************************************/
34
\class SIM::Coin3D::Quarter::Mouse Mouse.h Quarter/devices/Mouse.h
36
\brief The Mouse class provides translation of mouse events on the
40
#include "PreCompiled.h"
43
#pragma warning(disable : 4267)
50
#include <Gui/SoMouseWheelEvent.h>
51
#include <Inventor/SbVec2s.h>
52
#include <Inventor/errors/SoDebugError.h>
53
#include <Inventor/events/SoEvents.h>
55
#include "QuarterWidget.h"
56
#include "devices/Mouse.h"
59
namespace SIM { namespace Coin3D { namespace Quarter {
63
MouseP(Mouse * publ) {
65
this->location2 = new SoLocation2Event;
66
this->mousebutton = new SoMouseButtonEvent;
67
this->wheel = new SoMouseWheelEvent;
71
delete this->location2;
72
delete this->mousebutton;
76
const SoEvent * mouseMoveEvent(QMouseEvent * event);
77
const SoEvent * mouseWheelEvent(QWheelEvent * event);
78
const SoEvent * mouseButtonEvent(QMouseEvent * event);
80
void resizeEvent(QResizeEvent * event);
82
class SoLocation2Event * location2;
83
class SoMouseButtonEvent * mousebutton;
84
class SoMouseWheelEvent * wheel;
91
using namespace SIM::Coin3D::Quarter;
93
#define PRIVATE(obj) obj->pimpl
94
#define PUBLIC(obj) obj->publ
98
PRIVATE(this) = new MouseP(this);
101
Mouse::Mouse(QuarterWidget *quarter) :
104
PRIVATE(this) = new MouseP(this);
109
delete PRIVATE(this);
112
/*! Translates from QMouseEvents to SoLocation2Events and
116
Mouse::translateEvent(QEvent * event)
118
switch (event->type()) {
119
case QEvent::MouseMove:
120
return PRIVATE(this)->mouseMoveEvent((QMouseEvent *) event);
121
case QEvent::MouseButtonPress:
122
case QEvent::MouseButtonRelease:
123
// a dblclick event comes in a series of press, release, dblclick,
124
// release, so we can simply treat it as an ordinary press event.
126
case QEvent::MouseButtonDblClick:
127
return PRIVATE(this)->mouseButtonEvent((QMouseEvent *) event);
129
return PRIVATE(this)->mouseWheelEvent((QWheelEvent *) event);
131
PRIVATE(this)->resizeEvent((QResizeEvent *) event);
139
MouseP::resizeEvent(QResizeEvent * event)
141
this->windowsize = SbVec2s(event->size().width(),
142
event->size().height());
146
MouseP::mouseMoveEvent(QMouseEvent * event)
148
PUBLIC(this)->setModifiers(this->location2, event);
150
assert(this->windowsize[1] != -1);
151
SbVec2s pos(event->pos().x(), this->windowsize[1] - event->pos().y() - 1);
152
// the following corrects for high-dpi displays (e.g. mac retina)
153
pos *= publ->quarter->devicePixelRatio();
154
this->location2->setPosition(pos);
155
this->mousebutton->setPosition(pos);
156
return this->location2;
160
MouseP::mouseWheelEvent(QWheelEvent * event)
162
PUBLIC(this)->setModifiers(this->wheel, event);
163
#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
164
QPoint pnt = event->position().toPoint();
165
SbVec2s pos(pnt.x(), PUBLIC(this)->windowsize[1] - pnt.y() - 1);
167
SbVec2s pos(event->pos().x(), PUBLIC(this)->windowsize[1] - event->pos().y() - 1);
169
// the following corrects for high-dpi displays (e.g. mac retina)
170
pos *= publ->quarter->devicePixelRatio();
171
this->location2->setPosition(pos); //I don't know why location2 is assigned here, I assumed it important --DeepSOIC
172
this->wheel->setPosition(pos);
174
// QWheelEvent::delta() returns the distance that the wheel is
175
// rotated, in eights of a degree. A positive value indicates that
176
// the wheel was rotated forwards away from the user; a negative
177
// value indicates that the wheel was rotated backwards toward the
178
// user. A typical wheel click is 120, but values coming from touchpad
179
// can be a lot lower
180
this->wheel->setDelta(event->angleDelta().y());
186
MouseP::mouseButtonEvent(QMouseEvent * event)
188
PUBLIC(this)->setModifiers(this->mousebutton, event);
189
SbVec2s pos(event->pos().x(), PUBLIC(this)->windowsize[1] - event->pos().y() - 1);
190
// the following corrects for high-dpi displays (e.g. mac retina)
191
pos *= publ->quarter->devicePixelRatio();
192
this->location2->setPosition(pos);
193
this->mousebutton->setPosition(pos);
195
((event->type() == QEvent::MouseButtonPress) ||
196
(event->type() == QEvent::MouseButtonDblClick)) ?
197
this->mousebutton->setState(SoButtonEvent::DOWN):
198
this->mousebutton->setState(SoButtonEvent::UP);
200
switch (event->button()) {
202
this->mousebutton->setButton(SoMouseButtonEvent::BUTTON1);
204
case Qt::RightButton:
205
this->mousebutton->setButton(SoMouseButtonEvent::BUTTON2);
207
case Qt::MiddleButton:
208
this->mousebutton->setButton(SoMouseButtonEvent::BUTTON3);
211
this->mousebutton->setButton(SoMouseButtonEvent::ANY);
212
SoDebugError::postInfo("Mouse::mouseButtonEvent",
213
"Unhandled ButtonState = %x", event->button());
216
return this->mousebutton;