File: table_view.py

package info (click to toggle)
python-enamlx 0.6.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 388 kB
  • sloc: python: 3,338; makefile: 18
file content (109 lines) | stat: -rw-r--r-- 2,866 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
Copyright (c) 2015, Jairus Martin.
Distributed under the terms of the MIT License.
The full license is in the file COPYING.txt, distributed with this software.
Created on Jun 3, 2015
"""
from atom.api import Bool, ForwardTyped, Int, Typed, observe
from enaml.core.declarative import d_

from enamlx.widgets.abstract_item import (
    AbstractWidgetItem,
    AbstractWidgetItemGroup,
    ProxyAbstractWidgetItem,
    ProxyAbstractWidgetItemGroup,
)
from enamlx.widgets.abstract_item_view import AbstractItemView, ProxyAbstractItemView


class ProxyTableView(ProxyAbstractItemView):
    declaration = ForwardTyped(lambda: TableView)

    def get_row_count(self):
        raise NotImplementedError

    def get_column_count(self):
        raise NotImplementedError

    def set_show_grid(self, show):
        pass


class ProxyTableViewRow(ProxyAbstractWidgetItemGroup):
    declaration = ForwardTyped(lambda: TableViewRow)

    def set_row(self, row):
        raise NotImplementedError


class ProxyTableViewColumn(ProxyAbstractWidgetItemGroup):
    declaration = ForwardTyped(lambda: TableViewColumn)

    def set_column(self, column):
        raise NotImplementedError


class ProxyTableViewItem(ProxyAbstractWidgetItem):
    declaration = ForwardTyped(lambda: TableViewItem)

    def data_changed(self, change):
        raise NotImplementedError


class TableView(AbstractItemView):
    #: Proxy reference
    proxy = Typed(ProxyTableView)

    #: Show grid of cells
    show_grid = d_(Bool(True))

    @observe("show_grid")
    def _update_proxy(self, change):
        """An observer which sends state change to the proxy."""
        if change["name"] == "items":
            self._update_visible_area()

        super(TableView, self)._update_proxy(change)

    def _update_visible_area(self):
        self.visible_rows = min(100, len(self.items))
        self.visible_columns = min(100, len(self.items))


class TableViewItem(AbstractWidgetItem):
    """The base class implementation is sufficient."""

    #: Proxy reference
    proxy = Typed(ProxyTableViewItem)


class TableViewRow(AbstractWidgetItemGroup):
    """Use this to build a table by defining the rows."""

    #: Proxy reference
    proxy = Typed(ProxyTableViewRow)

    #: Row within the table
    row = d_(Int())

    @observe("row")
    def _update_index(self, change):
        for column, item in enumerate(self._items):
            item.row = self.row
            item.column = column


class TableViewColumn(AbstractWidgetItemGroup):
    """Use this to build a table by defining the columns."""

    #: Proxy reference
    proxy = Typed(ProxyTableViewColumn)

    #: Column within the table
    column = d_(Int())

    @observe("column")
    def _update_index(self, change):
        for row, item in enumerate(self._item):
            item.row = row
            item.column = self.column