<commit_before>import gtk
from matplotlib.figure import Figure
from matplotlib.backends.backend_cairo import RendererCairo, FigureCanvasCairo
import numpy

import reinteract.custom_result as custom_result

class _PlotResultCanvas(FigureCanvasCairo):
    def draw_event(*args):
        # Since we never change anything about the figure, the only time we
        # need to redraw is in response to an expose event, which we handle
        # ourselves
        pass

class PlotResult(custom_result.CustomResult):
    def __init__(self, *args, **kwargs):
        self.__args = args
        self.__kwargs = kwargs

    def create_widget(self):
        widget = PlotWidget(self)
        widget.axes.plot(*self.__args, **self.__kwargs)

        return widget
    
class ImshowResult(custom_result.CustomResult):
    def __init__(self, *args, **kwargs):
        self.__args = args
        self.__kwargs = kwargs

    def create_widget(self):
        widget = PlotWidget(self)
        widget.axes.imshow(*self.__args, **self.__kwargs)

        return widget
    
class PlotWidget(gtk.DrawingArea):
    __gsignals__ = {
        'button-press-event': 'override',
        'button-release-event': 'override',
        'expose-event': 'override'
    }

    def __init__(self, result):
        gtk.DrawingArea.__init__(self)
        self.figure = Figure(facecolor='white', figsize=(6,4.5))
        self.canvas = _PlotResultCanvas(self.figure)

        self.axes = self.figure.add_axes((0.05,0.05,0.9,0.9))
        self.add_events(gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE)

    def do_expose_event(self, event):
        cr = self.window.cairo_create()
        
        renderer = RendererCairo(self.figure.dpi)
        renderer.set_width_height(self.allocation.width, self.allocation.height)
        renderer.set_ctx_from_surface(cr.get_target())
        
        # event.region is not bound: http://bugzilla.gnome.org/show_bug.cgi?id=487158
#        gdk_context = gtk.gdk.CairoContext(renderer.ctx)
#        gdk_context.region(event.region)
#        gdk_context.clip()
        
        self.figure.draw(renderer)

    def do_button_press_event(self, event):
        if event.button == 3:
            custom_result.show_menu(self, event, save_callback=self.__save)
            return True
        else:
            return True
    
    def do_button_release_event(self, event):
        return True

    def do_realize(self):<commit_msg>Use add_subplot instead of add_axes because it uses good default margin widths, etc. Fixes (albeit not perfectly) ticket #26<commit_after>import gtk
from matplotlib.figure import Figure
from matplotlib.backends.backend_cairo import RendererCairo, FigureCanvasCairo
import numpy

import reinteract.custom_result as custom_result

class _PlotResultCanvas(FigureCanvasCairo):
    def draw_event(*args):
        # Since we never change anything about the figure, the only time we
        # need to redraw is in response to an expose event, which we handle
        # ourselves
        pass

class PlotResult(custom_result.CustomResult):
    def __init__(self, *args, **kwargs):
        self.__args = args
        self.__kwargs = kwargs

    def create_widget(self):
        widget = PlotWidget(self)
        widget.axes.plot(*self.__args, **self.__kwargs)

        return widget
    
class ImshowResult(custom_result.CustomResult):
    def __init__(self, *args, **kwargs):
        self.__args = args
        self.__kwargs = kwargs

    def create_widget(self):
        widget = PlotWidget(self)
        widget.axes.imshow(*self.__args, **self.__kwargs)

        return widget
    
class PlotWidget(gtk.DrawingArea):
    __gsignals__ = {
        'button-press-event': 'override',
        'button-release-event': 'override',
        'expose-event': 'override'
    }

    def __init__(self, result):
        gtk.DrawingArea.__init__(self)
        self.figure = Figure(facecolor='white', figsize=(6,4.5))
        self.canvas = _PlotResultCanvas(self.figure)

        self.axes = self.figure.add_subplot(111)
        self.add_events(gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE)

    def do_expose_event(self, event):
        cr = self.window.cairo_create()
        
        renderer = RendererCairo(self.figure.dpi)
        renderer.set_width_height(self.allocation.width, self.allocation.height)
        renderer.set_ctx_from_surface(cr.get_target())
        
        # event.region is not bound: http://bugzilla.gnome.org/show_bug.cgi?id=487158
#        gdk_context = gtk.gdk.CairoContext(renderer.ctx)
#        gdk_context.region(event.region)
#        gdk_context.clip()
        
        self.figure.draw(renderer)

    def do_button_press_event(self, event):
        if event.button == 3:
            custom_result.show_menu(self, event, save_callback=self.__save)
            return True
        else:
            return True
    
    def do_button_release_event(self, event):
        return True

    def do_realize(self):
{'fraction_non_alphanumeric': 0.09810254339927331, 'fraction_numerical': 0.007064997981429148, 'mean_word_length': 2.776676829268293, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4350332', 'n_tokens_mistral': 1538, 'n_tokens_neox': 1491, 'n_words': 311}