1 2 3 4 5 6 7 8 9 10 11 12 13 14 | def do_size_request(self, oreq): reqMgrs = (req.Manager(), req.Manager()) # (X,Y) for child in self.gChildren: request = child.widget.size_request() # compute! for xyi in (X, Y): be = child.lrtb[xyi] glue1d = child.glue.xy[xyi] sz = request[xyi] + glue1d.base() rr = req.RangeSize(be, sz) reqMgrs[xyi].addRangeReq(rr) self.reqs = map(lambda m: m.solve(), reqMgrs) # (X,Y) bw2 = 2 * self.border_width oreq.width = min(sum(self.reqs[X]) + bw2, self.maxsize[0]) oreq.height = min(sum(self.reqs[Y]) + bw2, self.maxsize[1]) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | def do_size_allocate(self, allocation): self.allocation = allocation allocs = (allocation.width, allocation.height) alloc_offsets = (self.border_width, self.border_width) self.crSizes = [None, None] # 2-lists: columns size, rows sizes. self.offsets = [None, None] # alloc_offsets + partial sums of crSizes for xyi in (X, Y): a = allocs[xyi] reqs = self.reqs[xyi] gWeights = self._getGrowWeights(xyi) self.crSizes[xyi] = given = self._divide(allocs[xyi], reqs, gWeights) offsets = len(given) * [None] offsets[0] = alloc_offsets[xyi] for oi in range(1, len(offsets)): offsets[oi] = offsets[oi - 1] + given[oi - 1] self.offsets[xyi] = offsets for child in self.gChildren: self._allocate_child(child) if self.flags() & GTK.REALIZED: self.window.move_resize(*allocation) |
1 2 3 4 5 6 7 8 9 10 11 12 | def _getGrowWeights(self, xyi): wMgr = req.Manager() for child in self.gChildren: be = child.lrtb[xyi] glue1d = child.glue.xy[xyi] rr = req.RangeSize(be, glue1d.total_grow_weight()) wMgr.addRangeReq(rr) wMgr.solve() gws = wMgr.reqs if sum(gws) == 0: gws = len(gws) * [1] # if zero weights then equalize return gws |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | def _divide(self, cake, requirements, growWeights): n = len(requirements) # == len(growWeights) given = requirements[:] # start with exact satisfaction reqTotal = sum(requirements) delta = cake - reqTotal if delta < 0: # rarely, "invert" weights growWeights = map(lambda x: max(growWeights) - x, growWeights) if sum(growWeights) == 0: growWeights = n * [1]; # equalize i = 0 gwTotal = sum(growWeights) while gwTotal > 0 and delta != 0: add = (delta * growWeights + gwTotal/2) / gwTotal gwTotal -= growWeights given += add delta -= add i += 1 return given |
1 2 3 4 5 6 7 8 9 10 11 12 13 | def _allocate_child(self, child): offsetsxy = [None, None] req = list( child.widget.get_child_requisition() ) # pre-calculated for xyi in (X, Y): segRange = child.lrtb[xyi] g1d = child.glue.xy[xyi] supply = sum( self.crSizes[xyi][segRange[0]: segRange[1]] ) (oadd, cadd) = g1d.place(req[xyi], supply) offsetsxy[xyi] = self.offsets[xyi][ segRange[0] ] + oadd req[xyi] += cadd allocation = gdk.Rectangle(x=offsetsxy[0], y=offsetsxy[1], width=req[0], height=req[1]) child.widget.size_allocate(allocation) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | def place(self, cneed, supply): pads = self.base() need = cneed + pads delta = supply - need; if delta >= 0: gwTotal = self.total_grow_weight() oadd = self.springs[0].pad if gwTotal == 0: cadd = delta else: oadd += round_div(delta * self.springs[0].wGrow, gwTotal) cadd = round_div(delta * self.wGrow, gwTotal) else: # rare shrink = -delta if pads >= shrink: # Cutting from the pads is sufficient oadd = round_div(self.springs[0].pad * delta, pads) cadd = 0 else: oadd = 0 cadd = delta # reduce the child as well return (oadd, cadd) |
1 2 | def round_div(n, d): return (n + d/2) / d |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |