Mercurial > libervia-web
comparison src/browser/sat_browser/dialog.py @ 625:ed9cd20260ff frontends_multi_profiles
browser_side (PromptDialog): rename prompt attribute to prompt_widgets + related changes
author | souliane <souliane@mailoo.org> |
---|---|
date | Sun, 22 Feb 2015 21:55:27 +0100 |
parents | d41e850b31b9 |
children | ba2555c29c84 |
comparison
equal
deleted
inserted
replaced
624:9092e624bb27 | 625:ed9cd20260ff |
---|---|
243 DialogBox.hide(self, autoClosed=True) | 243 DialogBox.hide(self, autoClosed=True) |
244 | 244 |
245 | 245 |
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_widgets=None, **kwargs): |
249 """ | 249 """ |
250 Dialog to confirm an action | 250 Dialog to confirm an action |
251 @param widgets (list[Widget]): 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 (callable): method to call when a button is pressed, |
253 with the following arguments: | |
254 - result (bool): set to True if the dialog has been confirmed | |
255 - *args: a list of unicode (the values for the prompt_widgets) | |
253 @param title: title of the dialog | 256 @param title: title of the dialog |
254 @param prompt (TextBox, list[TextBox]): input widgets from which to retrieve | 257 @param prompt_widgets (list[TextBox]): input widgets from which to retrieve |
255 the string value(s) to be passed to the callback when OK button is pressed. | 258 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". | 259 If None, OK button will return "True". Cancel button always returns "False". |
257 """ | 260 """ |
258 self.callback = callback | 261 self.callback = callback |
259 added_style = kwargs.pop('AddStyleName', None) | 262 added_style = kwargs.pop('AddStyleName', None) |
260 DialogBox.__init__(self, centered=True, **kwargs) | 263 DialogBox.__init__(self, centered=True, **kwargs) |
261 if added_style: | 264 if added_style: |
262 self.addStyleName(added_style) | 265 self.addStyleName(added_style) |
263 | 266 |
264 if prompt is None: | 267 if prompt_widgets is None: |
265 prompt = [] | 268 prompt_widgets = [] |
266 elif isinstance(prompt, TextBox): | |
267 prompt = [prompt] | |
268 | 269 |
269 content = VerticalPanel() | 270 content = VerticalPanel() |
270 content.setWidth('100%') | 271 content.setWidth('100%') |
271 for wid in widgets: | 272 for wid in widgets: |
272 content.add(wid) | 273 content.add(wid) |
273 if wid in prompt: | 274 if wid in prompt_widgets: |
274 wid.setWidth('100%') | 275 wid.setWidth('100%') |
275 button_panel = HorizontalPanel() | 276 button_panel = HorizontalPanel() |
276 button_panel.addStyleName("marginAuto") | 277 button_panel.addStyleName("marginAuto") |
277 self.confirm_button = Button("OK", self.onConfirm) | 278 self.confirm_button = Button("OK", self.onConfirm) |
278 button_panel.add(self.confirm_button) | 279 button_panel.add(self.confirm_button) |
279 self.cancel_button = Button("Cancel", self.onCancel) | 280 self.cancel_button = Button("Cancel", self.onCancel) |
280 button_panel.add(self.cancel_button) | 281 button_panel.add(self.cancel_button) |
281 content.add(button_panel) | 282 content.add(button_panel) |
282 self.setHTML(title) | 283 self.setHTML(title) |
283 self.setWidget(content) | 284 self.setWidget(content) |
284 self.prompt = prompt | 285 self.prompt_widgets = prompt_widgets |
285 | 286 |
286 def onConfirm(self, sender): | 287 def onConfirm(self, sender): |
287 self.hide() | 288 self.hide() |
288 result = [box.getText() for box in self.prompt] if self.prompt else [True] | 289 result = [True] |
290 result.extend([box.getText() for box in self.prompt_widgets]) | |
289 self.callback(*result) | 291 self.callback(*result) |
290 | 292 |
291 def onCancel(self, sender): | 293 def onCancel(self, sender): |
292 self.hide() | 294 self.hide() |
293 self.callback(False) | 295 self.callback(False) |
294 | 296 |
295 def show(self): | 297 def show(self): |
296 DialogBox.show(self) | 298 DialogBox.show(self) |
297 if self.prompt: | 299 if self.prompt_widgets: |
298 self.prompt[0].setFocus(True) | 300 self.prompt_widgets[0].setFocus(True) |
299 | 301 |
300 | 302 |
301 class ConfirmDialog(GenericConfirmDialog): | 303 class ConfirmDialog(GenericConfirmDialog): |
302 | 304 |
303 def __init__(self, callback, text='Are you sure ?', title='Confirmation', **kwargs): | 305 def __init__(self, callback, text='Are you sure ?', title='Confirmation', **kwargs): |
326 _body.setSize('100%', '100%') | 328 _body.setSize('100%', '100%') |
327 _body.setSpacing(4) | 329 _body.setSpacing(4) |
328 _body.add(main_widget) | 330 _body.add(main_widget) |
329 _body.setCellWidth(main_widget, '100%') | 331 _body.setCellWidth(main_widget, '100%') |
330 _body.setCellHeight(main_widget, '100%') | 332 _body.setCellHeight(main_widget, '100%') |
331 if not 'NO_CLOSE' in options: | 333 if 'NO_CLOSE' not in options: |
332 _close_button = Button("Close", self.onClose) | 334 _close_button = Button("Close", self.onClose) |
333 _body.add(_close_button) | 335 _body.add(_close_button) |
334 _body.setCellHorizontalAlignment(_close_button, HasAlignment.ALIGN_CENTER) | 336 _body.setCellHorizontalAlignment(_close_button, HasAlignment.ALIGN_CENTER) |
335 self.setHTML(title) | 337 self.setHTML(title) |
336 self.setWidget(_body) | 338 self.setWidget(_body) |
355 class PromptDialog(GenericConfirmDialog): | 357 class PromptDialog(GenericConfirmDialog): |
356 | 358 |
357 def __init__(self, callback, textes=None, values=None, title='User input', **kwargs): | 359 def __init__(self, callback, textes=None, values=None, title='User input', **kwargs): |
358 """Prompt the user for one or more input(s). | 360 """Prompt the user for one or more input(s). |
359 | 361 |
360 @param callback (callable): method to call when clicking OK | 362 @param callback (callable): method to call when a button is pressed, |
361 @param textes (str, list[str]): HTML textes to display before the inputs | 363 with the following arguments: |
362 @param values (str, list[str]): default values for each input | 364 - result (bool): set to True if the dialog has been confirmed |
363 @param title (str): dialog title | 365 - *args: a list of unicode (the values entered by the user) |
366 @param textes (list[unicode]): HTML textes to display before the inputs | |
367 @param values (list[unicode]): default values for each input | |
368 @param title (unicode): dialog title | |
364 """ | 369 """ |
365 if textes is None: | 370 if textes is None: |
366 textes = [''] # display a single input without any description | 371 textes = [''] # display a single input without any description |
367 elif not isinstance(textes, list): | |
368 textes = [textes] # allow to pass a single string instead of a list | |
369 if values is None: | 372 if values is None: |
370 values = [] | 373 values = [] |
371 elif not isinstance(values, list): | |
372 values = [values] # allow to pass a single string instead of a list | |
373 all_widgets = [] | 374 all_widgets = [] |
374 prompt_widgets = [] | 375 prompt_widgets = [] |
375 for count in xrange(len(textes)): | 376 for count in xrange(len(textes)): |
376 all_widgets.append(HTML(textes[count])) | 377 all_widgets.append(HTML(textes[count])) |
377 prompt = TextBox() | 378 prompt = TextBox() |
386 class PopupPanelWrapper(PopupPanel): | 387 class PopupPanelWrapper(PopupPanel): |
387 """This wrapper catch Escape event to avoid request cancellation by Firefox""" | 388 """This wrapper catch Escape event to avoid request cancellation by Firefox""" |
388 | 389 |
389 def onEventPreview(self, event): | 390 def onEventPreview(self, event): |
390 if event.type in ["keydown", "keypress", "keyup"] and event.keyCode == KEY_ESCAPE: | 391 if event.type in ["keydown", "keypress", "keyup"] and event.keyCode == KEY_ESCAPE: |
391 #needed to prevent request cancellation in Firefox | 392 # needed to prevent request cancellation in Firefox |
392 event.preventDefault() | 393 event.preventDefault() |
393 return PopupPanel.onEventPreview(self, event) | 394 return PopupPanel.onEventPreview(self, event) |
394 | 395 |
395 | 396 |
396 class ExtTextBox(TextBox): | 397 class ExtTextBox(TextBox): |