tuikit/pager.py
author Radek Brich <radek.brich@devl.cz>
Sun, 02 Oct 2011 23:32:35 +0200
changeset 15 c55b4749e562
child 17 5be7cc43402e
permissions -rw-r--r--
Add Pager.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
import logging
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
from .container import Container
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
from .layout import VerticalLayout, HorizontalLayout
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
from .button import Button
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
class Pager(Container):
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
    '''Only one of children is visible at the time.'''
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
    def __init__(self, width=20, height=20):
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
        Container.__init__(self, width, height)
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
        # selected child (only one visible)
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
        self.selected = None
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
        
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
        vert = VerticalLayout()
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
        self.layout(vert)
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
        
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
        self.buttons = Container(20, 1)
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
        Container.add(self, self.buttons)
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
        horz = HorizontalLayout()
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
        self.buttons.layout(horz)
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
                
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
    def add(self, widget, **kw):
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
        Container.add(self, widget, **kw)
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
        
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
        if self.selected is None:
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
            self.selected = widget
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
            widget.hidden = False
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
        else:
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
            widget.hidden = True
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
        
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
        btn = Button(kw['title'])
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
        btn.connect('click', lambda: self.select(widget))
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
        self.buttons.add(btn)
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
    def select(self, child):
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
        log = logging.getLogger('tuikit')
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
        log.debug('Pager: select %r', child)
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
        self.selected.hidden = True
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
        child.hidden = False
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
        self.selected = child
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
        self.resize()
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48