changeset 516:1af112b97e45

browser_side: PromptDialog can display several input fields
author souliane <souliane@mailoo.org>
date Tue, 02 Sep 2014 21:05:32 +0200 (2014-09-02)
parents da690ef8019e
children 85699d18921f
files src/browser/sat_browser/dialog.py
diffstat 1 files changed, 40 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/browser/sat_browser/dialog.py	Tue Aug 26 21:25:01 2014 +0200
+++ b/src/browser/sat_browser/dialog.py	Tue Sep 02 21:05:32 2014 +0200
@@ -248,20 +248,26 @@
     def __init__(self, widgets, callback, title='Confirmation', prompt=None, **kwargs):
         """
         Dialog to confirm an action
-        @param widgets: widgets to attach
+        @param widgets (list[Widget]): widgets to attach
         @param callback: method to call when a button is clicked
         @param title: title of the dialog
-        @param prompt: textbox from which to retrieve the string value to be passed to the callback when
-        OK button is pressed. If None, OK button will return "True". Cancel button always returns "False".
+        @param prompt (TextBox, list[TextBox]): input widgets from which to retrieve
+        the string value(s) to be passed to the callback when OK button is pressed.
+        If None, OK button will return "True". Cancel button always returns "False".
         """
         self.callback = callback
         DialogBox.__init__(self, centered=True, **kwargs)
 
+        if prompt is None:
+            prompt = []
+        elif isinstance(prompt, TextBox):
+            prompt = [prompt]
+
         content = VerticalPanel()
         content.setWidth('100%')
         for wid in widgets:
             content.add(wid)
-            if wid == prompt:
+            if wid in prompt:
                 wid.setWidth('100%')
         button_panel = HorizontalPanel()
         button_panel.addStyleName("marginAuto")
@@ -276,7 +282,8 @@
 
     def onConfirm(self, sender):
         self.hide()
-        self.callback(self.prompt.getText() if self.prompt else True)
+        result = [box.getText() for box in self.prompt] if self.prompt else [True]
+        self.callback(*result)
 
     def onCancel(self, sender):
         self.hide()
@@ -285,7 +292,7 @@
     def show(self):
         DialogBox.show(self)
         if self.prompt:
-            self.prompt.setFocus(True)
+            self.prompt[0].setFocus(True)
 
 
 class ConfirmDialog(GenericConfirmDialog):
@@ -340,10 +347,33 @@
 
 class PromptDialog(GenericConfirmDialog):
 
-    def __init__(self, callback, text='', title='User input', **kwargs):
-        prompt = TextBox()
-        prompt.setText(text)
-        GenericConfirmDialog.__init__(self, [prompt], callback, title, prompt, **kwargs)
+    def __init__(self, callback, textes=None, values=None, title='User input', **kwargs):
+        """Prompt the user for one or more input(s).
+
+        @param callback (callable): method to call when clicking OK
+        @param textes (str, list[str]): HTML textes to display before the inputs
+        @param values (str, list[str]): default values for each input
+        @param title (str): dialog title
+        """
+        if textes is None:
+            textes = ['']  # display a single input without any description
+        elif not isinstance(textes, list):
+            textes = [textes]  # allow to pass a single string instead of a list
+        if values is None:
+            values = []
+        elif not isinstance(values, list):
+            values = [values]  # allow to pass a single string instead of a list
+        all_widgets = []
+        prompt_widgets = []
+        for count in xrange(len(textes)):
+            all_widgets.append(HTML(textes[count]))
+            prompt = TextBox()
+            if len(values) > count:
+                prompt.setText(values[count])
+            all_widgets.append(prompt)
+            prompt_widgets.append(prompt)
+
+        GenericConfirmDialog.__init__(self, all_widgets, callback, title, prompt_widgets, **kwargs)
 
 
 class PopupPanelWrapper(PopupPanel):