tuikit/pager.py
author Radek Brich <radek.brich@devl.cz>
Sun, 16 Dec 2012 20:49:54 +0100
changeset 34 e3beacd5e536
parent 32 088b92ffb119
child 40 5faa38c10b67
permissions -rw-r--r--
Update event propagation, keypress event, focusing.
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
18
e6c3a5ee91aa Eliminate relative imports.
Radek Brich <radek.brich@devl.cz>
parents: 17
diff changeset
     5
from tuikit.container import Container
e6c3a5ee91aa Eliminate relative imports.
Radek Brich <radek.brich@devl.cz>
parents: 17
diff changeset
     6
from tuikit.layout import VerticalLayout, HorizontalLayout
e6c3a5ee91aa Eliminate relative imports.
Radek Brich <radek.brich@devl.cz>
parents: 17
diff changeset
     7
from tuikit.button import Button
15
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):
30
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    11
    '''Only one of children is visible at the time.
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
    12
30
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    13
    Pages are switched using buttons at top (tabs).
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
    14
30
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    15
    page_one = Container()
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    16
    pager.add(page_one, title="page one")
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    17
    pager.select(page_one)
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
    18
30
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    19
    '''
15
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
    def __init__(self, width=20, height=20):
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
        Container.__init__(self, width, height)
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
        # selected child (only one visible)
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
        self.selected = None
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
    25
15
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
        vert = VerticalLayout()
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
        self.layout(vert)
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
    28
15
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
        self.buttons = Container(20, 1)
17
5be7cc43402e Handle curses resize event.
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    30
        self.buttons.sizereq.h = 1
15
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
        Container.add(self, self.buttons)
17
5be7cc43402e Handle curses resize event.
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    32
        horz = HorizontalLayout(homogeneous=True, spacing=1)
15
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
        self.buttons.layout(horz)
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
    34
15
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
    def add(self, widget, **kw):
17
5be7cc43402e Handle curses resize event.
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    36
        Container.add(self, widget, expand=True, fill=True)
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
    37
15
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
        if self.selected is None:
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
            self.selected = widget
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
            widget.hidden = False
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
        else:
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
            widget.hidden = True
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
    43
15
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
        btn = Button(kw['title'])
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
        btn.connect('click', lambda: self.select(widget))
17
5be7cc43402e Handle curses resize event.
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    46
        self.buttons.add(btn, fill=True)
15
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
    def select(self, child):
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
        log = logging.getLogger('tuikit')
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
        log.debug('Pager: select %r', child)
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
        self.selected.hidden = True
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
        child.hidden = False
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
        self.selected = child
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
34
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 32
diff changeset
    56
        self.emit('resize')
15
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57