changeset 177:8f56238309d9

Primitivus: AdvancedEdit and Notification bar improved - AdvancedEdit now support a completion callback - Notification bar now show progressions
author Goffi <goffi@goffi.org>
date Mon, 16 Aug 2010 21:06:03 +0800
parents a50953ac6191
children bd24f2aed80c
files frontends/primitivus/custom_widgets.py
diffstat 1 files changed, 44 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/primitivus/custom_widgets.py	Thu Aug 12 23:09:31 2010 +0800
+++ b/frontends/primitivus/custom_widgets.py	Mon Aug 16 21:06:03 2010 +0800
@@ -58,6 +58,19 @@
     new behaviour: emit a 'click' signal when enter is pressed"""
     signals = urwid.Edit.signals + ['click']
 
+    def setCompletionMethod(self, callback):
+        """Define method called when completion is asked
+        @callback: method with 2 arguments:
+                    - the text to complete
+                    - if there was already a completion, a dict with
+                        - 'completed':last completion
+                        - 'completion_pos': cursor position where the completion starts 
+                        - 'position': last completion cursor position
+                      this dict must be used (and can be filled) to find next completion)
+                   and which return the full text completed"""
+        self.completion_cb = callback
+        self.completion_data = {}
+
     def keypress(self, size, key):
         #TODO: insert mode is not managed yet
         if key == 'ctrl a':
@@ -74,6 +87,25 @@
             self.set_edit_pos(pos)
         elif key == 'enter':
             self._emit('click')
+        elif key == 'shift tab':
+            try:
+                before = self.edit_text[:self.edit_pos]
+                if self.completion_data:
+                    if (not self.completion_data['completed']
+                        or self.completion_data['position'] != self.edit_pos
+                        or not before.endswith(self.completion_data['completed'])):
+                        self.completion_data.clear()
+                    else:
+                        before = before[:-len(self.completion_data['completed'])]
+                complet = self.completion_cb(before, self.completion_data)
+                self.completion_data['completed'] = complet[len(before):]
+                self.set_edit_text(complet+self.edit_text[self.edit_pos:])
+                self.set_edit_pos(len(complet))
+                self.completion_data['position'] = self.edit_pos
+                return
+            except AttributeError:
+                #No completion method defined
+                pass
         return super(AdvancedEdit, self).keypress(size, key) 
        
 
@@ -372,9 +404,10 @@
 
     def __init__(self):
         self.waitNotifs = urwid.Text('')
-        self.message = ClickableText('', default_attr='notifs', select_attr='notifs')
+        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])
+        self.progress = ClickableText('', default_attr='notifs')
+        self.columns = urwid.Columns([('fixed',6,self.waitNotifs),self.message,('fixed',4,self.progress)])
         urwid.WidgetWrap.__init__(self, urwid.AttrMap(self.columns,'notifs'))
         self.notifs = []
     
@@ -383,6 +416,14 @@
         self.waitNotifs.set_text("(%i)" % len(self.notifs) if self.notifs else '')
         self._emit('change')
 
+    def setProgress(self,percentage):
+        """Define the progression to show on the right side of the bar"""
+        if percentage == None:
+            self.progress.set_text('')
+        else:
+            self.progress.set_text('%02i%%' % percentage)
+        self._emit('change')
+
     def addPopUp(self, pop_up_widget):
         """Add a popup to the waiting queue"""
         self.notifs.append(('popup',pop_up_widget))
@@ -431,7 +472,7 @@
 
     def canHide(self):
         """Return True if there is now important information to show"""
-        return self.isQueueEmpty() and not self.message.get_text()
+        return self.isQueueEmpty() and not self.message.get_text() and not self.progress.get_text()
 
 
 class MenuBox(urwid.WidgetWrap):