forked from IamFlea/bartender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui_widget.py
128 lines (101 loc) · 4.78 KB
/
ui_widget.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from PyQt5 import QtCore, QtGui, QtWidgets, Qt
class QOverlayWidget(QtWidgets.QWidget):
"""docstring for QOverlayWidget"""
GRIP_PX = 10 # Size of grip for resizing these widgets (in pixels)
EXPAND_LIST = ["→↓", "→↑", "←↓", "←↑", "↓←", "↑←", "↓→", "↑→", ]
def __init__(self, name, parent, resizable=True):
super(QOverlayWidget, self).__init__(parent)
self.parent = parent
self.name = name
# Adds background widget
# Creates a movable stuff
self.drag_widget = QtWidgets.QWidget(self)
self.drag_widget.setStyleSheet("background-color: rgba(255,180,0,164);")
self.drag_widget.setVisible(False)
self.label = QtWidgets.QLabel(self.name, self.drag_widget)
# Sets grip (white area)
if resizable:
self.grip = QtWidgets.QWidget(self.drag_widget)
self.grip.setStyleSheet("background-color: rgba(255,255,255,255);")
self.expand = QtWidgets.QLabel("", self.drag_widget)
self.expand.setStyleSheet("color: rgba(0,0,0,255); background-color: rgba(0,0,0,0);")
font = QtGui.QFont(self.expand.font().family(), 20)
font.setBold(True)
self.expand.setFont(font)
# comment this
self.movable = False
self.resizable = resizable
self.__moving = False
self.__resizing = False
self.__offset = QtCore.QPoint()
self.expand_index = 0
self.expand.setText(QOverlayWidget.EXPAND_LIST[self.expand_index])
def setGeometry(self, *arg):
super(QOverlayWidget, self).setGeometry(*arg)
self.drag_widget.setGeometry(0,0,self.width(), self.height())
if self.resizable: # and self.icon_list :)
self.grip.setGeometry(self.width() - QOverlayWidget.GRIP_PX, self.height() - QOverlayWidget.GRIP_PX, QOverlayWidget.GRIP_PX, QOverlayWidget.GRIP_PX)
self.expand.setGeometry(self.width() - 42, self.height() - self.expand.height() - 15, 42, self.expand.height())
def mousePressEvent(self, event):
pos = event.pos()
if event.buttons() == QtCore.Qt.LeftButton and self.movable\
and (pos.x() <= self.width() - QOverlayWidget.GRIP_PX \
or pos.y() <= self.height() - QOverlayWidget.GRIP_PX
or not self.resizable):
# Draging // left click on orange area
self.__moving = True
self.__offset = pos
elif not (pos.x() <= self.width() - QOverlayWidget.GRIP_PX or pos.y() <= self.height() - QOverlayWidget.GRIP_PX):
# Resizing // left click on the white area (grip)
self.__resizing = True
if event.buttons() == QtCore.Qt.RightButton and self.movable:
# Changing direction // right click on the whole widget
self.expand_index = (self.expand_index + 1) % len(QOverlayWidget.EXPAND_LIST)
self.expand.setText(QOverlayWidget.EXPAND_LIST[self.expand_index])
def set_expand_index(self, index):
self.expand_index = index
self.expand.setText(QOverlayWidget.EXPAND_LIST[self.expand_index])
def drag(self, position):
# Move with it
self.move(self.mapToParent(position - self.__offset))
# Grab the new stuff
x, y = self.x(), self.y()
width, height = self.width(), self.height()
max_width, max_height = self.parent.width(), self.parent.height()
# Checking X value
if x < 0:
x = 0
elif x + width >= max_width:
x = max_width - width
# Checking Y value
if y < 0:
y = 0
elif y + height >= max_height:
y = max_height - height
# Update if the values are wrong
self.setGeometry(x, y, width, height)
def resize(self, position):
# Make it resizable, restric it to GRIP_PX so user won't hide the widget by mistake
x, y = position.x(), position.y()
if x < QOverlayWidget.GRIP_PX:
x = QOverlayWidget.GRIP_PX
if y < QOverlayWidget.GRIP_PX:
y = QOverlayWidget.GRIP_PX
self.setGeometry(self.x(), self.y(), x, y)
def mouseMoveEvent(self, event):
if event.buttons() == QtCore.Qt.LeftButton:
if self.__moving:
self.drag(event.pos()) # DO NOT USE NAME "self.move" !!!!
elif self.__resizing:
self.resize(event.pos())
def mouseReleaseEvent(self, event):
self.__offset = QtCore.QPoint()
self.__moving = False
self.__resizing = False
def set_movable(self, boolean):
self.raise_()
self.movable = boolean
self.drag_widget.setVisible(boolean)
self.drag_widget.raise_()
if __name__ == '__main__':
import bartender