/
tree.py
176 lines (147 loc) · 5.82 KB
/
tree.py
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
from pygame import Rect, Surface
from pygame.locals import *
class TreeOptions(object):
empty = (255,255,255), u'\u2610'
yes = (0,255,0), u'\u2611'
no = (255,0,0), u'\u2612'
collapsed = (255,255,255), u'\u25b6'
expanded = (255,255,255), u'\u25e2'
def __init__(self, options, font, prefs, changed):
self._options = self._initoptions(options)
self._prefs = prefs
self._changed = changed
self.scale(font)
@classmethod
def _initoptions(cls, branch):
result = []
for key, name, value in branch:
if hasattr(value, '__iter__'):
child = cls._initoptions(value)
else:
child = value
result.append([key, name, False, child])
return result
def scale(self, font):
self._font = font
self._emp, self._y, self._n, self._col, self._exp = [
self._font.render(s[1], True, s[0])
for s in self.empty, self.yes, self.no,
self.collapsed, self.expanded]
def selected(self, branch = None):
branch = branch or self._options
result = []
for key, name, expanded, value in branch:
if hasattr(value, '__iter__'):
result.extend(self.selected(value))
elif value:
result.append(key)
return result
def _setvalue(self, branch, key, value):
if value is None:
return
for i in range(len(branch)):
if branch[i][0] == key:
if hasattr(branch[i][3], '__iter__'):
for child in branch[i][3]:
self._setvalue(branch[i][3], child[0], value)
else:
branch[i][3] = value
self._changed()
break
else:
raise ValueError
def _boxhandler(self, branch, key, value):
return lambda: self._setvalue(branch, key, value)
def _setexpand(self, branch, key, expand):
for i in range(len(branch)):
if branch[i][0] == key:
if hasattr(branch[i][3], '__iter__'):
branch[i][2] = expand
else:
raise ValueError
break
else:
raise ValueError
def _arrowhandler(self, branch, key, expand):
return lambda: self._setexpand(branch, key, expand)
def _drawboxes(self, branch, key, a, b, aval, bval):
boxes = Surface((a.get_width() + b.get_width(), self._emp.get_height()),
flags=SRCALPHA)
locs = (0,0), (a.get_width(), 0)
boxes.blit(a, locs[0])
boxes.blit(b, locs[1])
return [(Rect(locs[i], self._emp.get_size()), 1,
self._boxhandler(branch, key, (aval,bval)[i]))
for i in range(len(locs))], boxes
def _drawbranch(self, indent, branch):
result = []
truths = []
boxoffset = 0
for key, name, expanded, value in branch:
space = self._font.render(' '*4*indent, False, (0,0,0))
title = self._font.render(name, True, (255,255,255))
if hasattr(value, '__iter__'):
arrow = self._exp if expanded else self._col
lines, suboffset, allsame = self._drawbranch(indent+1, value)
selected = allsame
boxoffset = max(boxoffset, suboffset)
else:
arrow = None
lines = []
selected = value
width = (space.get_width() +
(arrow.get_width() if arrow else 0) +
title.get_width())
height = title.get_height()
image = Surface((width, height), flags=SRCALPHA)
image.blit(space, (0,0))
if arrow:
loc = space.get_width(), 0
image.blit(arrow, loc)
buttons = [(Rect(loc, arrow.get_size()), 0,
self._arrowhandler(branch, key, not expanded))]
else:
buttons = []
image.blit(title, (image.get_width()-title.get_width(), 0))
if selected:
boxes = self._y, self._emp
actions = None, False
elif selected == False:
boxes = self._emp, self._n
actions = True, None
else:
boxes = self._emp, self._emp
actions = True, False
boxbuttons, boximage = self._drawboxes(branch, key,
*(boxes + actions))
buttons.extend(boxbuttons)
result.append((image, buttons, boximage))
if expanded:
result.extend(lines)
truths.append(selected)
boxoffset = max(boxoffset, image.get_width())
allsame = (True if all(truths) else
False if all([t == False for t in truths]) else
None)
return result, boxoffset, allsame
def handle(self, e):
if e.type == MOUSEBUTTONDOWN:
for b in self._buttons:
if b[0].collidepoint(e.pos):
b[1]()
return True
return False
def draw(self):
entries, boxoffset, allsame = self._drawbranch(0, self._options)
self._buttons = []
dh = entries[0][0].get_height()
surface = Surface((boxoffset + entries[0][2].get_width(), len(entries)*dh),
flags=SRCALPHA)
h = 0
for line, buttons, boxes in entries:
surface.blit(line, (0, h))
surface.blit(boxes, (boxoffset, h))
self._buttons.extend([(b[0].move(b[1] * boxoffset, h), b[2])
for b in buttons])
h += dh
return surface