def test_DCClipper(self): dc = wx.ClientDC(self.frame) c = wx.DCClipper(dc, wx.Rect(10,10,25,25)) del c c = wx.DCClipper(dc, (10,10,25,25)) del c c = wx.DCClipper(dc, 10,10,25,25) del c r = wx.Region(10,10,25,25) c = wx.DCClipper(dc, r) del c
def DrawVideo(self, dc, t): bmp = self.bitmaps.get(t['path']) if bmp: _ = wx.DCClipper(dc, t['left'], t['top'], t['width'], t['height']) dc.DrawBitmap(bmp, t['left'] - t.get('crop', 0) / 2, t['top']) else: dc.SetPen(wx.ThePenList.FindOrCreatePen(self.VIDEO_BORDER_COLOR)) dc.SetBrush( wx.TheBrushList.FindOrCreateBrush(self.VIDEO_BACKGROUND_COLOR)) dc.DrawRectangle(t['left'], t['top'], t['width'], t['height'])
def draw(self, axes, dc): newPen = wx.Pen(mapColor(self.color), self.lineWidth, wx.SOLID) X, Y, W, H = axes.getGeometry() with wx.DCPenChanger(dc, newPen), wx.DCClipper(dc, X, Y, W, H): points = [] for x, y in zip(self.x, self.y): x = X + W * axes.xaxis.scaleValue(x) y = Y + H * (1 - axes.yaxis.scaleValue(y)) #Must break line segments at NaN or Inf. #TODO: Turn this into a PolyPolyline if np.isnan(x) or np.isnan(y) or np.isinf(x) or np.isinf(y): if len(points) > 0: dc.DrawLines(points) points = [] else: points.append((x, y)) if len(points) > 0: dc.DrawLines(points)
def DrawString( dc, text, bounds, align=wx.ALIGN_LEFT, valign=wx.ALIGN_TOP, allowClipping=False): """ Draw the given text word-wrapped within the given bounds. bounds must be a wx.Rect or a 4-element collection: (left, top, width, height). If allowClipping is True, this method changes the clipping region so that no text is drawn outside of the given bounds. """ if not text: return if align == wx.ALIGN_CENTER: align = wx.ALIGN_CENTER_HORIZONTAL if valign == wx.ALIGN_CENTER: valign = wx.ALIGN_CENTER_VERTICAL # DrawLabel only accepts a wx.Rect try: bounds = wx.Rect(*bounds) except: pass if allowClipping: clipper = wx.DCClipper(dc, bounds) # There is a bug in the wordwrap routine where a string that needs truncated and # that ends with a single space causes the method to throw an error (wx 2.8). # Our simple, but not always accurate, is to remove trailing spaces. # This won't catch single trailing space imbedded in a multiline # string. text = text.rstrip(' ') lines = wordwrap(text, bounds[2], dc, True) dc.DrawLabel(lines, bounds, align | valign)
def OnDraw(self, dc): x0, y0 = self.X0, self.Y0 w, h = self.W - self.X0 - self.X1, self.H - self.Y0 - self.Y1 # TODO # Figure out how to avoid redrawing the tick labels every time # Involves use of GetUpdateRegion (in wxWindow not wxDC!!) self.DrawBox(dc) oldPen = dc.GetPen() pen1 = wx.Pen(self._gridColor1, 0, wx.SOLID) pen2 = wx.Pen(self._gridColor2, 0, wx.SOLID) oldFont = dc.GetFont() dc.SetFont( wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD, False, 'Arial Narrow')) tickLength = 5 minorTicksAsGrid = False # Horizontal axis ticks and tick labels format = "%.0f" nTicks = self.nxMajorTicks * self.nxMinorTicks for i in range(int(nTicks + 1)): x = x0 + int(round(i * w / nTicks)) if i != 0 and i != nTicks: #dc.SetPen(pen2 if i % self.nxMinorTicks == 0 else pen1) if i % self.nxMinorTicks != 0: if minorTicksAsGrid: dc.SetPen(pen1) dc.DrawLine(x, y0 - 1, x, y0 + h) else: dc.SetPen(pen2) dc.DrawLine(x, y0 - 1, x, y0 + tickLength) dc.DrawLine(x, y0 + h - tickLength, x, y0 + h) else: dc.SetPen(pen1) dc.DrawLine(x, y0 - 1, x, y0 + h) dc.SetPen(pen2) dc.DrawLine(x, y0 - 1, x, y0 + 2 * tickLength) dc.DrawLine(x, y0 + h - 2 * tickLength, x, y0 + h) if i % self.nxMinorTicks == 0 and not self.hideXInfo: v = -self.width + i * self.width / nTicks s = format % v e = dc.GetTextExtent(s) dc.DrawText(s, x - e[0] / 2, y0 + h + 5) # Vertical axis ticks and tick labels lMax = max(abs(self.yMin[0]), abs(self.yMax[0])) lFormat = "%.2f" if lMax <= 1 else "%.1f" if lMax <= 1000 else "%.0f" rMax = max(abs(self.yMin[1]), abs(self.yMax[1])) rFormat = "%.2f" if rMax <= 1 else "%.1f" if rMax <= 1000 else "%.0f" lFormat = rFormat = "%g" nTicks = self.nyMajorTicks * self.nyMinorTicks for i in range(int(nTicks + 1)): y = y0 + h - int(round(i * h / nTicks)) if i != 0 and i != nTicks: if i % self.nyMinorTicks != 0: if minorTicksAsGrid: dc.SetPen(pen1) dc.DrawLine(x0 - 1, y, x0 + w, y) else: dc.SetPen(pen2) dc.DrawLine(x0 - 1, y, x0 + tickLength, y) dc.DrawLine(x0 + w - tickLength, y, x0 + w, y) else: dc.SetPen(pen1) dc.DrawLine(x0 - 1, y, x0 + w, y) dc.SetPen(pen2) dc.DrawLine(x0 - 1, y, x0 + 2 * tickLength, y) dc.DrawLine(x0 + w - 2 * tickLength, y, x0 + w, y) if i % self.nyMinorTicks == 0: v = self.yMin[0] + i * (self.yMax[0] - self.yMin[0]) / nTicks s = lFormat % v wt, ht = dc.GetTextExtent(s) dc.SetTextForeground(wx.BLUE) dc.DrawText(s, x0 - wt - 3, y - ht / 2) # Alternate axix tick labels: Use second min/max values for now v = self.yMin[1] + i * (self.yMax[1] - self.yMin[1]) / nTicks s = rFormat % v wt, ht = dc.GetTextExtent(s) dc.SetTextForeground(wx.BLACK) dc.DrawText(s, x0 + w + 3, y - ht / 2) dc.SetTextBackground(wx.BLACK) #dc.SetFont(wx.Font(13,wx.SWISS,wx.NORMAL,wx.BOLD,False,'Arial Narrow')) if not self.hideXInfo: s = "Time [seconds]" e = dc.GetTextExtent(s) dc.DrawText(s, x0 + w / 2 - e[0] / 2, y0 + h + e[1]) #s = self.labels[0] #e = dc.GetTextExtent(s) #dc.DrawRotatedText(s,10,y0+(h+e[0])/2,90) #dc.SetFont(wx.NullFont) # Draw in the data with wx.DCClipper(dc, x0, y0, w, h): # if 1: start = self.TimeBase or self.clock n = len(self.trend) points = [0] * n for j in range(self.nLines): yMin = self.yMin[j] yMax = self.yMax[j] xScale = float(w) / self.width yScale = float(h) / (yMax - yMin) for i in range(n): v = self.trend[i] x = x0 + xScale * (v[0] - start) y = y0 + yScale * (yMax - v[j + 1]) points[i] = (x, y) dc.SetPen(self.pens[j]) dc.DrawLines(points) # Draw the legend x, y = x0, y0 - dc.GetTextExtent(" ")[1] - 5 for i in range(self.nLines): #dc.SetTextForeground(self.pens[i].GetColour()) s = self.labels[i] + " = " + self.legendFormat[i] % self.values[i] dc.DrawText(s, x, y) e = dc.GetTextExtent(s) dc.SetPen(self.pens[i]) dc.DrawLine(x + e[0] + 20, y0 - e[1] // 2 - 5, x + e[0] + 20 + 80, y0 - e[1] // 2 - 5) # x += 2*dc.GetTextExtent(s)[0] x += 250 #dc.SetTextForeground(wx.BLACK) dc.SetFont(oldFont) dc.SetPen(oldPen)