Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ep_page_zoom.py
blob: c630fa78253fa5cfe2abe90cd31cbf5118848611 (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
# coding: UTF-8
# Copyright 2009, 2010 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

from gettext import gettext as _

import cairo
import gtk

import ka_debug
import ka_task
import ka_controller

class ZoomController(object):
    """
    inv: self._widget_list is not None
    """

    def __init__(self, controller, widget_list, activity_root, started_anew):
        """
        pre: controller is not None
        pre: widget_list is not None
        """
        self._controller = controller
        self._widget_list = widget_list
        self._surface = None
        self._protozoon = None
        self.position = 100

    def close(self):
        """Clean up"""
        pass

    def create_gui(self):
        """ """
        page = gtk.HBox()
        self._widget_list.remember('zoomPage', page)
        aspectframe = gtk.AspectFrame()
        drawingarea = gtk.DrawingArea()
        self._widget_list.remember('zoomarea', drawingarea)
        aspectframe.add(drawingarea)
        page.pack_start(aspectframe, expand=True, fill=True)
        return page, gtk.Label(_('Zoom'))

    def autoconnect_events(self):
        """Auto connect zoom view."""
        drawing_area = self._widget_list.get_widget('zoomarea')
        drawing_area.connect('expose-event', self.on_zoomarea_expose)
        drawing_area.connect('size-allocate', self.on_zoomarea_size_allocate)
        
    def localize(self):
        """A dummy"""
        pass

    def activate_gui(self):
        """Hide zoom view initially."""
        drawing_page = self._widget_list.get_widget('zoomPage')
        drawing_page.hide()

    def on_zoomarea_expose(self, widget, event):
        """ Repaint image of a single protozoon inside zoom view.
        pre: widget is not None
        """
#        ka_debug.info('on_zoomarea_expose: ' + widget.name + ' ' 
#                      + str(widget.allocation.width) 
#                      + 'x' + str(widget.allocation.height))
        # draw precalculated protozoon stored in the surface cache. 
        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._controller.switch_page('ZoomController')
        self._draw_from_cache(self._widget_list.get_widget('zoomarea'))

    def task_render(self, task, *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(task, 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_list.get_widget('zoomarea')
            task = ka_task.GeneratorTask(self.task_render,
                                         self.on_zoom_completed,
                                         'zoomarea')
            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_CLEAR)
#            ctx.paint()
            ctx.set_operator(cairo.OPERATOR_SOURCE)
#            ctx.set_operator(cairo.OPERATOR_OVER)
            ctx.set_source_surface(self._surface)
            ctx.paint()