def __showCircle(self): ratio = 3 pen = QPen() pen.setBrush(Qt.red) pen.setWidth(0.01) font = QFont() font.setPointSize(200 * ratio) self.scene.clear() for i in range(len(self.pointsXYItem)): (x, y) = self.pointsXYItem[i] r = self.radius[i] * ratio circleItem = QGraphicsEllipseItem(x - r, y - r, 2 * r, 2 * r) circleItem.setPen(pen) self.scene.addItem(circleItem) strItem = QGraphicsTextItem() strItem.setDefaultTextColor(Qt.blue) strItem.setFont(font) strItem.setPlainText(self.pointsName[i]) strItem.setPos(x, y) self.scene.addItem(strItem)
def createValuesText(self): painter = QPainter(self) # painter.setRenderHint(QPainter.HighQualityAntialiasing) painter.setRenderHint(QPainter.Antialiasing) painter.translate(self.width() / 2, self.height() / 2) font = QFont(self.valueFontName, self.valueFontSize) fm = QFontMetrics(font) penShadow = QPen() penShadow.setBrush(self.displayValueColor) painter.setPen(penShadow) textRadius = self.widgetDiameter / 2 * self.textRadiusFactor # angle_distance = (float(self.scaleAngleSize) / float(self.scala_main_count)) # for i in range(self.scala_main_count + 1): text = str(int(self.value)) w = fm.width(text) + 1 h = fm.height() painter.setFont(QFont(self.valueFontName, self.valueFontSize)) angleEnd = float(self.scaleAngleStartValue + self.scaleAngleSize - 360) angle = (angleEnd - self.scaleAngleStartValue) / 2 + self.scaleAngleStartValue x = textRadius * math.cos(math.radians(angle)) y = textRadius * math.sin(math.radians(angle)) text = [ x - int(w / 2), y - int(h / 2), int(w), int(h), Qt.AlignCenter, text ] painter.drawText(text[0], text[1], text[2], text[3], text[4], text[5])
def _paintRegistrationMarks(printer, painter): page = printer.pageLayout().fullRectPoints() margin = printer.pageLayout().marginsPoints() # Get ourselves some nice abbreviations pw = page.width() ph = page.height() ml = margin.left() mr = margin.right() mt = margin.top() mb = margin.bottom() # A caution factor of 90% to keep our registration lines from # running into the main page area cf = 0.9 pen = QPen() pen.setStyle(Qt.SolidLine) pen.setWidth(1) pen.setBrush(Qt.black) painter.save() painter.setPen(pen) painter.drawLine(0, mt, ml * cf, mt) painter.drawLine(ml, 0, ml, mt * cf) painter.drawLine(pw, mt, pw - mr * cf, mt) painter.drawLine(pw - mr, 0, pw - mr, mt * cf) painter.drawLine(0, ph - mb, ml * cf, ph - mb) painter.drawLine(ml, ph, ml, ph - mb * cf) painter.drawLine(pw, ph - mb, pw - mr * cf, ph - mb) painter.drawLine(pw - mr, ph, pw - mr, ph - mb * cf) painter.restore()
def paintEvent(self, event): # Check whether this orb is enhanced if type(self.parent) == Board: enh = self.parent.enhanced[self.position] else: enh = False painter = QPainter(self) painter.drawPixmap(event.rect().adjusted(2,2,-2,-2), self.pixmap()) w = event.rect().width() if enh: path = QPainterPath() pen = QPen() pen.setWidth(1); pen.setBrush(Qt.white) brush = QBrush(Qt.yellow) font = QFont() font.setPointSize(20) font.setWeight(QFont.Black) path.addText(event.rect().x()+w-15,event.rect().y()+w-5,font,'+') painter.setPen(pen) painter.setBrush(brush) painter.setFont(font) painter.drawPath(path)
def drawLines(self, qp): pen = QPen(Qt.black, 2, Qt.SolidLine) qp.setPen(Qt.gray) with open('edge_list.pkl', 'rb') as f: edg = pickle.load(f) #main() for e in edg : qp.drawLine(e[0][0],e[0][1],e[1][0],e[1][1]) if(self.init_phase == 3): pen = QPen(Qt.black, 5, Qt.DashDotLine) # qp.setPen(Qt.red) # qp.setWidth(10) pen.setBrush(Qt.red) pen.setWidth(5) qp.setPen(pen) for i in range(len(self.path_t)-1): a=self.path_t[i] # print("---- point a --- ") # print(a) a_pos=dict_index_coord[a] b=self.path_t[i+1] # print("---- point b --- ") # print(b) b_pos=dict_index_coord[b] qp.drawLine(a_pos[0],a_pos[1],b_pos[0],b_pos[1])
def drawDigitalValueUnits(self): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.translate(self.width() / 2, self.height() / 2) font = QFont(self.digitalValueFontName, self.digitalValueUnitsFontSize) fm = QFontMetrics(font) penShadow = QPen() penShadow.setBrush(self.digitalValueColor) painter.setPen(penShadow) digitalValueRadius = self.widgetDiameter / 2 * self.digitalValueRadius text = self.gaugeValueUnits w = fm.width(text) + 1 h = fm.height() painter.setFont( QFont(self.digitalValueFontName, self.digitalValueUnitsFontSize)) angleEnd = float(self.gaugeRotation + self.gaugeArcAngle - 360) angle = (angleEnd - self.gaugeRotation) / 2 + self.gaugeRotation x = digitalValueRadius * math.cos(math.radians(angle)) y = digitalValueRadius * math.sin(math.radians(angle)) text = [ x - int(w / 2) + 20, y - int(h / 2), int(w), int(h), Qt.AlignCenter, text ] painter.drawText(text[0], text[1], text[2], text[3], text[4], text[5])
def draw_head_arrow( painter: QPainter, up: bool, center_x: int, center_y: int, width: int, height: int, brush: QBrush ): painter.save() pen = QPen() pen.setBrush(brush) painter.setBrush(brush) painter.setPen(pen) path = QPainterPath() path.moveTo(center_x + width // 2, center_y) path.lineTo(center_x - width // 2, center_y) path.lineTo(center_x, center_y - height if up else center_y + height) path.lineTo(center_x + width // 2, center_y) painter.fillPath(path, brush) painter.drawPath(path) painter.restore()
def drawSnakeAndFood(self, event, painter): """ Draws the snake and food""" pen = QPen() pen.setWidth(1) pen.setBrush(Qt.white) pen.setCapStyle(Qt.SquareCap) pen.setJoinStyle(Qt.MiterJoin) brush = QBrush(Qt.white) painter.setPen(pen) painter.setBrush(brush) # Draw each individual square of snake for bodySquare in self.snake.bodyPositions: x = bodySquare[0] * self.squareSize y = bodySquare[1] * self.squareSize painter.drawRect(x, y, self.squareSize, self.squareSize) # Draw food x = self.foodPosition[0] * self.squareSize y = self.foodPosition[1] * self.squareSize painter.drawRect(x, y, self.squareSize, self.squareSize)
def draw_beacon_indicators(painter: QPainter, map: map, robot: Robot): pen = QPen() pen.setStyle(Qt.DotLine) pen.setCapStyle(Qt.RoundCap) pen.setBrush(SETTINGS["COLOR_BEACON"]) pen.setWidth(2) painter.setPen(pen) distances = [b.distance_to(robot.x, robot.y) for b in map.beacons] closest_beacon_id = distances.index(min(distances)) for i, beacon in enumerate(map.beacons): if beacon.distance_to( robot.x, robot.y) <= SETTINGS["BEACON_INDICATOR_DISTANCE"]: painter.drawLine(beacon.x, beacon.y, robot.x, robot.y) if i == closest_beacon_id: pen.setWidth(1) pen.setStyle(Qt.SolidLine) painter.setPen(pen) painter.drawEllipse(QPoint(beacon.x, beacon.y), SETTINGS["BEACON_SIZE"] * 1.2, SETTINGS["BEACON_SIZE"] * 1.2) pen.setWidth(2) pen.setStyle(Qt.DotLine) painter.setPen(pen)
def draw(self, pt): # Устанавливаем кисть, цвет, и прочее pen = QPen() pen.setBrush(QBrush(self.color)) pen.setCapStyle(Qt.RoundCap) pen.setWidth(self.width) pt.setPen(pen) pt.drawRect(self.sx, self.sy, self.w, self.h)
def draw(self, pt): # Устанавливаем кисть, цвет, и прочее pen = QPen() pen.setBrush(QBrush(self.color)) pen.setWidth(self.width) pt.setPen(pen) pt.drawEllipse(self.x - self.width // 2, self.y - self.width // 2, self.width, self.width)
def pen(self): """create and return default pen""" pen = QPen() pen.setStyle(DefaultDrawParams.pen_style) pen.setWidth(DefaultDrawParams.pen_thickness) pen.setBrush(self.border_color) pen.setCapStyle(DefaultDrawParams.pen_cap_style) pen.setJoinStyle(DefaultDrawParams.pen_join_style) return pen
def draw(self, pt): # Устанавливаем кисть, цвет, и прочее # Находим радиус окружностипо формуле из геометрии pen = QPen() pen.setBrush(QBrush(self.color)) pen.setCapStyle(Qt.RoundCap) pen.setWidth(self.width) pt.setPen(pen) rad = int(((self.ex - self.sx)**2 + (self.ey - self.sy)**2)**0.5) pt.drawEllipse(self.ex - rad, self.ey - rad, 2 * rad, 2 * rad)
def draw_filter_trace(painter: QPainter, trace: QPainterPath): pen = QPen() pen.setStyle(Qt.DashLine) pen.setCapStyle(Qt.RoundCap) pen.setBrush(SETTINGS["COLOR_FILTER_TRACE"]) pen.setWidth(3) painter.setPen(pen) painter.setBrush(Qt.NoBrush) painter.drawPath(trace)
def create_inner_and_outer_mask(self): ''' Based on the traces, this function creates two masks: One ('inner_mask') to mark all pixels inside the shape as defined by the traces, and one mask, 'outer_mask' to mark the pixels outside the shape defined by the traces :return: inner_mask and outer_mask: Two boolean masks ''' # use the same method to draw the traces pixmap = QPixmap(self.slice_photo.size()) pixmap.fill(QColor(255, 255, 255)) painter = QPainter() painter.begin(pixmap) pen = QPen() pen.setWidth(self.pen_width) pen.setBrush(Qt.black) pen.setCapStyle(Qt.RoundCap) painter.setPen(pen) for trace in self.traces: painter.drawPolyline(QPolygon(trace)) painter.end() # that is us done painting #convert the Qpixmap into a numpy array arr = self.get_np_array(pixmap) # find the center arr = np.mean(arr, axis=-1, dtype=np.uint8) l = [] for trace in self.traces: l = l + [[p.x(), p.y()] for p in trace] center = np.mean(np.array(l), axis=0, dtype=np.int) # fill the shape ( using floodfill) starting from the center inner_mask = arr.copy() h, w = inner_mask.shape[:2] mask = np.zeros((h + 2, w + 2), np.uint8) cv2.floodFill(inner_mask, mask, (center[0], center[1]), 128) #and calculate the masks inner_mask = (inner_mask > 100).astype(np.bool) middle_mask = (arr < 32).astype(np.bool) outer_mask = np.logical_and(~inner_mask, ~middle_mask) # for debugging purposes, create a matplotlib image (only visible in IDE) fig, axs = plt.subplots(2, 2) axs[0, 0].imshow(arr) axs[0, 1].imshow(middle_mask) axs[1, 0].imshow(inner_mask) axs[1, 1].imshow(outer_mask) for axh in axs: for ax in axh: ax.set_aspect('equal') fig.tight_layout() plt.show() return inner_mask, outer_mask
class MyQLabel(QLabel): def __init__(self, parent): super().__init__(parent) self.label_maxw = 1200.0 self.label_maxh = 700.0 self.setGeometry(50, 50, self.label_maxw, self.label_maxh) self.pen = QPen() self.pen.setWidth(5) self.pen.setBrush(Qt.red) self.pos = None self.png = None def paintEvent(self, e): qp = QPainter() qp.begin(self) if self.png: qp.drawPixmap(0, 0, self.png) if self.pos: qp.setPen(self.pen) qp.drawPoint(self.pos.x(), self.pos.y()) qp.end() def mousePressEvent(self, e): self.pos = e.pos() print('PRESS: %d,%d' % (self.pos.x(), self.pos.y())) self.update() global CurrentAnnos if CurrentAnnos.imagepath: CurrentAnnos.cur_keypoint[CurrentAnnos.cur_partID, 0] = self.pos.x() CurrentAnnos.cur_keypoint[CurrentAnnos.cur_partID, 1] = self.pos.y() if CurrentAnnos.cur_vis == True: CurrentAnnos.cur_keypoint[CurrentAnnos.cur_partID, 2] = 1 elif CurrentAnnos.cur_vis == False: CurrentAnnos.cur_keypoint[CurrentAnnos.cur_partID, 2] = 2 CurrentAnnos.print('mousePressEvent') def loadimg(self, filename): self.pos = None png = QPixmap(filename) ratio = min(self.label_maxw / png.width(), self.label_maxh / png.height()) self.png = png.scaled(png.width() * ratio, png.height() * ratio) self.update() global CurrentAnnos CurrentAnnos.init() CurrentAnnos.imagepath = filename CurrentAnnos.scaleratio = ratio CurrentAnnos.print('loadimg')
def drawPipes(self, qp): pen = QPen() pen.setWidth(2) brush = QBrush() brush.setColor(QColor(0x88, 0x88, 0x88)) brush.setStyle(Qt.SolidPattern) pen.setBrush(brush) qp.setPen(pen) for b, (i, j) in zip(self.blocks, self.block_generator()): coord = self.get_coord(i, j) b.draw(qp, coord)
def draw_line(self, points, pencil): pen = QPen() pen.setBrush(QColor(pencil)) point1 = points[0] point2 = points[1] self.addLine(point1.x(), point1.y(), point2.x(), point2.y(), pen) self.svg_document.add( self.svg_document.line(start=(point1.x(), point1.y()), end=(point2.x(), point2.y()), stroke=pencil)) self.resert_clicks()
def __makePen(self): """ Dit is een hulpmethode om de gepaste pen te maken die dan wordt teruggegeven :return: QPen """ pen = QPen() pen.setColor(self.__color) pen.setBrush(QBrush(self.__color)) pen.setWidth(self.__penWidth) return pen
def __init__(self, x1, y1, x2, y2, thickness, visible, color, parent): super().__init__(x1, y1, x2, y2, parent) pen = QPen() pen.setWidth(1) if color is None: pen.setBrush(Qt.green) else: pen.setBrush(color) #self.setBrush(Qt.white) self.setPen(pen) self.setFlag(QGraphicsItem.ItemIsMovable) self.setVisible(visible)
def paintEvent(self, event): qp = QPainter() qp.begin(self) qp.setRenderHint(QPainter.Antialiasing) pen = QPen() pen.setStyle(Qt.SolidLine) pen.setBrush(SETTINGS["COLOR_BACKGROUND"].darker(200)) pen.setWidth(6) qp.setOpacity(0.6) qp.setPen(pen) qp.setBrush(SETTINGS["COLOR_BACKGROUND"].darker(120)) qp.drawRect(0, 0, self.tracker_width, self.tracker_height) qp.end()
def draw_polygon(self, polygon, pencil, fill): pen = QPen() pen.setBrush(QColor(pencil)) self.addPolygon(polygon, pen) \ .setBrush(QColor(fill)) newpolygon = [] for e in polygon: newpolygon.append((int(e.x()), int(e.y()))) self.svg_document.add( self.svg_document.polygon(newpolygon, stroke_width="1", stroke=pencil, fill=fill)) self.resert_clicks()
def draw_rectangle(self, points, width, height, pencil, fill): pen = QPen() pen.setBrush(QColor(pencil)) point = points[0] (self.addRect(point.x(), point.y(), width, height, pen)) \ .setBrush(QColor(fill)) self.svg_document.add( self.svg_document.rect(insert=(point.x(), point.y()), size=("{}px".format(width), "{}px".format(height)), stroke_width="1", stroke=pencil, fill=fill)) self.resert_clicks()
def draw_ellipse(self, points, width, height, pencil, fill): pen = QPen() pen.setBrush(QColor(pencil)) point = points[0] self.addEllipse(point.x(), point.y(), width, height, pen) \ .setBrush(QColor(fill)) self.svg_document.add( self.svg_document.ellipse(center=(point.x() + width / 2, point.y() + height / 2), r=(width / 2, height / 2), stroke_width="1", stroke=pencil, fill=fill)) self.resert_clicks()
def paintEvent(self, QPaintEvent): painter = QPainter(self) pen = QPen() if self.direction == 'forward': trianglePointsF = [ QPointF(self.size * 0.05, self.size * 0.05), QPointF(self.size - (self.size * 0.05), self.size / 2), QPointF(self.size * 0.05, self.size - (self.size * 0.05)) ] gradient = QLinearGradient(0, 0, 100, 100) else: trianglePointsF = [ QPointF(self.size - (self.size * 0.05), self.size * 0.05), QPointF(self.size * 0.05, self.size / 2), QPointF(self.size - (self.size * 0.05), self.size - (self.size * 0.05)) ] gradient = QLinearGradient(100, 0, 0, 100) pen.setWidth(self.size * 0.04) painter.setRenderHint(QPainter.Antialiasing) if self.isChecked(): color = QColor(110, 110, 110) gradient.setColorAt(0, self.on_color_1) gradient.setColorAt(1, self.on_color_2) else: color = QColor(189, 189, 189) gradient.setColorAt(0, self.off_color_1) gradient.setColorAt(1, self.off_color_2) pen.setBrush(QBrush(color)) painter.setPen(pen) painter.setBrush(gradient) triangleF = QPolygonF(trianglePointsF) basePoly = QPolygonF(triangleF) painter.drawPolygon(basePoly)
def createArrow(self): rad = self._RADIUS pen = QPen() pen.setWidth(3) color = QColor(Qt.blue) color.setAlphaF(0.25) pen.setBrush(color) if self._virtual_helix.isEvenParity(): arrow = QGraphicsLineItem(rad, rad, 2*rad, rad, self) else: arrow = QGraphicsLineItem(0, rad, rad, rad, self) arrow.setTransformOriginPoint(rad, rad) arrow.setZValue(400) arrow.setPen(pen) self.arrow = arrow self.arrow.hide()
def paint(self, painter): if self._num_signals is None: return None self._calc_visual_parametres() pen = QPen() pen.setStyle(Qt.DashLine) pen.setWidth(1) pen.setBrush(QColor('#565656')) painter.setPen(pen) for curr_yaxe_position in self._yaxes_positions: painter.drawLine(0, curr_yaxe_position, self.width(), curr_yaxe_position) for curr_xaxe_position in self._xaxes_positions: painter.drawLine(curr_xaxe_position, 0, curr_xaxe_position, self.height())
def paintEvent(self, event): if self.eventByButton == True: self.eventByButton = False self.width = self.frameGeometry().width() self.height = self.frameGeometry().height() painter = QPainter() painter.begin(self) myPen = QPen() for i in range(self.nbPointsNoirs): myPen.setColor(Qt.black) myPen.setBrush(Qt.black) painter.setPen(myPen) calcul_taille_bordure_gauche = int(30 / 1366 * self.width) calcul_taille_bordure_droite = int( self.width - 166) #166 : 1366-1200 : taille de la bordure sur mon écran calcul_taille_bordure_haut = calcul_taille_bordure_gauche calcul_taille_bordure_bas = int(665 / 728 * self.height) x = randint(calcul_taille_bordure_gauche, int(calcul_taille_bordure_droite)) y = randint(calcul_taille_bordure_haut, calcul_taille_bordure_bas) painter.drawRect(x, y, 1, 1) myPen.setColor(Qt.red) myPen.setBrush(Qt.red) x = randint(calcul_taille_bordure_gauche, int(calcul_taille_bordure_droite)) y = randint(calcul_taille_bordure_haut, calcul_taille_bordure_bas) print(x, y) painter.setPen(myPen) painter.drawRect(x, y, 1, 1) myPen.setWidth(5) painter.setPen(myPen) painter.drawEllipse(x - 15, y - 15, 30, 30) painter.end()
def createImage(self, transform): sx = min(transform.m11(), transform.m22()) sy = max(transform.m22(), sx) fm = QFontMetrics(Colors.headingFont()) w = fm.width(self.text) + 1 h = fm.height() xShadow = 3.0 yShadow = 3.0 image = QImage( int((w + xShadow) * sx), int((h + yShadow) * sy), QImage.Format_ARGB32_Premultiplied, ) image.fill(QColor(0, 0, 0, 0).rgba()) painter = QPainter(image) painter.setFont(Colors.headingFont()) painter.scale(sx, sy) # Draw shadow. brush_shadow = QLinearGradient(xShadow, yShadow, w, yShadow) brush_shadow.setSpread(QLinearGradient.PadSpread) if Colors.useEightBitPalette: brush_shadow.setColorAt(0.0, QColor(0, 0, 0)) else: brush_shadow.setColorAt(0.0, QColor(0, 0, 0, 100)) pen_shadow = QPen() pen_shadow.setBrush(brush_shadow) painter.setPen(pen_shadow) painter.drawText(int(xShadow), int(yShadow), int(w), int(h), Qt.AlignLeft, self.text) # Draw text. brush_text = QLinearGradient(0, 0, w, w) brush_text.setSpread(QLinearGradient.PadSpread) brush_text.setColorAt(0.0, QColor(255, 255, 255)) brush_text.setColorAt(0.2, QColor(255, 255, 255)) brush_text.setColorAt(0.5, QColor(190, 190, 190)) pen_text = QPen() pen_text.setBrush(brush_text) painter.setPen(pen_text) painter.drawText(0, 0, int(w), int(h), Qt.AlignLeft, self.text) return image
def paint(self, painter, size, show_traces): ''' painting the slice :param painter: the painter it is to use :param size: :param show_traces: :return: ''' try: photo = self.slice_photo size_draw = QSize(min(size.width(), photo.width()), min(size.height(), photo.height())) if not show_traces and not self.mask is None: #if we are not showing traces but we do have a mask # create a full new QPixmap which combines the slice_photo with the mask # more or less standard work with images arr_photo = self.get_np_array(photo) arr_photo = cv2.cvtColor(arr_photo, cv2.COLOR_BGR2RGB) exp_mask = np.expand_dims(self.mask, -1) #adding the alpha channel: arr_photo = np.concatenate((arr_photo, 255 * exp_mask), axis=2).astype(np.uint8) #creating the QImage qimage = QImage(arr_photo, arr_photo.shape[1], arr_photo.shape[0], QImage.Format_ARGB32) #and the QPixmap pixmap = QPixmap(qimage) painter.drawPixmap(QRect(QPoint(0, 0), size_draw), pixmap, QRect(QPoint(0, 0), size_draw)) else: painter.drawPixmap(QRect(QPoint(0, 0), size_draw), photo, QRect(QPoint(0, 0), size_draw)) pen = QPen() pen.setWidth(self.pen_width) pen.setBrush(Qt.green) pen.setCapStyle(Qt.RoundCap) painter.setPen(pen) for trace in self.traces: painter.drawPolyline(QPolygon(trace)) except: self.logger.error(sys.exc_info()[0]) self.logger.error(traceback.format_exc()) return None
def create_values_text(self): painter = QPainter(self) # painter.setRenderHint(QPainter.HighQualityAntialiasing) painter.setRenderHint(QPainter.Antialiasing) # Koordinatenursprung in die Mitte der Flaeche legen painter.translate(self.width() / 2, self.height() / 2) # painter.save() # xShadow = 3.0 # yShadow = 3.0 font = QFont(self.value_fontname, self.value_fontsize) fm = QFontMetrics(font) pen_shadow = QPen() pen_shadow.setBrush(self.DisplayValueColor) painter.setPen(pen_shadow) text_radius = self.widget_diameter / 2 * self.text_radius_factor # angle_distance = (float(self.scale_angle_size) / float(self.scala_main_count)) # for i in range(self.scala_main_count + 1): text = str(int(self.value)) w = fm.width(text) + 1 h = fm.height() painter.setFont(QFont(self.value_fontname, self.value_fontsize)) # Mitte zwischen Skalenstart und Skalenende: # Skalenende = Skalenanfang - 360 + Skalenlaenge # Skalenmitte = (Skalenende - Skalenanfang) / 2 + Skalenanfang angle_end = float(self.scale_angle_start_value + self.scale_angle_size - 360) angle = (angle_end - self.scale_angle_start_value ) / 2 + self.scale_angle_start_value x = text_radius * math.cos(math.radians(angle)) y = text_radius * math.sin(math.radians(angle)) # print(w, h, x, y, text) text = [ x - int(w / 2), y - int(h / 2), int(w), int(h), Qt.AlignCenter, text ] painter.drawText(text[0], text[1], text[2], text[3], text[4], text[5])
def __init__(self, img): super(DrawScene, self).__init__() self.imagePanel = ImageDrawPanel(parent=self, image=img) self.addItem(self.imagePanel) self.rect = QGraphicsRectItem() self.addItem(self.rect) self.setImage(img) pen = QPen(Qt.DashDotLine) pen.setBrush(Qt.red) # pen.setWidth(3) self.rect.setPen(pen) self.setBackgroundBrush(QBrush(Qt.darkGray)) comm.rectResult.connect(self.drawRect) comm.reset.connect(self.resetRect)
def createImage(self, transform): sx = min(transform.m11(), transform.m22()) sy = max(transform.m22(), sx) fm = QFontMetrics(Colors.headingFont()) w = fm.width(self.text) + 1 h = fm.height() xShadow = 3.0 yShadow = 3.0 image = QImage(int((w + xShadow) * sx), int((h + yShadow) * sy), QImage.Format_ARGB32_Premultiplied) image.fill(QColor(0, 0, 0, 0).rgba()) painter = QPainter(image) painter.setFont(Colors.headingFont()) painter.scale(sx, sy) # Draw shadow. brush_shadow = QLinearGradient(xShadow, yShadow, w, yShadow) brush_shadow.setSpread(QLinearGradient.PadSpread) if Colors.useEightBitPalette: brush_shadow.setColorAt(0.0, QColor(0, 0, 0)) else: brush_shadow.setColorAt(0.0, QColor(0, 0, 0, 100)) pen_shadow = QPen() pen_shadow.setBrush(brush_shadow) painter.setPen(pen_shadow) painter.drawText(int(xShadow), int(yShadow), int(w), int(h), Qt.AlignLeft, self.text) # Draw text. brush_text = QLinearGradient(0, 0, w, w) brush_text.setSpread(QLinearGradient.PadSpread) brush_text.setColorAt(0.0, QColor(255, 255, 255)) brush_text.setColorAt(0.2, QColor(255, 255, 255)) brush_text.setColorAt(0.5, QColor(190, 190, 190)) pen_text = QPen() pen_text.setBrush(brush_text) painter.setPen(pen_text) painter.drawText(0, 0, int(w), int(h), Qt.AlignLeft, self.text) return image
def paintEvent(self,event): global monster_data global dmg painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.drawPixmap(event.rect(),self.pixmap) if self.card is not None and self.card.ID is not 0: card = self.card # Draw card level at the bottom centered pen = QPen() if np.floor(card.lv) == monster_data[card.ID]['max_level']: lvstr = 'Lv.Max' brush = QBrush(QColor(252,232,131)) else: lvstr = 'Lv.%d' % np.floor(card.lv) brush = QBrush(Qt.white) path = QPainterPath() pen.setWidth(0); pen.setBrush(Qt.black) font = QFont() font.setPointSize(11) font.setWeight(QFont.Black) path.addText(event.rect().x(),event.rect().y()+48,font,lvstr) rect = path.boundingRect() target = (event.rect().x()+event.rect().width())/2 # center the rect in event.rect() path.translate(target-rect.center().x(), 0) painter.setPen(pen) painter.setBrush(QBrush(Qt.black)) painter.drawPath(path.translated(.5,.5)) painter.setPen(pen) painter.setBrush(brush) painter.drawPath(path) # Draw +eggs at the top right eggs = card.plus_atk+card.plus_hp+card.plus_rcv if eggs > 0: eggstr = '+%d' % eggs pen.setBrush(Qt.yellow) brush = QBrush(Qt.yellow) path = QPainterPath() pen.setWidth(0) pen.setBrush(Qt.black) font = QFont() font.setPointSize(11) font.setWeight(QFont.Black) path.addText(event.rect().x(),event.rect().y()+12,font,eggstr) path.translate(50-path.boundingRect().right()-3,0) #painter.setFont(font) painter.setPen(pen) painter.setBrush(QBrush(Qt.black)) painter.drawPath(path.translated(.5,.5)) painter.setPen(pen) painter.setBrush(brush) painter.drawPath(path) #painter.drawText(event.rect().adjusted(0,0,0,0),Qt.AlignRight, eggstr) # Draw awakenings at the top left in a green circle if card.current_awakening > 0: path = QPainterPath() rect = QRectF(event.rect()).adjusted(4,4,-36,-36) path.addEllipse(rect) painter.setBrush(QBrush(QColor(34,139,34))) pen.setBrush(Qt.white) pen.setWidth(1) painter.setPen(pen) painter.drawPath(path) path = QPainterPath() font.setPointSize(9) awkstr = ('%d' % card.current_awakening if card.current_awakening < card.max_awakening else '★') path.addText(rect.x(),rect.bottom(),font,awkstr) br = path.boundingRect() path.translate(rect.center().x()-br.center().x(), rect.center().y()-br.center().y()) pen.setBrush(QColor(0,0,0,0)) pen.setWidth(0) painter.setPen(pen) painter.setBrush(QBrush(Qt.yellow)) painter.drawPath(path) # Draw main attack damage #print(self.main_attack) if self.main_attack > 0: matkstr = '%d' % self.main_attack painter.setBrush(QBrush(COLORS[self.card.element[0]])) path = QPainterPath() font = QFont() font.setFamily('Helvetica') font.setWeight(QFont.Black) #font.setStretch(25) font.setPointSize(13) path.addText(rect.x(),rect.bottom(),font,matkstr) rect = QRectF(event.rect()) br = path.boundingRect() path.translate(rect.center().x()-br.center().x(), rect.center().y()-br.bottom()-1) # pen.setBrush(Qt.black) pen.setWidthF(.75) painter.setPen(pen) painter.drawPath(path) # Draw sub attack damage #print(self.main_attack) if self.sub_attack > 0: satkstr = '%d' % self.sub_attack painter.setBrush(QBrush(COLORS[self.card.element[1]])) path = QPainterPath() font = QFont() font.setFamily('Helvetica') font.setWeight(QFont.Black) #font.setStretch(25) font.setPointSize(12) path.addText(rect.x(),rect.bottom(),font,satkstr) rect = QRectF(event.rect()) br = path.boundingRect() path.translate(rect.center().x()-br.center().x(), rect.center().y()-br.top()+1) # pen.setBrush(Qt.black) pen.setWidthF(.75) painter.setPen(pen) painter.drawPath(path)