diff frontends/primitivus/custom_widgets.py @ 159:2fa58703f1b7

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 7fcb4f083686
children ae50b53ff868
line wrap: on
line diff
--- a/frontends/primitivus/custom_widgets.py	Wed Aug 04 17:53:20 2010 +0800
+++ b/frontends/primitivus/custom_widgets.py	Wed Aug 04 17:57:51 2010 +0800
@@ -115,6 +115,14 @@
     def getValue(self):
         return self.text
 
+    def get_text(self):
+        """for compatibility with urwid.Text"""
+        return self.getValue()
+
+    def set_text(self, text):
+        self.text=unicode(text)
+        self._invalidate()
+
     def setAttribute(self, name, value):
         """Change attribut used for rendering widget
         @param name: one of
@@ -358,6 +366,74 @@
 
 ## MISC ##
 
+class NotificationBar(urwid.WidgetWrap):
+    """Bar used to show misc information to user"""
+    signals = ['change']
+
+    def __init__(self):
+        self.waitNotifs = urwid.Text('')
+        self.message = ClickableText('', default_attr='notifs')
+        urwid.connect_signal(self.message, 'click', lambda wid: self.showNext())
+        self.columns = urwid.Columns([('fixed',6,self.waitNotifs),self.message])
+        urwid.WidgetWrap.__init__(self, urwid.AttrMap(self.columns,'notifs'))
+        self.notifs = []
+    
+    def __modQueue(self):
+        """must be called each time the notifications queue is changed"""
+        self.waitNotifs.set_text("(%i)" % len(self.notifs) if self.notifs else '')
+        self._emit('change')
+
+    def addPopUp(self, pop_up_widget):
+        """Add a popup to the waiting queue"""
+        self.notifs.append(('popup',pop_up_widget))
+        self.__modQueue()
+
+    def addMessage(self, message):
+        "Add a message to the notificatio bar"
+        if not self.message.get_text():
+            self.message.set_text(message)
+            self._invalidate()
+            self._emit('change')
+        else:
+            self.notifs.append(('message',message))
+            self.__modQueue()
+
+    def showNext(self):
+        """Show next message if any, else delete current message"""
+        found = None
+        for notif in self.notifs:
+            if notif[0] == "message":
+                found = notif
+                break
+        if found:
+            self.notifs.remove(found)
+            self.message.set_text(found[1])
+            self.__modQueue()
+        else:
+            self.message.set_text('')
+            self._emit('change')
+
+    def getNextPopup(self):
+        """Return next pop-up and remove it from the queue
+        @return: pop-up or None if there is no more in the queue"""
+        ret = None
+        for notif in self.notifs:
+            if notif[0] == 'popup':
+                ret = notif[1]
+                break
+        if ret:
+            self.notifs.remove(notif)
+            self.__modQueue()
+        return ret
+
+    def isQueueEmpty(self):
+        return not bool(self.notifs)
+
+    def canHide(self):
+        """Return True if there is now important information to show"""
+        return self.isQueueEmpty() and not self.message.get_text()
+
+
 class MenuBox(urwid.WidgetWrap):
     """Show menu items of a category in a box"""
     signals = ['click']