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 pathelement.py
155 lines (124 loc) · 4.34 KB
/
element.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
import gobject
import goocanvas
import controller
import view
import selectable
import gtk
import gst
import utils
import weakref
from pad import make_pad_view
from receiver import receiver, handler
class ElementView(selectable.Selectable, goocanvas.Group):
__NORMAL__ = 0x709fb899
__SELECTED__ = 0xa6cee3AA
padding = 2
def __init__(self, element, selection):
goocanvas.Group.__init__(self)
selectable.Selectable.__init__(self, selection)
self.register_instance(self)
self.element = element
self.bg = goocanvas.Rect(
parent = self,
fill_color = "grey",
radius_x = 5,
radius_y = 5)
self.text = goocanvas.Text(
parent = self,
font = "Sans 8 Bold",
text = element.get_name())
self.__sourcePads = {}
self.__sinkPads = {}
self.__links = {}
for pad in element.get_pad_template_list():
if pad.presence != gst.PAD_ALWAYS:
self.__addPad(pad)
for pad in element.pads():
self.__addPad(pad)
## public api
def joinLink(self, pad, linkObj):
self.__links[pad] = pad.direction()
def breakLink(self, pad):
del self.__links[pad]
def set_simple_transform(self, x, y, scale, rotation):
goocanvas.Group.set_simple_transform(self, x, y, scale, rotation)
for pad in self.__sourcePads.values():
pad.updateLinks()
for pad in self.__sinkPads.values():
pad.updateLinks()
## Selectable Methods methods
def select(self):
self.bg.props.stroke_color = "red"
def deselect(self):
self.bg.props.stroke_color = "black"
def delete(self):
p = self.element.get_parent()
if p:
p.remove(self.element)
element = receiver()
## element signal handlers
@handler(element, "pad-added")
def __padAdded(self, element, pad):
# this handler is called in pipeline context, and we need to add the
# pad in the UI context
print pad.get_name(), pad.get_direction(), pad
gobject.idle_add(self.__addPad, pad)
@handler(element, "pad-removed")
def __padRemoved(self, element, pad):
print pad.get_name(), pad.get_direction(), pad
# this handler is called in pipeline context, and we need to add the
# pad in the UI context
gobject.idle_add(self.__removePad, pad)
## implementation functions
def __sizepads(self, pads):
width = 0
height = 0
for pad, widget in pads.iteritems():
height += widget.height
width = max(width, widget.width)
return width, height
def __pospads(self, pads, x, y):
for widget in pads.itervalues():
widget.set_simple_transform(x, y, 1.0, 0)
y += widget.height
def __update(self):
twidth, theight = utils.get_text_dimensions(self.text)
lwidth, lheight = self.__sizepads(self.__sinkPads)
rwidth, rheight = self.__sizepads(self.__sourcePads)
width = max(twidth, lwidth + rwidth)
height = theight + max(lheight, rheight) + 5
self.bg.props.width = width
self.bg.props.height = height
self.__pospads(self.__sinkPads, 0, theight)
self.__pospads(self.__sourcePads, width - rwidth, theight)
def __addPad(self, pad):
child = make_pad_view(pad, self, self.selection)
if child.direction() == gst.PAD_SRC:
self.__sourcePads[pad] = child
else:
self.__sinkPads[pad] = child
self.add_child(child)
self.__update()
def __removePad(self, pad):
if pad.get_direction() == gst.PAD_SRC:
widget = self.__sourcePads[pad]
del self.__sourcePads[pad]
else:
widget = self.__sinkPads[pad]
del self.__sinkPads[pad]
widget.unlinkAll()
widget.remove()
self.__update()
## class methods
__instances__ = []
@classmethod
def register_instance(cls, instance):
cls.__instances__.append(weakref.proxy(instance,
cls.unregister_instance))
@classmethod
def unregister_instance(cls, proxy):
self.__instances__.remove(proxy)
@classmethod
def set_all_to_null(cls):
for instance in cls.__instances__:
instance.element.set_state(gst.STATE_NULL)