-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnicebox.py
More file actions
185 lines (137 loc) · 5.24 KB
/
nicebox.py
File metadata and controls
185 lines (137 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""
NiceBox is convenient wrapper for wx.BoxSizer and wx.StaticBoxSizer.
"""
import wx
def NiceBox(orient, parent=None, label=None):
"""Generate a NiceBox sizer for wx widgets."""
if label is None:
sizer_parent = wx.BoxSizer
args, kwargs = (), {'orient': orient}
elif parent is not None:
static_box = wx.StaticBox(parent, label=label)
sizer_parent = wx.StaticBoxSizer
args, kwargs = (static_box,), {'orient': orient}
else:
raise ValueError('NiceBox must have a parent if a label is provided!')
class NiceBox(sizer_parent):
def __init__(self, orient, parent, label):
super(NiceBox, self).__init__(*args, **kwargs)
self.orient = orient
def add(self, widget, align=None, border=0, grow=(0, 0)):
_validate_align(align)
_validate_border(border)
_validate_grow(grow)
self.Add(widget,
flag=_get_flag(align, border, grow, self.orient),
border=_get_border(border),
proportion=_get_proportion(grow, self.orient))
return self
def pad(self, size):
"""Add fixed space in the the dimension of the sizer."""
if not _validate_positive_int(size):
raise ValueError('Size must be a positive integer!')
x = size if self.orient == wx.HORIZONTAL else -1
y = size if self.orient != wx.HORIZONTAL else -1
self.Add((x, y))
return self
def space(self, grow=1):
"""Add space that grows in the dimension of the sizer."""
self.Add((-1, -1), proportion=grow)
return self
return NiceBox(orient, parent, label)
def _get_border(border):
"""Return the border size."""
border = border if type(border) == int else max(border)
return border
def _get_flag(align, border, grow, orient):
"""Determine the flags for the widget."""
flag = 0
if align is not None:
top, right, bottom, left = align
if top and bottom:
flag |= wx.ALIGN_CENTER_VERTICAL
elif top:
flag |= wx.ALIGN_TOP
elif bottom:
flag |= wx.ALIGN_BOTTOM
if left and right:
flag |= wx.ALIGN_CENTER_HORIZONTAL
elif left:
flag |= wx.ALIGN_LEFT
elif right:
flag |= wx.ALIGN_RIGHT
if type(border) == int:
if border > 0:
flag |= wx.ALL
else:
for val, side in zip(border, (wx.TOP, wx.RIGHT, wx.BOTTOM, wx.LEFT)):
if val > 0:
flag |= side
expand = grow[1] if orient == wx.HORIZONTAL else grow[0]
if expand:
flag |= wx.EXPAND
return flag
def _get_proportion(grow, orient):
"""Return the wx.Sizer.Add proportion argument."""
if orient == wx.HORIZONTAL:
return grow[0]
else:
return grow[1]
def _validate_align(align):
"""Ensure align is a tuple of four 1's and 0's."""
if align is None:
return True
if type(align) not in (list, tuple):
raise TypeError('align must be a tuple of four 1\'s and 0\'s!')
try:
assert len(align) == 4
assert _validate_positive_ints(align)
assert 0 <= max(align) <= 1
assert 0 <= min(align) <= 1
except (AssertionError, TypeError):
raise ValueError('align must be a tuple of four 1\'s and 0\'s!')
return True
def _validate_border(border):
"""Ensure the border is an integer or four integer tuple."""
if _validate_positive_int(border):
return True
# At this point, border is not a positive integer
if type(border) not in (list, tuple):
raise TypeError(
'border must be a positive integer or four integer tuple!')
try:
assert len(border) == 4
assert _validate_positive_ints(border)
except AssertionError:
raise ValueError(
'border must be a positive integer or four integer tuple!')
# At this point, border is a four element tuple
non_zero = [size for size in border if size > 0]
if not non_zero:
return True
try:
assert max(non_zero) == min(non_zero)
except AssertionError:
raise ValueError('border sizes must be equal!')
return True
def _validate_grow(grow):
"""Ensure grow is a two element tuple of positive integers."""
if type(grow) not in (list, tuple):
raise TypeError('grow must be a tuple of two positive integers!')
try:
assert len(grow) == 2
assert _validate_positive_ints(grow)
except (AssertionError, AttributeError):
raise ValueError('grow must be a tuple of two positive integers!')
return True
def _validate_positive_int(value):
"""Ensure a value is a positive integer."""
return type(value) == int and value >= 0
def _validate_positive_ints(iterable):
"""Ensure every value in the iterable is a positive integer."""
try:
return all(map(_validate_positive_int, iterable))
except TypeError:
return False
if __name__ == '__main__':
pass