comparison frontends/primitivus/custom_widgets.py @ 16:263fe4d067ad

Primitivus: notification bar, first draft - popup queue is now managed - notifications can auto-hide when nothing to show - ctrl-n show next notification Primitivus: ctrl-s allow to temporarily hide a popup Primitivus: cards in card_game now answer to mouse click Primitivus: notification is shown when invalid card is played in card_game Primitivus: SelectableText has now methods get_text and set_text
author Goffi <goffi@goffi.org>
date Wed, 04 Aug 2010 17:57:51 +0800
parents 8241b3157699
children 222aa33716ad
comparison
equal deleted inserted replaced
15:8241b3157699 16:263fe4d067ad
112 self.align = align 112 self.align = align
113 self.__selected=selected 113 self.__selected=selected
114 114
115 def getValue(self): 115 def getValue(self):
116 return self.text 116 return self.text
117
118 def get_text(self):
119 """for compatibility with urwid.Text"""
120 return self.getValue()
121
122 def set_text(self, text):
123 self.text=unicode(text)
124 self._invalidate()
117 125
118 def setAttribute(self, name, value): 126 def setAttribute(self, name, value):
119 """Change attribut used for rendering widget 127 """Change attribut used for rendering widget
120 @param name: one of 128 @param name: one of
121 -default: when not selected 129 -default: when not selected
356 height = min(list_size,self.max_height) or 1 364 height = min(list_size,self.max_height) or 1
357 return urwid.BoxAdapter(self.genericList, height) 365 return urwid.BoxAdapter(self.genericList, height)
358 366
359 ## MISC ## 367 ## MISC ##
360 368
369 class NotificationBar(urwid.WidgetWrap):
370 """Bar used to show misc information to user"""
371 signals = ['change']
372
373 def __init__(self):
374 self.waitNotifs = urwid.Text('')
375 self.message = ClickableText('', default_attr='notifs')
376 urwid.connect_signal(self.message, 'click', lambda wid: self.showNext())
377 self.columns = urwid.Columns([('fixed',6,self.waitNotifs),self.message])
378 urwid.WidgetWrap.__init__(self, urwid.AttrMap(self.columns,'notifs'))
379 self.notifs = []
380
381 def __modQueue(self):
382 """must be called each time the notifications queue is changed"""
383 self.waitNotifs.set_text("(%i)" % len(self.notifs) if self.notifs else '')
384 self._emit('change')
385
386 def addPopUp(self, pop_up_widget):
387 """Add a popup to the waiting queue"""
388 self.notifs.append(('popup',pop_up_widget))
389 self.__modQueue()
390
391 def addMessage(self, message):
392 "Add a message to the notificatio bar"
393 if not self.message.get_text():
394 self.message.set_text(message)
395 self._invalidate()
396 self._emit('change')
397 else:
398 self.notifs.append(('message',message))
399 self.__modQueue()
400
401 def showNext(self):
402 """Show next message if any, else delete current message"""
403 found = None
404 for notif in self.notifs:
405 if notif[0] == "message":
406 found = notif
407 break
408 if found:
409 self.notifs.remove(found)
410 self.message.set_text(found[1])
411 self.__modQueue()
412 else:
413 self.message.set_text('')
414 self._emit('change')
415
416 def getNextPopup(self):
417 """Return next pop-up and remove it from the queue
418 @return: pop-up or None if there is no more in the queue"""
419 ret = None
420 for notif in self.notifs:
421 if notif[0] == 'popup':
422 ret = notif[1]
423 break
424 if ret:
425 self.notifs.remove(notif)
426 self.__modQueue()
427 return ret
428
429 def isQueueEmpty(self):
430 return not bool(self.notifs)
431
432 def canHide(self):
433 """Return True if there is now important information to show"""
434 return self.isQueueEmpty() and not self.message.get_text()
435
436
361 class MenuBox(urwid.WidgetWrap): 437 class MenuBox(urwid.WidgetWrap):
362 """Show menu items of a category in a box""" 438 """Show menu items of a category in a box"""
363 signals = ['click'] 439 signals = ['click']
364 440
365 def __init__(self,parent,items): 441 def __init__(self,parent,items):