~gdesklets-desklet-team/gdesklets/0.36

« back to all changes in this revision

Viewing changes to display/TargetArray.py

  • Committer: Robert Pastierovic
  • Date: 2007-10-07 10:08:42 UTC
  • Revision ID: pastierovic@gmail.com-20071007100842-fdvp2vzmqgh1j87k
merged 0.3x branch and basic documentation and some other changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from ContainerTarget import ContainerTarget
 
2
from Layouter import Layouter, LayoutError
 
3
from utils.datatypes import *
 
4
from layout import Unit
 
5
import utils
 
6
 
 
7
import gtk
 
8
 
 
9
 
 
10
#
 
11
# Class for arrays of TargetDisplays.
 
12
#
 
13
class TargetArray(ContainerTarget):
 
14
 
 
15
    def __init__(self, name, parent):
 
16
 
 
17
        # the current layouter function
 
18
        self.__layouter = []
 
19
 
 
20
        # the class of the array elements
 
21
        self.__elementtype = None
 
22
 
 
23
        # the default settings of the array elements
 
24
        self.__elementsettings = None
 
25
 
 
26
        # children data of the array elements
 
27
        self.__elementchildren = []
 
28
 
 
29
        # the current elements of this array
 
30
        self.__children = []
 
31
 
 
32
        # mapping between unique IDs and targets
 
33
        self.__targets = {}
 
34
 
 
35
        ContainerTarget.__init__(self, name, parent)
 
36
        self.__layout = gtk.Fixed()
 
37
        self.__layout.show()
 
38
 
 
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)
 
43
 
 
44
 
 
45
 
 
46
    #
 
47
    # Returns the widget
 
48
    #
 
49
    def get_widget(self): return self.__layout
 
50
 
 
51
 
 
52
 
 
53
    #
 
54
    # Returns next child index
 
55
    #
 
56
    def get_next_child_index(self): return len(self.__children)
 
57
 
 
58
 
 
59
 
 
60
    def new_child(self, childtype, settings, children):
 
61
 
 
62
        self.__elementtype = childtype
 
63
        self.__elementsettings = settings
 
64
        self.__elementchildren = children
 
65
 
 
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
 
68
        # remove eventually
 
69
        self.__add_child()
 
70
        self.__remove_child()
 
71
 
 
72
 
 
73
 
 
74
    def __on_observe_children(self, src, cmd):
 
75
 
 
76
        def f():
 
77
            x, y, w, h = src.get_geometry()
 
78
            self.__layout.move(src.get_widget(), x.as_px(), y.as_px())
 
79
 
 
80
 
 
81
        if (cmd == src.OBS_GEOMETRY and self.__children):
 
82
            utils.request_call(f)
 
83
 
 
84
 
 
85
 
 
86
    #
 
87
    # Adds an element to this array.
 
88
    #
 
89
    def __add_child(self):
 
90
 
 
91
        # use a unique ID
 
92
        settings = self.__elementsettings.copy()
 
93
        settings["id"] += "#" + str(len(self.__children))
 
94
        child = ContainerTarget.new_child(self, self.__elementtype,
 
95
                                          settings,
 
96
                                          self.__elementchildren)
 
97
        self.__children.append(child)
 
98
 
 
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)
 
102
 
 
103
        if (not self._is_geometry_locked()): child.unlock_geometry()
 
104
 
 
105
 
 
106
 
 
107
    #
 
108
    # Removes an element from this array.
 
109
    #
 
110
    def __remove_child(self):
 
111
        assert(self.__children)
 
112
 
 
113
        child = self.__children.pop()
 
114
        self.__layout.remove(child.get_widget())
 
115
        child.remove_observer(self.__on_observe_children)
 
116
 
 
117
        self.get_layout_object().remove_child(child.get_layout_object())
 
118
 
 
119
        self._unregister_child(child)
 
120
        child.delete()
 
121
 
 
122
 
 
123
 
 
124
    #
 
125
    # Positions all elements of this array.
 
126
    #
 
127
    def __place_children(self):
 
128
 
 
129
        if (not self.__layouter): return
 
130
        parts = self.__layouter
 
131
        layout = parts[0]
 
132
        args = parts[1:]
 
133
 
 
134
        try:
 
135
            layouter = Layouter(layout)
 
136
        except LayoutError:
 
137
            return
 
138
 
 
139
        cnt = 0
 
140
        previous_id = ""
 
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)
 
146
 
 
147
            cx, cy = child.get_anchored_coords(cx, cy, w, h)
 
148
 
 
149
            if (not rel):
 
150
                child.set_position(cx, cy)
 
151
 
 
152
            if (previous_id and rel):
 
153
                child.set_prop("relative-to", (previous_id, rel))
 
154
 
 
155
            previous_id = self.get_id_by_child(child)
 
156
            cnt += 1
 
157
 
 
158
        #utils.request_call(self.adjust_geometry)
 
159
        #self.adjust_geometry()
 
160
 
 
161
 
 
162
 
 
163
    # FIXME: remove eventually
 
164
    def distribute_sensor_output(self, sensor, indexes, key, value):
 
165
 
 
166
        index = int(indexes.pop(0))
 
167
 
 
168
        # add children, if necessary
 
169
        if (index >= len(self.__children)):
 
170
            while (index >= len(self.__children)): self.__add_child()
 
171
            self.__place_children()
 
172
 
 
173
        entries = self.__targets.get((sensor, index, key), [])
 
174
        for target, prop in entries:
 
175
            if (indexes):
 
176
                target.distribute_sensor_output(sensor, indexes[:],
 
177
                                                key, value)
 
178
            else:
 
179
                target.set_xml_prop(prop, value)
 
180
 
 
181
 
 
182
 
 
183
    #
 
184
    # "length" property.
 
185
    #
 
186
    def _setp_length(self, key, value):
 
187
 
 
188
        length = int(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()
 
193
 
 
194
 
 
195
 
 
196
    def _getp_length(self, key):
 
197
 
 
198
        return len(self.__children)
 
199
 
 
200
 
 
201
 
 
202
    #
 
203
    # "layout" property.
 
204
    #
 
205
    def _setp_layout(self, key, value):
 
206
 
 
207
        self.__layouter = value
 
208
        self.__place_children()
 
209
        self._setp(key, value)
 
210