def paintEvent(self, event): """Qt method override to paint a custom image on the Widget.""" super(FigureCanvas, self).paintEvent(event) # Prepare the rect on which the image is going to be painted : fw = self.frameWidth() rect = QRect(0 + fw, 0 + fw, self.size().width() - 2 * fw, self.size().height() - 2 * fw) if self.fig is None or self._blink_flag: return # Check/update the qpixmap buffer : qpix2paint = None for qpix in self._qpix_buffer: if qpix.size().width() == rect.width(): qpix2paint = qpix break else: if self.fmt in ['image/png', 'image/jpeg']: qpix2paint = self._qpix_orig.scaledToWidth( rect.width(), mode=Qt.SmoothTransformation) elif self.fmt == 'image/svg+xml': qpix2paint = QPixmap(svg_to_image(self.fig, rect.size())) self._qpix_buffer.append(qpix2paint) if qpix2paint is not None: # Paint the image on the widget : qp = QPainter() qp.begin(self) qp.drawPixmap(rect, qpix2paint) qp.end()
def paintEvent(self, event): """Qt method override to paint a custom image on the Widget.""" super(FigureCanvas, self).paintEvent(event) # Prepare the rect on which the image is going to be painted. fw = self.frameWidth() rect = QRect(0 + fw, 0 + fw, self.size().width() - 2 * fw, self.size().height() - 2 * fw) if self.fig is None or self._blink_flag: return # Prepare the scaled qpixmap to paint on the widget. if (self._qpix_scaled is None or self._qpix_scaled.size().width() != rect.width()): if self.fmt in ['image/png', 'image/jpeg']: self._qpix_scaled = self._qpix_orig.scaledToWidth( rect.width(), mode=Qt.SmoothTransformation) elif self.fmt == 'image/svg+xml': self._qpix_scaled = QPixmap(svg_to_image( self.fig, rect.size())) if self._qpix_scaled is not None: # Paint the image on the widget. qp = QPainter() qp.begin(self) qp.drawPixmap(rect, self._qpix_scaled) qp.end()
def render(self): rect = QRect(QPoint(), self.geometry().size()) self.m_backingStore.resize(rect.size()) self.m_backingStore.beginPaint(QRegion(rect)) device = self.m_backingStore.paintDevice() p = QPainter(device) p.drawImage(0, 0, self.m_image) font = QFont() font.setPixelSize(32) p.setFont(font) p.drawText(rect, 0, self.m_text) self.m_backingStore.endPaint() self.m_backingStore.flush(QRegion(rect))
def paintEvent(self, event): """Override Qt method.""" painter = QPainter(self) painter.setOpacity(self.current_opacity) max_width = ( SASS_VARIABLES.WIDGET_CONTENT_TOTAL_WIDTH - 2 * SASS_VARIABLES.WIDGET_CONTENT_PADDING - 2 * SASS_VARIABLES.WIDGET_CONTENT_MARGIN ) # Hover top br = self.label_icon.rect().bottomRight() tl = self.label_icon.rect().topLeft() + QPoint(1, 1) y = br.y() + self.label_text.height() - 2 # br_new = QPoint(br.x() + 2, y) - QPoint(1, 1) br_new = QPoint(max_width - 1, y) - QPoint(1, 1) rect_hover = QRect(tl, br_new) # 2 is the border pen = QPen(Qt.NoPen) brush = QBrush(Qt.SolidPattern) brush.setColor(QColor('#fff')) painter.setBrush(brush) painter.setPen(pen) painter.drawRect(rect_hover) font = self.font() font.setPointSize(10) painter.setFont(font) pen = QPen() pen.setColor(QColor('black')) painter.setPen(pen) td = self.text_document td.setPageSize(QSizeF(rect_hover.size())) td.setHtml(self.summary) td.drawContents(painter) self.raise_()
def _GetNumPyImage(self, clip_rect: QC.QRect, target_resolution: QC.QSize): if self._numpy_image is None: return numpy.zeros( (target_resolution.height(), target_resolution.width()), dtype='uint8') clip_size = clip_rect.size() clip_width = clip_size.width() clip_height = clip_size.height() (my_width, my_height) = self._resolution my_full_rect = QC.QRect(0, 0, my_width, my_height) ZERO_MARGIN = QC.QMargins(0, 0, 0, 0) clip_padding = ZERO_MARGIN target_padding = ZERO_MARGIN if clip_rect == my_full_rect: # full image source = self._numpy_image else: if target_resolution.width() > clip_width: # this is a tile that is being scaled up! # to reduce tiling artifacts (disagreement at otherwise good borders), we want to oversample the clip for our tile so lanczos and friends can get good neighbour data and then crop it # therefore, we'll figure out some padding for the clip, and then calculate what that means in the target end, and do a crop at the end # we want to pad. that means getting a larger resolution and keeping a record of the padding # can't pad if we are at 0 for x or y, or up against width/height max, but no problem in that case obviously # there is the float-int precision calculation problem again. we can't pick a padding of 3 in the clip if we are zooming by 150%--what do we clip off in the target: 4 or 5 pixels? whatever, we get warping # first let's figure a decent zoom estimate: zoom_estimate = target_resolution.width( ) / clip_width if target_resolution.width( ) > target_resolution.height( ) else target_resolution.height() / clip_height # now, if zoom is 150% (as a fraction, 3/2), we want a padding at the target of something that divides by 3 cleanly, or, since we are choosing at the clip in this case and will be multiplying, something that divides cleanly to 67% zoom_estimate_for_clip_padding_multiplier = 1 / zoom_estimate # and we want a nice padding size limit, big enough to make clean numbers but not so big that we are rendering the 8 tiles in a square around the one we want no_bigger_than = max(4, (clip_width + clip_height) // 4) nice_number = HydrusData.GetNicelyDivisibleNumberForZoom( zoom_estimate_for_clip_padding_multiplier, no_bigger_than) if nice_number != -1: # lanczos, I think, uses 4x4 neighbour grid to render. we'll say padding of 4 pixels to be safe for now, although 2 or 3 is probably correct??? # however it works, numbers these small are not a big deal while nice_number < 4: nice_number *= 2 PADDING_AMOUNT = nice_number # LIMITATION: There is still a problem here for the bottom and rightmost edges. These tiles are not squares, so the shorter/thinner dimension my be an unpleasant number and be warped _anyway_, regardless of nice padding # perhaps there is a way to boost left or top padding so we are rendering a full square tile but still cropping our target at the end, but with a little less warping # I played around with this idea but did not have much success LEFT_PADDING_AMOUNT = PADDING_AMOUNT TOP_PADDING_AMOUNT = PADDING_AMOUNT left_padding = min(LEFT_PADDING_AMOUNT, clip_rect.x()) top_padding = min(TOP_PADDING_AMOUNT, clip_rect.y()) right_padding = min(PADDING_AMOUNT, (my_width - 1) - clip_rect.bottomRight().x()) bottom_padding = min(PADDING_AMOUNT, (my_height - 1) - clip_rect.bottomRight().y()) clip_padding = QC.QMargins(left_padding, top_padding, right_padding, bottom_padding) target_padding = clip_padding * zoom_estimate clip_rect_with_padding = clip_rect + clip_padding (x, y, clip_width, clip_height) = (clip_rect_with_padding.x(), clip_rect_with_padding.y(), clip_rect_with_padding.width(), clip_rect_with_padding.height()) source = self._numpy_image[y:y + clip_height, x:x + clip_width] if target_resolution == clip_size: # 100% zoom result = source else: if clip_padding == ZERO_MARGIN: result = ClientImageHandling.ResizeNumPyImageForMediaViewer( self._mime, source, (target_resolution.width(), target_resolution.height())) else: target_width_with_padding = target_resolution.width( ) + target_padding.left() + target_padding.right() target_height_with_padding = target_resolution.height( ) + target_padding.top() + target_padding.bottom() result = ClientImageHandling.ResizeNumPyImageForMediaViewer( self._mime, source, (target_width_with_padding, target_height_with_padding)) y = target_padding.top() x = target_padding.left() result = result[y:y + target_resolution.height(), x:x + target_resolution.width()] if not result.data.c_contiguous: result = result.copy() return result
def _GetNumPyImage(self, clip_rect: QC.QRect, target_resolution: QC.QSize): clip_size = clip_rect.size() (my_width, my_height) = self._resolution my_full_rect = QC.QRect(0, 0, my_width, my_height) ZERO_MARGIN = QC.QMargins(0, 0, 0, 0) clip_padding = ZERO_MARGIN target_padding = ZERO_MARGIN if clip_rect == my_full_rect: # full image source = self._numpy_image else: if target_resolution.width() > clip_size.width(): # this is a tile that is being scaled! # to reduce tiling artifacts, we want to oversample the clip for our tile so lanczos and friends can get good neighbour data and then crop it # therefore, we'll figure out some padding for the clip, and then calculate what that means in the target end, and do a crop at the end # we want to pad. that means getting a larger resolution and keeping a record of the padding # can't pad if we are at 0 for x or y, or up against width/height max # but if we can pad, we will get a larger clip size and then _clip_ a better target endpoint. this is tricky. PADDING_AMOUNT = 4 left_padding = min(PADDING_AMOUNT, clip_rect.x()) top_padding = min(PADDING_AMOUNT, clip_rect.y()) right_padding = min(PADDING_AMOUNT, my_width - clip_rect.bottomRight().x()) bottom_padding = min(PADDING_AMOUNT, my_height - clip_rect.bottomRight().y()) clip_padding = QC.QMargins(left_padding, top_padding, right_padding, bottom_padding) # this is ugly and super inaccurate target_padding = clip_padding * (target_resolution.width() / clip_size.width()) clip_rect_with_padding = clip_rect + clip_padding (x, y, clip_width, clip_height) = (clip_rect_with_padding.x(), clip_rect_with_padding.y(), clip_rect_with_padding.width(), clip_rect_with_padding.height()) source = self._numpy_image[y:y + clip_height, x:x + clip_width] if target_resolution == clip_size: # 100% zoom result = source else: if clip_padding == ZERO_MARGIN: result = ClientImageHandling.ResizeNumPyImageForMediaViewer( self._mime, source, (target_resolution.width(), target_resolution.height())) else: target_width_with_padding = target_resolution.width( ) + target_padding.left() + target_padding.right() target_height_with_padding = target_resolution.height( ) + target_padding.top() + target_padding.bottom() result = ClientImageHandling.ResizeNumPyImageForMediaViewer( self._mime, source, (target_width_with_padding, target_height_with_padding)) y = target_padding.top() x = target_padding.left() result = result[y:y + target_resolution.height(), x:x + target_resolution.width()] if not result.data.c_contiguous: result = result.copy() return result