/
textbox.py
416 lines (338 loc) · 12.6 KB
/
textbox.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
from text import Label, text_dimensions
from datetime import datetime, timedelta
from line import Line
import vtk
def rotate(point, angle):
import math
x, y = point
# Rotate about the origin
theta = math.radians(angle)
xrot = x * math.cos(theta) - y * math.sin(theta)
yrot = x * math.sin(theta) + y * math.cos(theta)
return int(xrot), int(yrot)
class Textbox(Label):
def __init__(self, interactor, string, on_editing_end=None,
highlight_color=None, highlight_opacity=None, **kwargs):
super(Textbox, self).__init__(interactor, string, **kwargs)
self.drag_interval = timedelta(0, .1)
self.editing = False
self.blank = False
self.edit_indicator = None
self.column = 0
self.row = 0
self.text = string
self.on_editing_end = on_editing_end
self.highlight_opacity = highlight_opacity
self.highlight_color = highlight_color
self.cursor = Line(
(0, 0), (1, 1), renderer=self.widget.GetCurrentRenderer(), width=2)
# Blink the cursor if we're editing.
self.blink_timer = self.interactor.CreateRepeatingTimer(600)
self.blink_observer = self.interactor.AddObserver(
"TimerEvent",
self.blink_cursor)
# All timer events trigger all listeners, so we will only update when
# the time elapsed is the expected period.
self.last_blink = datetime.now()
# Use the third argument (priority) to intercept key events before
# anything else does
self.keyboard_observer = self.interactor.AddObserver(
"KeyPressEvent",
self.typed,
1.0)
def blink_cursor(self, obj, event):
if datetime.now() - self.last_blink < timedelta(0, 0, 0, 400):
return
self.last_blink = datetime.now()
if self.editing:
self.place_cursor()
if self.cursor.showing:
self.cursor.hide()
else:
self.cursor.show()
self.manager.queue_render()
def show_cursor(self):
self.last_blink = datetime.now()
self.place_cursor()
self.cursor.show()
self.manager.queue_render()
def get_char(self):
keycode = self.interactor.GetKeyCode()
if len(keycode) == 0 or ord(keycode) > 126 or ord(keycode) < 33:
keycode = self.interactor.GetKeySym()
return keycode
def add_character(self, character):
rows = self.text.split("\n")
row = rows[self.row]
if self.column >= len(row):
# It shouldn't be possible for self.blank to be true here
# self.column will only be >= len(row) if we're navigating
# up from another row (indicating that blank is false, because
# there's text on another line)
row += character
rows[self.row] = row
if character == "\n":
self.column = 0
self.row += 1
else:
self.column = len(row)
else:
if self.blank:
self.blank = False
row = character
else:
row = row[:self.column] + character + row[self.column:]
rows[self.row] = row
if character != "\n":
self.column += 1
else:
self.row += 1
self.column = 0
self.text = "\n".join(rows)
def delete_character(self):
rows = self.text.split("\n")
if self.column == 0 and self.row > 0:
row = rows[self.row - 1]
self.column = len(row)
row += rows[self.row]
rows[self.row - 1] = row
del rows[self.row]
self.row -= 1
elif self.column == 0:
return
else:
row = rows[self.row]
if self.column < len(row):
rows[self.row] = row[:self.column - 1] + row[self.column:]
self.column -= 1
else:
rows[self.row] = row[:-1]
self.column = len(row) - 1
self.text = "\n".join(rows)
if self.text == "":
self.blank = True
self.text = " "
def typed(self, obj, event):
if self.editing:
c = self.get_char()
if c in "qQeEjJ3cCtTaAsS":
# Prevents VTK from doing stuff with these keys
self.interactor.SetKeyCode("`")
if c == "Backspace":
self.delete_character()
elif c == "Return":
self.add_character("\n")
elif len(c) == 1:
self.add_character(c)
elif c == "space":
self.add_character(" ")
elif c == "Escape":
self.stop_editing()
return
elif c[:5] == "Shift":
pass
elif c == "Tab":
self.add_character("\t")
if c in ("Left", "Right", "Up", "Down"):
rows = self.text.split("\n")
if c == "Left":
if self.column == 0 and self.row > 0:
self.row -= 1
self.column = len(rows[self.row])
else:
self.column = max(0, self.column - 1)
elif c == "Right":
if self.column == len(
rows[self.row]) and self.row < len(rows) - 1:
self.column = 0
self.row += 1
else:
self.column = min(self.column + 1, len(rows[self.row]))
elif c == "Up":
if self.row == 0:
self.column = 0
else:
self.row = self.row - 1
elif c == "Down":
if self.row == len(rows) - 1:
self.column = len(rows[self.row])
else:
self.row += 1
self.show_cursor()
if self.text[-2:] == "QQ":
self.stop_editing()
self.text = self.text[:-2]
self.update()
def update(self):
if self.repr.GetText() != self.text:
self.set_text(self.text)
self.place_cursor()
def place_cursor(self):
# Find current position of the text actor
x, y = self.left, self.top
# Use to adjust all window-space numbers
w, h = self.interactor.GetRenderWindow().GetSize()
# Translate distance from top to distance from bottom
y = h - y
# Prep a text property for getting measurements
prop = vtk.vtkTextProperty()
prop.ShallowCopy(self.actor.GetTextProperty())
# Store for rotating the cursor's coords
angle = prop.GetOrientation()
# Reset so we get accurate dimensions
prop.SetOrientation(0)
rows = self.text.split("\n")
dpi = self.interactor.GetRenderWindow().GetDPI()
width, height = text_dimensions(self.text, prop, dpi)
line_height = float(height) / len(rows)
column_adjustment, _ = text_dimensions(rows[self.row][:self.column],
prop, dpi)
x += column_adjustment
row_width, _ = text_dimensions(rows[self.row], prop, dpi)
# Adjust for blank space caused by justifications
halign = prop.GetJustificationAsString()
if halign == "Centered":
x += (width - row_width) / 2.
elif halign == "Right":
x += (width - row_width) + 1 # Adjust for some margin issues
elif halign == "Left":
x -= 3 # Adjust for some margin issues
# Manual adjustments for justification artefacts
valign = prop.GetVerticalJustificationAsString()
if valign == "Top":
y += 2
elif valign == "Centered":
pass
elif valign == "Bottom":
y -= 2
# Get to the current row
y -= line_height * self.row
# Rotate both points to the orientation as the text
y1 = y
y2 = y - line_height
x1 = x
x2 = x
x1, x2 = x1 - self.x, x2 - self.x
y1, y2 = y1 - self.y, y2 - self.y
x1, y1 = rotate((x1, y1), angle)
x2, y2 = rotate((x2, y2), angle)
x1 += self.x
x2 += self.x
y1 += self.y
y2 += self.y
self.cursor.point_1 = (x1, y1)
self.cursor.point_2 = (x2, y2)
def row_col_at_point(self, x, y):
rows = self.text.split("\n")
prop = self.actor.GetTextProperty()
dpi = self.interactor.GetRenderWindow().GetDPI()
text_width, text_height = text_dimensions(self.text, prop, dpi)
# Viewport coordinates of widget
sw, sh = self.interactor.GetRenderWindow().GetSize()
# Normalize to window space
if x < 1 and y < 1:
x = sw * x
y = sh * y
# Rotate the click out of box space
x -= self.x
y -= self.y
x, y = rotate((x, y), -1 * prop.GetOrientation())
x += self.x
y += self.y
x0, y0 = self.left, sh - self.top
# Adjust click coords to widget's bounds
x = abs(x - x0)
y = text_height - abs(y - y0)
# Calculate the bounds of each row
row_bounds = []
max_width = 0
# We're iterating in reverse because y goes from 0 at the bottom to 1
# at the top
row_at_point = None
for row in rows[::-1]:
if row == '':
dim_row = ' '
else:
dim_row = row
w, h = text_dimensions(dim_row, prop, dpi)
row_bounds.append((w, h))
if w > max_width:
# Use for x offset calculations
max_width = w
if h < y:
y = y - h
else:
if row_at_point is None:
row_at_point = rows.index(row)
if row_at_point is None:
row_at_point = 0
# List was assembled backwards
row_bounds.reverse()
# Now let's find the column clicked...
row = row_bounds[row_at_point]
text = rows[row_at_point]
# If max_width == row[0], then offset is 0 and all of the calcs below
# still work
just = prop.GetJustificationAsString()
if just == "Left":
row_left = 0
elif just == "Centered":
row_left = (max_width - row[0]) / 2.
elif just == "Right":
row_left = max_width - row[0]
row_right = row_left + row[0]
if x < row_left:
# Clicked to the left of the row
return row_at_point, 0
if x > row_right:
# Clicked to the right of the row
return row_at_point, len(rows[row_at_point])
if row == '':
# Clicked on the blank row (inserted a space when calculating width
# earlier, for height considerations)
return row_at_point, 1
# OK, no easy answer; have to calc the width of each letter in the row till we find the column
# Start from left
w = 0
ind = 1
while row_left + w < x and ind < len(text):
w, _ = text_dimensions(text[:ind], prop, dpi)
ind += 1
return row_at_point, ind - 1
def show_highlight(self):
prop = self.actor.GetTextProperty()
prop.SetBackgroundColor(self.highlight_color)
prop.SetBackgroundOpacity(self.highlight_opacity)
def hide_highlight(self):
prop = self.actor.GetTextProperty()
prop.SetBackgroundOpacity(0)
def start_editing(self, point=None):
if point is not None:
self.row, self.column = self.row_col_at_point(*point)
else:
rows = self.text.split("\n")
self.row = len(rows) - 1
self.column = len(rows[-1])
self.place_cursor()
self.editing = True
def stop_editing(self):
self.editing = False
self.cursor.hide()
if self.blank:
self.text = ""
if self.on_editing_end:
self.on_editing_end(self)
if self.blank:
self.text = " "
def place(self):
super(Textbox, self).place()
if self.editing:
self.place_cursor()
def detach(self):
if self.cursor is not None:
self.cursor.detach()
self.cursor = None
self.interactor.DestroyTimer(self.blink_timer)
self.interactor.RemoveObserver(self.blink_observer)
self.interactor.RemoveObserver(self.keyboard_observer)
super(Textbox, self).detach()