class OptionWidget(WidgetWrap): def __init__(self, name, optype, description, default, current_value=None, value_changed_callback=None): self.name = name self.optype = OptionType.__members__[optype.upper()] self.description = description self.default = default self.current_value = current_value or default w = self.build_widgets() self.value_changed_callback = value_changed_callback super().__init__(w) self.update() def selectable(self): return True def build_widgets(self): desc_text = Text(["\n", strip_solo_dots(self.description)]) self.reset_button = PlainButton("Reset to Default", self.do_reset) if self.optype == OptionType.BOOLEAN: self.control = CheckBox('', state=bool(self.current_value)) self.wrapped_control = self.control elif self.optype == OptionType.INT: self.control = IntEdit(default=self.current_value) self.wrapped_control = Color.string_input( self.control, focus_map='string_input focus') elif self.optype == OptionType.STRING: edit_text = self.current_value or "" self.control = StringEditor(edit_text=edit_text) self.wrapped_control = Color.string_input( self.control, focus_map='string_input focus') elif self.optype == OptionType.FLOAT: edit_text = str(self.current_value) self.control = StringEditor(edit_text=edit_text) self.wrapped_control = Color.string_input( self.control, focus_map='string_input focus') else: raise Exception("Unknown option type") self.control_columns = Columns( [('pack', Text("{}:".format(self.name), align='right')), (80, self.wrapped_control)], dividechars=1) if self.optype in [OptionType.STRING, OptionType.FLOAT]: connect_signal(self.control._edit, 'change', self.handle_value_changed) else: connect_signal(self.control, 'change', self.handle_value_changed) button_grid = GridFlow([ Color.button_secondary(self.reset_button, focus_map='button_secondary focus') ], 36, 1, 0, 'right') return Pile([ Padding.line_break(""), Padding.left(self.control_columns, left=1), Padding.left(desc_text, left=2), button_grid ]) def handle_value_changed(self, sender, value): self.current_value = value if self.optype == OptionType.INT: v = value if value not in ['', '-']: v = int(value) self.value_changed_callback(self.name, v) elif self.optype == OptionType.FLOAT: try: v = float(value) except: pass else: self.value_changed_callback(self.name, v) else: self.value_changed_callback(self.name, self.current_value) def do_reset(self, sender): self.current_value = str(self.default) if self.optype == OptionType.BOOLEAN: newstate = True if self.current_value == "True" else False self.control.state = newstate elif self.optype == OptionType.INT: edit_text = self.current_value or "" self.control.set_edit_text(edit_text) elif self.optype == OptionType.STRING: edit_text = self.current_value or "" self.control.value = edit_text elif self.optype == OptionType.FLOAT: self.control.value = self.current_value def update(self): pass
class OptionWidget(WidgetWrap): def __init__(self, name, optype, description, default, current_value=None, value_changed_callback=None): self.name = name self.optype = OptionType.__members__[optype.upper()] self.description = description self.default = default self.current_value = current_value or default w = self.build_widgets() self.value_changed_callback = value_changed_callback super().__init__(w) self.update() def selectable(self): return True def build_widgets(self): title_text = Text([("body", self.name)], align="center") desc_text = Text(["\n", strip_solo_dots(self.description)]) self.reset_button = PlainButton("Reset to Default", self.do_reset) if self.optype == OptionType.BOOLEAN: self.control = CheckBox(self.name, state=bool(self.current_value)) elif self.optype == OptionType.INT: self.control = IntEdit(caption="{}: ".format(self.name), default=self.current_value) elif self.optype == OptionType.STRING: edit_text = self.current_value or "" self.control = StringEditor( caption="{}: ".format(self.name), edit_text=edit_text) else: raise Exception("Unknown option type") if self.optype == OptionType.STRING: connect_signal(self.control._edit, 'change', self.handle_value_changed) else: connect_signal(self.control, 'change', self.handle_value_changed) button_grid = GridFlow([self.reset_button], 36, 1, 0, 'right') return Pile([Divider(), title_text, desc_text, self.control, button_grid]) def handle_value_changed(self, sender, value): self.current_value = value if self.optype == OptionType.INT: v = value if value not in ['', '-']: v = int(value) self.value_changed_callback(self.name, v) else: self.value_changed_callback(self.name, self.current_value) def do_reset(self, sender): self.current_value = str(self.default) if self.optype == OptionType.BOOLEAN: newstate = True if self.current_value == "True" else False self.control.state = newstate elif self.optype == OptionType.INT: edit_text = self.current_value or "" self.control.set_edit_text(edit_text) elif self.optype == OptionType.STRING: edit_text = self.current_value or "" self.control.value = edit_text def update(self): pass
class OptionWidget(WidgetWrap): def __init__(self, name, optype, description, default, current_value=None, value_changed_callback=None): self.name = name self.optype = OptionType.__members__[optype.upper()] self.description = description self.default = default self.current_value = current_value or default w = self.build_widgets() self.value_changed_callback = value_changed_callback super().__init__(w) self.update() def selectable(self): return True def build_widgets(self): desc_text = Text(["\n", strip_solo_dots(self.description)]) self.reset_button = PlainButton("Reset to Default", self.do_reset) if self.optype == OptionType.BOOLEAN: self.control = CheckBox('', state=bool(self.current_value)) self.wrapped_control = self.control elif self.optype == OptionType.INT: self.control = IntEdit(default=self.current_value) self.wrapped_control = Color.string_input( self.control, focus_map='string_input focus') elif self.optype == OptionType.STRING: edit_text = self.current_value or "" self.control = StringEditor(edit_text=edit_text) self.wrapped_control = Color.string_input( self.control, focus_map='string_input focus') else: raise Exception("Unknown option type") self.control_columns = Columns( [ ('pack', Text("{}:".format(self.name), align='right')), (80, self.wrapped_control) ], dividechars=1 ) if self.optype == OptionType.STRING: connect_signal(self.control._edit, 'change', self.handle_value_changed) else: connect_signal(self.control, 'change', self.handle_value_changed) button_grid = GridFlow([ Color.button_secondary(self.reset_button, focus_map='button_secondary focus')], 36, 1, 0, 'right') return Pile([Padding.line_break(""), Padding.left(self.control_columns, left=1), Padding.left(desc_text, left=2), button_grid]) def handle_value_changed(self, sender, value): self.current_value = value if self.optype == OptionType.INT: v = value if value not in ['', '-']: v = int(value) self.value_changed_callback(self.name, v) else: self.value_changed_callback(self.name, self.current_value) def do_reset(self, sender): self.current_value = str(self.default) if self.optype == OptionType.BOOLEAN: newstate = True if self.current_value == "True" else False self.control.state = newstate elif self.optype == OptionType.INT: edit_text = self.current_value or "" self.control.set_edit_text(edit_text) elif self.optype == OptionType.STRING: edit_text = self.current_value or "" self.control.value = edit_text def update(self): pass