[Sugar-devel] [PATCH] Copy and Paste feature in Abacus Activity (#2324)

Walter Bender walter.bender at gmail.com
Sat Oct 2 07:27:08 EDT 2010


On Sat, Oct 2, 2010 at 2:30 AM,  <shanjit at seeta.in> wrote:
> From: Shanjit Singh Jajmann <shanjit at dev.seeta.in>
>
> Values can now be copied to the clipboard. Values can also now be pasted from the clipboard to the abacus activity.

I think that the essence of your patch will work, but you seem to have
generated from a version of the code that was slightly out of date and
hence it also includes the undoing of many recent changes, which I
don't think is your intension. Comments in line.

> ---
>  AbacusActivity.py |   77 +++++++++++--------
>  abacus_window.py  |   62 ++++++---------
>  sprites.py        |  222 +++++++++++++++++++++++------------------------------
>  3 files changed, 168 insertions(+), 193 deletions(-)
>
> diff --git a/AbacusActivity.py b/AbacusActivity.py
> index d14f58a..480e351 100644
> --- a/AbacusActivity.py
> +++ b/AbacusActivity.py
> @@ -38,11 +38,11 @@ import locale
>  import logging
>  _logger = logging.getLogger("abacus-activity")
>
> -from abacus_window import Abacus, Custom, Suanpan, Soroban, Schety,\
> +from abacus_window import Abacus,AbacusGeneric , Custom, Suanpan, Soroban, Schety,\
>                           Nepohualtzintzin, Binary, Hex, Decimal, Fractions,\
>                           Caacupe, Cuisenaire

Why are you importing AbacusGeneric?

(Also, throughout, please add a space after a comma.)

>
> -def _button_factory(icon_name, tooltip, callback, toolbar):
> +def _button_factory(icon_name, tooltip, callback, toolbar, accelerator = None):
>     """Factory for making toolbar buttons"""
>     my_button = ToolButton( icon_name )
>     my_button.set_tooltip(tooltip)
> @@ -100,7 +100,7 @@ class AbacusActivity(activity.Activity):
>
>         _abacus_toolbar = gtk.Toolbar()
>         _custom_toolbar = gtk.Toolbar()
> -
> +        edit_toolbar = gtk.Toolbar()

Whether or not the current naming convention of using an _ for local
variable names is correct, you should be consistent.

>         if _new_sugar_system:
>             # Use 0.86 toolbar design
>             toolbox = ToolbarBox()
> @@ -127,6 +127,14 @@ class AbacusActivity(activity.Activity):
>             toolbox.toolbar.insert(_custom_toolbar_button, -1)
>             _custom_toolbar_button.show()
>
> +            edit_toolbar_button = ToolbarButton(label=_('Edit'),
> +                                                page=edit_toolbar,
> +                                                icon_name='toolbar-edit')
> +            edit_toolbar_button.show()
> +            toolbox.toolbar.insert(edit_toolbar_button, -1)
> +            edit_toolbar_button.show()
> +
> +

Why two newlines?

>             _separator_factory(toolbox.toolbar, True, False)
>
>             stop_button = StopButton(self)
> @@ -136,6 +144,7 @@ class AbacusActivity(activity.Activity):
>
>             self.set_toolbox(toolbox)
>             _abacus_toolbar_button.set_expanded(True)
> +            edit_toolbar_button.set_expanded(True)

The set_expanded call here is to circumvent a bug with activation of
the accelerator keys. (I don't recall the bug number, but you should
reference it with comment. Also, you need to expand the edit toolbar
before you expand the abacus toolbar or the wrong toolbar will be open
upon launch.

>             toolbox.show()
>
>         else:
> @@ -145,7 +154,8 @@ class AbacusActivity(activity.Activity):
>
>             toolbox.add_toolbar( _('Project'), _abacus_toolbar )
>             toolbox.add_toolbar( _('Custom'), _custom_toolbar )
> -
> +            toolbox.add_toolbar(_('Edit'), edit_toolbar)
> +
>             self._basic_abacus(_abacus_toolbar)
>
>             toolbox.set_current_toolbar(1)
> @@ -156,18 +166,19 @@ class AbacusActivity(activity.Activity):
>             elif hasattr(toolbox, 'props'):
>                toolbox.props.visible = False
>
> +        self.copyme = ' '

Where is this used?

>         # Add the buttons and spinners to the toolbars
>         self.japanese = _button_factory("soroban-off", _('Soroban'),
>                                         self._japanese_cb, _abacus_toolbar)
> +        self.russian = _button_factory("schety-off", _('Schety'),
> +                                       self._russian_cb, _abacus_toolbar)
>         self.mayan = _button_factory("nepohualtzintzin-off",
>                                      _('Nepohualtzintzin'),
>                                      self._mayan_cb, _abacus_toolbar)
> -        self.hex = _button_factory("hexadecimal-off", _('Hexadecimal'),
> -                                   self._hex_cb, _abacus_toolbar)
>         self.binary = _button_factory("binary-off", _('Binary'),
>                                       self._binary_cb, _abacus_toolbar)
> -        self.russian = _button_factory("schety-off", _('Schety'),
> -                                       self._russian_cb, _abacus_toolbar)
> +        self.hex = _button_factory("hex-off", _('Hexadecimal'),self._hex_cb,
> +                                   _abacus_toolbar)
>         self.fraction = _button_factory("fraction-off", _('Fraction'),
>                                         self._fraction_cb, _abacus_toolbar)
>         self.caacupe = _button_factory("caacupe-off", _('Caacupé'),
> @@ -195,6 +206,11 @@ class AbacusActivity(activity.Activity):
>         self.custom = _button_factory("new-game", _('Custom'),
>                                       self._custom_cb, _custom_toolbar)
>
> +        copy = _button_factory('edit-copy',_('Copy'), self._copy_cb,
> +                          edit_toolbar_button, accelerator='<Ctrl>c')
> +        paste = _button_factory('edit-paste', _('Paste'), self._paste_cb,
> +                          edit_toolbar_button, accelerator='<Ctrl>v')
> +
>         self.toolbox.show()
>
>         # Create a canvas
> @@ -207,7 +223,8 @@ class AbacusActivity(activity.Activity):
>
>         # Initialize the canvas
>         self.abacus = Abacus(canvas, self)
> -
> +        #self.abacusgeneric=AbacusGeneric(self)
> +
>         # Read the current mode from the Journal
>         try:
>             self._rods_spin.set_value(int(self.metadata['rods']))
> @@ -264,7 +281,7 @@ class AbacusActivity(activity.Activity):
>         self.russian.set_icon("schety-off")
>         self.mayan.set_icon("nepohualtzintzin-off")
>         self.binary.set_icon("binary-off")
> -        self.hex.set_icon("hexadecimal-off")
> +        self.hex.set_icon("hex-off")
>         self.fraction.set_icon("fraction-off")
>         self.caacupe.set_icon("caacupe-off")
>         self.cuisenaire.set_icon("cuisenaire-off")
> @@ -318,7 +335,6 @@ class AbacusActivity(activity.Activity):
>
>     def _custom_cb(self, button):
>         """ Display the custom abacus; hide the others """
> -        value = float(self.abacus.mode.value(count_beads=False))
>         if self.abacus.custom is not None:
>             self.abacus.custom.hide()
>         self.abacus.custom = Custom(self.abacus,
> @@ -328,28 +344,20 @@ class AbacusActivity(activity.Activity):
>                              self._value_spin.get_value_as_int(),
>                              self._base_spin.get_value_as_int())
>         self._select_abacus(None, None, self.abacus.custom)
> -        self.abacus.mode.set_value_from_number(value)
> -        self.abacus.mode.label(self.abacus.generate_label())
>
>     def _chinese_cb(self, button):
>         """ Display the suanpan; hide the others """
> -        value = float(self.abacus.mode.value(count_beads=False))
>         if self.abacus.chinese is None:
>             self.abacus.chinese = Suanpan(self.abacus)
>         self._select_abacus(self.chinese, self.abacus.chinese.name+"-on",
>                             self.abacus.chinese)
> -        self.abacus.mode.set_value_from_number(value)
> -        self.abacus.mode.label(self.abacus.generate_label())
>
>     def _japanese_cb(self, button):
>         """ Display the soroban; hide the others """
> -        value = float(self.abacus.mode.value(count_beads=False))
>         if self.abacus.japanese is None:
>             self.abacus.japanese = Soroban(self.abacus)
>         self._select_abacus(self.japanese, self.abacus.japanese.name+"-on",
>                             self.abacus.japanese)
> -        self.abacus.mode.set_value_from_number(value)
> -        self.abacus.mode.label(self.abacus.generate_label())
>
>     def _russian_cb(self, button):
>         """ Display the schety; hide the others """
> @@ -360,43 +368,31 @@ class AbacusActivity(activity.Activity):
>
>     def _mayan_cb(self, button):
>         """ Display the nepohualtzintzin; hide the others """
> -        value = float(self.abacus.mode.value(count_beads=False))
>         if self.abacus.mayan is None:
>             self.abacus.mayan = Nepohualtzintzin(self.abacus)
>         self._select_abacus(self.mayan, self.abacus.mayan.name+"-on",
>                             self.abacus.mayan)
> -        self.abacus.mode.set_value_from_number(value)
> -        self.abacus.mode.label(self.abacus.generate_label())
>
>     def _binary_cb(self, button):
>         """ Display the binary; hide the others """
> -        value = float(self.abacus.mode.value(count_beads=False))
>         if self.abacus.binary is None:
>             self.abacus.binary = Binary(self.abacus)
>         self._select_abacus(self.binary, self.abacus.binary.name+"-on",
>                             self.abacus.binary)
> -        self.abacus.mode.set_value_from_number(value)
> -        self.abacus.mode.label(self.abacus.generate_label())
>
>     def _hex_cb(self, button):
>         """ Display the hex; hide the others """
> -        value = float(self.abacus.mode.value(count_beads=False))
>         if self.abacus.hex is None:
>             self.abacus.hex = Hex(self.abacus)
>         self._select_abacus(self.hex, self.abacus.hex.name+"-on",
>                             self.abacus.hex)
> -        self.abacus.mode.set_value_from_number(value)
> -        self.abacus.mode.label(self.abacus.generate_label())
>
>     def _decimal_cb(self, button):
>         """ Display the decimal; hide the others """
> -        value = float(self.abacus.mode.value(count_beads=False))
>         if self.abacus.decimal is None:
>             self.abacus.decimal = Decimal(self.abacus)
>         self._select_abacus(self.decimal, self.abacus.decimal.name+"-on",
>                             self.abacus.decimal)
> -        self.abacus.mode.set_value_from_number(value)
> -        self.abacus.mode.label(self.abacus.generate_label())
>
>     def _fraction_cb(self, button):
>         """ Display the fraction; hide the others """
> @@ -434,3 +430,22 @@ class AbacusActivity(activity.Activity):
>         except:
>             pass

None of the above changes are relevant to your patch and should not be included.

>
> +    def _copy_cb(self, arg = None):
> +        """ Copy a number to the clipboard from the active slide. """
> +        clipBoard = gtk.Clipboard()
> +        if self.abacus.generate_label() is not None:
> +            copyme = self.abacus.generate_label()#last.labels
> +        if copyme is not None:
> +            clipBoard.set_text(str(copyme))
> +        return

Seems overly redundant. Also, did we not agree to copy just the sum?
Will not this code copy all of the rod values as well?

> +
> +    def _paste_cb(self, arg = None):
> +        """ Paste a number from the clipboard to the active silde. """
> +        clipBoard = gtk.Clipboard()
> +        text = clipBoard.wait_for_text()
> +        if text is not None:
> +            self.abacus.set_value_from_number(text)
> +        return
> +

Maybe check text to make sure it is a number?

> +
> +
> diff --git a/abacus_window.py b/abacus_window.py
> index 9e27f0e..a1c60fb 100644
> --- a/abacus_window.py
> +++ b/abacus_window.py

Are any of the changes below related to your patch?

> @@ -435,7 +435,16 @@ class Abacus():
>     def _destroy_cb(self, win, event):
>         """ Callback to handle quit """
>         gtk.main_quit()
> -
> +
> +    def set_value_from_number(self, text):
> +        """ Set abacus to value in string """
> +        number = float(text)
> +        for r in range(self.mode.num_rods):
> +            number -= self.mode.set_rod(r,number)
> +            if number == 0:
> +                break
> +        self.mode.label(self.generate_label())
> +
>     def generate_label(self, sum_only=False):
>         """ The complexity below is to make the label as simple as possible """
>         sum = ""
> @@ -678,24 +687,7 @@ class AbacusGeneric():
>         for r in range(self.num_rods):
>             self.set_rod_value(r, v[r])
>         return
> -
> -    def max_value(self):
> -        """ Maximum value possible on abacus """
> -        max = 0
> -        for bead in self.beads:
> -            max += bead.value
> -        return max
> -
> -    def set_value_from_number(self, number):
> -        """ Set abacus to value in string """
> -        print "setting %s to %f" % (self.name, number)
> -        self.reset_abacus()
> -        if number <= self.max_value():
> -            for r in range(self.num_rods):
> -                number -= self.set_rod(r, number)
> -                if number == 0:
> -                    break
> -
> +
>     def set_rod(self, rod, number):
>         bead = rod * (self.top_beads + self.bot_beads)
>         bead_value = self.beads[bead + self.top_beads].value
> @@ -757,24 +749,22 @@ class AbacusGeneric():
>
>     def value(self, count_beads=False):
>         """ Return a string representing the value of each rod. """
> +        string = ''
> +        v = []
> +        for r in range(self.num_rods+1): # +1 for overflow
> +            v.append(0)
>
> -        if count_beads:
> -            # Save the value associated with each rod as a 2-byte integer.
> -            string = ''
> -            v = []
> -            for r in range(self.num_rods+1): # +1 for overflow
> -                v.append(0)
> -
> -            # Tally the values on each rod.
> -            for i, bead in enumerate(self.beads):
> -                r = i/(self.top_beads+self.bot_beads)
> -                j = i % (self.top_beads+self.bot_beads)
> -                if bead.get_state() == 1:
> -                    if j < self.top_beads:
> -                        v[r+1] += self.top_factor
> -                    else:
> -                        v[r+1] += 1
> +        # Tally the values on each rod.
> +        for i, bead in enumerate(self.beads):
> +            r = i/(self.top_beads+self.bot_beads)
> +            j = i % (self.top_beads+self.bot_beads)
> +            if bead.get_state() == 1:
> +                if j < self.top_beads:
> +                    v[r+1] += self.top_factor
> +                else:
> +                    v[r+1] += 1
>
> +        if count_beads:
>             # Save the value associated with each rod as a 2-byte integer.
>             for j in v[1:]:
>                 string += "%2d" % (j)
> @@ -783,7 +773,7 @@ class AbacusGeneric():
>             for bead in self.beads:
>                 sum += bead.get_value()
>             string = str(sum)
> -            print self.name, string
> +
>         return(string)
>
>     def label(self, string):
> diff --git a/sprites.py b/sprites.py
> index 2364335..8896d36 100644
> --- a/sprites.py
> +++ b/sprites.py
> @@ -34,7 +34,7 @@ class Sprite manages individual sprites within the collection.
>
>  Example usage:
>         # Import the classes into your program.
> -        from sprites import Sprites Sprite
> +        from sprites import Sprites, Sprite
>
>         # Create a new sprite collection for a gtk Drawing Area.
>         my_drawing_area = gtk.DrawingArea()
> @@ -75,14 +75,15 @@ def svg_str_to_pixbuf(svg_string):
>  import pygtk
>  pygtk.require('2.0')
>  import gtk
> +import gobject
>  import pango
>
> +#
> +# A class for the list of sprites and everything they share in common
> +#
>
>  class Sprites:
> -    """ A class for the list of sprites and everything they share in common """
> -
>     def __init__(self, canvas, area=None, gc=None):
> -        """ Initialize an empty array of sprites """
>         self.canvas = canvas
>         if area == None:
>             self.area = self.canvas.window
> @@ -94,61 +95,51 @@ class Sprites:
>         self.list = []
>
>     def get_sprite(self, i):
> -        """ Return a sprint from the array """
>         if i < 0 or i > len(self.list)-1:
>             return(None)
>         else:
>             return(self.list[i])
>
>     def length_of_list(self):
> -        """ How many sprites are there? """
>         return(len(self.list))
>
>     def append_to_list(self, spr):
> -        """ Append a new sprite to the end of the list. """
>         self.list.append(spr)
>
>     def insert_in_list(self, spr, i):
> -        """ Insert a sprite at position i. """
>         if i < 0:
>             self.list.insert(0, spr)
> -        elif i > len(self.list) - 1:
> +        elif i > len(self.list)-1:
>             self.list.append(spr)
>         else:
>             self.list.insert(i, spr)
>
>     def remove_from_list(self, spr):
> -        """ Remove a sprite from the list. """
>         if spr in self.list:
>             self.list.remove(spr)
>
>     def find_sprite(self, pos):
> -        """ Search based on (x, y) position. Return the 'top/first' one. """
>         list = self.list[:]
>         list.reverse()
>         for spr in list:
> -            if spr.hit(pos):
> -                return spr
> +            if spr.hit(pos): return spr
>         return None
>
>     def redraw_sprites(self, area=None):
>         """ Redraw the sprites that intersect area. """
>         for spr in self.list:
> -            if area == None:
> -                spr.draw()
> -            else:
> -                intersection = spr.rect.intersect(area)
> -                if intersection.width > 0 or intersection.height > 0:
> -                    spr.draw()
> +            spr.draw()
>
>
> +#
> +# A class for the individual sprites
> +#
>  class Sprite:
> -    """ A class for the individual sprites """
> -
>     def __init__(self, sprites, x, y, image):
> -        """ Initialize an individual sprite """
>         self._sprites = sprites
> +        self._x = int(x)
>         self.rect = gtk.gdk.Rectangle(int(x), int(y), 0, 0)
> +        self._y = int(y)
>         self._scale = [12]
>         self._rescale = [True]
>         self._horiz_align = ["center"]
> @@ -157,71 +148,65 @@ class Sprite:
>         self._bold = False
>         self._italic = False
>         self._color = None
> -        self._margins = [0, 0, 0, 0]
> +        self._width = 0
> +        self._height = 0
> +        self._margins = [0,0,0,0]
>         self.layer = 100
>         self.labels = []
>         self.images = []
> -        self._dx = []  # image offsets
> +        self._dx = [] # image offsets
>         self._dy = []
>         self.set_image(image)
>         self._sprites.append_to_list(self)
>
>     def set_image(self, image, i=0, dx=0, dy=0):
> -        """ Add an image to the sprite. """
> -        while len(self.images) < i + 1:
> -            self.images.append(None)
> -            self._dx.append(0)
> -            self._dy.append(0)
> +        while len(self.images) < i+1:
> +           self.images.append(None)
> +           self._dx.append(0)
> +           self._dy.append(0)
>         self.images[i] = image
>         self._dx[i] = dx
>         self._dy[i] = dy
> -        if isinstance(self.images[i], gtk.gdk.Pixbuf):
> -            w = self.images[i].get_width()
> -            h = self.images[i].get_height()
> +        if isinstance(image, gtk.gdk.Pixbuf):
> +            _w = image.get_width()
> +            _h = image.get_height()
>         else:
> -            w, h = self.images[i].get_size()
> -        if i == 0:  # Always reset width and height when base image changes.
> -            self.rect.width = w + dx
> -            self.rect.height = h + dy
> +            _w, _h = image.get_size()
> +        if i == 0: # Always reset width and height when base image changes.
> +            self._width = _w + dx
> +            self._height = _h + dy
>         else:
> -            if w + dx > self.rect.width:
> -                self.rect.width = w + dx
> -            if h + dy > self.rect.height:
> -                self.rect.height = h + dy
> +            if _w + dx > self._width:
> +                self._width = _w + dx
> +            if _h + dy > self._height:
> +                self._height = _h + dy
>
>     def move(self, pos):
> -        """ Move to new (x, y) position """
>         self.inval()
> -        self.rect.x, self.rect.y = int(pos[0]), int(pos[1])
> +        self._x,self._y = int(pos[0]),int(pos[1])
>         self.inval()
>
>     def move_relative(self, pos):
> -        """ Move to new (x+dx, y+dy) position """
>         self.inval()
> -        self.rect.x += int(pos[0])
> -        self.rect.y += int(pos[1])
> +        self._x += int(pos[0])
> +        self._y += int(pos[1])
>         self.inval()
>
>     def get_xy(self):
> -        """ Return current (x, y) position """
> -        return (self.rect.x, self.rect.y)
> +        return (self._x, self._y)
>
>     def get_dimensions(self):
> -        """ Return current size """
> -        return (self.rect.width, self.rect.height)
> +        return (self._width, self._height)
>
>     def get_layer(self):
> -        """ Return current layer """
>         return self.layer
>
>     def set_shape(self, image, i=0):
> -        """ Set the current image associated with the sprite """
>         self.inval()
>         self.set_image(image, i)
>         self.inval()
>
>     def set_layer(self, layer):
> -        """ Set the layer for a sprite """
>         self._sprites.remove_from_list(self)
>         self.layer = layer
>         for i in range(self._sprites.length_of_list()):
> @@ -233,43 +218,37 @@ class Sprite:
>         self.inval()
>
>     def set_label(self, new_label, i=0):
> -        """ Set the label drawn on the sprite """
>         self._extend_labels_array(i)
>         if type(new_label) is str or type(new_label) is unicode:
>             # pango doesn't like nulls
> -            self.labels[i] = new_label.replace("\0", " ")
> +            self.labels[i] = new_label.replace("\0"," ")
>         else:
>             self.labels[i] = str(new_label)
>         self.inval()
>
>     def set_margins(self, l=0, t=0, r=0, b=0):
> -        """ Set the margins for drawing the label """
> -        self._margins = [l, t, r, b]
> +        self._margins = [l,t,r,b]
>
>     def _extend_labels_array(self, i):
> -        """ Append to the labels attribute list """
>         if self._fd is None:
> -            self.set_font('Sans')
> +           self.set_font('Sans')
>         if self._color is None:
> -            self._color = self._sprites.cm.alloc_color('black')
> -        while len(self.labels) < i + 1:
> -            self.labels.append(" ")
> -            self._scale.append(self._scale[0])
> -            self._rescale.append(self._rescale[0])
> -            self._horiz_align.append(self._horiz_align[0])
> -            self._vert_align.append(self._vert_align[0])
> +           self._color = self._sprites.cm.alloc_color('black')
> +        while len(self.labels) < i+1:
> +           self.labels.append(" ")
> +           self._scale.append(self._scale[0])
> +           self._rescale.append(self._rescale[0])
> +           self._horiz_align.append(self._horiz_align[0])
> +           self._vert_align.append(self._vert_align[0])
>
>     def set_font(self, font):
> -        """ Set the font for a label """
>         self._fd = pango.FontDescription(font)
>
>     def set_label_color(self, rgb):
> -        """ Set the font color for a label """
>         self._color = self._sprites.cm.alloc_color(rgb)
>
>     def set_label_attributes(self, scale, rescale=True, horiz_align="center",
>                              vert_align="middle", i=0):
> -        """ Set the various label attributes """
>         self._extend_labels_array(i)
>         self._scale[i] = scale
>         self._rescale[i] = rescale
> @@ -277,124 +256,115 @@ class Sprite:
>         self._vert_align[i] = vert_align
>
>     def hide(self):
> -        """ Hide a sprite """
>         self.inval()
>         self._sprites.remove_from_list(self)
>
>     def inval(self):
> -        """ Force a region redraw by gtk """
> -        self._sprites.area.invalidate_rect(self.rect, False)
> +        self._sprites.area.invalidate_rect(
> +            gtk.gdk.Rectangle(self._x,self._y,self._width,self._height), False)
>
>     def draw(self):
> -        """ Draw the sprite (and label) """
> -        for i, img in enumerate(self.images):
> +        for i,img in enumerate(self.images):
>             if isinstance(img, gtk.gdk.Pixbuf):
> -                self._sprites.area.draw_pixbuf(self._sprites.gc, img, 0, 0,
> -                                               self.rect.x + self._dx[i],
> -                                               self.rect.y + self._dy[i])
> +                self._sprites.area.draw_pixbuf(
> +                    self._sprites.gc, img, 0, 0, self._x+self._dx[i],
> +                                                 self._y+self._dy[i])
>             elif img is not None:
> -                self._sprites.area.draw_drawable(self._sprites.gc, img, 0, 0,
> -                                                 self.rect.x + self._dx[i],
> -                                                 self.rect.y + self._dy[i],
> -                                                 -1, -1)
> +                self._sprites.area.draw_drawable(
> +                    self._sprites.gc, img, 0, 0, self._x+self._dx[i],
> +                                                 self._y+self._dy[i], -1, -1)
>         if len(self.labels) > 0:
>             self.draw_label()
>
>     def hit(self, pos):
> -        """ Is (x, y) on top of the sprite? """
>         x, y = pos
> -        if x < self.rect.x:
> +        if x < self._x:
>             return False
> -        if x > self.rect.x + self.rect.width:
> +        if x > self._x+self._width:
>             return False
> -        if y < self.rect.y:
> +        if y < self._y:
>             return False
> -        if y > self.rect.y + self.rect.height:
> +        if y > self._y+self._height:
>             return False
>         return True
>
>     def draw_label(self):
> -        """ Draw the label based on its attributes """
> -        my_width = self.rect.width - self._margins[0] - self._margins[2]
> +        my_width = self._width-self._margins[0]-self._margins[2]
>         if my_width < 0:
>             my_width = 0
> -        my_height = self.rect.height - self._margins[1] - self._margins[3]
> +        my_height = self._height-self._margins[1]-self._margins[3]
>         for i in range(len(self.labels)):
>             pl = self._sprites.canvas.create_pango_layout(str(self.labels[i]))
> -            self._fd.set_size(int(self._scale[i] * pango.SCALE))
> +            self._fd.set_size(int(self._scale[i]*pango.SCALE))
>             pl.set_font_description(self._fd)
> -            w = pl.get_size()[0] / pango.SCALE
> +            w = pl.get_size()[0]/pango.SCALE
>             if w > my_width:
> -                if self._rescale[i]:
> +                if self._rescale[i] is True:
>                     self._fd.set_size(
> -                            int(self._scale[i] * pango.SCALE * my_width / w))
> +                                    int(self._scale[i]*pango.SCALE*my_width/w))
>                     pl.set_font_description(self._fd)
> -                    w = pl.get_size()[0] / pango.SCALE
> +                    w = pl.get_size()[0]/pango.SCALE
>                 else:
> -                    j = len(self.labels[i]) - 1
> +                    j = len(self.labels[i])-1
>                     while(w > my_width and j > 0):
>                         pl = self._sprites.canvas.create_pango_layout(
> -                              "…" + self.labels[i][len(self.labels[i]) - j:])
> -                        self._fd.set_size(int(self._scale[i] * pango.SCALE))
> +                                 "…"+self.labels[i][len(self.labels[i])-j:])
> +                        self._fd.set_size(int(self._scale[i]*pango.SCALE))
>                         pl.set_font_description(self._fd)
> -                        w = pl.get_size()[0] / pango.SCALE
> +                        w = pl.get_size()[0]/pango.SCALE
>                         j -= 1
>             if self._horiz_align[i] == "center":
> -                x = int(self.rect.x + self._margins[0] + (my_width - w) / 2)
> +                x = int(self._x+self._margins[0]+(my_width-w)/2)
>             elif self._horiz_align[i] == 'left':
> -                x = int(self.rect.x + self._margins[0])
> +                x = int(self._x+self._margins[0])
>             else: # right
> -                x = int(self.rect.x + self.rect.width - w - self._margins[2])
> -            h = pl.get_size()[1] / pango.SCALE
> +                x = int(self._x+self._width-w-self._margins[2])
> +            h = pl.get_size()[1]/pango.SCALE
>             if self._vert_align[i] == "middle":
> -                y = int(self.rect.y + self._margins[1] + (my_height - h) / 2)
> +                y = int(self._y+self._margins[1]+(my_height-h)/2)
>             elif self._vert_align[i] == "top":
> -                y = int(self.rect.y + self._margins[1])
> +                y = int(self._y+self._margins[1])
>             else: # bottom
> -                y = int(self.rect.y + self.rect.height - h - self._margins[3])
> +                y = int(self._y+self._height-h-self._margins[3])
>             self._sprites.gc.set_foreground(self._color)
>             self._sprites.area.draw_layout(self._sprites.gc, x, y, pl)
>
>     def label_width(self):
> -        """ Calculate the width of a label """
>         max = 0
>         for i in range(len(self.labels)):
>             pl = self._sprites.canvas.create_pango_layout(self.labels[i])
> -            self._fd.set_size(int(self._scale[i] * pango.SCALE))
> +            self._fd.set_size(int(self._scale[i]*pango.SCALE))
>             pl.set_font_description(self._fd)
> -            w = pl.get_size()[0] / pango.SCALE
> +            w = pl.get_size()[0]/pango.SCALE
>             if w > max:
>                 max = w
>         return max
>
>     def label_safe_width(self):
> -        """ Return maximum width for a label """
> -        return self.rect.width - self._margins[0] - self._margins[2]
> -
> +        return self._width-self._margins[0]-self._margins[2]
> +
>     def label_safe_height(self):
> -        """ Return maximum height for a label """
> -        return self.rect.height - self._margins[1] - self._margins[3]
> -
> +        return self._height-self._margins[1]-self._margins[3]
> +
>     def label_left_top(self):
> -        """ Return the upper-left corner of the label safe zone """
> -        return(self._margins[0], self._margins[1])
> +        return (self._margins[0], self._margins[1])
>
>     def get_pixel(self, pos, i=0):
> -        """ Return the pixl at (x, y) """
>         x, y = pos
> -        x = x - self.rect.x
> -        y = y - self.rect.y
> -        if y > self.images[i].get_height() - 1:
> -            return(-1, -1, -1, -1)
> +        x = x-self._x
> +        y = y-self._y
> +        if y > self.images[i].get_height()-1:
> +            return (-1,-1,-1,-1)
>         try:
>             array = self.images[i].get_pixels()
>             if array is not None:
> -                offset = (y * self.images[i].get_width() + x) * 4
> -                r, g, b, a = ord(array[offset]), ord(array[offset + 1]),\
> -                             ord(array[offset + 2]), ord(array[offset + 3])
> -                return(r, g, b, a)
> +                offset = (y*self.images[i].get_width()+x)*4
> +                r,g,b,a = ord(array[offset]), ord(array[offset+1]),\
> +                          ord(array[offset+2]), ord(array[offset+3])
> +                return (r,g,b,a)
>             else:
> -                return(-1, -1, -1, -1)
> +                return (-1,-1,-1,-1)
>         except IndexError:
>             print "Index Error: %d %d" % (len(array), offset)
> -            return(-1, -1, -1, -1)
> +            return (-1,-1,-1,-1)
> +
> --
> 1.7.2.2
>
>
> _______________________________________________
> Sugar-devel mailing list
> Sugar-devel at lists.sugarlabs.org
> http://lists.sugarlabs.org/listinfo/sugar-devel
>
>

-walter

-- 
Walter Bender
Sugar Labs
http://www.sugarlabs.org


More information about the Sugar-devel mailing list