| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU Lesser General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | 
| 21 | ** packaging of this file. Please review the following information to | 
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | 
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | 
| 24 | ** | 
| 25 | ** GNU General Public License Usage | 
| 26 | ** Alternatively, this file may be used under the terms of the GNU | 
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | 
| 28 | ** Public license version 3 or any later version approved by the KDE Free | 
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | 
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | 
| 31 | ** included in the packaging of this file. Please review the following | 
| 32 | ** information to ensure the GNU General Public License requirements will | 
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | 
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | 
| 35 | ** | 
| 36 | ** $QT_END_LICENSE$ | 
| 37 | ** | 
| 38 | ****************************************************************************/ | 
| 39 |  | 
| 40 | #include "qmouseevent.h" | 
| 41 |  | 
| 42 | QT_BEGIN_NAMESPACE | 
| 43 |  | 
| 44 | namespace Qt3DInput { | 
| 45 |  | 
| 46 | namespace { | 
| 47 |  | 
| 48 | template<typename EventClass, typename QtEventClass> | 
| 49 | typename EventClass::Modifiers modifiersForEvent(const QtEventClass &event) | 
| 50 | { | 
| 51 |     const Qt::KeyboardModifiers eventModifiers = event.modifiers(); | 
| 52 |     int modifiers = EventClass::NoModifier; | 
| 53 |  | 
| 54 |     if (eventModifiers & Qt::ShiftModifier) | 
| 55 |         modifiers |= EventClass::ShiftModifier; | 
| 56 |  | 
| 57 |     if (eventModifiers & Qt::ControlModifier) | 
| 58 |         modifiers |= EventClass::ControlModifier; | 
| 59 |  | 
| 60 |     if (eventModifiers & Qt::AltModifier) | 
| 61 |         modifiers |= EventClass::AltModifier; | 
| 62 |  | 
| 63 |     if (eventModifiers & Qt::MetaModifier) | 
| 64 |         modifiers |= EventClass::MetaModifier; | 
| 65 |  | 
| 66 |     if (eventModifiers & Qt::KeypadModifier) | 
| 67 |         modifiers |= EventClass::KeypadModifier; | 
| 68 |  | 
| 69 |     // Abuse the int used to store an enum to store multiple | 
| 70 |     // modifiers into one | 
| 71 |     return static_cast<typename EventClass::Modifiers>(modifiers); | 
| 72 | } | 
| 73 |  | 
| 74 | } // anonymous | 
| 75 |  | 
| 76 | // Notes: | 
| 77 | // Maybe we should provide the world pos of the intersection | 
| 78 | // The distance t along the segment line at which the intersection occurs | 
| 79 | // Screen Pos / Mouse Pos / Viewport Pos | 
| 80 | // The intersection Ray | 
| 81 | // These can always be added in follow up commits once the input API takes shape | 
| 82 |  | 
| 83 | /*! | 
| 84 |  * \qmltype MouseEvent | 
| 85 |  * \instantiates Qt3DInput::QMouseEvent | 
| 86 |  * \inqmlmodule Qt3D.Input | 
| 87 |  * \since 5.5 | 
| 88 |  * \brief Provides parameters that describe a mouse event. | 
| 89 |  * | 
| 90 |  * Mouse events occur when a mouse button is pressed and the ray | 
| 91 |  * traversing the view, originating from the mouse position intersects with one | 
| 92 |  * or more elements of the scene. | 
| 93 |  * | 
| 94 |  * \sa KeyEvent, WheelEvent, MouseHandler | 
| 95 |  */ | 
| 96 |  | 
| 97 | /*! | 
| 98 |  * \qmlproperty int Qt3D.Input::MouseEvent::x | 
| 99 |  * Specifies The X coordinate of the mouse event | 
| 100 |  * \readonly | 
| 101 |  */ | 
| 102 |  | 
| 103 | /*! | 
| 104 |  * \qmlproperty int Qt3D.Input::MouseEvent::y | 
| 105 |  * Specifies The Y coordinate of the mouse event | 
| 106 |  * \readonly | 
| 107 |  */ | 
| 108 |  | 
| 109 | /*! | 
| 110 |  * \qmlproperty bool Qt3D.Input::MouseEvent::wasHeld | 
| 111 |  * Specifies if a mouse button was held down during the mouse event | 
| 112 |  * \readonly | 
| 113 |  */ | 
| 114 |  | 
| 115 | /*! | 
| 116 |  * \qmlproperty Buttons Qt3D.Input::MouseEvent::button | 
| 117 |  * Specifies the button triggering the mouse event | 
| 118 |  * \readonly | 
| 119 |  */ | 
| 120 |  | 
| 121 | /*! | 
| 122 |  * \qmlproperty int Qt3D.Input::MouseEvent::buttons | 
| 123 |  * Specifies the button triggering the mouse event | 
| 124 |  * \readonly | 
| 125 |  */ | 
| 126 |  | 
| 127 | /*! | 
| 128 |  * \qmlproperty Modifiers Qt3D.Input::MouseEvent::modifiers | 
| 129 |  * Specifies if any modifiers were applied to the mouse event | 
| 130 |  * \readonly | 
| 131 |  */ | 
| 132 |  | 
| 133 | /*! | 
| 134 |  * \qmlproperty bool Qt3D.Input::MouseEvent::accepted | 
| 135 |  * Specifies if the mouse event has been accepted | 
| 136 |  */ | 
| 137 |  | 
| 138 | /*! | 
| 139 |  * \class Qt3DInput::QMouseEvent | 
| 140 |  * \inheaderfile Qt3DInput/QMouseEvent | 
| 141 |  * \inmodule Qt3DInput | 
| 142 |  * | 
| 143 |  * \brief The Qt3DCore::QMouseEvent contains parameters that describe a mouse event. | 
| 144 |  * | 
| 145 |  * Mouse events occur when a mouse button is pressed and the ray | 
| 146 |  * traversing the view, originating from the mouse position intersects with one | 
| 147 |  * or more elements of the scene. | 
| 148 |  * | 
| 149 |  * \since 5.5 | 
| 150 |  * | 
| 151 |  * \sa QKeyEvent, QWheelEvent, QMouseHandler | 
| 152 |  * | 
| 153 |  */ | 
| 154 |  | 
| 155 | /*! | 
| 156 |  * \property QMouseEvent::x | 
| 157 |  * Specifies The X coordinate of the mouse event | 
| 158 |  * \readonly | 
| 159 |  */ | 
| 160 |  | 
| 161 | /*! | 
| 162 |  * \property QMouseEvent::y | 
| 163 |  * Specifies The y coordinate of the mouse event | 
| 164 |  * \readonly | 
| 165 |  */ | 
| 166 |  | 
| 167 | /*! | 
| 168 |  * \property QMouseEvent::wasHeld | 
| 169 |  * Specifies if a mouse button was held down during the mouse event | 
| 170 |  * \readonly | 
| 171 |  */ | 
| 172 |  | 
| 173 | /*! | 
| 174 |  * \property QMouseEvent::button | 
| 175 |  * Specifies the button triggering the mouse event | 
| 176 |  * \readonly | 
| 177 |  */ | 
| 178 |  | 
| 179 | /*! | 
| 180 |  * \property QMouseEvent::buttons | 
| 181 |  * Specifies the button triggering the mouse event | 
| 182 |  * \readonly | 
| 183 |  */ | 
| 184 |  | 
| 185 | /*! | 
| 186 |  * \property QMouseEvent::modifiers | 
| 187 |  * Specifies if any modifiers were applied to the mouse event | 
| 188 |  * \readonly | 
| 189 |  */ | 
| 190 |  | 
| 191 | /*! | 
| 192 |  * \property QMouseEvent::accepted | 
| 193 |  * Specifies if the mouse event has been accepted | 
| 194 |  */ | 
| 195 |  | 
| 196 | /*! | 
| 197 |  * \enum Qt3DInput::QMouseEvent::Buttons | 
| 198 |  * | 
| 199 |  * \value LeftButton | 
| 200 |  * \value RightButton | 
| 201 |  * \value MiddleButton | 
| 202 |  * \value BackButton | 
| 203 |  * \value NoButton | 
| 204 |  */ | 
| 205 |  | 
| 206 | /*! | 
| 207 |  * \enum Qt3DInput::QMouseEvent::Modifiers | 
| 208 |  * | 
| 209 |  * \value NoModifier | 
| 210 |  * \value ShiftModifier | 
| 211 |  * \value ControlModifier | 
| 212 |  * \value AltModifier | 
| 213 |  * \value MetaModifier | 
| 214 |  * \value KeypadModifier | 
| 215 |  */ | 
| 216 |  | 
| 217 | /*! | 
| 218 |  * \typedef Qt3DInput::QMouseEventPtr | 
| 219 |  * \relates Qt3DInput::QMouseEvent | 
| 220 |  * | 
| 221 |  * A shared pointer for QMouseEvent. | 
| 222 |  */ | 
| 223 |  | 
| 224 | /*! | 
| 225 |  * \fn int Qt3DInput::QMouseEvent::x() const | 
| 226 |  * | 
| 227 |  *  Returns the x position of the mouse event. | 
| 228 |  */ | 
| 229 |  | 
| 230 | /*! | 
| 231 |  * \fn int Qt3DInput::QMouseEvent::y() const | 
| 232 |  * | 
| 233 |  *  Returns the y position of the mouse event. | 
| 234 |  */ | 
| 235 |  | 
| 236 | /*! | 
| 237 |  * \fn bool Qt3DInput::QMouseEvent::isAccepted() const | 
| 238 |  * | 
| 239 |  *  Returns whether the event was accepted. | 
| 240 |  */ | 
| 241 |  | 
| 242 | /*! | 
| 243 |  * \fn void Qt3DInput::QMouseEvent::setAccepted(bool accepted) | 
| 244 |  * | 
| 245 |  *  Sets the event as accepted if \a accepted is true. | 
| 246 |  * | 
| 247 |  * \note When an event is accepted, it will prevent further propagation to other | 
| 248 |  * listeners. | 
| 249 |  */ | 
| 250 |  | 
| 251 | /*! | 
| 252 |  * \fn QEvent::Type Qt3DInput::QMouseEvent::type() const | 
| 253 |  * | 
| 254 |  *  Returns the QEvent::Type of the event. | 
| 255 |  */ | 
| 256 |  | 
| 257 | /*! | 
| 258 |  * Constructs a new QMouseEvent instance for the QMouseEvent \a e. | 
| 259 |  */ | 
| 260 | QMouseEvent::QMouseEvent(const QT_PREPEND_NAMESPACE(QMouseEvent) &e) | 
| 261 |     : QObject() | 
| 262 |     , m_event(e) | 
| 263 | { | 
| 264 | } | 
| 265 |  | 
| 266 | QMouseEvent::~QMouseEvent() | 
| 267 | { | 
| 268 | } | 
| 269 |  | 
| 270 | /*! | 
| 271 |  * Returns the mouse button of the mouse event. | 
| 272 |  */ | 
| 273 | QMouseEvent::Buttons QMouseEvent::button() const | 
| 274 | { | 
| 275 |     switch (m_event.button()) { | 
| 276 |     case Qt::LeftButton: | 
| 277 |         return QMouseEvent::LeftButton; | 
| 278 |     case Qt::RightButton: | 
| 279 |         return QMouseEvent::RightButton; | 
| 280 |     case Qt::MiddleButton: | 
| 281 |         return QMouseEvent::MiddleButton; | 
| 282 |     case Qt::BackButton: | 
| 283 |         return QMouseEvent::BackButton; | 
| 284 |     default: | 
| 285 |         return QMouseEvent::NoButton; | 
| 286 |     } | 
| 287 | } | 
| 288 |  | 
| 289 | /*! | 
| 290 |  * Returns a bitfield to be used to check for mouse buttons that may be | 
| 291 |  * accompanying the mouse event. | 
| 292 |  */ | 
| 293 | int QMouseEvent::buttons() const | 
| 294 | { | 
| 295 |    return m_event.buttons(); | 
| 296 | } | 
| 297 |  | 
| 298 | /*! | 
| 299 |  * Returns the keyboard modifiers that may be accompanying the mouse event. | 
| 300 |  */ | 
| 301 | QMouseEvent::Modifiers QMouseEvent::modifiers() const | 
| 302 | { | 
| 303 |     return modifiersForEvent<QMouseEvent, decltype(m_event)>(event: m_event); | 
| 304 | } | 
| 305 |  | 
| 306 | /*! | 
| 307 |  * \qmltype WheelEvent | 
| 308 |  * \instantiates Qt3DInput::QWheelEvent | 
| 309 |  * \inqmlmodule Qt3D.Input | 
| 310 |  * \since 5.5 | 
| 311 |  * \brief Contains parameters that describe a mouse wheel event. | 
| 312 |  * | 
| 313 |  * Mouse wheel events occur when the mouse wheel is rotated. | 
| 314 |  * | 
| 315 |  * \sa KeyEvent, MouseEvent, MouseHandler | 
| 316 |  * | 
| 317 |  */ | 
| 318 |  | 
| 319 | /*! | 
| 320 |  * \qmlproperty int Qt3D.Input::WheelEvent::x | 
| 321 |  * Specifies The X coordinate of the mouse wheel event | 
| 322 |  * \readonly | 
| 323 |  */ | 
| 324 |  | 
| 325 | /*! | 
| 326 |  * \qmlproperty int Qt3D.Input::WheelEvent::y | 
| 327 |  * Specifies The Y coordinate of the mouse wheel event | 
| 328 |  * \readonly | 
| 329 |  */ | 
| 330 |  | 
| 331 | /*! | 
| 332 |  * \qmlproperty Point Qt3D.Input::WheelEvent::angleDelta | 
| 333 |  * Specifies The change wheel angle of the mouse wheel event | 
| 334 |  * \readonly | 
| 335 |  */ | 
| 336 |  | 
| 337 | /*! | 
| 338 |  * \qmlproperty int Qt3D.Input::WheelEvent::buttons | 
| 339 |  * Specifies the button if present in the mouse wheel event | 
| 340 |  * \readonly | 
| 341 |  */ | 
| 342 |  | 
| 343 | /*! | 
| 344 |  * \qmlproperty Modifiers Qt3D.Input::WheelEvent::modifiers | 
| 345 |  * Specifies if any modifiers were applied to the mouse wheel event | 
| 346 |  * \readonly | 
| 347 |  */ | 
| 348 |  | 
| 349 | /*! | 
| 350 |  * \qmlproperty bool Qt3D.Input::WheelEvent::accepted | 
| 351 |  * Specifies if the mouse wheel event has been accepted | 
| 352 |  */ | 
| 353 |  | 
| 354 | /*! | 
| 355 |  * \class Qt3DInput::QWheelEvent | 
| 356 |  * \inheaderfile Qt3DInput/QWheelEvent | 
| 357 |  * \inmodule Qt3DInput | 
| 358 |  * | 
| 359 |  * \brief The QWheelEvent class contains parameters that describe a mouse wheel event. | 
| 360 |  * | 
| 361 |  * Mouse wheel events occur when the mouse is rotated. | 
| 362 |  * | 
| 363 |  * \since 5.5 | 
| 364 |  * | 
| 365 |  * \sa QKeyEvent, QMouseEvent, QMouseHandler | 
| 366 |  * | 
| 367 |  */ | 
| 368 |  | 
| 369 | /*! | 
| 370 |  * \property QWheelEvent::x | 
| 371 |  * Specifies The X coordinate of the mouse wheel event | 
| 372 |  * \readonly | 
| 373 |  */ | 
| 374 |  | 
| 375 | /*! | 
| 376 |  * \property QWheelEvent::y | 
| 377 |  * Specifies The Y coordinate of the mouse wheel event | 
| 378 |  * \readonly | 
| 379 |  */ | 
| 380 |  | 
| 381 | /*! | 
| 382 |  * \property QWheelEvent::angleDelta | 
| 383 |  * Specifies The change wheel angle of the mouse wheel event | 
| 384 |  * \readonly | 
| 385 |  */ | 
| 386 |  | 
| 387 | /*! | 
| 388 |  * \property QWheelEvent::buttons | 
| 389 |  * Specifies the button if present in the mouse wheel event | 
| 390 |  * \readonly | 
| 391 |  */ | 
| 392 |  | 
| 393 | /*! | 
| 394 |  * \property  QWheelEvent::modifiers | 
| 395 |  * Specifies if any modifiers were applied to the mouse wheel event | 
| 396 |  * \readonly | 
| 397 |  */ | 
| 398 |  | 
| 399 | /*! | 
| 400 |  * \property QWheelEvent::accepted | 
| 401 |  * Specifies if the mouse wheel event has been accepted | 
| 402 |  */ | 
| 403 |  | 
| 404 | /*! | 
| 405 |  * \enum Qt3DInput::QWheelEvent::Buttons | 
| 406 |  * | 
| 407 |  * \value LeftButton | 
| 408 |  * \value RightButton | 
| 409 |  * \value MiddleButton | 
| 410 |  * \value BackButton | 
| 411 |  * \value NoButton | 
| 412 |  */ | 
| 413 |  | 
| 414 | /*! | 
| 415 |  * \enum Qt3DInput::QWheelEvent::Modifiers | 
| 416 |  * | 
| 417 |  * \value NoModifier | 
| 418 |  * \value ShiftModifier | 
| 419 |  * \value ControlModifier | 
| 420 |  * \value AltModifier | 
| 421 |  * \value MetaModifier | 
| 422 |  * \value KeypadModifier | 
| 423 |  */ | 
| 424 |  | 
| 425 |  | 
| 426 | /*! | 
| 427 |  * \typedef Qt3DInput::QWheelEventPtr | 
| 428 |  * \relates Qt3DInput::QWheelEvent | 
| 429 |  * | 
| 430 |  * A shared pointer for QWheelEvent. | 
| 431 |  */ | 
| 432 |  | 
| 433 | /*! | 
| 434 |  * \fn int Qt3DInput::QWheelEvent::x() const | 
| 435 |  * | 
| 436 |  *  Returns the x position of the mouse event. | 
| 437 |  */ | 
| 438 |  | 
| 439 | /*! | 
| 440 |  * \fn int Qt3DInput::QWheelEvent::y() const | 
| 441 |  * | 
| 442 |  *  Returns the x position of the mouse event. | 
| 443 |  */ | 
| 444 |  | 
| 445 | /*! | 
| 446 |  * \fn QPoint Qt3DInput::QWheelEvent::angleDelta() const | 
| 447 |  * | 
| 448 |  * Returns the distance that the wheel is rotated, in eighths of a degree. A | 
| 449 |  * positive value indicates that the wheel was rotated forward (away from the | 
| 450 |  * user), a negative value indicates the wheel was rotated backward (toward the | 
| 451 |  * user). | 
| 452 |  */ | 
| 453 |  | 
| 454 | /*! | 
| 455 |  * \fn bool Qt3DInput::QWheelEvent::isAccepted() const | 
| 456 |  * | 
| 457 |  *  Returns whether the event was accepted. | 
| 458 |  */ | 
| 459 |  | 
| 460 | /*! | 
| 461 |  * \fn void Qt3DInput::QWheelEvent::setAccepted(bool accepted) | 
| 462 |  * | 
| 463 |  *  Sets the event as accepted if \a accepted is true. | 
| 464 |  * | 
| 465 |  * \note When an event is accepted, it will prevent further propagation to other | 
| 466 |  * listeners. | 
| 467 |  */ | 
| 468 |  | 
| 469 | /*! | 
| 470 |  * \fn QEvent::Type Qt3DInput::QWheelEvent::type() const | 
| 471 |  * | 
| 472 |  *  Returns the QEvent::Type of the event. | 
| 473 |  */ | 
| 474 |  | 
| 475 | #if QT_CONFIG(wheelevent) | 
| 476 | /*! | 
| 477 |  * Constructs a new QWheelEvent instance from the QWheelEvent \a e. | 
| 478 |  */ | 
| 479 | QWheelEvent::QWheelEvent(const QT_PREPEND_NAMESPACE(QWheelEvent) &e) | 
| 480 |     : QObject() | 
| 481 |     , m_event(e) | 
| 482 | { | 
| 483 | } | 
| 484 |  | 
| 485 | QWheelEvent::~QWheelEvent() | 
| 486 | { | 
| 487 | } | 
| 488 |  | 
| 489 | /*! | 
| 490 |  * Returns a bitfield to be used to check for mouse buttons that may be | 
| 491 |  * accompanying the wheel event. | 
| 492 |  */ | 
| 493 | int QWheelEvent::buttons() const | 
| 494 | { | 
| 495 |     return m_event.buttons(); | 
| 496 | } | 
| 497 |  | 
| 498 | /*! | 
| 499 |  * Returns the keyboard modifiers that may be accompanying the wheel event. | 
| 500 |  */ | 
| 501 | QWheelEvent::Modifiers QWheelEvent::modifiers() const | 
| 502 | { | 
| 503 |     return modifiersForEvent<QWheelEvent, decltype(m_event)>(event: m_event); | 
| 504 | } | 
| 505 | #endif // QT_CONFIG(wheelevent) | 
| 506 |  | 
| 507 | } // namespace Qt3DInput | 
| 508 |  | 
| 509 | QT_END_NAMESPACE | 
| 510 |  |