def small_square(p, colors=['w', 0], linecolor='g'): w1, h1, w2, h2 = [2.5, 1.5, 2, 1] # Exterior square p.setBrush(pg.mkBrush(colors[1])) p.setPen(pg.mkPen('k')) p.translate(-w1 / 2, -h1 / 2) # translate from center to low-x, low-y position. rectangle = QtCore.QRectF(0, 0, w1, h1) p.drawRoundedRect(rectangle, 0.05, 0.05) p.translate(w1 / 2, h1 / 2) # go back to center positio # Interior square p.setBrush(pg.mkBrush(colors[0])) p.setPen(pg.mkPen(linecolor)) p.translate(-w2 / 2, -h2 / 2) rectangle = QtCore.QRectF(0, 0, w2, h2) p.drawRoundedRect(rectangle, 0.05, 0.05) p.translate(w2 / 2, h2 / 2) # go back to center positio # Inter-Internal p.setPen(pg.mkPen('k')) w = w2 * 0.8 h = h2 * 0.5 p.translate(-w / 2, -h / 2) rectangle = QtCore.QRectF(0, 0, w, h) p.drawRoundedRect(rectangle, 0.5, 0.5) p.translate(w / 2, h / 2) # go back to center position
def generate_picture(self, boundingRect): w = self.datasrc.period * self.candle_width w2 = w * 0.5 left,right = boundingRect.left(), boundingRect.right() p = self.painter p.begin(self.picture) rows = list(self.datasrc.bear_rows(5, left, right, yscale=self.ax.vb.yscale)) if self.draw_shadow: p.setPen(pg.mkPen(self.bear_color)) for t,open,close,high,low in rows: if high > low: p.drawLine(QtCore.QPointF(t, low), QtCore.QPointF(t, high)) if self.draw_body: p.setPen(pg.mkPen(self.bear_frame_color)) p.setBrush(pg.mkBrush(self.bear_body_color)) for t,open,close,high,low in rows: p.drawRect(QtCore.QRectF(t-w2, open, w, close-open)) rows = list(self.datasrc.bull_rows(5, left, right, yscale=self.ax.vb.yscale)) if self.draw_shadow: p.setPen(pg.mkPen(self.bull_color)) for t,open,close,high,low in rows: if high > low: p.drawLine(QtCore.QPointF(t, low), QtCore.QPointF(t, high)) if self.draw_body: p.setPen(pg.mkPen(self.bull_frame_color)) p.setBrush(pg.mkBrush(self.bull_body_color)) for t,open,close,high,low in rows: p.drawRect(QtCore.QRectF(t-w2, open, w, close-open)) p.end()
def getPath(self): if self.path is None: if self.data is None or len(self.data) < 2: self.path = [QtGui.QPainterPath(), QtGui.QPainterPath()] else: redBars = QtGui.QPainterPath() greenBars = QtGui.QPainterPath() self.step = self.data[1][0] - self.data[0][0] w = self.step / 3.0 for data in self.data: if not np.isnan(data).any(): t, o, h, l, c = data if o > c: redBars.moveTo(QtCore.QPointF(t, l)) redBars.lineTo(QtCore.QPointF(t, h)) redBars.addRect( QtCore.QRectF(t - w, o, w * 2, c - o)) else: greenBars.moveTo(QtCore.QPointF(t, l)) greenBars.lineTo(QtCore.QPointF(t, h)) greenBars.addRect( QtCore.QRectF(t - w, o, w * 2, c - o)) self.path = [redBars, greenBars] return self.path
def boundingRect(self): if self._data: x_min = np.float(-1) x_max = np.float(len(self._data) + 1) y_min = np.float(-1) y_max = np.float(max([v[0] for v in self._data])) return QtCore.QRectF(x_min, y_min, x_max-x_min, y_max-y_min) return QtCore.QRectF(self._picture.boundingRect())
def boundingRect(self): # If not use numpy, then very small value will not paint if self._data: x_min = np.float(-1) x_max = np.float(len(self._data) + 1) y_min = np.float(-1) y_max = np.float(max([item[1] for item in self._data])) return QtCore.QRectF(x_min, y_min, x_max - x_min, y_max - y_min) return QtCore.QRectF(self._picture.boundingRect())
def SSModule(self, opt, pwr, p): print(opt, pwr) # lengths framessx = 144.1 framessy = 125 sssensorx = 102.7 sssensory = 94.2 epsilon = sssensorx * 0.1 w1, h1, w2, h2 = [sssensorx, sssensory, framessx, framessy] p.rotate(-90) # rotate to put empty part to the left # frame |_| frame1+frame3+frame2 p.setBrush(pg.mkBrush(GetPowerColor(pwr))) p.setPen(0) xoff = -w2 / 2 yoff = -h2 / 2 p.translate( xoff, yoff) # translate from center to low-x, low-y position. w = (w2 - w1) * 0.5 h = h2 frame1 = QtCore.QRectF(0, 0, w + epsilon, h) p.drawRect(frame1) p.translate(w + w1 - epsilon, 0) w = (w2 - w1) * 0.5 h = h2 frame2 = QtCore.QRectF(0, 0, w + epsilon, h) p.drawRect(frame2) p.translate(-w - w1 + epsilon, (h2 - h1) * 0.5 + h1) w = w2 h = (h2 - h1) * 0.5 frame3 = QtCore.QRectF(0, 0, w, h) p.drawRect(frame3) p.translate(0, -epsilon) #p.drawRect(rectangle) p.translate(0, epsilon) # sensor p.setBrush(pg.mkBrush('#778899')) p.setPen(pg.mkPen(GetOpticalColor(opt))) p.translate((w2 - w1) * 0.5, -h1) sensor = QtCore.QRectF(0, 0, w1, h1) p.drawRect(sensor) p.translate(w1 * 0.5, h1 * 0.5) # go back to center position p.rotate(90) # rotate to put empty part to the left
def boundingRect(self): if self._boundingRect is None: (xmn, xmx) = self.dataBounds(ax=0) (ymn, ymx) = self.dataBounds(ax=1) if xmn is None or ymn is None: return QtCore.QRectF() px = py = 0.0 self._boundingRect = QtCore.QRectF(xmn - px, ymn - py, (2 * px) + xmx - xmn, (2 * py) + ymx - ymn) return self._boundingRect
def generate_picture(self, boundingRect): w = self.datasrc.period * 0.7 w2 = w * 0.5 left,right = boundingRect.left(), boundingRect.right() p = self.painter p.begin(self.picture) p.setPen(pg.mkPen(self.bear_color)) p.setBrush(pg.mkBrush(self.bear_color)) for t,open,close,volume in self.datasrc.bear_rows(4, left, right, yscale=self.ax.vb.yscale): p.drawRect(QtCore.QRectF(t-w2, 0, w, volume)) p.setPen(pg.mkPen(self.bull_color)) p.setBrush(pg.mkBrush(self.bull_color)) for t,open,close,volume in self.datasrc.bull_rows(4, left, right, yscale=self.ax.vb.yscale): p.drawRect(QtCore.QRectF(t-w2, 0, w, volume)) p.end()
def paint_position(painter, x, y, direction): painter.setBrush(mkBrush(RED)) painter.setPen(mkPen(None)) if direction in ['E', 'W']: robot_width = 50 robot_height = 100 else: robot_width = 100 robot_height = 50 x_compensation = 0 y_compensation = 0 if direction == 'E': y_compensation = robot_height / 2 elif direction == 'S': x_compensation = (CELL_WIDTH - robot_width) / 2 elif direction == 'W': x_compensation = CELL_WIDTH - robot_width y_compensation = robot_height / 2 elif direction == 'N': x_compensation = (CELL_WIDTH - robot_width) / 2 y_compensation = CELL_WIDTH - robot_height painter.drawRect( QtCore.QRectF( x * CELL_WIDTH + x_compensation, -(y + 1) * CELL_WIDTH + y_compensation, robot_width, robot_height, ))
def generatePicture(self): ## pre-computing a QPicture object allows paint() to run much more quickly, ## rather than re-drawing the shapes every time. global last_pf self.picture = QtGui.QPicture() p = QtGui.QPainter(self.picture) w = 1.0/3 #(self.data[1][0] - self.data[0][0]) / 3. #last_pf = self.data[0][2] first = True for (t, open, close, min, max, pf) in self.data: if open > close: #p.setBrush(pg.mkBrush('g')) p.setPen(pg.mkPen('g')) elif open < close: #p.setBrush(pg.mkBrush('r')) p.setPen(pg.mkPen('r')) else: p.setPen(pg.mkPen('w')) if min < max: p.drawLine(QtCore.QPointF(t, min), QtCore.QPointF(t, max)) p.drawRect(QtCore.QRectF(t-w, open, w*2, close-open)) p.setPen(pg.mkPen('y', width=1.5, style=QtCore.Qt.DashLine)) if not first: p.drawLine(QtCore.QPointF(t-1, last_pf), QtCore.QPointF(t, pf)) first = False last_pf = pf p.end()
def draw(self, data, width=1. / 3.): """draw(self, data, width=1./3.) -> None draw an item of boxplot Parameters ---------- data : tuple data has the following format: timestamp : int outliers : array-like lower whisker : float first quartile : float median : float third quartile : float upper whisker : float width : float (default : 1/3) width of an item of boxplot """ t_, out, low_w, q1, q2, q3, up_w = data[:] self.p.drawLine(QtCore.QPointF(t_, low_w), QtCore.QPointF(t_, up_w)) self.p.drawLine(QtCore.QPointF(t_ - width, low_w), QtCore.QPointF(t_ + width, low_w)) self.p.drawLine(QtCore.QPointF(t_ - width, up_w), QtCore.QPointF(t_ + width, up_w)) for out_ in out: self.p.drawEllipse(QtCore.QPointF(t_, out_), width, width) self.p.setBrush(pg.mkBrush("#000000")) self.p.drawRect(QtCore.QRectF(t_ - width, q1, width * 2, q3 - q1)) self.p.setBrush(pg.mkBrush("#FFFFFF")) self.p.drawLine(QtCore.QPointF(t_ - width, q2), QtCore.QPointF(t_ + width, q2))
def setRect(self, *args): """ setRect(rect) or setRect(x,y,w,h) Sets translation and scaling of this ImageItem to display the current image within the rectangle given as ``QtCore.QRect`` or ``QtCore.QRectF`` `rect`, or described by parameters `x, y, w, h`, defining starting position, width and height. This method cannot be used before an image is assigned. See the :ref:`examples <ImageItem_examples>` for how to manually set transformations. """ if len(args) == 0: self.resetTransform( ) # reset scaling and rotation when called without argument return if isinstance(args[0], (QtCore.QRectF, QtCore.QRect)): rect = args[0] # use QRectF or QRect directly else: if hasattr(args[0], '__len__'): args = args[0] # promote tuple or list of values rect = QtCore.QRectF( *args) # QRectF(x,y,w,h), but also accepts other initializers tr = QtGui.QTransform() tr.translate(rect.left(), rect.top()) tr.scale(rect.width() / self.width(), rect.height() / self.height()) self.setTransform(tr)
def boundingRect(self): ## boundingRect _must_ indicate the entire area that will be drawn on ## or else we will get artifacts and possibly crashing. # This constructor is (x_topleft, y_topleft, width, height) topleftx = self.x - self.r toplefty = self.y - self.r return QtCore.QRectF(topleftx, toplefty, 2 * self.r, 2 * self.r)
def big_square(p, colors=[1, 0]): w1, h1, w2, h2 = [2, 2, 2.2, 2.2] epsilon = w1 * 0.1 p.rotate(-90) # rotate to put empty part to the left # Exterior square p.setBrush(pg.mkBrush(colors[1])) p.setPen(0) xoff = -w2 / 2 yoff = -h2 / 2 p.translate( xoff, yoff) # translate from center to low-x, low-y position. w = (w2 - w1) * 0.5 h = h2 rectangle = QtCore.QRectF(0, 0, w + epsilon, h) p.drawRoundedRect(rectangle, 0.0, 0.5) p.translate(w + w1 - epsilon, 0) w = (w2 - w1) * 0.5 h = h2 rectangle = QtCore.QRectF(0, 0, w + epsilon, h) p.drawRoundedRect(rectangle, 0.0, 0.05) p.translate(-w - w1 + epsilon, (h2 - h1) * 0.5 + h1) w = w2 h = (h2 - h1) * 0.5 rectangle = QtCore.QRectF(0, 0, w, h) p.drawRoundedRect(rectangle, 0.05, 0.0) p.translate(0, -epsilon) p.drawRect(rectangle) p.translate(0, epsilon) # Interior square p.setBrush(pg.mkBrush(colors[0])) p.setPen(pg.mkPen('k')) p.translate((w2 - w1) * 0.5, -h1) rectangle = QtCore.QRectF(0, 0, w1, h1) p.drawRoundedRect(rectangle, 0.05, 0.05) p.translate(w1 * 0.5, h1 * 0.5) # go back to center position p.rotate(90) # rotate to put empty part to the left
def boundingRect(self): """ indicate the entire area that will be drawn on or else we will get artifacts and possibly crashing. (in this case, QPicture does all the work of computing the bouning rect for us) :return: """ return QtCore.QRectF(self.picture.boundingRect())
def getPath(self): if self.path is None: if self.data is None or len(self.data) < 2: self.path = [QtGui.QPainterPath(), QtGui.QPainterPath()] else: redBars = QtGui.QPainterPath() greenBars = QtGui.QPainterPath() step = self.data[1][0] - self.data[0][0] w = step / 3.0 for t, buy, sell in self.data: if buy + sell != 0: redBars.addRect(QtCore.QRectF(t - w, 0, w * 2, sell)) greenBars.addRect(QtCore.QRectF(t - w, sell, w * 2, buy)) self.path = [redBars, greenBars] return self.path
def __init__(self, datasrc): super().__init__() self.datasrc = datasrc self.picture = QtGui.QPicture() self.painter = QtGui.QPainter() self.dirty = True # generate picture visibleRect = QtCore.QRectF(self.datasrc.init_x0, 0, self.datasrc.init_x1-self.datasrc.init_x0, 0) self._generate_picture(visibleRect)
def generatePicture(self): ## pre-computing a QPicture object allows paint() to run much more quickly, ## rather than re-drawing the shapes every time. self.picture = QtGui.QPicture() p = QtGui.QPainter(self.picture) p.setPen(pg.mkPen('w')) w = (self.data[1][0] - self.data[0][0]) / 3. for (t, open, close, min, max) in self.data: p.drawLine(QtCore.QPointF(t, min), QtCore.QPointF(t, max)) if open > close: p.setBrush(pg.mkBrush('r')) p.drawRect(QtCore.QRectF(t - w, open, w * 2, open - close)) elif close > open: p.setBrush(pg.mkBrush('g')) p.drawRect(QtCore.QRectF(t - w, open, w * 2, close - open)) else: p.setBrush(pg.mkBrush('w')) p.drawRect(QtCore.QRectF(t - w, open, w * 2, 1)) p.end()
def update_picture(self): self._picture = QtGui.QPicture() pen = QtGui.QPainter(self._picture) pen.setPen(pg.mkPen(self._border_color)) pen.setBrush(pg.mkBrush(self._color_bar)) width = 1 / 3. for ind, bar in enumerate(self._data): pen.drawRect(QtCore.QRectF(ind - width, 0, width * 2, bar[0])) pen.end() self.update()
def set_range(self, x0, y0, x1, y1, pad=False): if np.isnan(y0) or np.isnan(y1): return if pad: x0 -= self.datasrc.period*0.5 x1 += self.datasrc.period*0.5 if self.yscale == 'log': y0 = np.log10(y0) if y0 > 0 else 0 y1 = np.log10(y1) if y1 > 0 else 0 self.setRange(QtCore.QRectF(pg.Point(x0, y0), pg.Point(x1, y1)), padding=0)
def updateManual(self): """ :return: """ if self.manual_levels is None or self.manual_lut is None: return self.vb.setYRange(*self.manual_levels, padding=0) self.bar.setRect( QtCore.QRectF(0, self.manual_levels[0], 1, self.manual_levels[1] - self.manual_levels[0])) self.bar.setLookupTable(self.manual_lut)
def updateData(self): self.picture = QtGui.QPicture() p = QtGui.QPainter(self.picture) for x, y, df, step, alpha in self.data: p.setPen(pg.mkPen(63, 63, 63, alpha)) p.setBrush(pg.mkBrush(63, 63, 63, alpha)) p.drawRect(QtCore.QRectF(x[0], y[1], x[1] - x[0], y[0] - y[1])) x_length = x[1] - x[0] x_pos = minmax_scale(df.to_numpy(), (0.1 * x_length, x_length / 2)) for interval, width in zip(df.index, x_pos): p.setBrush(pg.mkBrush(0, 255, 0, alpha)) p.drawRect(QtCore.QRectF(x[0], interval.left, width[0], step)) p.setBrush(pg.mkBrush(255, 0, 0, alpha)) p.drawRect( QtCore.QRectF(x[0] + width[0], interval.left, width[1], step)) p.end() self.update()
def paint_discovered(painter, distances, walls): if walls is not None: paint_walls(painter, walls, color=WHITE) for (x, y) in product(range(MAZE_SIZE), repeat=2): painter.setPen(mkPen(color=GRAY)) if walls is not None: wall = walls[x][y] if wall & VISITED_BIT: painter.setPen(mkPen(color=GREEN)) painter.drawText( QtCore.QRectF((x + .5) * CELL_WIDTH + WALL_WIDTH / 2 - 50, -(y + .5) * CELL_WIDTH + WALL_WIDTH / 2 - 50, 100, 100), QtCore.Qt.AlignCenter, '%s' % distances[x][y])
def PSModule(self, opt, pwr, p): # lengths: pssensorx = 98.7 pssensory = 49.2 framepsx = 130 framepsy = 69.6 # Exterior square p.setBrush(pg.mkBrush(GetPowerColor(pwr))) p.setPen(pg.mkPen('k')) w1 = framepsx h1 = framepsy p.translate(-w1 / 2, -h1 / 2) # translate from center to low-x, low-y position. rectangle = QtCore.QRectF(0, 0, w1, h1) p.drawRoundedRect(rectangle, 5, 5) p.translate(w1 / 2, h1 / 2) # go back to center positio # Interior square p.setBrush(pg.mkBrush('#778899')) p.setPen(pg.mkPen(GetOpticalColor(opt))) w2 = pssensorx h2 = pssensory p.translate(-w2 / 2, -h2 / 2) rectangle = QtCore.QRectF(0, 0, w2, h2) p.drawRoundedRect(rectangle, 5, 5) p.translate(w2 / 2, h2 / 2) # go back to center positio # Inter-Internal p.setPen(pg.mkPen('k')) w = w2 * 0.8 h = h2 * 0.5 p.translate(-w / 2, -h / 2) rectangle = QtCore.QRectF(0, 0, w, h) p.drawRoundedRect(rectangle, 20, 20) p.translate(w / 2, h / 2) # go back to center position
def generatePicture(self): self.picture = QtGui.QPicture() p = QtGui.QPainter(self.picture) p.setPen(pg.mkPen('w')) w = (self.data[1][0] - self.data[0][0]) / 3. for (t, open, close, min, max) in self.data: p.drawLine(QtCore.QPointF(t, min), QtCore.QPointF(t, max)) if open > close: p.setBrush(pg.mkBrush('r')) else: p.setBrush(pg.mkBrush('g')) p.drawRect(QtCore.QRectF(t - w, open, w * 2, close - open)) p.end()
def generatePicture(self): # pre-computing a QPicture object allows paint() to run much more quickly, # rather than re-drawing the shapes every time. self.picture = QtGui.QPicture() p = QtGui.QPainter(self.picture) p.setPen(pg.mkPen('w')) p.setBrush(pg.mkBrush(None)) for zhongshu in self.data: p.drawRect(QtCore.QRectF(chan.chanBars[zhongshu.barIndex1].closeIndex, zhongshu.low, chan.chanBars[ zhongshu.barIndex2].closeIndex - chan.chanBars[zhongshu.barIndex1].closeIndex, zhongshu.high - zhongshu.low)) p.end()
def paint_template(painter, walls): if walls is not None: paint_walls(painter=painter, walls=walls, color=GRAY) for (x, y) in product(range(MAZE_SIZE + 1), repeat=2): painter.setBrush(mkBrush(WHITE)) painter.setPen(mkPen(None)) painter.drawRect( QtCore.QRectF( x * CELL_WIDTH - WALL_WIDTH / 2, -y * CELL_WIDTH + WALL_WIDTH / 2, WALL_WIDTH, WALL_WIDTH, ))
def paint_walls(painter, walls, color): painter.setBrush(mkBrush(color)) painter.setPen(mkPen(None)) for (x, y) in product(range(MAZE_SIZE), repeat=2): wall = walls[y][x] if wall & EAST_BIT: painter.drawRect( QtCore.QRectF( (y + 1) * CELL_WIDTH - WALL_WIDTH / 2, -(x + 1) * CELL_WIDTH + WALL_WIDTH / 2, WALL_WIDTH, CELL_WIDTH, )) if wall & SOUTH_BIT: painter.drawRect( QtCore.QRectF( y * CELL_WIDTH + WALL_WIDTH / 2, -x * CELL_WIDTH + WALL_WIDTH / 2, CELL_WIDTH, WALL_WIDTH, )) if wall & WEST_BIT: painter.drawRect( QtCore.QRectF( y * CELL_WIDTH - WALL_WIDTH / 2, -(x + 1) * CELL_WIDTH + WALL_WIDTH / 2, WALL_WIDTH, CELL_WIDTH, )) if wall & NORTH_BIT: painter.drawRect( QtCore.QRectF( y * CELL_WIDTH + WALL_WIDTH / 2, -(x + 1) * CELL_WIDTH + WALL_WIDTH / 2, CELL_WIDTH, WALL_WIDTH, ))
def generatePicture(self): ''' pre-computing a QPicture object allows paint() to run much more quickly, rather than re-drawing the shapes every time.''' self.picture = QtGui.QPicture() p = QtGui.QPainter(self.picture) p.setPen(pg.mkPen('w')) w = (self.data[1][0] - self.data[0][0]) / 3. for (t, opn, high, low, close) in self.data: p.drawLine(QtCore.QPointF(t, low), QtCore.QPointF(t, high)) if opn > close: p.setBrush(pg.mkBrush('r')) else: p.setBrush(pg.mkBrush('g')) p.drawRect(QtCore.QRectF(t - w, opn, w * 2, close - opn)) p.end()
def generatePicture(self): ## pre-computing a QPicture object allows paint() to run much more quickly, ## rather than re-drawing the shapes every time. self.picture = QtGui.QPicture() p = QtGui.QPainter(self.picture) p.setPen(pg.mkPen('w')) w = 1 / 3. for (open, max, min, close, vol, ma10, ma20, ma30, t) in self.data.values: p.drawLine(QtCore.QPointF(t, min), QtCore.QPointF(t, max)) if open > close: p.setBrush(pg.mkBrush('g')) else: p.setBrush(pg.mkBrush('r')) p.drawRect(QtCore.QRectF(t - w, open, w * 2, close - open)) p.end()