This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpad.py
249 lines (189 loc) · 6.67 KB
/
pad.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
from receiver import receiver, handler
import gst
import goocanvas
import gobject
import controller
import view
import gtk
import utils
from point import Point
from link import Link
class PadBaseView(view.View, goocanvas.Group):
spacing = 3
__OUTLINE__ = "black"
__FOCUS__ = "red"
__HILIGHT__ = "black"
class Controller(controller.Controller):
__ARROW_COLOR__ = "black"
arrow = goocanvas.Polyline(
stroke_color = __ARROW_COLOR__)
__center = None
__prev_pads = []
def enter(self, item, target):
self._view.focus()
def leave(self, item, target):
self._view.unfocus()
def drag_start(self):
self._canvas.get_root_item().add_child(self.arrow)
self.__center = self.center(self._view.socket)
points = [self.__center, self.__center]
self._mousedown = Point(0, 0)
self.arrow.props.points = goocanvas.Points(points)
def drag_end(self):
self.arrow.remove()
for pad in self.__prev_pads:
pad.unhilight()
if pad.canLink(self._view):
pad.linkPads(self._view)
def __pads_under_point(self, (x, y)):
items = self._canvas.get_items_in_area(
goocanvas.Bounds(x - 1, y - 1, x + 1, y + 1), True, True, True)
if items:
return [item for item in items if isinstance(
item, PadBaseView)]
return []
def set_pos(self, obj, pos):
points = [self.__center, pos]
self.arrow.props.points = goocanvas.Points(points)
for pad in self.__prev_pads:
pad.unhilight()
self.__prev_pads = self.__pads_under_point(pos)
for pad in self.__prev_pads:
if pad.canLink(self._view):
pad.hilight()
def __init__(self, pad, element, selection):
self.pad = pad
self.element = element
self.selection = selection
goocanvas.Group.__init__(self)
view.View.__init__(self)
self.__createUi()
self.links = []
def __createUi(self):
self.text = goocanvas.Text(
font = "Sans 7",
parent = self,
text = self.name())
self.socket = goocanvas.Rect(
parent = self,
width = 10,
height = 10,
fill_color = self.__COLOR__,
stroke_color = self.__OUTLINE__)
twidth, theight = utils.get_text_dimensions(self.text)
self.width = self.spacing + self.socket.props.width + twidth
self.height = max(self.socket.props.height, theight)
if self.direction() == gst.PAD_SRC:
self.socket.props.x = self.width - self.socket.props.width
else:
self.text.props.x = self.socket.props.width + self.spacing
def name(self):
raise NotImplementedError
def direction(self):
raise NotImplementedError
def canLink(self, other):
raise NotImplementedError
def linkPads(self, other):
if self.direction() == gst.PAD_SRC:
link = Link(self, other, self.selection)
else:
link = Link(other, self, self.selection)
self.links.append(link)
other.links.append(link)
self.get_canvas().get_root_item().add_child(link)
self.updateLinks()
other.updateLinks()
def unlink(self, link):
self.links.remove(link)
def unlinkAll(self):
for link in self.links:
link.delete()
def updateLinks(self):
for link in self.links:
link.updateEndpoints()
def hilight(self):
self.socket.props.fill_color = self.__HILIGHT__
def unhilight(self):
self.socket.props.fill_color = self.__COLOR__
def focus(self):
self.socket.props.stroke_color = self.__FOCUS__
def unfocus(self):
self.socket.props.stroke_color = self.__OUTLINE__
class PadView(PadBaseView):
__COLOR__ = "yellow"
__BLOCKED__ = "dark yellow"
def __init__(self, *args, **kwargs):
PadBaseView.__init__(self, *args, **kwargs)
self.block()
def direction(self):
return self.pad.get_direction()
def name(self):
return self.pad.get_name()
def canLink(self, other):
return ((not self.pad.is_linked()) and isinstance(other, PadView) and
(not (self.pad is other.pad)) and
(self.pad.can_link(other.pad) or other.pad.can_link(self.pad))
)
def unlink(self, link):
PadBaseView.unlink(self, link)
self.block()
# yes, we wan't this to be a static list
blocked_pads = []
is_blocked = False
@classmethod
def unblock_all_pads(cls):
for pad in cls.blocked_pads:
pad.unblock()
def block(self):
self.pad.set_blocked_async(True, self.__block_cb)
def unblock(self):
self.pad.set_blocked_async(False, self.__block_cb)
def unhilight(self):
if self.is_blocked:
self.socket.props.fill_color_rgba = 0xAAAA00FF
else:
self.socket.props.fill_color = self.__COLOR__
def __finish_pad_blocking(self, blocked):
if blocked:
self.blocked_pads.append(self)
self.is_blocked = True
else:
self.blocked_pads.remove(self)
self.is_blocked = False
self.unhilight()
return False
def __block_cb(self, pad, state):
# this callback is called from pipeline context, and we need to do UI
# tasks in the main context
gobject.idle_add(self.__finish_pad_blocking, state)
class PadTemplateView(PadBaseView):
def name(self):
return self.pad.name_template
def direction(self):
return self.pad.direction
class RequestTemplateView(PadTemplateView):
__COLOR__ = "blue"
class Controller(controller.Controller):
def click(self, pos):
self._view.props.parent.element.get_pad(
self._view.pad.name_template)
def set_pos(self, obj, pos):
pass
def canLink(self, other):
return False
class SometimesTemplateView(PadTemplateView):
__COLOR__ = "green"
def canLink(self, other):
return False
def linkSrc(self, other):
pass
def linkSink(self, other):
pass
def make_pad_view(pad, element, selection):
if isinstance(pad, gst.Pad):
return PadView(pad, element, selection)
elif pad.presence == gst.PAD_REQUEST:
return RequestTemplateView(pad, element, selection)
elif pad.presence == gst.PAD_SOMETIMES:
return SometimesTemplateView(pad, element, selection)
return None