backend_gtk3agg.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import numpy as np
  2. from .. import cbook
  3. try:
  4. from . import backend_cairo
  5. except ImportError as e:
  6. raise ImportError('backend Gtk3Agg requires cairo') from e
  7. from . import backend_agg, backend_gtk3
  8. from .backend_cairo import cairo
  9. from .backend_gtk3 import Gtk, _BackendGTK3
  10. from matplotlib import transforms
  11. class FigureCanvasGTK3Agg(backend_gtk3.FigureCanvasGTK3,
  12. backend_agg.FigureCanvasAgg):
  13. def __init__(self, figure):
  14. backend_gtk3.FigureCanvasGTK3.__init__(self, figure)
  15. self._bbox_queue = []
  16. def on_draw_event(self, widget, ctx):
  17. """GtkDrawable draw event, like expose_event in GTK 2.X."""
  18. allocation = self.get_allocation()
  19. w, h = allocation.width, allocation.height
  20. if not len(self._bbox_queue):
  21. Gtk.render_background(
  22. self.get_style_context(), ctx,
  23. allocation.x, allocation.y,
  24. allocation.width, allocation.height)
  25. bbox_queue = [transforms.Bbox([[0, 0], [w, h]])]
  26. else:
  27. bbox_queue = self._bbox_queue
  28. ctx = backend_cairo._to_context(ctx)
  29. for bbox in bbox_queue:
  30. x = int(bbox.x0)
  31. y = h - int(bbox.y1)
  32. width = int(bbox.x1) - int(bbox.x0)
  33. height = int(bbox.y1) - int(bbox.y0)
  34. buf = cbook._unmultiplied_rgba8888_to_premultiplied_argb32(
  35. np.asarray(self.copy_from_bbox(bbox)))
  36. image = cairo.ImageSurface.create_for_data(
  37. buf.ravel().data, cairo.FORMAT_ARGB32, width, height)
  38. ctx.set_source_surface(image, x, y)
  39. ctx.paint()
  40. if len(self._bbox_queue):
  41. self._bbox_queue = []
  42. return False
  43. def blit(self, bbox=None):
  44. # If bbox is None, blit the entire canvas to gtk. Otherwise
  45. # blit only the area defined by the bbox.
  46. if bbox is None:
  47. bbox = self.figure.bbox
  48. allocation = self.get_allocation()
  49. x = int(bbox.x0)
  50. y = allocation.height - int(bbox.y1)
  51. width = int(bbox.x1) - int(bbox.x0)
  52. height = int(bbox.y1) - int(bbox.y0)
  53. self._bbox_queue.append(bbox)
  54. self.queue_draw_area(x, y, width, height)
  55. def draw(self):
  56. backend_agg.FigureCanvasAgg.draw(self)
  57. super().draw()
  58. def print_png(self, filename, *args, **kwargs):
  59. # Do this so we can save the resolution of figure in the PNG file
  60. agg = self.switch_backends(backend_agg.FigureCanvasAgg)
  61. return agg.print_png(filename, *args, **kwargs)
  62. class FigureManagerGTK3Agg(backend_gtk3.FigureManagerGTK3):
  63. pass
  64. @_BackendGTK3.export
  65. class _BackendGTK3Cairo(_BackendGTK3):
  66. FigureCanvas = FigureCanvasGTK3Agg
  67. FigureManager = FigureManagerGTK3Agg