Features/GTK3/Porting/Record

From Sugar Labs
< Features‎ | GTK3‎ | Porting
Revision as of 20:22, 18 December 2012 by Francis (talk | contribs)
Jump to navigation Jump to search

Some changes

Replace:

def getStringFromPixbuf(pixbuf):
    data = [""]
    pixbuf.save_to_callback(_saveDataToBufferCb, "png", {}, data)
    return base64.b64encode(str(data[0]))


def _saveDataToBufferCb(buf, data):
    data[0] += buf
    return True

With:

def getStringFromPixbuf(pixbuf):
    """Converts a pixbuf in a string."""
  
    # Save_to_bufferv return: (bool, string)
    data = pixbuf.save_to_bufferv('png', [], [])
   
    return base64.b64encode(data[1])

Replace:

thumbImg.save(thumbPath, "jpeg", {"quality":"85"} )

With:

thumbImg.savev(thumbPath, "jpeg", {"quality":"85"} )

Replace:

def load_colored_svg(filename, stroke, fill):
    path = os.path.join(constants.GFX_PATH, filename)
    data = open(path, 'r').read()

    entity = '<!ENTITY fill_color "%s">' % fill
    data = re.sub('<!ENTITY fill_color .*>', entity, data)

    entity = '<!ENTITY stroke_color "%s">' % stroke
    data = re.sub('<!ENTITY stroke_color .*>', entity, data)

    """return rsvg.Handle(data=data).get_pixbuf()"""

With:

def load_colored_svg(filename, stroke, fill):
    """Loads an svg, will change the fill and
    stroke colors and returns the pixbuf."""
   
    path = os.path.join(constants.GFX_PATH, filename)
    data = open(path, 'r').read()

    entity = '<!ENTITY fill_color "%s">' % fill
    data = re.sub('<!ENTITY fill_color .*>', entity, data)

    entity = '<!ENTITY stroke_color "%s">' % stroke
    data = re.sub('<!ENTITY stroke_color .*>', entity, data)

    """return Rsvg.Handle.new_from_data(data.encode('utf-8')).get_pixbuf()"""

Not implement:

super(Record, self).close()

Due to: SL#4345

From Pixmap to Pixbuf

When an application fails and you want to close from the corresponding button occurs:

Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/sugar3/activity/activity.py", line 890, in _keep_failed_dialog_response_cb
self.close(skip_save=True)
TypeError: close() got an unexpected keyword argument 'skip_save'
And the activity is not closed, nor can restart sugar as alert reappears again and again, you need to stop.

This error is caused because:

When a class B inherits from class A, and in B is overwritten a function of A, A function is no longer running, but instead runs the function of the class B, even when explicitly call function A from B.


The solution is in using Pixbuf instead of Pixmap:

Replace:

   def _generate_image(self, num):
       w = 55
       h = w
       pixmap = gdk.Pixmap(self.get_window(), w, h, -1)
       ctx = pixmap.cairo_create()
       ctx.rectangle(0, 0, w, h)
       ctx.set_source_rgb(0, 0, 0)
       ctx.fill()

By:

   def _generate_image(self, num):

       w = 55
       h = w
       
       x, y = self.get_property('window').get_position()
       rect = self.get_allocation()
       width, height = rect.width, rect.height
      
       thumb_surface = Gdk.Window.create_similar_surface(
           self.get_property('window'),
           cairo.CONTENT_COLOR,
           width, height)
      
       ctx = cairo.Context(thumb_surface)
       
       Gdk.cairo_set_source_window(
           cairo_context,
           self.get_property('window'),
           x, y)
           
       ctx.paint()
       
       ctx.rectangle(x, y, w, h)
       ctx.set_source_rgb(0, 0, 0)
       ctx.fill()