tuikit/pager.py
author Radek Brich <radek.brich@devl.cz>
Sun, 15 Feb 2015 12:48:23 +0100
changeset 114 26c02bd94bd9
parent 46 2b43a7f38c34
permissions -rw-r--r--
Add Widget.posreq. Add OffsetLayout.
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
    '''
40
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    20
15
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
    def __init__(self, width=20, height=20):
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
        Container.__init__(self, width, height)
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
        # selected child (only one visible)
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
        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
    26
42
0224ce40792f Make Container.layout a property.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    27
        self.layout = VerticalLayout()
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
42
0224ce40792f Make Container.layout a property.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    31
        self.buttons.layout = HorizontalLayout(homogeneous=True, spacing=1)
46
2b43a7f38c34 Minor updates. Replace super() with direct class reference. Add demo_colors.
Radek Brich <radek.brich@devl.cz>
parents: 45
diff changeset
    32
        Container.add(self, self.buttons)
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
    33
15
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
    def add(self, widget, **kw):
46
2b43a7f38c34 Minor updates. Replace super() with direct class reference. Add demo_colors.
Radek Brich <radek.brich@devl.cz>
parents: 45
diff changeset
    35
        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
    36
15
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
        if self.selected is None:
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
            self.selected = widget
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
            widget.hidden = False
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
        else:
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
            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
    42
15
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
        btn = Button(kw['title'])
45
43b2279b06e1 Clean up Emitter class, simplify event handling. Fix Container.focusnext() method. Add events test (handler auto-registration, order).
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    44
        btn.add_handler('click', lambda: self.select(widget))
17
5be7cc43402e Handle curses resize event.
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    45
        self.buttons.add(btn, fill=True)
15
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
    def select(self, child):
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
        log = logging.getLogger('tuikit')
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
        log.debug('Pager: select %r', child)
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
        self.selected.hidden = True
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
        child.hidden = False
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
        self.selected = child
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
34
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 32
diff changeset
    55
        self.emit('resize')
15
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56