1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """Defines a helper class for setting alignments using a short notation."""
17
18 from muntjac.ui.alignment import Alignment
19 from muntjac.ui.layout import IAlignmentHandler
23 """Helper class for setting alignments using a short notation.
24
25 Supported notation is:
26
27 - t, top for top alignment
28 - m, middle for vertical center alignment
29 - b, bottom for bottom alignment
30 - l, left for left alignment
31 - c, center for horizontal center alignment
32 - r, right for right alignment
33
34 @deprecated: replaced by L{Alignment}.
35 """
36
37 _horizontalMask = (IAlignmentHandler.ALIGNMENT_LEFT
38 | IAlignmentHandler.ALIGNMENT_HORIZONTAL_CENTER
39 | IAlignmentHandler.ALIGNMENT_RIGHT)
40
41 _verticalMask = (IAlignmentHandler.ALIGNMENT_TOP
42 | IAlignmentHandler.ALIGNMENT_VERTICAL_CENTER
43 | IAlignmentHandler.ALIGNMENT_BOTTOM)
44
45 _alignmentStrings = dict()
46
47 @classmethod
51
52
53 @classmethod
55 """Set the alignment for the component using short notation.
56
57 @param parent:
58 @param component:
59 @param alignment:
60 String containing one or two alignment strings. If short
61 notation "r", "t", etc is used valid strings include
62 "r", "rt", "tr", "t". If the longer notation is used the
63 alignments should be separated by a space e.g.
64 "right", "right top", "top right", "top". It is valid to
65 mix short and long notation but they must be separated by a
66 space e.g. "r top".
67 @raise ValueError:
68 """
69 if alignment is None or len(alignment) == 0:
70 raise ValueError, ('alignment for setComponentAlignment() '
71 'cannot be null or empty')
72
73 currentAlignment = parent.getComponentAlignment(
74 component).getBitMask()
75
76 if len(alignment) == 1:
77
78 currentAlignment = cls.parseAlignment(alignment[:1],
79 currentAlignment)
80
81 elif len(alignment) == 2:
82
83 currentAlignment = cls.parseAlignment(alignment[:1],
84 currentAlignment)
85 currentAlignment = cls.parseAlignment(alignment[1:2],
86 currentAlignment)
87
88 else:
89
90 strings = alignment.split(' ')
91 if len(strings) > 2:
92 raise ValueError, ('alignment for setComponentAlignment() '
93 'should not contain more than 2 alignments')
94
95 for alignmentString in strings:
96 currentAlignment = cls.parseAlignment(alignmentString,
97 currentAlignment)
98
99 horizontalAlignment = currentAlignment & cls._horizontalMask
100 verticalAlignment = currentAlignment & cls._verticalMask
101 parent.setComponentAlignment(component,
102 Alignment(horizontalAlignment + verticalAlignment))
103
104
105 @classmethod
107 """Parse alignmentString which contains one alignment (horizontal
108 or vertical) and return and updated version of the passed alignment
109 where the alignment in one direction has been changed. If the passed
110 alignmentString is unknown an exception is thrown
111
112 @raise ValueError:
113 """
114 parsed = cls._alignmentStrings.get( alignmentString.lower() )
115
116 if parsed is None:
117 raise ValueError, ('Could not parse alignment string \''
118 + alignmentString + '\'')
119
120 if parsed & cls._horizontalMask != 0:
121
122 vertical = alignment & cls._verticalMask
123
124 alignment = vertical | parsed
125
126 else:
127
128 horizontal = alignment & cls._horizontalMask
129
130 alignment = horizontal | parsed
131
132 return alignment
133
134
135 AlignmentUtils.addMapping(IAlignmentHandler.ALIGNMENT_TOP, 't', 'top')
136 AlignmentUtils.addMapping(IAlignmentHandler.ALIGNMENT_BOTTOM, 'b', 'bottom')
137 AlignmentUtils.addMapping(IAlignmentHandler.ALIGNMENT_VERTICAL_CENTER, 'm', 'middle')
138 AlignmentUtils.addMapping(IAlignmentHandler.ALIGNMENT_LEFT, 'l', 'left')
139 AlignmentUtils.addMapping(IAlignmentHandler.ALIGNMENT_RIGHT, 'r', 'right')
140 AlignmentUtils.addMapping(IAlignmentHandler.ALIGNMENT_HORIZONTAL_CENTER, 'c', 'center')
141