-
Notifications
You must be signed in to change notification settings - Fork 0
/
mode_selector.py
132 lines (95 loc) · 3.33 KB
/
mode_selector.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
# coding: utf-8
# This file is part of https://github.com/marcus67/rechtschreibung
import ui
import six
import log
import defaults
import ui_util
import popup
import predefined_modes
import spelling_mode
if six.PY3:
from importlib import reload
reload(log)
reload(defaults)
reload(ui_util)
reload(popup)
reload(predefined_modes)
reload(spelling_mode)
global logger
logger = log.open_logging(__name__)
class SpellingModeSelector(ui_util.ViewController):
def __init__(self, parent_vc=None):
super(SpellingModeSelector, self).__init__(parent_vc)
self.selected_index = None
self.popup_vc = None
self.load('mode_selector')
def get_selected_mode(self):
if self.selected_index == None:
return None
else:
return self.modes[self.selected_index]
def is_my_action(self, sender):
print ("is_my_action=", str(sender), str(self.list_data_source))
return sender == self.list_data_source
def select(self, modes, cancel_label=defaults.DEFAULT_CANCEL_LABEL,
close_label=defaults.DEFAULT_CLOSE_LABEL, style='sheet', title='Regelsatz laden'):
global logger
self.modes = sorted(modes, key = lambda m:m.compare_key())
self.cancel_label = cancel_label
self.close_label = close_label
items = []
for mode in self.modes:
logger.debug("add mode '%s' to list" % mode.control.name)
entryMap = { 'title' : mode.control.name }
if mode.control.isImmutable:
entryMap['image'] = 'ionicons-ios7-locked-32'
else:
entryMap['image'] = 'ionicons-ios7-unlocked-outline-32'
if len(mode.control.comment) > 0:
entryMap['accessory_type'] = 'detail_button'
logger.debug("add accessory for mode '%s'" % mode.control.name)
items.append(entryMap)
self.list_data_source = ui.ListDataSource(items)
self.list_data_source.highlight_color = defaults.COLOR_LIGHT_GREEN
self.selected_index = None
self.tableview_spelling_mode_selector = self.find_subview_by_name('tableview_spelling_mode_selector')
self.tableview_spelling_mode_selector.data_source = self.list_data_source
self.button_view_cancel = self.find_subview_by_name('button_cancel')
self.button_view_cancel.title = self.cancel_label
self.view.name = title
self.present(style=style)
if not self.parent_vc:
self.view.wait_modal()
def handle_button_action(self, name, sender):
if name == 'button_cancel':
self.view.close()
else:
super(SpellingModeSelector, self).handle_button_action(name, sender)
def handle_list_data_source_action(self, sender):
self.selected_index = sender.selected_row
self.view.close()
self.handle_named_action('load_mode_finish')
def handle_accessory(self, sender):
"""
:type sender: ui.ListDataSource
"""
global logger
logger.debug("handle_accessory row=%d" % sender.tapped_accessory_row)
comment = self.modes[sender.tapped_accessory_row].control.comment
if not self.popup_vc:
self.popup_vc = popup.PopupViewController()
self.popup_vc.present(comment, close_label=self.close_label)
def test():
global logger
logger.info("Test started")
selector = SpellingModeSelector()
selector.select(predefined_modes.get_predefined_modes())
result = selector.get_selected_mode()
if result:
logger.info("selected_mode='%s'" % result.control.name)
else:
logger.info("selection cancelled")
logger.info("Test finished")
if __name__ == '__main__':
test()