-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_log.py
276 lines (218 loc) · 10.8 KB
/
command_log.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from PySide2.QtCore import Qt, QPoint
from PySide2.QtGui import QPainter, QPen, QColor, QPalette, QRegion
from PySide2.QtWidgets import QWidget, QVBoxLayout, QScrollArea
from PySide2.QtCore import Qt
from PySide2 import QtWidgets
from command_text_edit import CommandTextEdit
class CommandLog(QScrollArea):
def __init__(self,
max_text_edits = 100,
text_edit_margins = 4,
rect_border_radius = 5,
):
super().__init__()
self.max_text_edits = max_text_edits
self.rect_border_radius = rect_border_radius
self.text_edit_margins = text_edit_margins
log = QWidget()
self.setWidget(log)
self.setWidgetResizable(True)
self.layout = QVBoxLayout(log)
self.layout.setContentsMargins(0, 0, 0, 0)
self.setAlignment(Qt.AlignLeft | Qt.AlignTop)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.viewport().setAutoFillBackground(False)
self.rect_outline_color = QColor(0, 0, 0, 0)
self.rect_outline_width = 0
self.scroll_point_pos = None
self.force_disable_background = False
self.scroll_to_bottom = False
self.verticalScrollBar().rangeChanged.connect(self.scrollToBottom)
def setTextEditMargins(self, margins):
'''
Retroactively sets margins of text edits, updating scrollbar position
'''
if self.text_edit_margins != margins:
self.text_edit_margins = margins
if self.layout.count():
normalized_position = self.getNormalizedScrollBarPosition()
for i in range(self.layout.count()):
text_edit = self.layout.itemAt(i).widget()
text_edit.setDocumentMargin(margins)
self.resizeTextEdit(text_edit)
self.setScrollBarToNormalizedPosition(normalized_position, True)
def setTextEditBorderRadius(self, radius):
''' Retroactively sets border radius of text edits '''
if self.rect_border_radius != radius:
self.rect_border_radius = radius
for i in range(self.layout.count()):
text_edit = self.layout.itemAt(i).widget()
text_edit.setRectBorderRadius(radius)
text_edit.update()
def setDrawFrame(self, draw_frame):
if draw_frame == True:
self.setFrameShape(QtWidgets.QFrame.StyledPanel)
else:
self.setFrameShape(QtWidgets.QFrame.NoFrame)
def setDirection(self, direction):
''' Sets layout direction, updating scrollbar position '''
if self.layout.direction() != direction:
self.layout.setDirection(direction)
if self.layout.count():
normalized_position = self.getNormalizedScrollBarPosition()
# inverting scroll bar position
# to match previously shown text edits
maximum = self.verticalScrollBar().maximum()
new_position = maximum * (1 - normalized_position)
self.verticalScrollBar().setValue(new_position)
def setAlignment(self, alignment):
''' Retroactively sets alignment of text edits in layout '''
if self.layout.alignment() != alignment:
self.layout.setAlignment(alignment)
for i in range(self.layout.count()):
text_edit = self.layout.itemAt(i).widget()
self.layout.setAlignment(text_edit, alignment)
def setSpacing(self, spacing):
''' Retroactively sets spacing between text edits '''
if self.layout.spacing() != spacing:
if self.layout.count() == 0:
self.layout.setSpacing(spacing)
else:
normalized_position = self.getNormalizedScrollBarPosition()
self.layout.setSpacing(spacing)
self.setScrollBarToNormalizedPosition(normalized_position, True)
def setMaxTextEdits(self, max):
self.max_text_edits = max
self.clearTo(max)
def getNormalizedScrollBarPosition(self):
''' Returns scroll bar position normalized to values between 0 and 1 '''
scroll_bar = self.verticalScrollBar()
if scroll_bar.maximum() == 0:
if self.layout.direction() == QVBoxLayout.TopToBottom:
return 1
else:
return 0
return scroll_bar.value() / scroll_bar.maximum()
def setScrollBarToNormalizedPosition(self, position, wait_for_update = False):
# waiting for scrollbar to be updated
if wait_for_update == True:
qApp.processEvents()
qApp.processEvents()
# won't be 100 accurate for middle values,
# but will keep scrollbar at the bottom, when at bottom
maximum = self.verticalScrollBar().maximum()
new_position = maximum * position
self.verticalScrollBar().setValue(new_position)
def setRectOutlineColor(self, color):
''' Retroactively sets rectangle outline color'''
if self.rect_outline_color != color:
self.rect_outline_color = color
for i in range(self.layout.count()):
text_edit = self.layout.itemAt(i).widget()
text_edit.setRectOutlineColor(color)
text_edit.update()
def setRectOutlineWidth(self, width):
''' Retroactively sets rectangle outline width'''
if self.rect_outline_width != width:
self.rect_outline_width = width
if self.layout.count() > 0:
normalized_position = self.getNormalizedScrollBarPosition()
for i in range(self.layout.count()):
text_edit = self.layout.itemAt(i).widget()
text_edit.setRectOutlineWidth(width)
self.resizeTextEdit(text_edit)
self.setScrollBarToNormalizedPosition(normalized_position, True)
def append(self, text):
''' Appends a new text edit to the end '''
command_text_edit = CommandTextEdit(
text,
self.text_edit_margins,
self.rect_border_radius,
self.rect_outline_color,
self.rect_outline_width
)
self.layout.addWidget(command_text_edit, 0, self.layout.alignment())
self.clearTo(self.max_text_edits)
command_text_edit.show()
self.resizeTextEdit(command_text_edit)
if self.layout.direction() == QVBoxLayout.TopToBottom:
self.scroll_to_bottom = True
def clearTo(self, num_to_keep):
if self.layout.count() > num_to_keep:
while self.layout.count() > num_to_keep:
child = self.layout.takeAt(0)
if child.widget():
child.widget().deleteLater()
self.update()
def clear(self):
self.clearTo(0)
def scrollToBottom(self):
if self.scroll_to_bottom == True:
self.scroll_to_bottom = False
self.verticalScrollBar().setValue(self.verticalScrollBar().maximum())
def resizeEvent(self, event):
super().resizeEvent(event)
self.updateTextEdits()
def updateTextEdits(self):
for i in range(self.layout.count()):
self.resizeTextEdit(self.layout.itemAt(i).widget())
def resizeTextEdit(self, text):
size = text.sizeForWidth(self.viewport().width())
text.setFixedSize(size)
"""
Scrolling point is drawn under the mouse when scrolling,
when the window has a fully transparent or forcefully disabled background.
Without it, scrolling and the mouse hitting a transparent area,
stops the scrolling.
"""
def wheelEvent(self, event):
if (self.palette().color(QPalette.Window).alpha() == 0 or
self.force_disable_background == True):
self.enableScrollingPoint(event.position())
super().wheelEvent(event)
def mouseMoveEvent(self, event):
self.disableScrollingPoint()
super().mouseMoveEvent(event)
def leaveEvent(self, event):
self.disableScrollingPoint()
super().leaveEvent(event)
def paintEvent(self, event):
super().paintEvent(event)
if self.scroll_point_pos:
painter = QPainter(self.viewport())
pen = QPen(QColor(0, 0, 0, 1))
painter.setPen(pen)
painter.drawPoint(self.scroll_point_pos)
def enableScrollingPoint(self, position):
if self.scroll_point_pos == None:
self.scroll_point_pos = position
# both required
self.setMouseTracking(True)
self.widget().setMouseTracking(True)
def disableScrollingPoint(self):
if self.scroll_point_pos:
self.scroll_point_pos = None
# both required
self.setMouseTracking(False)
self.widget().setMouseTracking(False)
self.update()
def setForceDisableBackground(self, toggle):
self.force_disable_background = toggle
def getBackgroundlessMask(self):
''' Returns a mask matching the widget excluding the background '''
frame_geometry = self.frameGeometry()
region = QRegion(frame_geometry)
region -= self.childrenRegion()
for i in range(self.layout.count()):
text_edit = self.layout.itemAt(i).widget()
text_edit_region = text_edit.region()
text_edit_top_left = text_edit.rect().topLeft()
text_edit_top_left += QPoint(self.frameWidth(), self.frameWidth())
offset = text_edit.mapTo(self.viewport(), text_edit_top_left)
text_edit_region.translate(offset)
region += text_edit_region
if self.scroll_point_pos:
region += QRegion(self.scroll_point_pos.x() + self.frameWidth(),
self.scroll_point_pos.y() + self.frameWidth(),
1, 1)
return region