Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNostalghia <b.vehikel@googlemail.com>2010-05-30 07:06:22 (GMT)
committer Nostalghia <b.vehikel@googlemail.com>2010-05-30 07:06:22 (GMT)
commit1c85682c78b69b1799c190bafa898ca2ca1bc2ca (patch)
tree493034bb82009b2fcc465a690f6e5ee2daf13e0e
parent0e093254c3c3e10569fa3a199d5609695bcb395f (diff)
A simple version of a layer showing bitmap images added.
-rw-r--r--activity/activity.info2
-rw-r--r--ep_layer_image.py211
-rw-r--r--ep_stamp_svg.py2
-rw-r--r--exon_direction.py2
-rw-r--r--exon_position.py2
-rw-r--r--ka_importer.py2
-rw-r--r--model_constraintpool.py3
-rw-r--r--model_locus.py13
-rw-r--r--model_population.py1
-rw-r--r--model_protozoon.py2
-rw-r--r--model_treenode.py4
-rw-r--r--test_model.py2
-rw-r--r--test_suite.py10
13 files changed, 236 insertions, 20 deletions
diff --git a/activity/activity.info b/activity/activity.info
index 0add1fc..4fd3f25 100644
--- a/activity/activity.info
+++ b/activity/activity.info
@@ -3,6 +3,6 @@ name = Kandid
service_name = net.sourceforge.kandid
class = activity.KandidActivity
icon = activity-kandid
-activity_version = 6
+activity_version = 7
show_launcher = yes
mime_types = application/x-kandid-activity;
diff --git a/ep_layer_image.py b/ep_layer_image.py
new file mode 100644
index 0000000..8934c70
--- /dev/null
+++ b/ep_layer_image.py
@@ -0,0 +1,211 @@
+# 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 random
+import os
+import cairo
+import model_locus
+import model_layer
+import model_random
+import ka_importer
+import ka_debug
+import model_constraintpool
+from gettext import gettext as _
+
+# Cairo's compositing operators
+# OPERATOR_ADD = 12
+# OPERATOR_ATOP = 5
+# OPERATOR_CLEAR = 0
+# OPERATOR_DEST = 6
+# OPERATOR_DEST_ATOP = 10
+# OPERATOR_DEST_IN = 8
+# OPERATOR_DEST_OUT = 9
+# OPERATOR_DEST_OVER = 7
+# OPERATOR_IN = 3
+# OPERATOR_OUT = 4
+# OPERATOR_OVER = 2
+# OPERATOR_SATURATE = 13
+# OPERATOR_SOURCE = 1
+# OPERATOR_XOR = 11
+
+OPERATOR_CONSTRAINT = 'operatorconstraint'
+
+class ImageLayer(model_layer.Layer):
+ """ImageLayer
+ """
+
+ cdef = [{'bind' : OPERATOR_CONSTRAINT,
+ 'name' : 'Permitted operators for combining two layers',
+ 'domain': model_constraintpool.INT_M_OF_N,
+ 'enum' : [('add', cairo.OPERATOR_ADD),
+ ('atop', cairo.OPERATOR_ATOP),
+# not usable ('clear', cairo.OPERATOR_CLEAR),
+# not usable ('destination', cairo.OPERATOR_DEST),
+ ('destination atop', cairo.OPERATOR_DEST_ATOP),
+ ('destination in', cairo.OPERATOR_DEST_IN),
+ ('destination out', cairo.OPERATOR_DEST_OUT),
+ ('destination over', cairo.OPERATOR_DEST_OVER),
+ ('in', cairo.OPERATOR_IN),
+ ('out', cairo.OPERATOR_OUT),
+ ('over', cairo.OPERATOR_OVER),
+ ('saturate', cairo.OPERATOR_SATURATE),
+ ('source', cairo.OPERATOR_SOURCE),
+ ('xor', cairo.OPERATOR_XOR),],},
+ ]
+
+ OPERATORS = [o[1] for o in cdef[0]['enum']]
+ OPERATOR_NAMES = {}
+ for o in cdef[0]['enum']:
+ OPERATOR_NAMES[o[1]] = o[0]
+
+ def __init__(self, trunk):
+ """Constructor for bitmap image layer."""
+ super(ImageLayer, self).__init__(trunk)
+ self.theme = 'StockPhotos' #TODO 'theme' in den allen Methoden verwenden.
+ self.selected_image = ''
+ self.alpha_blending = 1.0
+ self.operator = cairo.OPERATOR_ADD
+
+ def __eq__(self, other):
+ """Equality based on the cells color components."""
+ equal = isinstance(other, ImageLayer) \
+ and super(ImageLayer, self).__eq__(other) \
+ and self.selected_image == other.selected_image \
+ and self.alpha_blending == other.alpha_blending \
+ and self.operator == other.operator
+ return equal
+
+ def randomize(self):
+ """Randomize the layers components."""
+ super(ImageLayer, self).randomize()
+ cpool = model_constraintpool.ConstraintPool.get_pool()
+ operator_constraint = cpool.get(self, OPERATOR_CONSTRAINT)
+ self.operator = random.choice(operator_constraint)
+ image_list = ka_importer.get_rgb_image_list(self.theme)
+ if len(image_list) > 0:
+ self.selected_image = \
+ image_list[random.randint(0, len(image_list)-1)]
+ self.alpha_blending = random.random()
+
+ def mutate(self):
+ """Make small random changes to the layers components."""
+ super(ImageLayer, self).mutate()
+ cpool = model_constraintpool.ConstraintPool.get_pool()
+ operator_constraint = cpool.get(self, OPERATOR_CONSTRAINT)
+ if model_random.is_mutating():
+ self.operator = random.choice(operator_constraint)
+ if model_random.is_mutating():
+ image_list = ka_importer.get_rgb_image_list(self.theme)
+ if len(image_list) > 0:
+ self.selected_image = \
+ image_list[random.randint(0, len(image_list)-1)]
+ if model_random.is_mutating():
+ self.alpha_blending += model_random.jitter(0.2)
+ self.alpha_blending = model_random.limit(self.alpha_blending)
+
+ def swap_places(self):
+ """Do nothing."""
+
+ def crossingover(self, other):
+ """
+ pre: isinstance(other, ImageLayer)
+ pre: isinstance(self, ImageLayer)
+ # 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) == ''
+ """
+ new_one = ImageLayer(self.get_trunk())
+ cross_sequence = self.crossingover_base(new_one, other, 3)
+ new_one.operator = other.operator if cross_sequence[0] \
+ else self.operator
+ new_one.selected_image = self.selected_image if cross_sequence[1] \
+ else other.selected_image
+ new_one.alpha_blending = self.alpha_blending if cross_sequence[2] \
+ else other.alpha_blending
+ return new_one
+
+ def render(self, task, ctx, width, height):
+ """
+ pre: ctx is not None
+ pre: width > 0
+ pre: height > 0
+ pre: width == height
+ """
+ self.begin_render(ctx, width, height)
+
+ if os.path.isfile(self.selected_image):
+ png_surface = cairo.ImageSurface.create_from_png(self.selected_image)
+ target_surface = ctx.get_target()
+ png_ctx = cairo.Context(png_surface)
+ png_ctx.set_operator(self.operator)
+# ka_debug.matrix_s(png_ctx.get_matrix())
+ png_ctx.scale(float(png_surface.get_width()) / width,
+ float(png_surface.get_height()) / height)
+ png_ctx.set_source_surface(target_surface)
+ png_ctx.paint_with_alpha(self.alpha_blending)
+ png_surface.write_to_png('/dev/shm/img_' + self.get_unique_id() + '.png')
+
+ ctx.save()
+# ka_debug.matrix_r(ctx.get_matrix())
+ ctx.translate(-0.5, -0.5)
+# ka_debug.matrix_s(ctx.get_matrix())
+# ctx.scale(-1.0, -1.0)
+# ka_debug.matrix_r(ctx.get_matrix())
+ ctx.rectangle(0.0, 0.0, 1.0, 1.0)
+ ctx.clip()
+ ctx.scale(1.0/png_surface.get_width(), 1.0/png_surface.get_height())
+ ctx.set_source_surface(png_surface)
+# ctx.paint_with_alpha(self.alpha_blending)
+ ctx.paint()
+ ctx.restore()
+ png_surface.finish()
+
+ def explain(self, formater):
+ """
+ pre: formater is not None
+ """
+ formater.text_item(_('Compositing drawing operator for merging png image: ') \
+ + self.OPERATOR_NAMES[self.operator])
+ formater.alpha_item(self.alpha_blending, _('alfa blendig: %d%%') \
+ % (100*self.alpha_blending))
+ if os.path.isfile(self.selected_image):
+ png_surface = cairo.ImageSurface.create_from_png(self.selected_image)
+ width = height = 48
+ surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
+ ctx = cairo.Context(surface)
+ ctx.scale(float(width), float(height))
+ ctx.scale(1.0/png_surface.get_width(), 1.0/png_surface.get_height())
+ ctx.set_source_surface(png_surface)
+ ctx.paint()
+ formater.surface_item(surface,
+ _('image file:') + self.selected_image,
+ self.selected_image)
+ png_surface.finish()
+
+ def copy(self):
+ """The Voronoi diagram layers copy constructor.
+ # check for distinct references, needs to copy content, not references
+ post: __return__ is not self
+ """
+ new_one = ImageLayer(self.get_trunk())
+ self.copy_base(new_one)
+ new_one.operator = self.operator
+ new_one.theme = self.theme[:]
+ new_one.selected_image = self.selected_image
+ new_one.alpha_blending = self.alpha_blending
+ return new_one
diff --git a/ep_stamp_svg.py b/ep_stamp_svg.py
index a9d18e0..dff20da 100644
--- a/ep_stamp_svg.py
+++ b/ep_stamp_svg.py
@@ -171,7 +171,7 @@ class SvgStamp(model_allele.Allele):
svg = rsvg.Handle(file=svg_pathname)
ctx.translate(-self.dw/2.0+point[0], -self.dh/2.0+point[1])
# ka_debug.matrix(ctx.get_matrix())
- ctx.scale(self.dw/100, self.dh/100)
+ ctx.scale(self.dw/100.0, self.dh/100.0)
# ka_debug.matrix(ctx.get_matrix())
svg.render_cairo(ctx)
svg.close()
diff --git a/exon_direction.py b/exon_direction.py
index 030e25b..5f54efa 100644
--- a/exon_direction.py
+++ b/exon_direction.py
@@ -108,7 +108,7 @@ class Direction(model_allele.Allele):
"""Calculate an icon visualizing the direction vectors"""
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
ctx = cairo.Context(surface)
- ctx.scale(width, height)
+ ctx.scale(float(width), float(height))
# ka_debug.matrix(ctx.get_matrix())
# paint background
ctx.set_operator(cairo.OPERATOR_OVER)
diff --git a/exon_position.py b/exon_position.py
index 899762e..f2a606a 100644
--- a/exon_position.py
+++ b/exon_position.py
@@ -107,7 +107,7 @@ class Position(model_allele.Allele):
def make_icon(position_list, width, height):
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
ctx = cairo.Context(surface)
- ctx.scale(width, height)
+ ctx.scale(float(width), float(height))
# ka_debug.matrix(ctx.get_matrix())
# paint background
ctx.set_operator(cairo.OPERATOR_OVER)
diff --git a/ka_importer.py b/ka_importer.py
index 765899c..86e40c1 100644
--- a/ka_importer.py
+++ b/ka_importer.py
@@ -163,7 +163,7 @@ def _create_icon(is_plus_sign):
width, height = 16, 16
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
ctx = cairo.Context(surface)
- ctx.scale(width, height)
+ ctx.scale(float(width), float(height))
# ka_debug.matrix(ctx.get_matrix())
# paint background
ctx.set_operator(cairo.OPERATOR_OVER)
diff --git a/model_constraintpool.py b/model_constraintpool.py
index 91ad99b..0726c90 100644
--- a/model_constraintpool.py
+++ b/model_constraintpool.py
@@ -137,13 +137,14 @@ class ConstraintPool(object):
# self.set('*', 'mergertypeconstraint', ['combine',])
# self.set('*', 'layertypeconstraint', ['filledspline', ])
# self.set('*', 'layertypeconstraint', ['lca', ])
+# self.set('*', 'layertypeconstraint', ['image', ])
# self.set('*', 'modifiertypeconstraint', ['flip', ])
# self.set('*', 'modifiertypeconstraint', ['border', ])
# self.set('*', 'stamptypeconstraint', ['filledcyclic',])
# self.set('*', 'layertypeconstraint', ['markovchain', ])
# self.set('*', 'samplertypeconstraint', ['randomwalk',])
# self.set('*', 'layertypeconstraint', ['referencepattern', ])
-# self.set('*', 'modifiertypeconstraint', ['border', ])
+# self.set('*', 'modifiertypeconstraint', ['rectangulartile', ])
# self.set('*', 'mergertypeconstraint', ['mask', ])
# self.set('*', 'layertypeconstraint', ['quadtree', ])
# self.set('*', 'colorconstraint', ['colorconstraint_none', ])
diff --git a/model_locus.py b/model_locus.py
index c55b76e..a09d02b 100644
--- a/model_locus.py
+++ b/model_locus.py
@@ -21,7 +21,6 @@ import ka_debug
class Locus(object):
-# @staticmethod
def _random_pattern(self):
pattern = '_'
for dummy in range(6):
@@ -37,10 +36,7 @@ class Locus(object):
pre: isinstance(trunk, str)
pre: not (trunk == '')
"""
- self._pattern = self._random_pattern()
- Locus._modulo_count = Locus._modulo_count + 1 \
- if Locus._modulo_count < 999999 else 0
- self._unique_id = self._pattern + str(Locus._modulo_count)
+ self.create_unique_id()
if trunk == '/':
self.path = '/' + self.__class__.__name__
else:
@@ -58,6 +54,13 @@ class Locus(object):
else:
return '/'
+ def create_unique_id(self):
+ """ unique identification for this protozoon."""
+ self._pattern = self._random_pattern()
+ Locus._modulo_count = Locus._modulo_count + 1 \
+ if Locus._modulo_count < 999999 else 0
+ self._unique_id = self._pattern + str(Locus._modulo_count)
+
def get_unique_id(self):
""" unique identification for this protozoon."""
return self._unique_id
diff --git a/model_population.py b/model_population.py
index c9eea65..163f8e4 100644
--- a/model_population.py
+++ b/model_population.py
@@ -157,6 +157,7 @@ class KandidModel(object):
dummy, dummy, poor = self.classify()
for new_at, protoz in enumerate(self.protozoans):
if protoz in poor:
+ self.protozoans[new_at].create_unique_id()
self.protozoans[new_at].randomize()
self.fitness[new_at] = 3.0
new_indices.append(new_at)
diff --git a/model_protozoon.py b/model_protozoon.py
index 8bd7d61..eb75294 100644
--- a/model_protozoon.py
+++ b/model_protozoon.py
@@ -92,7 +92,7 @@ class Protozoon(model_allele.Allele):
"""
ctx.save()
# ka_debug.matrix_s(ctx.get_matrix())
- ctx.scale(width, height)
+ ctx.scale(float(width), float(height))
# ka_debug.matrix(ctx.get_matrix())
ctx.translate(0.5, 0.5)
# ka_debug.matrix(ctx.get_matrix())
diff --git a/model_treenode.py b/model_treenode.py
index 99f0174..4837153 100644
--- a/model_treenode.py
+++ b/model_treenode.py
@@ -296,7 +296,7 @@ class TreeNode(model_allele.Allele):
new_surface = ctx.get_target().create_similar(cairo.CONTENT_COLOR_ALPHA,
width, height)
new_ctx = cairo.Context(new_surface)
- new_ctx.scale(width, height)
+ new_ctx.scale(float(width), float(height))
# ka_debug.matrix(new_ctx.get_matrix())
new_ctx.translate(0.5, 0.5)
# ka_debug.matrix(new_ctx.get_matrix())
@@ -355,7 +355,7 @@ class TreeNode(model_allele.Allele):
def _preview(self, task, source, formater, width, height):
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
ctx = cairo.Context(surface)
- ctx.scale(width, height)
+ ctx.scale(float(width), float(height))
# ka_debug.matrix(ctx.get_matrix())
ctx.translate(0.5, 0.5)
# ka_debug.matrix(ctx.get_matrix())
diff --git a/test_model.py b/test_model.py
index 7ccfedb..3c74cc9 100644
--- a/test_model.py
+++ b/test_model.py
@@ -484,7 +484,7 @@ class TestKandidModel(unittest.TestCase):
def _create_context(self, width, height):
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
ctx = cairo.Context(surface)
- ctx.scale(width, height)
+ ctx.scale(float(width), float(height))
# ka_debug.matrix(ctx.get_matrix())
# paint background
ctx.set_operator(cairo.OPERATOR_SOURCE)
diff --git a/test_suite.py b/test_suite.py
index b9e3cf8..1d9d1e8 100644
--- a/test_suite.py
+++ b/test_suite.py
@@ -34,10 +34,10 @@ gtk.gdk.threads_init()
alltests = unittest.TestSuite((\
unittest.makeSuite(test_buildingblocks.TestKandidBuildingBlocks), \
unittest.makeSuite(test_layer.TestKandidLayer), \
- unittest.makeSuite(test_exporter.TestKandidExporter), \
- unittest.makeSuite(test_status.TestKandidStatus), \
- unittest.makeSuite(test_history.TestKandidHistory), \
- unittest.makeSuite(test_pages.TestKandidPages), \
- unittest.makeSuite(test_model.TestKandidModel), \
+# unittest.makeSuite(test_exporter.TestKandidExporter), \
+# unittest.makeSuite(test_status.TestKandidStatus), \
+# unittest.makeSuite(test_history.TestKandidHistory), \
+# unittest.makeSuite(test_pages.TestKandidPages), \
+# unittest.makeSuite(test_model.TestKandidModel), \
))
unittest.TextTestRunner(verbosity=2).run(alltests)