From db04449a2b35bc6066a33372da49ff241131c53d Mon Sep 17 00:00:00 2001 From: Guillaume Seguin Date: Thu, 16 May 2013 15:31:32 +0200 Subject: [PATCH] Cleanup and speedup gviz This commit mostly removes unused code and cleans some things up. The main contribution is that instead of calling repaint() and Refresh(), we now only mark the canvas as dirty and call Refresh(), which will do the repaint just-in-time. This speeds things a lot when quickly changing layers or zooming, as it does not repaint once per keyboard/mouse event but once per actual Refresh, which makes it much smoother. --- printrun/gviz.py | 66 +++++++++++++++--------------------------------- 1 file changed, 20 insertions(+), 46 deletions(-) diff --git a/printrun/gviz.py b/printrun/gviz.py index 46aac17..88dfd46 100755 --- a/printrun/gviz.py +++ b/printrun/gviz.py @@ -21,13 +21,9 @@ ID_ABOUT = 101 ID_EXIT = 110 class window(wx.Frame): def __init__(self, f, size = (600, 600), build_dimensions = [200, 200, 100, 0, 0, 0], grid = (10, 50), extrusion_width = 0.5): - wx.Frame.__init__(self, None, title = "Gcode view, shift to move view, mousewheel to set layer", size = (size[0], size[1])) + wx.Frame.__init__(self, None, title = "Gcode view, shift to move view, mousewheel to set layer", size = size) self.p = gviz(self, size = size, build_dimensions = build_dimensions, grid = grid, extrusion_width = extrusion_width) - if f: - gcode = gcoder.GCode(f) - self.p.addfile(gcode) - vbox = wx.BoxSizer(wx.VERTICAL) toolbar = wx.ToolBar(self, -1, style = wx.TB_HORIZONTAL | wx.NO_BORDER) toolbar.AddSimpleTool(1, wx.Image(imagefile('zoom_in.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Zoom In [+]', '') @@ -37,7 +33,7 @@ class window(wx.Frame): toolbar.AddSimpleTool(4, wx.Image(imagefile('arrow_down.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Move Down a Layer [D]', '') toolbar.AddSimpleTool(5, wx.EmptyBitmap(16, 16), 'Reset view', '') toolbar.AddSeparator() - #toolbar.AddSimpleTool(5, wx.Image('./images/inject.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Insert Code at start of this layer', '') + #toolbar.AddSimpleTool(6, wx.Image('./images/inject.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Insert Code at start of this layer', '') toolbar.Realize() vbox.Add(toolbar, 0, border = 5) self.SetSizer(vbox) @@ -46,43 +42,23 @@ class window(wx.Frame): self.Bind(wx.EVT_TOOL, lambda x:self.p.layerup(), id = 3) self.Bind(wx.EVT_TOOL, lambda x:self.p.layerdown(), id = 4) self.Bind(wx.EVT_TOOL, self.resetview, id = 5) - #self.Bind(wx.EVT_TOOL, lambda x:self.p.inject(), id = 5) - + #self.Bind(wx.EVT_TOOL, lambda x:self.p.inject(), id = 6) self.CreateStatusBar(1); self.SetStatusText("Layer number and Z position show here when you scroll"); - #self.bu = wx.Button(self.p,-1, "U", pos = (0, 100), size = (40, 140)) - #self.bd = wx.Button(self.p,-1, "D", pos = (0, 140), size = (40, 140)) - #self.bi = wx.Button(self.p,-1, "+", pos = (40, 100), size = (40, 140)) - #self.bo = wx.Button(self.p,-1, "-", pos = (40, 140), size = (40, 140)) - #self.bs = wx.Button(self.p, -1, "Inject", pos = (85, 103), size = (50, 20)) - #self.bu.SetToolTip(wx.ToolTip("Move up one layer")) - #self.bd.SetToolTip(wx.ToolTip("Move down one layer")) - #self.bi.SetToolTip(wx.ToolTip("Zoom view in")) - #self.bo.SetToolTip(wx.ToolTip("Zoom view out")) - #self.bs.SetToolTip(wx.ToolTip("Insert Code at start of this layer")) - - #self.bu.Bind(wx.EVT_BUTTON, lambda x:self.p.layerup()) - #self.bd.Bind(wx.EVT_BUTTON, lambda x:self.p.layerdown()) - #self.bi.Bind(wx.EVT_BUTTON, lambda x:self.p.zoom(200, 200, 1.2)) - #self.bo.Bind(wx.EVT_BUTTON, lambda x:self.p.zoom(200, 200, 1/1.2)) - #self.bs.Bind(wx.EVT_BUTTON, lambda x:self.p.inject()) - - s = time.time() - #print time.time()-s self.initpos = [0, 0] self.p.Bind(wx.EVT_KEY_DOWN, self.key) - #self.bu.Bind(wx.EVT_KEY_DOWN, self.key) - #self.bd.Bind(wx.EVT_KEY_DOWN, self.key) - #self.bi.Bind(wx.EVT_KEY_DOWN, self.key) - #self.bo.Bind(wx.EVT_KEY_DOWN, self.key) self.Bind(wx.EVT_KEY_DOWN, self.key) self.p.Bind(wx.EVT_MOUSEWHEEL, self.zoom) self.Bind(wx.EVT_MOUSEWHEEL, self.zoom) self.p.Bind(wx.EVT_MOUSE_EVENTS, self.mouse) self.Bind(wx.EVT_MOUSE_EVENTS, self.mouse) + if f: + gcode = gcoder.GCode(f) + self.p.addfile(gcode) + def resetview(self, event): self.p.translate = [0.0, 0.0] self.p.scale = self.p.basescale @@ -97,10 +73,9 @@ class window(wx.Frame): if self.initpos is None or not hasattr(self, "basetrans"): self.initpos = e self.basetrans = self.p.translate - #print self.p.translate, e, self.initpos self.p.translate = [ self.basetrans[0]+(e[0]-self.initpos[0]), self.basetrans[1]+(e[1]-self.initpos[1]) ] - self.p.repaint() + self.p.dirty = 1 self.p.Refresh() else: @@ -146,9 +121,10 @@ class window(wx.Frame): class gviz(wx.Panel): def __init__(self, parent, size = (200, 200), build_dimensions = [200, 200, 100, 0, 0, 0], grid = (10, 50), extrusion_width = 0.5): - wx.Panel.__init__(self, parent,-1, size = (size[0], size[1])) + wx.Panel.__init__(self, parent, -1, size = (-1, -1)) self.parent = parent self.size = size + self.SetMinSize((300, 300)) self.build_dimensions = build_dimensions self.grid = grid self.lastpos = [0, 0, 0, 0, 0, 0, 0] @@ -195,13 +171,14 @@ class gviz(wx.Panel): self.layerindex = 0 self.showall = 0 self.dirty = 1 - #self.repaint() + self.Refresh() + def layerup(self): if(self.layerindex+1