-
Notifications
You must be signed in to change notification settings - Fork 0
/
label_grid.py
209 lines (168 loc) · 6.76 KB
/
label_grid.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from PySide2.QtCore import QRect, QRectF, Qt, QTimer, Slot, Signal
from PySide2.QtGui import QFont, QColor, QPainter, QPixmap
from PySide2.QtWidgets import QWidget
from command_label import CommandLabel
from logger import *
from tiles import *
class GridState:
# currently shown layer
layer: str = "empty"
# keep grid open after selecting
hold: bool = False
# select on timeout
timeout: bool = False
# selected key modifiers
modifiers: set[str] = set()
class GridLines(QWidget):
geometry: QRect
def __init__(self, parent, geometry: QRect):
super().__init__(parent)
self.setGeometry(geometry)
self.geometry = geometry
self.show()
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen(Colors.grid_lines)
dx = int(self.geometry.width() / Tiles.x)
dy = int(self.geometry.height() / Tiles.y)
# # horizontal lines
# for i in range(3):
# painter.drawRect(0, i * 2 * dy, self.geometry.width(), 2 * dy)
# # vertical lines
# for i in range(3):
# painter.drawRect(i * 4 * dx, 0, 4 * dx, self.geometry.height())
class LabelGrid(QWidget):
state = GridState()
labels: dict[tuple[int, int], CommandLabel] = {}
hover_item = None
action_signal = Signal(object, object, bool)
def __init__(self, parent, geometry, tags):
super().__init__(parent)
self.tags = tags
self.setGeometry(geometry)
self.setFocusPolicy(Qt.NoFocus)
self.hide_timer = QTimer(self)
self.hide_timer.timeout.connect(lambda: self.activate("empty"))
self.hide_timer.setSingleShot(True)
self.hover_timer = QTimer(self)
self.hover_timer.timeout.connect(self.select_item)
dx = int(geometry.width() / Tiles.x)
dy = int(geometry.height() / Tiles.y)
for y in range(Tiles.y):
for x in range(Tiles.x):
label = CommandLabel(self)
label.setAlignment(Qt.AlignCenter)
label.setGeometry(x * dx, y * dy, dx, dy)
label.installEventFilter(self)
label.setFocusPolicy(Qt.NoFocus)
self.labels[(x, y)] = label
self.update_grid()
self.lines = GridLines(self, geometry)
def activate(self, label, modifiers=set()):
if label != "_hide":
self.state.modifiers.clear()
self.state.modifiers.update(modifiers)
self.set_layer(label)
if not self.isVisible():
self.show()
elif self.isVisible():
self.set_hovered_item(None)
self.state.modifiers.clear()
self.state.hold = False
self.hover_timer.stop()
self.hide()
def hide_delayed(self):
if self.isVisible() and not self.hide_timer.isActive():
self.hide_timer.start(50)
def set_layer(self, levelId):
self.state.layer = levelId
self.update_grid()
def on_gaze(self, x, y, after_activation=False):
if not QRectF(0, 0, 1, 1).contains(x, y):
return log_debug("outside")
xWidget = int(Tiles.x * x)
yWidget = int(Tiles.y * y)
if xWidget >= Tiles.x or yWidget >= Tiles.y:
return log_debug("invalid indices: " + str(xWidget) + ", " + str(yWidget))
widget = self.labels[(xWidget, yWidget)]
self.set_hovered_item(widget)
if not after_activation and self.state.timeout:
self.hover_timer.start(int(Times.element_selection * 1000))
else:
self.hover_timer.stop()
def set_hovered_item(self, widget):
if widget != self.hover_item:
if widget:
widget.setHovered(True)
if self.hover_item:
self.hover_item.setHovered(False)
self.hover_item = widget
def update_grid(self):
for label in self.labels.values():
label.id = None
label.setToggled(False)
# self.hoverItem.setHovered(False)
shown_labels = tiles[self.state.layer] | tiles["always"]
for group_id, (x, y) in shown_labels.items():
group = tile_groups[group_id]
for i, (tile_id, actions) in enumerate(group["tiles"].items()):
label = self.labels[
(x + (i % group["width"]), y + (int(i / group["width"])))
]
label.id = tile_id
label.setItems(actions)
label.setColor(group.get("color", Colors.item))
label.setModifiers(self.state.modifiers)
if self.tags.has("hide_labels"):
label.setText("")
else:
label.setText(actions[0].label)
label.show()
# special cases
if label.id == "hold" and self.state.hold:
label.setToggled(True)
elif label.id in self.state.modifiers:
label.setToggled(True)
elif type(actions[0]) is TagAction:
if type(actions[1]) is TagAction:
label.setToggled(
(
self.tags.has(actions[0].tag),
self.tags.has(actions[1].tag),
)
)
else:
label.setToggled(self.tags.has(actions[0].tag))
for label in self.labels.values():
if label.id == None:
label.hide()
@Slot()
def select_item(self, index=0, hide=None):
if self.hover_item == None or self.hover_item.id == None:
self.action_signal.emit(None, None, hide)
return
log_info(self.hover_item)
log_info("selectItem: " + self.hover_item.id)
self.hover_timer.stop()
self.hover_item.activate()
if index == 1 and self.hover_item.items[index] is None:
index = 0
if hide == None:
hide = not self.state.hold
if self.hover_item.id == "hold":
self.state.hold = not self.state.hold
self.update_grid()
elif self.hover_item.id in modifierColors:
if self.hover_item.id in self.state.modifiers:
self.state.modifiers.remove(self.hover_item.id)
else:
self.state.modifiers.add(self.hover_item.id)
self.update_grid()
elif type(self.hover_item.items[index]) is KeyAction:
self.action_signal.emit(
self.hover_item.items[index], self.state.modifiers, hide
)
self.state.modifiers.clear()
self.update_grid()
else:
self.action_signal.emit(self.hover_item.items[index], None, hide)