/
clutter-test.py
executable file
·371 lines (305 loc) · 10.6 KB
/
clutter-test.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
#!/usr/bin/env python
"""
ClutterScope - Clutter-based software digitial storage oscilloscope
Copyright (C) 2011 Leo Singer
"""
__author__ = "Leo Singer <leo.singer@ligo.org>"
# TODO: Add fade-in effect for graticule
# TODO: Add labels for traces showing name, color, scale, etc.
# TODO: Dragging to change trace offset should snap to horizontal or vertical
# TODO: Add data API
# TODO: Add triggering
import math
import numpy
import clutter
import gobject
from clutter import cogl
# Disable font mipmapping (see <http://bugzilla.clutter-project.org/show_bug.cgi?id=2584>)
clutter.set_font_flags(0)
class ClutterScope(clutter.Group):
"""A Clutter-powered digital storage oscilloscope."""
__gtype_name__ = 'ClutterScope'
SCROLL_TIMEOUT = 250
def __init__(self):
super(ClutterScope, self).__init__()
self.graticule = Graticule()
self.add(self.graticule)
self.set_reactive(True)
self.traces = []
layout = clutter.BoxLayout()
layout.set_vertical(False)
layout.set_use_animations(True)
layout.set_easing_duration(100)
layout.set_spacing(4)
label_box = clutter.Box(layout)
self.add(label_box)
label_box.set_position(0, 0)
#label_box.add_constraint(clutter.SnapConstraint(self, clutter.SNAP_EDGE_BOTTOM, clutter.SNAP_EDGE_BOTTOM, 0.))
label_box.add_constraint(clutter.BindConstraint(self, clutter.BIND_WIDTH, 0.))
# Add some traces (just for looks)
tr = Trace()
tr.set_position(0, -50)
self.graticule.add(tr)
self.traces += [tr]
label_box.add(TraceLabel(tr))
tr.set_name('H1:DMT-STRAIN')
tr = Trace()
tr.set_color(clutter.color_from_string('magenta'))
self.graticule.add(tr)
self.traces += [tr]
label_box.add(TraceLabel(tr))
tr.set_name('L1:DMT-STRAIN')
tr = Trace()
tr.set_color(clutter.color_from_string('yellow'))
tr.set_position(0, 50)
self.graticule.add(tr)
self.traces += [tr]
label_box.add(TraceLabel(tr))
tr.set_name('A1:DMT-STRAIN')
# State for event signal handlers
self.selected_trace = self.traces[0]
self.__last_scroll_time = 0
self.__drag_origin = None
# Hook up event signal handlers
self.connect_after('button-press-event', self.button_press)
self.connect_after('button-release-event', self.button_release)
self.connect_after('motion-event', self.motion)
self.connect_after('scroll-event', self.scroll)
def scroll(self, actor, event):
time = event.time
if time - self.__last_scroll_time > self.SCROLL_TIMEOUT:
direction = event.get_scroll_direction()
if direction == clutter.SCROLL_UP:
self.selected_trace.set_scale_level_y(self.selected_trace.get_scale_level_y() + 1)
elif direction == clutter.SCROLL_DOWN:
self.selected_trace.set_scale_level_y(self.selected_trace.get_scale_level_y() - 1)
elif direction == clutter.SCROLL_LEFT:
scale_level = self.selected_trace.get_scale_level_x() + 1
for trace in self.traces:
trace.set_scale_level_x(scale_level)
elif direction == clutter.SCROLL_RIGHT:
scale_level = self.selected_trace.get_scale_level_x() - 1
for trace in self.traces:
trace.set_scale_level_x(scale_level)
self.__last_scroll_time = time
def motion(self, actor, event):
if self.__drag_origin:
actor_origin, event_origin = self.__drag_origin
self.selected_trace.set_position(actor_origin[0] + event.x - event_origin[0], actor_origin[1] + event.y - event_origin[1])
def button_press(self, actor, event):
if event.get_button() == 1:
self.__drag_origin = (self.selected_trace.get_position(), (event.x, event.y))
def button_release(self, actor, event):
if event.get_button() == 1:
self.__drag_origin = None
def hline(y, x1, x2):
"""Draw a horizontal line from (x1, y) to (x2, y)."""
cogl.path_line(x1, y, x2, y)
def vline(x, y1, y2):
"""Draw a vertical line from (x, y1) to (x, y2)."""
cogl.path_line(x, y1, x, y2)
class Graticule(clutter.Group):
"""Actor that provides grid and axes for a ClutterScope.
This actor paints into an area that extends from half its width above its
top to half its width above its bottom, so that it is possible to conveniently
parent objects to it and have their coordinates referenced from (0, 0) refer
to the center of the stage."""
__gtype_name__ = 'Graticule'
"""Pixels between major gridlines"""
MAJOR_PIXELS = 60
"""Background color"""
BACKGROUND_COLOR = clutter.Color(40, 40, 40, 255)
"""Gridline color"""
GRIDLINE_COLOR = clutter.Color(60, 60, 60, 255)
def __init__(self):
super(Graticule, self).__init__()
self.__bind_constraint = None
# FIXME: replace allocation-changed handler with align constraints
self.__allocation_changed_handler = None
def do_paint(self):
"""paint signal handler."""
w = self.get_width()
h = self.get_height()
half_w = 0.5 * w
half_h = 0.5 * h
half_major = self.MAJOR_PIXELS / 2
tenth_major = self.MAJOR_PIXELS / 10
# Fill background.
cogl.set_source_color(self.BACKGROUND_COLOR)
cogl.rectangle(-half_w, -half_h, half_w, half_h)
# Create paths for vertical gridlines.
x0 = int((-half_w // self.MAJOR_PIXELS) * self.MAJOR_PIXELS)
for x in xrange(x0, int(math.ceil(half_w)), self.MAJOR_PIXELS):
halfway = x + half_major
vline(x, -half_h, half_h)
vline(halfway, -8, 0)
for xx in xrange(x + tenth_major, halfway, tenth_major):
vline(xx, -4, 0)
for xx in xrange(halfway + tenth_major, x + self.MAJOR_PIXELS, tenth_major):
vline(xx, -4, 0)
# Create paths for horizontal gridlines.
y0 = int((-half_h // self.MAJOR_PIXELS) * self.MAJOR_PIXELS)
for y in xrange(y0, int(math.ceil(half_h)), self.MAJOR_PIXELS):
halfway = y + half_major
hline(y, -half_w, half_w)
hline(halfway, 0, 8)
for yy in xrange(y + tenth_major, halfway, tenth_major):
hline(yy, 0, 4)
for yy in xrange(halfway + tenth_major, y + self.MAJOR_PIXELS, tenth_major):
hline(yy, 0, 4)
# Stroke gridlines.
cogl.set_source_color(self.GRIDLINE_COLOR)
cogl.path_stroke()
# Chain up to parent.
clutter.Group.do_paint(self)
def parent_allocation_changed(self, parent, box, flags):
"""parent's allocation-changed signal handler."""
parent_width = box.get_width()
parent_height = box.get_height()
self.set_position(0.5 * parent_width, 0.5 * parent_height)
def do_parent_set(self, old_parent):
"""parent-set signal handler."""
parent = self.get_parent()
if self.__bind_constraint:
self.remove_constraint(self.__bind_constraint)
if parent:
self.__bind_constraint = clutter.BindConstraint(parent, clutter.BIND_SIZE, 0.)
self.add_constraint(self.__bind_constraint)
else:
self.__bind_constraint = None
if self.__allocation_changed_handler:
if old_parent and self.__allocation_changed_handler:
old_parent.disconnect(self.__allocation_changed_handler)
if parent:
self.__allocation_changed_handler = parent.connect_after('allocation-changed', self.parent_allocation_changed)
else:
self.__allocation_changed_handler = None
if parent:
self.set_position(0.5 * parent.get_width(), 0.5 * parent.get_height())
class Trace(clutter.Actor):
__gtype_name__ = 'Trace'
__gproperties__ = {
'color': (
clutter.Color,
'color',
'Trace stroke color',
gobject.PARAM_READWRITE
),
'scale-level-x': (
gobject.TYPE_INT,
'scale-level-x',
'Scale level, x-axis',
gobject.G_MININT, gobject.G_MAXINT, 0,
gobject.PARAM_READWRITE
),
'scale-level-y': (
gobject.TYPE_INT,
'scale-level-y',
'Scale level, y-axis',
gobject.G_MININT, gobject.G_MAXINT, 0,
gobject.PARAM_READWRITE
)
}
def __init__(self):
super(Trace, self).__init__()
self.set_size(0, 0)
self.set_anchor_point_from_gravity(clutter.GRAVITY_CENTER)
self.color = clutter.color_from_string('cyan')
self.scale_level_x = 0
self.scale_level_y = 0
def do_set_property(self, prop, val):
if prop.name == 'color':
old_color = self.color
self.color = val
if old_color != self.color:
self.queue_redraw()
elif prop.name == 'scale-level-x':
self.scale_level_x = val
a, b = divmod(val, 2)
scale = 10 ** a
if b:
scale *= 2
self.animate(clutter.LINEAR, 100, 'scale-x', scale)
elif prop.name == 'scale-level-y':
self.scale_level_y = val
a, b = divmod(val, 2)
scale = 10 ** a
if b:
scale *= 2
self.animate(clutter.LINEAR, 100, 'scale-y', scale)
def do_get_property(self, prop):
if prop.name == 'color':
return self.color
elif prop.name == 'scale-level-x':
return self.scale_level_x
elif prop.name == 'scale-level-y':
return self.scale_level_y
def set_color(self, val):
self.set_property('color', val)
def get_color(self):
return self.get_property()
def set_scale_level_x(self, val):
self.set_property('scale-level-x', val)
def get_scale_level_x(self):
return self.get_property('scale-level-x')
def set_scale_level_y(self, val):
self.set_property('scale-level-y', val)
def get_scale_level_y(self):
return self.get_property('scale-level-y')
def do_paint(self):
x = numpy.arange(-400, 400)
y = 20 * numpy.sin(x * 0.1)
# Plot trace, setting down lines wherever both x and y are finite
# (neither NaN, nor infinity, nor minus infinity)
pendown = False
for x, y in zip(x, y):
if numpy.isfinite(x) and numpy.isfinite(y):
if pendown:
cogl.path_line_to(x, -y)
else:
cogl.path_move_to(x, -y)
pendown = True
else:
pendown = False
cogl.set_source_color(self.color)
cogl.path_stroke()
class TraceLabel(clutter.Group):
"""Label for a trace showing its name, color, and scale."""
__gtype_name__ = 'TraceLabel'
def __init__(self, trace):
super(TraceLabel, self).__init__()
self.trace = trace
self.set_size(144, 48)
self.name_label = clutter.Text()
self.name_label.set_color(clutter.color_from_string('black'))
self.name_label.set_text('foo bar')
self.add(self.name_label)
self.name_label.set_position(6, 6)
self.name_label.set_size(*self.get_size())
self.trace.connect_after('notify::color', self.color_changed)
self.trace.connect_after('notify::name', self.name_changed)
def color_changed(self, param, user_data):
self.queue_redraw()
def name_changed(self, param, user_data):
self.name_label.set_text(self.trace.get_name())
def do_paint(self):
"""paint signal handler."""
w, h = self.get_size()
color = self.trace.color
dark_color = color.darken()
cogl.set_source_color(dark_color)
cogl.path_round_rectangle(0, 0, w, h, 5, 10)
cogl.path_fill()
cogl.set_source_color(color)
cogl.path_round_rectangle(3, 3, w - 3, h - 3, 3, 10)
cogl.path_fill()
clutter.Group.do_paint(self)
stage = clutter.Stage()
stage.set_size(576, 576)
stage.connect('destroy', clutter.main_quit)
stage.set_user_resizable(True)
scope = ClutterScope()
stage.add(scope)
scope.add_constraint(clutter.BindConstraint(stage, clutter.BIND_SIZE | clutter.BIND_POSITION, 0.))
stage.show_all()
clutter.main()