Commit 2614bb5e authored by Guillaume Seguin's avatar Guillaume Seguin

Factorize method wrapping and fix orig_handler call

parent 4e359fde
...@@ -22,6 +22,14 @@ import os ...@@ -22,6 +22,14 @@ import os
import types import types
import wx import wx
def patch_method(obj, method, replacement):
orig_handler = getattr(obj, method)
def wrapped(*a, **kwargs):
kwargs['orig_handler'] = orig_handler
return replacement(*a, **kwargs)
setattr(obj, method, types.MethodType(wrapped, obj))
class Plater(wx.Frame): class Plater(wx.Frame):
def __init__(self, filenames = [], size = (800, 580), callback = None, parent = None, build_dimensions = None): def __init__(self, filenames = [], size = (800, 580), callback = None, parent = None, build_dimensions = None):
super(Plater, self).__init__(parent, title = _("Plate building tool"), size = size) super(Plater, self).__init__(parent, title = _("Plate building tool"), size = size)
...@@ -81,9 +89,7 @@ class Plater(wx.Frame): ...@@ -81,9 +89,7 @@ class Plater(wx.Frame):
def set_viewer(self, viewer): def set_viewer(self, viewer):
# Patch handle_rotation on the fly # Patch handle_rotation on the fly
if hasattr(viewer, "handle_rotation"): if hasattr(viewer, "handle_rotation"):
orig_handler = viewer.handle_rotation def handle_rotation(self, event, orig_handler):
def handle_rotation(self, event, orig_handler = orig_handler):
if self.initpos is None: if self.initpos is None:
self.initpos = event.GetPositionTuple() self.initpos = event.GetPositionTuple()
else: else:
...@@ -95,9 +101,8 @@ class Plater(wx.Frame): ...@@ -95,9 +101,8 @@ class Plater(wx.Frame):
self.parent.move_shape((x2 - x1, y2 - y1)) self.parent.move_shape((x2 - x1, y2 - y1))
self.initpos = p2 self.initpos = p2
else: else:
orig_handler(self, event) orig_handler(event)
patch_method(viewer, "handle_rotation", handle_rotation)
viewer.handle_rotation = types.MethodType(handle_rotation, viewer)
self.s = viewer self.s = viewer
self.mainsizer.Add(self.s, 1, wx.EXPAND) self.mainsizer.Add(self.s, 1, wx.EXPAND)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment