Add setup script. Add Checkbox widget + demo. Updates.
authorRadek Brich <radek.brich@devl.cz>
Thu, 13 Dec 2012 10:01:32 +0100
changeset 30 05500124d7fb
parent 29 c0cdef06fd16
child 31 629d5edb1602
Add setup script. Add Checkbox widget + demo. Updates.
.hgignore
demo_checkbox.py
setup.py
tuikit/__init__.py
tuikit/application.py
tuikit/button.py
tuikit/checkbox.py
tuikit/combobox.py
tuikit/editfield.py
tuikit/pager.py
tuikit/treeview.py
--- a/.hgignore	Mon Feb 20 18:15:13 2012 +0100
+++ b/.hgignore	Thu Dec 13 10:01:32 2012 +0100
@@ -1,8 +1,8 @@
 .*~
-tuikit/.*\.pyc
-docs/_build
-tuikit\.log
-old
+^tuikit/.*\.pyc
+^docs/_build
+^tuikit\.log
+^build
 \.project
 \.pydevproject
 \.settings
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/demo_checkbox.py	Thu Dec 13 10:01:32 2012 +0100
@@ -0,0 +1,33 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
+import locale
+locale.setlocale(locale.LC_ALL, '')
+
+from tuikit import *
+
+
+class MyApplication(Application):
+    def __init__(self):
+        Application.__init__(self)
+        self.top.connect('keypress', self.globalkeypress)
+
+        vert = VerticalLayout(homogeneous=False)
+        self.top.layout(vert)
+        
+        combo = ComboBox(items=['abc', 'xyz'])
+        self.top.add(combo)
+        
+        for i in range(10):
+            cbox = Checkbox('checkbox ' + str(i))
+            self.top.add(cbox)
+        
+    def globalkeypress(self, keyname, char):
+        if keyname == 'escape' or char == 'q':
+            self.terminate()
+
+
+if __name__ == '__main__':
+    app = MyApplication()
+    app.start()
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/setup.py	Thu Dec 13 10:01:32 2012 +0100
@@ -0,0 +1,15 @@
+#!/usr/bin/env python
+
+from distutils.core import setup
+
+setup(
+    name='tuikit',
+    version='0.1',
+    description='Curses UI toolkit',
+    author='Radek Brich',
+    author_email='radek.brich@devl.cz',
+    url='http://hg.devl.cz/tuikit/',
+    keywords=['curses', 'tui', 'toolkit'],
+    packages=['tuikit'],
+    )
+
--- a/tuikit/__init__.py	Mon Feb 20 18:15:13 2012 +0100
+++ b/tuikit/__init__.py	Thu Dec 13 10:01:32 2012 +0100
@@ -2,6 +2,7 @@
 
 from tuikit.application import Application
 from tuikit.button import Button
+from tuikit.checkbox import Checkbox
 from tuikit.combobox import ComboBox
 from tuikit.common import Rect, Size, Coords
 from tuikit.container import Container
--- a/tuikit/application.py	Mon Feb 20 18:15:13 2012 +0100
+++ b/tuikit/application.py	Thu Dec 13 10:01:32 2012 +0100
@@ -142,6 +142,7 @@
         driver.setcolor('button-active',           'black on cyan')
         driver.setcolor('menu',                    'black on cyan')
         driver.setcolor('menu-active',             'white on cyan, bold')
+        driver.setcolor('combo:normal',            'black on green')
 
 
     def get_driver_instance(self, name):
--- a/tuikit/button.py	Mon Feb 20 18:15:13 2012 +0100
+++ b/tuikit/button.py	Thu Dec 13 10:01:32 2012 +0100
@@ -15,6 +15,13 @@
 
         #: Button label.
         self.label = label
+        #: Text or graphics to be added before label
+        self.prefix = '['
+        #: Text or graphics to be added after label
+        self.suffix = ']'
+        #: How should label be aligned if button has excess space - center | left | right
+        self.align = 'center'
+        
         self.bg = 'button'
         self.bghi = 'button-active'
         self.highlight = False
@@ -23,11 +30,6 @@
         self.sizereq.w = w
         self.sizereq.h = h
 
-        #: Left bracket graphic character.
-        self.lbracket = '['
-        #: Right bracket graphic character.
-        self.rbracket = ']'
-
         self.connect('draw', self.on_draw)
         self.connect('mousedown', self.on_mousedown)
         self.connect('mouseup', self.on_mouseup)
@@ -37,15 +39,23 @@
 
 
     def on_draw(self, screen, x, y):
-        l = (self.width - len(self.label)) // 2
+        pad = self.width - len(self.label) - len(self.prefix) - len(self.suffix)
+        lpad, rpad = self._divide_padding(pad)
         screen.pushcolor(self.getcolor())
-        screen.putch(x, y, self.lbracket)
-        for i in range(x+1, x+l):
-            screen.putch(i, y, ' ')
-        screen.puts(x + l, y, self.label)
-        for i in range(x+l+len(self.label), x+self.width-1):
-            screen.putch(i, y, ' ')
-        screen.putch(x + self.width - 1, y, self.rbracket)
+        # prefix
+        screen.puts(x, y, self.prefix)
+        pos = len(self.prefix)
+        # left pad
+        screen.puts(x + pos, y, ' ' * lpad)
+        pos += lpad
+        # label
+        screen.puts(x + pos, y, self.label)
+        pos += len(self.label)
+        # right pad
+        screen.puts(x + pos, y, ' ' * rpad)
+        pos += rpad
+        # suffix
+        screen.puts(x + pos, y, self.suffix)
         screen.popcolor()
 
 
@@ -72,3 +82,13 @@
             return self.bghi
         return self.bg
 
+    def _divide_padding(self, pad):
+        # default is 'left'
+        lpad, rpad = 0, pad
+        if self.align == 'center':
+            lpad = pad // 2
+            rpad = pad - lpad
+        elif self.align == 'right':
+            lpad, rpad = pad, 0
+        return lpad, rpad
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tuikit/checkbox.py	Thu Dec 13 10:01:32 2012 +0100
@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+
+from tuikit.button import Button
+
+
+class Checkbox(Button):
+    
+    '''Checkbox - button.'''
+    
+    def __init__(self, label=''):
+        Button.__init__(self, label)
+        
+        self.checked = False
+        
+        self.prefix = '[ ] '
+        self.suffix = ''
+        self.align = 'left'
+        self.bg = 'normal'
+        self.bghi = 'active'
+
+        self.connect('click', self.on_click)
+    
+    def on_click(self):
+        if self.checked:
+            self.checked = False
+            self.prefix = '[ ] '
+        else:
+            self.checked = True
+            self.prefix = '[x] '
+
--- a/tuikit/combobox.py	Mon Feb 20 18:15:13 2012 +0100
+++ b/tuikit/combobox.py	Thu Dec 13 10:01:32 2012 +0100
@@ -10,22 +10,24 @@
     def __init__(self, width=15, value='', items=[]):
         Container.__init__(self, width, 1)
         
+        self.colorprefix = 'combo:'
+        
         self._edit = EditField(width - 3, value)
         self.add(self._edit)
         
         self._btn = Button('v')
+        self._btn.prefix = ''
+        self._btn.suffix = ''
         self._btn.x = width - 3
         self._btn.width = 3
         self._btn.connect('click', self._on_btn_click)
-        self.add(self._edit)
+        self.add(self._btn)
         
         self._menu = Menu(items)
         self._menu.hide()
         self._menu.allowlayout = False
-#        self.top.add(self._menu)
-        
+        #self.top.add(self._menu)
     
     def _on_btn_click(self):
         pass
-    
-    
+
--- a/tuikit/editfield.py	Mon Feb 20 18:15:13 2012 +0100
+++ b/tuikit/editfield.py	Thu Dec 13 10:01:32 2012 +0100
@@ -30,6 +30,7 @@
 
 
     def on_draw(self, screen, x, y):
+        screen.pushcolor('normal')
         # draw value
         val = self.value + ' ' * self.tw         # add spaces to fill rest of field
         val = val[self.ofs : self.ofs + self.tw]  # cut value - begin from ofs, limit to tw chars
@@ -47,7 +48,7 @@
         screen.putch(x + self.width-1, y, c)
 
         self.cursor = (1 + self.pos - self.ofs, 0)
-
+        screen.popcolor()
 
     def on_keypress(self, keyname, char):
         handled = False
--- a/tuikit/pager.py	Mon Feb 20 18:15:13 2012 +0100
+++ b/tuikit/pager.py	Thu Dec 13 10:01:32 2012 +0100
@@ -8,7 +8,15 @@
 
 
 class Pager(Container):
-    '''Only one of children is visible at the time.'''
+    '''Only one of children is visible at the time.
+    
+    Pages are switched using buttons at top (tabs).
+    
+    page_one = Container()
+    pager.add(page_one, title="page one")
+    pager.select(page_one)
+    
+    '''
     def __init__(self, width=20, height=20):
         Container.__init__(self, width, height)
 
@@ -23,7 +31,7 @@
         Container.add(self, self.buttons)
         horz = HorizontalLayout(homogeneous=True, spacing=1)
         self.buttons.layout(horz)
-                
+        
     def add(self, widget, **kw):
         Container.add(self, widget, expand=True, fill=True)
         
--- a/tuikit/treeview.py	Mon Feb 20 18:15:13 2012 +0100
+++ b/tuikit/treeview.py	Thu Dec 13 10:01:32 2012 +0100
@@ -122,16 +122,24 @@
 
     def setmodel(self, value):
         if self._model:
-            self._model.disconnect('change', self.redraw)
+            self._model.disconnect('change', self.model_change)
         self._model = value
         if self._model:
-            self._model.connect('change', self.redraw)
+            self._model.connect('change', self.model_change)
             try:
                 self.cnode = self._model.root[0]
             except IndexError:
                 pass
+
+    model = property(getmodel, setmodel)
     
-    model = property(getmodel, setmodel)
+    def model_change(self):
+        if self.cnode is None:
+            try:
+                self.cnode = self._model.root[0]
+            except IndexError:
+                pass
+        self.redraw()
 
     def collapse(self, path, collapse=True):
         node = self._model.find(path)