1
from layout import Unit
2
from utils import dialog
3
from utils.datatypes import *
4
from DisplayTarget import DisplayTarget
13
# Abstract class for targets that can contain others.
15
class ContainerTarget(DisplayTarget):
17
__slots__ = ('__children', '__children_id', '__ids', '__relatives')
19
def __init__(self, name, parent):
21
# the list is necessary for preserving order
24
# mapping: id -> child
25
self.__children_id = {}
27
# mapping: child -> id
30
# the relative-relations: id_of_relative -> [child]
33
DisplayTarget.__init__(self, name, parent)
38
# Observer callback for watching children.
40
def child_observer(self, src, cmd): pass
45
# Creates a new child of the given type.
47
def new_child(self, childtype, settings, children):
52
child = targetregistry.create(childtype, self)
53
only_one = targetregistry.one_child(self.get_name())
55
if (only_one and len(self.__children) > 0):
56
log("Warning: The %s container accepts only one child."
59
self._register_child(child, cid)
60
child.get_widget().show()
62
for t, s, c in children:
63
child.new_child(t, s, c)
65
for key, value in settings.items():
66
child.set_xml_prop(key, value)
72
def _register_child(self, child, cid):
74
self.__children_id[cid] = child
75
self.__ids[child] = cid
76
self.__children.append(child)
77
self._get_display().add_target_to_script(cid, child)
81
def _unregister_child(self, child):
83
cid = self.__ids[child]
86
del self.__children_id[cid]
95
self.__children.remove(child)
99
def _get_children(self): return self.__children[:]
103
def _get_child(self):
105
if (not self.__children): return
106
else: return self.__children[0]
110
def get_child_by_id(self, ident): return self.__children_id.get(ident)
111
def get_id_by_child(self, child): return self.__ids.get(child)
117
for c in self._get_children():
119
self._unregister_child(c)
122
self.__children_id.clear()
124
DisplayTarget.delete(self)
129
# Override this method if the container size differs from the target size.
131
def get_container_geometry(self):
133
return self.get_geometry()
138
# Returns the border size of this container. Override this method to return
139
# the sizes of the four borders (left, top, right, bottom).
141
def get_border_size(self):
143
return (Unit.ZERO, Unit.ZERO, Unit.ZERO, Unit.ZERO)
148
# Containers which set index values for the children override this method.
150
def get_next_child_index(self): return -1
154
def detect_leave(self, stamp):
156
if (not self._is_active()): return
157
DisplayTarget.detect_leave(self, stamp)
158
for c in self.__children:
159
c.detect_leave(stamp)
163
def unlock_geometry(self):
165
self._geometry_lock = False
166
for c in self._get_children(): c.unlock_geometry()