tuikit/tableview.py
author Radek Brich <radek.brich@devl.cz>
Wed, 26 Dec 2012 01:00:31 +0100
changeset 40 5faa38c10b67
parent 34 e3beacd5e536
child 41 37b7dfc3eae6
permissions -rw-r--r--
Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
import math
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
     4
import logging
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
33
45f1b6d590bd Refactoring: rename eventsource module to emitter.
Radek Brich <radek.brich@devl.cz>
parents: 32
diff changeset
     6
from tuikit.emitter import Emitter
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
     7
from tuikit.widget import Widget
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
from tuikit.common import Coords
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    11
class TableModel(Emitter):
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
    def __init__(self, list_of_lists):
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    13
        self.add_events('change')
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
        self.data = list_of_lists
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    15
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
    def getcount(self):
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
        '''Get number of rows.'''
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
        return len(self.data)
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    19
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
    def getrows(self, begin, end):
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
        '''Get rows from begin to end, including begin, excluding end.'''
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
        return self.data[begin:end]
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    23
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
    def update(self, row, col, val):
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
        self.data[row][col] = val
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
class Column:
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    29
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
    '''Columns description.'''
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    31
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    32
    def __init__(self, title='', header=False, expand=True, sizereq=1,
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    33
        readonly=False, maxlength=None):
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
        '''Create column with default values.'''
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    35
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
        self.title = title
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    37
        '''Title is displayed in heading before first row.'''
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    38
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    39
        self.header = header
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    40
        '''Header column is highlighted, values in this column cannot be edited.'''
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    41
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
        self.expand = expand
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    43
        '''If true, this column will autoresize to consume any free space.'''
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    44
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
        self.sizereq = sizereq
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
        '''Size request. Meaning depends on value of expand:
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    47
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
        When false, sizereq is number of characters.
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
        When true, sizereq is relative size ratio.
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    50
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
        '''
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    52
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    53
        self.size = None
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
        '''Computed size of column.'''
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    55
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    56
        self.index = None
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    57
        '''Computed index.'''
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    58
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
        self.readonly = readonly
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    60
        '''If not readonly, values in this column can be changed by user.'''
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    61
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
        self.maxlength = maxlength
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    63
        '''Maximum length of value (for EditField).'''
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
class TableView(Widget):
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
    def __init__(self, model=None, width=20, height=20):
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
        Widget.__init__(self, width, height)
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    69
34
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
    70
        self.allow_focus = True
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
    71
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
        # model
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
        self._model = None
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
        self.setmodel(model)
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    75
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
        self.columns = []
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
        self.spacing = 1
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    78
        self.rowcount = 0
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    79
        self.headsize = 1
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    80
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    81
        self.offset = Coords()
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    82
        self.acell = Coords()
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    83
        '''Active cell (cursor).'''
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    84
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    85
        self.add_events('scroll', 'areasize')
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    86
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    87
    def getmodel(self):
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    88
        return self._model
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    89
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    90
    def setmodel(self, value):
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
        if self._model:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
            self._model.disconnect('change', self.redraw)
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    93
        self._model = value
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    94
        if self._model:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    95
            self._model.connect('change', self.redraw)
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    96
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    97
    model = property(getmodel, setmodel)
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    98
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    99
    def addcolumn(self, *args, **kwargs):
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   100
        for col in args:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
            self.columns.append(col)
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   102
        if len(args) == 0:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   103
            col = Column(**kwargs)
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   104
            self.columns.append(col)
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
   105
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   106
    def compute_column_sizes(self):
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   107
        total_space = self.size.w - self.spacing * len(self.columns)
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   108
        no_expand_cols = [col for col in self.columns if not col.expand]
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   109
        no_expand_size = sum([col.sizereq for col in no_expand_cols])
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   110
        expand_cols = [col for col in self.columns if col.expand]
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   111
        expand_num = len(expand_cols)
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   112
        expand_size = total_space - no_expand_size
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
   113
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   114
        # compute size of cols without expand
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   115
        for col in no_expand_cols:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   116
            col.size = col.sizereq
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
   117
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   118
        # compute size of cols with expand
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   119
        if no_expand_size > total_space + expand_num:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   120
            for col in expand_cols:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   121
                col.size = 1
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   122
        else:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   123
            total_req = sum([col.sizereq for col in expand_cols])
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   124
            remaining_space = 0.
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   125
            for col in expand_cols:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   126
                frac, intp = math.modf(expand_size * col.sizereq / total_req)
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   127
                col.size = int(intp)
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   128
                remaining_space += frac
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   129
                if remaining_space > 0.99:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   130
                    remaining_space -= 1.
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   131
                    col.size += 1
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
   132
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   133
        # compute indexes
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   134
        idx = 0
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   135
        for col in self.columns:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   136
            if not col.header:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   137
                col.index = idx
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   138
                idx += 1
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
   139
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   140
    def draw_head(self, screen, x, y):
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   141
        screen.pushcolor('strong')
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   142
        for col in self.columns:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   143
            screen.puts(x, y, col.title[:col.size])
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   144
            x += col.size + self.spacing
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   145
        screen.popcolor()
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
   146
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   147
    def draw_row(self, screen, x, y, row, highlight):
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   148
        for col, data in zip(self.columns, row):
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   149
            if col.header:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   150
                screen.pushcolor('strong')
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   151
            elif col.index in highlight:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   152
                screen.pushcolor('active')
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   153
            else:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   154
                screen.pushcolor('normal')
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   155
            screen.puts(x, y, data[:col.size])
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   156
            screen.popcolor()
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   157
            x += col.size + self.spacing
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
   158
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
   159
    def _handle_draw(self, screen, x, y):
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
   160
        super()._handle_draw(screen, x, y)
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
   161
        screen.pushcolor('normal')
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   162
        self.rowcount = self.model.getcount()
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   163
        numrows = min(self.rowcount - self.offset.y, self.size.h - self.headsize)
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   164
        rows = self.model.getrows(self.offset.y, self.offset.y + numrows)
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   165
        self.compute_column_sizes()
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   166
        self.draw_head(screen, x, y)
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   167
        y += self.headsize
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   168
        for row in rows:
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   169
            highlight = []
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   170
            if self.offset.y + rows.index(row) == self.acell.y:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   171
                highlight.append(self.acell.x)
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   172
            self.draw_row(screen, x, y, row, highlight)
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   173
            y += 1
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
   174
        screen.popcolor()
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   175
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
   176
    def _handle_keypress(self, keyname, char):
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
   177
        super()._handle_keypress(keyname, char)
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   178
        if keyname:
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   179
            if keyname == 'up':  self.move_up()
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   180
            if keyname == 'down':  self.move_down()
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   181
            if keyname == 'left':  self.move_left()
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   182
            if keyname == 'right':  self.move_right()
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   183
            if keyname == 'pageup':  self.move_pageup()
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   184
            if keyname == 'pagedown':  self.move_pagedown()
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   185
        self.redraw()
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   186
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   187
    def set_yofs(self, yofs):
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   188
        if yofs > self.rowcount - (self.height - self.headsize):
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   189
            yofs = self.rowcount - (self.height - self.headsize)
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   190
        if yofs < 0:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   191
            yofs = 0
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   192
        self.offset.y = yofs
34
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   193
        self.emit('scroll')
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
   194
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   195
    def move_up(self):
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   196
        if self.acell.y > 0:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   197
            self.acell.y -= 1
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   198
            if self.acell.y < self.offset.y:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   199
                self.set_yofs(self.acell.y)
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   200
            return True
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   201
        return False
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
   202
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   203
    def move_down(self):
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   204
        log=logging.getLogger('tuikit')
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   205
        log.debug('height %d', self.height)
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   206
        if self.acell.y < self.rowcount - 1:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   207
            self.acell.y += 1
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   208
            if self.acell.y > self.offset.y + (self.height - self.headsize - 1):
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   209
                self.set_yofs(self.acell.y - (self.height - self.headsize - 1))
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   210
            return True
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   211
        return False
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   212
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   213
    def move_pageup(self):
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   214
        if self.acell.y >= self.height - self.headsize - 1:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   215
            self.acell.y -= self.height - self.headsize - 1
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   216
            self.set_yofs(self.offset.y - (self.height - self.headsize - 1))
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   217
        else:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   218
            self.acell.y = 0
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   219
            self.set_yofs(0)
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   220
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   221
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   222
    def move_pagedown(self):
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   223
        if self.acell.y <= self.rowcount - (self.height - self.headsize - 1):
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   224
            self.acell.y += self.height - self.headsize - 1
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   225
            self.set_yofs(self.offset.y + (self.height - self.headsize - 1))
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   226
        else:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   227
            self.acell.y = self.rowcount - 1
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   228
            self.set_yofs(self.acell.y)
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   229
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   230
    def move_left(self):
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   231
        if self.acell.x > 0:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   232
            self.acell.x -= 1
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   233
            return True
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   234
        return False
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   235
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   236
    def move_right(self):
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   237
        if self.acell.x < len([col for col in self.columns if not col.header]) - 1:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   238
            self.acell.x += 1
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   239
            return True
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   240
        return False
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   241