1
from ContainerTarget import ContainerTarget
2
from Layouter import Layouter, LayoutError
3
from utils.datatypes import *
4
from layout import Unit
11
# Class for arrays of TargetDisplays.
13
class TargetArray(ContainerTarget):
15
def __init__(self, name, parent):
17
# the current layouter function
20
# the class of the array elements
21
self.__elementtype = None
23
# the default settings of the array elements
24
self.__elementsettings = None
26
# children data of the array elements
27
self.__elementchildren = []
29
# the current elements of this array
32
# mapping between unique IDs and targets
35
ContainerTarget.__init__(self, name, parent)
36
self.__layout = gtk.Fixed()
39
self._register_property("length", TYPE_INT,
40
self._setp_length, self._getp_length)
41
self._register_property("layout", TYPE_LIST,
42
self._setp_layout, self._getp)
49
def get_widget(self): return self.__layout
54
# Returns next child index
56
def get_next_child_index(self): return len(self.__children)
60
def new_child(self, childtype, settings, children):
62
self.__elementtype = childtype
63
self.__elementsettings = settings
64
self.__elementchildren = children
66
# the array should be empty at the beginning but we need to create a
67
# child so the array can connect to sensor output
74
def __on_observe_children(self, src, cmd):
77
x, y, w, h = src.get_geometry()
78
self.__layout.move(src.get_widget(), x.as_px(), y.as_px())
81
if (cmd == src.OBS_GEOMETRY and self.__children):
87
# Adds an element to this array.
89
def __add_child(self):
92
settings = self.__elementsettings.copy()
93
settings["id"] += "#" + str(len(self.__children))
94
child = ContainerTarget.new_child(self, self.__elementtype,
96
self.__elementchildren)
97
self.__children.append(child)
99
x, y, w, h = child.get_geometry()
100
self.__layout.put(child.get_widget(), x.as_px(), y.as_px())
101
child.add_observer(self.__on_observe_children)
103
if (not self._is_geometry_locked()): child.unlock_geometry()
108
# Removes an element from this array.
110
def __remove_child(self):
111
assert(self.__children)
113
child = self.__children.pop()
114
self.__layout.remove(child.get_widget())
115
child.remove_observer(self.__on_observe_children)
117
self.get_layout_object().remove_child(child.get_layout_object())
119
self._unregister_child(child)
125
# Positions all elements of this array.
127
def __place_children(self):
129
if (not self.__layouter): return
130
parts = self.__layouter
135
layouter = Layouter(layout)
141
for child in self.__children:
142
x, y, w, h = child.get_geometry()
143
rel, cx, cy = layouter.layout(cnt, args)
144
cx = Unit.Unit(cx, Unit.UNIT_PX)
145
cy = Unit.Unit(cy, Unit.UNIT_PX)
147
cx, cy = child.get_anchored_coords(cx, cy, w, h)
150
child.set_position(cx, cy)
152
if (previous_id and rel):
153
child.set_prop("relative-to", (previous_id, rel))
155
previous_id = self.get_id_by_child(child)
158
#utils.request_call(self.adjust_geometry)
159
#self.adjust_geometry()
163
# FIXME: remove eventually
164
def distribute_sensor_output(self, sensor, indexes, key, value):
166
index = int(indexes.pop(0))
168
# add children, if necessary
169
if (index >= len(self.__children)):
170
while (index >= len(self.__children)): self.__add_child()
171
self.__place_children()
173
entries = self.__targets.get((sensor, index, key), [])
174
for target, prop in entries:
176
target.distribute_sensor_output(sensor, indexes[:],
179
target.set_xml_prop(prop, value)
186
def _setp_length(self, key, value):
189
if (len(self.__children) != length):
190
while (length > len(self.__children)): self.__add_child()
191
while (length < len(self.__children)): self.__remove_child()
192
self.__place_children()
196
def _getp_length(self, key):
198
return len(self.__children)
205
def _setp_layout(self, key, value):
207
self.__layouter = value
208
self.__place_children()
209
self._setp(key, value)