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
|
# 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 cairo
import ka_debug
import model_locus
import model_allele
import model_treenode
import exon_color
from gettext import gettext as _
TRUNK = '/'
class Protozoon(model_allele.Allele):
"""
inv: self.treenode is not None
#CLEAR inv: isinstance(self.background, exon_color.Color)
"""
def __init__(self):
super(Protozoon, self).__init__(TRUNK)
self.treenode = model_treenode.TreeNode(TRUNK)
self.background = exon_color.Color(self.path, 0, 0, 0, 1)
def dot(self):
result = ""
anchor = ka_debug.dot_id(self) + ' -> '
for ref in [self.treenode, self.background]:
result += ka_debug.dot_ref(anchor, ref)
return result
def __eq__(self, other):
"""Equality based on layers quantity and content."""
equal = isinstance(other, Protozoon) \
and self.treenode == other.treenode \
and self.background == other.background
return equal
def randomize(self):
"""Randomize the protozoons components.
"""
self.treenode.randomize()
self.background.randomize()
def mutate(self):
"""Make random changes to the protozoon.
"""
self.treenode.mutate()
# mutate background color
self.background.mutate()
def swap_places(self):
"""Delegate swapping to the protozoons components."""
self.treenode.swap_places()
def crossingover(self, other):
"""Returns a deep copy mixed from my protozoon and the other protozoon.
pre: isinstance(other, Protozoon)
# check for distinct references, needs to copy content, not references
post: __return__ is not self
post: __return__ is not other
post: model_locus.unique_check(__return__, self, other) == ''
"""
# deep copy
new_one = Protozoon()
# crossing over the layers
new_one.treenode = self.treenode.crossingover(other.treenode).copy()
# crossing over the background color
new_one.background = self.background.crossingover(other.background)
return new_one
def render(self, task, ctx, width, height):
"""
pre: ctx is not None
pre: width > 0
pre: height > 0
pre: width == height
"""
ctx.save()
ctx.scale(width, height)
ctx.translate(0.5, 0.5)
# paint background
# ctx.set_operator(cairo.OPERATOR_CLEAR)
ctx.set_operator(cairo.OPERATOR_SOURCE)
rgb = self.background.rgba
ctx.set_source_rgb(rgb[0], rgb[1], rgb[2])
ctx.paint()
self.treenode.render(task, ctx, width, height)
ctx.restore()
def explain(self, task, formater):
"""Explain all layers and mergers."""
# begin with header
titel = 'protozoon ' + self.get_unique_id()
formater.header(titel)
# display combined layers
width = height = 256
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
ctx = cairo.Context(surface)
# ctx.set_operator(cairo.OPERATOR_SOURCE)
# ctx.set_source_rgb(0, 0, 0)
# ctx.paint()
self.render(task, ctx, width, height)
formater.surface_item(surface, 'Final image, all layers are combined', titel)
# explain all layers from top to bottom
self.treenode.explain(task, formater)
#explain background color
formater.color_item(self.background, 'background color:', alpha=False)
# stop with footer
formater.footer()
def copy(self):
""" The protozoons copy constructor.
"""
new_one = Protozoon()
new_one.treenode = self.treenode.copy()
new_one.background = self.background.copy()
return new_one
|