Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ka_incoming.py
blob: 99afe56b87f32b3b79ba67c0d06d53d827ca88b7 (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
# coding: UTF-8
# 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 ka_controller
import ka_task
import cairo

import ka_debug

class KandidIncoming(object):
    """
    inv: 0 <= len(self.incoming_protozoans) <= self._capacity
    inv: 0 <= len(self.incoming_surface_cache) <= self._capacity
    inv: 0 <= len(self.incoming_surface_cache) <= len(self.incoming_protozoans)
    inv: self._capacity > 0
    """
    
    ids = 0

    def __init__(self, capacity, widget_tree):
        """
        pre: capacity > 0
        pre: widget_tree is not None
        """
        self._capacity = capacity
        self._widget_tree = widget_tree
        self.incoming_id = []
        self.incoming_protozoans = {}
        self.incoming_surface_cache = {}
        self._inbox_widget = self._widget_tree.get_widget('incomingBox')
        self._update_incomming_gui()
 
    def autoconnect_events(self):
        """Create a dictionary to connect events."""
        events = { }
        for cell_index in range(self._capacity):
            key = 'on_incomingarea_#_expose'.replace('#', str(cell_index))
            events[key] = self.on_incomingarea_expose
        self._widget_tree.signal_autoconnect(events)

    def at_index(self, index):
        iid = self.incoming_id[index] if index < len(self.incoming_id) \
                                      else -1
        protozoon = self.incoming_protozoans[iid] \
                      if self.incoming_protozoans.has_key(iid) \
                      else None
        return protozoon, iid

    def append_protozoon(self, incoming_protozoon):
        """ Append incoming protozoon and manage capacity.
        pre: incoming_protozoon is not None
        """
        ka_debug.info('incoming: append protozoon')
        while len(self.incoming_protozoans) >= self._capacity:
            iid = self.incoming_id[0]
            del self.incoming_id[0]
            del self.incoming_protozoans[iid]
            if self.incoming_surface_cache.has_key(iid):
                del self.incoming_surface_cache[iid]
        KandidIncoming.ids += 1
        self.incoming_id.append(KandidIncoming.ids)
        self.incoming_protozoans[KandidIncoming.ids] = incoming_protozoon
        index = len(self.incoming_id) - 1
        widget_name = 'incomingarea_' + str(index)
        widget = self._widget_tree.get_widget(widget_name)
        task = ka_task.GeneratorTask(self.task_render,
                                     self.on_image_completed,
                                     widget_name)
        task.start(incoming_protozoon, KandidIncoming.ids,
                   widget.allocation.width, widget.allocation.height)
#        ka_debug.info('incoming: start_calculation %ux%u, iid %u for %s' % 
#          (widget.allocation.width, widget.allocation.height,
#           KandidIncoming.ids, widget.name))

    def accept_protozoon(self, index):
        """ Move protozoon from incoming list to image population.
        pre: 0 <= index < self._capacity
        """
        protozoon, iid = self.at_index(index)
        if protozoon is not None:
#            ka_debug.info('incoming: accept incoming protozoon %u' % iid)
            del self.incoming_id[index]
            del self.incoming_protozoans[iid]
            if self.incoming_surface_cache.has_key(iid):
                del self.incoming_surface_cache[iid]
            self._update_incomming_gui()
        return protozoon

    def decline_protozoon(self, index):
        """ Remove protozoon from incoming list
        pre: 0 <= index < self._capacity
        """
        if index < len(self.incoming_protozoans):
#            ka_debug.info('incoming: decline incoming protozoon %u' % index)
            iid = self.incoming_id[index]
            del self.incoming_id[index]
            del self.incoming_protozoans[iid]
            if self.incoming_surface_cache.has_key(iid):
                del self.incoming_surface_cache[iid]
            self._update_incomming_gui()

    def task_render(self, task, *args, **kwargs):
        """Draw protozoon to surface cache.
        pre: len(args) == 4
        """
        protozoon, iid, width, height = \
                                             args[0], args[1], args[2], args[3]
#        ka_debug.info('incoming: task_render entry: ' + str(iid))
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
        ctx = cairo.Context(surface)
        protozoon.render(task, ctx, width, height)
        if self.incoming_protozoans.has_key(iid):
            self.incoming_surface_cache[iid] = surface
#        ka_debug.info('incoming: task_render exit: ' + str(iid))
        return iid

    def on_image_completed(self, *args):
        """Rendering task has finished"""
#        ka_debug.info('incoming: on_image_completed: ' + str(args[0]))
        self._update_incomming_gui()

    def on_incomingarea_expose(self, widget, dummy):
        """ Repaint image of a single protozoon inside incoming area.
        pre: widget is not None
        """
#        ka_debug.info('incoming: on_incomingarea_expose: ' + widget.name)
        self.draw(ka_controller.name_to_index(widget.name), \
                           ka_controller.create_context(widget), \
                           widget.allocation.width, widget.allocation.height)

    def draw(self, index, ctx, width, height):
        """ Repaint one protozoon image inside incoming area.
        pre: 0 <= index < self._capacity
        pre: ctx is not None
        pre: width > 0
        pre: height > 0
        """
        iid = -1
        if index < len(self.incoming_id):
            iid = self.incoming_id[index]
        if self.incoming_surface_cache.has_key(iid):
            # draw protozoon to screen
            ctx.set_operator(cairo.OPERATOR_SOURCE)
            ctx.set_source_surface(self.incoming_surface_cache[iid])
            ctx.paint()
        else:
            ctx.set_source_rgba(0.65, 0.65, 0.65, 0.0)
            ctx.rectangle(0.0, 0.0, width, height)
            ctx.fill()

    def _update_incomming_gui(self):
        for index in range(self._capacity):
            self._widget_tree.get_widget('incomingbutton_' + str(index)). \
                  set_sensitive(len(self.incoming_id) > index)
        self._inbox_widget.queue_draw()