tuikit/tableview.py
author Radek Brich <radek.brich@devl.cz>
Fri, 14 Dec 2012 10:20:14 +0100
changeset 32 088b92ffb119
parent 22 6ca8b2d221c3
child 33 45f1b6d590bd
permissions -rw-r--r--
Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
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
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
     6
from tuikit.eventsource import Emitter
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
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
        # model
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
        self._model = None
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
        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
    73
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
        self.columns = []
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
        self.spacing = 1
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    76
        self.rowcount = 0
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    77
        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
    78
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    79
        self.offset = Coords()
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    80
        self.acell = Coords()
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    81
        '''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
    82
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
        self.connect('draw', self.on_draw)
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
        self.connect('keypress', self.on_keypress)
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
    85
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
        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
    87
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    88
    def getmodel(self):
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    89
        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
    90
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
    def setmodel(self, value):
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
        if self._model:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    93
            self._model.disconnect('change', self.redraw)
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    94
        self._model = value
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    95
        if self._model:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    96
            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
    97
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    98
    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
    99
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   100
    def addcolumn(self, *args, **kwargs):
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
        for col in args:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   102
            self.columns.append(col)
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   103
        if len(args) == 0:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   104
            col = Column(**kwargs)
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   105
            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
   106
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   107
    def compute_column_sizes(self):
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   108
        total_space = self.size.w - self.spacing * len(self.columns)
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   109
        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
   110
        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
   111
        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
   112
        expand_num = len(expand_cols)
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   113
        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
   114
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   115
        # compute size of cols without expand
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   116
        for col in no_expand_cols:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   117
            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
   118
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   119
        # compute size of cols with expand
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   120
        if no_expand_size > total_space + expand_num:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   121
            for col in expand_cols:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   122
                col.size = 1
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   123
        else:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   124
            total_req = sum([col.sizereq for col in expand_cols])
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   125
            remaining_space = 0.
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   126
            for col in expand_cols:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   127
                frac, intp = math.modf(expand_size * col.sizereq / total_req)
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   128
                col.size = int(intp)
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   129
                remaining_space += frac
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   130
                if remaining_space > 0.99:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   131
                    remaining_space -= 1.
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   132
                    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
   133
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   134
        # compute indexes
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   135
        idx = 0
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   136
        for col in self.columns:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   137
            if not col.header:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   138
                col.index = idx
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   139
                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
   140
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   141
    def draw_head(self, screen, x, y):
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   142
        screen.pushcolor('strong')
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   143
        for col in self.columns:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   144
            screen.puts(x, y, col.title[:col.size])
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   145
            x += col.size + self.spacing
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   146
        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
   147
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   148
    def draw_row(self, screen, x, y, row, highlight):
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   149
        for col, data in zip(self.columns, row):
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   150
            if col.header:
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   151
                screen.pushcolor('strong')
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   152
            elif col.index in highlight:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   153
                screen.pushcolor('active')
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   154
            else:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   155
                screen.pushcolor('normal')
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   156
            screen.puts(x, y, data[:col.size])
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   157
            screen.popcolor()
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   158
            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
   159
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   160
    def on_draw(self, 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
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   176
    def on_keypress(self, keyname, char):
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   177
        if keyname:
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   178
            if keyname == 'up':  self.move_up()
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   179
            if keyname == 'down':  self.move_down()
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   180
            if keyname == 'left':  self.move_left()
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   181
            if keyname == 'right':  self.move_right()
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   182
            if keyname == 'pageup':  self.move_pageup()
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   183
            if keyname == 'pagedown':  self.move_pagedown()
21
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   184
        self.redraw()
8553a6bd2d82 Add TableView plus demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   185
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   186
    def set_yofs(self, yofs):
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   187
        if yofs > self.rowcount - (self.height - self.headsize):
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   188
            yofs = self.rowcount - (self.height - self.headsize)
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   189
        if yofs < 0:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   190
            yofs = 0
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   191
        self.offset.y = yofs
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   192
        self.handle('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
   193
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   194
    def move_up(self):
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   195
        if self.acell.y > 0:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   196
            self.acell.y -= 1
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   197
            if self.acell.y < self.offset.y:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   198
                self.set_yofs(self.acell.y)
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   199
            return True
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   200
        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
   201
22
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   202
    def move_down(self):
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   203
        log=logging.getLogger('tuikit')
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   204
        log.debug('height %d', self.height)
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   205
        if self.acell.y < self.rowcount - 1:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   206
            self.acell.y += 1
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   207
            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
   208
                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
   209
            return True
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   210
        return False
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   211
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   212
    def move_pageup(self):
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   213
        if self.acell.y >= self.height - self.headsize - 1:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   214
            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.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
   216
        else:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   217
            self.acell.y = 0
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   218
            self.set_yofs(0)
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   219
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
    def move_pagedown(self):
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   222
        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
   223
            self.acell.y += self.height - self.headsize - 1
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   224
            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
   225
        else:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   226
            self.acell.y = self.rowcount - 1
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   227
            self.set_yofs(self.acell.y)
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   228
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   229
    def move_left(self):
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   230
        if self.acell.x > 0:
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   231
            self.acell.x -= 1
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   232
            return True
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   233
        return False
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   234
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   235
    def move_right(self):
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   236
        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
   237
            self.acell.x += 1
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   238
            return True
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   239
        return False
6ca8b2d221c3 Update TableView: cursor movement.
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   240