xref: /aoo42x/main/toolkit/src2xml/source/boxer.py (revision cdf0e10c)
1*cdf0e10cSrcweir
2*cdf0e10cSrcweirfrom globals import *
3*cdf0e10cSrcweir
4*cdf0e10cSrcweir
5*cdf0e10cSrcweirclass DlgLayoutBuilder(object):
6*cdf0e10cSrcweir    def __init__ (self, dlgnode):
7*cdf0e10cSrcweir        self.dlgnode = dlgnode
8*cdf0e10cSrcweir        self.rows = {}
9*cdf0e10cSrcweir
10*cdf0e10cSrcweir    def addWidget (self, elem):
11*cdf0e10cSrcweir        x, y = int(elem.getAttr('x')), int(elem.getAttr('y'))
12*cdf0e10cSrcweir        self.rows[y] = self.rows.get (y, {})
13*cdf0e10cSrcweir        while self.rows[y].has_key(x):
14*cdf0e10cSrcweir            y += 1
15*cdf0e10cSrcweir            self.rows[y] = self.rows.get (y, {})
16*cdf0e10cSrcweir        self.rows[y][x] = elem
17*cdf0e10cSrcweir
18*cdf0e10cSrcweir    def build (self):
19*cdf0e10cSrcweir        root = Element('vbox')
20*cdf0e10cSrcweir        ys = self.rows.keys()
21*cdf0e10cSrcweir        ys.sort()
22*cdf0e10cSrcweir        for y in ys:
23*cdf0e10cSrcweir            xs = self.rows[y].keys()
24*cdf0e10cSrcweir            xs.sort()
25*cdf0e10cSrcweir
26*cdf0e10cSrcweir            if len(xs) == 1:
27*cdf0e10cSrcweir                root.appendChild(self.rows[y][xs[0]])
28*cdf0e10cSrcweir                continue
29*cdf0e10cSrcweir
30*cdf0e10cSrcweir            hbox = Element('hbox')
31*cdf0e10cSrcweir            root.appendChild(hbox)
32*cdf0e10cSrcweir            for x in xs:
33*cdf0e10cSrcweir                elem = self.rows[y][x]
34*cdf0e10cSrcweir                hbox.appendChild(elem)
35*cdf0e10cSrcweir
36*cdf0e10cSrcweir        return root
37*cdf0e10cSrcweir
38*cdf0e10cSrcweir
39*cdf0e10cSrcweirclass Boxer(object):
40*cdf0e10cSrcweir    def __init__ (self, root):
41*cdf0e10cSrcweir        self.root = root
42*cdf0e10cSrcweir
43*cdf0e10cSrcweir    def layout (self):
44*cdf0e10cSrcweir
45*cdf0e10cSrcweir        newroot = RootNode()
46*cdf0e10cSrcweir        for dlgnode in self.root.children:
47*cdf0e10cSrcweir            newdlgnode = self.__walkDlgNode(dlgnode)
48*cdf0e10cSrcweir            newroot.children.append(newdlgnode)
49*cdf0e10cSrcweir
50*cdf0e10cSrcweir        return newroot
51*cdf0e10cSrcweir
52*cdf0e10cSrcweir    def __walkDlgNode (self, dlgnode):
53*cdf0e10cSrcweir
54*cdf0e10cSrcweir        newnode = Element(dlgnode.name)
55*cdf0e10cSrcweir        newnode.clone(dlgnode)
56*cdf0e10cSrcweir        if dlgnode.name == 'string':
57*cdf0e10cSrcweir            return newnode
58*cdf0e10cSrcweir        newnode.setAttr("xmlns", "http://openoffice.org/2007/layout")
59*cdf0e10cSrcweir        newnode.setAttr("xmlns:cnt", "http://openoffice.org/2007/layout/container")
60*cdf0e10cSrcweir        mx = DlgLayoutBuilder(newnode)
61*cdf0e10cSrcweir
62*cdf0e10cSrcweir        # Each dialog node is expected to have a flat list of widgets.
63*cdf0e10cSrcweir        for widget in dlgnode.children:
64*cdf0e10cSrcweir            if widget.hasAttr('x') and widget.hasAttr('y'):
65*cdf0e10cSrcweir                mx.addWidget(widget)
66*cdf0e10cSrcweir            else:
67*cdf0e10cSrcweir                newnode.appendChild(widget)
68*cdf0e10cSrcweir
69*cdf0e10cSrcweir        vbox = mx.build()
70*cdf0e10cSrcweir        if len(vbox.children) > 0:
71*cdf0e10cSrcweir            newnode.appendChild(vbox)
72*cdf0e10cSrcweir
73*cdf0e10cSrcweir        return newnode
74