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 x in self.rows[y]: 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 = sorted(self.rows.keys()) 42 for y in ys: 43 xs = sorted(self.rows[y].keys()) 44 45 if len(xs) == 1: 46 root.appendChild(self.rows[y][xs[0]]) 47 continue 48 49 hbox = Element('hbox') 50 root.appendChild(hbox) 51 for x in xs: 52 elem = self.rows[y][x] 53 hbox.appendChild(elem) 54 55 return root 56 57 58class Boxer(object): 59 def __init__ (self, root): 60 self.root = root 61 62 def layout (self): 63 64 newroot = RootNode() 65 for dlgnode in self.root.children: 66 newdlgnode = self.__walkDlgNode(dlgnode) 67 newroot.children.append(newdlgnode) 68 69 return newroot 70 71 def __walkDlgNode (self, dlgnode): 72 73 newnode = Element(dlgnode.name) 74 newnode.clone(dlgnode) 75 if dlgnode.name == 'string': 76 return newnode 77 newnode.setAttr("xmlns", "http://openoffice.org/2007/layout") 78 newnode.setAttr("xmlns:cnt", "http://openoffice.org/2007/layout/container") 79 mx = DlgLayoutBuilder(newnode) 80 81 # Each dialog node is expected to have a flat list of widgets. 82 for widget in dlgnode.children: 83 if widget.hasAttr('x') and widget.hasAttr('y'): 84 mx.addWidget(widget) 85 else: 86 newnode.appendChild(widget) 87 88 vbox = mx.build() 89 if len(vbox.children) > 0: 90 newnode.appendChild(vbox) 91 92 return newnode 93