tuikit/pager.py
author Radek Brich <radek.brich@devl.cz>
Sat, 15 Mar 2014 11:05:12 +0100
changeset 83 ebe732b9ef19
parent 46 2b43a7f38c34
permissions -rw-r--r--
Refactor Driver, CursesDriver: init, close, draw.

# -*- coding: utf-8 -*-

import logging

from tuikit.container import Container
from tuikit.layout import VerticalLayout, HorizontalLayout
from tuikit.button import Button


class Pager(Container):
    '''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)

        # selected child (only one visible)
        self.selected = None

        self.layout = VerticalLayout()

        self.buttons = Container(20, 1)
        self.buttons.sizereq.h = 1
        self.buttons.layout = HorizontalLayout(homogeneous=True, spacing=1)
        Container.add(self, self.buttons)

    def add(self, widget, **kw):
        Container.add(self, widget, expand=True, fill=True)

        if self.selected is None:
            self.selected = widget
            widget.hidden = False
        else:
            widget.hidden = True

        btn = Button(kw['title'])
        btn.add_handler('click', lambda: self.select(widget))
        self.buttons.add(btn, fill=True)

    def select(self, child):
        log = logging.getLogger('tuikit')
        log.debug('Pager: select %r', child)

        self.selected.hidden = True
        child.hidden = False
        self.selected = child

        self.emit('resize')