-
Notifications
You must be signed in to change notification settings - Fork 2
/
ContextMenu.py
95 lines (75 loc) · 3.26 KB
/
ContextMenu.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
from PySide2 import QtCore, QtGui, QtWidgets
from time import sleep
class ContextMenu(QtWidgets.QMenu):
def __init__(self, *args):
QtWidgets.QMenu.__init__(self, *args)
self.stock_css = ""
self.is_stock_css = True
self.setWindowFlags(self.windowFlags() | QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.initAnimation()
def reset_css(self):
if not self.is_stock_css:
self.setStyleSheet(self.stock_css)
self.is_stock_css = True
def update_css(self, custom_css):
self.is_stock_css = False
self.setStyleSheet(self.stock_css + custom_css)
def set_stock_css(self, css):
self.setStyleSheet(css)
self.is_stock_css = True
self.stock_css = css
def initAnimation(self):
self.opacityEffect = QtWidgets.QGraphicsOpacityEffect(self, opacity=1.0)
self.setGraphicsEffect(self.opacityEffect)
self.opacityAnim = QtCore.QPropertyAnimation(self.opacityEffect, b"opacity", duration=1)
self.opacityAnim.setStartValue(0)
self.opacityAnim.setEndValue(1)
self.geomAnim = QtCore.QPropertyAnimation(self, b'geometry', easingCurve=QtCore.QEasingCurve.OutCubic, duration=130)
# self.posAnim.setStartValue(startPos)
# self.posAnim.setEndValue(endPos)
# self.posAnim.setEasingCurve(QtCore.QEasingCurve.InSine)
def readyUp_Anim(self, event = None, up_right = False, up_left = False, bottom_left = False):
if up_left and (up_right or bottom_left) or (up_right and bottom_left):
print("please give set only one direction for context menu")
print("Either up_right or up_left or bottom_left")
return
if event:
pos = event.globalPos()
else:
pos = QtGui.QCursor.pos()
size = self.sizeHint()
x, y, w, h = pos.x(), pos.y(), size.width(), size.height()
self.geomAnim.setStartValue(QtCore.QRect(x, y, 0, 0))
if up_right:
self.geomAnim.setEndValue(QtCore.QRect(x, y-h, w, h))
elif up_left:
self.geomAnim.setEndValue(QtCore.QRect(x-w, y-h, w, h))
elif bottom_left:
self.geomAnim.setEndValue(QtCore.QRect(x-w, y, w, h))
else:
self.geomAnim.setEndValue(QtCore.QRect(x, y, w, h))
self.opacityAnim.setDirection(QtCore.QVariantAnimation.Forward)
def getAnim(self):
return [self.opacityAnim, self.geomAnim]
def add_action(self, *args, custom_css = None):
action = self.addAction(*args)
if custom_css:
action.hovered.connect(lambda: self.update_css(custom_css))
else:
action.hovered.connect(self.reset_css)
return action
def hideEvent(self, arg__1: QtGui.QHideEvent) -> None:
# do stuff when context menu is hidden here
self.destroy()
return super().hideEvent(arg__1)
def show_menu(self, showAnim = False, event = None, **kwargs):
self.readyUp_Anim(event= event, **kwargs)
if not event:
pos = QtGui.QCursor.pos()
else:
pos = event.globalPos()
if showAnim:
self.opacityAnim.start()
self.geomAnim.start()
self.exec_(pos)