comparison engine/extensions/pychan/autoposition.py @ 205:54bfd1015b35

* PyChan event handling rework (part I) ** Unified listeners ** ...hopefully more robust attach/detach code. * Added compat, layout and also the new autopsition feature. * Documentation * Minor style fixes in core.
author phoku@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 14 Mar 2009 12:13:29 +0000
parents
children 48c99636453e
comparison
equal deleted inserted replaced
204:5816ab527da8 205:54bfd1015b35
1 # coding: utf-8
2
3 """
4 Automatic widget positioning
5 ============================
6
7 You can use the C{position_techinque} attribute
8 on top level widgets which can also be set from xml.
9
10 For direct use call L{placeWidget}.
11 """
12
13 from internal import screen_width, screen_height
14 from exceptions import PyChanException
15
16 EXPLICIT = "explicit"
17 AUTOMATIC = "automatic"
18
19 TOP = "top"
20 LEFT = "left"
21 RIGHT = "right"
22 CENTER = "center"
23 BOTTOM = "bottom"
24
25
26 def _splicePosition(p):
27 if "+" in p:
28 technique,delta = p.split("+")
29 elif "-" in p:
30 technique,delta = p.split("-")
31 delta = '-' + delta
32 else:
33 technique,delta = p,0
34 delta = int(delta)
35 return technique,delta
36
37 def _parsePosition(position):
38 try:
39 if position == AUTOMATIC:
40 position = "center+0:center+0"
41
42 x_pos,y_pos = position.split(":")
43 x_pos, x_delta = _splicePosition(x_pos)
44 y_pos, y_delta = _splicePosition(y_pos)
45
46 if x_pos not in [EXPLICIT,LEFT,CENTER,RIGHT]:
47 raise
48 if y_pos not in [EXPLICIT,TOP,CENTER,BOTTOM]:
49 raise
50 except:
51 raise PyChanException("Malformed position definition: " + repr(position))
52 return x_pos,x_delta,y_pos,y_delta
53
54 def placeWidget(widget,position):
55 """
56 Place a widget according to a string defining relative coordinates to screen borders.
57
58 The position definition has to be of the form: C{"<x_pos><x_delta>:<y_pos><y_delta>"}
59
60 C{<x_pos>} may be one of:
61 - left
62 - right
63 - center
64 - explicit
65
66 C{<y_pos>} may be one of:
67 - top
68 - bottom
69 - center
70 - explicit
71
72 C{explicit} means that the widgets x or y position will not be touched. The rest should be
73 self explanatory.
74
75 C{<x_delta>} and C{<y_delta>} must be of the form: +pixel_number or -pixel_number. Or comletely
76 omitted. Note that the sign has to be there for for positive deltas, too.
77
78 For brevity two shortcuts exist:
79 - "explict" -> "explict:explict"
80 - "automatic" -> "center:center"
81
82 A few examples::
83 "right-20:top"
84 "center:top+10"
85 "center:center"
86
87 @param widget: The PyChan widget.
88 @param position: A position definition.
89
90 If the position cannot be parsed a L{PyChanException} is thrown.
91
92 """
93 if position == EXPLICIT:
94 return
95 x_pos,x_delta,y_pos,y_delta = _parsePosition(position)
96
97 x,y = widget.position
98 w,h = widget.size
99
100 if x_pos == CENTER:
101 x = (screen_width()-w)/2 + x_delta
102
103 if y_pos == CENTER:
104 y = (screen_height()-h)/2 + y_delta
105
106 if x_pos == LEFT:
107 x = x_delta
108
109 if y_pos == TOP:
110 y = y_delta
111
112 if x_pos == RIGHT:
113 x = screen_width() - w + x_delta
114
115 if y_pos == BOTTOM:
116 y = screen_height() - h + y_delta
117
118 widget.position = x,y
119
120
121