comparison browser_side/dialog.py @ 253:19153af4f327

browser_side: isolate the "add contact group" panel in a re-usable class: - also prevent from adding a new group twice by handling an internal list
author souliane <souliane@mailoo.org>
date Sat, 09 Nov 2013 08:53:03 +0100
parents a565ce2facc0
children da0487f0a2e7
comparison
equal deleted inserted replaced
252:b77940d8a9bf 253:19153af4f327
221 221
222 def onKeyPress(self, sender, keycode, modifiers): 222 def onKeyPress(self, sender, keycode, modifiers):
223 if self.enter_cb and keycode == KEY_ENTER: 223 if self.enter_cb and keycode == KEY_ENTER:
224 self.enter_cb(self) 224 self.enter_cb(self)
225 225
226
226 class GroupSelector(DialogBox): 227 class GroupSelector(DialogBox):
227 228
228 def __init__(self, top_widgets, initial_groups, selected_groups, 229 def __init__(self, top_widgets, initial_groups, selected_groups,
229 ok_title="OK", ok_cb=None, cancel_cb=None): 230 ok_title="OK", ok_cb=None, cancel_cb=None):
230 DialogBox.__init__(self, centered = True) 231 DialogBox.__init__(self, centered=True)
231 main_panel = VerticalPanel() 232 main_panel = VerticalPanel()
232 self.ok_cb = ok_cb 233 self.ok_cb = ok_cb
233 self.cancel_cb = cancel_cb 234 self.cancel_cb = cancel_cb
234 235
235 for wid in top_widgets: 236 for wid in top_widgets:
241 self.list_box.setVisibleItemCount(5) 242 self.list_box.setVisibleItemCount(5)
242 self.setAvailableGroups(initial_groups) 243 self.setAvailableGroups(initial_groups)
243 self.setGroupsSelected(selected_groups) 244 self.setGroupsSelected(selected_groups)
244 main_panel.add(self.list_box) 245 main_panel.add(self.list_box)
245 246
246 add_group_panel = HorizontalPanel() 247 def cb(text):
247 add_group_lb = Label('Add group:') 248 self.list_box.addItem(text)
248 add_group_tb = ExtTextBox(enter_cb = self.onGroupInput) 249 self.list_box.setItemSelected(self.list_box.getItemCount() - 1, "selected")
249 add_group_bt = Button("add", lambda sender: self.onGroupInput(add_group_tb)) 250
250 add_group_panel.add(add_group_lb) 251 main_panel.add(AddGroupPanel(initial_groups, cb))
251 add_group_panel.add(add_group_tb)
252 add_group_panel.add(add_group_bt)
253 main_panel.add(add_group_panel)
254 252
255 button_panel = HorizontalPanel() 253 button_panel = HorizontalPanel()
256 button_panel.addStyleName("marginAuto") 254 button_panel.addStyleName("marginAuto")
257 button_panel.add(Button(ok_title, self.onOK)) 255 button_panel.add(Button(ok_title, self.onOK))
258 button_panel.add(Button("Cancel", self.onCancel)) 256 button_panel.add(Button("Cancel", self.onCancel))
282 def onCancel(self, sender): 280 def onCancel(self, sender):
283 self.hide() 281 self.hide()
284 if self.cancel_cb: 282 if self.cancel_cb:
285 self.cancel_cb(self) 283 self.cancel_cb(self)
286 284
285
286 class AddGroupPanel(HorizontalPanel):
287 def __init__(self, groups, cb=None):
288 """
289 @param groups: list of the already existing groups
290 """
291 HorizontalPanel.__init__(self)
292 self.groups = groups
293 self.add(Label('Add group:'))
294 textbox = ExtTextBox(enter_cb=self.onGroupInput)
295 self.add(textbox)
296 self.add(Button("add", lambda sender: self.onGroupInput(textbox)))
297 self.cb = cb
298
287 def onGroupInput(self, sender): 299 def onGroupInput(self, sender):
288 text = sender.getText() 300 text = sender.getText()
289 for index in xrange(0, self.list_box.getItemCount()): 301 for group in self.groups:
290 if text == self.list_box.getValue(index): 302 if text == group:
291 Window.alert("The group '%s' already exists." % text) 303 Window.alert("The group '%s' already exists." % text)
292 return 304 return
293 for pattern in FORBIDDEN_PATTERNS_IN_GROUP: 305 for pattern in FORBIDDEN_PATTERNS_IN_GROUP:
294 if pattern in text: 306 if pattern in text:
295 Window.alert("The pattern '%s' is not allowed in group names." % pattern) 307 Window.alert("The pattern '%s' is not allowed in group names." % pattern)
296 return 308 return
297 self.list_box.addItem(text)
298 sender.setText('') 309 sender.setText('')
299 self.list_box.setItemSelected(self.list_box.getItemCount()-1, "selected") 310 self.groups.append(text)
311 if self.cb is not None:
312 self.cb(text)
300 313
301 314
302 class WheelTextBox(TextBox, MouseWheelHandler): 315 class WheelTextBox(TextBox, MouseWheelHandler):
303 316
304 def __init__(self, *args, **kwargs): 317 def __init__(self, *args, **kwargs):