Beispiel #1
0
 def sizeHint(self, option, index):
     msg = index.model().data(index, Qt.DisplayRole)
     field = QRect(option.rect)
     field.setWidth(variables.window_width - variables.WINDOW_PADDING)
     field = field.marginsRemoved(variables.TEXT_PADDING)
     doc = QTextDocument(msg.text)
     doc.setDocumentMargin(0)
     opt = QTextOption()
     opt.setWrapMode(opt.WrapAtWordBoundaryOrAnywhere)
     doc.setDefaultTextOption(opt)
     doc.setDefaultFont(variables.font)
     if msg.user == variables.USER_ME:
         doc.setTextWidth(field.size().width() - 20 - 50)
     else:
         doc.setTextWidth(field.size().width() - 20)
     field.setHeight(int(doc.size().height()))
     field.setWidth(int(doc.idealWidth()))
     field = field.marginsAdded(variables.TEXT_PADDING)
     return QSize(0, field.size().height())
Beispiel #2
0
 def paint(self, painter, option, index):
     msg = index.model().data(index, Qt.DisplayRole)
     field = QRect(option.rect)
     field = field.marginsRemoved(variables.TEXT_PADDING)
     doc = QTextDocument(msg.text)
     doc.setDocumentMargin(0)
     opt = QTextOption()
     opt.setWrapMode(opt.WrapAtWordBoundaryOrAnywhere)
     doc.setDefaultTextOption(opt)
     doc.setDefaultFont(variables.font)
     if msg.user == variables.USER_ME:
         doc.setTextWidth(field.size().width() - 20 - 50)
     else:
         doc.setTextWidth(field.size().width() - 20)
     field.setHeight(int(doc.size().height()))
     field.setWidth(int(doc.idealWidth()))
     field = field.marginsAdded(variables.TEXT_PADDING)
     line_height = QFontMetrics(variables.font).lineSpacing(
     ) + variables.TEXT_PADDING.bottom() - variables.BUBBLE_PADDING.bottom(
     ) + variables.TEXT_PADDING.top() - variables.BUBBLE_PADDING.top()
     if msg.user == variables.USER_ME:
         rect = QRect(option.rect.right() - field.size().width() - 20,
                      option.rect.top(),
                      field.size().width(),
                      field.size().height())
     else:
         rect = QRect(20, option.rect.top(),
                      field.size().width(),
                      field.size().height())
     bubblerect = rect.marginsRemoved(variables.BUBBLE_PADDING)
     textrect = rect.marginsRemoved(variables.TEXT_PADDING)
     if msg.user == variables.USER_ME:
         p1 = bubblerect.topRight()
         p2 = bubblerect.bottomLeft() + QPoint(-36, -int(line_height / 2))
     else:
         p1 = bubblerect.topLeft()
     painter.setRenderHint(QPainter.Antialiasing)
     painter.setPen(Qt.NoPen)
     color = QColor(variables.BUBBLE_COLORS[msg.user])
     painter.setBrush(color)
     painter.drawRoundedRect(bubblerect, 10, 10)
     painter.drawPolygon(p1 + QPoint(-20, 0), p1 + QPoint(20, 0),
                         p1 + QPoint(0, 15))
     if msg.user == variables.USER_ME:
         if msg.status == variables.STATUS_UNREAD:
             painter.setPen(Qt.NoPen)
             painter.setBrush(QColor(variables.STATUS_COLOR))
             painter.drawEllipse(p2, 7, 7)
         elif msg.status == variables.STATUS_UNDELIVERED:
             pen = QPen(QColor(variables.STATUS_COLOR))
             pen.setWidth(2)
             painter.setPen(pen)
             painter.setBrush(Qt.NoBrush)
             painter.drawEllipse(p2, 7, 7)
             painter.drawLine(p2, p2 + QPoint(0, -5))
             painter.drawLine(p2, p2 + QPoint(3, 0))
     painter.setPen(Qt.gray)
     painter.setFont(variables.font)
     painter.translate(textrect.x(), textrect.y())
     textrectf = QRectF(textrect)
     textrectf.moveTo(0, 0)
     doc.drawContents(painter, textrectf)
     painter.translate(-textrect.x(), -textrect.y())
Beispiel #3
0
    def paint(self, painter, option, index):
        dlg = index.model().data(index, Qt.DisplayRole)
        painter.setRenderHint(QPainter.Antialiasing)
        color = QColor(Qt.white)
        if option.state & QStyle.State_MouseOver:
            color = QColor(variables.HIGHLIGHT_COLOR)
            painter.setPen(QPen(color))
            painter.setBrush(QBrush(color))
            painter.drawRect(option.rect)
        painter.setPen(Qt.black)
        painter.setBrush(Qt.NoBrush)
        painter.drawLine(option.rect.bottomLeft(), option.rect.bottomRight())
        doc = QTextDocument(dlg.ip)
        doc.setDocumentMargin(0)
        doc.setDefaultFont(variables.font)
        textrect = QRect(option.rect)
        textrect = textrect.marginsRemoved(variables.IP_PADDING)
        textrect.setHeight(int(doc.size().height()))
        textrect.setWidth(int(doc.size().width()))
        offset = textrect.marginsAdded(variables.IP_PADDING).height()
        painter.setPen(Qt.black)
        painter.setFont(variables.font)
        painter.translate(textrect.x(), textrect.y())
        textrectf = QRectF(textrect)
        textrectf.moveTo(0, 0)
        doc.drawContents(painter, textrectf)
        painter.translate(-textrect.x(), -textrect.y())
        string = ""
        if dlg.user == variables.USER_ME:
            string += "You: "
        print(dlg.msg)
        string += dlg.msg
        fm = QFontMetrics(variables.font_small)
        textrect = QRect(option.rect)
        textrect.moveTop(textrect.y() + offset)
        textrect = textrect.marginsRemoved(variables.MSG_PADDING)
        textrect.setHeight(fm.lineSpacing())
        spainter = QStylePainter(painter.device(), QWidget())
        spainter.setRenderHint(QPainter.Antialiasing)
        if fm.horizontalAdvance(string) > textrect.width():
            fade = QLinearGradient(variables.MSG_PADDING.left() + textrect.width()* 0.9, 0, variables.MSG_PADDING.left() + textrect.width(), 0)
            fade.setSpread(QGradient.PadSpread)
            fade.setColorAt(0, Qt.darkGray)
            fade.setColorAt(1, color)
            pal = QPalette()
            pal.setBrush(QPalette.Text, QBrush(fade))
            spainter.setFont(variables.font_small)
            spainter.drawItemText(textrect, Qt.TextSingleLine, pal, True, string, QPalette.Text)
        else:
            spainter.setPen(Qt.darkGray)
            spainter.setFont(variables.font_small)
            spainter.drawText(textrect, Qt.TextSingleLine, string)
        p1 = textrect.bottomRight() + QPoint(36, -int(textrect.height() / 2))
        if dlg.status == variables.STATUS_UNREAD:
            spainter.setPen(Qt.NoPen)
            spainter.setBrush(QColor(variables.STATUS_COLOR))
            spainter.drawEllipse(p1, 7, 7)
        elif dlg.status == variables.STATUS_UNDELIVERED:
            pen = QPen(QColor(variables.STATUS_COLOR))
            pen.setWidth(2)
            spainter.setPen(pen)
            spainter.setBrush(Qt.NoBrush)
            spainter.drawEllipse(p1, 7, 7)
            spainter.drawLine(p1, p1 + QPoint(0, -5))
            spainter.drawLine(p1, p1 + QPoint(3, 0))
        elif dlg.status == variables.STATUS_NEW:
            pen = QPen(QColor(variables.STATUS_NEW_COLOR))
            pen.setWidth(5)
            spainter.setPen(pen)
            spainter.setBrush(Qt.NoBrush)
            spainter.drawEllipse(p1, 7, 7)
        #painter.translate(-textrect.x(), -textrect.y())

        '''doc = QTextDocument(str)