1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| window_height = 10 begin_width_spacing = 20 begin_height_spacing = 16 icon_width = 40 icon_height = 40 text_width_spacing = 12 text_height_spacing = 12 triangle_width = 6 triangle_height = 10 triangle_height_spacing = 10 text_min_width = 0 min_width = 0 text_max_width = 0 real_width = 0 text_height = 0
class MessageItemWidget(QWidget):
def __init__(self,parent=None,fromUser="",content=""): super(MessageItemWidget,self).__init__(parent=parent) self.content=content
def paintEvent(self, event): self.init_data() global text_min_width, min_width, text_max_width, real_width, text_height, window_height painter = QtGui.QPainter(self) painter.setRenderHints(QtGui.QPainter.Antialiasing | QtGui.QPainter.SmoothPixmapTransform) font = QtGui.QFont() font.setFamily("实体") font.setPointSize(12) painter.setFont(font) bubbleRect = QRect(begin_width_spacing + icon_width, begin_height_spacing, triangle_width + text_width_spacing + text_max_width + text_width_spacing, text_height_spacing + text_height + text_height_spacing) painter.setPen(Qt.NoPen) painter.setBrush(QtGui.QBrush(QtGui.QColor(180, 180, 180))) painter.drawRoundedRect(bubbleRect.x() + triangle_width, bubbleRect.y(), bubbleRect.width() - triangle_width, bubbleRect.height(), 18, 18) linearGradient = QtGui.QLinearGradient(QPointF(bubbleRect.x() + triangle_width + 1, bubbleRect.y() + 1), QPointF(bubbleRect.x() + bubbleRect.width() - 1, bubbleRect.y() + bubbleRect.height() - 1)) linearGradient.setColorAt(0, QtGui.QColor(151, 220, 227)) linearGradient.setColorAt(0.25, QtGui.QColor(151, 220, 227)) linearGradient.setColorAt(0.5, QtGui.QColor(151, 220, 227)) linearGradient.setColorAt(0.75, QtGui.QColor(151, 220, 227)) linearGradient.setColorAt(1, QtGui.QColor(151, 220, 227)) painter.setBrush(linearGradient) painter.drawRoundedRect(bubbleRect.x() + triangle_width + 1, bubbleRect.y() + 1, bubbleRect.width() - triangle_width - 2, bubbleRect.height() - 2, 18, 18) painter.setPen(QtGui.QPen(QtGui.QColor(244, 164, 96))) painter.drawPolygon(QPointF(bubbleRect.x(), bubbleRect.y() + triangle_height_spacing - 4), QPointF(bubbleRect.x() + triangle_width + 1, bubbleRect.y() + triangle_height_spacing), QPointF(bubbleRect.x() + 6 + 1, bubbleRect.y() + 10 + 10))
painter.setPen(QtGui.QPen(QtGui.QColor(180, 180, 180))) painter.drawLine(QPointF(bubbleRect.x(), bubbleRect.y() + 10 - 4), QPointF(bubbleRect.x() + 6, bubbleRect.y() + 10)) painter.drawLine(QPointF(bubbleRect.x(), bubbleRect.y() + 10 - 4), QPointF(bubbleRect.x() + 6, bubbleRect.y() + 10 + 10)) penText = QtGui.QPen() penText.setColor(QtGui.QColor(56, 56, 56)) painter.setPen(penText) option = QtGui.QTextOption(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter) option.setWrapMode(QtGui.QTextOption.WrapMode.WrapAtWordBoundaryOrAnywhere) painter.drawText( QRectF(bubbleRect.x() + triangle_width + text_width_spacing, bubbleRect.y() + text_height_spacing, text_max_width, text_height), self.content, option) def init_data(self): font = QtGui.QFont() font.setFamily("实体") font.setPointSize(12) metrics = QtGui.QFontMetrics(font) global text_min_width, min_width,text_max_width,real_width, text_height, window_height if metrics.horizontalAdvance("A") * 2 + begin_height_spacing * 1.5 > text_width_spacing: text_min_width = begin_height_spacing * 1.5 - text_width_spacing else: text_min_width = 0 min_width = begin_width_spacing + icon_width + triangle_width + text_width_spacing + text_width_spacing + icon_width + begin_width_spacing if self.width()< min_width+text_min_width: self.setMinimumWidth(min_width + text_min_width) text_max_width = self.width() - min_width real_width = metrics.width(self.content) if real_width < text_max_width: text_max_width = real_width if text_height_spacing+metrics.height()+text_height_spacing>triangle_height_spacing+triangle_height+triangle_height_spacing: text_height = metrics.height() else: text_height = triangle_height_spacing + triangle_height + triangle_height_spacing else: flag = Qt.TextFlag.TextWordWrap textRect = QRect(0, 0, text_max_width, 0) textRect = metrics.boundingRect(textRect, flag, self.content) text_height = textRect.height()
|