ARGH
[matches/honours.git] / research / transmission_spectroscopy / simulator / pgu-0.18 / build / lib / pgu / gui / layout.py
1 """Document layout engine."""
2
3 class Layout:
4     """The document layout engine."""
5     
6     def __init__(self,rect=None):
7         """initialize the object with the size of the box."""
8         self._widgets = []
9         self.rect = rect
10         
11     def add(self,e): 
12         """Add a document element to the layout.
13         
14         The document element may be
15         * a tuple (w,h) if it is a whitespace element
16         * a tuple (0,h) if it is a linebreak element
17         * an integer -1,0,1 if it is a command to start a new block of elements 
18             that are aligned either left,center, or right.
19         * an object with a .rect (for size) -- such as a word element
20         * an object with a .rect (for size) and .align -- such as an image element
21
22         """
23         
24         self._widgets.append(e)
25         
26         
27     def resize(self):
28         """Resize the layout.
29
30         This method recalculates the position of all document elements after 
31         they have been added to the document.  .rect.x,y will be updated for
32         all objects.
33
34         """
35         self.init()
36         self.widgets = []
37         for e in self._widgets:
38             if type(e) is tuple and e[0] != 0:
39                 self.do_space(e)
40             elif type(e) is tuple and e[0] == 0:
41                 self.do_br(e[1])
42             elif type(e) is int:
43                 self.do_block(align=e)
44             elif hasattr(e,'align'):
45                 self.do_align(e)
46             else:
47                 self.do_item(e)
48         self.line()
49         self.rect.h = max(self.y,self.left_bottom,self.right_bottom)
50             
51     def init(self):
52         self.x,self.y = self.rect.x,self.rect.y
53         self.left = self.rect.left
54         self.right = self.rect.right
55         self.left_bottom = 0
56         self.right_bottom = 0
57         self.y = self.rect.y
58         self.x = self.rect.x
59         self.h = 0
60         
61         self.items = []
62         self.align = -1
63         
64     def getleft(self):
65         if self.y > self.left_bottom:
66             self.left = self.rect.left
67         return self.left
68         
69     def getright(self):
70         if self.y > self.right_bottom:
71             self.right = self.rect.right
72         return self.right
73         
74     def do_br(self,h): 
75         self.line()
76         self.h = h
77     
78     def do_block(self,align=-1):
79         self.line()
80         self.align = align
81
82     def do_align(self,e):
83         align = e.align
84         ox,oy,oh = self.x,self.y,self.h
85         w,h = e.rect.w,e.rect.h
86         
87         if align == 0: 
88             self.line()
89             self.x = self.rect.left + (self.rect.width-w)/2
90             self.fit = 0
91         elif align == -1: 
92             self.line()
93             self.y = max(self.left_bottom,self.y + self.h)
94             self.h = 0
95             self.x = self.rect.left
96         elif align == 1: 
97             self.line()
98             self.y = max(self.right_bottom,self.y + self.h)
99             self.h = 0
100             self.x = self.rect.left + (self.rect.width-w)
101
102         e.rect.x,e.rect.y = self.x,self.y        
103             
104         self.x = self.x + w
105         self.y = self.y
106         
107         if align == 0:
108             self.h = max(self.h,h)
109             self.y = self.y + self.h
110             self.x = self.getleft()
111             self.h = 0
112         elif align == -1:
113             self.left = self.x
114             self.left_bottom = self.y + h
115             self.x,self.y,self.h = ox + w,oy,oh
116         elif align == 1: 
117             self.right = self.x - w
118             self.right_bottom = self.y + h
119             self.x,self.y,self.h = ox,oy,oh
120         
121         self.widgets.append(e)
122
123     def do_space(self,e):
124         w,h = e
125         if self.x+w >= self.getright(): 
126             self.line()
127         else: 
128             self.items.append(e)
129             self.h = max(self.h,h)
130             self.x += w
131     
132     def do_item(self,e):
133         w,h = e.rect.w,e.rect.h
134         if self.x+w >= self.getright(): 
135             self.line()
136         self.items.append(e)
137         self.h = max(self.h,h)
138         self.x += w
139     
140     def line(self):
141         x1 = self.getleft()
142         x2 = self.getright()
143         align = self.align
144         y = self.y
145         
146         if len(self.items) != 0 and type(self.items[-1]) == tuple:
147             del self.items[-1]
148         
149         w = 0
150         for e in self.items:
151             if type(e) == tuple: w += e[0]
152             else: w += e.rect.w
153             
154         if align == -1: x = x1
155         elif align == 0: 
156             x = x1 + ((x2-x1)-w)/2
157             self.fit = 0
158         elif align == 1: x = x2 - w
159             
160         for e in self.items:
161             if type(e) == tuple: x += e[0]
162             else:
163                 e.rect.x,e.rect.y = x,y
164                 self.widgets.append(e)
165                 x += e.rect.w
166         
167         self.items = []
168         self.y = self.y + self.h
169         self.x = self.getleft()
170         self.h = 0
171         
172

UCC git Repository :: git.ucc.asn.au