-
Notifications
You must be signed in to change notification settings - Fork 4
/
ui_util.py
214 lines (155 loc) · 5.91 KB
/
ui_util.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# coding: utf-8
# This file is part of https://github.com/marcus67/gitsynchista
import ui
import logging
import six
import log
if six.PY3:
from importlib import reload
reload(log)
SWITCH_PREFIX = "switch_"
BITSWITCH_PREFIX = "bitswitch_"
SEGMENTED_CONTROL_PREFIX = "segmented_control_"
SPLIT_CHAR = "|"
MAX_BITS = 8
global logger
logger = log.open_logging(__name__)
def compute_bit_value(state, old_value, state_bit):
if state != bool(old_value & state_bit):
return old_value ^ state_bit
else:
return old_value
def set_container_value(container, name, value):
global logger
if getattr(container, name, None) != None:
setattr(container, name, value)
else:
fmt = "ERROR: name '%s' not found in container '%s'"
logger.error(fmt % (name, type(container).__name__))
def add_missing_attributes(object, template):
changes = 0
for attr in template.__dict__:
if not attr in object.__dict__:
setattr(object, attr, getattr(template, attr))
changes = changes + 1
return changes
def store_in_model(sender, model):
switch_name = sender.name
#print "switch_name=", switch_name
if switch_name.startswith(SWITCH_PREFIX):
util.set_container_value(model, switch_name, sender.value)
elif switch_name.startswith(BITSWITCH_PREFIX):
(switch_name, bit_value) = switch_name.split(SPLIT_CHAR)
#print "mode='%s' value='%s'" % (mode_name, bit_value)
old_value = int(getattr(model, switch_name, None))
util.set_container_value(model, switch_name, util.compute_bit_value(sender.value, old_value, int(bit_value)))
elif switch_name.startswith(SEGMENTED_CONTROL_PREFIX):
util.set_container_value(model, switch_name, sender.selected_index)
else:
return 0
return 1
class ViewController (object):
def __init__(self, parent_view=None):
self.parent_view = parent_view
self.model = None
self.child_controllers = {}
self.view = None
self.subview_map = {}
self.warningWorkaroundIssued = False
def add_child_controller(self, name, child_controller):
self.child_controllers[name] = child_controller
self.subview_map[name] = child_controller.get_view()
def load(self, view_name):
self.view = ui.load_view(view_name)
if self.parent_view:
self.parent_view.add_child_controller(view_name, self)
def get_view(self):
return self.view
def find_subview_by_name(self, name):
global logger
if name in self.subview_map:
logger.debug("find_subview_by_name: found '%s' in cache" % name)
return self.subview_map[name]
if self.view:
logger.debug("find_subview_by_name: find %s in vc of view %s" % (name, self.view.name))
descendant_view = self.find_subview_by_name2(self.view, name)
if descendant_view != None:
self.subview_map[name] = descendant_view
return descendant_view
for child in self.child_controllers.values():
descendant_view = child.find_subview_by_name(name)
if descendant_view != None:
self.subview_map[name] = descendant_view
return descendant_view
logger.debug("find_subview_by_name: view '%s' not found!" % name)
#self.subview_map[name] = None
return None
def find_subview_by_name2(self, view, name):
if view != None:
if view.name == name:
return view
else:
if type(view).__name__ == 'SegmentedControl':
if not self.warningWorkaroundIssued:
logger.warning("find_subview_by_name2: WORKAROUND: skipping iteration over subviews of SegmentedControl '%s'" % view.name)
self.warningWorkaroundIssued = True
else:
if view.subviews:
for subview in view.subviews:
if not subview.name:
continue
descendent_view = self.find_subview_by_name2(subview, name)
if descendent_view != None:
return descendent_view
return None
def handle_action(self, sender):
if self.parent_view:
return self.parent_view.handle_action(sender)
else:
return 0
def present(self, mode='popover'):
global logger
self.view.present(style=mode)
try:
self.view.wait_modal()
except Exception as e:
logger.error("Exception '%s' caught" % str(e))
self.view.close()
def retrieve_from_model(self):
global logger
#print "retrieve_from_model"
for att in self.model.__dict__:
if att.startswith(SWITCH_PREFIX):
view = self.find_subview_by_name(att)
if view:
view.value = getattr(self.model, att)
else:
logger.warning("retrieve_from_model: no view found for switch attribute '%s'" % att)
elif att.startswith(BITSWITCH_PREFIX):
bit = 1
found = False
for i in range(0, MAX_BITS):
viewname = "%s|%d" % (att, bit)
view = self.find_subview_by_name(viewname)
if view != None:
view.value = bool(getattr(self.model, att) & bit)
found = True
bit = bit * 2
if not found:
logger.warning("retrieve_from_model: no view found for BIT switch attribute '%s'" % att)
elif att.startswith(SEGMENTED_CONTROL_PREFIX):
view = self.find_subview_by_name(att)
if view != None:
view.selected_index = getattr(self.model, att)
else:
logger.warning("retrieve_from_model: no view found for segmented control attribute '%s'" % att)
else:
logger.debug("retrieve_from_model: unsupported attribute prefix in attribute '%s'" % att)
def set_model(self, model, retrieve=True):
self.model = model
if retrieve:
self.retrieve_from_model()
def test():
vc = ViewController()
if __name__ == '__main__':
test()