def paintEvent(self, event: QPaintEvent): outerRadius = min(self.width(), self.height()) baseRect = QRectF(1, 1, outerRadius - 2, outerRadius - 2) buffer = QImage(outerRadius, outerRadius, QImage.Format_ARGB32_Premultiplied) p = QPainter(buffer) p.setRenderHint(QPainter.Antialiasing) self.rebuildDataBrushIfNeeded() self.drawBackground(p, buffer.rect()) self.drawBase(p, baseRect) if self.m_value > 0: delta = (self.m_max - self.m_min) / (self.m_value - self.m_min) else: delta = 0 self.drawValue(p, baseRect, self.m_value, delta) innerRect, innerRadius = self.calculateInnerRect(outerRadius) self.drawInnerBackground(p, innerRect) self.drawText(p, innerRect, innerRadius, self.m_value) p.end() painter = QPainter(self) painter.fillRect(baseRect, self.palette().window()) painter.drawImage(0, 0, buffer)
def create_blue_green_rect(): pixmap = QPixmap(4, 2) painter = QPainter(pixmap) try: painter.fillRect(0, 0, 2, 2, QColor('blue')) painter.fillRect(2, 0, 2, 2, QColor('green')) finally: painter.end() return pixmap
def assert_equal(self): __tracebackhide__ = True self.end() self.different_pixels = 0 actual_image: QImage = self.actual.device().toImage() expected_image: QImage = self.expected.device().toImage() diff_pixmap = QPixmap(actual_image.width(), actual_image.height()) diff = QPainter(diff_pixmap) try: white = QColor('white') diff.fillRect(0, 0, actual_image.width(), actual_image.height(), white) for x in range(actual_image.width()): for y in range(actual_image.height()): actual_colour = actual_image.pixelColor(x, y) expected_colour = expected_image.pixelColor(x, y) diff.setPen( self.diff_colour(actual_colour, expected_colour, x, y)) diff.drawPoint(x, y) finally: diff.end() diff_image: QImage = diff.device().toImage() display_diff(actual_image, diff_image, expected_image, self.different_pixels) if self.different_pixels == 0: return actual_image.save(str(self.work_dir / (self.name + '_actual.png'))) expected_image.save(str(self.work_dir / (self.name + '_expected.png'))) diff_path = self.work_dir / (self.name + '_diff.png') is_saved = diff_image.save(str(diff_path)) diff_width = self.diff_max_x - self.diff_min_x + 1 diff_height = self.diff_max_y - self.diff_min_y + 1 diff_section = QImage(diff_width, diff_height, QImage.Format_RGB32) diff_section_painter = QPainter(diff_section) try: diff_section_painter.drawPixmap(0, 0, diff_width, diff_height, QPixmap.fromImage(diff_image), self.diff_min_x, self.diff_min_y, diff_width, diff_height) finally: diff_section_painter.end() # To see an image dumped in the Travis CI log, copy the text from the # log, and paste it in test_pixmap_differ.test_decode_image. print(f'Encoded image of differing section ' f'({self.diff_min_x}, {self.diff_min_y}) - ' f'({self.diff_max_x}, {self.diff_max_y}):') print(encode_image(diff_section)) message = f'Found {self.different_pixels} different pixels, ' message += f'see' if is_saved else 'could not write' message += f' {diff_path.relative_to(Path(__file__).parent.parent)}.' assert self.different_pixels == 0, message
def assert_equal(self): __tracebackhide__ = True self.end() self.different_pixels = 0 actual_image: QImage = self.actual.device().toImage() expected_image: QImage = self.expected.device().toImage() diff_pixmap = QPixmap(actual_image.width(), actual_image.height()) diff = QPainter(diff_pixmap) try: white = QColor('white') diff.fillRect(0, 0, actual_image.width(), actual_image.height(), white) for x in range(actual_image.width()): for y in range(actual_image.height()): actual_colour = actual_image.pixelColor(x, y) expected_colour = expected_image.pixelColor(x, y) diff.setPen( self.diff_colour(actual_colour, expected_colour, x, y)) diff.drawPoint(x, y) finally: diff.end() diff_image: QImage = diff.device().toImage() display_diff(actual_image, diff_image, expected_image, self.different_pixels) if self.different_pixels == 0: return actual_image.save(str(self.work_dir / (self.name + '_actual.png'))) expected_image.save(str(self.work_dir / (self.name + '_expected.png'))) diff_path = self.work_dir / (self.name + '_diff.png') is_saved = diff_image.save(str(diff_path)) diff_width = self.diff_max_x - self.diff_min_x + 1 diff_height = self.diff_max_y - self.diff_min_y + 1 diff_section = QImage(diff_width, diff_height, QImage.Format_RGB32) diff_section_painter = QPainter(diff_section) try: diff_section_painter.drawPixmap(0, 0, diff_width, diff_height, QPixmap.fromImage(diff_image), self.diff_min_x, self.diff_min_y, diff_width, diff_height) finally: diff_section_painter.end() message = f'Found {self.different_pixels} different pixels.' assert self.different_pixels == 0, message
def lineNumberAreaPaintEvent(self, event): painter = QPainter(self.line_number_area) painter.fillRect(event.rect(), Qt.lightGray) block = self.firstVisibleBlock() block_number = block.blockNumber() offset = self.contentOffset() top = self.blockBoundingGeometry(block).translated(offset).top() bottom = top + self.blockBoundingRect(block).height() while block.isValid() and top <= event.rect().bottom(): if block.isVisible() and bottom >= event.rect().top(): number = str(block_number + 1) painter.setPen(Qt.black) width = self.line_number_area.width() height = self.fontMetrics().height() painter.drawText(0, top, width, height, Qt.AlignRight, number) block = block.next() top = bottom bottom = top + self.blockBoundingRect(block).height() block_number += 1
def paintEvent(self, event): painter = QPainter(self) painter.fillRect(self.rect(), Qt.black) if self.pixmap.isNull(): painter.setPen(Qt.white) painter.drawText(self.rect(), Qt.AlignCenter, "Rendering initial image, please wait...") return if self.curScale == self.pixmapScale: painter.drawPixmap(self.pixmapOffset, self.pixmap) else: scaleFactor = self.pixmapScale / self.curScale newWidth = int(self.pixmap.width() * scaleFactor) newHeight = int(self.pixmap.height() * scaleFactor) newX = self.pixmapOffset.x() + (self.pixmap.width() - newWidth) / 2 newY = self.pixmapOffset.y() + (self.pixmap.height() - newHeight) / 2 painter.save() painter.translate(newX, newY) painter.scale(scaleFactor, scaleFactor) exposed, _ = painter.transform().inverted() exposed = exposed.mapRect(self.rect()).adjusted(-1, -1, 1, 1) painter.drawPixmap(exposed, self.pixmap, exposed) painter.restore() text = "Use mouse wheel or the '+' and '-' keys to zoom. Press and " \ "hold left mouse button to scroll." metrics = painter.fontMetrics() textWidth = metrics.horizontalAdvance(text) painter.setPen(Qt.NoPen) painter.setBrush(QColor(0, 0, 0, 127)) painter.drawRect((self.width() - textWidth) / 2 - 5, 0, textWidth + 10, metrics.lineSpacing() + 5) painter.setPen(Qt.white) painter.drawText((self.width() - textWidth) / 2, metrics.leading() + metrics.ascent(), text)
def setIcons(self) -> None: settings = QSettings() colored = str(settings.value('iconColors', 'True')) == 'True' self._icons: Dict[str, QIcon] = {} pixmap = QPixmap(str(getRuntimePath('resources/icons/dia.ico'))) painter = QPainter(pixmap) painter.setCompositionMode(QPainter.CompositionMode_SourceIn) painter.fillRect(pixmap.rect(), QColor('#427aa1') if colored else QColor('#333333')) painter.end() self._icons['mod'] = QIcon(pixmap) pixmap = QPixmap(str(getRuntimePath('resources/icons/puzzle.ico'))) painter = QPainter(pixmap) painter.setCompositionMode(QPainter.CompositionMode_SourceIn) painter.fillRect(pixmap.rect(), QColor('#aad576') if colored else QColor('#333333')) painter.end() self._icons['dlc'] = QIcon(pixmap) pixmap = QPixmap(str(getRuntimePath('resources/icons/folder.ico'))) painter = QPainter(pixmap) painter.setCompositionMode(QPainter.CompositionMode_SourceIn) painter.fillRect(pixmap.rect(), QColor('#E55934') if colored else QColor('#333333')) painter.end() self._icons['bin'] = QIcon(pixmap) pixmap = QPixmap(str(getRuntimePath('resources/icons/patch.ico'))) painter = QPainter(pixmap) painter.setCompositionMode(QPainter.CompositionMode_SourceIn) painter.fillRect(pixmap.rect(), QColor('#b08968') if colored else QColor('#333333')) painter.end() self._icons['pat'] = QIcon(pixmap) pixmap = QPixmap(str(getRuntimePath('resources/icons/question.ico'))) painter = QPainter(pixmap) painter.setCompositionMode(QPainter.CompositionMode_SourceIn) painter.fillRect(pixmap.rect(), QColor('#ffcf40') if colored else QColor('#333333')) painter.end() self._icons['udf'] = QIcon(pixmap)
def DrawWidget(self, Painter: QtGui.QPainter, Option, Index): state = Option.state if state & self._style.State_Enabled and state & self._style.State_Selected: # Selected color = QtGui.QColor(self._theme.get("ui-02")) border = QtGui.QColor(self._theme.get("ui-01")) elif state & self._style.State_Enabled and state & self._style.State_Active: # Normal color = QtGui.QColor(self._theme.get("ui-01")) border = QtGui.QColor(self._theme.get("ui-02")) elif state & self._style.State_Enabled: # Inactive color = QtGui.QColor(self._theme.get("ui-01")) border = QtGui.QColor(self._theme.get("ui-02")) else: # Disabled color = QtGui.QColor(self._theme.get("disabled-02")) border = QtGui.QColor(self._theme.get("disabled-02")) Painter.setPen(color) self._style.drawPrimitive(self._style.PE_PanelItemViewItem, Option, Painter, Option.widget) Painter.fillRect(Option.rect, (color)) Painter.setPen(QtGui.QColor(self._theme.get("text-02"))) items = self.getData(Index) self.DrawCover(Painter, Option) self.DrawTop(Painter, Option, items[0]) self.DrawMid(Painter, Option, items[1]) self.DrawBottom(Painter, Option, [items[2], items[3], items[4]]) Painter.setPen(border) Painter.drawLine(QPoint(0, Option.rect.y() + 63), QPoint(Option.rect.width(), Option.rect.y() + 63))
def paintEvent(self, event): painter = QPainter(self) painter.fillRect(event.rect(), self.numberBarColor) block = self.editor.firstVisibleBlock() # Iterate over all visible text blocks in the document. while block.isValid(): blockNumber = block.blockNumber() block_top = self.editor.blockBoundingGeometry( block).translated(self.editor.contentOffset()).top() # Check if the position of the block is out side of the visible area. if not block.isVisible() or block_top >= event.rect().bottom(): break # We want the line number for the selected line to be bold. if blockNumber == self.editor.textCursor().blockNumber(): self.font.setBold(True) painter.setPen(bnstyles["blockSelected"]) else: self.font.setBold(False) painter.setPen(bnstyles["blockNormal"]) painter.setFont(self.font) # Draw the line number right justified at the position of the line. paint_rect = QRect(0, block_top, self.width(), self.editor.fontMetrics().height()) painter.drawText(paint_rect, Qt.AlignLeft, str(blockNumber + 1)) block = block.next() painter.end() QWidget.paintEvent(self, event)
def drawrect(self, row, col): painter = QPainter(self._image) painter.fillRect((col + self.border) * self.box_size, (row + self.border) * self.box_size, self.box_size, self.box_size, Qt.black)
def paintEvent(self, event): paint = QPainter() paint.begin(self) paint.save() size = self.size() width = size.width() height = size.height() units = self.data.get('units') proportion = self.getProportion() horizontalOrientation = self.data.get('horizontal_orientation') if horizontalOrientation: rulerLength = width traceLengthLimit = height else: # vertical orientation rulerLength = height traceLengthLimit = width paint.translate(width, 0) paint.rotate(90) # the length of the traces (lines) small = traceLengthLimit / 6 medium = traceLengthLimit / 4 large = traceLengthLimit / 3 limit = rulerLength / proportion # draw less lines for centimeters if units == 'cm': step = 10 else: step = 5 # begin drawing fontSize = 10 font = QFont('Serif', fontSize) fontMetrics = QFontMetrics(font) # draw background background = self.data.get('background_color') paint.fillRect(0, 0, rulerLength, traceLengthLimit, background) # draw the lines paint.setPen(self.data.get('lines_color')) paint.setFont(font) # the builtin range() doesn't support floats def float_range(current, end, rangeStep): while current < end: yield current current += rangeStep # we skip 0 and start in the first step, since there's no point in drawing the first line/text (it would appear cut off, since we're at the limit) for a in float_range(step, limit, step): position = a * proportion if (a % 100) == 0: lineLength = large if units == 'px': text = '{}{}'.format(str(a), units) else: text = '{}{}'.format(str(int(a / 100)), units) textWidth = fontMetrics.boundingRect(text).width() paint.drawText(position - textWidth / 2, traceLengthLimit / 2 + fontSize / 2, text) elif (a % 50) == 0: lineLength = large # since 'cm' has a different step compared to the other units if units == 'cm': lineLength = medium elif (a % 25) == 0: lineLength = medium else: lineLength = small paint.drawLine(position, 0, position, lineLength) paint.drawLine(position, traceLengthLimit, position, traceLengthLimit - lineLength) # paint the division lines if self.data.get('division_lines'): paint.setPen(self.data.get('divisions_color')) halfPoint = rulerLength / 2 quarterPoint = rulerLength / 4 threeQuarterPoint = 3 / 4 * rulerLength paint.drawLine(quarterPoint, 0, quarterPoint, traceLengthLimit) paint.drawLine(halfPoint, 0, halfPoint, traceLengthLimit) paint.drawLine(threeQuarterPoint, 0, threeQuarterPoint, traceLengthLimit) paint.restore() paint.end()
def drawBackground(self, p: QPainter, baseRect: QRectF): p.fillRect(baseRect, self.palette().window())