Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/jarabe/journal/tableview.py
blob: a2ce24b6834ae50d275e1066d914e9721da961f0 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Copyright (C) 2009, Aleksey Lim
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import gtk
import math
import hippo
import gobject

from sugar.graphics import style
from sugar.graphics.roundbox import CanvasRoundBox
from jarabe.journal.smoothtable import SmoothTable

COLOR_BACKGROUND = style.COLOR_WHITE
COLOR_SELECTED = style.COLOR_TEXT_FIELD_GREY


class TableCell:

    def __init__(self):
        self.row = None
        self.table = None

    def do_fill_in(self):
        pass


class TableView(SmoothTable):

    def __init__(self, cell_class, rows, columns):
        SmoothTable.__init__(self, rows, columns,
                lambda: self._create_cell(cell_class), self._do_fill_in)

        self._model = None
        self._hover_selection = False
        self._selected_cell = None
        self._row_changed_id = None

    def get_cursor(self):
        return (self.frame[0], )

    def set_cursor(self, cursor):
        self.goto(cursor)

    def get_model(self):
        return self._model

    def set_model(self, model):
        if self._model == model:
            return

        if self._model is not None and self._row_changed_id is not None:
            self._model.disconnect(self._row_changed_id)

        self._model = model

        if model is not None:
            self._row_changed_id = \
                    self._model.connect('row-changed', self.__row_changed_cb)

        if model is not None:
            rows = math.ceil(float(model.iter_n_children(None)) / self.columns)
            self.bin_rows = int(rows)

    model = gobject.property(type=object,
            getter=get_model, setter=set_model)

    def get_hover_selection(self):
        return self._hover_selection

    def set_hover_selection(self, value):
        self._hover_selection = value

    hover_selection = gobject.property(type=object,
            getter=get_hover_selection, setter=set_hover_selection)

    def get_visible_range(self):
        return ((self.frame[0], ), (self.frame[1], ))

    def _create_cell(self, cell_class):
        canvas = hippo.Canvas()
        canvas.show()
        canvas.modify_bg(gtk.STATE_NORMAL, COLOR_BACKGROUND.get_gdk_color())

        sel_box = CanvasRoundBox()
        sel_box.props.border_color = COLOR_BACKGROUND.get_int()
        canvas.set_root(sel_box)

        cell = cell_class()
        cell.table = self
        sel_box.append(cell, hippo.PACK_EXPAND)

        canvas.connect('enter-notify-event',
                self.__enter_notify_event_cb, cell)
        canvas.connect('leave-notify-event', self.__leave_notify_event_cb)

        canvas.table_view_cell_sel_box = sel_box
        canvas.table_view_cell = cell

        return canvas

    def _do_fill_in(self, canvas, y, x, prepared_row=None):

        cell = canvas.table_view_cell
        sel_box = canvas.table_view_cell_sel_box

        if self._selected_cell == cell and cell.get_visible():
            bg_color = COLOR_SELECTED
        else:
            bg_color = COLOR_BACKGROUND
        sel_box.props.background_color = bg_color.get_int()

        cell.row = prepared_row

        if cell.row is None:
            cell_num = y * self.columns + x

            if cell_num < self._model.iter_n_children(None):
                row = self._model.get_row((cell_num, ), self.frame)
                if row is not None and row != False:
                    cell.row = row

        if cell.row is None:
            cell.set_visible(False)
        else:
            cell.do_fill_in()
            cell.set_visible(True)

    def __enter_notify_event_cb(self, canvas, event, cell):
        if not self.hover_selection:
            return

        if cell.get_visible():
            sel_box = canvas.table_view_cell_sel_box
            sel_box.props.background_color = COLOR_SELECTED.get_int()

        self._selected_cell = cell

    def __leave_notify_event_cb(self, canvas, event):
        if not self.hover_selection:
            return

        sel_box = canvas.table_view_cell_sel_box
        sel_box.props.background_color = COLOR_BACKGROUND.get_int()

        self._selected_cell = None

    def __row_changed_cb(self, model, path, iterator):
        y = path[0] / self.columns
        x = path[0] % self.columns

        canvas = self.get_visible_cell(y, x)
        if canvas is None:
            return

        row = self._model.get_row(path)
        self._do_fill_in(canvas, y, x, row)