class ImageViewer(QWidget): def __init__(self): super(ImageViewer, self).__init__() self.painter = QPainter() self.my_pen = QPen(QColor("red")) self.my_pen.setWidth(5) self.my_brush = QBrush(QColor("#123456")) self.photo = QPixmap() self.photo_rect = QRect() def set_pixmap(self, image_path): self.photo.load(image_path) self.repaint() def paintEvent(self, event): self.painter.begin(self) self.draw() self.painter.end() def draw(self): rect = self.rect() self.painter.setPen(self.my_pen) self.painter.setBrush(self.my_brush) photo = self.photo.scaled(QSize(rect.width(), rect.height()), Qt.KeepAspectRatio, Qt.SmoothTransformation) self.photo_rect.setRect(rect.x(), rect.y(), photo.width(), photo.height()) self.photo_rect.moveCenter(rect.center()) self.painter.drawPixmap(self.photo_rect, photo)
def draw(self): rect = self.rect() scaled_image = self.pixmap.scaledToWidth(rect.width(), Qt.SmoothTransformation) if scaled_image.height() < rect.height(): scaled_image = self.pixmap.scaledToHeight(rect.height(), Qt.SmoothTransformation) image_rect = QRect(rect.x(), rect.y(), scaled_image.width(), scaled_image.height()) image_rect.moveCenter(rect.center()) self.painter.drawPixmap(image_rect, scaled_image) self.painter.setBrush(self.fill_brush) self.painter.setPen(Qt.NoPen) self.painter.drawRect(rect)
def mouseMoveEvent(self, event: QMouseEvent): """ Event: Don't Invoke This Method Manually """ mpos = event.pos() # Mouse Position if self.__is_selection_enabled or self.__is_resize_enable: # Prevent Selection From Outside Canvas; if mpos.x() > self.width(): mpos.setX(self.width()) if mpos.y() > self.height(): mpos.setY(self.height()) if not self.__ratio: # -> Set Selection Rect's End Position; self.__sRect.setBottomRight(mpos) else: # -> Calclulate & Apply Ratio; #-----------[ Can Be Improve ]-----------# width_as_ratio = round(self.__sRect.height() * self.__ratio) # Ratio Appled; self.__sRect.setWidth(width_as_ratio) self.__sRect.setBottom(mpos.y()) elif self.__is_move_enable: # -> Move Selection Rect; rectRef = QRect(self.__sRect) rectRef.moveCenter(mpos) if rectRef.x() < 0 or rectRef.right() > self.width(): mpos.setX(self.__sRect.center().x()) if rectRef.y() < 0 or rectRef.bottom() > self.height(): mpos.setY(self.__sRect.center().y()) self.__sRect.moveCenter(mpos) """``````````````````````````````````````````````````""" # -> Cursor Pointer Logic; if self.getResizeHandle.contains(event.x(), event.y()): self.setCursor(Qt.SizeFDiagCursor) elif self.getMoveHandle.contains(event.x(), event.y()): self.setCursor(Qt.SizeAllCursor) else: self.setCursor(Qt.ArrowCursor) # -> Update Widget; self.update()
def paint(self, painter, option, index): rect = option.rect poster_file = index.data(Qt.UserRole) self.poster_pixmap.load(poster_file) poster_file_rescaled = self.poster_pixmap.scaled( MovieItem.poster_size, Qt.KeepAspectRatio, Qt.SmoothTransformation) painter.setPen(self.outline_pen) painter.setBrush(self.background_brush) painter.drawRect(rect) # posters poster_rect = QRect(rect.x(), rect.y(), poster_file_rescaled.width(), poster_file_rescaled.height()) poster_rect.moveCenter(rect.center()) painter.drawPixmap(poster_rect, poster_file_rescaled) if option.state & QStyle.State_Selected: painter.setBrush(self.selected_brush) painter.drawRect(rect)
def draw_icon(self, painter: QPainter, rect: QRect, icon: QIcon): r = QRect(0, 0, 20, 20) r.moveCenter(rect.center()) painter.drawPixmap(r, icon.pixmap(20, 20))
class VideoFrameGrabber(QAbstractVideoSurface): frameAvailable = QSignalTransition.setSignal(QImage) # frameAvailable.append() # frameAvailable = QSignalTransition.signal(QImage) def __init__(self, widget: QWidget, parent: QObject): super().__init__(parent) self.widget = widget def supportedPixelFormats(self, handleType): return [ QVideoFrame.Format_ARGB32, QVideoFrame.Format_ARGB32_Premultiplied, QVideoFrame.Format_RGB32, QVideoFrame.Format_RGB24, QVideoFrame.Format_RGB565, QVideoFrame.Format_RGB555, QVideoFrame.Format_ARGB8565_Premultiplied, QVideoFrame.Format_BGRA32, QVideoFrame.Format_BGRA32_Premultiplied, QVideoFrame.Format_BGR32, QVideoFrame.Format_BGR24, QVideoFrame.Format_BGR565, QVideoFrame.Format_BGR555, QVideoFrame.Format_BGRA5658_Premultiplied, QVideoFrame.Format_AYUV444, QVideoFrame.Format_AYUV444_Premultiplied, QVideoFrame.Format_YUV444, QVideoFrame.Format_YUV420P, QVideoFrame.Format_YV12, QVideoFrame.Format_UYVY, QVideoFrame.Format_YUYV, QVideoFrame.Format_NV12, QVideoFrame.Format_NV21, QVideoFrame.Format_IMC1, QVideoFrame.Format_IMC2, QVideoFrame.Format_IMC3, QVideoFrame.Format_IMC4, QVideoFrame.Format_Y8, QVideoFrame.Format_Y16, QVideoFrame.Format_Jpeg, QVideoFrame.Format_CameraRaw, QVideoFrame.Format_AdobeDng ] def isFormatSupported(self, format): imageFormat = QVideoFrame.imageFormatFromPixelFormat( format.pixelFormat()) size = format.frameSize() return imageFormat != QImage.Format_Invalid and not size.isEmpty() and \ format.handleType() == QAbstractVideoBuffer.NoHandle def start(self, format: QVideoSurfaceFormat): imageFormat = QVideoFrame.imageFormatFromPixelFormat( format.pixelFormat()) size = format.frameSize() if imageFormat != QImage.Format_Invalid and not size.isEmpty(): self.imageFormat = imageFormat self.imageSize = size self.sourceRect = format.viewport() super().start(format) self.widget.updateGeometry() self.updateVideoRect() return True else: return False def stop(self): self.currentFrame = QVideoFrame() self.targetRect = QRect() super().stop() self.widget.update() def present(self, frame): if frame.isValid(): cloneFrame = QVideoFrame(frame) cloneFrame.map(QAbstractVideoBuffer.ReadOnly) image = QImage( cloneFrame.bits(), cloneFrame.width(), cloneFrame.height(), QVideoFrame.imageFormatFromPixelFormat( cloneFrame.pixelFormat())) self.frameAvailable.emit(image) # this is very important cloneFrame.unmap() if self.surfaceFormat().pixelFormat() != frame.pixelFormat() or \ self.surfaceFormat().frameSize() != frame.size(): self.setError(QAbstractVideoSurface.IncorrectFormatError) self.stop() return False else: self.currentFrame = frame self.widget.repaint(self.targetRect) return True def updateVideoRect(self): size = self.surfaceFormat().sizeHint() size.scale(self.widget.size().boundedTo(size), Qt.KeepAspectRatio) self.targetRect = QRect(QPoint(0, 0), size) self.targetRect.moveCenter(self.widget.rect().center()) def paint(self, painter): if self.currentFrame.map(QAbstractVideoBuffer.ReadOnly): oldTransform = self.painter.transform() if self.surfaceFormat().scanLineDirection( ) == QVideoSurfaceFormat.BottomToTop: self.painter.scale(1, -1) self.painter.translate(0, -self.widget.height()) image = QImage(self.currentFrame.bits(), self.currentFrame.width(), self.currentFrame.height(), self.currentFrame.bytesPerLine(), self.imageFormat) self.painter.drawImage(self.targetRect, image, self.sourceRect) self.painter.setTransform(oldTransform) self.currentFrame.unmap()
def paint(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex): """Apply graphical formatting to each item in each displayed column in the view""" brush = QBrush() pen = QPen() font = QFont() if option.state & QStyle.State_Selected: text_color = option.palette.color(QPalette.Normal, QPalette.BrightText) else: text_color = option.palette.color(QPalette.Normal, QPalette.Text) is_selected = option.state & QStyle.State_Selected # Default theme color pen.setColor(text_color) field_name = self.field_name(index).lower() value = self.value(index) # Colour bases (default color is the one of the current theme) if (field_name == "ref" or field_name == "alt") and (value in ("A", "C", "G", "T") and not is_selected): pen.setColor( self.BASE_COLOR.get(value, option.palette.color(QPalette.WindowText))) if field_name == "impact" and not is_selected: font.setBold(True) pen.setColor( self.IMPACT_COLOR.get(value, self.IMPACT_COLOR["MODIFIER"])) if field_name == "gene" and not is_selected: pen.setColor("#6a9fca") if field_name == "classification": icon = self.ACMG_ICON.get(str(value), self.ACMG_ICON["0"]) self.draw_icon(painter, option.rect, icon) return if field_name == "favorite": icon = self.FAV_ICON.get(int(value), self.FAV_ICON[0]) self.draw_icon(painter, option.rect, icon) return if field_name == "hgvs_c": font.setBold(True) m = re.search(r"([cnm]\..+)", str(value)) if m: value = m.group(1) if field_name == "hgvs_p": font.setBold(True) m = re.search(r"(p\..+)", str(value)) if m: value = m.group(1) if re.match(r"sample\[.+\]\.gt", field_name): icon = self.GENOTYPE_ICONS.get(int(value), self.GENOTYPE_ICONS[-1]) self.draw_icon(painter, option.rect, icon) return if field_name == "consequence": values = str(self.value(index)).split("&") metrics = QFontMetrics(font) x = option.rect.x() + 5 # y = option.rect.center().y() for value in values: width = metrics.width(value) height = metrics.height() rect = QRect(x, 0, width + 15, height + 10) rect.moveCenter(option.rect.center()) rect.moveLeft(x) painter.setFont(font) painter.setClipRect(option.rect, Qt.IntersectClip) painter.setBrush( QBrush(QColor(self.SO_COLOR.get(value, "#90d4f7")))) painter.setPen(Qt.NoPen) painter.drawRoundedRect(rect, 3, 3) painter.setPen(QPen(QColor("white"))) painter.drawText(rect, Qt.AlignCenter | Qt.AlignVCenter, value) x += width + 20 painter.setClipping(False) return if field_name == "rsid": self.draw_url(painter, option.rect, value, QUrl("http://www.google.fr"), index) return painter.setBrush(brush) painter.setPen(pen) painter.setFont(font) painter.drawText(option.rect, option.displayAlignment, value)