def _finishSaveToFile(self): img = self.GetImage() self.Draw(self.pdc, img, drawid=99) FileName, FileType, width, height = self._finishRenderingInfo ibuffer = EmptyBitmap(max(1, width), max(1, height)) dc = wx.BufferedDC(None, ibuffer) dc.Clear() self.pdc.DrawToDC(dc) ibuffer.SaveFile(FileName, FileType) self.Map.GetRenderMgr().updateMap.disconnect(self._finishSaveToFile) self._finishRenderingInfo = None
def OnSize(self, event): Debug.msg(5, "BufferedWindow.OnSize()") # The Buffer init is done here, to make sure the buffer is always # the same size as the Window #Size = self.GetClientSizeTuple() size = self.GetClientSize() # Make new offscreen bitmap: this bitmap will always have the # current drawing in it, so it can be used to save the image to # a file, or whatever. self._Buffer = EmptyBitmap(*size) self.UpdateDrawing()
def RenderText(text, font, bgcolor, fgcolor): """Renders text with given font to bitmap.""" dc = wx.MemoryDC(EmptyBitmap(20, 20)) dc.SetFont(font) w, h = dc.GetTextExtent(text) bmp = EmptyBitmap(w + 2, h + 2) dc.SelectObject(bmp) dc.SetBackgroundMode(wx.SOLID) dc.SetTextBackground(wx.Colour(*bgcolor)) dc.SetTextForeground(wx.Colour(*fgcolor)) dc.Clear() dc.DrawText(text, 1, 1) dc.SelectObject(wx.NullBitmap) return bmp
def createNoDataBitmap(imageWidth, imageHeight, text="No data"): """Creates 'no data' bitmap. Used when requested bitmap is not available (loading data was not successful) or we want to show 'no data' bitmap. :param imageWidth: image width :param imageHeight: image height """ Debug.msg( 4, "createNoDataBitmap: w={w}, h={h}, text={t}".format(w=imageWidth, h=imageHeight, t=text), ) bitmap = EmptyBitmap(imageWidth, imageHeight) dc = wx.MemoryDC() dc.SelectObject(bitmap) dc.Clear() text = _(text) dc.SetFont( wx.Font( pointSize=40, family=wx.FONTFAMILY_SCRIPT, style=wx.FONTSTYLE_NORMAL, weight=wx.FONTWEIGHT_BOLD, )) tw, th = dc.GetTextExtent(text) dc.DrawText(text, (imageWidth - tw) // 2, (imageHeight - th) // 2) dc.SelectObject(wx.NullBitmap) return bitmap
def __init__(self, parent, id=wx.ID_ANY, style=wx.DEFAULT_FRAME_STYLE | wx.FULL_REPAINT_ON_RESIZE | wx.BORDER_RAISED): Debug.msg(2, "AnimationWindow.__init__()") self.bitmap = EmptyBitmap(1, 1) self.parent = parent self._pdc = PseudoDC() self._overlay = None self._tmpMousePos = None self.x = self.y = 0 self.bitmap_overlay = None BufferedWindow.__init__(self, parent=parent, id=id, style=style) self.SetBackgroundColour(wx.BLACK) self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM) self.Bind(wx.EVT_SIZE, self.OnSize) self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
def SaveToFile(self, FileName, FileType, width, height): """This will save the contents of the buffer to the specified file. See the wx.Windows docs for wx.Bitmap::SaveFile for the details """ busy = wx.BusyInfo(_("Please wait, exporting image..."), parent=self) wx.GetApp().Yield() self.Map.ChangeMapSize((width, height)) ibuffer = EmptyBitmap(max(1, width), max(1, height)) self.Map.Render(force=True, windres=True) img = self.GetImage() self.Draw(self.pdc, img, drawid=99) dc = wx.BufferedDC(None, ibuffer) dc.Clear() # probably does nothing, removed from wxPython 2.9 # self.PrepareDC(dc) self.pdc.DrawToDC(dc) ibuffer.SaveFile(FileName, FileType) del busy
def __init__( self, parent, id=wx.ID_ANY, style=wx.NO_FULL_REPAINT_ON_RESIZE, Map=None, **kwargs, ): wx.Window.__init__(self, parent, id=id, style=style, **kwargs) self.parent = parent self.Map = Map self.mapname = self.parent.mapname # # Flags # self.render = True # re-render the map from GRASS or just redraw image self.resize = False # indicates whether or not a resize event has taken place self.dragimg = None # initialize variable for map panning self.pen = None # pen for drawing zoom boxes, etc. self._oldfont = self._oldencoding = None # # Event bindings # self.Bind(wx.EVT_PAINT, self.OnPaint) self.Bind(wx.EVT_SIZE, self.OnSize) self.Bind(wx.EVT_IDLE, self.OnIdle) # # Render output objects # self.mapfile = None # image file to be rendered self.img = None # wx.Image object (self.mapfile) self.imagedict = { } # images and their PseudoDC ID's for painting and dragging self.pdc = PseudoDC() # will store an off screen empty bitmap for saving to file self._buffer = EmptyBitmap(max(1, self.Map.width), max(1, self.Map.height)) # make sure that extents are updated at init self.Map.region = self.Map.GetRegion() self.Map.SetRegion() self._finishRenderingInfo = None self.Bind(wx.EVT_ERASE_BACKGROUND, lambda x: None)
def OnSize(self, event): """Init image size to match window size """ # set size of the input image self.Map.width, self.Map.height = self.GetClientSize() # Make new off screen bitmap: this bitmap will always have the # current drawing in it, so it can be used to save the image to # a file, or whatever. self._buffer = EmptyBitmap(self.Map.width, self.Map.height) # get the image to be rendered self.img = self.GetImage() # update map display if self.img and self.Map.width + self.Map.height > 0: # scale image during resize self.img = self.img.Scale(self.Map.width, self.Map.height) self.render = False self.UpdateHist() # re-render image on idle self.resize = True
class BufferedWindow(wx.Window): """ A Buffered window class (http://wiki.wxpython.org/DoubleBufferedDrawing). To use it, subclass it and define a Draw(DC) method that takes a DC to draw to. In that method, put the code needed to draw the picture you want. The window will automatically be double buffered, and the screen will be automatically updated when a Paint event is received. When the drawing needs to change, you app needs to call the UpdateDrawing() method. Since the drawing is stored in a bitmap, you can also save the drawing to file by calling the SaveToFile(self, file_name, file_type) method. """ def __init__(self, *args, **kwargs): # make sure the NO_FULL_REPAINT_ON_RESIZE style flag is set. kwargs['style'] = kwargs.setdefault( 'style', wx.NO_FULL_REPAINT_ON_RESIZE) | wx.NO_FULL_REPAINT_ON_RESIZE wx.Window.__init__(self, *args, **kwargs) Debug.msg(2, "BufferedWindow.__init__()") self.Bind(wx.EVT_PAINT, self.OnPaint) self.Bind(wx.EVT_SIZE, self.OnSize) # OnSize called to make sure the buffer is initialized. # This might result in OnSize getting called twice on some # platforms at initialization, but little harm done. self.OnSize(None) def Draw(self, dc): # just here as a place holder. # This method should be over-ridden when subclassed pass def OnPaint(self, event): Debug.msg(5, "BufferedWindow.OnPaint()") # All that is needed here is to draw the buffer to screen dc = wx.BufferedPaintDC(self, self._Buffer) def OnSize(self, event): Debug.msg(5, "BufferedWindow.OnSize()") # The Buffer init is done here, to make sure the buffer is always # the same size as the Window #Size = self.GetClientSizeTuple() size = self.GetClientSize() # Make new offscreen bitmap: this bitmap will always have the # current drawing in it, so it can be used to save the image to # a file, or whatever. self._Buffer = EmptyBitmap(*size) self.UpdateDrawing() # event.Skip() def SaveToFile(self, FileName, FileType=wx.BITMAP_TYPE_PNG): # This will save the contents of the buffer # to the specified file. See the wxWindows docs for # wx.Bitmap::SaveFile for the details self._Buffer.SaveFile(FileName, FileType) def UpdateDrawing(self): """ This would get called if the drawing needed to change, for whatever reason. The idea here is that the drawing is based on some data generated elsewhere in the system. If that data changes, the drawing needs to be updated. This code re-draws the buffer, then calls Update, which forces a paint event. """ dc = wx.MemoryDC() dc.SelectObject(self._Buffer) self.Draw(dc) del dc # need to get rid of the MemoryDC before Update() is called. self.Refresh() self.Update()
class AnimationWindow(BufferedWindow): def __init__(self, parent, id=wx.ID_ANY, style=wx.DEFAULT_FRAME_STYLE | wx.FULL_REPAINT_ON_RESIZE | wx.BORDER_RAISED): Debug.msg(2, "AnimationWindow.__init__()") self.bitmap = EmptyBitmap(1, 1) self.parent = parent self._pdc = PseudoDC() self._overlay = None self._tmpMousePos = None self.x = self.y = 0 self.bitmap_overlay = None BufferedWindow.__init__(self, parent=parent, id=id, style=style) self.SetBackgroundColour(wx.BLACK) self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM) self.Bind(wx.EVT_SIZE, self.OnSize) self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents) def Draw(self, dc): """Draws bitmap.""" Debug.msg(5, "AnimationWindow.Draw()") dc.Clear() # make sure you clear the bitmap! if self.bitmap.GetWidth() > 1: dc.DrawBitmap(self.bitmap, x=self.x, y=self.y) def OnSize(self, event): Debug.msg(5, "AnimationWindow.OnSize()") BufferedWindow.OnSize(self, event) if event: event.Skip() def _rescaleIfNeeded(self, bitmap): """!If the bitmap has different size than the window, rescale it.""" bW, bH = bitmap.GetSize() wW, wH = self.GetClientSize() if abs(bW - wW) > 5 or abs(bH - wH) > 5: params = ComputeScaledRect((bW, bH), (wW, wH)) im = wx.ImageFromBitmap(bitmap) im.Rescale(params['width'], params['height']) self.x = params['x'] self.y = params['y'] bitmap = wx.BitmapFromImage(im) if self._overlay: im = wx.ImageFromBitmap(self.bitmap_overlay) im.Rescale( im.GetWidth() * params['scale'], im.GetHeight() * params['scale']) self._setOverlay( wx.BitmapFromImage(im), xperc=self.perc[0], yperc=self.perc[1]) else: self.x = 0 self.y = 0 return bitmap def DrawBitmap(self, bitmap): """Draws bitmap. Does not draw the bitmap if it is the same one as last time. """ bitmap = self._rescaleIfNeeded(bitmap) if self.bitmap == bitmap: return self.bitmap = bitmap self.UpdateDrawing() def DrawOverlay(self, x, y): self._pdc.BeginDrawing() self._pdc.SetId(1) self._pdc.DrawBitmap(bmp=self._overlay, x=x, y=y) self._pdc.SetIdBounds(1, Rect(x, y, self._overlay.GetWidth(), self._overlay.GetHeight())) self._pdc.EndDrawing() def _setOverlay(self, bitmap, xperc, yperc): if self._overlay: self._pdc.RemoveAll() self._overlay = bitmap size = self.GetClientSize() x = xperc * size[0] y = yperc * size[1] self.DrawOverlay(x, y) def SetOverlay(self, bitmap, xperc, yperc): """Sets overlay bitmap (legend) :param bitmap: instance of wx.Bitmap :param xperc: x coordinate of bitmap top left corner in % of screen :param yperc: y coordinate of bitmap top left corner in % of screen """ Debug.msg(3, "AnimationWindow.SetOverlay()") if bitmap: self._setOverlay(bitmap, xperc, yperc) self.bitmap_overlay = bitmap self.perc = (xperc, yperc) else: self._overlay = None self._pdc.RemoveAll() self.bitmap_overlay = None self.UpdateDrawing() def ClearOverlay(self): """Clear overlay (legend) """ Debug.msg(3, "AnimationWindow.ClearOverlay()") self._overlay = None self.bitmap_overlay = None self._pdc.RemoveAll() self.UpdateDrawing() def OnPaint(self, event): Debug.msg(5, "AnimationWindow.OnPaint()") # All that is needed here is to draw the buffer to screen dc = wx.BufferedPaintDC(self, self._Buffer) if self._overlay: self._pdc.DrawToDC(dc) def OnMouseEvents(self, event): """Handle mouse events.""" # If it grows larger, split it. current = event.GetPosition() if event.LeftDown(): self._dragid = None idlist = self._pdc.FindObjects(current[0], current[1], radius=10) if 1 in idlist: self._dragid = 1 self._tmpMousePos = current elif event.LeftUp(): self._dragid = None self._tmpMousePos = None elif event.Dragging(): if self._dragid is None: return dx = current[0] - self._tmpMousePos[0] dy = current[1] - self._tmpMousePos[1] self._pdc.TranslateId(self._dragid, dx, dy) self.UpdateDrawing() self._tmpMousePos = current def GetOverlayPos(self): """Returns x, y position in pixels""" rect = self._pdc.GetIdBounds(1) return rect.GetX(), rect.GetY()