| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
""" |
| 20 |
A class displaying all available sensors |
| 21 |
""" |
| 22 |
|
| 23 |
import urwid |
| 24 |
|
| 25 |
from s_tui.sturwid.ui_elements import ViListBox |
| 26 |
|
| 27 |
|
| 28 |
class SensorsMenu: |
| 29 |
MAX_TITLE_LEN = 120 |
| 30 |
|
| 31 |
def on_mode_button(self, button, state): |
| 32 |
pass |
| 33 |
|
| 34 |
def __init__(self, return_fn, source_list, default_source_conf): |
| 35 |
self.return_fn = return_fn |
| 36 |
|
| 37 |
|
| 38 |
cancel_button = urwid.Button("Cancel", on_press=self.on_cancel) |
| 39 |
cancel_button._label.align = "center" |
| 40 |
apply_button = urwid.Button("Apply", on_press=self.on_apply) |
| 41 |
apply_button._label.align = "center" |
| 42 |
if_buttons = urwid.Columns([apply_button, cancel_button]) |
| 43 |
|
| 44 |
self.sensor_status_dict = {} |
| 45 |
sensor_column_list = [] |
| 46 |
selector_buttons_column_list = [] |
| 47 |
self.sensor_button_dict = {} |
| 48 |
self.active_sensors = {} |
| 49 |
for source in source_list: |
| 50 |
source_name = source.get_source_name() |
| 51 |
|
| 52 |
|
| 53 |
conf = default_source_conf.get(source_name) or {} |
| 54 |
self.sensor_status_dict[source_name] = [] |
| 55 |
for sensor in source.get_sensor_list(): |
| 56 |
|
| 57 |
self.sensor_status_dict[source_name].append( |
| 58 |
conf.get(sensor.lower(), True) |
| 59 |
) |
| 60 |
|
| 61 |
self.sensor_button_dict[source_name] = [] |
| 62 |
self.active_sensors[source_name] = [] |
| 63 |
|
| 64 |
|
| 65 |
sensor_title_str = source_name |
| 66 |
sensor_title = urwid.Text(("bold text", sensor_title_str), "center") |
| 67 |
|
| 68 |
|
| 69 |
current_col_nr = 0 |
| 70 |
for sensor, s_tatus in zip( |
| 71 |
source.get_sensor_list(), self.sensor_status_dict[source_name] |
| 72 |
): |
| 73 |
cb = urwid.CheckBox(sensor, s_tatus) |
| 74 |
self.sensor_button_dict[source_name].append(cb) |
| 75 |
self.active_sensors[source_name].append(s_tatus) |
| 76 |
current_col_nr += 1 |
| 77 |
|
| 78 |
col_selector_buttons = [] |
| 79 |
if current_col_nr > 0: |
| 80 |
checkall_button = urwid.Button( |
| 81 |
"Check all", |
| 82 |
on_press=self.on_checkall_col, |
| 83 |
user_data=sensor_title_str, |
| 84 |
) |
| 85 |
|
| 86 |
col_selector_buttons.append(checkall_button) |
| 87 |
uncheckall_button = urwid.Button( |
| 88 |
"Uncheck all", |
| 89 |
on_press=self.on_uncheckall_col, |
| 90 |
user_data=sensor_title_str, |
| 91 |
) |
| 92 |
|
| 93 |
col_selector_buttons.append(uncheckall_button) |
| 94 |
|
| 95 |
sensor_title_and_buttons = [sensor_title] + self.sensor_button_dict[ |
| 96 |
source_name |
| 97 |
] |
| 98 |
listw = urwid.SimpleFocusListWalker(sensor_title_and_buttons) |
| 99 |
sensor_column_list.append(urwid.Pile(listw)) |
| 100 |
|
| 101 |
listw = urwid.SimpleFocusListWalker(col_selector_buttons) |
| 102 |
selector_buttons_column_list.append(urwid.Pile(listw)) |
| 103 |
|
| 104 |
sensor_select_widget = urwid.Columns(sensor_column_list) |
| 105 |
selector_buttons_widget = urwid.Columns( |
| 106 |
selector_buttons_column_list, box_columns=[0, 1] |
| 107 |
) |
| 108 |
|
| 109 |
list_temp = [sensor_select_widget, selector_buttons_widget, if_buttons] |
| 110 |
listw = urwid.SimpleFocusListWalker(list_temp) |
| 111 |
self.main_window = urwid.LineBox(ViListBox(listw)) |
| 112 |
|
| 113 |
max_height = 6 |
| 114 |
for _, s_tatus in self.active_sensors.items(): |
| 115 |
max_height = max(max_height, len(s_tatus) + 6) |
| 116 |
|
| 117 |
self.size = max_height, self.MAX_TITLE_LEN |
| 118 |
|
| 119 |
def get_size(self): |
| 120 |
return self.size |
| 121 |
|
| 122 |
def set_checkbox_value(self): |
| 123 |
for sensor, sensor_cb in self.sensor_button_dict.items(): |
| 124 |
sensor_cb_next_state = self.active_sensors[sensor] |
| 125 |
for checkbox, state in zip(sensor_cb, sensor_cb_next_state): |
| 126 |
checkbox.set_state(state) |
| 127 |
|
| 128 |
def on_cancel(self, w): |
| 129 |
self.set_checkbox_value() |
| 130 |
self.return_fn(update=False) |
| 131 |
|
| 132 |
def on_apply(self, w): |
| 133 |
update_sensor_visibility = False |
| 134 |
for s_name, sensor_buttons in self.sensor_button_dict.items(): |
| 135 |
cb_sensor_visibility = [ |
| 136 |
sensor_cb.get_state() for sensor_cb in sensor_buttons |
| 137 |
] |
| 138 |
|
| 139 |
if cb_sensor_visibility != self.active_sensors[s_name]: |
| 140 |
update_sensor_visibility = True |
| 141 |
|
| 142 |
self.active_sensors[s_name] = cb_sensor_visibility |
| 143 |
|
| 144 |
self.set_checkbox_value() |
| 145 |
self.return_fn(update=update_sensor_visibility) |
| 146 |
|
| 147 |
def setall_cb_col(self, w, col, state): |
| 148 |
for s_name, sensor_buttons in self.sensor_button_dict.items(): |
| 149 |
if s_name == col: |
| 150 |
for checkbox in sensor_buttons: |
| 151 |
checkbox.set_state(state) |
| 152 |
|
| 153 |
def on_uncheckall_col(self, w, col): |
| 154 |
self.setall_cb_col(self, col, False) |
| 155 |
|
| 156 |
def on_checkall_col(self, w, col): |
| 157 |
self.setall_cb_col(self, col, True) |