-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse_event_example.py
103 lines (70 loc) · 2.92 KB
/
mouse_event_example.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from PySide2 import QtCore
from PySide2 import QtGui
from PySide2 import QtWidgets
from shiboken2 import wrapInstance
import maya.cmds as cmds
import maya.OpenMayaUI as omui
def maya_main_window():
"""
Return the Maya main window widget as a Python object
"""
main_window_ptr = omui.MQtUtil.mainWindow()
return wrapInstance(long(main_window_ptr), QtWidgets.QWidget)
class MoveableWidget(QtWidgets.QWidget):
def __init__(self, x, y, width, height, color, parent=None):
super(MoveableWidget, self).__init__(parent)
self.setFixedSize(width, height)
self.move(x, y)
self.color = color
self.original_color = color
self.move_enabled = False
def mousePressEvent(self, mouse_event):
print("Mouse Button Pressed")
if mouse_event.button() == QtCore.Qt.LeftButton:
self.initial_pos = self.pos()
self.global_pos = mouse_event.globalPos()
self.move_enabled = True
def mouseReleaseEvent(self, mouse_event):
if self.move_enabled:
self.move_enabled = False
def mouseDoubleClickEvent(self, mouse_event):
print("Mouse Double-Click")
if self.color == self.original_color:
self.color = QtCore.Qt.yellow
else:
self.color = self.original_color
self.update() # explicitly call refresh
def mouseMoveEvent(self, mouse_event):
if self.move_enabled:
diff = mouse_event.globalPos() - self.global_pos
print("diff: {}".format(diff))
self.move(self.initial_pos + diff)
def paintEvent(self, paint_event):
painter = QtGui.QPainter(self)
painter.fillRect(paint_event.rect(), self.color)
class MouseEventExample(QtWidgets.QDialog):
WINDOW_TITLE = "Mouse Event Example"
def __init__(self, parent=maya_main_window()):
super(MouseEventExample, self).__init__(parent)
self.setWindowTitle(self.WINDOW_TITLE)
if cmds.about(ntOS=True):
self.setWindowFlags(self.windowFlags() ^ QtCore.Qt.WindowContextHelpButtonHint)
elif cmds.about(macOS=True):
self.setWindowFlags(QtCore.Qt.Tool)
self.setMinimumSize(400, 400)
self.create_widgets()
self.create_layout()
def create_widgets(self):
self.red_widget = MoveableWidget(100, 100, 24, 24, QtCore.Qt.red, self)
self.blue_widget = MoveableWidget(300, 300, 24, 24, QtCore.Qt.blue, self)
def create_layout(self):
main_layout = QtWidgets.QVBoxLayout(self)
main_layout.setContentsMargins(2, 2, 2, 2)
if __name__ == "__main__":
try:
example_dialog.close() # pylint: disable=E0601
example_dialog.deleteLater()
except:
pass
example_dialog = MouseEventExample()
example_dialog.show()