File: QtMouseDevice.py

package info (click to toggle)
uranium 5.0.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,328 kB
  • sloc: python: 31,765; sh: 132; makefile: 12
file content (61 lines) | stat: -rw-r--r-- 2,410 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Copyright (c) 2022 Ultimaker B.V.
# Uranium is released under the terms of the LGPLv3 or higher.

from PyQt6.QtCore import Qt, QEvent

from UM.InputDevice import InputDevice
from UM.Event import MouseEvent, WheelEvent


class QtMouseDevice(InputDevice):
    """An InputDevice subclass that processes Qt mouse events and returns a UM.Event.MouseEvent"""

    def __init__(self, window):
        super().__init__()
        self._x = None
        self._y = None
        self._window = window

    def handleEvent(self, event):
        if event.type() == QEvent.Type.MouseButtonPress:
            ex, ey = self._normalizeCoordinates(event.pos().x(), event.pos().y())
            e = MouseEvent(MouseEvent.MousePressEvent, ex, ey, self._x, self._y, self._qtButtonsToButtonList(event.buttons()))
            self._x = ex
            self._y = ey
            self.event.emit(e)
        elif event.type() == QEvent.Type.MouseMove:
            ex, ey = self._normalizeCoordinates(event.pos().x(), event.pos().y())
            e = MouseEvent(MouseEvent.MouseMoveEvent, ex, ey, self._x, self._y, self._qtButtonsToButtonList(event.buttons()))
            self._x = ex
            self._y = ey
            self.event.emit(e)
        elif event.type() == QEvent.Type.MouseButtonRelease:
            ex, ey = self._normalizeCoordinates(event.pos().x(), event.pos().y())
            e = MouseEvent(MouseEvent.MouseReleaseEvent, ex, ey, self._x, self._y, self._qtButtonsToButtonList(event.button()))
            self._x = ex
            self._y = ey
            self.event.emit(e)
        elif event.type() == QEvent.Type.Wheel:
            delta = event.angleDelta()
            e = WheelEvent(delta.x(), delta.y())
            self.event.emit(e)

    def _qtButtonsToButtonList(self, qt_buttons):
        buttons = []

        if qt_buttons == Qt.MouseButton.LeftButton.value:
            buttons.append(MouseEvent.LeftButton)
        if qt_buttons == Qt.MouseButton.RightButton.value:
            buttons.append(MouseEvent.RightButton)
        if qt_buttons == Qt.MouseButton.MiddleButton.value:
            buttons.append(MouseEvent.MiddleButton)
        return buttons

    def _normalizeCoordinates(self, x, y):
        try:
            nx = 2.0 * (x / self._window.width()) - 1.0
            ny = 2.0 * (y / self._window.height()) - 1.0
        except ZeroDivisionError:
            return 0, 0

        return nx, ny