This repository has been archived by the owner on Sep 17, 2019. It is now read-only.
/
plumber.py
545 lines (425 loc) · 18.2 KB
/
plumber.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#!/usr/bin/env python
from __future__ import division
import sys
import math
import cairo
from gi.repository import Gtk, Gdk, GObject
import components
UI_FILE = 'gui.xml'
ID_MAIN_WINDOW = 'main_window'
ID_TOOLBAR_BUTTON = 'toolbar_'
ID_COMPONENT_PALETTE = 'component_palette'
ID_CANVAS = 'canvas'
class PlumberPart(object):
def __init__(self, app):
self.app = app
# This class needs to be subclassed
def init_ui(self):
raise NotImplementedError('Implement this!')
class Toolbar(PlumberPart):
BUTTONS = ('save', 'open', 'edit', 'delete', 'undo', 'redo', 'play',
'stop', 'help',)
def init_ui(self):
for name in Toolbar.BUTTONS:
button = self.app.builder.get_object(ID_TOOLBAR_BUTTON + name)
button.connect('clicked', getattr(self, 'do_' + name))
def do_save(self, button):
dialog = Gtk.FileChooserDialog(
'Save...',
self.app.builder.get_object(ID_MAIN_WINDOW),
Gtk.FileChooserAction.SAVE,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK))
if dialog.run() == Gtk.ResponseType.OK:
with open(dialog.get_filename(), 'w') as f:
functions = {}
f.write('#!/bin/bash\n')
n_funcs = 0
n_fifos = 0
for component_box in self.app.builder.get_object(ID_CANVAS):
component = component_box.get_children()[0].component
fname = 'component_{}()'.format(n_funcs)
n_funcs += 1
f.write(component.get_function(fname))
f.write('\n')
out_fifos = []
for i in range(len(component.output_pipes)):
fifo_name = '/tmp/plumber_{}'.format(n_fifos)
n_fifos += 1
out_fifos.append(fifo_name)
f.write('\nmkfifo {}\n'.format(fifo_name))
functions[component] = (fname, out_fifos, out_fifos[:])
for component, (fname, fifos1, fifos2) in functions.items():
f.write(fname[:-2])
for in_pipe in component.input_pipes:
fifo = functions[in_pipe.start][1].pop(0)
f.write(' ' + fifo)
for fifo in fifos2:
f.write(' ' + fifo)
f.write(' &\n')
f.write('wait\n')
for i in range(n_fifos):
f.write('rm /tmp/plumber_{}\n'.format(i))
dialog.destroy()
def do_open(self, button):
print('OPEN')
def do_edit(self, button):
print('EDIT')
def do_delete(self, button):
print('DELETE')
def do_undo(self, button):
print('UNDO')
def do_redo(self, button):
print('REDO')
def do_play(self, button):
print('PLAY')
def do_stop(self, button):
print('STOP')
def do_help(self, button):
print('HELP')
class ComponentDrawer(Gtk.DrawingArea):
ICON_WIDTH = 50
ICON_HEIGHT = 50
CANVAS_WIDTH = 125
CANVAS_HEIGHT = 55
FONT_FACE = 'Sans'
FONT_SIZE = 15
BASE_MARGIN = 10
BASE_CORNER_RADIUS = 6
BASE_LINE_WIDTH = 2
BASE_STROKE_COLOR = (0, 0, 0)
BASE_FILL_COLOR = (135 / 255, 206 / 255, 250 / 255)
BASE_FILL_COLOR_SELECTED = (255 / 255, 215 / 255, 0 / 255)
PORT_RADIUS = 4
PORT_LINE_WIDTH = 1
PORT_STROKE_COLOR = (0, 0, 0)
PORT_IN_COLOR = (34 / 255, 139 / 255, 34 / 255)
PORT_OUT_COLOR = (255 / 255, 140 / 255, 0 / 255)
def __init__(self, builder, component, is_icon):
super(ComponentDrawer, self).__init__()
self.set_has_window(False)
self.builder = builder
self.component = component
self.is_icon = is_icon
self.is_drag = False
self.is_selected = False
self.connect('draw', self.do_draw)
if self.is_icon:
self.set_size_request(self.ICON_WIDTH, self.ICON_HEIGHT)
else:
self.set_size_request(self.CANVAS_WIDTH, self.CANVAS_HEIGHT)
def do_draw(self, *args):
if len(args) == 1:
ctx = args[0]
else:
ctx = args[1]
width = self.get_allocated_width()
height = self.get_allocated_height()
self.draw_component(ctx, width, height)
def draw_component(self, ctx, width, height):
self.draw_base(ctx, width, height)
self.draw_inputs(ctx, width, height)
self.draw_outputs(ctx, width, height)
if not self.is_icon:
self.draw_name(ctx, width, height)
def draw_base(self, ctx, width, height):
x1 = y1 = self.BASE_MARGIN
x2 = width - self.BASE_MARGIN
y2 = height - self.BASE_MARGIN
r = self.BASE_CORNER_RADIUS
# From http://cairographics.org/cookbook/roundedrectangles Method D
ctx.arc(x1 + r, y1 + r, r, 2 * (math.pi / 2), 3 * (math.pi / 2))
ctx.arc(x2 - r, y1 + r, r, 3 * (math.pi / 2), 4 * (math.pi / 2))
ctx.arc(x2 - r, y2 - r, r, 0, math.pi / 2)
ctx.arc(x1 + r, y2 - r, r, math.pi / 2, 2 * (math.pi / 2))
ctx.close_path()
ctx.set_line_width(self.BASE_LINE_WIDTH)
ctx.set_source_rgb(*self.BASE_STROKE_COLOR)
ctx.stroke_preserve()
if self.is_selected:
ctx.set_source_rgb(*self.BASE_FILL_COLOR_SELECTED)
else:
ctx.set_source_rgb(*self.BASE_FILL_COLOR)
ctx.fill()
def draw_inputs(self, ctx, width, height):
if self.component.inputs == 0:
return
self.draw_ports(ctx, width, height, self.BASE_MARGIN,
self.component.inputs, self.PORT_IN_COLOR)
def draw_outputs(self, ctx, width, height):
if self.component.outputs == 0:
return
self.draw_ports(ctx, width, height, width - self.BASE_MARGIN,
self.component.outputs, self.PORT_OUT_COLOR)
@classmethod
def draw_ports(cls, ctx, width, height, x, count, color):
available_space = (height - cls.BASE_MARGIN * 2)
offset = available_space / (count + 1)
y = cls.BASE_MARGIN + offset
ctx.set_line_width(cls.PORT_LINE_WIDTH)
for c in range(count):
ctx.arc(x, y, cls.PORT_RADIUS, 0, 360)
ctx.set_source_rgb(0, 0, 0)
ctx.close_path()
ctx.stroke_preserve()
ctx.set_source_rgb(*color)
ctx.fill()
y += offset
def draw_name(self, ctx, width, height):
ctx.select_font_face(self.FONT_FACE)
ctx.set_font_size(self.FONT_SIZE)
ctx.move_to(self.BASE_MARGIN * 2, height - self.BASE_MARGIN * 2)
ctx.set_source_rgb(0, 0, 0)
ctx.show_text(self.component.name)
ctx.stroke()
class ComponentPalette(PlumberPart):
def init_ui(self):
self.categories = {}
pane = self.app.builder.get_object(ID_COMPONENT_PALETTE)
for component in self.app.components.values():
button = Gtk.ToolButton.new(None, component.name)
button.set_icon_widget(ComponentDrawer(self.app.builder, component,
True))
button.set_use_drag_window(True)
button.drag_source_set(Gdk.ModifierType.BUTTON1_MASK, None, Gdk.DragAction.COPY)
button.drag_source_add_text_targets()
button.connect('drag-data-get', self.do_data_get, component.name)
try:
group = self.categories[component.category]
except KeyError:
group = Gtk.ToolItemGroup(label=component.category)
self.categories[component.category] = group
pane.add(group)
group.add(button)
def do_data_get(self, button, context, data, info, time, name):
data.set_text(name, len(name))
class PipeDrawer(object):
PIPE_WIDTH = 6
PIPE_COLOR = (0, 0, 0)
def __init__(self, start_box, end_box):
self.start_box = start_box
self.end_box = end_box
self.start = start_box.get_children()[0].component
self.end = end_box.get_children()[0].component
try:
self.start.attach_output(self)
self.end.attach_input(self)
except components.FullPipeError:
self.detach()
raise
def detach(self):
try:
self.start.detach_output(self)
self.end.detach_input(self)
except ValueError:
pass
def do_draw(self, canvas, ctx):
ctx.set_line_width(self.PIPE_WIDTH)
ctx.set_source_rgb(*self.PIPE_COLOR)
ctx.set_line_join(cairo.LINE_JOIN_ROUND)
ctx.set_line_cap(cairo.LINE_CAP_ROUND)
value = GObject.Value()
value.init(GObject.TYPE_INT)
available_space = (ComponentDrawer.CANVAS_HEIGHT
- ComponentDrawer.BASE_MARGIN * 2)
start_n = self.start.output_pipes.index(self) + 1
end_n = self.end.input_pipes.index(self) + 1
start_offset = available_space / (self.start.outputs + 1) * start_n
end_offset = available_space / (self.end.inputs + 1) * end_n
canvas.child_get_property(self.start_box, 'x', value)
start_x = value.get_int() + ComponentDrawer.CANVAS_WIDTH - ComponentDrawer.BASE_MARGIN
canvas.child_get_property(self.start_box, 'y', value)
start_y = value.get_int() + start_offset + ComponentDrawer.BASE_MARGIN
canvas.child_get_property(self.end_box, 'x', value)
end_x = value.get_int() + ComponentDrawer.BASE_MARGIN
canvas.child_get_property(self.end_box, 'y', value)
end_y = value.get_int() + end_offset + ComponentDrawer.BASE_MARGIN
ctx.move_to(start_x, start_y)
ctx.line_to(start_x + ((end_x - start_x) / 2), start_y)
ctx.line_to(start_x + ((end_x - start_x) / 2), end_y)
ctx.line_to(end_x, end_y)
ctx.stroke()
class Canvas(PlumberPart):
GRID_SPACING = 30
GRID_LENGTH = 3
GRID_WIDTH = 0.1
def init_ui(self):
self.pipes = []
self.drag_component = None
self.add_pipe_component = None
self.remove_pipe_component = None
canvas = self.app.builder.get_object(ID_CANVAS)
canvas.add_events(Gdk.EventMask.POINTER_MOTION_HINT_MASK
| Gdk.EventMask.BUTTON_MOTION_MASK
| Gdk.EventMask.BUTTON_PRESS_MASK
| Gdk.EventMask.BUTTON_RELEASE_MASK)
canvas.set_reallocate_redraws(True)
canvas.drag_dest_set(Gtk.DestDefaults.MOTION | Gtk.DestDefaults.DROP,
None, Gdk.DragAction.COPY)
canvas.drag_dest_add_text_targets()
canvas.connect('draw', self.do_draw)
canvas.connect('motion-notify-event', self.do_motion)
canvas.connect('drag-data-received', self.do_drag_received)
def do_child_press(self, component_box, event):
component_drawer = component_box.get_children()[0]
component = component_drawer.component
if event.button == 1 and event.type == Gdk.EventType.BUTTON_PRESS:
self.drag_component = component_box
else:
self.drag_component = None
if event.button == 1 and event.type == Gdk.EventType._2BUTTON_PRESS:
self.show_properties(component)
if event.button == 3 and event.type == Gdk.EventType.BUTTON_PRESS:
if self.add_pipe_component is component_box:
self.add_pipe_component = None
component_drawer.is_selected = False
elif self.add_pipe_component:
self.add_pipe(self.add_pipe_component, component_box)
self.add_pipe_component.get_children()[0].is_selected = False
self.add_pipe_component = None
component_drawer.is_selected = False
else:
self.add_pipe_component = component_box
component_drawer.is_selected = True
elif self.add_pipe_component:
self.add_pipe_component.get_children()[0].is_selected = False
self.add_pipe_component = None
if event.button == 2 and event.type == Gdk.EventType.BUTTON_PRESS:
if self.remove_pipe_component is component_box:
self.remove_pipe_component = None
component_drawer.is_selected = False
elif self.remove_pipe_component:
self.remove_pipe(self.remove_pipe_component, component_box)
self.remove_pipe_component.get_children()[0].is_selected = False
self.remove_pipe_component = None
component_drawer.is_selected = False
else:
self.remove_pipe_component = component_box
component_drawer.is_selected = True
elif self.remove_pipe_component:
self.remove_pipe_component.get_children()[0].is_selected = False
self.remove_pipe_component = None
component_box.get_window().invalidate_rect(None, True)
def do_child_release(self, component_box, event):
component = component_box.get_children()[0].component
self.drag_component = None
def do_motion(self, canvas, event):
if self.drag_component:
value = GObject.Value()
value.init(GObject.TYPE_INT)
canvas.child_get_property(self.drag_component, 'x', value)
value.set_int(value.get_int() + event.x
- ComponentDrawer.CANVAS_WIDTH / 2)
canvas.child_set_property(self.drag_component, 'x', value)
canvas.child_get_property(self.drag_component, 'y', value)
value.set_int(value.get_int() + event.y
- ComponentDrawer.CANVAS_HEIGHT / 2)
canvas.child_set_property(self.drag_component, 'y', value)
def show_properties(self, component):
if component.properties_dialog is None:
return
dialog = Gtk.Dialog(component.name + ' Properties',
self.app.builder.get_object(ID_MAIN_WINDOW),
Gtk.DialogFlags.MODAL
| Gtk.DialogFlags.DESTROY_WITH_PARENT,
(Gtk.STOCK_OK, Gtk.ResponseType.OK))
dbuilder = Gtk.Builder()
dbuilder.add_from_string(component.properties_dialog)
content_area = dialog.get_content_area()
content_area.pack_start(dbuilder.get_object('properties_box'),
False, False, 0)
dbuilder.connect_signals(component)
component.init_properties(dbuilder)
content_area.show_all()
response = dialog.run()
print('Response:', response)
dialog.destroy()
def add_pipe(self, start_component_box, end_component_box):
try:
self.pipes.append(PipeDrawer(start_component_box, end_component_box))
except components.FullPipeError:
pass
def remove_pipe(self, start_box, end_box):
for pipe in self.pipes:
if pipe.start_box == start_box and pipe.end_box == end_box:
pipe.detach()
self.pipes.remove(pipe)
return
def find_pipe(self, start, end):
for pipe in self.pipes:
if pipe.start is start and pipe.end is end:
return pipe
return None
def do_drag_received(self, canvas, context, x, y, data, info, time):
component = self.app.components[data.get_text()]
Gtk.drag_finish(context, True, False, time)
event_box = Gtk.EventBox()
event_box.set_visible_window(False)
event_box.add_events(Gdk.EventMask.POINTER_MOTION_HINT_MASK
| Gdk.EventMask.BUTTON_MOTION_MASK
| Gdk.EventMask.BUTTON_PRESS_MASK
| Gdk.EventMask.BUTTON_RELEASE_MASK)
event_box.connect('button-press-event', self.do_child_press)
event_box.connect('button-release-event', self.do_child_release)
drawer = ComponentDrawer(self.app.builder, component(), False)
event_box.add(drawer)
drawer.set_visible(True)
event_box.set_visible(True)
canvas.put(event_box,
x - ComponentDrawer.CANVAS_WIDTH / 2,
y - ComponentDrawer.CANVAS_HEIGHT/ 2)
canvas.get_window().invalidate_rect(None, True)
def do_draw(self, canvas, ctx):
width = canvas.get_allocated_width()
height = canvas.get_allocated_height()
self.draw_background(ctx, width, height)
self.draw_grid(ctx, width, height)
for pipe in self.pipes:
ctx.save()
pipe.do_draw(canvas, ctx)
ctx.restore()
def draw_background(self, ctx, width, height):
ctx.save()
ctx.set_source_rgb(255, 255, 255)
ctx.rectangle(0, 0, width, height)
ctx.fill()
ctx.restore()
def draw_grid(self, ctx, width, height):
ctx.save()
ctx.set_line_width(self.GRID_WIDTH)
ctx.set_source_rgb(0, 0, 0)
ctx.set_dash((self.GRID_LENGTH,))
for x in range(0, width, self.GRID_SPACING):
ctx.move_to(x, 0)
ctx.line_to(x, height)
for y in range(0, height, self.GRID_SPACING):
ctx.move_to(0, y)
ctx.line_to(width, y)
ctx.stroke()
ctx.restore()
class Plumber(object):
def __init__(self):
self.builder = Gtk.Builder()
self.builder.add_from_file(UI_FILE)
self.components = {}
for c in components.ACTIVE_COMPONENTS:
self.components[c.name] = c
self.init_ui()
def start(self):
self.builder.get_object(ID_MAIN_WINDOW).show_all()
def init_ui(self):
'Wires the UI XML description to the actual implementing code.'
main_window = self.builder.get_object(ID_MAIN_WINDOW)
main_window.connect('destroy', Gtk.main_quit)
self.toolbar = Toolbar(self)
self.toolbar.init_ui()
self.component_palette = ComponentPalette(self)
self.component_palette.init_ui()
self.canvas = Canvas(self)
self.canvas.init_ui()
def main(argv):
p = Plumber()
p.start()
Gtk.main()
if __name__ == '__main__':
sys.exit(main(sys.argv))