def paint(self, canvas, is_secondary_color=False, additional_flag=False): pen = QPen() pen.setWidth(self.data_singleton.pen_size) pen.setStyle(Qt.SolidLine) pen.setCapStyle(Qt.RoundCap) pen.setJoinStyle(Qt.RoundJoin) if is_secondary_color: pen.setBrush(self.data_singleton.secondary_color) else: pen.setBrush(self.data_singleton.primary_color) painter = QPainter(canvas.image) painter.setPen(pen) if self._start_point != self._end_point: painter.drawLine(self._start_point, self._end_point) if self._start_point == self._end_point: painter.drawPoint(self._start_point) painter.end() canvas.edited = True canvas.update()
def paintEvent(self, pe): # make an arrow polygon right in the middle painter = QPainter(self) painter.setPen(Qt.NoPen) # draw the background transparent rect painter.save() painter.setOpacity(self.BACKGROUND_OPACITY) # get the rectangle coordinates it should extend over the whole width with only a portion at the center painter.setBrush(Qt.black) empty_space_percent = 1 - self.BACKROUND_HEIGHT_PERCENT rect_top = empty_space_percent / 2 * self.height() rect_height = self.BACKROUND_HEIGHT_PERCENT * self.height() painter.drawRect(0, rect_top, self.width(), rect_height) painter.restore() painter.setRenderHint(QPainter.Antialiasing) pen = QPen() pen.setWidth(self.ARROW_LINE_WIDTH) pen.setCapStyle(Qt.RoundCap) if self._mouse_inside: pen.setColor(self._hover_color) else: pen.setColor(self._normal_color) # get the arrow coords painter.setPen(pen) self_center = QPointF(self.width() / 2, self.height() / 2) # use this as the arrow tip for now if self._direction == self.LEFT: h_shift = self._arrow_width elif self._direction == self.RIGHT: h_shift = - self._arrow_width v_shift = self._arrow_height / 2 top_point = self_center + QPointF(h_shift, - v_shift) bottom_point = self_center + QPointF(h_shift, v_shift) painter.drawLine(top_point, self_center) painter.drawLine(self_center, bottom_point)
def __paintMargin(self, event): """ Paints the right margin as postPainting step. """ rect = event.rect() font = self.editor.codeEdit.currentCharFormat().font() fm = QFontMetricsF(font) pos = self.marginPos offset = self.editor.codeEdit.contentOffset().x() + \ self.editor.codeEdit.document().documentMargin() x80 = round(fm.width(' ') * pos) + offset p = QPainter(self.editor.codeEdit.viewport()) p.setPen(self.pen) p.drawLine(x80, rect.top(), x80, rect.bottom())
def paintEvent(self, event): """ Paints the widget """ if self.enabled is False: return Panel.paintEvent(self, event) painter = QPainter(self) painter.fillRect(event.rect(), self.back_brush) for vb in self.editor.codeEdit.visible_blocks: line = vb.row # paint marker for line marker = self.getIndicatorForLine(line) if marker is None: continue # use the normal pen to draw the fold indicator drawLines = False pen = self.normal_pen if marker.hover is True: pen = self.highlight_pen drawLines = True painter.setPen(pen) # get the text to draw txt = '-' if marker.folded is True: drawLines = False txt = '+' offset = 4 h = self.size_hint.height() fm = QFontMetricsF(self.editor.codeEdit.font()) hoffset = (fm.height() - h) / 2.0 r = QRect(vb.rect.x(), vb.rect.y() + hoffset, self.size_hint.width(), self.size_hint.height()) painter.setFont(self.font) painter.drawText(r, Qt.AlignVCenter | Qt.AlignHCenter, txt) w = self.size_hint.width() - 2 * offset h = self.size_hint.width() - 2 * offset hoffset = (fm.height() - h) / 2.0 r.setX(vb.rect.x() + offset) r.setY(vb.rect.y() + hoffset) r.setWidth(w) r.setHeight(h) painter.drawRect(r) if drawLines is True: top = (vb.rect.x() + self.size_hint.width() / 2.0, vb.rect.y() + hoffset + offset * 2) delta = ((marker.end - marker.start) * vb.height) # - (vb.rect.height() / 2.0) bottom = (top[0], top[1] + delta) painter.drawLine(top[0], top[1], bottom[0], bottom[1]) painter.drawLine(bottom[0], bottom[1], bottom[0] + self.size_hint.width() / 2.0, bottom[1]) return
def draw_uvs(self): self.img = QPixmap(QSize(self.w, self.h)) pen = QPen() pen.setWidth(int(self.pen_w.text())) pen.setBrush(QBrush(Qt.white)) pen.setColor(QColor('white')) painter = QPainter() painter.begin(self.img) painter.setPen(pen) coords = self.get_coords() for face in coords: for n in xrange(len(face) - 1): print face[n][0], face[n][1], face[n + 1][0], face[n + 1][1] painter.drawLine(face[n][0], face[n][1], face[n + 1][0], face[n + 1][1]) painter.end() self.view.scene().addPixmap(self.img)
def paintEvent(self, event): rect = self.rect() painter = QPainter(self) painter.setRenderHints(QPainter.Antialiasing) painter.setPen(QPen(Qt.darkGreen, 0, Qt.DotLine)) if self.orientation() == Qt.Horizontal: # Visually vertical offset = round(rect.height() * 0.05) x = rect.x() + (rect.width() / 2) y = rect.y() + offset painter.drawLine(x, y, x, rect.height() - offset) else: # Visually horizontal offset = round(rect.width() * 0.05) y = rect.y() + (rect.height() / 2) painter.drawLine(offset, y, rect.width() - offset, y)
def drawPreview(self, endPoint_x, endPoint_y): painter = QPainter(self.imagePreview) painter.setPen(QPen( self.myPenColor, self.myPenWidth, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin)) painter.setClipping(True) painter.setRenderHint(QPainter.SmoothPixmapTransform, True) painter.setRenderHint(QPainter.HighQualityAntialiasing, True) painter.setRenderHint(QPainter.Antialiasing, True) painter.setOpacity(0.5) if self.tools['circle']: x1 = self.firstPoint_x y1 = self.firstPoint_y x2 = endPoint_x y2 = endPoint_y painter.drawEllipse(x1, y1, (x2 - x1), (y2 - y1)) if self.tools['line']: painter.drawLine( self.firstPoint_x, self.firstPoint_y, endPoint_x, endPoint_y) if self.tools['rect']: painter.drawRect( self.firstPoint_x, self.firstPoint_y, endPoint_x - self.firstPoint_x, endPoint_y - self.firstPoint_y) if self.tools['roundRect']: x1 = self.firstPoint_x y1 = self.firstPoint_y dx = endPoint_x - self.firstPoint_x dy = endPoint_y - self.firstPoint_y if x1 > endPoint_x and y1 > endPoint_y: painter.drawRoundedRect( endPoint_x, endPoint_y, -dx, -dy, 20, 20, 0) else: painter.drawRoundedRect(x1, y1, dx, dy, 20., 20.) self.update()
def paintEvent(self, pe): # Those lines are from # http://stackoverflow.com/questions/18344135/why-do-stylesheets-not-work-when-subclassing-qwidget-and-using-q-object # the big idea is : According to the Qt help files, "Every widget displaying custom content must implement the paintEvent". o = QStyleOption() o.initFrom(self) p = QPainter(self) self.style().drawPrimitive(QStyle.PE_Widget, o, p, self) w = self.minimumWidth() pen = QPen() pen.setColor(QColor(128, 128, 192)) # pen.setWidth(1) p.setPen(pen) p.drawLine(0, 0, max(0, self.width() - 1), self.height() - 1)
def paintEvent(self, pe): if self.left_view and self.right_view and self.right_view.model(): vr = self.left_view.visualRect(self.left_view.currentIndex()) self.left_top = self.mapFromGlobal( self.left_view.viewport().mapToGlobal(vr.topRight())).y() self.left_bottom = self.mapFromGlobal( self.left_view.viewport().mapToGlobal(vr.bottomRight())).y() vr_top = self.right_view.visualRect(self.right_view.model().index( 0, 0)) vr = self.right_view.visualRect(self.right_view.model().index( self.right_view.model().rowCount() - 1, 0)) self.right_top = self.mapFromGlobal( self.left_view.viewport().mapToGlobal(vr_top.topLeft())).y() self.right_bottom = self.mapFromGlobal( self.left_view.viewport().mapToGlobal(vr.bottomLeft())).y() w = self.minimumWidth() - 1 p = QPainter(self) p.setBrush(QBrush(QColor(210, 255, 210))) pen = QPen() pen.setColor(Qt.transparent) p.setPen(pen) poly = QPolygon() poly.append(QPoint(0, self.left_top)) poly.append(QPoint(w, self.right_top)) poly.append(QPoint(w, self.right_bottom)) poly.append(QPoint(0, self.left_bottom)) p.drawConvexPolygon(poly) p.setRenderHint(QPainter.Antialiasing) pen.setColor(Qt.GlobalColor.black) pen.setWidth(2) p.setPen(pen) p.drawLine(0, self.left_top, w, self.right_top) p.drawLine(0, self.left_bottom, w, self.right_bottom)
def createPixmapIcon(self): self.markerIcon = QPixmap(MARKER_WIDTH, MARKER_HEIGHT) self.markerIcon.fill(Qt.transparent) painter = QPainter(self.markerIcon) p1 = QPoint(MARKER_WIDTH / 2, MARKER_HEIGHT - 1) p2 = QPoint(MARKER_WIDTH / 2, MARKER_HEIGHT - 1 - MARKER_PIN_LEN) pen = QPen(Qt.black) pen.setWidth(2) pen.setCosmetic(True) painter.setPen(pen) painter.drawLine(p1, p2) ellipse = QRect(0, 0, MARKER_WIDTH - 1, MARKER_HEIGHT - 1) pen.setWidth(1) painter.setPen(pen) color = QColor(Qt.green) color.setAlpha(127) brush = QBrush(color) painter.setBrush(brush) painter.drawEllipse(ellipse)
def paintEvent(self, pe): painter = QPainter(self) painter.save() background = QColor(55, 55, 55) brush = QBrush(background) painter.setOpacity(self._opacity) painter.setBrush(brush) painter.setPen(Qt.NoPen) painter.drawRect(self.rect()) painter.restore() # draw a bottom border painter.setPen(Qt.black) painter.drawLine(0, self.height(), self.width(), self.height()) # now the text pen = QPen(Qt.white) pen.setWidth(16) painter.setPen(pen) painter.setFont(QFont("Trebuchet MS", 16, QFont.Bold)) text_rect = QRect(self.rect()) text_rect.adjust(self._margin, self._margin, self._margin, self._margin) painter.drawText(text_rect, Qt.AlignLeft, self._message_str)
def paintEvent(self, ev): if not self._osx: return size = self.size() height = size.height()-1 width = size.width()-1 painter = QPainter(self) painter.setPen(QColor(165, 165, 165, 255)) painter.drawLine(QPoint(0, 0), QPoint(0, height)) painter.drawLine(QPoint(0, height), QPoint(width, height)) painter.drawLine(QPoint(width, height), QPoint(width, 0)) for index in range(self._buttonCount): xCoord = (index + 1) * 21 - 1 painter.drawLine(QPoint(xCoord, 0), QPoint(xCoord, height))
def paintEvent(self, ev): if not self._osx: return size = self.size() height = size.height() - 1 width = size.width() - 1 painter = QPainter(self) painter.setPen(QColor(165, 165, 165, 255)) painter.drawLine(QPoint(0, 0), QPoint(0, height)) painter.drawLine(QPoint(0, height), QPoint(width, height)) painter.drawLine(QPoint(width, height), QPoint(width, 0)) for index in range(self._buttonCount): xCoord = (index + 1) * 21 - 1 painter.drawLine(QPoint(xCoord, 0), QPoint(xCoord, height))
def paintEvent(self, event): painter = QPainter(self) width = self.width() height = self.height() pen = QPen() pen.setBrush(Qt.black) pen.setWidth(2) painter.setPen(pen) painter.setRenderHint(painter.Antialiasing) painter.drawEllipse(0, 0, width, height) painter.drawLine(width - 20, height // 2, width - 1, height // 2) painter.drawLine(0, height // 2, 20, height // 2) painter.drawLine(width // 2, 0, width // 2, 20) painter.drawLine(width // 2, height - 20, width // 2, height - 1) painter.drawText(0, 20, width, height - 20, Qt.AlignHCenter | Qt.AlignTop, "N") painter.drawText(0, 0, width, height - 20, Qt.AlignHCenter | Qt.AlignBottom, "S") painter.drawText(25, 0, width - 25, height, Qt.AlignVCenter | Qt.AlignLeft, "W") painter.drawText(0, 0, width - 25, height, Qt.AlignVCenter | Qt.AlignRight, "E") pen.setWidth(4) pen.setBrush(Qt.blue) painter.setPen(pen) x = (math.cos(-self.heading) * width // 2) + width // 2 y = (math.sin(-self.heading) * height // 2) + height // 2 painter.drawLine(width // 2, height // 2, x, y)
def draw(self, endPoint_x, endPoint_y): painter = QPainter(self.image) painter.setPen(QPen( self.myPenColor, self.myPenWidth, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin)) painter.setClipping(True) painter.setRenderHint(QPainter.SmoothPixmapTransform, True) painter.setRenderHint(QPainter.HighQualityAntialiasing, True) painter.setRenderHint(QPainter.Antialiasing, True) painter.setCompositionMode(QPainter.CompositionMode()) if self.tools['circle']: x1 = self.firstPoint_x y1 = self.firstPoint_y x2 = endPoint_x y2 = endPoint_y painter.drawEllipse(x1, y1, (x2 - x1), (y2 - y1)) if self.tools['eraser']: painter.setPen(QPen(QtCore.Qt.white, 10, QtCore.Qt.SolidLine)) painter.drawLine( self.firstPoint_x, self.firstPoint_y, endPoint_x, endPoint_y) self.firstPoint_x = endPoint_x self.firstPoint_y = endPoint_y if self.tools['pen']: painter.drawLine( self.firstPoint_x, self.firstPoint_y, endPoint_x, endPoint_y) self.firstPoint_x = endPoint_x self.firstPoint_y = endPoint_y if self.tools['line'] and self.flag: painter.drawLine( self.firstPoint_x, self.firstPoint_y, endPoint_x, endPoint_y) if self.tools['rect']: dx = endPoint_x - self.firstPoint_x dy = endPoint_y - self.firstPoint_y painter.drawRect(self.firstPoint_x, self.firstPoint_y, dx, dy) if self.tools['roundRect']: x1 = self.firstPoint_x y1 = self.firstPoint_y dx = endPoint_x - self.firstPoint_x dy = endPoint_y - self.firstPoint_y if x1 > endPoint_x and y1 > endPoint_y: painter.drawRoundedRect( endPoint_x, endPoint_y, -dx, -dy, 20, 20, 0) else: painter.drawRoundedRect(x1, y1, dx, dy, 20., 20.) self.modified = True self.update()
def paintEvent(self, event): painter = QPainter() painter.begin(self) painter.setRenderHint(QPainter.HighQualityAntialiasing) painter.setRenderHint(QPainter.Antialiasing) painter.setRenderHint(QPainter.SmoothPixmapTransform) self.img = QPixmap() if self.isChecked(): painter.setBrush(self.color1) painter.drawRoundedRect(QRect(-1, -1, 31, 31), 7, 7) painter.drawPixmap(QRect(-2, -5, 35, 40), self.img.fromImage(self.check)) else: pen = QPen() pen.setWidth(2) painter.setPen(pen) # hand draw rect painter.drawLine(QPoint(0, 0), QPoint(30, 0)) painter.drawLine(QPoint(30, 0), QPoint(30, 30)) painter.drawLine(QPoint(0, 0), QPoint(0, 30)) painter.drawLine(QPoint(0, 30), QPoint(30, 30)) painter.end()
def paintEvent( self, event ): painter = QPainter() painter.begin(self) painter.setRenderHint(painter.Antialiasing) font = painter.font() font.setBold(True) painter.setFont(font) x = self.rect().x() y = self.rect().y() w = self.rect().width() - 1 h = self.rect().height() - 1 r = 8 # draw a rounded style if self._rolloutStyle == 2: # draw the text painter.drawText(x + 33, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title()) # draw the triangle self.__drawTriangle(painter, x, y) # draw the borders pen = QPen(self.palette().color(QPalette.Light)) pen.setWidthF(0.6) painter.setPen(pen) painter.drawRoundedRect(x + 1, y + 1, w - 1, h - 1, r, r) pen.setColor(self.palette().color(QPalette.Shadow)) painter.setPen(pen) painter.drawRoundedRect(x, y, w - 1, h - 1, r, r) # draw a square style if self._rolloutStyle == 3: # draw the text painter.drawText(x + 33, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title()) self.__drawTriangle(painter, x, y) # draw the borders pen = QPen(self.palette().color(QPalette.Light)) pen.setWidthF(0.6) painter.setPen(pen) painter.drawRect(x + 1, y + 1, w - 1, h - 1) pen.setColor(self.palette().color(QPalette.Shadow)) painter.setPen(pen) painter.drawRect(x, y, w - 1, h - 1) # draw a Maya style if self._rolloutStyle == 4: # draw the text painter.drawText(x + 33, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title()) painter.setRenderHint(QPainter.Antialiasing, False) self.__drawTriangle(painter, x, y) # draw the borders - top headerHeight = 20 headerRect = QRect(x + 1, y + 1, w - 1, headerHeight) headerRectShadow = QRect(x - 1, y - 1, w + 1, headerHeight + 2) # Highlight pen = QPen(self.palette().color(QPalette.Light)) pen.setWidthF(0.4) painter.setPen(pen) painter.drawRect(headerRect) painter.fillRect(headerRect, QColor(255, 255, 255, 18)) # Shadow pen.setColor(self.palette().color(QPalette.Dark)) painter.setPen(pen) painter.drawRect(headerRectShadow) if not self.isCollapsed(): # draw the lover border pen = QPen(self.palette().color(QPalette.Dark)) pen.setWidthF(0.8) painter.setPen(pen) offSet = headerHeight + 3 bodyRect = QRect(x, y + offSet, w, h - offSet) bodyRectShadow = QRect(x + 1, y + offSet, w + 1, h - offSet + 1) painter.drawRect(bodyRect) pen.setColor(self.palette().color(QPalette.Light)) pen.setWidthF(0.4) painter.setPen(pen) painter.drawRect(bodyRectShadow) # draw a boxed style elif self._rolloutStyle == 1: if self.isCollapsed(): arect = QRect(x + 1, y + 9, w - 1, 4) brect = QRect(x, y + 8, w - 1, 4) text = '+' else: arect = QRect(x + 1, y + 9, w - 1, h - 9) brect = QRect(x, y + 8, w - 1, h - 9) text = '-' # draw the borders pen = QPen(self.palette().color(QPalette.Light)) pen.setWidthF(0.6) painter.setPen(pen) painter.drawRect(arect) pen.setColor(self.palette().color(QPalette.Shadow)) painter.setPen(pen) painter.drawRect(brect) painter.setRenderHint(painter.Antialiasing, False) painter.setBrush(self.palette().color(QPalette.Window).darker(120)) painter.drawRect(x + 10, y + 1, w - 20, 16) painter.drawText(x + 16, y + 1, w - 32, 16, Qt.AlignLeft | Qt.AlignVCenter, text) painter.drawText(x + 10, y + 1, w - 20, 16, Qt.AlignCenter, self.title()) if self.dragDropMode(): rect = self.dragDropRect() # draw the lines l = rect.left() r = rect.right() cy = rect.center().y() for y in (cy - 3, cy, cy + 3): painter.drawLine(l, y, r, y) painter.end()
def paintEvent(self, event): super().paintEvent(event) painter = QPainter(self) # Рисование цифр в ячейки таблицы for i in range(self.matrix_size): for j in range(self.matrix_size): # Если текущая ячейка относится к дефолтной судоку if self.def_num_matrix[i][j]: painter.save() # painter.setPen() painter.setBrush(Qt.yellow) x = i * self.cell_size y = j * self.cell_size w, h = self.cell_size, self.cell_size painter.drawRect(x, y, w, h) painter.restore() # TODO: Закомментировано # Если индекс ячейки под курсором валидный if 0 <= self.x_highlight_cell < self.matrix_size and 0 <= self.y_highlight_cell < self.matrix_size: # # Выделение всего столбца и строки пересекающих ячейку под курсором # painter.save() # painter.setBrush(Qt.lightGray) # # # Выделение строки # for i in range(self.matrix_size): # painter.drawRect(i * self.cell_size, # self.y_highlight_cell * self.cell_size, # self.cell_size, # self.cell_size) # # # Выделение столбца # for j in range(self.matrix_size): # painter.drawRect(self.x_highlight_cell * self.cell_size, # j * self.cell_size, # self.cell_size, # self.cell_size) # # painter.restore() x, y = self.x_highlight_cell, self.y_highlight_cell # Не подсвечиваем дефолтную ячейку if not self.def_num_matrix[x][y]: # Выделение ячейки под курсором painter.save() painter.setBrush(Qt.darkYellow) painter.drawRect(x * self.cell_size, y * self.cell_size, self.cell_size, self.cell_size) painter.restore() # Рисование цифр в ячейки таблицы for i in range(self.matrix_size): for j in range(self.matrix_size): num = self.matrix[i][j] if not num: continue num = str(num) # Алгоритм изменения размера текста взят из http://stackoverflow.com/a/2204501 # Для текущего пришлось немного адаптировать factor = (self.cell_size / 2) / painter.fontMetrics().width(num) if factor < 1 or factor > 1.25: f = painter.font() point_size = f.pointSizeF() * factor if point_size > 0: f.setPointSizeF(point_size) painter.setFont(f) x = i * self.cell_size y = j * self.cell_size w, h = self.cell_size, self.cell_size painter.drawText(x, y, w, h, Qt.AlignCenter, num) # Рисование сетки таблицы y1, y2 = 0, 0 factor = min(self.width(), self.height()) / self.default_size size = self.default_pen_size_1 size2 = self.default_pen_size_2 if factor < 1 or factor > 1.25: size *= factor if size < self.min_default_pen_size_2: size = self.min_default_pen_size_2 painter.save() for i in range(self.matrix_size + 1): painter.setPen(QPen(Qt.black, size2 if i % 3 == 0 and i and i < self.matrix_size else size)) painter.drawLine(0, y1, self.cell_size * self.matrix_size, y2) y1 += self.cell_size y2 += self.cell_size x1, x2 = 0, 0 for i in range(self.matrix_size + 1): painter.setPen(QPen(Qt.black, size2 if i % 3 == 0 and i and i < self.matrix_size else size)) painter.drawLine(x1, 0, x2, self.cell_size * self.matrix_size) x1 += self.cell_size x2 += self.cell_size painter.restore()