FreeCAD

Форк
0
/
Mouse.cpp 
220 строк · 6.8 Кб
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
#include "PreCompiled.h"
41

42
#ifdef _MSC_VER
43
#pragma warning(disable : 4267)
44
#endif
45

46
#include <QEvent>
47
#include <QMouseEvent>
48
#include <QWheelEvent>
49

50
#include <Gui/SoMouseWheelEvent.h>
51
#include <Inventor/SbVec2s.h>
52
#include <Inventor/errors/SoDebugError.h>
53
#include <Inventor/events/SoEvents.h>
54

55
#include "QuarterWidget.h"
56
#include "devices/Mouse.h"
57

58

59
namespace SIM { namespace Coin3D { namespace Quarter {
60

61
class MouseP {
62
public:
63
  MouseP(Mouse * publ) {
64
    this->publ = publ;
65
    this->location2 = new SoLocation2Event;
66
    this->mousebutton = new SoMouseButtonEvent;
67
    this->wheel = new SoMouseWheelEvent;
68
  }
69

70
  ~MouseP() {
71
    delete this->location2;
72
    delete this->mousebutton;
73
    delete this->wheel;
74
  }
75

76
  const SoEvent * mouseMoveEvent(QMouseEvent * event);
77
  const SoEvent * mouseWheelEvent(QWheelEvent * event);
78
  const SoEvent * mouseButtonEvent(QMouseEvent * event);
79

80
  void resizeEvent(QResizeEvent * event);
81

82
  class SoLocation2Event * location2;
83
  class SoMouseButtonEvent * mousebutton;
84
  class SoMouseWheelEvent * wheel;
85
  SbVec2s windowsize;
86
  Mouse * publ;
87
};
88

89
}}} // namespace
90

91
using namespace SIM::Coin3D::Quarter;
92

93
#define PRIVATE(obj) obj->pimpl
94
#define PUBLIC(obj) obj->publ
95

96
Mouse::Mouse()
97
{
98
  PRIVATE(this) = new MouseP(this);
99
}
100

101
Mouse::Mouse(QuarterWidget *quarter) :
102
    InputDevice(quarter)
103
{
104
    PRIVATE(this) = new MouseP(this);
105
}
106

107
Mouse::~Mouse()
108
{
109
  delete PRIVATE(this);
110
}
111

112
/*! Translates from QMouseEvents to SoLocation2Events and
113
  SoMouseButtonEvents
114
 */
115
const SoEvent *
116
Mouse::translateEvent(QEvent * event)
117
{
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.
125
    // -mortene.
126
  case QEvent::MouseButtonDblClick:
127
    return PRIVATE(this)->mouseButtonEvent((QMouseEvent *) event);
128
  case QEvent::Wheel:
129
    return PRIVATE(this)->mouseWheelEvent((QWheelEvent *) event);
130
  case QEvent::Resize:
131
    PRIVATE(this)->resizeEvent((QResizeEvent *) event);
132
    return nullptr;
133
  default:
134
    return nullptr;
135
  }
136
}
137

138
void
139
MouseP::resizeEvent(QResizeEvent * event)
140
{
141
  this->windowsize = SbVec2s(event->size().width(),
142
                             event->size().height());
143
}
144

145
const SoEvent *
146
MouseP::mouseMoveEvent(QMouseEvent * event)
147
{
148
  PUBLIC(this)->setModifiers(this->location2, event);
149

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;
157
}
158

159
const SoEvent *
160
MouseP::mouseWheelEvent(QWheelEvent * event)
161
{
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);
166
#else
167
  SbVec2s pos(event->pos().x(), PUBLIC(this)->windowsize[1] - event->pos().y() - 1);
168
#endif
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);
173

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());
181

182
  return this->wheel;
183
}
184

185
const SoEvent *
186
MouseP::mouseButtonEvent(QMouseEvent * event)
187
{
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);
194

195
  ((event->type() == QEvent::MouseButtonPress) ||
196
   (event->type() == QEvent::MouseButtonDblClick)) ?
197
    this->mousebutton->setState(SoButtonEvent::DOWN):
198
    this->mousebutton->setState(SoButtonEvent::UP);
199

200
  switch (event->button()) {
201
  case Qt::LeftButton:
202
    this->mousebutton->setButton(SoMouseButtonEvent::BUTTON1);
203
    break;
204
  case Qt::RightButton:
205
    this->mousebutton->setButton(SoMouseButtonEvent::BUTTON2);
206
    break;
207
  case Qt::MiddleButton:
208
    this->mousebutton->setButton(SoMouseButtonEvent::BUTTON3);
209
    break;
210
  default:
211
    this->mousebutton->setButton(SoMouseButtonEvent::ANY);
212
    SoDebugError::postInfo("Mouse::mouseButtonEvent",
213
                           "Unhandled ButtonState = %x", event->button());
214
    break;
215
  }
216
  return this->mousebutton;
217
}
218

219
#undef PRIVATE
220
#undef PUBLIC
221

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.