Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/jarabe/journal/homogenetable.py
blob: 64a22ecd7c01b892dc434ccc5e9f6358e5a3cddd (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
# Copyright (C) 2010, 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 gobject
import math
import bisect
import logging

# Having spare rows let us making smooth scrolling w/o empty spaces
_SPARE_ROWS_COUNT = 2


class VHomogeneTable(gtk.Container):
    """
    Grid widget with homogeneously placed children of the same class.

    Grid has fixed number of columns that are visible all time and unlimited
    rows number. There are frame cells - visible at particular moment - frame
    cells and virtual (widget is model less itself and only ask callback
    object about right cell's value) ones - just cells. User can scroll up/down
    grid to see all virtual cells and the same frame cell could represent
    content of various virtual cells (widget will call cell_fill_in_cb callback
    to refill frame cell content) in different time moments.

    By default widget doesn't have any cells, to make it useful, assign proper
    value to either frame_size or cell_size property. Also set cell_count to
    set number of virual rows.

    """
    __gsignals__ = {
            'set-scroll-adjustments': (gobject.SIGNAL_RUN_FIRST, None,
                                      [gtk.Adjustment, gtk.Adjustment]),
            'cursor-changed': (gobject.SIGNAL_RUN_FIRST, None, [object]),
            }

    def __init__(self, cell_class, **kwargs):
        assert(hasattr(cell_class, 'do_fill_in'))

        self._cell_class = cell_class
        self._row_cache = []
        self._cell_cache = []
        self._cell_cache_pos = 0
        self._adjustment = None
        self._adjustment_value_changed_id = None
        self._bin_window = None
        self._cell_count = 0
        self._cell_height = 0
        self._frame_size = [None, None]
        self._cell_size = [None, None]
        self._selected_index = None
        self._editable = True
        self._pending_allocate = None

        gtk.Container.__init__(self, **kwargs)

        # when focused cell is out of visible frame,
        # table itslef will be focused to follow gtk focusing scheme
        self.props.can_focus = True

        self.connect('key-press-event', self.__key_press_event_cb)

    def set_frame_size(self, value):
        value = list(value)
        if self._frame_size == value:
            return

        if value[0] is not None:
            self._cell_size[1] = None
        if value[1] is not None:
            self._cell_size[0] = None

        self._frame_size = value
        self._resize_table()

    """Set persistent number of frame rows/columns, value is (rows, columns)
       Cells will be resized while resizing widget.
       Mutually exclusive to cell_size."""
    frame_size = gobject.property(setter=set_frame_size)

    def set_cell_size(self, value):
        value = list(value)
        if self._cell_size == value:
            return

        if value[0] is not None:
            self._frame_size[1] = None
        if value[1] is not None:
            self._frame_size[0] = None

        self._cell_size = value
        self._resize_table()

    """Set persistent cell sizes, value is (width, height)
       Number of cells will be changed while resizing widget.
       Mutually exclusive to frame_size."""
    cell_size = gobject.property(setter=set_cell_size)

    def get_cell_count(self):
        return self._cell_count

    def set_cell_count(self, count):
        if self._cell_count == count:
            return
        self._cell_count = count
        self.refill()
        self._setup_adjustment(dry_run=False)

    """Number of virtual cells
       Defines maximal number of virtual rows, the minimal has being described
       by frame_size/cell_size values."""
    cell_count = gobject.property(getter=get_cell_count, setter=set_cell_count)

    def get_cell(self, cell_index):
        """Get cell widget by index
           Method returns non-None values only for visible cells."""
        cell = self._get_cell(cell_index)
        if cell is None:
            return None
        else:
            return cell.widget

    def __getitem__(self, cell_index):
        return self.get_cell(cell_index)

    def get_cursor(self):
        return self._selected_index

    def set_cursor(self, cell_index):
        cell_index = min(max(0, cell_index), self.cell_count - 1)
        if cell_index == self.cursor:
            return
        self.scroll_to_cell(cell_index)
        self._set_cursor(cell_index)

    """Selected cell"""
    cursor = gobject.property(getter=get_cursor, setter=set_cursor)

    def get_editable(self):
        return self._editable

    def set_editable(self, value):
        self._editable = value

    """Can cells be focused"""
    editable = gobject.property(getter=get_editable, setter=set_editable)

    def get_editing(self):
        if not self._editable or self._selected_index is None or \
                self.props.has_focus:
            return False
        cell = self._get_cell(self._selected_index)
        if cell is None:
            return False
        else:
            return cell.widget.get_focus_child()

    def set_editing(self, value):
        if value == self.editing:
            return
        if value:
            if not self.props.has_focus:
                self.grab_focus()
            cell = self._get_cell(self._selected_index)
            if cell is not None:
                cell.widget.child_focus(gtk.DIR_TAB_FORWARD)
        else:
            self.grab_focus()

    """Selected cell got focused"""
    editing = gobject.property(getter=get_editing, setter=set_editing)

    def get_cell_at_pos(self, x, y):
        """Get cell index at pos which is relative to VHomogeneTable widget"""
        if self._empty:
            return None

        x, y = self.get_pointer()
        x = min(max(0, x), self.allocation.width)
        y = min(max(0, y), self.allocation.height) + self._pos_y

        return self._get_cell_at_pos(x, y)

    def scroll_to_cell(self, cell_index):
        """Scroll VHomogeneTable to position where cell is viewable"""
        if self._empty:
            return

        self.editing = False

        row = cell_index / self._column_count
        pos = row * self._cell_height

        if pos < self._pos_y:
            self._pos_y = pos
        elif pos + self._cell_height >= self._pos_y + self._page:
            self._pos_y = pos + self._cell_height - self._page
        else:
            return

        self._pos_changed()

    def refill(self):
        """Force VHomogeneTable widget to run filling method for all cells"""
        for cell in self._cell_cache:
            cell.invalidate_pos()
            cell.index = -1
        self._allocate_rows(force=True)

    # gtk.Widget overrides

    def do_realize(self):
        self.set_flags(gtk.REALIZED)

        self.window = gtk.gdk.Window(
                self.get_parent_window(),
                window_type=gtk.gdk.WINDOW_CHILD,
                x=self.allocation.x,
                y=self.allocation.y,
                width=self.allocation.width,
                height=self.allocation.height,
                wclass=gtk.gdk.INPUT_OUTPUT,
                colormap=self.get_colormap(),
                event_mask=gtk.gdk.VISIBILITY_NOTIFY_MASK)
        self.window.set_user_data(self)

        self._bin_window = gtk.gdk.Window(
                self.window,
                window_type=gtk.gdk.WINDOW_CHILD,
                x=0,
                y=-self._pos_y,
                width=self.allocation.width,
                height=self._max_y,
                colormap=self.get_colormap(),
                wclass=gtk.gdk.INPUT_OUTPUT,
                event_mask=(self.get_events() | gtk.gdk.EXPOSURE_MASK |
                            gtk.gdk.SCROLL_MASK))
        self._bin_window.set_user_data(self)

        self.set_style(self.style.attach(self.window))
        self.style.set_background(self.window, gtk.STATE_NORMAL)
        self.style.set_background(self._bin_window, gtk.STATE_NORMAL)

        for row in self._row_cache:
            for cell in row:
                cell.widget.set_parent_window(self._bin_window)

        if self._pending_allocate is not None:
            self._allocate_rows(force=self._pending_allocate)
            self._pending_allocate = None
        #self.queue_resize()

    def do_size_allocate(self, allocation):
        resize_tabel = self.allocation != allocation
        self.allocation = allocation

        if resize_tabel:
            self._resize_table()

        if self.flags() & gtk.REALIZED:
            self.window.move_resize(*allocation)

    def do_unrealize(self):
        self._bin_window.set_user_data(None)
        self._bin_window.destroy()
        self._bin_window = None
        gtk.Container.do_unrealize(self)

    def do_style_set(self, style):
        gtk.Widget.do_style_set(self, style)
        if self.flags() & gtk.REALIZED:
            self.style.set_background(self._bin_window, gtk.STATE_NORMAL)

    def do_expose_event(self, event):
        if event.window != self._bin_window:
            return False
        gtk.Container.do_expose_event(self, event)
        return False

    def do_map(self):
        self.set_flags(gtk.MAPPED)

        for row in self._row_cache:
            for cell in row:
                cell.widget.map()

        self._bin_window.show()
        self.window.show()

    def do_size_request(self, req):
        req.width = 0
        req.height = 0

        for row in self._row_cache:
            for cell in row:
                cell.widget.size_request()

    def do_set_scroll_adjustments(self, hadjustment, vadjustment):
        if vadjustment is None or vadjustment == self._adjustment:
            return

        if self._adjustment is not None:
            self._adjustment.disconnect(self._adjustment_value_changed_id)

        self._adjustment = vadjustment
        self._setup_adjustment(dry_run=True)

        self._adjustment_value_changed_id = vadjustment.connect(
                'value-changed', self.__adjustment_value_changed_cb)

    # gtk.Container overrides

    def do_forall(self, include_internals, callback, data):
        for row in self._row_cache:
            for cell in row:
                callback(cell.widget, data)

    def do_add(self, widget):
        # container is not intended to add children manually
        assert(False)

    def do_remove(self, widget):
        # container is not intended to remove children manually
        pass

    def do_set_focus_child(self, widget):
        if widget is not None:
            x, y, __, __ = widget.allocation
            self.cursor = self._get_cell_at_pos(x, y)

    def do_focus(self, type):
        if self.editing:
            cell = self._get_cell(self._selected_index)
            if cell is None:
                logging.error('cannot find _selected_index cell')
            elif not cell.widget.child_focus(type):
                self.grab_focus()
            return True
        else:
            if self.props.has_focus:
                return False
            else:
                if self._selected_index is None:
                    x, y = self.get_pointer()
                    self._set_cursor(self.get_cell_at_pos(x, y))
                self.grab_focus()
                return True

    @property
    def _frame_range(self):
        if self._empty:
            return xrange(0)
        else:
            first = self._pos_y / self._cell_height * self._column_count
            last = int(math.ceil(float(self._pos_y + self._page) / \
                    self._cell_height) * self._column_count)
            return xrange(first, min(last, self.cell_count))

    @property
    def _empty(self):
        return not self._row_cache

    @property
    def _column_count(self):
        if self._row_cache:
            return len(self._row_cache[0])
        else:
            return 0

    @property
    def _row_count(self):
        if self._column_count == 0:
            return 0
        else:
            rows = math.ceil(float(self.cell_count) / self._column_count)
            return max(self._frame_row_count, rows)

    @property
    def _frame_row_count(self):
        return len(self._row_cache) - _SPARE_ROWS_COUNT

    @property
    def _page(self):
        return self._frame_row_count * self._cell_height

    @property
    def _pos_y(self):
        if self._adjustment is None or math.isnan(self._adjustment.value):
            return 0
        else:
            return max(0, int(self._adjustment.value))

    @_pos_y.setter
    def _pos_y(self, value):
        if self._adjustment is not None:
            self._adjustment.value = value

    @property
    def _max_pos_y(self):
        if self._adjustment is None:
            return 0
        else:
            return max(0, self._max_y - self._page)

    @property
    def _max_y(self):
        if self._adjustment is None:
            return self.allocation.height
        else:
            return int(self._adjustment.upper)

    def _get_cell(self, cell_index):
        if cell_index is None:
            return None
        column = cell_index % self._column_count
        base_index = cell_index - column
        for row in self._row_cache:
            if row[0].is_valid() and row[0].index == base_index:
                return row[column]
        return None

    def _set_cursor(self, cell_index):
        old_cursor = self._selected_index
        self._selected_index = cell_index
        if old_cursor != self._selected_index:
            self.emit('cursor-changed', old_cursor)

    def _get_cell_at_pos(self, x, y):
        cell_row = y / self._cell_height
        cell_column = x / (self.allocation.width / self._column_count)
        cell_index = cell_row * self._column_count + cell_column
        return min(cell_index, self.cell_count - 1)

    def _pos_changed(self):
        if self._adjustment is not None:
            self._adjustment.value_changed()

    def _abandon_cells(self):
        for row in self._row_cache:
            for cell in row:
                cell.widget.unparent()
        self._cell_cache_pos = 0
        self._row_cache = []

    def _pop_a_cell(self):
        if self._cell_cache_pos < len(self._cell_cache):
            cell = self._cell_cache[self._cell_cache_pos]
            self._cell_cache_pos += 1
        else:
            cell = _Cell()
            cell.widget = self._cell_class()
            self._cell_cache.append(cell)
            self._cell_cache_pos = len(self._cell_cache)

        cell.invalidate_pos()
        return cell

    def _resize_table(self):
        x, y, width, height = self.allocation
        if x < 0 or y < 0:
            return

        frame_row_count, column_count = self._frame_size
        cell_width, cell_height = self._cell_size

        if frame_row_count is None:
            if cell_height is None:
                return
            frame_row_count = max(1, height / cell_height)
        if column_count is None:
            if cell_width is None:
                return
            column_count = max(1, width / cell_width)

        if (column_count != self._column_count or \
                frame_row_count != self._frame_row_count):
            self._abandon_cells()
            for i_ in range(frame_row_count + _SPARE_ROWS_COUNT):
                row = []
                for j_ in range(column_count):
                    cell = self._pop_a_cell()
                    if self.flags() & gtk.REALIZED:
                        cell.widget.set_parent_window(self._bin_window)
                    cell.widget.set_parent(self)
                    row.append(cell)
                self._row_cache.append(row)
        else:
            for row in self._row_cache:
                for cell in row:
                    cell.invalidate_pos()

        self._cell_height = height / self._frame_row_count
        self._setup_adjustment(dry_run=True)

        if self.flags() & gtk.REALIZED:
            self._bin_window.resize(self.allocation.width, self._max_y)

        self._allocate_rows(force=True)

    def _setup_adjustment(self, dry_run):
        if self._adjustment is None:
            return

        self._adjustment.lower = 0
        self._adjustment.upper = self._row_count * self._cell_height
        self._adjustment.page_size = self._page
        self._adjustment.changed()

        if self._pos_y > self._max_pos_y:
            self._pos_y = self._max_pos_y
            if not dry_run:
                self._adjustment.value_changed()

    def _allocate_cells(self, row, cell_y):
        cell_x = 0
        cell_row = cell_y / self._cell_height
        cell_index = cell_row * self._column_count

        for cell_column, cell in enumerate(row):
            if cell.index != cell_index:
                if cell_index < self.cell_count:
                    cell.widget.do_fill_in(self, cell_index)
                    cell.widget.show()
                else:
                    cell.widget.hide()
                cell.index = cell_index

            cell_alloc = gtk.gdk.Rectangle(cell_x, cell_y)
            cell_alloc.width = self.allocation.width / self._column_count
            cell_alloc.height = self._cell_height
            cell.widget.size_request()
            cell.widget.size_allocate(cell_alloc)

            cell_x += cell_alloc.width
            cell_index += 1

    def _allocate_rows(self, force):
        if self._empty:
            return

        if not self.flags() & gtk.REALIZED:
            self._pending_allocate = self._pending_allocate or force
            return

        pos = self._pos_y
        if pos < 0 or pos > self._max_pos_y:
            return

        spare_rows = []
        visible_rows = []
        page_end = pos + self._page

        if force:
            spare_rows = [] + self._row_cache
        else:
            for row in self._row_cache:
                row_y = row[0].widget.allocation.y
                if row_y < 0 or row_y > page_end or \
                        (row_y + self._cell_height) < pos:
                    spare_rows.append(row)
                else:
                    bisect.insort_right(visible_rows, _IndexedRow(row))

        if visible_rows or spare_rows:

            def try_insert_spare_row(cell_y, end_y):
                while cell_y < end_y:
                    if not spare_rows:
                        logging.error('spare_rows should not be empty')
                        return
                    row = spare_rows.pop()
                    self._allocate_cells(row, cell_y)
                    cell_y = cell_y + self._cell_height

            # visible_rows could not be continuous
            # lets try to add spare rows to missed points
            cell_y = int(pos) - int(pos) % self._cell_height
            for i in visible_rows:
                cell = i.row[0].widget.allocation
                try_insert_spare_row(cell_y, cell.y)
                cell_y = cell.y + cell.height

            try_insert_spare_row(cell_y, page_end)

            if self.editing and self._selected_index not in self._frame_range:
                self.editing = False

        self._bin_window.move(0, int(-pos))
        self._bin_window.process_updates(True)

    def __adjustment_value_changed_cb(self, adjustment):
        self._allocate_rows(force=False)

    def __key_press_event_cb(self, widget, event):
        if self._empty or self.cursor is None:
            return

        page = self._column_count * self._frame_row_count

        if event.keyval == gtk.keysyms.Return and self.editable:
            self.editing = not self.editing
        elif event.keyval == gtk.keysyms.Left:
            self.cursor -= 1
        elif event.keyval == gtk.keysyms.Right:
            self.cursor += 1
        elif event.keyval == gtk.keysyms.Up:
            if self.cursor >= self._column_count:
                self.cursor -= self._column_count
        elif event.keyval == gtk.keysyms.Down:
            if self.cursor / self._column_count < \
                    (self.cell_count - 1) / self._column_count:
                self.cursor += self._column_count
        elif event.keyval in (gtk.keysyms.Page_Up, gtk.keysyms.KP_Page_Up):
            self.cursor -= page
        elif event.keyval in (gtk.keysyms.Page_Down, gtk.keysyms.KP_Page_Down):
            self.cursor += page
        elif event.keyval in (gtk.keysyms.Home, gtk.keysyms.KP_Home):
            self.cursor = 0
        elif event.keyval in (gtk.keysyms.End, gtk.keysyms.KP_End):
            self.cursor = self.cell_count - 1
        else:
            return False

        return True


class _IndexedRow:

    def __init__(self, row):
        self.row = row

    def __lt__(self, other):
        return self.row[0].widget.allocation.y < \
               other.row[0].widget.allocation.y


class _Cell:
    widget = None
    index = -1

    def invalidate_pos(self):
        self.widget.size_allocate(gtk.gdk.Rectangle(-1, -1, 0, 0))

    def is_valid(self):
        return self.index >= 0 and self.widget is not None and \
               self.widget.allocation >= 0 and self.widget.allocation >= 0


VHomogeneTable.set_set_scroll_adjustments_signal('set-scroll-adjustments')