def paintEvent(self, ev): color = self.palette().color(QPalette.Highlight) painter = QPainter(self) # Filled rectangle. painter.setClipRect(self.rect()) color.setAlpha(50) painter.fillRect(self.rect().adjusted(2,2,-2,-2), color) # Thin rectangle outside. color.setAlpha(150) painter.setPen(color) painter.drawRect(self.rect().adjusted(0,0,-1,-1)) # Pseudo-handles at the corners and sides color.setAlpha(100) pen = QPen(color) pen.setWidth(8) painter.setPen(pen) painter.setBackgroundMode(Qt.OpaqueMode) # Clip at 4 corners region = QRegion(QRect(0,0,20,20)) region += QRect(self.rect().width()-20, 0, 20, 20) region += QRect(self.rect().width()-20, self.rect().height()-20, 20, 20) region += QRect(0, self.rect().height()-20, 20, 20) # Clip middles region += QRect(0, self.rect().height()/2-10, self.rect().width(), 20) region += QRect(self.rect().width()/2-10, 0, 20, self.rect().height()) # Draw thicker rectangles, clipped at corners and sides. painter.setClipRegion(region) painter.drawRect(self.rect())
def _render_qwebpage_full(web_page, logger, viewport_size, image_size): image = QImage(image_size, QImage.Format_ARGB32) image.fill(0) painter = QPainter(image) try: painter.setRenderHint(QPainter.Antialiasing, True) painter.setRenderHint(QPainter.TextAntialiasing, True) painter.setRenderHint(QPainter.SmoothPixmapTransform, True) painter.setWindow(QRect(QPoint(0, 0), web_page.viewportSize())) painter.setViewport(QRect(QPoint(0, 0), viewport_size)) if image_size != viewport_size: # Try not to draw stuff that won't fit into the image. Clipping # must be specified in input (aka logical) coordinates, but we know # it in output (aka physical) coordinates, so we have to do an # inverse transformation. If, for some reason, we cannot, skip the # clipping altogether. clip_rect = QRect(QPoint(0, 0), viewport_size) inv_transform, invertible = painter.combinedTransform().inverted() if invertible: painter.setClipRect(inv_transform.mapRect(clip_rect)) web_page.mainFrame().render(painter) finally: # It is important to end painter explicitly in python code, because # Python finalizer invocation order, unlike C++ destructors, is not # deterministic and there is a possibility of image's finalizer running # before painter's which may break tests and kill your cat. painter.end() return qimage_to_pil_image(image)
def _render_qwebpage_full(self, web_rect, render_rect, canvas_size): """Render web page in one step.""" if self._qpainter_needs_tiling(render_rect, canvas_size): # If this condition is true, this function may get stuck. raise ValueError("Rendering region is too large to be drawn" " in one step, use tile-by-tile renderer instead") canvas = QImage(canvas_size, self.qt_image_format) if self.is_jpeg(): # White background for JPEG images, same as we have in all browsers. canvas.fill(Qt.white) else: # Preserve old behaviour for PNG format. canvas.fill(0) painter = QPainter(canvas) try: painter.setRenderHint(QPainter.Antialiasing, True) painter.setRenderHint(QPainter.TextAntialiasing, True) painter.setRenderHint(QPainter.SmoothPixmapTransform, True) painter.setWindow(web_rect) painter.setViewport(render_rect) painter.setClipRect(web_rect) self.web_page.mainFrame().render(painter) finally: painter.end() return WrappedQImage(canvas)
def _plot(self, width, height, paint_device, plot_object = None, plot_method = None): if not plot_object: plot_object = self.controller if not plot_method: plot_method = plot_object.plot plot_object.setPlotWindow(width, height) canvas = DiPaintGLCanvas(paint_device) glpainter = DiPaintGLPainter(canvas) painter = QPainter() painter.begin(canvas.device()) glpainter.begin(painter) painter.setRenderHints(self.renderHints) painter.setClipRect(QRect(0, 0, width, height)) glpainter.Viewport(0, 0, width, height) value = plot_method(glpainter) transform = glpainter.transform glpainter.end() painter.end() return paint_device, value, transform
def paintEvent(self, event): """ Paint the interpolated pixmap image. """ p = QPainter(self) p.setClipRect(event.rect()) factor = self.blendingFactor_ ** 2 if self.pixmap1 and 1. - factor: p.setOpacity(1. - factor) p.drawPixmap(QPoint(0, 0), self.pixmap1) if self.pixmap2 and factor: p.setOpacity(factor) p.drawPixmap(QPoint(0, 0), self.pixmap2)
def paintEvent(self, ev): rect = ev.rect() s = self._square rows = range(rect.top() // s, rect.bottom() // s + 1) cols = range(rect.left() // s, rect.right() // s + 1) painter = QPainter(self) painter.setPen(QPen(self.palette().color(QPalette.Window))) painter.setFont(self._font) metrics = QFontMetrics(self._font) # draw characters on white tiles tile = self.palette().color(QPalette.Base) selected_tile = self.palette().color(QPalette.Highlight) selected_tile.setAlpha(96) selected_box = self.palette().color(QPalette.Highlight) text_pen = QPen(self.palette().text()) disabled_pen = QPen(self.palette().color(QPalette.Disabled, QPalette.Text)) selection_pen = QPen(selected_box) for row in rows: for col in cols: char = row * self._column_count + col + self._range[0] if char > self._range[1]: break printable = self.isprint(char) painter.setClipRect(col * s, row * s, s, s) if char == self._selected: painter.fillRect(col * s + 1, row * s + 1, s - 2, s - 2, selected_tile) painter.setPen(selection_pen) painter.drawRect(col * s, row * s, s - 1, s - 1) elif printable: painter.fillRect(col * s + 1, row * s + 1, s - 2, s - 2, tile) painter.setPen(text_pen if printable else disabled_pen) t = chr(char) x = col * s + s // 2 - metrics.width(t) // 2 y = row * s + 4 + metrics.ascent() painter.drawText(x, y, t) else: continue break
def _render_qwebpage_full(web_page, logger, web_rect, render_rect, canvas_size): """Render web page in one step.""" if _qpainter_needs_tiling(render_rect, canvas_size): # If this condition is true, this function may get stuck. raise ValueError("Rendering region is too large to be drawn" " in one step, use tile-by-tile renderer instead") canvas = QImage(canvas_size, QImage.Format_ARGB32) canvas.fill(0) painter = QPainter(canvas) try: painter.setRenderHint(QPainter.Antialiasing, True) painter.setRenderHint(QPainter.TextAntialiasing, True) painter.setRenderHint(QPainter.SmoothPixmapTransform, True) painter.setWindow(web_rect) painter.setViewport(render_rect) painter.setClipRect(web_rect) web_page.mainFrame().render(painter) finally: painter.end() return WrappedQImage(canvas)
def paintEvent(self, ev): color = self.palette().color(QPalette.Highlight) painter = QPainter(self) # Filled rectangle. painter.setClipRect(self.rect()) color.setAlpha(50) painter.fillRect(self.rect().adjusted(2, 2, -2, -2), color) # Thin rectangle outside. color.setAlpha(150) painter.setPen(color) painter.drawRect(self.rect().adjusted(0, 0, -1, -1)) # Pseudo-handles at the corners and sides color.setAlpha(100) pen = QPen(color) pen.setWidth(8) painter.setPen(pen) painter.setBackgroundMode(Qt.OpaqueMode) # Clip at 4 corners region = QRegion(QRect(0, 0, 20, 20)) region += QRect(self.rect().width() - 20, 0, 20, 20) region += QRect(self.rect().width() - 20, self.rect().height() - 20, 20, 20) region += QRect(0, self.rect().height() - 20, 20, 20) # Clip middles region += QRect(0, self.rect().height() / 2 - 10, self.rect().width(), 20) region += QRect(self.rect().width() / 2 - 10, 0, 20, self.rect().height()) # Draw thicker rectangles, clipped at corners and sides. painter.setClipRegion(region) painter.drawRect(self.rect())