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
|
# coding: UTF8
# Copyright 2009 Thomas Jourdan
#
# 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 3 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 cairo
import ka_debug
import ka_task
import ka_controller
_ZOOM_PAGE_NUMBER = 1
class ZoomController(object):
"""
inv: self._widget_tree is not None
inv: self._drawing_page is not None
inv: self._drawing_area is not None
"""
def __init__(self, controller, init_widget_tree):
"""
pre: init_widget_tree is not None
"""
self._widget_tree = init_widget_tree
self._my_page = False
self._surface = None
self._protozoon = None
self._drawing_page = self._widget_tree.get_widget('kandidNotebook'). \
get_nth_page(_ZOOM_PAGE_NUMBER)
self._drawing_area = self._widget_tree.get_widget('zoomarea')
def autoconnect_events(self):
"""Auto connect zoom view."""
events = {
'on_notebook_switch_page' : self.on_notebook_switch_page,
'on_zoomarea_expose' : self.on_zoomarea_expose,
'on_zoomarea_size_allocate' : self.on_zoomarea_size_allocate,
}
self._widget_tree.signal_autoconnect(events)
def localize(self):
"""A dummy"""
pass
def show(self):
"""Hide zoom view initially."""
self._drawing_page.hide()
def on_zoomarea_expose(self, widget, event):
""" Repaint image of a single protozoon inside zoom view.
pre: widget is not None
"""
# draw precalculated protozoon stored in the surface cache.
# ka_debug.info('on_zoomarea_expose: ' + widget.name + ' '
# + str(widget.allocation.width)
# + 'x' + str(widget.allocation.height))
self._draw_from_cache(widget)
def on_zoomarea_size_allocate(self, widget, event):
"""New size for zoom drawing area available.
pre: widget is not None
"""
# ka_debug.info('on_zoomarea_size_allocate: ' + widget.name + ' '
# + str(widget.allocation.width)
# + 'x' + str(widget.allocation.height))
self.start_calculation(self._protozoon)
def on_zoom_completed(self, *args):
"""Rendering protozoon is completed."""
# ka_debug.info('on_zoom_completed: ' + str(args[0]))
self._drawing_page.show()
self._widget_tree.get_widget('kandidNotebook'). \
set_current_page(_ZOOM_PAGE_NUMBER)
self._drawing_area = self._widget_tree.get_widget('zoomarea')
self._draw_from_cache(self._drawing_area)
def on_notebook_switch_page(self, *args):
"""Test if zoom page will be displayed.
pre: len(args) >= 3
"""
# ka_debug.info('on_notebook_switch_page %s' % (args[2]))
self._my_page = args[2] == _ZOOM_PAGE_NUMBER
def task_render(self, *args, **kwargs):
"""Render zoom view of protozoon.
pre: len(args) == 4
"""
protozoon, dummy, width, height = \
args[0], args[1], args[2], args[3]
# ka_debug.info('task_render entry: ')
self._surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
ctx = cairo.Context(self._surface)
protozoon.render(ctx, width, height)
# ka_debug.info('task_render exit: ')
def start_calculation(self, zoom_protozoon):
"""Start rendering of protozoon."""
self._protozoon = zoom_protozoon
if self._protozoon is not None:
widget = self._widget_tree.get_widget('zoomarea')
task = ka_task.GeneratorTask(self.task_render,
self.on_zoom_completed)
task.start(self._protozoon, -1,
widget.allocation.width, widget.allocation.height)
# ka_debug.info('start_calculation %ux%u for %s' %
# (widget.allocation.width, widget.allocation.height, widget.name))
def _draw_from_cache(self, widget):
"""Paint to drawing area.
pre: widget is not None
"""
if self._surface is not None:
ctx = ka_controller.create_context(widget)
ctx.set_operator(cairo.OPERATOR_SOURCE)
ctx.set_source_surface(self._surface)
ctx.paint()
|