def paint(self, painter: QtGui.QPainter, option: QStyleOptionViewItem, index: QtCore.QModelIndex) -> None: item_size = option.rect.size() custom_decoration_w, custom_decoration_h = index.data( SlideListModel.DecorationSizeOrRatioRole) if option.decorationPosition == QStyleOptionViewItem.Left: text_x, text_y = custom_decoration_w + self.icon_and_text_spacing, 0 text_width = item_size.width( ) - custom_decoration_w - self.icon_and_text_spacing text_height = custom_decoration_h elif option.decorationPosition == QStyleOptionViewItem.Top: text_size = super().sizeHint(option, index) custom_decoration_h = custom_decoration_h - text_size.height() text_x, text_y = 0, custom_decoration_h + self.icon_and_text_spacing text_width = custom_decoration_w text_height = item_size.height( ) - custom_decoration_h - self.icon_and_text_spacing slide_view_params: SlideViewParams = index.data( SlideListModel.SlideViewParamsRole) scene_rect = QRectF(*slide_view_params.level_rect) img_key = "{}_{}_{}".format(custom_decoration_w, custom_decoration_h, slide_view_params.cache_key()) icon_pixmap = QPixmapCache.find(img_key) if icon_pixmap is None: slide_helper = SlideHelper(slide_view_params.slide_path) # print("read", img_key) scene = QGraphicsScene() # t1 = elapsed() # print("before slide_graphics", t1) slide_graphics = SlideGraphicsGroup(slide_view_params) # t2 = elapsed() # print("slide_graphics", t2 - t1) scene.clear() scene.invalidate() scene.addItem(slide_graphics) slide_graphics.update_visible_level(slide_view_params.level) scene.setSceneRect( slide_helper.get_rect_for_level(slide_view_params.level)) image = build_screenshot_image( scene, QSize(custom_decoration_w, custom_decoration_h), scene_rect) # t3 = elapsed() # print("build_screenshot_image", t3 - t2) icon_pixmap = QtGui.QPixmap.fromImage(image) QPixmapCache.insert(img_key, icon_pixmap) # t4 = elapsed() painter.fillRect(option.rect, painter.background()) painter.drawPixmap(option.rect.topLeft(), icon_pixmap) # painter.drawRect(option.rect) painter.drawRect(option.rect.topLeft().x(), option.rect.topLeft().y(), icon_pixmap.width(), icon_pixmap.height()) option.rect = option.rect.translated(text_x, text_y) option.rect.setSize(QSize(text_width, text_height)) super().paint(painter, option, index)
def paintCell(self, painter: QtGui.QPainter, rect: QtCore.QRect, date: typing.Union[QtCore.QDate, datetime.date]) -> None: painter.save() with open(get_resource("config.json")) as file: if date.toPyDate().strftime("%Y-%m-%d") in json.loads( file.read())["favorites"]: painter.fillRect(rect, QtGui.QColor.fromRgb(255, 255, 0)) if (date.month() != self.monthShown()): painter.setPen(QtGui.QColor("#888888")) elif date.dayOfWeek() == 6 or date.dayOfWeek() == 7: painter.setPen(QtGui.QColor("red")) tags = self.get_tags_from_date_file(date.toPyDate()) rect.adjust(0, 0, -1, -1) pen = painter.pen() pen.setColor( QtGui.QColor.fromHsl(pen.color().hue(), pen.color().saturation(), pen.color().lightness(), 150)) painter.setPen(pen) painter.drawRect(rect) pen.setColor( QtGui.QColor.fromHsl(pen.color().hue(), pen.color().saturation(), pen.color().lightness(), 255)) painter.setPen(pen) painter.drawText(rect, QtCore.Qt.AlignTop, str(date.day())) text = "" try: for tag in tags[:5]: if len(tag) > 12: tag = str(tag[:12]) + "..." text += f" {tag} \n" except TypeError: text = "" font = QtGui.QFont() font.setPixelSize(10) painter.setFont(font) brush = painter.background() random.seed(date) brush.setColor(QtGui.QColor().fromHsl(randint(0, 255), randint(0, 255), randint(200, 255))) painter.setPen(QtGui.QColor("black")) painter.setBackground(brush) painter.setBackgroundMode(QtCore.Qt.OpaqueMode) painter.drawText(rect, QtCore.Qt.AlignBottom | QtCore.Qt.AlignHCenter, text) painter.restore()
def paint_screenshot_image( painter: QPainter, scene: QGraphicsScene, image_size: QSize, scene_rect: QRectF = QRectF(), transparent_background=False, ) -> QImage: # with elapsed_timer() as elapsed: painter.fillRect(QRect(QPoint(0, 0), image_size), painter.background().color()) scene_items = scene.items(scene_rect, Qt.IntersectsItemBoundingRect) only_leaf_items = [ item for item in scene_items if len(item.childItems()) == 0 ] item_parents = {} item_groups = {} for item in only_leaf_items: if item.group(): item_groups[item] = item.group() item.group().setVisible(False) elif item.parentItem(): item_parents[item] = item.parentItem() item.parentItem().setVisible(False) group_for_screenshot = QGraphicsItemGroup() for item in only_leaf_items: group_for_screenshot.addToGroup(item) scene.addItem(group_for_screenshot) group_for_screenshot.setVisible(True) # scene_items2 = scene.items(scene_rect, Qt.IntersectsItemBoundingRect) # print(scene_items2) # print("before render==========================================") rendered_size = scene_rect.size().scaled(QSizeF(image_size), Qt.KeepAspectRatio) dsize = QSizeF(image_size) - rendered_size top_left = QPointF(dsize.width() / 2, dsize.height() / 2) scene.render(painter, QRectF(top_left, rendered_size), scene_rect, Qt.KeepAspectRatio) scene.destroyItemGroup(group_for_screenshot) for item in item_parents: parent = item_parents[item] item.setParentItem(parent) parent.setVisible(True) for item in item_groups: group = item_groups[item] group.addToGroup(item) group.setVisible(True)
def paintEvent(self, event): # From Peter, thanks ! # http://thecodeinn.blogspot.fr/2015/02/customizing-qdials-in-qt-part-1.html painter = QPainter(self) # So that we can use the background color painter.setBackgroundMode(1) # Smooth out the circle painter.setRenderHint(QPainter.Antialiasing) # Use background color painter.setBrush(painter.background()) # Store color from stylesheet, pen will be overriden pointColor = QColor(painter.pen().color()) # No border painter.setPen(QPen(Qt.NoPen)) # Draw first circle painter.drawEllipse(0, 0, self.width(), self.height()) # Reset color to pointColor from stylesheet painter.setBrush(QBrush(pointColor)) # Get ratio between current value and maximum to calculate angle ratio = self.value() / self.maximum() # The maximum amount of degrees is 270, offset by 225 angle = ratio * self._degree270 - self._degree225 # Radius of background circle rx = self.width() / 2 ry = self.height() / 2 # Add r to have (0,0) in center of dial y = sin(angle) * (ry - self.knobRadius - self.knobMargin) + ry x = cos(angle) * (rx - self.knobRadius - self.knobMargin) + rx # Draw the ellipse painter.drawEllipse(QPointF(x, y), self.knobRadius, self.knobRadius)
def slidepath_to_pximap(slidepath, icon_size: QSize): img_key = "{}_{}".format(slidepath, str(icon_size)) icon_pixmap = QPixmapCache.find(img_key) if icon_pixmap is None: # pilimg: Image.Image = Image.open(slidepath) with openslide.open_slide(slidepath) as slide: pilimg = slide.get_thumbnail( (icon_size.width(), icon_size.height())) icon_image = QImage(icon_size, QImage.Format_RGB888) painter = QPainter(icon_image) painter.fillRect(icon_image.rect(), painter.background()) img = ImageQt(pilimg) scaled_icon_image = img.scaled(icon_size, Qt.KeepAspectRatio) p = QPoint((icon_size.width() - scaled_icon_image.width()) / 2, (icon_size.height() - scaled_icon_image.height()) / 2) painter.drawImage(p, scaled_icon_image) painter.end() icon_pixmap = QPixmap.fromImage(icon_image) QPixmapCache.insert(img_key, icon_pixmap) return icon_pixmap
def paintEvent(self, event): if self._drawing: painter = QPainter(self._page.drawing_image) painter.setRenderHint(QPainter.HighQualityAntialiasing, True) if self._toolMode == ToolMode.ERASER: painter.setPen(Qt.NoPen) painter.setBrush(painter.background()) r = int(self._eraserSize / 2) painter.drawEllipse(self._drawPoint, r, r) if self._toolMode == ToolMode.PEN: painter.setPen(self._pen) if self._lastPenPos is None: painter.drawPoint(self._drawPoint) else: painter.drawLine(self._lastPenPos, self._drawPoint) painter.end() self.updateDrawingLayer() super(PageViewer, self).paintEvent(event)