~glitter-team/glitter/trunk

« back to all changes in this revision

Viewing changes to glitter/box.py

  • Committer: Jan Jokela
  • Date: 2009-04-03 13:54:00 UTC
  • mto: (38.2.2 trunk)
  • mto: This revision was merged to the branch mainline in revision 20.
  • Revision ID: jan@jan-laptop-20090403135400-w55mhbrkn77fbkcq
(glitter/box.py) Algorithm improvements

Show diffs side-by-side

added added

removed removed

Lines of Context:
132
132
        super(HBox, self)._update_layout()
133
133
        
134
134
        fixed_width_children = []
 
135
        # For fixed width children and layouting
 
136
        if len(self.packed_children > 0):
 
137
            reserved_width = self.spacing * (len(self.packed_children) - 1) * \
 
138
              self.get_widthu()
 
139
        else:
 
140
            reserved_width = 0.0
 
141
        
 
142
        # Discover fixed width children and update the reserved height
 
143
        print self.packed_children, reserved_width
 
144
        for child in self.packed_children:         
 
145
            if child.size_ratio > 0 or \
 
146
              (child.minimum_width > 0 and child.natural_width == 0):
 
147
                fixed_width_children.append(child)
 
148
                child._update_layout()
 
149
                # Increment reserved width by child height and horizontal 
 
150
                # offset
 
151
                reserved_width += child.get_widthu() + \
 
152
                  child.h_offset * child.get_widthu() * 2.0
 
153
        
 
154
        # Total width excluding reserved width        
 
155
        free_width = self.get_widthu() - reserved_width
 
156
        # Width for variable width children
 
157
        var_width_child_count = len(self.packed_children) - \
 
158
          len(fixed_width_children)
 
159
        var_child_width = free_width        
 
160
        if var_width_child_count > 0:
 
161
            var_child_width /= var_width_child_count        
 
162
        
 
163
        # Position all children
 
164
        current_x = 0.0
 
165
        for child in self.packed_children:
 
166
            current_x += child.get_widthu() * child.h_offset
 
167
            child.natural_x = current_x
 
168
            if child not in fixed_width_children:
 
169
                child.natural_width = var_child_width
 
170
            child._update_layout()
 
171
            current_x += child.get_widthu() * child.h_offset
 
172
            current_x += self.get_widthu() * self.spacing
 
173
        
 
174
        """fixed_width_children = []
135
175
        
136
176
        reserved_width = 0 # For fixed width children
137
177
        for child in self.packed_children:
138
 
            if child.size_ratio > 0:
 
178
            if child.size_ratio > 0 or \
 
179
               (child.minimum_width > 0 and child.natural_width == 0):
139
180
                fixed_width_children.append(child)
140
181
                child._update_layout()
141
182
                reserved_width += child.get_widthu()
142
 
   
 
183
     
143
184
        if self.get_widthu() > 0: # Account for zero width
144
185
            reserved_width = reserved_width * 1.0 / self.get_widthu()
145
186
        available_width = 1.0 - reserved_width
146
187
        current_x = 0.0
147
 
        max_height = 0
 
188
        max_height = self.minimum_height
148
189
        for child in self.packed_children:
149
190
            child.set_natural_x(current_x)
150
191
            if child not in fixed_width_children:
152
193
                  len(fixed_width_children))
153
194
                child.set_natural_width(child_width)
154
195
            child._update_layout()
155
 
            if child.get_height() > max_height:
156
 
                max_height = child.get_height()
 
196
            if child.get_height() + (child.v_offset * self.get_height() * 2) > max_height:
 
197
                max_height = child.get_height() + (child.v_offset * self.get_height() * 2)
157
198
            if self.get_widthu() > 0:
158
199
                current_x += child.get_widthu() * 1.0 / self.get_widthu()
159
200
            current_x += self.spacing  
160
201
            
161
202
        self.minimum_height = max_height     
162
 
                 
 
203
        
 
204
        super(HBox, self)._update_layout()      """   
163
205
                           
164
206
          
165
207
class VBox(Box):
178
220
        super(VBox, self)._update_style(props)
179
221
        
180
222
    def _update_layout(self):
181
 
        """ Updates layout """
 
223
        """ Updates layout 
 
224
        
 
225
        Fixed height children are flagged and will be offered the space they 
 
226
        need. Other children will share the remaining space.
 
227
        
 
228
        Children with minimum height and width->height size ratios qualify as 
 
229
        fixed height.
 
230
        """
182
231
        
183
232
        super(VBox, self)._update_layout()
184
233
        
185
234
        fixed_height_children = []
186
 
        
187
 
        reserved_height = 0 # For fixed height children
 
235
        # For fixed height children and layouting
 
236
        reserved_height = self.spacing * (len(self.packed_children) - 1) * \
 
237
          self.get_heightu()
 
238
        
 
239
        # Discover fixed height children and update the reserved height
 
240
        for child in self.packed_children:            
 
241
            if child.size_ratio < 0 or \
 
242
              (child.minimum_height > 0 and child.natural_height == 0):
 
243
                fixed_height_children.append(child)
 
244
                child._update_layout()
 
245
                # Increment reserved height by child height and vertical 
 
246
                # offset
 
247
                reserved_height += child.get_heightu() + \
 
248
                  child.v_offset * child.get_heightu() * 2.0
 
249
        
 
250
        # Total height excluding reserved height        
 
251
        free_height = self.get_heightu() - reserved_height
 
252
        # Height for variable height children
 
253
        var_height_child_count = len(self.packed_children) - \
 
254
          len(fixed_height_children)
 
255
        var_child_height = free_height        
 
256
        if var_height_child_count > 0:
 
257
            var_child_height /= var_height_child_count        
 
258
        
 
259
        # Position all children
 
260
        current_y = 0.0
 
261
        for child in self.packed_children:
 
262
            current_y += child.get_heightu() * child.v_offset
 
263
            child.natural_y = current_y
 
264
            if child not in fixed_height_children:
 
265
                child.natural_height = var_child_height
 
266
            child._update_layout()
 
267
            current_y += child.get_heightu() * child.v_offset
 
268
            current_y += self.get_heightu() * self.spacing
 
269
        
 
270
        """fixed_height_children = []
 
271
        
 
272
        reserved_height = 0.0 # For fixed height children
188
273
        for child in self.packed_children:
189
274
            if child.size_ratio < 0 or \
190
275
               (child.minimum_height > 0 and child.natural_height == 0):
191
276
                fixed_height_children.append(child)
192
277
                child._update_layout()
193
 
                reserved_height += child.get_heightu()
194
 
        
195
 
        self.minimum_height = reserved_height
196
 
        #print 'minimum height is ', self.minimum_height        
197
 
   
198
 
        if self.get_heightu() > 0:
199
 
            reserved_height = (reserved_height * 1.0 / self.get_heightu())
 
278
                reserved_height += child.get_heightu() + (child.v_offset * self.get_heightu() * 2.0)
 
279
            
 
280
        if self.get_heightu() > 0: # Account for zero width
 
281
            reserved_height = reserved_height * 1.0 / self.get_heightu()
200
282
        available_height = 1.0 - reserved_height
201
283
        current_y = 0.0
 
284
        max_width = self.minimum_width
202
285
        for child in self.packed_children:
203
286
            child.set_natural_y(current_y)
204
287
            if child not in fixed_height_children:
206
289
                  len(fixed_height_children))
207
290
                child.set_natural_height(child_height)
208
291
            child._update_layout()
 
292
            if child.get_width() + (child.h_offset * self.get_width() * 2) > max_width:
 
293
                max_width = child.get_width() + (child.h_offset * self.get_width() * 2)
209
294
            if self.get_heightu() > 0:
210
295
                current_y += child.get_heightu() * 1.0 / self.get_heightu()
211
 
            current_y += self.spacing    
 
296
            current_y += self.spacing 
 
297
 
 
298
        self.minimum_width = max_width     
 
299
        
 
300
        super(VBox, self)._update_layout()"""
212
301
            
213
302
    def get_item(self, i):
214
303
        """ Retrieve natural y for packed item i """