From 86e2111461fab98d8c188d50ec8bd0c72c527f73 Mon Sep 17 00:00:00 2001 From: Pootle daemon Date: Mon, 13 Jun 2011 14:23:40 +0000 Subject: Merge branch 'master' of git.sugarlabs.org:turtleart/mainline --- (limited to 'TurtleArt/tablock.py') diff --git a/TurtleArt/tablock.py b/TurtleArt/tablock.py index 05b95ae..bcca2cd 100644 --- a/TurtleArt/tablock.py +++ b/TurtleArt/tablock.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -#Copyright (c) 2010 Walter Bender +#Copyright (c) 2010,11 Walter Bender #Permission is hereby granted, free of charge, to any person obtaining a copy #of this software and associated documentation files (the "Software"), to deal @@ -20,31 +20,21 @@ #THE SOFTWARE. import gtk - from gettext import gettext as _ -from taconstants import EXPANDABLE, EXPANDABLE_BLOCKS, EXPANDABLE_ARGS, \ - PRIMITIVES, OLD_NAMES, BLOCK_SCALE, BLOCK_NAMES, CONTENT_BLOCKS, \ - PALETTES, COLORS, BASIC_STYLE_HEAD, BASIC_STYLE_HEAD_1ARG, \ - BASIC_STYLE_TAIL, BASIC_STYLE, BASIC_STYLE_EXTENDED, BASIC_STYLE_1ARG, \ - BASIC_STYLE_VAR_ARG, BULLET_STYLE, BASIC_STYLE_2ARG, BOX_STYLE, \ - BOX_STYLE_MEDIA, NUMBER_STYLE, NUMBER_STYLE_VAR_ARG, NUMBER_STYLE_BLOCK, \ - NUMBER_STYLE_PORCH, NUMBER_STYLE_1ARG, NUMBER_STYLE_1STRARG, \ - COMPARE_STYLE, BOOLEAN_STYLE, NOT_STYLE, FLOW_STYLE, FLOW_STYLE_TAIL, \ - FLOW_STYLE_1ARG, FLOW_STYLE_BOOLEAN, FLOW_STYLE_WHILE, FLOW_STYLE_ELSE, \ - COLLAPSIBLE_TOP, COLLAPSIBLE_TOP_NO_ARM, COLLAPSIBLE_TOP_NO_LABEL, \ - COLLAPSIBLE_TOP_NO_ARM_NO_LABEL, COLLAPSIBLE_BOTTOM, PORTFOLIO_STYLE_2x2, \ - PORTFOLIO_STYLE_1x1, PORTFOLIO_STYLE_2x1, PORTFOLIO_STYLE_1x2, \ - STANDARD_STROKE_WIDTH, BOX_COLORS, GRADIENT_COLOR, \ - BASIC_STYLE_EXTENDED_VERTICAL, CONSTANTS, INVISIBLE +from taconstants import EXPANDABLE, EXPANDABLE_ARGS, OLD_NAMES, CONSTANTS, \ + STANDARD_STROKE_WIDTH, BLOCK_SCALE, BOX_COLORS, GRADIENT_COLOR +from tapalette import palette_blocks, block_colors, expandable_blocks, \ + content_blocks, block_names, block_primitives, block_styles, \ + special_block_colors from tasprite_factory import SVG, svg_str_to_pixbuf import sprites -import logging -_logger = logging.getLogger('turtleart-activity') +from tautils import debug_output, error_output class Blocks: + """ A class for the list of blocks and everything they share in common """ def __init__(self, font_scale_factor=1, decimal_point='.'): @@ -130,7 +120,9 @@ class Block: """ A class for the individual blocks """ def __init__(self, block_list, sprite_list, name, x, y, type='block', - values=[], scale=BLOCK_SCALE, colors=['#FF0000', '#A00000']): + values=[], scale=BLOCK_SCALE[0], + colors=['#A0A0A0', '#808080']): + self.block_list = block_list self.spr = None self.shapes = [None, None] @@ -150,6 +142,51 @@ class Block: self._font_size = [6.0, 4.5] self._image = None + self.block_methods = { + 'basic-style': self._make_basic_style, + 'blank-style': self._make_blank_style, + 'basic-style-head': self._make_basic_style_head, + 'basic-style-head-1arg': self._make_basic_style_head_1arg, + 'basic-style-tail': self._make_basic_style_tail, + 'basic-style-extended': [self._make_basic_style, 16, 16], + 'basic-style-extended-vertical': [self._make_basic_style, 0, 4], + 'basic-style-1arg': self._make_basic_style_1arg, + 'basic-style-2arg': self._make_basic_style_2arg, + 'basic-style-3arg': self._make_basic_style_3arg, + 'basic-style-var-arg': self._make_basic_style_var_arg, + 'invisible': self._make_invisible_style, + 'bullet-style': self._make_bullet_style, + 'box-style': self._make_box_style, + 'box-style-media': self._make_media_style, + 'number-style': self._make_number_style, + 'number-style-block': self._make_number_style_block, + 'number-style-porch': self._make_number_style_porch, + 'number-style-1arg': self._make_number_style_1arg, + 'number-style-1strarg': self._make_number_style_1strarg, + 'number-style-var-arg': self._make_number_style_var_arg, + 'compare-style': self._make_compare_style, + 'compare-porch-style': self._make_compare_porch_style, + 'boolean-style': self._make_boolean_style, + 'not-style': self._make_not_style, + 'flow-style': self._make_flow_style, + 'flow-style-tail': self._make_flow_style_tail, + 'flow-style-1arg': self._make_flow_style_1arg, + 'flow-style-boolean': self._make_flow_style_boolean, + 'flow-style-while': self._make_flow_style_while, + 'flow-style-else': self._make_flow_style_else, + 'collapsible-top': [self._make_collapsible_style_top, True, True], + 'collapsible-top-no-arm': [self._make_collapsible_style_top, + False, True], + 'collapsible-top-no-label': [self._make_collapsible_style_top, + True, False], + 'collapsible-top-no-arm-no-label': [ + self._make_collapsible_style_top, False, False], + 'collapsible-bottom': self._make_collapsible_style_bottom, + 'portfolio-style-2x2': self._make_portfolio_style_2x2, + 'portfolio-style-1x1': self._make_portfolio_style_1x1, + 'portfolio-style-2x1': self._make_portfolio_style_2x1, + 'portfolio-style-1x2': self._make_portfolio_style_1x2} + if self.name in OLD_NAMES: self.name = OLD_NAMES[self.name] @@ -162,22 +199,38 @@ class Block: # If there is already a block with the same name, reuse it copy_block = None - if self.name not in EXPANDABLE and \ - self.name not in EXPANDABLE_BLOCKS and \ - self.name not in EXPANDABLE_ARGS and \ - self.name not in BOX_STYLE and \ - self.name not in ['sandwichtop', 'sandwichtop_no_label']: + if self.cloneable(): for b in self.block_list.list: if b.scale == self.scale and b.name == self.name: copy_block = b break self._new_block_from_factory(sprite_list, x, y, copy_block) - if name in PRIMITIVES: - self.primitive = PRIMITIVES[self.name] + if name in block_primitives: + self.primitive = block_primitives[self.name] self.block_list.append_to_list(self) + def expandable(self): + """ Can this block be expanded? """ + if self.name in EXPANDABLE: + return True + if self.name in expandable_blocks: + return True + if self.name in EXPANDABLE_ARGS: + return True + return False + + def cloneable(self): + """ Is it safe to clone this block? """ + if self.expandable(): + return False + if self.name in block_styles['box-style']: + return False + if self.name in ['sandwichtop', 'sandwichtop_no_label']: + return False + return True + def highlight(self): """ We may want to highlight a block... """ if self.spr is not None: @@ -310,7 +363,6 @@ class Block: return (self.ex, self.ey) def _new_block_from_factory(self, sprite_list, x, y, copy_block=None): - self.svg = SVG() self.svg.set_scale(self.scale) self.svg.set_innie([False]) @@ -335,10 +387,7 @@ class Block: self.shapes[1] = copy_block.shapes[1] self.docks = copy_block.docks[:] else: - if (self.name in EXPANDABLE or \ - self.name in EXPANDABLE_BLOCKS or \ - self.name in EXPANDABLE_ARGS) and \ - self.type == 'block': + if self.expandable() and self.type == 'block': self.svg.set_show(True) self._make_block(self.svg) @@ -358,11 +407,11 @@ class Block: else: self._set_labels(i, str(v)) elif self.type == 'block' and self.name in CONSTANTS: - self._set_labels(0, BLOCK_NAMES[self.name][0] + ' = ' + \ + self._set_labels(0, block_names[self.name][0] + ' = ' + \ str(CONSTANTS[self.name])) - elif self.name in BLOCK_NAMES: - for i, n in enumerate(BLOCK_NAMES[self.name]): + elif self.name in block_names: + for i, n in enumerate(block_names[self.name]): self._set_labels(i, n) if copy_block is None: @@ -374,18 +423,24 @@ class Block: self.svg.margins[2], self.svg.margins[3]) def _set_label_attributes(self): - if self.name in CONTENT_BLOCKS: + if self.name in content_blocks: n = len(self.values) if n == 0: n = 1 # Force a scale to be set, even if there is no value. else: - if self.name in BLOCK_NAMES: - n = len(BLOCK_NAMES[self.name]) + if self.name in block_names: + n = len(block_names[self.name]) else: - _logger.debug('WARNING: unknown block name %s' % (self.name)) + debug_output('WARNING: unknown block name %s' % (self.name)) n = 0 for i in range(n): - if i == 1: # top + if self.name in block_styles['compare-porch-style']: + self.spr.set_label_attributes(int(self._font_size[0] + 0.5), + True, 'center', 'bottom', i) + elif self.name in block_styles['number-style-porch']: + self.spr.set_label_attributes(int(self._font_size[0] + 0.5), + True, 'right', 'bottom', i) + elif i == 1: # top self.spr.set_label_attributes(int(self._font_size[1] + 0.5), True, 'right', 'top', i) elif i == 2: # bottom @@ -405,92 +460,26 @@ class Block: self._bottom = 0 self.svg.set_stroke_width(STANDARD_STROKE_WIDTH) self.svg.clear_docks() - if self.name in BASIC_STYLE: - self._make_basic_style(svg) - elif self.name in BASIC_STYLE_HEAD: - self._make_basic_style_head(svg) - elif self.name in BASIC_STYLE_EXTENDED: - self._make_basic_style(svg, 16, 16) - elif self.name in BASIC_STYLE_EXTENDED_VERTICAL: - self._make_basic_style(svg, 0, 4) - elif self.name in BASIC_STYLE_HEAD_1ARG: - self._make_basic_style_head_1arg(svg) - elif self.name in BASIC_STYLE_TAIL: - self._make_basic_style_tail(svg) - elif self.name in BASIC_STYLE_1ARG: - self._make_basic_style_1arg(svg) - elif self.name in BASIC_STYLE_2ARG: - self._make_basic_style_2arg(svg) - elif self.name in BASIC_STYLE_VAR_ARG: - self._make_basic_style_var_arg(svg) - elif self.name in BULLET_STYLE: - self._make_bullet_style(svg) - elif self.name in BOX_STYLE: - self._make_box_style(svg) - elif self.name in BOX_STYLE_MEDIA: - self._make_media_style(svg) - elif self.name in NUMBER_STYLE: - self._make_number_style(svg) - elif self.name in NUMBER_STYLE_BLOCK: - self._make_number_style_block(svg) - elif self.name in NUMBER_STYLE_VAR_ARG: - self._make_number_style_var_arg(svg) - elif self.name in NUMBER_STYLE_1ARG: - self._make_number_style_1arg(svg) - elif self.name in NUMBER_STYLE_1STRARG: - self._make_number_style_1strarg(svg) - elif self.name in NUMBER_STYLE_PORCH: - self._make_number_style_porch(svg) - elif self.name in COMPARE_STYLE: - self._make_compare_style(svg) - elif self.name in BOOLEAN_STYLE: - self._make_boolean_style(svg) - elif self.name in NOT_STYLE: - self._make_not_style(svg) - elif self.name in FLOW_STYLE: - self._make_flow_style(svg) - elif self.name in FLOW_STYLE_TAIL: - self._make_flow_style_tail(svg) - elif self.name in FLOW_STYLE_1ARG: - self._make_flow_style_1arg(svg) - elif self.name in FLOW_STYLE_BOOLEAN: - self._make_flow_style_boolean(svg) - elif self.name in FLOW_STYLE_WHILE: - self._make_flow_style_while(svg) - elif self.name in FLOW_STYLE_ELSE: - self._make_flow_style_else(svg) - elif self.name in COLLAPSIBLE_TOP: - self._make_collapsible_style_top(svg, arm=True, label=True) - elif self.name in COLLAPSIBLE_TOP_NO_ARM: - self._make_collapsible_style_top(svg, arm=False, label=True) - elif self.name in COLLAPSIBLE_TOP_NO_LABEL: - self._make_collapsible_style_top(svg, arm=True, label=False) - elif self.name in COLLAPSIBLE_TOP_NO_ARM_NO_LABEL: - self._make_collapsible_style_top(svg, arm=False, label=False) - elif self.name in COLLAPSIBLE_BOTTOM: - self._make_collapsible_style_bottom(svg) - elif self.name in INVISIBLE: - self._make_invisible_style(svg) - elif self.name in PORTFOLIO_STYLE_2x2: - self._make_portfolio_style_2x2(svg) - elif self.name in PORTFOLIO_STYLE_2x1: - self._make_portfolio_style_2x1(svg) - elif self.name in PORTFOLIO_STYLE_1x1: - self._make_portfolio_style_1x1(svg) - elif self.name in PORTFOLIO_STYLE_1x2: - self._make_portfolio_style_1x2(svg) - else: - self._make_basic_style(svg) - _logger.debug("WARNING: I don't know how to create a %s block" % \ - (self.name)) + for k in block_styles.keys(): + if self.name in block_styles[k]: + if type(self.block_methods[k]) == type([]): + self.block_methods[k][0](svg, self.block_methods[k][1], + self.block_methods[k][2]) + else: + self.block_methods[k](svg) + return + error_output('block type not found %s' % (self.name)) + self.block_methods['basic-style'](svg) def _set_colors(self, svg): if self.name in BOX_COLORS: self.colors = BOX_COLORS[self.name] + elif self.name in special_block_colors: + self.colors = special_block_colors[self.name] else: - for p in range(len(PALETTES)): - if self.name in PALETTES[p]: - self.colors = COLORS[p] + for p in range(len(palette_blocks)): + if self.name in palette_blocks[p]: + self.colors = block_colors[p] self.svg.set_colors(self.colors) def _make_basic_style(self, svg, extend_x=0, extend_y=0): @@ -500,6 +489,13 @@ class Block: self.svg.docks[0][1]], ['flow', False, self.svg.docks[1][0], self.svg.docks[1][1]]] + def _make_blank_style(self, svg, extend_x=0, extend_y=0): + self.svg.expand(self.dx + self.ex + extend_x, self.ey + extend_y) + self.svg.set_slot(False) + self.svg.set_tab(False) + self._make_block_graphics(svg, self.svg.basic_block) + self.docks = [] + def _make_basic_style_head(self, svg): self.svg.expand(10 + self.dx + self.ex, self.ey) self.svg.set_slot(False) @@ -553,6 +549,21 @@ class Block: ['flow', False, self.svg.docks[3][0], self.svg.docks[3][1]]] + def _make_basic_style_3arg(self, svg): + self.svg.expand(10 + self.dx + self.ex, self.ey) + self.svg.set_innie([True, True, True]) + self._make_block_graphics(svg, self.svg.basic_block) + self.docks = [['flow', True, self.svg.docks[0][0], + self.svg.docks[0][1]], + ['number', False, self.svg.docks[1][0], + self.svg.docks[1][1]], + ['number', False, self.svg.docks[2][0], + self.svg.docks[2][1]], + ['number', False, self.svg.docks[3][0], + self.svg.docks[3][1]], + ['flow', False, self.svg.docks[4][0], + self.svg.docks[4][1]]] + def _make_basic_style_var_arg(self, svg): self.svg.expand(10 + self.dx + self.ex, self.ey) innie = [True] @@ -706,6 +717,10 @@ class Block: self.svg.docks[2][1]], ['unavailable', False, 0, 0, ')']] + def _make_compare_porch_style(self, svg): + self.svg.set_porch(True) + self._make_compare_style(svg) + def _make_boolean_style(self, svg): self.svg.expand(10 + self.dx + self.ex, self.ey) self._make_block_graphics(svg, self.svg.boolean_and_or) -- cgit v0.9.1