Commit ea5eb65d authored by GUILLOU Perceval's avatar GUILLOU Perceval
Browse files

add choices

parent d032e28f
...@@ -245,21 +245,26 @@ class UserInput: ...@@ -245,21 +245,26 @@ class UserInput:
class AskYesNo(UserInput): class AskYesNo(UserInput):
def __init__(self, title="Question", label="", defval=False, validator=None ): def __init__(self, title="Question", label="", defval=False, validator=None ):
super(AskYesNo, self).__init__( title, label, defval, validator ) super().__init__( title, label, defval, validator )
class AskStr(UserInput): class AskStr(UserInput):
def __init__(self, title="Enter a string", label="word", defval="", validator=None ): def __init__(self, title="Enter a string", label="word", defval="", validator=None ):
super(AskStr, self).__init__( title, label, defval, validator ) super().__init__( title, label, defval, validator )
class AskInt(UserInput): class AskInt(UserInput):
def __init__(self, title="Enter a integer", label="value", defval=0, mini=None, maxi=None, validator=None ): def __init__(self, title="Enter a integer", label="value", defval=0, mini=None, maxi=None, validator=None ):
super(AskInt, self).__init__( title, label, defval, validator ) super().__init__( title, label, defval, validator )
self.mini = mini self.mini = mini
self.maxi = maxi self.maxi = maxi
class AskChoice(UserInput):
def __init__(self, title="Select one value", label="select", choices=[], defval=0, validator=None ):
super().__init__( title, label, defval, validator )
self.choices = choices
class _BaseWidget:
class DlgWidget:
""" Dialog widget base class """ """ Dialog widget base class """
def __init__(self, label=""): def __init__(self, label=""):
self.label = label self.label = label
...@@ -271,44 +276,68 @@ class _BaseWidget: ...@@ -271,44 +276,68 @@ class _BaseWidget:
def get_result(self): def get_result(self):
return None return None
#class DlgMsg(_BaseWidget):
# def __init__(self, label="This is a message"):
# super().__init__(self, label=label)
#
# def build(self):
# self.body = Label(text=self.label, dont_extend_height=True)
#
# def get_result(self):
# return True
class DlgInput(_BaseWidget): class InputWidget(DlgWidget):
def __init__(self, label="Please, enter a value"): def __init__(self, label="Please, enter a value"):
super().__init__(label) if not isinstance(label,(tuple,list)):
label = [label,]
self.label = label self.label = label
self.build()
def build(self): super().__init__(label)
wlabel = Label(text=self.label, dont_extend_height=True, dont_extend_width=True)
self.textfield = TextArea( def build(self):
self.textfields = []
hlist = []
for label in self.label:
wlabel = Label(text=label+" ", dont_extend_height=True, dont_extend_width=True)
textfield = TextArea(
multiline=False, multiline=False,
#password=password, #password=password,
#completer=completer, #completer=completer,
accept_handler=self.accept) accept_handler=self.accept)
self.body = VSplit( [wlabel, self.textfield] ) self.textfields.append(textfield)
hlist.append(VSplit( [wlabel, textfield] ))
self.body = HSplit( hlist )
def get_result(self): def get_result(self):
return self.textfield.text return [textfield.text for textfield in self.textfields]
def accept(self, buf): def accept(self, buf):
return True return True
class ChoiceWidget(DlgWidget):
def __init__(self, choices=[], label="Please, select one value"):
self.label = label
self.choices = choices
super().__init__(label)
def build(self):
wlabel = Label(text=self.label+" ", dont_extend_height=True, dont_extend_width=True)
self.radio_list = RadioList(self.choices)
self.body = HSplit( [wlabel, self.radio_list] )
def get_result(self):
return self.radio_list.current_value
def accept(self, buf):
return True
def get_dlg_widget(user_input): def get_dlg_widget(user_input):
if isinstance(user_input, ( AskStr, AskInt ) ): if isinstance(user_input, ( AskStr, AskInt ) ):
return DlgInput(user_input.label) return InputWidget(user_input.label)
elif isinstance(user_input, AskChoice ):
return ChoiceWidget(user_input.choices, user_input.label)
else: else:
raise NotImplementedError raise NotImplementedError
...@@ -377,7 +406,7 @@ def display( user_input ): ...@@ -377,7 +406,7 @@ def display( user_input ):
def test_multi_dialog( title="multi_dialog" ): def test_multi_dialog( title="multi_dialog" ):
dlg = BlissDialog( [ [AskStr(label="word1.1"), AskStr(label="word1.2")], [AskStr(label="word2.1"), AskStr(label="word2.2")] ] , title=title ) dlg = BlissDialog( [ [AskStr(label=["word1.1.1","word1.1.2","word1.1.3"]), AskChoice(choices=[("A","A"),("B","B"),("C","C")],label="choice1.2")], [AskStr(label="word2.1"), AskStr(label="word2.2")] ] , title=title )
ans = dlg.show() ans = dlg.show()
print(f"results = {ans}") print(f"results = {ans}")
return ans return ans
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment