1# ************************************************************* 2# 3# Licensed to the Apache Software Foundation (ASF) under one 4# or more contributor license agreements. See the NOTICE file 5# distributed with this work for additional information 6# regarding copyright ownership. The ASF licenses this file 7# to you under the Apache License, Version 2.0 (the 8# "License"); you may not use this file except in compliance 9# with the License. You may obtain a copy of the License at 10# 11# http://www.apache.org/licenses/LICENSE-2.0 12# 13# Unless required by applicable law or agreed to in writing, 14# software distributed under the License is distributed on an 15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16# KIND, either express or implied. See the License for the 17# specific language governing permissions and limitations 18# under the License. 19# 20# ************************************************************* 21 22 23from globals import * 24 25 26class DlgLayoutBuilder(object): 27 def __init__ (self, dlgnode): 28 self.dlgnode = dlgnode 29 self.rows = {} 30 31 def addWidget (self, elem): 32 x, y = int(elem.getAttr('x')), int(elem.getAttr('y')) 33 self.rows[y] = self.rows.get (y, {}) 34 while self.rows[y].has_key(x): 35 y += 1 36 self.rows[y] = self.rows.get (y, {}) 37 self.rows[y][x] = elem 38 39 def build (self): 40 root = Element('vbox') 41 ys = self.rows.keys() 42 ys.sort() 43 for y in ys: 44 xs = self.rows[y].keys() 45 xs.sort() 46 47 if len(xs) == 1: 48 root.appendChild(self.rows[y][xs[0]]) 49 continue 50 51 hbox = Element('hbox') 52 root.appendChild(hbox) 53 for x in xs: 54 elem = self.rows[y][x] 55 hbox.appendChild(elem) 56 57 return root 58 59 60class Boxer(object): 61 def __init__ (self, root): 62 self.root = root 63 64 def layout (self): 65 66 newroot = RootNode() 67 for dlgnode in self.root.children: 68 newdlgnode = self.__walkDlgNode(dlgnode) 69 newroot.children.append(newdlgnode) 70 71 return newroot 72 73 def __walkDlgNode (self, dlgnode): 74 75 newnode = Element(dlgnode.name) 76 newnode.clone(dlgnode) 77 if dlgnode.name == 'string': 78 return newnode 79 newnode.setAttr("xmlns", "http://openoffice.org/2007/layout") 80 newnode.setAttr("xmlns:cnt", "http://openoffice.org/2007/layout/container") 81 mx = DlgLayoutBuilder(newnode) 82 83 # Each dialog node is expected to have a flat list of widgets. 84 for widget in dlgnode.children: 85 if widget.hasAttr('x') and widget.hasAttr('y'): 86 mx.addWidget(widget) 87 else: 88 newnode.appendChild(widget) 89 90 vbox = mx.build() 91 if len(vbox.children) > 0: 92 newnode.appendChild(vbox) 93 94 return newnode 95