comparison src/browser/sat_browser/dialog.py @ 516:1af112b97e45

browser_side: PromptDialog can display several input fields
author souliane <souliane@mailoo.org>
date Tue, 02 Sep 2014 21:05:32 +0200
parents 97c72fe4a5f2
children d41e850b31b9
comparison
equal deleted inserted replaced
515:da690ef8019e 516:1af112b97e45
246 class GenericConfirmDialog(DialogBox): 246 class GenericConfirmDialog(DialogBox):
247 247
248 def __init__(self, widgets, callback, title='Confirmation', prompt=None, **kwargs): 248 def __init__(self, widgets, callback, title='Confirmation', prompt=None, **kwargs):
249 """ 249 """
250 Dialog to confirm an action 250 Dialog to confirm an action
251 @param widgets: widgets to attach 251 @param widgets (list[Widget]): widgets to attach
252 @param callback: method to call when a button is clicked 252 @param callback: method to call when a button is clicked
253 @param title: title of the dialog 253 @param title: title of the dialog
254 @param prompt: textbox from which to retrieve the string value to be passed to the callback when 254 @param prompt (TextBox, list[TextBox]): input widgets from which to retrieve
255 OK button is pressed. If None, OK button will return "True". Cancel button always returns "False". 255 the string value(s) to be passed to the callback when OK button is pressed.
256 If None, OK button will return "True". Cancel button always returns "False".
256 """ 257 """
257 self.callback = callback 258 self.callback = callback
258 DialogBox.__init__(self, centered=True, **kwargs) 259 DialogBox.__init__(self, centered=True, **kwargs)
260
261 if prompt is None:
262 prompt = []
263 elif isinstance(prompt, TextBox):
264 prompt = [prompt]
259 265
260 content = VerticalPanel() 266 content = VerticalPanel()
261 content.setWidth('100%') 267 content.setWidth('100%')
262 for wid in widgets: 268 for wid in widgets:
263 content.add(wid) 269 content.add(wid)
264 if wid == prompt: 270 if wid in prompt:
265 wid.setWidth('100%') 271 wid.setWidth('100%')
266 button_panel = HorizontalPanel() 272 button_panel = HorizontalPanel()
267 button_panel.addStyleName("marginAuto") 273 button_panel.addStyleName("marginAuto")
268 self.confirm_button = Button("OK", self.onConfirm) 274 self.confirm_button = Button("OK", self.onConfirm)
269 button_panel.add(self.confirm_button) 275 button_panel.add(self.confirm_button)
274 self.setWidget(content) 280 self.setWidget(content)
275 self.prompt = prompt 281 self.prompt = prompt
276 282
277 def onConfirm(self, sender): 283 def onConfirm(self, sender):
278 self.hide() 284 self.hide()
279 self.callback(self.prompt.getText() if self.prompt else True) 285 result = [box.getText() for box in self.prompt] if self.prompt else [True]
286 self.callback(*result)
280 287
281 def onCancel(self, sender): 288 def onCancel(self, sender):
282 self.hide() 289 self.hide()
283 self.callback(False) 290 self.callback(False)
284 291
285 def show(self): 292 def show(self):
286 DialogBox.show(self) 293 DialogBox.show(self)
287 if self.prompt: 294 if self.prompt:
288 self.prompt.setFocus(True) 295 self.prompt[0].setFocus(True)
289 296
290 297
291 class ConfirmDialog(GenericConfirmDialog): 298 class ConfirmDialog(GenericConfirmDialog):
292 299
293 def __init__(self, callback, text='Are you sure ?', title='Confirmation', **kwargs): 300 def __init__(self, callback, text='Are you sure ?', title='Confirmation', **kwargs):
338 GenericDialog.__init__(self, title, HTML(body), callback, options, **kwargs) 345 GenericDialog.__init__(self, title, HTML(body), callback, options, **kwargs)
339 346
340 347
341 class PromptDialog(GenericConfirmDialog): 348 class PromptDialog(GenericConfirmDialog):
342 349
343 def __init__(self, callback, text='', title='User input', **kwargs): 350 def __init__(self, callback, textes=None, values=None, title='User input', **kwargs):
344 prompt = TextBox() 351 """Prompt the user for one or more input(s).
345 prompt.setText(text) 352
346 GenericConfirmDialog.__init__(self, [prompt], callback, title, prompt, **kwargs) 353 @param callback (callable): method to call when clicking OK
354 @param textes (str, list[str]): HTML textes to display before the inputs
355 @param values (str, list[str]): default values for each input
356 @param title (str): dialog title
357 """
358 if textes is None:
359 textes = [''] # display a single input without any description
360 elif not isinstance(textes, list):
361 textes = [textes] # allow to pass a single string instead of a list
362 if values is None:
363 values = []
364 elif not isinstance(values, list):
365 values = [values] # allow to pass a single string instead of a list
366 all_widgets = []
367 prompt_widgets = []
368 for count in xrange(len(textes)):
369 all_widgets.append(HTML(textes[count]))
370 prompt = TextBox()
371 if len(values) > count:
372 prompt.setText(values[count])
373 all_widgets.append(prompt)
374 prompt_widgets.append(prompt)
375
376 GenericConfirmDialog.__init__(self, all_widgets, callback, title, prompt_widgets, **kwargs)
347 377
348 378
349 class PopupPanelWrapper(PopupPanel): 379 class PopupPanelWrapper(PopupPanel):
350 """This wrapper catch Escape event to avoid request cancellation by Firefox""" 380 """This wrapper catch Escape event to avoid request cancellation by Firefox"""
351 381