chatlistitem.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "chatlistitem.h"
  2. #include "ui_chatlistitem.h"
  3. ChatListItem::ChatListItem(QWidget* parent)
  4. : QWidget(parent)
  5. , ui(new Ui::ChatListItem)
  6. {
  7. ui->setupUi(this);
  8. m_iconAlign = IaRight;
  9. m_textBackColor = QColor(158, 234, 106);
  10. }
  11. ChatListItem::~ChatListItem()
  12. {
  13. delete ui;
  14. }
  15. void ChatListItem::paintEvent(QPaintEvent* event)
  16. {
  17. QPainter painter;
  18. painter.begin(this);
  19. QRect iconRc, infoRc;
  20. QFontMetrics fm = painter.fontMetrics();
  21. int infoWidth = fm.width(m_info) + 24;
  22. if (m_iconAlign == IaLeft) {
  23. iconRc = QRect(12, 6, 24, 24);
  24. infoRc = QRect(48, 6, infoWidth, 24);
  25. } else {
  26. iconRc = QRect(width() - 36, 6, 24, 24);
  27. infoRc = QRect(width() - 48 - infoWidth, 6, infoWidth, 24);
  28. }
  29. painter.drawPixmap(iconRc, m_icon, m_icon.rect());
  30. painter.setPen(Qt::NoPen);
  31. painter.setBrush(m_textBackColor);
  32. painter.drawRoundRect(infoRc, 10, 10);
  33. painter.setPen(Qt::black);
  34. painter.drawText(infoRc, Qt::AlignCenter, m_info);
  35. painter.end();
  36. }
  37. QColor ChatListItem::textBackColor() const
  38. {
  39. return m_textBackColor;
  40. }
  41. void ChatListItem::setTextBackColor(const QColor& textBackColor)
  42. {
  43. m_textBackColor = textBackColor;
  44. }
  45. IconAlign ChatListItem::iconAlign() const
  46. {
  47. return m_iconAlign;
  48. }
  49. void ChatListItem::setIconAlign(const IconAlign& iconAlign)
  50. {
  51. m_iconAlign = iconAlign;
  52. update();
  53. }
  54. QString ChatListItem::info() const
  55. {
  56. return m_info;
  57. }
  58. void ChatListItem::setInfo(const QString& info)
  59. {
  60. m_info = info;
  61. }
  62. QPixmap ChatListItem::icon() const
  63. {
  64. return m_icon;
  65. }
  66. void ChatListItem::setIcon(const QPixmap& icon)
  67. {
  68. m_icon = icon;
  69. }